threads / patch / 27147

patch, 2 partsdiffcore: make addremove/modified/unmerged return a filepair

Subject: [PATCH 1/2] diffcore: make addremove/modified/unmerged return a filepair

## tl;dr

2 messages between Apr 21, 2011 and Apr 21, 2011. Diffs are folded; open one to read it.

replies: 1people: 1as markdown or json

Junio C Hamano· Apr 21, 2011, 01:07 UTC · lore

The return value from the underlying diff_queue() function these helper functions wrap are meant to be further manipulated by the caller, but the helpers discarded it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * Strictly speaking, I only need this patch for diff_unmerged(), but 
   fixed all the three helpers for consistency.
 diff.c     |   29 ++++++++++++++++++-----------
 diff.h     |   10 +++++-----
 revision.c |    6 ++++--
 3 files changed, 27 insertions(+), 18 deletions(-)
Show changes to 3 files +27 −18

diff.c, diff.h, revision.c

diff --git a/diff.c b/diff.c
index 9a5c77c..41b91cb 100644
--- a/diff.c
+++ b/diff.c
@@ -4225,15 +4225,16 @@ static int is_submodule_ignored(const char *path, struct diff_options *options)
 	return ignored;
 }
 
-void diff_addremove(struct diff_options *options,
+struct diff_filepair *diff_addremove(struct diff_options *options,
 		    int addremove, unsigned mode,
 		    const unsigned char *sha1,
 		    const char *concatpath, unsigned dirty_submodule)
 {
 	struct diff_filespec *one, *two;
+	struct diff_filepair *elem;
 
 	if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
-		return;
+		return NULL;
 
 	/* This may look odd, but it is a preparation for
 	 * feeding "there are unchanged files which should
@@ -4253,7 +4254,7 @@ void diff_addremove(struct diff_options *options,
 
 	if (options->prefix &&
 	    strncmp(concatpath, options->prefix, options->prefix_length))
-		return;
+		return NULL;
 
 	one = alloc_filespec(concatpath);
 	two = alloc_filespec(concatpath);
@@ -4265,12 +4266,13 @@ void diff_addremove(struct diff_options *options,
 		two->dirty_submodule = dirty_submodule;
 	}
 
-	diff_queue(&diff_queued_diff, one, two);
+	elem = diff_queue(&diff_queued_diff, one, two);
 	if (!DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
 		DIFF_OPT_SET(options, HAS_CHANGES);
+	return elem;
 }
 
-void diff_change(struct diff_options *options,
+struct diff_filepair *diff_change(struct diff_options *options,
 		 unsigned old_mode, unsigned new_mode,
 		 const unsigned char *old_sha1,
 		 const unsigned char *new_sha1,
@@ -4278,10 +4280,11 @@ void diff_change(struct diff_options *options,
 		 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
 {
 	struct diff_filespec *one, *two;
+	struct diff_filepair *elem;
 
 	if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
 	    is_submodule_ignored(concatpath, options))
-		return;
+		return NULL;
 
 	if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
 		unsigned tmp;
@@ -4294,7 +4297,7 @@ void diff_change(struct diff_options *options,
 
 	if (options->prefix &&
 	    strncmp(concatpath, options->prefix, options->prefix_length))
-		return;
+		return NULL;
 
 	one = alloc_filespec(concatpath);
 	two = alloc_filespec(concatpath);
@@ -4303,25 +4306,29 @@ void diff_change(struct diff_options *options,
 	one->dirty_submodule = old_dirty_submodule;
 	two->dirty_submodule = new_dirty_submodule;
 
-	diff_queue(&diff_queued_diff, one, two);
+	elem = diff_queue(&diff_queued_diff, one, two);
 	if (!DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
 		DIFF_OPT_SET(options, HAS_CHANGES);
+	return elem;
 }
 
-void diff_unmerge(struct diff_options *options,
+struct diff_filepair *diff_unmerge(struct diff_options *options,
 		  const char *path,
 		  unsigned mode, const unsigned char *sha1)
 {
 	struct diff_filespec *one, *two;
+	struct diff_filepair *elem;
 
 	if (options->prefix &&
 	    strncmp(path, options->prefix, options->prefix_length))
-		return;
+		return NULL;
 
 	one = alloc_filespec(path);
 	two = alloc_filespec(path);
 	fill_filespec(one, sha1, mode);
-	diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;
+	elem = diff_queue(&diff_queued_diff, one, two);
+	elem->is_unmerged = 1;
+	return elem;
 }
 
 static char *run_textconv(const char *pgm, struct diff_filespec *spec,
diff --git a/diff.h b/diff.h
index bf2f44d..d71965f 100644
--- a/diff.h
+++ b/diff.h
@@ -13,14 +13,14 @@ struct strbuf;
 struct diff_filespec;
 struct userdiff_driver;
 
-typedef void (*change_fn_t)(struct diff_options *options,
+typedef struct diff_filepair *(*change_fn_t)(struct diff_options *options,
 		 unsigned old_mode, unsigned new_mode,
 		 const unsigned char *old_sha1,
 		 const unsigned char *new_sha1,
 		 const char *fullpath,
 		 unsigned old_dirty_submodule, unsigned new_dirty_submodule);
 
-typedef void (*add_remove_fn_t)(struct diff_options *options,
+typedef struct diff_filepair *(*add_remove_fn_t)(struct diff_options *options,
 		    int addremove, unsigned mode,
 		    const unsigned char *sha1,
 		    const char *fullpath, unsigned dirty_submodule);
@@ -196,20 +196,20 @@ extern void diff_tree_combined_merge(const unsigned char *sha1, int, struct rev_
 
 void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b);
 
-extern void diff_addremove(struct diff_options *,
+extern struct diff_filepair *diff_addremove(struct diff_options *,
 			   int addremove,
 			   unsigned mode,
 			   const unsigned char *sha1,
 			   const char *fullpath, unsigned dirty_submodule);
 
-extern void diff_change(struct diff_options *,
+extern struct diff_filepair *diff_change(struct diff_options *,
 			unsigned mode1, unsigned mode2,
 			const unsigned char *sha1,
 			const unsigned char *sha2,
 			const char *fullpath,
 			unsigned dirty_submodule1, unsigned dirty_submodule2);
 
-extern void diff_unmerge(struct diff_options *,
+extern struct diff_filepair *diff_unmerge(struct diff_options *,
 			 const char *path,
 			 unsigned mode,
 			 const unsigned char *sha1);
diff --git a/revision.c b/revision.c
index b1c1890..614e5d9 100644
--- a/revision.c
+++ b/revision.c
@@ -276,7 +276,7 @@ static int everybody_uninteresting(struct commit_list *orig)
  */
 static int tree_difference = REV_TREE_SAME;
 
-static void file_add_remove(struct diff_options *options,
+static struct diff_filepair *file_add_remove(struct diff_options *options,
 		    int addremove, unsigned mode,
 		    const unsigned char *sha1,
 		    const char *fullpath, unsigned dirty_submodule)
@@ -286,9 +286,10 @@ static void file_add_remove(struct diff_options *options,
 	tree_difference |= diff;
 	if (tree_difference == REV_TREE_DIFFERENT)
 		DIFF_OPT_SET(options, HAS_CHANGES);
+	return NULL;
 }
 
-static void file_change(struct diff_options *options,
+static struct diff_filepair *file_change(struct diff_options *options,
 		 unsigned old_mode, unsigned new_mode,
 		 const unsigned char *old_sha1,
 		 const unsigned char *new_sha1,
@@ -297,6 +298,7 @@ static void file_change(struct diff_options *options,
 {
 	tree_difference = REV_TREE_DIFFERENT;
 	DIFF_OPT_SET(options, HAS_CHANGES);
+	return NULL;
 }
 
 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
-- 
1.7.5.rc3
Junio C Hamano· Apr 21, 2011, 01:11 UTC · re: Junio C Hamano · lore

[PATCH 2/2] fix "add -u" that sometimes failes to resolve unmerged paths

"git add -u" updates the index with the updated contents from the working tree by internally running "diff-files" to grab the set of paths that are different from the index, and updates the paths that are modified, and deletes the paths that are deleted.

It ignored the diff-files output that indicated a path is unmerged, and relied on the fact that an unmerged path is followed by the result of comparison between stage #2 (ours) and the working tree and used that to update or delete such a path when it is used to record the resolution of a conflict. As the result, when a path did not have stage #2 (e.g. "we deleted while the other side added"), these unmerged stages were left behind, instead of recording what the user resolved in the working tree.

This teaches the diff-files machinery to record if an unmerged path has a corresponding file in the working tree, teaches "add -u" codepath to tell the diff-files not to show the extra comparison with stage #2 (i.e. we get only the "unmerged" report), and upon seeing an unmerged path, either update (when the file exists in the working tree) or delete (otherwise).

The changes to the test vector in t2200 illustrates the nature of the bug and the fix. The test expected stage #1 and #3 entries be left behind, but it was codifying the buggy behaviour.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * Storing the working tree information in an unmerged index entry feels
   somewhat dirty, as it breaks the expectation that "U" entries would
   show zero mode and 0{40} SHA-1 people might have gained from the
   traditional behaviour.
   Considering that a similar change was made to diff-index in e9c8409
   (diff-index --cached --raw: show tree entry on the LHS for unmerged
   entries., 2007-01-05) and later in d1f2d7e (Make run_diff_index() use
   unpack_trees(), not read_tree(), 2008-01-19) and as the result of these
   changes, "diff-index --cached $tree" shows the information from the
   index on the preimage side ever since, and nobody complained when or
   after the change was made, I am hoping that nobody relies on the
   current behaviour of "diff --raw" family for the unmerged paths.
   If anything, the updated behaviour of "diff-files" is more consistent
   with the other parts of the system.  When showing the working tree
   status in the diff --raw output, we do not show its object name, but we
   do show its mode.  We didn't show its mode when the corresponding index
   entry is unmerged before this fix.
   When showing an unmerged index entry in the diff --raw output as a
   single entry, we do not show its mode nor object name, as it stands for
   "fate not determined yet".  That is not something this patch changes.
 builtin/add.c         |   39 +++++++++++++++++----------------------
 diff-lib.c            |    9 +++++++--
 t/t2200-add-update.sh |   24 +++++++-----------------
 3 files changed, 31 insertions(+), 41 deletions(-)
Show changes to 3 files +31 −41

builtin/add.c, diff-lib.c, t/t2200-add-update.sh

diff --git a/builtin/add.c b/builtin/add.c
index 56a4e0a..c85a3ac 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -27,6 +27,21 @@ struct update_callback_data
 	int add_errors;
 };
 
+static int fix_unmerged_status(struct diff_filepair *p,
+			       struct update_callback_data *data)
+{
+	if (p->status != DIFF_STATUS_UNMERGED)
+		return p->status;
+	if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
+		/*
+		 * This is not an explicit add request,
+		 * and the path is missing from the working tree.
+		 */
+		return DIFF_STATUS_DELETED;
+	else
+		return DIFF_STATUS_MODIFIED;
+}
+
 static void update_callback(struct diff_queue_struct *q,
 			    struct diff_options *opt, void *cbdata)
 {
@@ -36,30 +51,9 @@ static void update_callback(struct diff_queue_struct *q,
 	for (i = 0; i < q->nr; i++) {
 		struct diff_filepair *p = q->queue[i];
 		const char *path = p->one->path;
-		switch (p->status) {
+		switch (fix_unmerged_status(p, data)) {
 		default:
 			die("unexpected diff status %c", p->status);
-		case DIFF_STATUS_UNMERGED:
-			/*
-			 * ADD_CACHE_IGNORE_REMOVAL is unset if "git
-			 * add -u" is calling us, In such a case, a
-			 * missing work tree file needs to be removed
-			 * if there is an unmerged entry at stage #2,
-			 * but such a diff record is followed by
-			 * another with DIFF_STATUS_DELETED (and if
-			 * there is no stage #2, we won't see DELETED
-			 * nor MODIFIED).  We can simply continue
-			 * either way.
-			 */
-			if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL))
-				continue;
-			/*
-			 * Otherwise, it is "git add path" is asking
-			 * to explicitly add it; we fall through.  A
-			 * missing work tree file is an error and is
-			 * caught by add_file_to_index() in such a
-			 * case.
-			 */
 		case DIFF_STATUS_MODIFIED:
 		case DIFF_STATUS_TYPE_CHANGED:
 			if (add_file_to_index(&the_index, path, data->flags)) {
@@ -92,6 +86,7 @@ int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
 	data.flags = flags;
 	data.add_errors = 0;
 	rev.diffopt.format_callback_data = &data;
+	rev.max_count = 0; /* do not show extra M/D after U */
 	run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
 	return !!data.add_errors;
 }
diff --git a/diff-lib.c b/diff-lib.c
index 392ce2b..f0ce473 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -111,7 +111,9 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 
 		if (ce_stage(ce)) {
 			struct combine_diff_path *dpath;
+			struct diff_filepair *dp;
 			int num_compare_stages = 0;
+			unsigned int wt_mode = 0;
 			size_t path_len;
 
 			path_len = ce_namelen(ce);
@@ -129,7 +131,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 
 			changed = check_removed(ce, &st);
 			if (!changed)
-				dpath->mode = ce_mode_from_stat(ce, st.st_mode);
+				wt_mode = ce_mode_from_stat(ce, st.st_mode);
 			else {
 				if (changed < 0) {
 					perror(ce->name);
@@ -137,7 +139,9 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 				}
 				if (silent_on_removed)
 					continue;
+				wt_mode = 0;
 			}
+			dpath->mode = wt_mode;
 
 			while (i < entries) {
 				struct cache_entry *nce = active_cache[i];
@@ -183,7 +187,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 			 * Show the diff for the 'ce' if we found the one
 			 * from the desired stage.
 			 */
-			diff_unmerge(&revs->diffopt, ce->name, 0, null_sha1);
+			dp = diff_unmerge(&revs->diffopt, ce->name, 0, null_sha1);
+			dp->two->mode = wt_mode;
 			if (ce_stage(ce) != diff_unmerged_stage)
 				continue;
 		}
diff --git a/t/t2200-add-update.sh b/t/t2200-add-update.sh
index 2ad2819..d3bb9fa 100755
--- a/t/t2200-add-update.sh
+++ b/t/t2200-add-update.sh
@@ -149,31 +149,21 @@ test_expect_success 'add -u resolves unmerged paths' '
 	echo 3 >path1 &&
 	echo 2 >path3 &&
 	echo 2 >path5 &&
-	git add -u &&
-	git ls-files -s path1 path2 path3 path4 path5 path6 >actual &&
-	{
-		echo "100644 $three 0	path1"
-		echo "100644 $one 1	path3"
-		echo "100644 $one 1	path4"
-		echo "100644 $one 3	path5"
-		echo "100644 $one 3	path6"
-	} >expect &&
-	test_cmp expect actual &&
 
-	# Bonus tests.  Explicit resolving
-	git add path3 path5 &&
+	# Explicit resolving by adding removed paths should fail
 	test_must_fail git add path4 &&
 	test_must_fail git add path6 &&
-	git rm path4 &&
-	git rm path6 &&
 
-	git ls-files -s "path?" >actual &&
+	# "add -u" should notice removals no matter what stages
+	# the index entries are in.
+	git add -u &&
+	git ls-files -s path1 path2 path3 path4 path5 path6 >actual &&
 	{
 		echo "100644 $three 0	path1"
 		echo "100644 $two 0	path3"
 		echo "100644 $two 0	path5"
-	} >expect
-
+	} >expect &&
+	test_cmp expect actual
 '
 
 test_expect_success '"add -u non-existent" should fail' '

← back to recent threads