Merge branch 'pw/rebase-i-more-options' into pu

"git rebase -i" learns a bit more options.

Not quite.
cf. <nycvar.QRO.7.76.6.2005290437350.56@tvgsbejvaqbjf.bet>

* pw/rebase-i-more-options:
  rebase: add --reset-author-date
  rebase -i: support --ignore-date
  sequencer: rename amend_author to author_to_free
  rebase -i: support --committer-date-is-author-date
  rebase -i: add --ignore-whitespace flag
This commit is contained in:
Junio C Hamano
2020-06-19 14:52:42 -07:00
6 changed files with 379 additions and 24 deletions

View File

@ -459,17 +459,38 @@ with `--keep-base` in order to drop those commits from your branch.
See also INCOMPATIBLE OPTIONS below.
--ignore-whitespace::
Ignore whitespace differences when trying to reconcile
differences. Currently, each backend implements an approximation of
this behavior:
+
apply backend: When applying a patch, ignore changes in whitespace in
context lines. Unfortunately, this means that if the "old" lines being
replaced by the patch differ only in whitespace from the existing
file, you will get a merge conflict instead of a successful patch
application.
+
merge backend: Treat lines with only whitespace changes as unchanged
when merging. Unfortunately, this means that any patch hunks that were
intended to modify whitespace and nothing else will be dropped, even
if the other side had no changes that conflicted.
--whitespace=<option>::
These flags are passed to the 'git apply' program
This flag is passed to the 'git apply' program
(see linkgit:git-apply[1]) that applies the patch.
Implies --apply.
+
See also INCOMPATIBLE OPTIONS below.
--committer-date-is-author-date::
Instead of using the current time as the committer date, use
the author date of the commit being rebased as the committer
date. This option implies --force-rebase.
--ignore-date::
These flags are passed to 'git am' to easily change the dates
of the rebased commits (see linkgit:git-am[1]).
--reset-author-date::
Instead of using the author date of the original commit, use
the current time as the author date of the rebased commit. This
option implies `--force-rebase`.
+
See also INCOMPATIBLE OPTIONS below.
@ -607,9 +628,6 @@ INCOMPATIBLE OPTIONS
The following options:
* --apply
* --committer-date-is-author-date
* --ignore-date
* --ignore-whitespace
* --whitespace
* -C
@ -636,6 +654,9 @@ In addition, the following pairs of options are incompatible:
* --preserve-merges and --signoff
* --preserve-merges and --rebase-merges
* --preserve-merges and --empty=
* --preserve-merges and --ignore-whitespace
* --preserve-merges and --committer-date-is-author-date
* --preserve-merges and --ignore-date
* --keep-base and --onto
* --keep-base and --root
* --fork-point and --root

View File

