Skip to content

Commit 3ecc704

Browse files
pyokagangitster
authored andcommitted
am --skip/--abort: merge HEAD/ORIG_HEAD tree into index
After running "git am --abort", and then running "git reset --hard", files that were not modified would still be re-checked out. This is because clean_index() in builtin/am.c mistakenly called the read_tree() function, which overwrites all entries in the index, including the stat info. "git am --skip" did not seem to have this issue because am_skip() called am_run(), which called refresh_cache() to update the stat info. However, there's still a performance penalty as the lack of stat info meant that refresh_cache() would have to scan all files for changes. Fix this by using unpack_trees() instead to merge the tree into the index, so that the stat info from the index is kept. Reported-by: Linus Torvalds <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Paul Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e97a5e7 commit 3ecc704

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

builtin/am.c

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,16 +1939,49 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
19391939
return 0;
19401940
}
19411941

1942+
/**
1943+
* Merges a tree into the index. The index's stat info will take precedence
1944+
* over the merged tree's. Returns 0 on success, -1 on failure.
1945+
*/
1946+
static int merge_tree(struct tree *tree)
1947+
{
1948+
struct lock_file *lock_file;
1949+
struct unpack_trees_options opts;
1950+
struct tree_desc t[1];
1951+
1952+
if (parse_tree(tree))
1953+
return -1;
1954+
1955+
lock_file = xcalloc(1, sizeof(struct lock_file));
1956+
hold_locked_index(lock_file, 1);
1957+
1958+
memset(&opts, 0, sizeof(opts));
1959+
opts.head_idx = 1;
1960+
opts.src_index = &the_index;
1961+
opts.dst_index = &the_index;
1962+
opts.merge = 1;
1963+
opts.fn = oneway_merge;
1964+
init_tree_desc(&t[0], tree->buffer, tree->size);
1965+
1966+
if (unpack_trees(1, t, &opts)) {
1967+
rollback_lock_file(lock_file);
1968+
return -1;
1969+
}
1970+
1971+
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1972+
die(_("unable to write new index file"));
1973+
1974+
return 0;
1975+
}
1976+
19421977
/**
19431978
* Clean the index without touching entries that are not modified between
19441979
* `head` and `remote`.
19451980
*/
19461981
static int clean_index(const unsigned char *head, const unsigned char *remote)
19471982
{
1948-
struct lock_file *lock_file;
19491983
struct tree *head_tree, *remote_tree, *index_tree;
19501984
unsigned char index[GIT_SHA1_RAWSZ];
1951-
struct pathspec pathspec;
19521985

19531986
head_tree = parse_tree_indirect(head);
19541987
if (!head_tree)
@@ -1973,18 +2006,8 @@ static int clean_index(const unsigned char *head, const unsigned char *remote)
19732006
if (fast_forward_to(index_tree, remote_tree, 0))
19742007
return -1;
19752008

1976-
memset(&pathspec, 0, sizeof(pathspec));
1977-
1978-
lock_file = xcalloc(1, sizeof(struct lock_file));
1979-
hold_locked_index(lock_file, 1);
1980-
1981-
if (read_tree(remote_tree, 0, &pathspec)) {
1982-
rollback_lock_file(lock_file);
2009+
if (merge_tree(remote_tree))
19832010
return -1;
1984-
}
1985-
1986-
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1987-
die(_("unable to write new index file"));
19882011

19892012
remove_branch_state();
19902013

t/t4151-am-abort.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,28 @@ test_expect_success 'am --abort on unborn branch will keep local commits intact'
168168
test_cmp expect actual
169169
'
170170

171+
test_expect_success 'am --skip leaves index stat info alone' '
172+
git checkout -f --orphan skip-stat-info &&
173+
git reset &&
174+
test_commit skip-should-be-untouched &&
175+
test-chmtime =0 skip-should-be-untouched.t &&
176+
git update-index --refresh &&
177+
git diff-files --exit-code --quiet &&
178+
test_must_fail git am 0001-*.patch &&
179+
git am --skip &&
180+
git diff-files --exit-code --quiet
181+
'
182+
183+
test_expect_success 'am --abort leaves index stat info alone' '
184+
git checkout -f --orphan abort-stat-info &&
185+
git reset &&
186+
test_commit abort-should-be-untouched &&
187+
test-chmtime =0 abort-should-be-untouched.t &&
188+
git update-index --refresh &&
189+
git diff-files --exit-code --quiet &&
190+
test_must_fail git am 0001-*.patch &&
191+
git am --abort &&
192+
git diff-files --exit-code --quiet
193+
'
194+
171195
test_done

0 commit comments

Comments
 (0)