Skip to content

Commit 852a171

Browse files
pyokagangitster
authored andcommitted
am: let command-line options override saved options
When resuming, git-am mistakenly ignores command-line options. For instance, when a patch fails to apply with "git am patch", subsequently running "git am --3way" would not cause git-am to fall back on attempting a threeway merge. This occurs because by default the --3way option is saved as "false", and the saved am options are loaded after the command-line options are parsed, thus overwriting the command-line options when resuming. Fix this by moving the am_load() function call before parse_options(), so that command-line options will override the saved am options. The purpose of supporting this use case is to enable users to "wiggle" that one conflicting patch. As such, it is expected that the command-line options do not affect subsequent applied patches. Implement this by calling am_load() once we apply the conflicting patch successfully. Noticed-by: Junio C Hamano <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Paul Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 18d8c26 commit 852a171

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

builtin/am.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,6 @@ static void am_run(struct am_state *state, int resume)
17791779

17801780
if (resume) {
17811781
validate_resume_state(state);
1782-
resume = 0;
17831782
} else {
17841783
int skip;
17851784

@@ -1841,6 +1840,10 @@ static void am_run(struct am_state *state, int resume)
18411840

18421841
next:
18431842
am_next(state);
1843+
1844+
if (resume)
1845+
am_load(state);
1846+
resume = 0;
18441847
}
18451848

18461849
if (!is_empty_file(am_path(state, "rewritten"))) {
@@ -1895,6 +1898,7 @@ static void am_resolve(struct am_state *state)
18951898

18961899
next:
18971900
am_next(state);
1901+
am_load(state);
18981902
am_run(state, 0);
18991903
}
19001904

@@ -2022,6 +2026,7 @@ static void am_skip(struct am_state *state)
20222026
die(_("failed to clean index"));
20232027

20242028
am_next(state);
2029+
am_load(state);
20252030
am_run(state, 0);
20262031
}
20272032

@@ -2132,6 +2137,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
21322137
int keep_cr = -1;
21332138
int patch_format = PATCH_FORMAT_UNKNOWN;
21342139
enum resume_mode resume = RESUME_FALSE;
2140+
int in_progress;
21352141

21362142
const char * const usage[] = {
21372143
N_("git am [options] [(<mbox>|<Maildir>)...]"),
@@ -2227,6 +2233,10 @@ int cmd_am(int argc, const char **argv, const char *prefix)
22272233

22282234
am_state_init(&state, git_path("rebase-apply"));
22292235

2236+
in_progress = am_in_progress(&state);
2237+
if (in_progress)
2238+
am_load(&state);
2239+
22302240
argc = parse_options(argc, argv, prefix, options, usage, 0);
22312241

22322242
if (binary >= 0)
@@ -2239,7 +2249,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
22392249
if (read_index_preload(&the_index, NULL) < 0)
22402250
die(_("failed to read the index"));
22412251

2242-
if (am_in_progress(&state)) {
2252+
if (in_progress) {
22432253
/*
22442254
* Catch user error to feed us patches when there is a session
22452255
* in progress:
@@ -2257,8 +2267,6 @@ int cmd_am(int argc, const char **argv, const char *prefix)
22572267

22582268
if (resume == RESUME_FALSE)
22592269
resume = RESUME_APPLY;
2260-
2261-
am_load(&state);
22622270
} else {
22632271
struct argv_array paths = ARGV_ARRAY_INIT;
22642272
int i;

t/t4153-am-resume-override-opts.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/sh
2+
3+
test_description='git-am command-line options override saved options'
4+
5+
. ./test-lib.sh
6+
. "$TEST_DIRECTORY"/lib-terminal.sh
7+
8+
format_patch () {
9+
git format-patch --stdout -1 "$1" >"$1".eml
10+
}
11+
12+
test_expect_success 'setup' '
13+
test_commit initial file &&
14+
test_commit first file &&
15+
16+
git checkout initial &&
17+
git mv file file2 &&
18+
test_tick &&
19+
git commit -m renamed-file &&
20+
git tag renamed-file &&
21+
22+
git checkout -b side initial &&
23+
test_commit side1 file &&
24+
test_commit side2 file &&
25+
26+
format_patch side1 &&
27+
format_patch side2
28+
'
29+
30+
test_expect_success TTY '--3way overrides --no-3way' '
31+
rm -fr .git/rebase-apply &&
32+
git reset --hard &&
33+
git checkout renamed-file &&
34+
35+
# Applying side1 will fail as the file has been renamed.
36+
test_must_fail git am --no-3way side[12].eml &&
37+
test_path_is_dir .git/rebase-apply &&
38+
test_cmp_rev renamed-file HEAD &&
39+
test -z "$(git ls-files -u)" &&
40+
41+
# Applying side1 with am --3way will succeed due to the threeway-merge.
42+
# Applying side2 will fail as --3way does not apply to it.
43+
test_must_fail test_terminal git am --3way </dev/zero &&
44+
test_path_is_dir .git/rebase-apply &&
45+
test side1 = "$(cat file2)"
46+
'
47+
48+
test_expect_success '--no-quiet overrides --quiet' '
49+
rm -fr .git/rebase-apply &&
50+
git reset --hard &&
51+
git checkout first &&
52+
53+
# Applying side1 will be quiet.
54+
test_must_fail git am --quiet side[123].eml >out &&
55+
test_path_is_dir .git/rebase-apply &&
56+
! test_i18ngrep "^Applying: " out &&
57+
echo side1 >file &&
58+
git add file &&
59+
60+
# Applying side1 will not be quiet.
61+
# Applying side2 will be quiet.
62+
git am --no-quiet --continue >out &&
63+
echo "Applying: side1" >expected &&
64+
test_i18ncmp expected out
65+
'
66+
67+
test_expect_success TTY '--reject overrides --no-reject' '
68+
rm -fr .git/rebase-apply &&
69+
git reset --hard &&
70+
git checkout first &&
71+
rm -f file.rej &&
72+
73+
test_must_fail git am --no-reject side1.eml &&
74+
test_path_is_dir .git/rebase-apply &&
75+
test_path_is_missing file.rej &&
76+
77+
test_must_fail test_terminal git am --reject </dev/zero &&
78+
test_path_is_dir .git/rebase-apply &&
79+
test_path_is_file file.rej
80+
'
81+
82+
test_done

0 commit comments

Comments
 (0)