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

[PATCH v5 00/11] Commit-graph write refactor (was: Create commit-graph file format v2)

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Jun 12, 2019, 13:29 UTC
Message-ID
<pull.112.v5.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.112.v4.git.gitgitgadget@gmail.com>

This series replaces ds/commit-graph-file-v2, and I'm using the same gitgitgadget PR to continue the version numbers and hopefully make that clear. This is a slight modification on patches 1-11 from the incremental file format RFC [0].

The commit-graph feature is growing, thanks to all of the contributions by several community members. This also means that the write_commit_graph() method is a bit unwieldy now. This series refactors that method to use a write_commit_graph_context struct that is passed between several smaller methods. The final result should be a write_commit_graph() method that has a clear set of steps. Future changes should then be easier to understand.

 * Patches 1-4: these are small changes which either fix issues or just
   provide clean-up. These are mostly borrowed from
   ds/commit-graph-format-v2. 
   
   
 * Patches 5-11: these provide a non-functional refactor of
   write_commit_graph() into several methods using a "struct
   write_commit_graph_context" to share across the methods.
   
   
Updates to commits previously in this thread:
 * "commit-graph: remove Future Work section" no longer says that 'verify'
   takes as long as 'write'. [1]
   
   
 * "commit-graph: return with errors during write" now has a test to check
   we don't die(). [2]
   
   

Ævar: Looking at the old thread, I only saw two comments that still apply to this series [1] [2]. Please point me to any comments I have missed.

Updates in V5:
 * API calls are updated to return 0 on success and a negative value on
   failure.
   
   
 * Stopped passing 'errno' through an API function, instead returns -1.
   
   
 * "extracting methods" -> "extracting helper functions" in commit messages.
   
   
 * flags are now unsigned ints.
   
   
Thanks, -Stolee
[0] https://public-inbox.org/git/pull.184.git.gitgitgadget@gmail.com/
[1] https://public-inbox.org/git/87o94mql0a.fsf@evledraar.gmail.com/
[2] https://public-inbox.org/git/87pnp2qlkv.fsf@evledraar.gmail.com/
Derrick Stolee (11):
  commit-graph: fix the_repository reference
  commit-graph: return with errors during write
  commit-graph: collapse parameters into flags
  commit-graph: remove Future Work section
  commit-graph: create write_commit_graph_context
  commit-graph: extract fill_oids_from_packs()
  commit-graph: extract fill_oids_from_commit_hex()
  commit-graph: extract fill_oids_from_all_packs()
  commit-graph: extract count_distinct_commits()
  commit-graph: extract copy_oids_to_commits()
  commit-graph: extract write_commit_graph_file()
 Documentation/technical/commit-graph.txt |  17 -
 builtin/commit-graph.c                   |  22 +-
 builtin/commit.c                         |   5 +-
 builtin/gc.c                             |   7 +-
 commit-graph.c                           | 607 +++++++++++++----------
 commit-graph.h                           |  20 +-
 commit.c                                 |   2 +-
 t/t5318-commit-graph.sh                  |   8 +
 8 files changed, 378 insertions(+), 310 deletions(-)
