Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the pre-push hook setup #62

Merged
merged 1 commit into from
Jun 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions gitmedia/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@ import (
"errors"
"fmt"
"github.com/github/git-media/gitconfig"
"io/ioutil"
"os"
"path/filepath"
"regexp"
)

var valueRegexp = regexp.MustCompile("\\Agit[\\-\\s]media")
var (
valueRegexp = regexp.MustCompile("\\Agit[\\-\\s]media")
prePushHook = []byte("#!/bin/sh\ngit media push\n")
)

func InstallHooks() error {
if !InRepo() {
return errors.New("Not in a repository")
}

hookPath := filepath.Join(LocalGitDir, "hooks", "pre-push")
if _, err := os.Stat(hookPath); err == nil {
Print("Hook already exists: %s", hookPath)
} else {
ioutil.WriteFile(hookPath, prePushHook, 0755)
}

return nil
}

Expand Down Expand Up @@ -46,7 +59,7 @@ func requireFilters() {
gitconfig.UnsetGlobal(key)
gitconfig.SetGlobal(key, value)
} else if existing != value {
Print("Media filters should be required but are not")
Print("Media filters should be required but are not.")
}
}

Expand Down
28 changes: 28 additions & 0 deletions integration/empty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/bmizerany/assert"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -34,7 +35,10 @@ func TestEmptyRepository(t *testing.T) {
"Installing smudge filter\n" +
"git media initialized"

prePushHookFile := filepath.Join(repo.Path, ".git", "hooks", "pre-push")

cmd.After(func() {
// assert media filter config
configs := GlobalGitConfig(t)
fmt.Println(configs)
AssertIncludeString(t, "filter.media.clean=git media clean %f", configs)
Expand All @@ -47,6 +51,30 @@ func TestEmptyRepository(t *testing.T) {
}
}
assert.Equal(t, 3, found)

// assert hooks
stat, err := os.Stat(prePushHookFile)
assert.Equal(t, nil, err)
assert.Equal(t, false, stat.IsDir())
})

cmd = repo.Command("init")
cmd.Output = "Installing clean filter\n" +
"Installing smudge filter\n" +
"Hook already exists: " +
filepath.Join(repo.Path, ".git", "hooks", "pre-push") +
"\ngit media initialized"

customHook := []byte("echo 'yo'")
cmd.Before(func() {
err := ioutil.WriteFile(prePushHookFile, customHook, 0755)
assert.Equal(t, nil, err)
})

cmd.After(func() {
by, err := ioutil.ReadFile(prePushHookFile)
assert.Equal(t, nil, err)
assert.Equal(t, string(customHook), string(by))
})

repo.Test()
Expand Down
1 change: 1 addition & 0 deletions man/git-media-init.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ git-media-init(1) -- Ensure Git Media is configured properly.
Init performs the following actions to ensure Git Media is setup properly:

* Sets up the clean and smudge filters under the name "media".
* Installs a pre-push hook to run git-media-push(1)

## SEE ALSO

Expand Down