only remove pre-push if it's a known git lfs pre-push hook

This commit is contained in:
risk danger olson 2015-07-10 10:53:06 -06:00
parent 8407e01697
commit 6eaa77c5aa
2 changed files with 5 additions and 18 deletions

@ -1,7 +1,6 @@
package lfs
import (
"bufio"
"errors"
"fmt"
"io"
@ -65,32 +64,20 @@ func UninstallHooks() error {
return nil
}
hasLfs, err := fileHasGitLfs(file)
by, err := ioutil.ReadAll(io.LimitReader(file, 1024))
file.Close()
if err != nil {
return err
}
if hasLfs {
contents := strings.TrimSpace(string(by))
if contents == prePushHook || prePushUpgrades[contents] {
return os.RemoveAll(prePushHookPath)
}
return nil
}
var gitLfsRE = regexp.MustCompile("git[- ]lfs")
func fileHasGitLfs(file *os.File) (bool, error) {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if gitLfsRE.MatchString(scanner.Text()) {
return true, nil
}
}
return false, scanner.Err()
}
func upgradeHookOrError(hookPath, hookName, hook string, upgrades map[string]bool) error {
file, err := os.Open(hookPath)
if err != nil {

@ -53,11 +53,11 @@ begin_test "uninit inside repository without git lfs pre-push hook"
cd "$reponame"
git init
git lfs init
echo "something else" > .git/hooks/pre-push
echo "something something git-lfs" > .git/hooks/pre-push
[ -f .git/hooks/pre-push ]
[ "something else" = "$(cat .git/hooks/pre-push)" ]
[ "something something git-lfs" = "$(cat .git/hooks/pre-push)" ]
[ "git-lfs smudge %f" = "$(git config filter.lfs.smudge)" ]
[ "git-lfs clean %f" = "$(git config filter.lfs.clean)" ]