From e7187162a60a48ac2e43c4d6a00243f3af10a170 Mon Sep 17 00:00:00 2001 From: rubyist Date: Wed, 4 Jun 2014 10:29:19 -0400 Subject: [PATCH] Add the pre-push hook setup --- gitmedia/setup.go | 17 +++++++++++++++-- integration/empty_test.go | 28 ++++++++++++++++++++++++++++ man/git-media-init.1.ronn | 1 + 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/gitmedia/setup.go b/gitmedia/setup.go index 901ca0da7c..6e06c719f8 100644 --- a/gitmedia/setup.go +++ b/gitmedia/setup.go @@ -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 } @@ -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.") } } diff --git a/integration/empty_test.go b/integration/empty_test.go index fccda5e216..59d0aadd7a 100644 --- a/integration/empty_test.go +++ b/integration/empty_test.go @@ -3,6 +3,7 @@ package main import ( "fmt" "github.com/bmizerany/assert" + "io/ioutil" "os" "path/filepath" "strings" @@ -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) @@ -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() diff --git a/man/git-media-init.1.ronn b/man/git-media-init.1.ronn index 42c75659bd..0275a7649f 100644 --- a/man/git-media-init.1.ronn +++ b/man/git-media-init.1.ronn @@ -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