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 <xiaolong.ye@intel.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Xiaolong Ye committed
Apr 26, 2016 at 15:51 UTC
bb52995f3ec7fac2b282a91af4230e4f387af234
3 files changed
+34
-6
Documentation/config.txt
+4
@@ -1258,6 +1258,10 @@ format.outputDirectory::
1258
Set a custom directory to store the resulting files instead of the
1259
current working directory.
1260
1261
+format.useAutoBase::
1262
+ A boolean value which lets you enable the `--base=auto` option of
1263
+ format-patch by default.
1264
+
1265
filter.<driver>.clean::
1266
The command which is used to convert the content of a worktree
1267
file to a blob upon checkin. See linkgit:gitattributes[5] for
builtin/log.c
+11
-6
@@ -696,6 +696,7 @@ static void add_header(const char *value)
696
#define THREAD_DEEP 2
697
static int thread;
698
static int do_signoff;
699
+static int base_auto;
700
static const char *signature = git_version_string;
701
static const char *signature_file;
702
static int config_cover_letter;
@@ -780,6 +781,10 @@ static int git_format_config(const char *var, const char *value, void *cb)
781
}
782
if (!strcmp(var, "format.outputdirectory"))
783
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
+ }
788
789
return git_log_config(var, value, cb);
790
}
@@ -1199,7 +1204,11 @@ static struct commit *get_base_commit(const char *base_commit,
1204
struct commit **rev;
1205
int i = 0, rev_nr = 0;
1206
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) {
1212
struct branch *curr_branch = branch_get(NULL);
1213
const char *upstream = branch_get_upstream(curr_branch, NULL);
1214
if (upstream) {
@@ -1221,10 +1230,6 @@ static struct commit *get_base_commit(const char *base_commit,
1230
"please use git branch --set-upstream-to to track a remote branch.\n"
1231
"Or you could specify base commit by --base=<base-commit-id> manually."));
1232
}
1224
- } else {
1225
- base = lookup_commit_reference_by_name(base_commit);
1226
- if (!base)
1227
- die(_("Unknown commit %s"), base_commit);
1233
}
1234
1235
ALLOC_ARRAY(rev, total);
@@ -1662,7 +1667,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1667
}
1668
1669
memset(&bases, 0, sizeof(bases));
1665
- if (base_commit) {
1670
+ if (base_commit || base_auto) {
1671
struct commit *base = get_base_commit(base_commit, list, nr);
1672
reset_revision_walk();
1673
prepare_bases(&bases, base, list, nr);
t/t4014-format-patch.sh
+19
@@ -1546,4 +1546,23 @@ test_expect_success 'format-patch errors out when history involves criss-cross'
1546
test_must_fail git format-patch --base=auto -1
1547
'
1548
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
+
1568
test_done