Merge branch 'nd/rebase-show-current-patch'

The new "--show-current-patch" option gives an end-user facing way
to get the diff being applied when "git rebase" (and "git am")
stops with a conflict.

* nd/rebase-show-current-patch:
  rebase: introduce and use pseudo-ref REBASE_HEAD
  rebase: add --show-current-patch
  am: add --show-current-patch
This commit is contained in:
Junio C Hamano 2018-03-06 14:54:02 -08:00
commit 9ca488c04b
12 changed files with 130 additions and 11 deletions

View File

@ -16,7 +16,7 @@ SYNOPSIS
[--exclude=<path>] [--include=<path>] [--reject] [-q | --quiet] [--exclude=<path>] [--include=<path>] [--reject] [-q | --quiet]
[--[no-]scissors] [-S[<keyid>]] [--patch-format=<format>] [--[no-]scissors] [-S[<keyid>]] [--patch-format=<format>]
[(<mbox> | <Maildir>)...] [(<mbox> | <Maildir>)...]
'git am' (--continue | --skip | --abort | --quit) 'git am' (--continue | --skip | --abort | --quit | --show-current-patch)
DESCRIPTION DESCRIPTION
----------- -----------
@ -171,6 +171,10 @@ default. You can use `--no-utf8` to override this.
Abort the patching operation but keep HEAD and the index Abort the patching operation but keep HEAD and the index
untouched. untouched.
--show-current-patch::
Show the patch being applied when "git am" is stopped because
of conflicts.
DISCUSSION DISCUSSION
---------- ----------

View File

@ -12,7 +12,7 @@ SYNOPSIS
[<upstream> [<branch>]] [<upstream> [<branch>]]
'git rebase' [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>] 'git rebase' [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>]
--root [<branch>] --root [<branch>]
'git rebase' --continue | --skip | --abort | --quit | --edit-todo 'git rebase' --continue | --skip | --abort | --quit | --edit-todo | --show-current-patch
DESCRIPTION DESCRIPTION
----------- -----------
@ -255,6 +255,11 @@ leave out at most one of A and B, in which case it defaults to HEAD.
--edit-todo:: --edit-todo::
Edit the todo list during an interactive rebase. Edit the todo list during an interactive rebase.
--show-current-patch::
Show the current patch in an interactive rebase or when rebase
is stopped because of conflicts. This is the equivalent of
`git show REBASE_HEAD`.
-m:: -m::
--merge:: --merge::
Use merging strategies to rebase. When the recursive (default) merge Use merging strategies to rebase. When the recursive (default) merge

View File

