79 lines
1.4 KiB
Go
79 lines
1.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/logrusorgru/aurora"
|
||
|
)
|
||
|
|
||
|
func blacklisted() bool {
|
||
|
bl, err := blacklistedAux()
|
||
|
switch {
|
||
|
case err != nil:
|
||
|
Warn("failed to check blacklist: ", aurora.Red(err))
|
||
|
return true
|
||
|
case bl:
|
||
|
Warn("skipping git-commit hooks in blacklisted repo")
|
||
|
return true
|
||
|
default:
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func blacklistedAux() (bool, error) {
|
||
|
// open the user's blacklist file (and skip if ENOENT)
|
||
|
home, err := os.UserHomeDir()
|
||
|
if err != nil {
|
||
|
return true, err
|
||
|
}
|
||
|
|
||
|
f, err := os.Open(filepath.Join(home,
|
||
|
"git/hooks/pre-commit-blacklist"))
|
||
|
switch {
|
||
|
case os.IsNotExist(err):
|
||
|
return false, nil
|
||
|
case err != nil:
|
||
|
return true, err
|
||
|
}
|
||
|
|
||
|
// we're called from the root of the git repo
|
||
|
cwd, err := os.Getwd()
|
||
|
if err != nil {
|
||
|
return true, err
|
||
|
}
|
||
|
|
||
|
// see if it matches
|
||
|
scanner := bufio.NewScanner(f)
|
||
|
for scanner.Scan() {
|
||
|
// get a line, stripping comments / spaces / blank lines
|
||
|
bl := scanner.Text()
|
||
|
if pos := strings.IndexRune(bl, '#'); pos != -1 {
|
||
|
bl = bl[:pos]
|
||
|
}
|
||
|
bl = strings.TrimSpace(bl)
|
||
|
if bl == "" {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
// we know bl[0] can be dereferenced
|
||
|
switch bl[0] {
|
||
|
case '*':
|
||
|
return strings.HasSuffix(cwd, bl[1:]), nil
|
||
|
|
||
|
case '~':
|
||
|
return cwd == home+bl[1:], nil
|
||
|
|
||
|
default:
|
||
|
return cwd == bl, nil
|
||
|
}
|
||
|
}
|
||
|
if scanner.Err() != nil {
|
||
|
return false, scanner.Err()
|
||
|
}
|
||
|
|
||
|
return false, nil
|
||
|
}
|