base-commit: 93b4405ffe4ad9308740e7c1c71383bfc369baaa
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-112%2Fderrickstolee%2Fgraph%2Fv2-head-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-112/derrickstolee/graph/v2-head-v5
Pull-Request: https://github.com/gitgitgadget/git/pull/112
Range-diff vs v4:
  1:  0be7713a25 =  1:  0be7713a25 commit-graph: fix the_repository reference
  2:  a4082b827e !  2:  95f66e85b2 commit-graph: return with errors during write
     @@ -5,14 +5,17 @@
          The write_commit_graph() method uses die() to report failure and
          exit when confronted with an unexpected condition. This use of
          die() in a library function is incorrect and is now replaced by
     -    error() statements and an int return type.
     +    error() statements and an int return type. Return zero on success
     +    and a negative value on failure.
      
          Now that we use 'goto cleanup' to jump to the terminal condition
          on an error, we have new paths that could lead to uninitialized
          values. New initializers are added to correct for this.
      
          The builtins 'commit-graph', 'gc', and 'commit' call these methods,
     -    so update them to check the return value.
     +    so update them to check the return value. Test that 'git commit-graph
     +    write' returns a proper error code when hitting a failure condition
     +    in write_commit_graph().
      
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
      
     @@ -23,7 +26,7 @@
       	struct string_list *pack_indexes = NULL;
       	struct string_list *commit_hex = NULL;
       	struct string_list lines;
     -+	int result;
     ++	int result = 0;
       
       	static struct option builtin_commit_graph_write_options[] = {
       		OPT_STRING(0, "object-dir", &opts.obj_dir,
     @@ -49,11 +52,12 @@
      -			   commit_hex,
      -			   opts.append,
      -			   1);
     -+	result = write_commit_graph(opts.obj_dir,
     -+				    pack_indexes,
     -+				    commit_hex,
     -+				    opts.append,
     -+				    1);
     ++	if (write_commit_graph(opts.obj_dir,
     ++			       pack_indexes,
     ++			       commit_hex,
     ++			       opts.append,
     ++			       1))
     ++		result = 1;
       
       	UNLEAK(lines);
      -	return 0;
     @@ -167,12 +171,12 @@
      -				die(_("error opening index for %s"), packname.buf);
      +			if (!p) {
      +				error(_("error adding pack %s"), packname.buf);
     -+				res = 1;
     ++				res = -1;
      +				goto cleanup;
      +			}
      +			if (open_pack_index(p)) {
      +				error(_("error opening index for %s"), packname.buf);
     -+				res = 1;
     ++				res = -1;
      +				goto cleanup;
      +			}
       			for_each_object_in_pack(p, add_packed_commits, &oids,
     @@ -186,7 +190,7 @@
      -		die(_("the commit graph format cannot write %d commits"), count_distinct);
      +	if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
      +		error(_("the commit graph format cannot write %d commits"), count_distinct);
     -+		res = 1;
     ++		res = -1;
      +		goto cleanup;
      +	}
       
     @@ -200,7 +204,7 @@
      -		die(_("too many commits to write graph"));
      +	if (commits.nr >= GRAPH_EDGE_LAST_MASK) {
      +		error(_("too many commits to write graph"));
     -+		res = 1;
     ++		res = -1;
      +		goto cleanup;
      +	}
       
     @@ -213,7 +217,7 @@
      -			  graph_name);
      +		error(_("unable to create leading directories of %s"),
      +			graph_name);
     -+		res = errno;
     ++		res = -1;
      +		goto cleanup;
       	}
       
     @@ -240,6 +244,12 @@
       int generation_numbers_enabled(struct repository *r);
       
      -void write_commit_graph_reachable(const char *obj_dir, int append,
     ++/*
     ++ * The write_commit_graph* methods return zero on success
     ++ * and a negative value on failure. Note that if the repository
     ++ * is not compatible with the commit-graph feature, then the
     ++ * methods will return 0 without writing a commit-graph.
     ++ */
      +int write_commit_graph_reachable(const char *obj_dir, int append,
       				  int report_progress);
      -void write_commit_graph(const char *obj_dir,
  3:  469d0c9a32 !  3:  b4e3ae579a commit-graph: collapse parameters into flags
     @@ -4,8 +4,8 @@
      
          The write_commit_graph() and write_commit_graph_reachable() methods
          currently take two boolean parameters: 'append' and 'report_progress'.
     -    We will soon expand the possible options to send to these methods, so
     -    instead of complicating the parameter list, first simplify it.
     +    As we update these methods, adding more parameters this way becomes
     +    cluttered and hard to maintain.
      
          Collapse these parameters into a 'flags' parameter, and adjust the
          callers to provide flags as necessary.
     @@ -18,8 +18,8 @@
      @@
       	struct string_list *commit_hex = NULL;
       	struct string_list lines;
     - 	int result;
     -+	int flags = COMMIT_GRAPH_PROGRESS;
     + 	int result = 0;
     ++	unsigned int flags = COMMIT_GRAPH_PROGRESS;
       
       	static struct option builtin_commit_graph_write_options[] = {
       		OPT_STRING(0, "object-dir", &opts.obj_dir,
     @@ -39,15 +39,15 @@
       	string_list_init(&lines, 0);
       	if (opts.stdin_packs || opts.stdin_commits) {
      @@
     - 	result = write_commit_graph(opts.obj_dir,
     - 				    pack_indexes,
     - 				    commit_hex,
     --				    opts.append,
     --				    1);
     -+				    flags);
     + 	if (write_commit_graph(opts.obj_dir,
     + 			       pack_indexes,
     + 			       commit_hex,
     +-			       opts.append,
     +-			       1))
     ++			       flags))
     + 		result = 1;
       
       	UNLEAK(lines);
     - 	return result;
      
       diff --git a/builtin/commit.c b/builtin/commit.c
       --- a/builtin/commit.c
     @@ -124,11 +124,17 @@
        */
       int generation_numbers_enabled(struct repository *r);
       
     --int write_commit_graph_reachable(const char *obj_dir, int append,
     --				  int report_progress);
      +#define COMMIT_GRAPH_APPEND     (1 << 0)
      +#define COMMIT_GRAPH_PROGRESS   (1 << 1)
      +
     + /*
     +  * The write_commit_graph* methods return zero on success
     +  * and a negative value on failure. Note that if the repository
     +  * is not compatible with the commit-graph feature, then the
     +  * methods will return 0 without writing a commit-graph.
     +  */
     +-int write_commit_graph_reachable(const char *obj_dir, int append,
     +-				  int report_progress);
      +int write_commit_graph_reachable(const char *obj_dir, unsigned int flags);
       int write_commit_graph(const char *obj_dir,
       		       struct string_list *pack_indexes,
  4:  130007d0e1 =  4:  a5223a37a9 commit-graph: remove Future Work section
  5:  0ca4e18e98 !  5:  b5b8a87676 commit-graph: create write_commit_graph_context
     @@ -3,7 +3,7 @@
          commit-graph: create write_commit_graph_context
      
          The write_commit_graph() method is too large and complex. To simplify
     -    it, we should extract several small methods. However, we will risk
     +    it, we should extract several helper functions. However, we will risk
          repeating a lot of declarations related to progress incidators and
          object id or commit lists.
      
     @@ -11,7 +11,7 @@
          core data structures used in this process. Replace the other local
          variables with the values inside the context object. Following this
          change, we will start to lift code segments wholesale out of the
     -    write_commit_graph() method and into their own methods.
     +    write_commit_graph() method and into helper functions.
      
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
      
     @@ -454,7 +454,7 @@
       		for (i = 0; i < pack_indexes->nr; i++) {
       			struct packed_git *p;
      @@
     - 				res = 1;
     + 				res = -1;
       				goto cleanup;
       			}
      -			for_each_object_in_pack(p, add_packed_commits, &oids,
     @@ -610,7 +610,7 @@
      -	if (commits.nr >= GRAPH_EDGE_LAST_MASK) {
      +	if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
       		error(_("too many commits to write graph"));
     - 		res = 1;
     + 		res = -1;
       		goto cleanup;
       	}
       
     @@ -628,7 +628,7 @@
       		error(_("unable to create leading directories of %s"),
      -			graph_name);
      +			ctx->graph_name);
     - 		res = errno;
     + 		res = -1;
       		goto cleanup;
       	}
       
  6:  30c1b618b1 !  6:  98e243be67 commit-graph: extract fill_oids_from_packs()
     @@ -3,7 +3,7 @@
          commit-graph: extract fill_oids_from_packs()
      
          The write_commit_graph() method is too complex, so we are
     -    extracting methods one by one.
     +    extracting helper functions one by one.
      
          This extracts fill_oids_from_packs() that reads the given
          pack-file list and fills the oid list in the context.
     @@ -43,11 +43,11 @@
      +		p = add_packed_git(packname.buf, packname.len, 1);
      +		if (!p) {
      +			error(_("error adding pack %s"), packname.buf);
     -+			return 1;
     ++			return -1;
      +		}
      +		if (open_pack_index(p)) {
      +			error(_("error opening index for %s"), packname.buf);
     -+			return 1;
     ++			return -1;
      +		}
      +		for_each_object_in_pack(p, add_packed_commits, ctx,
      +					FOR_EACH_OBJECT_PACK_ORDER);
     @@ -89,12 +89,12 @@
      -			p = add_packed_git(packname.buf, packname.len, 1);
      -			if (!p) {
      -				error(_("error adding pack %s"), packname.buf);
     --				res = 1;
     +-				res = -1;
      -				goto cleanup;
      -			}
      -			if (open_pack_index(p)) {
      -				error(_("error opening index for %s"), packname.buf);
     --				res = 1;
     +-				res = -1;
      -				goto cleanup;
      -			}
      -			for_each_object_in_pack(p, add_packed_commits, ctx,
  7:  8cb2613dfa !  7:  fe36c8ad28 commit-graph: extract fill_oids_from_commit_hex()
     @@ -3,7 +3,7 @@
          commit-graph: extract fill_oids_from_commit_hex()
      
          The write_commit_graph() method is too complex, so we are
     -    extracting methods one by one.
     +    extracting helper functions one by one.
      
          Extract fill_oids_from_commit_hex() that reads the given commit
          id list and fille the oid list in the context.
  8:  8f7129672a !  8:  b8dfb663f3 commit-graph: extract fill_oids_from_all_packs()
     @@ -3,7 +3,7 @@
          commit-graph: extract fill_oids_from_all_packs()
      
          The write_commit_graph() method is too complex, so we are
     -    extracting methods one by one.
     +    extracting helper functions one by one.
      
          Extract fill_oids_from_all_packs() that reads all pack-files
          for commits and fills the oid list in the context.
  9:  a37548745b !  9:  40acc6ec37 commit-graph: extract count_distinct_commits()
     @@ -3,7 +3,7 @@
          commit-graph: extract count_distinct_commits()
      
          The write_commit_graph() method is too complex, so we are
     -    extracting methods one by one.
     +    extracting helper functions one by one.
      
          Extract count_distinct_commits(), which sorts the oids list, then
          iterates through to find duplicates.
 10:  57366ffdaa ! 10:  b403c01ef5 commit-graph: extract copy_oids_to_commits()
     @@ -3,7 +3,7 @@
          commit-graph: extract copy_oids_to_commits()
      
          The write_commit_graph() method is too complex, so we are
     -    extracting methods one by one.
     +    extracting helper functions one by one.
      
          Extract copy_oids_to_commits(), which fills the commits list
          with the distinct commits from the oids list. During this loop,
 11:  fc81c8946d ! 11:  7ecf923040 commit-graph: extract write_commit_graph_file()
     @@ -3,7 +3,7 @@
          commit-graph: extract write_commit_graph_file()
      
          The write_commit_graph() method is too complex, so we are
     -    extracting methods one by one.
     +    extracting helper functions one by one.
      
          Extract write_commit_graph_file() that takes all of the information
          in the context struct and writes the data to a commit-graph file.
     @@ -41,7 +41,7 @@
      +		UNLEAK(ctx->graph_name);
      +		error(_("unable to create leading directories of %s"),
      +			ctx->graph_name);
     -+		return errno;
     ++		return -1;
      +	}
      +
      +	hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
     @@ -124,7 +124,7 @@
      -		UNLEAK(ctx->graph_name);
      -		error(_("unable to create leading directories of %s"),
      -			ctx->graph_name);
     --		res = errno;
     +-		res = -1;
      -		goto cleanup;
      -	}
      -
-- 
gitgitgadget
Previous: Josh SteadmonNext: Derrick Stolee via GitGitGadget
Message 76 of 89 in “Create commit-graph file format v2”
  1. 0/6 Create commit-graph file format v2Derrick Stolee via GitGitGadget, Jan 23, 2019
  2. 1/6 commit-graph: return with errors during writeDerrick Stolee via GitGitGadget, Jan 23, 2019
  3. 2/6 commit-graph: collapse parameters into flagsDerrick Stolee via GitGitGadget, Jan 23, 2019
  4. 5/6 commit-graph: implement file format version 2Derrick Stolee via GitGitGadget, Jan 23, 2019
  5. Jonathan TanJan 23, 2019
  6. Ævar Arnfjörð BjarmasonJan 24, 2019
  7. Derrick StoleeJan 24, 2019
  8. Ævar Arnfjörð BjarmasonMar 21, 2019
  9. 4/6 commit-graph: add --version=<n> optionDerrick Stolee via GitGitGadget, Jan 23, 2019
  10. Ævar Arnfjörð BjarmasonJan 24, 2019
  11. 6/6 commit-graph: test verifying a corrupt v2 headerDerrick Stolee via GitGitGadget, Jan 23, 2019
  12. Jonathan TanJan 23, 2019
  13. 3/6 commit-graph: create new version flagsDerrick Stolee via GitGitGadget, Jan 23, 2019
  14. Junio C HamanoJan 24, 2019
  15. Junio C HamanoJan 24, 2019
  16. Derrick StoleeJan 25, 2019
  17. 0/5 Create commit-graph file format v2Derrick Stolee via GitGitGadget, Apr 24, 2019
  18. 1/5 commit-graph: return with errors during writeDerrick Stolee via GitGitGadget, Apr 24, 2019
  19. 2/5 commit-graph: collapse parameters into flagsDerrick Stolee via GitGitGadget, Apr 24, 2019
  20. Junio C HamanoApr 25, 2019
  21. 3/5 commit-graph: create new version flagsDerrick Stolee via GitGitGadget, Apr 24, 2019
  22. Junio C HamanoApr 25, 2019
  23. Derrick StoleeApr 25, 2019
  24. Ævar Arnfjörð BjarmasonApr 25, 2019
  25. Junio C HamanoApr 26, 2019
  26. 4/5 commit-graph: add --version=<n> optionDerrick Stolee via GitGitGadget, Apr 24, 2019
  27. 5/5 commit-graph: implement file format version 2Derrick Stolee via GitGitGadget, Apr 24, 2019
  28. Ævar Arnfjörð BjarmasonApr 25, 2019
  29. Junio C HamanoApr 26, 2019
  30. Ævar Arnfjörð BjarmasonApr 26, 2019
  31. Derrick StoleeApr 26, 2019
  32. Ævar Arnfjörð BjarmasonApr 26, 2019
  33. Ævar Arnfjörð BjarmasonApr 27, 2019
  34. 0/6 Create commit-graph file format v2Derrick Stolee via GitGitGadget, May 1, 2019
  35. 1/6 commit-graph: return with errors during writeDerrick Stolee via GitGitGadget, May 1, 2019
  36. Ævar Arnfjörð BjarmasonMay 1, 2019
  37. 2/6 commit-graph: collapse parameters into flagsDerrick Stolee via GitGitGadget, May 1, 2019
  38. 3/6 commit-graph: create new version parameterDerrick Stolee via GitGitGadget, May 1, 2019
  39. 5/6 commit-graph: implement file format version 2Derrick Stolee via GitGitGadget, May 1, 2019
  40. Ævar Arnfjörð BjarmasonMay 1, 2019
  41. Derrick StoleeMay 1, 2019
  42. 4/6 commit-graph: add --version=<n> optionDerrick Stolee via GitGitGadget, May 1, 2019
  43. 6/6 commit-graph: remove Future Work sectionDerrick Stolee via GitGitGadget, May 1, 2019
  44. Ævar Arnfjörð BjarmasonMay 1, 2019
  45. Derrick StoleeMay 1, 2019
  46. Ævar Arnfjörð BjarmasonMay 1, 2019
  47. Derrick StoleeMay 2, 2019
  48. Ævar Arnfjörð BjarmasonMay 2, 2019
  49. Derrick StoleeMay 3, 2019
  50. Ævar Arnfjörð BjarmasonMay 3, 2019
  51. Christian CouderMay 6, 2019
  52. Derrick StoleeMay 6, 2019
  53. SZEDER GáborMay 3, 2019
  54. Derrick StoleeMay 3, 2019
  55. 00/11 Commit-graph write refactor (was: Create commit-graph file format v2)Derrick Stolee via GitGitGadget, May 9, 2019
  56. 01/11 commit-graph: fix the_repository referenceDerrick Stolee via GitGitGadget, May 9, 2019
  57. Junio C HamanoMay 13, 2019
  58. 02/11 commit-graph: return with errors during writeDerrick Stolee via GitGitGadget, May 9, 2019
  59. Junio C HamanoMay 13, 2019
  60. Derrick StoleeMay 13, 2019
  61. Derrick StoleeMay 13, 2019
  62. 04/11 commit-graph: remove Future Work sectionDerrick Stolee via GitGitGadget, May 9, 2019
  63. 03/11 commit-graph: collapse parameters into flagsDerrick Stolee via GitGitGadget, May 9, 2019
  64. Junio C HamanoMay 13, 2019
  65. Derrick StoleeMay 13, 2019
  66. 08/11 commit-graph: extract fill_oids_from_all_packs()Derrick Stolee via GitGitGadget, May 9, 2019
  67. 06/11 commit-graph: extract fill_oids_from_packs()Derrick Stolee via GitGitGadget, May 9, 2019
  68. Junio C HamanoMay 13, 2019
  69. 05/11 commit-graph: create write_commit_graph_contextDerrick Stolee via GitGitGadget, May 9, 2019
  70. 10/11 commit-graph: extract copy_oids_to_commits()Derrick Stolee via GitGitGadget, May 9, 2019
  71. 11/11 commit-graph: extract write_commit_graph_file()Derrick Stolee via GitGitGadget, May 9, 2019
  72. Junio C HamanoMay 13, 2019
  73. 09/11 commit-graph: extract count_distinct_commits()Derrick Stolee via GitGitGadget, May 9, 2019
  74. 07/11 commit-graph: extract fill_oids_from_commit_hex()Derrick Stolee via GitGitGadget, May 9, 2019
  75. Josh SteadmonMay 9, 2019
  76. 00/11 Commit-graph write refactor (was: Create commit-graph file format v2)Derrick Stolee via GitGitGadget, Jun 12, 2019
  77. 01/11 commit-graph: fix the_repository referenceDerrick Stolee via GitGitGadget, Jun 12, 2019
  78. 03/11 commit-graph: collapse parameters into flagsDerrick Stolee via GitGitGadget, Jun 12, 2019
  79. 02/11 commit-graph: return with errors during writeDerrick Stolee via GitGitGadget, Jun 12, 2019
  80. SZEDER GáborJun 29, 2019
  81. Derrick StoleeJul 1, 2019
  82. 04/11 commit-graph: remove Future Work sectionDerrick Stolee via GitGitGadget, Jun 12, 2019
  83. 05/11 commit-graph: create write_commit_graph_contextDerrick Stolee via GitGitGadget, Jun 12, 2019
  84. 08/11 commit-graph: extract fill_oids_from_all_packs()Derrick Stolee via GitGitGadget, Jun 12, 2019
  85. 06/11 commit-graph: extract fill_oids_from_packs()Derrick Stolee via GitGitGadget, Jun 12, 2019
  86. 10/11 commit-graph: extract copy_oids_to_commits()Derrick Stolee via GitGitGadget, Jun 12, 2019
  87. 07/11 commit-graph: extract fill_oids_from_commit_hex()Derrick Stolee via GitGitGadget, Jun 12, 2019
  88. 11/11 commit-graph: extract write_commit_graph_file()Derrick Stolee via GitGitGadget, Jun 12, 2019
  89. 09/11 commit-graph: extract count_distinct_commits()Derrick Stolee via GitGitGadget, Jun 12, 2019

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.