merge: add a --signoff flag

Some projects require every commit, even merges, to be signed off [*1*]. Because "git merge" does not have a "--signoff" option like "git commit" does, the user needs to add one manually when the command presents an editor to describe the merge, or later use "git commit --amend --signoff". Help developers of these projects by teaching "--signoff" option to "git merge". *1* https://public-inbox.org/git/CAHv71zK5SqbwrBFX=a8-DY9H3KT4FEyMgv__p2gZzNr0WUAPUw@mail.gmail.com/T/#u Requested-by: Dan Kohn <dan@linuxfoundation.org> Signed-off-by: Łukasz Gryglicki <lukaszgryglicki@o2.pl> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Łukasz Gryglicki committed Jul 4, 2017 at 09:33 UTC 14d01b4f07a08ee55bffc74f2d6e1b0a4e8ce900
3 files changed +81
Documentation/git-merge.txt
+8
@@ -64,6 +64,14 @@ OPTIONS
64 -------
65 include::merge-options.txt[]
66
67 +--signoff::
68 + Add Signed-off-by line by the committer at the end of the commit
69 + log message. The meaning of a signoff depends on the project,
70 + but it typically certifies that committer has
71 + the rights to submit this work under the same license and
72 + agrees to a Developer Certificate of Origin
73 + (see http://developercertificate.org/ for more information).
74 +
75 -S[<keyid>]::
76 --gpg-sign[=<keyid>]::
77 GPG-sign the resulting merge commit. The `keyid` argument is
builtin/merge.c
+4
@@ -70,6 +70,7 @@ static int continue_current_merge;
70 static int allow_unrelated_histories;
71 static int show_progress = -1;
72 static int default_to_upstream = 1;
73 +static int signoff;
74 static const char *sign_commit;
75
76 static struct strategy all_strategy[] = {
@@ -233,6 +234,7 @@ static struct option builtin_merge_options[] = {
234 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
235 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
236 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
237 + OPT_BOOL(0, "signoff", &signoff, N_("add Signed-off-by:")),
238 OPT_END()
239 };
240
@@ -763,6 +765,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
765 strbuf_addch(&msg, '\n');
766 if (0 < option_edit)
767 strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
768 + if (signoff)
769 + append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
770 write_file_buf(git_path_merge_msg(), msg.buf, msg.len);
771 if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
772 git_path_merge_msg(), "merge", NULL))
t/t7614-merge-signoff.sh new
+69
@@ -0,0 +1,69 @@
1 +#!/bin/sh
2 +
3 +test_description='git merge --signoff
4 +
5 +This test runs git merge --signoff and makes sure that it works.
6 +'
7 +
8 +. ./test-lib.sh
9 +
10 +# Setup test files
11 +test_setup() {
12 + # Expected commit message after merge --signoff
13 + cat >expected-signed <<EOF &&
14 +Merge branch 'master' into other-branch
15 +
16 +Signed-off-by: $(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/")
17 +EOF
18 +
19 + # Expected commit message after merge without --signoff (or with --no-signoff)
20 + cat >expected-unsigned <<EOF &&
21 +Merge branch 'master' into other-branch
22 +EOF
23 +
24 + # Initial commit and feature branch to merge master into it.
25 + git commit --allow-empty -m "Initial empty commit" &&
26 + git checkout -b other-branch &&
27 + test_commit other-branch file1 1
28 +}
29 +
30 +# Setup repository, files & feature branch
31 +# This step must be run if You want to test 2,3 or 4
32 +# Order of 2,3,4 is not important, but 1 must be run before
33 +# For example `-r 1,4` or `-r 1,4,2 -v` etc
34 +# But not `-r 2` or `-r 4,3,2,1`
35 +test_expect_success 'setup' '
36 + test_setup
37 +'
38 +
39 +# Test with --signoff flag
40 +test_expect_success 'git merge --signoff adds a sign-off line' '
41 + git checkout master &&
42 + test_commit master-branch-2 file2 2 &&
43 + git checkout other-branch &&
44 + git merge master --signoff --no-edit &&
45 + git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
46 + test_cmp expected-signed actual
47 +'
48 +
49 +# Test without --signoff flag
50 +test_expect_success 'git merge does not add a sign-off line' '
51 + git checkout master &&
52 + test_commit master-branch-3 file3 3 &&
53 + git checkout other-branch &&
54 + git merge master --no-edit &&
55 + git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
56 + test_cmp expected-unsigned actual
57 +'
58 +
59 +# Test for --no-signoff flag
60 +test_expect_success 'git merge --no-signoff flag cancels --signoff flag' '
61 + git checkout master &&
62 + test_commit master-branch-4 file4 4 &&
63 + git checkout other-branch &&
64 + git merge master --no-edit --signoff --no-signoff &&
65 + git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
66 + test_cmp expected-unsigned actual
67 +'
68 +
69 +test_done