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

[PATCH 05/22] teach diff-files.c about cache iterators

From
CLChuck Lever <cel@netapp.com>
Date
Sep 12, 2005, 14:55 UTC
Message-ID
<20050912145554.28120.4307.stgit@dexter.citi.umich.edu>
In-Reply-To
<20050912145543.28120.7086.stgit@dexter.citi.umich.edu>
Signed-off-by: Chuck Lever <cel@netapp.com>
---
 diff-files.c |   79 ++++++++++++++++++++++++++++++----------------------------
 1 files changed, 41 insertions(+), 38 deletions(-)
diff --git a/diff-files.c b/diff-files.c
--- a/diff-files.c
+++ b/diff-files.c
@@ -11,6 +11,7 @@ static const char diff_files_usage[] =
 "[<common diff options>] [<path>...]"
 COMMON_DIFF_OPTIONS_HELP;
 
+static const unsigned char null_sha1[20] = { 0, };
 static int diff_output_format = DIFF_FORMAT_RAW;
 static int diff_line_termination = '\n';
 static int detect_rename = 0;
@@ -22,6 +23,7 @@ static int pickaxe_opts = 0;
 static int diff_break_opt = -1;
 static const char *orderfile = NULL;
 static const char *diff_filter = NULL;
+static const char **pathspec;
 static int silent = 0;
 
 static void show_unmerge(const char *path)
@@ -41,12 +43,47 @@ static void show_modified(int oldmode, i
 	diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
 }
 
+static int diff_one_file(struct cache_cursor *cc, struct cache_entry *ce)
+{
+	struct stat st;
+	unsigned int oldmode;
+	int changed;
+
+	if (!ce_path_match(ce, pathspec))
+		goto out;
+
+	if (ce_stage(ce)) {
+		show_unmerge(ce->name);
+		next_name(cc, ce);
+		return 0;
+	}
+
+	if (lstat(ce->name, &st) < 0) {
+		if (errno != ENOENT && errno != ENOTDIR) {
+			perror(ce->name);
+			goto out;
+		}
+		if (silent)
+			goto out;
+		show_file('-', ce);
+		goto out;
+	}
+	changed = ce_match_stat(ce, &st);
+	if (!changed && !find_copies_harder)
+		goto out;
+	oldmode = ntohl(ce->ce_mode);
+	show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
+		      ce->sha1, (changed ? null_sha1 : ce->sha1),
+		      ce->name);
+out:
+	next_cc(cc);
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
-	static const unsigned char null_sha1[20] = { 0, };
-	const char **pathspec;
 	const char *prefix = setup_git_directory();
