Skip to contentSkip to conversation

add a pre-merge hook

PATCH New Paolo Bonzini 2 participants 4 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add a pre-merge hook

This hook provides a way to look at what kind of merge is invoked, and stop it. The type of merge is passed to the hook.

The patch provides a sample implementation that implements a branch.<branch-name>.allowmerges key. It can be useful for project whose policy is "only fast forwards on the integrator's repository".

---
 Documentation/githooks.txt        |   18 +++++
 builtin-merge.c                   |   65 +++++++++++++++----
 t/t5407-pre-merge-hook.sh         |  130 +++++++++++++++++++++++++++++++++++++
 templates/hooks--pre-merge.sample |   49 ++++++++++++++
 4 files changed, 250 insertions(+), 12 deletions(-)
 create mode 100755 t/t5407-pre-merge-hook.sh
 create mode 100755 templates/hooks--pre-merge.sample

The included testcases demonstrate that trivial merges are currently broken. The failing test is equivalent to:

git init
echo a > a
git add a
git commit -ma
git checkout -b branch
echo b > b
git add b
git commit -mb
git checkout master
git merge --no-ff -s resolve branch

Running this in 1.5.5 shows:

Trying really trivial in-index merge...
Wonderful.
In-index merge

while `next' gives

Trying really trivial in-index merge...
error: Untracked working tree file 'a' would be overwritten by merge. Nope.
Trying simple merge.
Merge made by resolve.

Documentation/githooks.txt +18 -0
index 046a2a7..c45602c 100644
@@ -187,6 +187,24 @@ Both standard output and standard error output are forwarded to
 'git-send-pack' on the other end, so you can simply `echo` messages
 for the user.
 
+pre-merge
+---------
+
+This hook is invoked before a merge is attempted.  The command
+line passed to the hook can have multiple parameters.  The
+first parameter is the type of merge, which can be one of
+"squash" (if --squash was given on the command line), "ff"
+(fast-forward), "trivial" (trivial in-index merge), "merge"
+(non-trivial 2-head merge), "octopus" (any other merge).  After this
+there is a "--" argument, followed by the commit SHA1 values for the
+heads being merged.
+
+The hook can interrupt the merge by returning a non-zero
+status.  The default hook checks for a boolean configuration
+key `branch.<branch-name>.allowmerges`, where `<branch-name>`
+is the current branch.  If the key is false, only squash or
+fast-forward merges are allowed.
+
 [[update]]
 update
 ------
builtin-merge.c +53 -12

117 lines not on this page · raw message

t/t5407-pre-merge-hook.sh new file +130 -0

130 lines not on this page · raw message

templates/hooks--pre-merge.sample new file +49 -0

49 lines not on this page · raw message

Signature · 1 line
1.5.5
#1 lore raw

PATCH 1/1 add a pre-merge hook

This hook provides a way to look at what kind of merge is invoked, and stop it. The type of merge is passed to the hook.

The patch provides a sample implementation that implements a branch.<branch-name>.allowmerges key. It can be useful for project whose policy is "only fast forwards on the integrator's repository".

---
 Documentation/githooks.txt        |   18 +++++
 builtin-merge.c                   |   65 +++++++++++++++----
 t/t5407-pre-merge-hook.sh         |  130 +++++++++++++++++++++++++++++++++++++
 templates/hooks--pre-merge.sample |   49 ++++++++++++++
 4 files changed, 250 insertions(+), 12 deletions(-)
 create mode 100755 t/t5407-pre-merge-hook.sh
 create mode 100755 templates/hooks--pre-merge.sample

The only difference with respect to the previous version is that the trivial merge testcase passes.

The follow-up at
http://permalink.gmane.org/gmane.comp.version-control.git/93147
is unchanged.

Documentation/githooks.txt +18 -0
index 046a2a7..c45602c 100644
@@ -187,6 +187,24 @@ Both standard output and standard error output are forwarded to
 'git-send-pack' on the other end, so you can simply `echo` messages
 for the user.
 
+pre-merge
+---------
+
+This hook is invoked before a merge is attempted.  The command
+line passed to the hook can have multiple parameters.  The
+first parameter is the type of merge, which can be one of
+"squash" (if --squash was given on the command line), "ff"
+(fast-forward), "trivial" (trivial in-index merge), "merge"
+(non-trivial 2-head merge), "octopus" (any other merge).  After this
+there is a "--" argument, followed by the commit SHA1 values for the
+heads being merged.
+
+The hook can interrupt the merge by returning a non-zero
+status.  The default hook checks for a boolean configuration
+key `branch.<branch-name>.allowmerges`, where `<branch-name>`
+is the current branch.  If the key is false, only squash or
+fast-forward merges are allowed.
+
 [[update]]
 update
 ------
builtin-merge.c +53 -12

117 lines not on this page · raw message

t/t5407-pre-merge-hook.sh new file +130 -0

130 lines not on this page · raw message

templates/hooks--pre-merge.sample new file +49 -0

49 lines not on this page · raw message

Signature · 1 line
1.5.5
#2 lore raw

Documentation/githooks.txt Both standard output and standard error output are forwarded to · line 200

Quoted 11 lines · Paolo Bonzini
Documentation/githooks.txt +11 -0
+pre-merge
+---------
+
+This hook is invoked before a merge is attempted.  The command
+line passed to the hook can have multiple parameters.  The
+first parameter is the type of merge, which can be one of
+"squash" (if --squash was given on the command line), "ff"
+(fast-forward), "trivial" (trivial in-index merge), "merge"
+(non-trivial 2-head merge), "octopus" (any other merge).  After this
+there is a "--" argument, followed by the commit SHA1 values for the
+heads being merged.

I think we should have done something like this for post_merge, not just "1/0 to show if it is a squash" which was an ill-thought-out hack. Is there a clean way to fix the interface without breaking existing post_merge hooks in people's repositories?

Documentation/githooks.txt Both standard output and standard error output are forwarded to · line 206

Quoted 5 lines
Documentation/githooks.txt +5 -0
+The hook can interrupt the merge by returning a non-zero
+status.  The default hook checks for a boolean configuration
+key `branch.<branch-name>.allowmerges`, where `<branch-name>`
+is the current branch.  If the key is false, only squash or
+fast-forward merges are allowed.

Please call whatever you install from template/hooks--* a "sample", not "default".

builtin-merge.c static int run_hook(const char *name) · line 336

Quoted 11 lines
builtin-merge.c +10 -0
+static int run_hook(const char *name, struct commit_list *heads, ...)
 {
+ ...
+	if (heads) {
+		argv[i] = "--";
+		first_remote_head = i + 1;
+		for (; heads; heads = heads->next)
+			argv[++i] = xstrdup(sha1_to_hex(heads->item->object.sha1));
+		argv[++i] = NULL;
+	} else
+		first_remote_head = i;

Making this function varargs is fine, but this heads==NULL condition needs some explanation.

If it is primarily a hack to support calling post-merge with an non-object name "0" or "1", probably it will be _much_ cleaner to make this a separate function that does not know anything about post-merge calling convention (it can share the boilerplate parts such as "hook.no_stdin = 1" with the function that calls post-merge, of course).

builtin-merge.c static int run_hook(const char *name) · line 356

Quoted 5 lines
builtin-merge.c +4 -0
+static int run_pre_merge_hook(const char *kind)
+{
+	return run_hook("pre-merge", remoteheads,
+			squash ? "squash" : kind, "--", NULL);
 }

remoteheads is a commit_list, you have the name of the hook and kind, so this extra "--" is only for run_hook() hackery to distinguish between the callers to post-merge and the callers to new pre-merge?

The general idea to add "pre-merge" hook may be sound, but the run_hook() implementation looks quite unclean.

#3 lore raw
Quoted 5 lines

I think we should have done something like this for post_merge, not just "1/0 to show if it is a squash" which was an ill-thought-out hack. Is there a clean way to fix the interface without breaking existing post_merge hooks in people's repositories?

Well, you can add this argument as a second argument to post-merge. Shall I do it?

Quoted 2 lines

Please call whatever you install from template/hooks--* a "sample", not "default".

Ok.

Quoted 2 lines

If it is primarily a hack to support calling post-merge with an non-object name "0" or "1"

It is to support not appending the commit list to the arguments of post-merge.

Quoted 9 lines

builtin-merge.c static int run_hook(const char *name) · line 356

Quoted 5 lines
builtin-merge.c +4 -0
+static int run_pre_merge_hook(const char *kind)
+{
+	return run_hook("pre-merge", remoteheads,
+			squash ? "squash" : kind, "--", NULL);
 }

remoteheads is a commit_list, you have the name of the hook and kind, so this extra "--" is only for run_hook() hackery to distinguish between the callers to post-merge and the callers to new pre-merge?

No, absolutely. The "--" separator is for future extension, in case we want to add other arguments before the head list.

run_hook does not care about what it receives in its varargs part. It is copied verbatim until the NULL. Then it adds a "--" and the heads argument if requested (which is not the case for post-merge). The fact that I had to specify a "--" here though is fishy. Maybe I have a off-by-one somewhere that, if fixed, can simplify the code somehow.

I'll take a look tomorrow, thanks for the review.

Paolo