git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] tree-diff: make diff_tree_sha1 return void

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Mar 23, 2011, 01:30 UTC
Message-ID
<20110323013021.GC10621@elie>
In-Reply-To
<20110323012654.GA10621@elie>

diff_tree_sha1 does not return -1 on errors; it die()s. Now it would obviously be lovely to change that so long-running programs could call diff_tree_sha1 without fear, but until then, let's face the reality:

 - nobody has tested what would happen if it started returning
   instead of dying on failure;
 - the callers overwhelmingly ignore the return value.

Returning void also saves readers the trouble of checking documentation each time they use diff_tree_sha1 to see if its return value follows the GNU diff exit code convention.

So it's just more honest to return void.  Do so.

Noticed with gcc-4.6 -Wunused-but-set-variable (because one caller couldn't decide --- it saves the return value, but never does anything with it).

Noticed-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 diff.h      |    2 +-
 revision.c  |    4 +---
 tree-diff.c |    6 ++----
 3 files changed, 4 insertions(+), 8 deletions(-)
diff --git a/diff.h b/diff.h
index 4fde7f3..0a8a06c 100644
--- a/diff.h
+++ b/diff.h
@@ -165,7 +165,7 @@ extern void diff_tree_setup_paths(const char **paths, struct diff_options *);
 extern void diff_tree_release_paths(struct diff_options *);
 extern void diff_tree(struct tree_desc *t1, struct tree_desc *t2,
 		     const char *base, struct diff_options *opt);
-extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,
+extern void diff_tree_sha1(const unsigned char *old, const unsigned char *new,
 			  const char *base, struct diff_options *opt);
 extern int diff_root_tree_sha1(const unsigned char *new, const char *base,
                                struct diff_options *opt);
diff --git a/revision.c b/revision.c
index a6e78c9..0c810b4 100644
--- a/revision.c
+++ b/revision.c
@@ -329,9 +329,7 @@ static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct
 
 	tree_difference = REV_TREE_SAME;
 	DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
-	if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
-			   &revs->pruning) < 0)
-		return REV_TREE_DIFFERENT;
+	diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &revs->pruning);
 	return tree_difference;
 }
 
diff --git a/tree-diff.c b/tree-diff.c
index d1a7ae9..03b8ca0 100644
--- a/tree-diff.c
+++ b/tree-diff.c
@@ -17,7 +17,6 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
 	const unsigned char *sha1, *sha2;
 	int cmp, pathlen1, pathlen2;
 	int old_baselen = base->len;
-	int retval = 0;
 
 	sha1 = tree_entry_extract(t1, &path1, &mode1);
 	sha2 = tree_entry_extract(t2, &path2, &mode2);
@@ -53,7 +52,7 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
 				    sha1, sha2, base->buf, 0, 0);
 		}
 		strbuf_addch(base, '/');
-		retval = diff_tree_sha1(sha1, sha2, base->buf, opt);
+		diff_tree_sha1(sha1, sha2, base->buf, opt);
 	} else {
 		opt->change(opt, mode1, mode2, sha1, sha2, base->buf, 0, 0);
 	}
@@ -279,7 +278,7 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co
 	q->nr = 1;
 }
 
-int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
+void diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
 {
 	void *tree1, *tree2;
 	struct tree_desc t1, t2;
@@ -301,7 +300,6 @@ int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const cha
 	}
 	free(tree1);
 	free(tree2);
-	return 0;
 }
 
 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
-- 
1.7.4.1
Previous: Jonathan Nieder
Message 11 of 11 in “Fix issues found by gcc 4.6.0”
  1. 0/2 Fix issues found by gcc 4.6.0Johannes Schindelin, Mar 22, 2011
  2. 1/2 Remove unused variablesJohannes Schindelin, Mar 22, 2011
  3. Junio C HamanoMar 22, 2011
  4. Johannes SchindelinMar 22, 2011
  5. Jonathan NiederMar 22, 2011
  6. 2/2 Actually use retvalJohannes Schindelin, Mar 22, 2011
  7. Junio C HamanoMar 22, 2011
  8. Johannes SchindelinMar 22, 2011
  9. 0/2 Re: [PATCH 2/2] Actually use retvalJonathan Nieder, Mar 23, 2011
  10. 1/2 tree-diff: make diff_tree return voidJonathan Nieder, Mar 23, 2011
  11. 2/2 tree-diff: make diff_tree_sha1 return voidJonathan Nieder, Mar 23, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.