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

[PATCH] diff-stages: unuglify the too big main() function.

From
Junio C Hamano <junkio@cox.net>
Date
Jun 11, 2005, 01:44 UTC
Message-ID
<7vu0k56517.fsf_-_@assigned-by-dhcp.cox.net>
In-Reply-To
<7vekbbb2me.fsf_-_@assigned-by-dhcp.cox.net>

Split the core of the program, diff_stage, from one big "main()" function that does it all and leave only the parameter parsing, setup and finalize part in the main().

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 diff-stages.c |   75 ++++++++++++++++++++++++++++++---------------------------
 1 files changed, 40 insertions(+), 35 deletions(-)
diff --git a/diff-stages.c b/diff-stages.c
--- a/diff-stages.c
+++ b/diff-stages.c
@@ -17,9 +17,47 @@ static const char *orderfile = NULL;
 static char *diff_stages_usage =
 "git-diff-stages [-p] [-r] [-z] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] <stage1> <stage2> [<path>...]";
 
+static void diff_stages(int stage1, int stage2)
+{
+	int i = 0;
+	while (i < active_nr) {
+		struct cache_entry *ce, *stages[4] = { NULL, };
+		struct cache_entry *one, *two;
+		const char *name;
+		int len;
+		ce = active_cache[i];
+		len = ce_namelen(ce);
+		name = ce->name;
+		for (;;) {
+			int stage = ce_stage(ce);
+			stages[stage] = ce;
+			if (active_nr <= ++i)
+				break;
+			ce = active_cache[i];
+			if (ce_namelen(ce) != len ||
+			    memcmp(name, ce->name, len))
+				break;
+		}
+		one = stages[stage1];
+		two = stages[stage2];
+		if (!one && !two)
+			continue;
+		if (!one)
+			diff_addremove('+', ntohl(two->ce_mode),
+				       two->sha1, name, NULL);
+		else if (!two)
+			diff_addremove('-', ntohl(one->ce_mode),
+				       one->sha1, name, NULL);
+		else if (memcmp(one->sha1, two->sha1, 20) ||
+			 (one->ce_mode != two->ce_mode))
+			 diff_change(ntohl(one->ce_mode), ntohl(two->ce_mode),
+				     one->sha1, two->sha1, name, NULL);
+	}
+}
+
 int main(int ac, const char **av)
 {
-	int stage1, stage2, i;
+	int stage1, stage2;
 
 	read_cache();
 	while (1 < ac && av[1][0] == '-') {
@@ -67,40 +105,7 @@ int main(int ac, const char **av)
 	av += 3; /* The rest from av[0] are for paths restriction. */
 	diff_setup(diff_setup_opt);
 
-	i = 0;
-	while (i < active_nr) {
-		struct cache_entry *ce, *stages[4] = { NULL, };
-		struct cache_entry *one, *two;
-		const char *name;
-		int len;
-		ce = active_cache[i];
-		len = ce_namelen(ce);
-		name = ce->name;
-		for (;;) {
-			int stage = ce_stage(ce);
-			stages[stage] = ce;
-			if (active_nr <= ++i)
-				break;
-			ce = active_cache[i];
-			if (ce_namelen(ce) != len ||
-			    memcmp(name, ce->name, len))
-				break;
-		}
-		one = stages[stage1];
-		two = stages[stage2];
-		if (!one && !two)
-			continue;
-		if (!one)
-			diff_addremove('+', ntohl(two->ce_mode),
-				       two->sha1, name, NULL);
-		else if (!two)
-			diff_addremove('-', ntohl(one->ce_mode),
-				       one->sha1, name, NULL);
-		else if (memcmp(one->sha1, two->sha1, 20) ||
-			 (one->ce_mode != two->ce_mode))
-			 diff_change(ntohl(one->ce_mode), ntohl(two->ce_mode),
-				     one->sha1, two->sha1, name, NULL);
-	}
+	diff_stages(stage1, stage2);
 
 	diffcore_std(av,
 		     detect_rename, diff_score_opt,
------------
Previous: Linus TorvaldsNext: Junio C Hamano
Message 24 of 33 in “Handling merge conflicts a bit more gracefully..”
  1. Linus TorvaldsJun 8, 2005
  2. Junio C HamanoJun 8, 2005
  3. Linus TorvaldsJun 8, 2005
  4. Junio C HamanoJun 9, 2005
  5. Linus TorvaldsJun 9, 2005
  6. Junio C HamanoJun 9, 2005
  7. Junio C HamanoJun 9, 2005
  8. Linus TorvaldsJun 9, 2005
  9. Junio C HamanoJun 9, 2005
  10. Linus TorvaldsJun 9, 2005
  11. Junio C HamanoJun 9, 2005
  12. Linus TorvaldsJun 9, 2005
  13. Junio C HamanoJun 9, 2005
  14. 0/3 Handling merge conflicts a bit more gracefullyJunio C Hamano, Jun 9, 2005
  15. 1/3 read-tree.c: rename local variables used in 3-way merge code.Junio C Hamano, Jun 9, 2005
  16. 2/3 read-tree -m 3-way: loosen index requirements that is too strict.Junio C Hamano, Jun 9, 2005
  17. 3/3 read-tree -m 3-way: handle more trivial merges internallyJunio C Hamano, Jun 9, 2005
  18. Linus TorvaldsJun 9, 2005
  19. Junio C HamanoJun 9, 2005
  20. Linus TorvaldsJun 9, 2005
  21. Junio C HamanoJun 9, 2005
  22. Add git-diff-stages command.Junio C Hamano, Jun 9, 2005
  23. Linus TorvaldsJun 9, 2005
  24. diff-stages: unuglify the too big main() function.Junio C Hamano, Jun 11, 2005
  25. Junio C HamanoJun 10, 2005
  26. Herbert XuJun 18, 2005
  27. Linus TorvaldsJun 18, 2005
  28. Jeff GarzikJun 9, 2005
  29. Linus TorvaldsJun 9, 2005
  30. read-tree.c: rename local variables used in 3-way merge code.Junio C Hamano, Jun 9, 2005
  31. Handle entry removals during merge correctly.Junio C Hamano, Jun 9, 2005
  32. read-tree -m 3-way: loosen an index requirement that was too strict.Junio C Hamano, Jun 9, 2005
  33. read-tree -m 3-way: handle more trivial merges internally.Junio C Hamano, Jun 9, 2005

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.