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

[PATCH v4 05/10] diff-lib: accept option flags in run_diff_index()

From
Denton Liu <liu.denton@gmail.com>
Date
Sep 20, 2020, 11:22 UTC
Message-ID
<99d8b51585b0b6831f3feaefb83de8390689b683.1600600823.git.liu.denton@gmail.com>
In-Reply-To
<cover.1600600823.git.liu.denton@gmail.com>

In a future commit, we will teach run_diff_index() to accept more options via flag bits. For now, change `cached` into a flag in the `option` bitfield. The behaviour should remain exactly the same.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 builtin/diff-index.c | 8 ++++----
 builtin/diff.c       | 8 ++++----
 diff-lib.c           | 3 ++-
 diff.h               | 4 +++-
 4 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/builtin/diff-index.c b/builtin/diff-index.c
index 93ec642423..c3878f7ad6 100644
--- a/builtin/diff-index.c
+++ b/builtin/diff-index.c
@@ -15,7 +15,7 @@ COMMON_DIFF_OPTIONS_HELP;
 int cmd_diff_index(int argc, const char **argv, const char *prefix)
 {
 	struct rev_info rev;
-	int cached = 0;
+	unsigned int option = 0;
 	int i;
 	int result;
 
@@ -32,7 +32,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
 		const char *arg = argv[i];
 
 		if (!strcmp(arg, "--cached"))
-			cached = 1;
+			option |= DIFF_INDEX_CACHED;
 		else
 			usage(diff_cache_usage);
 	}
