aboutsummaryrefslogtreecommitdiff
path: root/git-codereview/hook.go
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2023-09-11 22:34:15 -0400
committerGopher Robot <gobot@golang.org>2023-10-18 12:53:42 +0000
commit8be3abfe097ddf04b28eec2783249d29b3a48dae (patch)
tree7839249866fdd7da93221eaf0ef9ee2b82e44ba4 /git-codereview/hook.go
parent406355a4df8af4b140e43c56c0c4c9869471edca (diff)
downloadgo-x-review-1.8.0.tar.xz
git-codereview: make hooks command report conflicting hooksv1.8.0
Display warning message when a hook is already installed and is different from the one installed by git-codereview. Improves upon CL 184417. Fixes golang/go#16777 Change-Id: I7579a3e86572e8b74f92317973e7cc7094b3942d Reviewed-on: https://go-review.googlesource.com/c/review/+/377034 Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Diffstat (limited to 'git-codereview/hook.go')
-rw-r--r--git-codereview/hook.go36
1 files changed, 28 insertions, 8 deletions
diff --git a/git-codereview/hook.go b/git-codereview/hook.go
index 1ad0f09..0b4995f 100644
--- a/git-codereview/hook.go
+++ b/git-codereview/hook.go
@@ -21,9 +21,15 @@ var hookFiles = []string{
"pre-commit",
}
-func installHook(args []string) {
+// installHook installs Git hooks to enforce code review conventions.
+//
+// auto is whether hooks are being installed automatically as part of
+// running another git-codereview command, rather than an explicit
+// invocation of the 'hooks' command itself.
+func installHook(args []string, auto bool) {
flags.Parse(args)
hooksDir := gitPath("hooks")
+ var existingHooks []string
for _, hookFile := range hookFiles {
filename := filepath.Join(hooksDir, hookFile)
hookContent := fmt.Sprintf(hookScript, hookFile)
@@ -45,15 +51,16 @@ func installHook(args []string) {
}
}
- // If hook file exists, assume it is okay.
+ // If hook file exists but has different content, let the user know.
_, err := os.Stat(filename)
if err == nil {
- if *verbose > 0 {
- data, err := ioutil.ReadFile(filename)
- if err != nil {
- verbosef("reading hook: %v", err)
- } else if string(data) != hookContent {
- verbosef("unexpected hook content in %s", filename)
+ data, err := ioutil.ReadFile(filename)
+ if err != nil {
+ verbosef("reading hook: %v", err)
+ } else if string(data) != hookContent {
+ verbosef("unexpected hook content in %s", filename)
+ if !auto {
+ existingHooks = append(existingHooks, filename)
}
}
continue
@@ -74,6 +81,19 @@ func installHook(args []string) {
dief("writing hook: %v", err)
}
}
+
+ switch {
+ case len(existingHooks) == 1:
+ dief("Hooks file %s already exists."+
+ "\nTo install git-codereview hooks, delete that"+
+ " file and re-run 'git-codereview hooks'.",
+ existingHooks[0])
+ case len(existingHooks) > 1:
+ dief("Hooks files %s already exist."+
+ "\nTo install git-codereview hooks, delete these"+
+ " files and re-run 'git-codereview hooks'.",
+ strings.Join(existingHooks, ", "))
+ }
}
// repoRoot returns the root of the currently selected git repo, or