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 <torvalds@linux-foundation.org> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tan committed Aug 19, 2015 at 16:22 UTC 3ecc7040eff29fea0051df9faf21b0a73ee6d911
2 files changed +60 -13
builtin/am.c
+36 -13
@@ -1939,16 +1939,49 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1939 return 0;
1940 }
1941
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 +
1977 /**
1978 * Clean the index without touching entries that are not modified between
1979 * `head` and `remote`.
1980 */
1981 static int clean_index(const unsigned char *head, const unsigned char *remote)
1982 {
1948 - struct lock_file *lock_file;
1983 struct tree *head_tree, *remote_tree, *index_tree;
1984 unsigned char index[GIT_SHA1_RAWSZ];
1951 - struct pathspec pathspec;
1985
1986 head_tree = parse_tree_indirect(head);
1987 if (!head_tree)
@@ -1973,18 +2006,8 @@ static int clean_index(const unsigned char *head, const unsigned char *remote)
2006 if (fast_forward_to(index_tree, remote_tree, 0))
2007 return -1;
2008
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))
2010 return -1;
1984 - }
1985 -
1986 - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1987 - die(_("unable to write new index file"));
2011
2012 remove_branch_state();
2013
t/t4151-am-abort.sh
+24
@@ -168,4 +168,28 @@ test_expect_success 'am --abort on unborn branch will keep local commits intact'
168 test_cmp expect actual
169 '
170
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 +
195 test_done