@@ -46,7 +46,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
 	if (rev.pending.nr != 1 ||
 	    rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
 		usage(diff_cache_usage);
-	if (!cached) {
+	if (!(option & DIFF_INDEX_CACHED)) {
 		setup_work_tree();
 		if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
 			perror("read_cache_preload");
@@ -56,7 +56,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
 		perror("read_cache");
 		return -1;
 	}
-	result = run_diff_index(&rev, cached);
+	result = run_diff_index(&rev, option);
 	UNLEAK(rev);
 	return diff_result_code(&rev.diffopt, result);
 }
diff --git a/builtin/diff.c b/builtin/diff.c
index cb98811c21..e45e19e37e 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -134,11 +134,11 @@ static int builtin_diff_blobs(struct rev_info *revs,
 static int builtin_diff_index(struct rev_info *revs,
 			      int argc, const char **argv)
 {
-	int cached = 0;
+	unsigned int option = 0;
 	while (1 < argc) {
 		const char *arg = argv[1];
 		if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
-			cached = 1;
+			option |= DIFF_INDEX_CACHED;
 		else
 			usage(builtin_diff_usage);
 		argv++; argc--;
@@ -151,7 +151,7 @@ static int builtin_diff_index(struct rev_info *revs,
 	    revs->max_count != -1 || revs->min_age != -1 ||
 	    revs->max_age != -1)
 		usage(builtin_diff_usage);
-	if (!cached) {
+	if (!(option & DIFF_INDEX_CACHED)) {
 		setup_work_tree();
 		if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
 			perror("read_cache_preload");
@@ -161,7 +161,7 @@ static int builtin_diff_index(struct rev_info *revs,
 		perror("read_cache");
 		return -1;
 	}
-	return run_diff_index(revs, cached);
+	return run_diff_index(revs, option);
 }
 
 static int builtin_diff_tree(struct rev_info *revs,
diff --git a/diff-lib.c b/diff-lib.c
index 50521e2093..d5b2c8af56 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -518,9 +518,10 @@ static int diff_cache(struct rev_info *revs,
 	return unpack_trees(1, &t, &opts);
 }
 
-int run_diff_index(struct rev_info *revs, int cached)
+int run_diff_index(struct rev_info *revs, unsigned int option)
 {
 	struct object_array_entry *ent;
+	int cached = !!(option & DIFF_INDEX_CACHED);
 
 	if (revs->pending.nr != 1)
 		BUG("run_diff_index must be passed exactly one tree");
diff --git a/diff.h b/diff.h
index e0c0af6286..aea0d5b96b 100644
--- a/diff.h
+++ b/diff.h
@@ -585,7 +585,9 @@ const char *diff_aligned_abbrev(const struct object_id *sha1, int);
 /* report racily-clean paths as modified */
 #define DIFF_RACY_IS_MODIFIED 02
 int run_diff_files(struct rev_info *revs, unsigned int option);
-int run_diff_index(struct rev_info *revs, int cached);
+
+#define DIFF_INDEX_CACHED 01
+int run_diff_index(struct rev_info *revs, unsigned int option);
 
 int do_diff_cache(const struct object_id *, struct diff_options *);
 int diff_flush_patch_id(struct diff_options *, struct object_id *, int, int);
-- 
2.28.0.760.g8d73e04208
Previous: Denton LiuNext: Denton Liu
Message 51 of 58 in “builtin/diff: learn --merge-base”
  1. 0/4 builtin/diff: learn --merge-baseDenton Liu, Sep 5, 2020
  2. 1/4 t4068: remove unnecessary >tmpDenton Liu, Sep 5, 2020
  3. 3/4 builtin/diff: parse --no-index using parse_options()Denton Liu, Sep 5, 2020
  4. 2/4 git-diff.txt: backtick quote command textDenton Liu, Sep 5, 2020
  5. 4/4 builtin/diff: learn --merge-baseDenton Liu, Sep 5, 2020
  6. Junio C HamanoSep 6, 2020
  7. 0/4 builtin/diff: learn --merge-baseDenton Liu, Sep 10, 2020
  8. 1/4 t4068: remove unnecessary >tmpDenton Liu, Sep 10, 2020
  9. 2/4 git-diff.txt: backtick quote command textDenton Liu, Sep 10, 2020
  10. 3/4 builtin/diff: parse --no-index using parse_options()Denton Liu, Sep 10, 2020
  11. Junio C HamanoSep 10, 2020
  12. Denton LiuSep 13, 2020
  13. Junio C HamanoSep 13, 2020
  14. 4/4 builtin/diff: learn --merge-baseDenton Liu, Sep 10, 2020
  15. 00/10 builtin/diff: learn --merge-baseDenton Liu, Sep 17, 2020
  16. 01/10 t4068: remove unnecessary >tmpDenton Liu, Sep 17, 2020
  17. 02/10 git-diff-index.txt: make --cached description a proper sentenceDenton Liu, Sep 17, 2020
  18. 10/10 contrib/completion: complete `git diff --merge-base`Denton Liu, Sep 17, 2020
  19. 09/10 builtin/diff-tree: learn --merge-baseDenton Liu, Sep 17, 2020
  20. Junio C HamanoSep 17, 2020
  21. Denton LiuSep 18, 2020
  22. Junio C HamanoSep 18, 2020
  23. Denton LiuSep 20, 2020
  24. Junio C HamanoSep 21, 2020
  25. Denton LiuSep 21, 2020
  26. Junio C HamanoSep 21, 2020
  27. Junio C HamanoSep 21, 2020
  28. Denton LiuSep 21, 2020
  29. Junio C HamanoSep 21, 2020
  30. Denton LiuSep 23, 2020
  31. Junio C HamanoSep 25, 2020
  32. Denton LiuSep 26, 2020
  33. 08/10 builtin/diff-index: learn --merge-baseDenton Liu, Sep 17, 2020
  34. Junio C HamanoSep 17, 2020
  35. Jeff KingSep 17, 2020
  36. Junio C HamanoSep 18, 2020
  37. Jeff KingSep 18, 2020
  38. 05/10 diff-lib: accept option flags in run_diff_index()Denton Liu, Sep 17, 2020
  39. Junio C HamanoSep 17, 2020
  40. 04/10 contrib/completion: extract common diff/difftool optionsDenton Liu, Sep 17, 2020
  41. 06/10 diff-lib: define diff_get_merge_base()Denton Liu, Sep 17, 2020
  42. Junio C HamanoSep 17, 2020
  43. Denton LiuSep 18, 2020
  44. Junio C HamanoSep 19, 2020
  45. 03/10 git-diff.txt: backtick quote command textDenton Liu, Sep 17, 2020
  46. 07/10 t4068: add --merge-base testsDenton Liu, Sep 17, 2020
  47. 00/10 builtin/diff: learn --merge-baseDenton Liu, Sep 20, 2020
  48. 01/10 t4068: remove unnecessary >tmpDenton Liu, Sep 20, 2020
  49. 02/10 git-diff-index.txt: make --cached description a proper sentenceDenton Liu, Sep 20, 2020
  50. 06/10 diff-lib: define diff_get_merge_base()Denton Liu, Sep 20, 2020
  51. 05/10 diff-lib: accept option flags in run_diff_index()Denton Liu, Sep 20, 2020
  52. 03/10 git-diff.txt: backtick quote command textDenton Liu, Sep 20, 2020
  53. 07/10 t4068: add --merge-base testsDenton Liu, Sep 20, 2020
  54. 08/10 builtin/diff-index: learn --merge-baseDenton Liu, Sep 20, 2020
  55. Martin ÅgrenSep 29, 2020
  56. 04/10 contrib/completion: extract common diff/difftool optionsDenton Liu, Sep 20, 2020
  57. 09/10 builtin/diff-tree: learn --merge-baseDenton Liu, Sep 20, 2020
  58. 10/10 contrib/completion: complete `git diff --merge-base`Denton Liu, Sep 20, 2020

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.