rebase -i: support --committer-date-is-author-date
Rebase is implemented with two different backends - 'apply' and 'merge' each of which support a different set of options. In particular the apply backend supports a number of options implemented by 'git am' that are not implemented in the merge backend. This means that the available options are different depending on which backend is used which is confusing. This patch adds support for the --committer-date-is-author-date option to the merge backend. This option uses the author date of the commit that is being rewritten as the committer date when the new commit is created. Original-patch-by: Rohit Ashiwal <rohit.ashiwal265@gmail.com> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Phillip Wood committed
Aug 17, 2020 at 18:40 UTC
7573cec52c0274ceb166c425be4288f6b3103d6f
6 files changed
+162
-13
Documentation/git-rebase.txt
+7
-3
@@ -445,9 +445,13 @@ if the other side had no changes that conflicted.
445
See also INCOMPATIBLE OPTIONS below.
446
447
--committer-date-is-author-date::
448
+ Instead of using the current time as the committer date, use
449
+ the author date of the commit being rebased as the committer
450
+ date. This option implies `--force-rebase`.
451
+
452
--ignore-date::
449
- These flags are passed to 'git am' to easily change the dates
450
- of the rebased commits (see linkgit:git-am[1]).
453
+ This flag is passed to 'git am' to change the author date
454
+ of each rebased commit (see linkgit:git-am[1]).
455
+
456
See also INCOMPATIBLE OPTIONS below.
457
@@ -585,7 +589,6 @@ INCOMPATIBLE OPTIONS
589
The following options:
590
591
* --apply
588
- * --committer-date-is-author-date
592
* --ignore-date
593
* --whitespace
594
* -C
@@ -613,6 +616,7 @@ In addition, the following pairs of options are incompatible:
616
* --preserve-merges and --rebase-merges
617
* --preserve-merges and --empty=
618
* --preserve-merges and --ignore-whitespace
619
+ * --preserve-merges and --committer-date-is-author-date
620
* --keep-base and --onto
621
* --keep-base and --root
622
builtin/rebase.c
+12
-5
@@ -88,6 +88,7 @@ struct rebase_options {
88
int autosquash;
89
char *gpg_sign_opt;
90
int autostash;
91
+ int committer_date_is_author_date;
92
char *cmd;
93
int allow_empty_message;
94
int rebase_merges, rebase_cousins;
@@ -124,6 +125,8 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
125
replay.quiet = !(opts->flags & REBASE_NO_QUIET);
126
replay.verbose = opts->flags & REBASE_VERBOSE;
127
replay.reschedule_failed_exec = opts->reschedule_failed_exec;
128
+ replay.committer_date_is_author_date =
129
+ opts->committer_date_is_author_date;
130
replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
131
replay.strategy = opts->strategy;
132
@@ -1497,9 +1500,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1500
PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
1501
OPT_BOOL(0, "signoff", &options.signoff,
1502
N_("add a Signed-off-by: line to each commit")),
1500
- OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
1501
- &options.git_am_opts, NULL,
1502
- N_("passed to 'git am'"), PARSE_OPT_NOARG),
1503
+ OPT_BOOL(0, "committer-date-is-author-date",
1504
+ &options.committer_date_is_author_date,
1505
+ N_("make committer date match author date")),
1506
OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL,
1507
N_("passed to 'git am'"), PARSE_OPT_NOARG),
1508
OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
@@ -1794,11 +1797,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1797
options.autosquash) {
1798
allow_preemptive_ff = 0;
1799
}
1800
+ if (options.committer_date_is_author_date)
1801
+ options.flags |= REBASE_FORCE;
1802
1803
for (i = 0; i < options.git_am_opts.argc; i++) {
1804
const char *option = options.git_am_opts.argv[i], *p;
1800
- if (!strcmp(option, "--committer-date-is-author-date") ||
1801
- !strcmp(option, "--ignore-date") ||
1805
+ if (!strcmp(option, "--ignore-date") ||
1806
!strcmp(option, "--whitespace=fix") ||
1807
!strcmp(option, "--whitespace=strip"))
1808
allow_preemptive_ff = 0;
@@ -1855,6 +1859,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1859
if (ignore_whitespace)
1860
argv_array_push(&options.git_am_opts,
1861
"--ignore-whitespace");
1862
+ if (options.committer_date_is_author_date)
1863
+ argv_array_push(&options.git_am_opts,
1864
+ "--committer-date-is-author-date");
1865
} else {
1866
/* REBASE_MERGE and PRESERVE_MERGES */
1867
if (ignore_whitespace) {
sequencer.c
+83
-3
@@ -149,6 +149,7 @@ static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
149
* command-line.
150
*/
151
static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
152
+static GIT_PATH_FUNC(rebase_path_cdate_is_adate, "rebase-merge/cdate_is_adate")
153
static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head")
154
static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose")
155
static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet")
@@ -302,6 +303,8 @@ int sequencer_remove_state(struct replay_opts *opts)
303
}
304
}
305
306
+ free(opts->committer_name);
307
+ free(opts->committer_email);
308
free(opts->gpg_sign);
309
free(opts->strategy);
310
for (i = 0; i < opts->xopts_nr; i++)
@@ -872,6 +875,22 @@ static char *get_author(const char *message)
875
return NULL;
876
}
877
878
+static const char *author_date_from_env_array(const struct argv_array *env)
879
+{
880
+ int i;
881
+ const char *date;
882
+
883
+ for (i = 0; i < env->argc; i++)
884
+ if (skip_prefix(env->argv[i],
885
+ "GIT_AUTHOR_DATE=", &date))
886
+ return date;
887
+ /*
888
+ * If GIT_AUTHOR_DATE is missing we should have already errored out when
889
+ * reading the script
890
+ */
891
+ BUG("GIT_AUTHOR_DATE missing from author script");
892
+}
893
+
894
static const char staged_changes_advice[] =
895
N_("you have staged changes in your working tree\n"
896
"If these changes are meant to be squashed into the previous commit, run:\n"
@@ -938,6 +957,10 @@ static int run_git_commit(struct repository *r,
957
gpg_opt, gpg_opt);
958
}
959
960
+ if (opts->committer_date_is_author_date)
961
+ argv_array_pushf(&cmd.env_array, "GIT_COMMITTER_DATE=%s",
962
+ author_date_from_env_array(&cmd.env_array));
963
+
964
argv_array_push(&cmd.args, "commit");
965
966
if (!(flags & VERIFY_MSG))
@@ -1315,6 +1338,7 @@ static int try_to_commit(struct repository *r,
1338
struct strbuf err = STRBUF_INIT;
1339
struct strbuf commit_msg = STRBUF_INIT;
1340
char *amend_author = NULL;
1341
+ const char *committer = NULL;
1342
const char *hook_commit = NULL;
1343
enum commit_msg_cleanup_mode cleanup;
1344
int res = 0;
@@ -1406,10 +1430,32 @@ static int try_to_commit(struct repository *r,
1430
goto out;
1431
}
1432
1409
- reset_ident_date();
1433
+ if (opts->committer_date_is_author_date) {
1434
+ struct ident_split id;
1435
+ struct strbuf date = STRBUF_INIT;
1436
+
1437
+ if (split_ident_line(&id, author, (int)strlen(author)) < 0) {
1438
+ res = error(_("invalid author identity '%s'"), author);
1439
+ goto out;
1440
+ }
1441
+ if (!id.date_begin) {
1442
+ res = error(_("corrupt author: missing date information"));
1443
+ goto out;
1444
+ }
1445
+ strbuf_addf(&date, "@%.*s %.*s",
1446
+ (int)(id.date_end - id.date_begin), id.date_begin,
1447
+ (int)(id.tz_end - id.tz_begin), id.tz_begin);
1448
+ committer = fmt_ident(opts->committer_name,
1449
+ opts->committer_email,
1450
+ WANT_COMMITTER_IDENT, date.buf,
1451
+ IDENT_STRICT);
1452
+ strbuf_release(&date);
1453
+ } else {
1454
+ reset_ident_date();
1455
+ }
1456
1457
if (commit_tree_extended(msg->buf, msg->len, &tree, parents, oid,
1412
- author, NULL, opts->gpg_sign, extra)) {
1458
+ author, committer, opts->gpg_sign, extra)) {
1459
res = error(_("failed to write commit object"));
1460
goto out;
1461
}
@@ -2532,6 +2578,11 @@ static int read_populate_opts(struct replay_opts *opts)
2578
opts->signoff = 1;
2579
}
2580
2581
+ if (file_exists(rebase_path_cdate_is_adate())) {
2582
+ opts->allow_ff = 0;
2583
+ opts->committer_date_is_author_date = 1;
2584
+ }
2585
+
2586
if (file_exists(rebase_path_reschedule_failed_exec()))
2587
opts->reschedule_failed_exec = 1;
2588
@@ -2622,6 +2673,8 @@ int write_basic_state(struct replay_opts *opts, const char *head_name,
2673
write_file(rebase_path_drop_redundant_commits(), "%s", "");
2674
if (opts->keep_redundant_commits)
2675
write_file(rebase_path_keep_redundant_commits(), "%s", "");
2676
+ if (opts->committer_date_is_author_date)
2677
+ write_file(rebase_path_cdate_is_adate(), "%s", "");
2678
if (opts->reschedule_failed_exec)
2679
write_file(rebase_path_reschedule_failed_exec(), "%s", "");
2680
@@ -3542,6 +3595,10 @@ static int do_merge(struct repository *r,
3595
goto leave_merge;
3596
}
3597
3598
+ if (opts->committer_date_is_author_date)
3599
+ argv_array_pushf(&cmd.env_array, "GIT_COMMITTER_DATE=%s",
3600
+ author_date_from_env_array(&cmd.env_array));
3601
+
3602
cmd.git_cmd = 1;
3603
argv_array_push(&cmd.args, "merge");
3604
argv_array_push(&cmd.args, "-s");
@@ -3819,7 +3876,8 @@ static int pick_commits(struct repository *r,
3876
setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
3877
if (opts->allow_ff)
3878
assert(!(opts->signoff || opts->no_commit ||
3822
- opts->record_origin || opts->edit));
3879
+ opts->record_origin || opts->edit ||
3880
+ opts->committer_date_is_author_date));
3881
if (read_and_refresh_cache(r, opts))
3882
return -1;
3883
@@ -4258,6 +4316,22 @@ static int commit_staged_changes(struct repository *r,
4316
return 0;
4317
}
4318
4319
+static int init_committer(struct replay_opts *opts)
4320
+{
4321
+ struct ident_split id;
4322
+ const char *committer;
4323
+
4324
+ committer = git_committer_info(IDENT_STRICT);
4325
+ if (split_ident_line(&id, committer, strlen(committer)) < 0)
4326
+ return error(_("invalid committer '%s'"), committer);
4327
+ opts->committer_name =
4328
+ xmemdupz(id.name_begin, id.name_end - id.name_begin);
4329
+ opts->committer_email =
4330
+ xmemdupz(id.mail_begin, id.mail_end - id.mail_end);
4331
+
4332
+ return 0;
4333
+}
4334
+
4335
int sequencer_continue(struct repository *r, struct replay_opts *opts)
4336
{
4337
struct todo_list todo_list = TODO_LIST_INIT;
@@ -4269,6 +4343,9 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts)
4343
if (read_populate_opts(opts))
4344
return -1;
4345
if (is_rebase_i(opts)) {
4346
+ if (opts->committer_date_is_author_date && init_committer(opts))
4347
+ return -1;
4348
+
4349
if ((res = read_populate_todo(r, &todo_list, opts)))
4350
goto release_todo_list;
4351
@@ -5145,6 +5222,9 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
5222
5223
res = -1;
5224
5225
+ if (opts->committer_date_is_author_date && init_committer(opts))
5226
+ goto cleanup;
5227
+
5228
if (checkout_onto(r, opts, onto_name, &oid, orig_head))
5229
goto cleanup;
5230
sequencer.h
+3
@@ -45,9 +45,12 @@ struct replay_opts {
45
int verbose;
46
int quiet;
47
int reschedule_failed_exec;
48
+ int committer_date_is_author_date;
49
50
int mainline;
51
52
+ char *committer_name;
53
+ char *committer_email;
54
char *gpg_sign;
55
enum commit_msg_cleanup_mode default_msg_cleanup;
56
int explicit_cleanup;
t/t3422-rebase-incompatible-options.sh
-1
@@ -61,7 +61,6 @@ test_rebase_am_only () {
61
}
62
63
test_rebase_am_only --whitespace=fix
64
-test_rebase_am_only --committer-date-is-author-date
64
test_rebase_am_only -C4
65
66
test_expect_success REBASE_P '--preserve-merges incompatible with --signoff' '
t/t3436-rebase-more-options.sh
+57
-1
@@ -9,6 +9,9 @@ test_description='tests to ensure compatibility between am and interactive backe
9
10
. "$TEST_DIRECTORY"/lib-rebase.sh
11
12
+GIT_AUTHOR_DATE="1999-04-02T08:03:20+05:30"
13
+export GIT_AUTHOR_DATE
14
+
15
# This is a special case in which both am and interactive backends
16
# provide the same output. It was done intentionally because
17
# both the backends fall short of optimal behaviour.
@@ -21,11 +24,20 @@ test_expect_success 'setup' '
24
test_write_lines "line 1" "new line 2" "line 3" >file &&
25
git commit -am "update file" &&
26
git tag side &&
27
+ test_commit commit1 foo foo1 &&
28
+ test_commit commit2 foo foo2 &&
29
+ test_commit commit3 foo foo3 &&
30
31
git checkout --orphan master &&
32
+ rm foo &&
33
test_write_lines "line 1" " line 2" "line 3" >file &&
34
git commit -am "add file" &&
28
- git tag main
35
+ git tag main &&
36
+
37
+ mkdir test-bin &&
38
+ write_script test-bin/git-merge-test <<-\EOF
39
+ exec git-merge-recursive "$@"
40
+ EOF
41
'
42
43
test_expect_success '--ignore-whitespace works with apply backend' '
@@ -52,6 +64,50 @@ test_expect_success '--ignore-whitespace is remembered when continuing' '
64
git diff --exit-code side
65
'
66
67
+test_ctime_is_atime () {
68
+ git log $1 --format=%ai >authortime &&
69
+ git log $1 --format=%ci >committertime &&
70
+ test_cmp authortime committertime
71
+}
72
+
73
+test_expect_success '--committer-date-is-author-date works with apply backend' '
74
+ GIT_AUTHOR_DATE="@1234 +0300" git commit --amend --reset-author &&
75
+ git rebase --apply --committer-date-is-author-date HEAD^ &&
76
+ test_ctime_is_atime -1
77
+'
78
+
79
+test_expect_success '--committer-date-is-author-date works with merge backend' '
80
+ GIT_AUTHOR_DATE="@1234 +0300" git commit --amend --reset-author &&
81
+ git rebase -m --committer-date-is-author-date HEAD^ &&
82
+ test_ctime_is_atime -1
83
+'
84
+
85
+test_expect_success '--committer-date-is-author-date works with rebase -r' '
86
+ git checkout side &&
87
+ GIT_AUTHOR_DATE="@1234 +0300" git merge --no-ff commit3 &&
88
+ git rebase -r --root --committer-date-is-author-date &&
89
+ test_ctime_is_atime
90
+'
91
+
92
+test_expect_success '--committer-date-is-author-date works when forking merge' '
93
+ git checkout side &&
94
+ GIT_AUTHOR_DATE="@1234 +0300" git merge --no-ff commit3 &&
95
+ PATH="./test-bin:$PATH" git rebase -r --root --strategy=test \
96
+ --committer-date-is-author-date &&
97
+ test_ctime_is_atime
98
+'
99
+
100
+test_expect_success '--committer-date-is-author-date works when committing conflict resolution' '
101
+ git checkout commit2 &&
102
+ GIT_AUTHOR_DATE="@1980 +0000" git commit --amend --only --reset-author &&
103
+ test_must_fail git rebase -m --committer-date-is-author-date \
104
+ --onto HEAD^^ HEAD^ &&
105
+ echo resolved > foo &&
106
+ git add foo &&
107
+ git rebase --continue &&
108
+ test_ctime_is_atime -1
109
+'
110
+
111
# This must be the last test in this file
112
test_expect_success '$EDITOR and friends are unchanged' '
113
test_editor_unchanged