Skip to content

Commit bb52995

Browse files
0day-trouble-makergitster
authored andcommitted
format-patch: introduce format.useAutoBase configuration
This 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. Signed-off-by: Xiaolong Ye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3de6651 commit bb52995

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

Documentation/config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,10 @@ format.outputDirectory::
12581258
Set a custom directory to store the resulting files instead of the
12591259
current working directory.
12601260

1261+
format.useAutoBase::
1262+
A boolean value which lets you enable the `--base=auto` option of
1263+
format-patch by default.
1264+
12611265
filter.<driver>.clean::
12621266
The command which is used to convert the content of a worktree
12631267
file to a blob upon checkin. See linkgit:gitattributes[5] for

builtin/log.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ static void add_header(const char *value)
696696
#define THREAD_DEEP 2
697697
static int thread;
698698
static int do_signoff;
699+
static int base_auto;
699700
static const char *signature = git_version_string;
700701
static const char *signature_file;
701702
static int config_cover_letter;
@@ -780,6 +781,10 @@ static int git_format_config(const char *var, const char *value, void *cb)
780781
}
781782
if (!strcmp(var, "format.outputdirectory"))
782783
return git_config_string(&config_output_directory, var, value);
784+
if (!strcmp(var, "format.useautobase")) {
785+
base_auto = git_config_bool(var, value);
786+
return 0;
787+
}
783788

784789
return git_log_config(var, value, cb);
785790
}
@@ -1199,7 +1204,11 @@ static struct commit *get_base_commit(const char *base_commit,
11991204
struct commit **rev;
12001205
int i = 0, rev_nr = 0;
12011206

1202-
if (!strcmp(base_commit, "auto")) {
1207+
if (base_commit && strcmp(base_commit, "auto")) {
1208+
base = lookup_commit_reference_by_name(base_commit);
1209+
if (!base)
1210+
die(_("Unknown commit %s"), base_commit);
1211+
} else if ((base_commit && !strcmp(base_commit, "auto")) || base_auto) {
12031212
struct branch *curr_branch = branch_get(NULL);
12041213
const char *upstream = branch_get_upstream(curr_branch, NULL);
12051214
if (upstream) {
@@ -1221,10 +1230,6 @@ static struct commit *get_base_commit(const char *base_commit,
12211230
"please use git branch --set-upstream-to to track a remote branch.\n"
12221231
"Or you could specify base commit by --base=<base-commit-id> manually."));
12231232
}
1224-
} else {
1225-
base = lookup_commit_reference_by_name(base_commit);
1226-
if (!base)
1227-
die(_("Unknown commit %s"), base_commit);
12281233
}
12291234

12301235
ALLOC_ARRAY(rev, total);
@@ -1662,7 +1667,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
16621667
}
16631668

16641669
memset(&bases, 0, sizeof(bases));
1665-
if (base_commit) {
1670+
if (base_commit || base_auto) {
16661671
struct commit *base = get_base_commit(base_commit, list, nr);
16671672
reset_revision_walk();
16681673
prepare_bases(&bases, base, list, nr);

t/t4014-format-patch.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,4 +1546,23 @@ test_expect_success 'format-patch errors out when history involves criss-cross'
15461546
test_must_fail git format-patch --base=auto -1
15471547
'
15481548

1549+
test_expect_success 'format-patch format.useAutoBaseoption' '
1550+
test_when_finished "git config --unset format.useAutoBase" &&
1551+
git checkout local &&
1552+
git config format.useAutoBase true &&
1553+
git format-patch --stdout -1 >patch &&
1554+
grep "^base-commit:" patch >actual &&
1555+
echo "base-commit: $(git rev-parse upstream)" >expected &&
1556+
test_cmp expected actual
1557+
'
1558+
1559+
test_expect_success 'format-patch --base overrides format.useAutoBase' '
1560+
test_when_finished "git config --unset format.useAutoBase" &&
1561+
git config format.useAutoBase true &&
1562+
git format-patch --stdout --base=HEAD~1 -1 >patch &&
1563+
grep "^base-commit:" patch >actual &&
1564+
echo "base-commit: $(git rev-parse HEAD~1)" >expected &&
1565+
test_cmp expected actual
1566+
'
1567+
15491568
test_done

0 commit comments

Comments
 (0)