@ -90,8 +90,11 @@ struct rebase_options {
int allow_rerere_autoupdate;
int keep_empty;
int autosquash;
int ignore_whitespace;
char *gpg_sign_opt;
int autostash;
int committer_date_is_author_date;
int ignore_date;
char *cmd;
int allow_empty_message;
int rebase_merges, rebase_cousins;
@ -114,6 +117,7 @@ struct rebase_options {
static struct replay_opts get_replay_opts(const struct rebase_options *opts)
{
struct strbuf strategy_buf = STRBUF_INIT;
struct replay_opts replay = REPLAY_OPTS_INIT;
replay.action = REPLAY_INTERACTIVE_REBASE;
@ -130,16 +134,25 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
replay.quiet = !(opts->flags & REBASE_NO_QUIET);
replay.verbose = opts->flags & REBASE_VERBOSE;
replay.reschedule_failed_exec = opts->reschedule_failed_exec;
replay.committer_date_is_author_date =
opts->committer_date_is_author_date;
replay.ignore_date = opts->ignore_date;
replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
replay.strategy = opts->strategy;
if (opts->strategy_opts)
parse_strategy_opts(&replay, opts->strategy_opts);
strbuf_addstr(&strategy_buf, opts->strategy_opts);
if (opts->ignore_whitespace)
strbuf_addstr(&strategy_buf, " --ignore-space-change");
if (strategy_buf.len)
parse_strategy_opts(&replay, strategy_buf.buf);
if (opts->squash_onto) {
oidcpy(&replay.squash_onto, opts->squash_onto);
replay.have_squash_onto = 1;
}
strbuf_release(&strategy_buf);
return replay;
}
@ -546,6 +559,8 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options,
builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0);
opts.strategy_opts = xstrdup_or_null(opts.strategy_opts);
if (!is_null_oid(&squash_onto))
opts.squash_onto = &squash_onto;
@ -813,6 +828,12 @@ static int run_am(struct rebase_options *opts)
am.git_cmd = 1;
argv_array_push(&am.args, "am");
if (opts->ignore_whitespace)
argv_array_push(&am.args, "--ignore-whitespace");
if (opts->committer_date_is_author_date)
argv_array_push(&opts->git_am_opts, "--committer-date-is-author-date");
if (opts->ignore_date)
argv_array_push(&opts->git_am_opts, "--ignore-date");
if (opts->action && !strcmp("continue", opts->action)) {
argv_array_push(&am.args, "--resolved");
argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
@ -1318,16 +1339,17 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
OPT_BOOL(0, "signoff", &options.signoff,
N_("add a Signed-off-by: line to each commit")),
OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options.git_am_opts,
NULL, N_("passed to 'git am'"),
PARSE_OPT_NOARG),
OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
&options.git_am_opts, NULL,
N_("passed to 'git am'"), PARSE_OPT_NOARG),
OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL,
N_("passed to 'git am'"), PARSE_OPT_NOARG),
OPT_BOOL(0, "committer-date-is-author-date",
&options.committer_date_is_author_date,
N_("make committer date match author date")),
OPT_BOOL(0, "reset-author-date", &options.ignore_date,
N_("ignore author date and use current date")),
OPT_HIDDEN_BOOL(0, "ignore-date", &options.ignore_date,
N_("synonym of --reset-author-date")),
OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
N_("passed to 'git apply'"), 0),
OPT_BOOL(0, "ignore-whitespace", &options.ignore_whitespace,
N_("ignore changes in whitespace")),
OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
N_("action"), N_("passed to 'git apply'"), 0),
OPT_BIT('f', "force-rebase", &options.flags,
@ -1624,12 +1646,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
options.autosquash) {
allow_preemptive_ff = 0;
}
if (options.committer_date_is_author_date || options.ignore_date)
options.flags |= REBASE_FORCE;
for (i = 0; i < options.git_am_opts.argc; i++) {
const char *option = options.git_am_opts.argv[i], *p;
if (!strcmp(option, "--committer-date-is-author-date") ||
!strcmp(option, "--ignore-date") ||
!strcmp(option, "--whitespace=fix") ||
if (!strcmp(option, "--whitespace=fix") ||
!strcmp(option, "--whitespace=strip"))
allow_preemptive_ff = 0;
else if (skip_prefix(option, "-C", &p)) {

View File

@ -150,6 +150,8 @@ static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
* command-line.
*/
static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
static GIT_PATH_FUNC(rebase_path_cdate_is_adate, "rebase-merge/cdate_is_adate")
static GIT_PATH_FUNC(rebase_path_ignore_date, "rebase-merge/ignore_date")
static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head")
static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose")
static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet")
@ -863,6 +865,40 @@ static char *get_author(const char *message)
return NULL;
}
static const char *author_date_from_env_array(const struct argv_array *env)
{
int i;
const char *date;
for (i = 0; i < env->argc; i++)
if (skip_prefix(env->argv[i],
"GIT_AUTHOR_DATE=", &date))
return date;
/*
* If GIT_AUTHOR_DATE is missing we should have already errored out when
* reading the script
*/
BUG("GIT_AUTHOR_DATE missing from author script");
}
/* Construct a free()able author string with current time as the author date */
static char *ignore_author_date(const char *author)
{
int len = strlen(author);
struct ident_split ident;
struct strbuf new_author = STRBUF_INIT;
if (split_ident_line(&ident, author, len) < 0) {
error(_("malformed ident line '%s'"), author);
return NULL;
}
len = ident.mail_end - ident.name_begin + 1;
strbuf_addf(&new_author, "%.*s ", len, ident.name_begin);
datestamp(&new_author);
return strbuf_detach(&new_author, NULL);
}
static const char staged_changes_advice[] =
N_("you have staged changes in your working tree\n"
"If these changes are meant to be squashed into the previous commit, run:\n"
@ -929,6 +965,14 @@ static int run_git_commit(struct repository *r,
gpg_opt, gpg_opt);
}
if (opts->committer_date_is_author_date)
argv_array_pushf(&cmd.env_array, "GIT_COMMITTER_DATE=%s",
opts->ignore_date ?
"" :
author_date_from_env_array(&cmd.env_array));
if (opts->ignore_date)
argv_array_push(&cmd.env_array, "GIT_AUTHOR_DATE=");
argv_array_push(&cmd.args, "commit");
if (!(flags & VERIFY_MSG))
@ -1307,7 +1351,7 @@ static int try_to_commit(struct repository *r,
struct commit_extra_header *extra = NULL;
struct strbuf err = STRBUF_INIT;
struct strbuf commit_msg = STRBUF_INIT;
char *amend_author = NULL;
char *author_to_free = NULL;
const char *hook_commit = NULL;
enum commit_msg_cleanup_mode cleanup;
int res = 0;
@ -1329,7 +1373,7 @@ static int try_to_commit(struct repository *r,
strbuf_addstr(msg, orig_message);
hook_commit = "HEAD";
}
author = amend_author = get_author(message);
author = author_to_free = get_author(message);
unuse_commit_buffer(current_head, message);
if (!author) {
res = error(_("unable to parse commit author"));
@ -1342,6 +1386,31 @@ static int try_to_commit(struct repository *r,
commit_list_insert(current_head, &parents);
}
if (opts->committer_date_is_author_date) {
int len = strlen(author);
struct ident_split ident;
struct strbuf date = STRBUF_INIT;
if (split_ident_line(&ident, author, len) < 0) {
res = error(_("malformed ident line '%s'"), author);
goto out;
}
if (!ident.date_begin) {
res = error(_("corrupted author without date information"));
goto out;
}
strbuf_addf(&date, "@%.*s %.*s",
(int)(ident.date_end - ident.date_begin), ident.date_begin,
(int)(ident.tz_end - ident.tz_begin), ident.tz_begin);
res = setenv("GIT_COMMITTER_DATE",
opts->ignore_date ? "" : date.buf, 1);
strbuf_release(&date);
if (res)
goto out;
}
if (write_index_as_tree(&tree, r->index, r->index_file, 0, NULL)) {
res = error(_("git write-tree failed to write a tree"));
goto out;
@ -1401,6 +1470,16 @@ static int try_to_commit(struct repository *r,
reset_ident_date();
if (opts->ignore_date) {
author = ignore_author_date(author);
if (!author) {
res = -1;
goto out;
}
free(author_to_free);
author_to_free = (char *)author;
}
if (commit_tree_extended(msg->buf, msg->len, &tree, parents,
oid, author, opts->gpg_sign, extra)) {
res = error(_("failed to write commit object"));
@ -1421,7 +1500,7 @@ out:
free_commit_extra_headers(extra);
strbuf_release(&err);
strbuf_release(&commit_msg);
free(amend_author);
free(author_to_free);
return res;
}
@ -2528,6 +2607,16 @@ static int read_populate_opts(struct replay_opts *opts)
opts->signoff = 1;
}
if (file_exists(rebase_path_cdate_is_adate())) {
opts->allow_ff = 0;
opts->committer_date_is_author_date = 1;
}
if (file_exists(rebase_path_ignore_date())) {
opts->allow_ff = 0;
opts->ignore_date = 1;
}
if (file_exists(rebase_path_reschedule_failed_exec()))
opts->reschedule_failed_exec = 1;
@ -2623,6 +2712,10 @@ int write_basic_state(struct replay_opts *opts, const char *head_name,
write_file(rebase_path_drop_redundant_commits(), "%s", "");
if (opts->keep_redundant_commits)
write_file(rebase_path_keep_redundant_commits(), "%s", "");
if (opts->committer_date_is_author_date)
write_file(rebase_path_cdate_is_adate(), "%s", "");
if (opts->ignore_date)
write_file(rebase_path_ignore_date(), "%s", "");
if (opts->reschedule_failed_exec)
write_file(rebase_path_reschedule_failed_exec(), "%s", "");
@ -3543,6 +3636,14 @@ static int do_merge(struct repository *r,
goto leave_merge;
}
if (opts->committer_date_is_author_date)
argv_array_pushf(&cmd.env_array, "GIT_COMMITTER_DATE=%s",
opts->ignore_date ?
"" :
author_date_from_env_array(&cmd.env_array));
if (opts->ignore_date)
argv_array_push(&cmd.env_array, "GIT_AUTHOR_DATE=");
cmd.git_cmd = 1;
argv_array_push(&cmd.args, "merge");
argv_array_push(&cmd.args, "-s");
@ -3906,7 +4007,9 @@ static int pick_commits(struct repository *r,
prev_reflog_action = xstrdup(getenv(GIT_REFLOG_ACTION));
if (opts->allow_ff)
assert(!(opts->signoff || opts->no_commit ||
opts->record_origin || opts->edit));
opts->record_origin || opts->edit ||
opts->committer_date_is_author_date ||
opts->ignore_date));
if (read_and_refresh_cache(r, opts))
return -1;

View File

@ -45,6 +45,8 @@ struct replay_opts {
int verbose;
int quiet;
int reschedule_failed_exec;
int committer_date_is_author_date;
int ignore_date;
int mainline;

View File

@ -61,8 +61,6 @@ test_rebase_am_only () {
}
test_rebase_am_only --whitespace=fix
test_rebase_am_only --ignore-whitespace
test_rebase_am_only --committer-date-is-author-date
test_rebase_am_only -C4
test_expect_success REBASE_P '--preserve-merges incompatible with --signoff' '

209
t/t3436-rebase-more-options.sh Executable file
View File

@ -0,0 +1,209 @@
#!/bin/sh
#
# Copyright (c) 2019 Rohit Ashiwal
#
test_description='tests to ensure compatibility between am and interactive backends'
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-rebase.sh
GIT_AUTHOR_DATE="1999-04-02T08:03:20+05:30"
export GIT_AUTHOR_DATE
# This is a special case in which both am and interactive backends
# provide the same output. It was done intentionally because
# both the backends fall short of optimal behaviour.
test_expect_success 'setup' '
git checkout -b topic &&
q_to_tab >file <<-\EOF &&
line 1
Qline 2
line 3
EOF
git add file &&
git commit -m "add file" &&
cat >file <<-\EOF &&
line 1
new line 2
line 3
EOF
git commit -am "update file" &&
git tag side &&
test_commit commit1 foo foo1 &&
test_commit commit2 foo foo2 &&
test_commit commit3 foo foo3 &&
git checkout --orphan master &&
git rm --cached foo &&
rm foo &&
sed -e "s/^|//" >file <<-\EOF &&
|line 1
| line 2
|line 3
EOF
git add file &&
git commit -m "add file" &&
git tag main
'
test_expect_success '--ignore-whitespace works with apply backend' '
cat >expect <<-\EOF &&
line 1
new line 2
line 3
EOF
test_must_fail git rebase --apply main side &&
git rebase --abort &&
git rebase --apply --ignore-whitespace main side &&
test_cmp expect file
'
test_expect_success '--ignore-whitespace works with merge backend' '
cat >expect <<-\EOF &&
line 1
new line 2
line 3
EOF
test_must_fail git rebase --merge main side &&
git rebase --abort &&
git rebase --merge --ignore-whitespace main side &&
test_cmp expect file
'
test_expect_success '--ignore-whitespace is remembered when continuing' '
cat >expect <<-\EOF &&
line 1
new line 2
line 3
EOF
(
set_fake_editor &&
FAKE_LINES="break 1" git rebase -i --ignore-whitespace main side
) &&
git rebase --continue &&
test_cmp expect file
'
test_expect_success '--committer-date-is-author-date works with apply backend' '
GIT_AUTHOR_DATE="@1234 +0300" git commit --amend --reset-author &&
git rebase --apply --committer-date-is-author-date HEAD^ &&
git log -1 --pretty=%ai >authortime &&
git log -1 --pretty=%ci >committertime &&
test_cmp authortime committertime
'
test_expect_success '--committer-date-is-author-date works with merge backend' '
GIT_AUTHOR_DATE="@1234 +0300" git commit --amend --reset-author &&
git rebase -m --committer-date-is-author-date HEAD^ &&
git log -1 --pretty=%ai >authortime &&
git log -1 --pretty=%ci >committertime &&
test_cmp authortime committertime
'
test_expect_success '--committer-date-is-author-date works with rebase -r' '
git checkout side &&
GIT_AUTHOR_DATE="@1234 +0300" git merge --no-ff commit3 &&
git rebase -r --root --committer-date-is-author-date &&
git log --pretty=%ai >authortime &&
git log --pretty=%ci >committertime &&
test_cmp authortime committertime
'
test_expect_success '--committer-date-is-author-date works when forking merge' '
git checkout side &&
GIT_AUTHOR_DATE="@1234 +0300" git merge --no-ff commit3 &&
git rebase -r --root --strategy=resolve --committer-date-is-author-date &&
git log --pretty=%ai >authortime &&
git log --pretty=%ci >committertime &&
test_cmp authortime committertime
'
test_expect_success '--committer-date-is-author-date works when committing conflict resolution' '
git checkout commit2 &&
GIT_AUTHOR_DATE="@1980 +0000" git commit --amend --only --reset-author &&
git log -1 --format=%at HEAD >expect &&
test_must_fail git rebase -m --committer-date-is-author-date \
--onto HEAD^^ HEAD^ &&
echo resolved > foo &&
git add foo &&
git rebase --continue &&
git log -1 --format=%ct HEAD >actual &&
test_cmp expect actual
'
# Checking for +0000 in author time is enough since default
# timezone is UTC, but the timezone used while committing
# sets to +0530.
test_expect_success '--reset-author-date works with apply backend' '
git commit --amend --date="$GIT_AUTHOR_DATE" &&
git rebase --apply --reset-author-date HEAD^ &&
git log -1 --pretty=%ai >authortime &&
grep "+0000" authortime
'
test_expect_success '--reset-author-date works with merge backend' '
git commit --amend --date="$GIT_AUTHOR_DATE" &&
git rebase --reset-author-date -m HEAD^ &&
git log -1 --pretty=%ai >authortime &&
grep "+0000" authortime
'
test_expect_success '--reset-author-date works after conflict resolution' '
test_must_fail git rebase --reset-author-date -m \
--onto commit2^^ commit2^ commit2 &&
echo resolved >foo &&
git add foo &&
git rebase --continue &&
git log -1 --pretty=%ai >authortime &&
grep +0000 authortime
'
test_expect_success '--reset-author-date works with rebase -r' '
git checkout side &&
git merge --no-ff commit3 &&
git rebase -r --root --reset-author-date &&
git log --pretty=%ai >authortime &&
! grep -v "+0000" authortime
'
test_expect_success '--reset-author-date with --committer-date-is-author-date works' '
test_must_fail git rebase -m --committer-date-is-author-date \
--reset-author-date --onto commit2^^ commit2^ commit3 &&
git checkout --theirs foo &&
git add foo &&
git rebase --continue &&
git log -2 --pretty=%ai >authortime &&
git log -2 --pretty=%ci >committertime &&
test_cmp authortime committertime &&
! grep -v "+0000" authortime
'
test_expect_success '--reset-author-date --committer-date-is-author-date works when forking merge' '
GIT_SEQUENCE_EDITOR="echo \"merge -C $(git rev-parse HEAD) commit3\">" \
git rebase -i --strategy=resolve --reset-author-date \
--committer-date-is-author-date side side &&
git log -1 --pretty=%ai >authortime &&
git log -1 --pretty=%ci >committertime &&
test_cmp authortime committertime &&
grep "+0000" authortime
'
test_expect_success '--ignore-date is an alias for --reset-author-date' '
git commit --amend --date="$GIT_AUTHOR_DATE" &&
git rebase --apply --ignore-date HEAD^ &&
git commit --allow-empty -m empty --date="$GIT_AUTHOR_DATE" &&
git rebase -m --ignore-date HEAD^ &&
git log -2 --pretty=%ai >authortime &&
grep "+0000" authortime >output &&
test_line_count = 2 output
'
# This must be the last test in this file
test_expect_success '$EDITOR and friends are unchanged' '
test_editor_unchanged
'
test_done