-	int entries, i;
+	int entries;
 
 	while (1 < argc && argv[1][0] == '-') {
 		if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u"))
@@ -112,42 +149,8 @@ int main(int argc, char **argv)
 
 	diff_setup(diff_setup_opt);
 
-	for (i = 0; i < entries; i++) {
-		struct stat st;
-		unsigned int oldmode;
-		struct cache_entry *ce = active_cache[i];
-		int changed;
-
-		if (!ce_path_match(ce, pathspec))
-			continue;
-
-		if (ce_stage(ce)) {
-			show_unmerge(ce->name);
-			while (i < entries &&
-			       !strcmp(ce->name, active_cache[i]->name))
-				i++;
-			i--; /* compensate for loop control increments */
-			continue;
-		}
+	walk_cache(diff_one_file);
 
-		if (lstat(ce->name, &st) < 0) {
-			if (errno != ENOENT && errno != ENOTDIR) {
-				perror(ce->name);
-				continue;
-			}
-			if (silent)
-				continue;
-			show_file('-', ce);
-			continue;
-		}
-		changed = ce_match_stat(ce, &st);
-		if (!changed && !find_copies_harder)
-			continue;
-		oldmode = ntohl(ce->ce_mode);
-		show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
-			      ce->sha1, (changed ? null_sha1 : ce->sha1),
-			      ce->name);
-	}
 	diffcore_std(pathspec, 
 		     detect_rename, diff_score_opt,
 		     pickaxe, pickaxe_opts,
Previous: Chuck LeverNext: Chuck Lever
Message 6 of 49 in “cache cursors: an introduction”
  1. 00/22 cache cursors: an introductionChuck Lever, Sep 12, 2005
  2. 01/22 introduce facility to walk through the active cacheChuck Lever, Sep 12, 2005
  3. 02/22 use cache iterator in checkout-index.cChuck Lever, Sep 12, 2005
  4. 03/22 teach diff.c about cache iteratorsChuck Lever, Sep 12, 2005
  5. 04/22 teach diff-index.c about cache iteratorsChuck Lever, Sep 12, 2005
  6. 05/22 teach diff-files.c about cache iteratorsChuck Lever, Sep 12, 2005
  7. 06/22 teach diff-stages.c about cache iteratorsChuck Lever, Sep 12, 2005
  8. 07/22 teach fsck-objects.c to use cache iteratorsChuck Lever, Sep 12, 2005
  9. 08/22 teach ls-files.c to use cache iteratorsChuck Lever, Sep 12, 2005
  10. 09/22 teach read-tree.c to use cache iteratorsChuck Lever, Sep 12, 2005
  11. 10/22 teach update-index.c about cache cursorsChuck Lever, Sep 12, 2005
  12. 11/22 teach write-tree.c to use cache iteratorsChuck Lever, Sep 12, 2005
  13. 12/22 simplify write_cache() calling sequenceChuck Lever, Sep 12, 2005
  14. 13/22 move purge_cache() to read-cache.cChuck Lever, Sep 12, 2005
  15. 14/22 move read_cache_unmerged into read-cache.cChuck Lever, Sep 12, 2005
  16. 15/22 replace cache_name_posChuck Lever, Sep 12, 2005
  17. 16/22 teach apply.c to use cache_find_name()Chuck Lever, Sep 12, 2005
  18. 17/22 teach checkout-index.c to use cache_find_name()Chuck Lever, Sep 12, 2005
  19. 18/22 teach diff.c to use cache_find_name()Chuck Lever, Sep 12, 2005
  20. 19/22 teach ls-files.c to use cache_find_name()Chuck Lever, Sep 12, 2005
  21. 20/22 teach merge-index.c to use cache_find_name()Chuck Lever, Sep 12, 2005
  22. 21/22 teach the merge algorithm about cache iteratorsChuck Lever, Sep 12, 2005
  23. Daniel BarkalowSep 12, 2005
  24. Chuck LeverSep 13, 2005
  25. Chuck LeverSep 14, 2005
  26. Daniel BarkalowSep 14, 2005
  27. Junio C HamanoSep 14, 2005
  28. Chuck LeverSep 14, 2005
  29. Daniel BarkalowSep 14, 2005
  30. Chuck LeverSep 14, 2005
  31. Linus TorvaldsSep 14, 2005
  32. Daniel BarkalowSep 14, 2005
  33. Chuck LeverSep 15, 2005
  34. 22/22 teach read-cache.c to use cache_find_name()Chuck Lever, Sep 12, 2005
  35. A Large Angry SCMSep 12, 2005
  36. Chuck LeverSep 12, 2005
  37. Daniel BarkalowSep 12, 2005
  38. Junio C HamanoSep 12, 2005
  39. Tim OttingerSep 13, 2005
  40. Junio C HamanoSep 13, 2005
  41. Tim OttingerSep 13, 2005
  42. Catalin MarinasSep 14, 2005
  43. Chuck LeverSep 14, 2005
  44. Junio C HamanoSep 12, 2005
  45. Daniel BarkalowSep 12, 2005
  46. Junio C HamanoSep 12, 2005
  47. Chuck LeverSep 12, 2005
  48. Junio C HamanoSep 13, 2005
  49. Linus TorvaldsSep 13, 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.