@ -1011,6 +1011,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
if (mkdir(state->dir, 0777) < 0 && errno != EEXIST) if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
die_errno(_("failed to create directory '%s'"), state->dir); die_errno(_("failed to create directory '%s'"), state->dir);
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
if (split_mail(state, patch_format, paths, keep_cr) < 0) { if (split_mail(state, patch_format, paths, keep_cr) < 0) {
am_destroy(state); am_destroy(state);
@ -1110,6 +1111,7 @@ static void am_next(struct am_state *state)
oidclr(&state->orig_commit); oidclr(&state->orig_commit);
unlink(am_path(state, "original-commit")); unlink(am_path(state, "original-commit"));
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
if (!get_oid("HEAD", &head)) if (!get_oid("HEAD", &head))
write_state_text(state, "abort-safety", oid_to_hex(&head)); write_state_text(state, "abort-safety", oid_to_hex(&head));
@ -1441,6 +1443,8 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
oidcpy(&state->orig_commit, &commit_oid); oidcpy(&state->orig_commit, &commit_oid);
write_state_text(state, "original-commit", oid_to_hex(&commit_oid)); write_state_text(state, "original-commit", oid_to_hex(&commit_oid));
update_ref("am", "REBASE_HEAD", &commit_oid,
NULL, REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR);
return 0; return 0;
} }
@ -1831,8 +1835,7 @@ static void am_run(struct am_state *state, int resume)
git_config_get_bool("advice.amworkdir", &advice_amworkdir); git_config_get_bool("advice.amworkdir", &advice_amworkdir);
if (advice_amworkdir) if (advice_amworkdir)
printf_ln(_("The copy of the patch that failed is found in: %s"), printf_ln(_("Use 'git am --show-current-patch' to see the failed patch"));
am_path(state, "patch"));
die_user_resolve(state); die_user_resolve(state);
} }
@ -2121,6 +2124,34 @@ static void am_abort(struct am_state *state)
am_destroy(state); am_destroy(state);
} }
static int show_patch(struct am_state *state)
{
struct strbuf sb = STRBUF_INIT;
const char *patch_path;
int len;
if (!is_null_oid(&state->orig_commit)) {
const char *av[4] = { "show", NULL, "--", NULL };
char *new_oid_str;
int ret;
av[1] = new_oid_str = xstrdup(oid_to_hex(&state->orig_commit));
ret = run_command_v_opt(av, RUN_GIT_CMD);
free(new_oid_str);
return ret;
}
patch_path = am_path(state, msgnum(state));
len = strbuf_read_file(&sb, patch_path, 0);
if (len < 0)
die_errno(_("failed to read '%s'"), patch_path);
setup_pager();
write_in_full(1, sb.buf, sb.len);
strbuf_release(&sb);
return 0;
}
/** /**
* parse_options() callback that validates and sets opt->value to the * parse_options() callback that validates and sets opt->value to the
* PATCH_FORMAT_* enum value corresponding to `arg`. * PATCH_FORMAT_* enum value corresponding to `arg`.
@ -2150,7 +2181,8 @@ enum resume_mode {
RESUME_RESOLVED, RESUME_RESOLVED,
RESUME_SKIP, RESUME_SKIP,
RESUME_ABORT, RESUME_ABORT,
RESUME_QUIT RESUME_QUIT,
RESUME_SHOW_PATCH
}; };
static int git_am_config(const char *k, const char *v, void *cb) static int git_am_config(const char *k, const char *v, void *cb)
@ -2172,6 +2204,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
int patch_format = PATCH_FORMAT_UNKNOWN; int patch_format = PATCH_FORMAT_UNKNOWN;
enum resume_mode resume = RESUME_FALSE; enum resume_mode resume = RESUME_FALSE;
int in_progress; int in_progress;
int ret = 0;
const char * const usage[] = { const char * const usage[] = {
N_("git am [<options>] [(<mbox> | <Maildir>)...]"), N_("git am [<options>] [(<mbox> | <Maildir>)...]"),
@ -2253,6 +2286,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
OPT_CMDMODE(0, "quit", &resume, OPT_CMDMODE(0, "quit", &resume,
N_("abort the patching operation but keep HEAD where it is."), N_("abort the patching operation but keep HEAD where it is."),
RESUME_QUIT), RESUME_QUIT),
OPT_CMDMODE(0, "show-current-patch", &resume,
N_("show the patch being applied."),
RESUME_SHOW_PATCH),
OPT_BOOL(0, "committer-date-is-author-date", OPT_BOOL(0, "committer-date-is-author-date",
&state.committer_date_is_author_date, &state.committer_date_is_author_date,
N_("lie about committer date")), N_("lie about committer date")),
@ -2367,11 +2403,14 @@ int cmd_am(int argc, const char **argv, const char *prefix)
am_rerere_clear(); am_rerere_clear();
am_destroy(&state); am_destroy(&state);
break; break;
case RESUME_SHOW_PATCH:
ret = show_patch(&state);
break;
default: default:
die("BUG: invalid resume value"); die("BUG: invalid resume value");
} }
am_state_release(&state); am_state_release(&state);
return 0; return ret;
} }

View File

@ -439,7 +439,7 @@ __git_refs ()
track="" track=""
;; ;;
*) *)
for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD; do
case "$i" in case "$i" in
$match*) $match*)
if [ -e "$dir/$i" ]; then if [ -e "$dir/$i" ]; then
@ -1077,7 +1077,7 @@ _git_am ()
{ {
__git_find_repo_path __git_find_repo_path
if [ -d "$__git_repo_path"/rebase-apply ]; then if [ -d "$__git_repo_path"/rebase-apply ]; then
__gitcomp "--skip --continue --resolved --abort --quit" __gitcomp "--skip --continue --resolved --abort --quit --show-current-patch"
return return
fi fi
case "$cur" in case "$cur" in
@ -1992,11 +1992,11 @@ _git_rebase ()
{ {
__git_find_repo_path __git_find_repo_path
if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
__gitcomp "--continue --skip --abort --quit --edit-todo" __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
return return
elif [ -d "$__git_repo_path"/rebase-apply ] || \ elif [ -d "$__git_repo_path"/rebase-apply ] || \
[ -d "$__git_repo_path"/rebase-merge ]; then [ -d "$__git_repo_path"/rebase-merge ]; then
__gitcomp "--continue --skip --abort --quit" __gitcomp "--continue --skip --abort --quit --show-current-patch"
return return
fi fi
__git_complete_strategy && return __git_complete_strategy && return

View File

@ -27,6 +27,9 @@ skip)
move_to_original_branch move_to_original_branch
return return
;; ;;
show-current-patch)
exec git am --show-current-patch
;;
esac esac
if test -z "$rebase_root" if test -z "$rebase_root"

View File

@ -199,12 +199,14 @@ make_patch () {
die_with_patch () { die_with_patch () {
echo "$1" > "$state_dir"/stopped-sha echo "$1" > "$state_dir"/stopped-sha
git update-ref REBASE_HEAD "$1"
make_patch "$1" make_patch "$1"
die "$2" die "$2"
} }
exit_with_patch () { exit_with_patch () {
echo "$1" > "$state_dir"/stopped-sha echo "$1" > "$state_dir"/stopped-sha
git update-ref REBASE_HEAD "$1"
make_patch $1 make_patch $1
git rev-parse --verify HEAD > "$amend" git rev-parse --verify HEAD > "$amend"
gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
@ -844,6 +846,9 @@ To continue rebase after editing, run:
exit exit
;; ;;
show-current-patch)
exec git show REBASE_HEAD --
;;
esac esac
comment_for_reflog start comment_for_reflog start
@ -859,6 +864,7 @@ fi
orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")" orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")" mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
rm -f "$(git rev-parse --git-path REBASE_HEAD)"
: > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")" : > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
write_basic_state write_basic_state

View File

@ -58,6 +58,7 @@ call_merge () {
echo "$msgnum" >"$state_dir/msgnum" echo "$msgnum" >"$state_dir/msgnum"
cmt="$(cat "$state_dir/cmt.$msgnum")" cmt="$(cat "$state_dir/cmt.$msgnum")"
echo "$cmt" > "$state_dir/current" echo "$cmt" > "$state_dir/current"
git update-ref REBASE_HEAD "$cmt"
hd=$(git rev-parse --verify HEAD) hd=$(git rev-parse --verify HEAD)
cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD) cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"' eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
@ -138,11 +139,15 @@ skip)
finish_rb_merge finish_rb_merge
return return
;; ;;
show-current-patch)
exec git show REBASE_HEAD --
;;
esac esac
mkdir -p "$state_dir" mkdir -p "$state_dir"
echo "$onto_name" > "$state_dir/onto_name" echo "$onto_name" > "$state_dir/onto_name"
write_basic_state write_basic_state
rm -f "$(git rev-parse --git-path REBASE_HEAD)"
msgnum=0 msgnum=0
for cmt in $(git rev-list --reverse --no-merges "$revisions") for cmt in $(git rev-list --reverse --no-merges "$revisions")

View File

@ -46,6 +46,7 @@ abort! abort and check out the original branch
skip! skip current patch and continue skip! skip current patch and continue
edit-todo! edit the todo list during an interactive rebase edit-todo! edit the todo list during an interactive rebase
quit! abort but keep HEAD where it is quit! abort but keep HEAD where it is
show-current-patch! show the patch file being applied or merged
" "
. git-sh-setup . git-sh-setup
set_reflog_action rebase set_reflog_action rebase
@ -183,6 +184,7 @@ You can run "git stash pop" or "git stash drop" at any time.
} }
finish_rebase () { finish_rebase () {
rm -f "$(git rev-parse --git-path REBASE_HEAD)"
apply_autostash && apply_autostash &&
{ git gc --auto || true; } && { git gc --auto || true; } &&
rm -rf "$state_dir" rm -rf "$state_dir"
@ -247,7 +249,7 @@ do
--verify) --verify)
ok_to_skip_pre_rebase= ok_to_skip_pre_rebase=
;; ;;
--continue|--skip|--abort|--quit|--edit-todo) --continue|--skip|--abort|--quit|--edit-todo|--show-current-patch)
test $total_argc -eq 2 || usage test $total_argc -eq 2 || usage
action=${1##--} action=${1##--}
;; ;;
@ -417,6 +419,10 @@ quit)
edit-todo) edit-todo)
run_specific_rebase run_specific_rebase
;; ;;
show-current-patch)
run_specific_rebase
die "BUG: run_specific_rebase is not supposed to return here"
;;
esac esac
# Make sure no rebase is in progress # Make sure no rebase is in progress

View File

@ -2314,6 +2314,9 @@ static int make_patch(struct commit *commit, struct replay_opts *opts)
p = short_commit_name(commit); p = short_commit_name(commit);
if (write_message(p, strlen(p), rebase_path_stopped_sha(), 1) < 0) if (write_message(p, strlen(p), rebase_path_stopped_sha(), 1) < 0)
return -1; return -1;
if (update_ref("rebase", "REBASE_HEAD", &commit->object.oid,
NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR))
res |= error(_("could not update %s"), "REBASE_HEAD");
strbuf_addf(&buf, "%s/patch", get_dir(opts)); strbuf_addf(&buf, "%s/patch", get_dir(opts));
memset(&log_tree_opt, 0, sizeof(log_tree_opt)); memset(&log_tree_opt, 0, sizeof(log_tree_opt));
@ -2565,6 +2568,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
unlink(rebase_path_author_script()); unlink(rebase_path_author_script());
unlink(rebase_path_stopped_sha()); unlink(rebase_path_stopped_sha());
unlink(rebase_path_amend()); unlink(rebase_path_amend());
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
} }
if (item->command <= TODO_SQUASH) { if (item->command <= TODO_SQUASH) {
if (is_rebase_i(opts)) if (is_rebase_i(opts))

View File

@ -277,4 +277,38 @@ EOF
test_cmp From_.msg out test_cmp From_.msg out
' '
test_expect_success 'rebase--am.sh and --show-current-patch' '
test_create_repo conflict-apply &&
(
cd conflict-apply &&
test_commit init &&
echo one >>init.t &&
git commit -a -m one &&
echo two >>init.t &&
git commit -a -m two &&
git tag two &&
test_must_fail git rebase --onto init HEAD^ &&
GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
grep "show.*$(git rev-parse two)" stderr
)
'
test_expect_success 'rebase--merge.sh and --show-current-patch' '
test_create_repo conflict-merge &&
(
cd conflict-merge &&
test_commit init &&
echo one >>init.t &&
git commit -a -m one &&
echo two >>init.t &&
git commit -a -m two &&
git tag two &&
test_must_fail git rebase --merge --onto init HEAD^ &&
git rebase --show-current-patch >actual.patch &&
GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
grep "show.*REBASE_HEAD" stderr &&
test "$(git rev-parse REBASE_HEAD)" = "$(git rev-parse two)"
)
'
test_done test_done

View File

@ -225,6 +225,14 @@ test_expect_success 'stop on conflicting pick' '
test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo) test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
' '
test_expect_success 'show conflicted patch' '
GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
grep "show.*REBASE_HEAD" stderr &&
# the original stopped-sha1 is abbreviated
stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
'
test_expect_success 'abort' ' test_expect_success 'abort' '
git rebase --abort && git rebase --abort &&
test $(git rev-parse new-branch1) = $(git rev-parse HEAD) && test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&

View File

@ -662,6 +662,11 @@ test_expect_success 'am pauses on conflict' '
test -d .git/rebase-apply test -d .git/rebase-apply
' '
test_expect_success 'am --show-current-patch' '
git am --show-current-patch >actual.patch &&
test_cmp .git/rebase-apply/0001 actual.patch
'
test_expect_success 'am --skip works' ' test_expect_success 'am --skip works' '
echo goodbye >expected && echo goodbye >expected &&
git am --skip && git am --skip &&