Add --base option to git-format-patch to record base tree info
v5 cover Add --base option to git-format-patch to record base tree info
Thanks for Junio's reviews and suggestions.
This version contains the following changes since v4:
- Refine the commit log as well as the documentation according to Junio's comments.
- Separate out get_base_commit function from prepare_bases to obtain the base commit.
- Use repeated pair-wise computation to get the merge base for the validation of base commit.
- Extract "auto handling thing" from prepare_bases and put it into get_base_commit.
- Use format.useAutoBase boolean variable for the auto configuration in format section.
Thanks,
Xiaolong.
Xiaolong Ye (4):
patch-ids: make commit_patch_id() a public helper function
format-patch: add '--base' option to record base tree info
format-patch: introduce --base=auto option
format-patch: introduce format.useAutoBase configuration
Documentation/config.txt | 5 ++ Documentation/git-format-patch.txt | 60 ++++++++++++++ builtin/log.c | 165 +++++++++++++++++++++++++++++++++++++ patch-ids.c | 2 +- patch-ids.h | 2 + t/t4014-format-patch.sh | 48 +++++++++++ 6 files changed, 281 insertions(+), 1 deletion(-)
Signature · 3 lines
2.8.1.221.ga4c6ba7 base-commit: e6ac6e1f7d54584c2b03f073b5f329a37f4a9561
PATCH v5 1/4 patch-ids: make commit_patch_id() a public helper function
Make commit_patch_id() available to other builtins.
--- patch-ids.c | 2 +- patch-ids.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)
patch-ids.c +1 -1
@@ -4,7 +4,7 @@#include "sha1-lookup.h" #include "patch-ids.h" -static int commit_patch_id (struct commit *commit,struct diff_options *options, +int commit_patch_id (struct commit *commit,struct diff_options *options,unsigned char *sha1) {if (commit->parents)
patch-ids.h +2 -0
@@ -13,6 +13,8 @@ struct patch_ids {struct patch_id_bucket *patches; }; +int commit_patch_id (struct commit *commit,struct diff_options *options, +unsigned char *sha1);int init_patch_ids (struct patch_ids *);int free_patch_ids (struct patch_ids *);struct patch_id *add_commit_patch_id (struct commit *,struct patch_ids *);
Signature · 1 line
2.8.1.221.ga4c6ba7
PATCH v5 2/4 format-patch: add '--base' option to record base tree info
In reply to Xiaolong YeMaintainers or third party testers may want to know the exact base tree the patch series applies to. Teach git format-patch a '--base' option to record the base tree info and append it at the end of the first message (either the cover letter or the first patch in the series).
The base tree info consists of the "base commit", which is a well-known commit that is part of the stable part of the project history everybody else works off of, and zero or more "prerequisite patches", which are well-known patches in flight that is not yet part of the "base commit" that need to be applied on top of "base commit" in topological order before the patches can be applied.
The "base commit" is shown as "base-commit: " followed by the 40-hex of the commit object name. A "prerequisite patch" is shown as "prerequisite-patch-id: " followed by the 40-hex "patch id", which can be obtained by passing the patch through the "git patch-id --stable" command.
Imagine that on top of the public commit P, you applied well-known patches X, Y and Z from somebody else, and then built your three-patch series A, B, C, the history would be like:
---P---X---Y---Z---A---B---C
With "git format-patch --base=P -3 C" (or variants thereof, e.g. with "--cover-letter" of using "Z..C" instead of "-3 C" to specify the range), the base tree information block is shown at the end of the first message the command outputs (either the first patch, or the cover letter), like this:
base-commit: P
prerequisite-patch-id: X
prerequisite-patch-id: Y
prerequisite-patch-id: Z
--- Documentation/git-format-patch.txt | 54 ++++++++++++++ builtin/log.c | 139 +++++++++++++++++++++++++++++++++++++ t/t4014-format-patch.sh | 15 ++++ 3 files changed, 208 insertions(+)
Documentation/git-format-patch.txt +54 -0
@@ -265,6 +265,11 @@ you can use `--suffix=-patch` to get `0001-description-of-my-change-patch`. Output an all-zero hash in each patch's From header instead of the hash of the commit. +--base=<commit>:: + Record the base tree information to identify the state the + patch series applies to. See the BASE TREE INFORMATION section + below for details. + --root:: Treat the revision argument as a <revision range>, even if it is just a single commit (that would normally be treated as a
@@ -520,6 +525,55 @@ This should help you to submit patches inline using KMail. 5. Back in the compose window: add whatever other text you wish to the message, complete the addressing and subject fields, and press send. +BASE TREE INFORMATION +--------------------- + +The base tree information block is used for maintainers or third party +testers to know the exact state the patch series applies to. It consists +of the 'base commit', which is a well-known commit that is part of the +stable part of the project history everybody else works off of, and zero +or more 'prerequisite patches', which are well-known patches in flight +that is not yet part of the 'base commit' that need to be applied on top +of 'base commit' in topological order before the patches can be applied. + +The 'base commit' is shown as "base-commit: " followed by the 40-hex of +the commit object name. A 'prerequisite patch' is shown as +"prerequisite-patch-id: " followed by the 40-hex 'patch id', which can +be obtained by passing the patch through the `git patch-id --stable` +command. + +Imagine that on top of the public commit P, you applied well-known +patches X, Y and Z from somebody else, and then built your three-patch +series A, B, C, the history would be like: + +................................................ +---P---X---Y---Z---A---B---C +................................................ + +With `git format-patch --base=P -3 C` (or variants thereof, e.g. with +`--cover-letter` of using `Z..C` instead of `-3 C` to specify the +range), the base tree information block is shown at the end of the +first message the command outputs (either the first patch, or the +cover letter), like this: + +------------ +base-commit: P +prerequisite-patch-id: X +prerequisite-patch-id: Y +prerequisite-patch-id: Z +------------ + +For non-linear topology, such as + +................................................ +---P---X---A---M---C + \ / + Y---Z---B +................................................ + +You can also use `git format-patch --base=P -3 C` to generate patches +for A, B and C, and the identifiers for P, X, Y, Z are appended at the +end of the first message. EXAMPLES --------
builtin/log.c +139 -0
175 lines not on this page · raw message
t/t4014-format-patch.sh +15 -0
@@ -1460,4 +1460,19 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' ' test_path_is_dir patchset ' +test_expect_success 'format-patch --base' ' + git checkout side && + git format-patch --stdout --base=HEAD~~~ -1 >patch && + grep -e "^base-commit:" -A3 patch >actual && + echo "base-commit: $(git rev-parse HEAD~~~)" >expected && + echo "prerequisite-patch-id: $(git show --patch HEAD~~ | git patch-id --stable | awk "{print \$1}")" >>expected && + echo "prerequisite-patch-id: $(git show --patch HEAD~ | git patch-id --stable | awk "{print \$1}")" >>expected && + test_cmp expected actual +' + +test_expect_success 'format-patch --base error handling' ' + ! git format-patch --base=HEAD~ -2 && + ! git format-patch --base=HEAD~ -3 +' + test_done
Signature · 1 line
2.8.1.221.ga4c6ba7
PATCH v5 3/4 format-patch: introduce --base=auto option
In reply to Xiaolong YeIntroduce --base=auto to record the base commit info automatically, the base_commit will be the merge base of tip commit of the upstream branch and revision-range specified in cmdline.
--- Documentation/git-format-patch.txt | 6 ++++++ builtin/log.c | 27 ++++++++++++++++++++++++--- t/t4014-format-patch.sh | 15 +++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-)
Documentation/git-format-patch.txt +6 -0
@@ -575,6 +575,12 @@ You can also use `git format-patch --base=P -3 C` to generate patches for A, B and C, and the identifiers for P, X, Y, Z are appended at the end of the first message. +If set `--base=auto` in cmdline, it will track base commit automatically, +the base commit will be the merge base of tip commit of the remote-tracking +branch and revision-range specified in cmdline. +For a local branch, you need to track a remote branch by `git branch +--set-upstream-to` before using this option. + EXAMPLES --------
builtin/log.c +24 -3
@@ -1205,9 +1205,30 @@ static struct commit *get_base_commit(const char *base_commit,struct commit **rev;int i =0 , rev_nr =0 ; - base =lookup_commit_reference_by_name (base_commit); -if (!base) -die (_ ("Unknown commit %s" ), base_commit); +if (!strcmp (base_commit,"auto" )) { +struct branch *curr_branch =branch_get (NULL ); +const char *upstream =branch_get_upstream (curr_branch,NULL ); +if (upstream) { +unsigned char sha1[20 ]; +if (get_sha1 (upstream, sha1)) +die (_ ("Failed to resolve '%s' as a valid ref." ), upstream); +struct commit *commit =lookup_commit_or_die (sha1,"upstream base" ); +struct commit_list *base_list =get_merge_bases_many (commit, total, list); +/* There should be one and only one merge base. */ +if (!base_list || base_list->next) +die (_ ("Could not find exact merge base." )); + base = base_list->item; +free_commit_list (base_list); + }else { +die (_ ("Failed to get upstream, if you want to record base commit automatically,\n" +"please use git branch --set-upstream-to to track a remote branch.\n" +"Or you could specify base commit by --base=<base-commit-id> manually." )); + } + }else { + base =lookup_commit_reference_by_name (base_commit); +if (!base) +die (_ ("Unknown commit %s" ), base_commit); + }ALLOC_ARRAY (rev, total);for (i =0 ; i < total; i++)
t/t4014-format-patch.sh +15 -0
@@ -1475,4 +1475,19 @@ test_expect_success 'format-patch --base error handling' ' ! git format-patch --base=HEAD~ -3 ' +test_expect_success 'format-patch --base=auto' ' + git checkout -b new master && + git branch --set-upstream-to=master && + echo "A" >>file && + git add file && + git commit -m "New change #A" && + echo "B" >>file && + git add file && + git commit -m "New change #B" && + git format-patch --stdout --base=auto -2 >patch && + grep -e "^base-commit:" patch >actual && + echo "base-commit: $(git rev-parse master)" >expected && + test_cmp expected actual +' + test_done
Signature · 1 line
2.8.1.221.ga4c6ba7
PATCH v5 4/4 format-patch: introduce format.useAutoBase configuration
In reply to Xiaolong YeThis allows to record the base commit automatically, it is equivalent to set --base=auto in cmdline.
The format.useAutoBase has lower priority than command line option, so if user set format.useAutoBase and pass the command line option in the meantime, base_commit will be the one passed to command line option.
--- Documentation/config.txt | 5 +++++ builtin/log.c | 17 +++++++++++------ t/t4014-format-patch.sh | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-)
Documentation/config.txt +5 -0
@@ -1259,6 +1259,11 @@ format.outputDirectory:: Set a custom directory to store the resulting files instead of the current working directory. +format.useAutoBase:: + A boolean value which lets you enable the `--base=auto` option of + format-patch by default. + + filter.<driver>.clean:: The command which is used to convert the content of a worktree file to a blob upon checkin. See linkgit:gitattributes[5] for
builtin/log.c +11 -6
@@ -702,6 +702,7 @@ static void add_header(const char *value)#define THREAD_DEEP 2static int thread;static int do_signoff; +static int base_auto;static const char *signature = git_version_string;static const char *signature_file;static int config_cover_letter;
@@ -786,6 +787,10 @@ static int git_format_config(const char *var, const char *value, void *cb) }if (!strcmp (var,"format.outputdirectory" ))return git_config_string (&config_output_directory, var, value); +if (!strcmp (var,"format.useautobase" )) { + base_auto =git_config_bool (var, value); +return 0 ; + }return git_log_config (var, value, cb); }
@@ -1205,7 +1210,11 @@ static struct commit *get_base_commit(const char *base_commit,struct commit **rev;int i =0 , rev_nr =0 ; -if (!strcmp (base_commit,"auto" )) { +if (base_commit &&strcmp (base_commit,"auto" )) { + base =lookup_commit_reference_by_name (base_commit); +if (!base) +die (_ ("Unknown commit %s" ), base_commit); + }else if ((base_commit && !strcmp (base_commit,"auto" )) || base_auto) {struct branch *curr_branch =branch_get (NULL );const char *upstream =branch_get_upstream (curr_branch,NULL );if (upstream) {
@@ -1224,10 +1233,6 @@ static struct commit *get_base_commit(const char *base_commit,"please use git branch --set-upstream-to to track a remote branch.\n" "Or you could specify base commit by --base=<base-commit-id> manually.")); } - }else { - base =lookup_commit_reference_by_name (base_commit); -if (!base) -die (_ ("Unknown commit %s" ), base_commit); }ALLOC_ARRAY (rev, total);
@@ -1666,7 +1671,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix) }memset (&bases,0 ,sizeof (bases)); -if (base_commit) { +if (base_commit || base_auto) {struct commit *base =get_base_commit (base_commit, list, nr);reset_revision_walk ();prepare_bases (&bases, base, list, nr);
t/t4014-format-patch.sh +18 -0
@@ -1490,4 +1490,22 @@ test_expect_success 'format-patch --base=auto' ' test_cmp expected actual ' +test_expect_success 'format-patch format.base option' ' + test_when_finished "git config --unset format.useAutoBase" && + git config format.useAutoBase true && + git format-patch --stdout -1 >patch && + grep -e "^base-commit:" patch >actual && + echo "base-commit: $(git rev-parse master)" >expected && + test_cmp expected actual +' + +test_expect_success 'format-patch --base overrides format.base' ' + test_when_finished "git config --unset format.useAutoBase" && + git config format.useAutoBase true && + git format-patch --stdout --base=HEAD~ -1 >patch && + grep -e "^base-commit:" patch >actual && + echo "base-commit: $(git rev-parse HEAD~)" >expected && + test_cmp expected actual +' + test_done
Signature · 1 line
2.8.1.221.ga4c6ba7
Re: [PATCH v5 2/4] format-patch: add '--base' option to record base tree info
In reply to Xiaolong Yet/t4014-format-patch.sh test_expect_success 'format-patch -o overrides format.outputDirectory' ' · line 1466
Quoted 4 lines · Xiaolong Ye
t/t4014-format-patch.sh +4
-0+test_expect_success 'format-patch --base' ' + git checkout side && + git format-patch --stdout --base=HEAD~~~ -1 >patch && + grep -e "^base-commit:" -A3 patch >actual &&
The -A3 is GNUism. To do this portably, perhaps you can do
sed -n -e "/^base-commit:/,+3p"
or something like that.
But more importantly, grabbing 3 lines (and always 3 lines) will not catch a future bug that somebody else may introduce to this code that shows extra "prerequisite-patch-id:" after them.
Quoted 5 lines
t/t4014-format-patch.sh +5
-0+ echo "base-commit: $(git rev-parse HEAD~~~)" >expected && + echo "prerequisite-patch-id: $(git show --patch HEAD~~ | git patch-id --stable | awk "{print \$1}")" >>expected && + echo "prerequisite-patch-id: $(git show --patch HEAD~ | git patch-id --stable | awk "{print \$1}")" >>expected && + test_cmp expected actual +'
Re: [PATCH v5 2/4] format-patch: add '--base' option to record base tree info
In reply to Xiaolong YeQuoted 20 lines · Xiaolong Ye
builtin/log.c +20
-0+static struct commit *get_base_commit (const char *base_commit, +struct commit **list, +int total) +{ +struct commit *base =NULL ; +struct commit **rev; +int i =0 , rev_nr =0 ; + + base =lookup_commit_reference_by_name (base_commit); +if (!base) +die (_ ("Unknown commit %s" ), base_commit); + +ALLOC_ARRAY (rev, total); +for (i =0 ; i < total; i++) + rev[i] = list[i]; + + rev_nr = total; +/* +* Get merge base through pair-wise computations +* and store it in rev[0].
builtin/log.c static int from_callback(const struct option *opt, const char *arg, int unset) · line 1229
Quoted 10 lines
builtin/log.c +10
-0+*/ +while (rev_nr >1 ) { +for (i =0 ; i < rev_nr /2 ; i++) { +struct commit_list *merge_base; + merge_base =get_merge_bases (rev[2 * i], rev[2 * i +1 ]); +if (!merge_base || merge_base->next) +die (_ ("Failed to find exact merge base" )); + + rev[i] = merge_base->item; + }
So merge-base(0,1) is stored in rev[0], merge-base(2,3) is then stored in rev[1], etc. and the last item, if rev_nr is odd, is left in rev[rev_nr-1]. When the loop finishes, i is left as rev_nr/2 and...
builtin/log.c static int from_callback(const struct option *opt, const char *arg, int unset) · line 1232
Quoted 2 lines
builtin/log.c +2
-0+if (rev_nr %2 ) + rev[i] = rev[2 * i];
... when rev_nr is odd, that left-over thing moved down here. E.g. if rev_nr == 5, the loop is left with i==2, rev[0] and rev[1] are filled with pairwise merge bases, and this moves rev[4] to rev[2], so that we can further process rev[0,1,2] with rev_nr set to 3 (i.e. (rev_nr + 1) / 2 below).
Sounds correct.
Quoted 14 lines
builtin/log.c +14
-0+ rev_nr = (rev_nr +1 ) /2 ; + } + +if (!in_merge_bases (base, rev[0 ])) +die (_ ("base commit should be the ancestor of revision list" )); + +for (i =0 ; i < total; i++) { +if (base == list[i]) +die (_ ("base commit shouldn't be in revision list" )); + } + +free (rev); +return base; +}
Re: [PATCH v5 2/4] format-patch: add '--base' option to record base tree info
In reply to Xiaolong Yet/t4014-format-patch.sh test_expect_success 'format-patch -o overrides format.outputDirectory' ' · line 1467
Quoted 13 lines · Xiaolong Ye
t/t4014-format-patch.sh +5
-0@@ -1460,4 +1460,19 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' ' test_path_is_dir patchset ' +test_expect_success 'format-patch --base' ' + git checkout side && + git format-patch --stdout --base=HEAD~~~ -1 >patch && + grep -e "^base-commit:" -A3 patch >actual && + echo "base-commit: $(git rev-parse HEAD~~~)" >expected &&
HEAD~3 would be easier to read (and HEAD~2 is easier than HEAD~~).
t/t4014-format-patch.sh test_expect_success 'format-patch -o overrides format.outputDirectory' ' · line 1476
Quoted 9 lines
t/t4014-format-patch.sh +9
-0+ echo "prerequisite-patch-id: $(git show --patch HEAD~~ | git patch-id --stable | awk "{print \$1}")" >>expected && + echo "prerequisite-patch-id: $(git show --patch HEAD~ | git patch-id --stable | awk "{print \$1}")" >>expected && + test_cmp expected actual +' + +test_expect_success 'format-patch --base error handling' ' + ! git format-patch --base=HEAD~ -2 && + ! git format-patch --base=HEAD~ -3 +'
When making sure that "git" exits with a failure in a controlled way (i.e. you want to consider "git" that segfaults as not passing the test), do not use "! git cmd", but use "test_must_fail git cmd" instead.
You now have a quite elaborate logic in base validation in this round. Is the topology of the history used in this test still complex enough to make sure the logic is being tested?
Re: [PATCH v5 3/4] format-patch: introduce --base=auto option
In reply to Xiaolong Yebuiltin/log.c static struct commit *get_base_commit(const char *base_commit, · line 1216
Quoted 6 lines · Xiaolong Ye
builtin/log.c +6
-0+if (upstream) { +unsigned char sha1[20 ]; +if (get_sha1 (upstream, sha1)) +die (_ ("Failed to resolve '%s' as a valid ref." ), upstream); +struct commit *commit =lookup_commit_or_die (sha1,"upstream base" ); +struct commit_list *base_list =get_merge_bases_many (commit, total, list);
This introduces decl-after-statement.
Re: [PATCH v5 2/4] format-patch: add '--base' option to record base tree info
In reply to Junio C HamanoQuoted 9 lines · Junio C Hamano
t/t4014-format-patch.sh test_expect_success 'format-patch -o overrides format.outputDirectory' ' · line 1466
Quoted 4 lines · Xiaolong Ye
t/t4014-format-patch.sh +4
-0+test_expect_success 'format-patch --base' ' + git checkout side && + git format-patch --stdout --base=HEAD~~~ -1 >patch && + grep -e "^base-commit:" -A3 patch >actual &&The -A3 is GNUism. To do this portably, perhaps you can do
Quoted 7 lines
sed -n -e "/^base-commit:/,+3p"or something like that.
But more importantly, grabbing 3 lines (and always 3 lines) will not catch a future bug that somebody else may introduce to this code that shows extra "prerequisite-patch-id:" after them.
I'll try to improve the testcase to make it more sensible.
Thanks,
Xiaolong
Quoted 6 lines
Quoted 6 lines
t/t4014-format-patch.sh +5
-0+ echo "base-commit: $(git rev-parse HEAD~~~)" >expected && + echo "prerequisite-patch-id: $(git show --patch HEAD~~ | git patch-id --stable | awk "{print \$1}")" >>expected && + echo "prerequisite-patch-id: $(git show --patch HEAD~ | git patch-id --stable | awk "{print \$1}")" >>expected && + test_cmp expected actual +'
Re: [PATCH v5 2/4] format-patch: add '--base' option to record base tree info
In reply to Junio C HamanoQuoted 18 lines · Junio C Hamano
t/t4014-format-patch.sh test_expect_success 'format-patch -o overrides format.outputDirectory' ' · line 1467
Quoted 13 lines · Xiaolong Ye
t/t4014-format-patch.sh +5
-0@@ -1460,4 +1460,19 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' ' test_path_is_dir patchset ' +test_expect_success 'format-patch --base' ' + git checkout side && + git format-patch --stdout --base=HEAD~~~ -1 >patch && + grep -e "^base-commit:" -A3 patch >actual && + echo "base-commit: $(git rev-parse HEAD~~~)" >expected &&HEAD~3 would be easier to read (and HEAD~2 is easier than HEAD~~).
Quoted 14 lines
t/t4014-format-patch.sh test_expect_success 'format-patch -o overrides format.outputDirectory' ' · line 1476
Quoted 9 lines
t/t4014-format-patch.sh +9
-0+ echo "prerequisite-patch-id: $(git show --patch HEAD~~ | git patch-id --stable | awk "{print \$1}")" >>expected && + echo "prerequisite-patch-id: $(git show --patch HEAD~ | git patch-id --stable | awk "{print \$1}")" >>expected && + test_cmp expected actual +' + +test_expect_success 'format-patch --base error handling' ' + ! git format-patch --base=HEAD~ -2 && + ! git format-patch --base=HEAD~ -3 +'When making sure that "git" exits with a failure in a controlled way (i.e. you want to consider "git" that segfaults as not passing the test), do not use "! git cmd", but use "
test_must_failgit cmd" instead.
Thanks for the reminder, I misunderstood the guide in t/README before.
Quoted 4 lines
You now have a quite elaborate logic in base validation in this rounhd. Is the topology of the history used in this test still complex enough to make sure the logic is being tested?
I'll try to set up more complex topology of the history for the testcase.
Thanks,
Xiaolong.
Re: [PATCH v5 3/4] format-patch: introduce --base=auto option
In reply to Junio C HamanoQuoted 10 lines · Junio C Hamano
builtin/log.c static struct commit *get_base_commit(const char *base_commit, · line 1216
Quoted 6 lines · Xiaolong Ye
builtin/log.c +6
-0+if (upstream) { +unsigned char sha1[20 ]; +if (get_sha1 (upstream, sha1)) +die (_ ("Failed to resolve '%s' as a valid ref." ), upstream); +struct commit *commit =lookup_commit_or_die (sha1,"upstream base" ); +struct commit_list *base_list =get_merge_bases_many (commit, total, list);This introduces decl-after-statement.
will fix it.
Thanks,
Xiaolong.