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

[PATCH 3/6] builtin/commit-graph.c: die() with unknown '--object-dir'

From
Taylor Blau <me@ttaylorr.com>
Date
Jan 30, 2020, 23:00 UTC
Message-ID
<bff635973073f4c496213e0416483985d7a4d12e.1580424766.git.me@ttaylorr.com>
In-Reply-To
<cover.1580424766.git.me@ttaylorr.com>

The commit-graph sub-commands 'write', 'verify' both take an '--object-dir' argument, which is used to specify the location of an object directory containing commit-graphs.

However, there was no verification that the '--object-dir' argument was an object directory. In the case of an '--object-dir' argument that either (1) doesn't exist, or (2) isn't an object directory, 'git commit-graph ...' would exit silently with status zero.

This can clearly lead to unintended behavior, such as verifying commit-graphs that aren't in a repository's own object store (or one of its alternates), or causing a typo to mask a legitimate commit-graph verification failure.

To remedy this, let's wrap 'find_odb()' with 'find_odb_or_die()' and cause the above such errors to produce an error and non-zero exit code.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 Documentation/git-commit-graph.txt |  5 ++++-
 builtin/commit-graph.c             | 13 +++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-commit-graph.txt b/Documentation/git-commit-graph.txt
index bcd85c1976..28d1fee505 100644
--- a/Documentation/git-commit-graph.txt
+++ b/Documentation/git-commit-graph.txt
@@ -26,7 +26,10 @@ OPTIONS
 	file. This parameter exists to specify the location of an alternate
 	that only has the objects directory, not a full `.git` directory. The
 	commit-graph file is expected to be in the `<dir>/info` directory and
-	the packfiles are expected to be in `<dir>/pack`.
+	the packfiles are expected to be in `<dir>/pack`. If the directory
+	could not be made into an absolute path, or does not match any known
+	object directory, `git commit-graph ...` will exit with non-zero
+	status.
 
 --[no-]progress::
 	Turn progress on/off explicitly. If neither is specified, progress is
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 3edac318e8..93ff90d73b 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -34,6 +34,15 @@ static struct opts_commit_graph {
 	int progress;
 } opts;
 
+static struct object_directory *find_odb_or_die(struct repository *r,
+						const char *obj_dir)
+{
+	struct object_directory *odb = find_odb(r, obj_dir);
+	if (!odb)
+		die(_("could not find object directory matching %s"), obj_dir);
+	return odb;
+}
+
 static int graph_verify(int argc, const char **argv)
 {
 	struct commit_graph *graph = NULL;
@@ -78,7 +87,7 @@ static int graph_verify(int argc, const char **argv)
 		graph = load_commit_graph_one_fd_st(fd, &st);
 	else {
 		struct object_directory *odb;
-		if ((odb = find_odb(the_repository, opts.obj_dir)))
+		if ((odb = find_odb_or_die(the_repository, opts.obj_dir)))
 			graph = read_commit_graph_one(the_repository, odb);
 	}
 
@@ -149,7 +158,7 @@ static int graph_write(int argc, const char **argv)
 		flags |= COMMIT_GRAPH_WRITE_PROGRESS;
 
 	read_replace_refs = 0;
-	odb = find_odb(the_repository, opts.obj_dir);
+	odb = find_odb_or_die(the_repository, opts.obj_dir);
 
 	if (opts.reachable) {
 		if (write_commit_graph_reachable(odb->path, flags, &split_opts))
-- 
2.25.0.dirty
Previous: Taylor BlauNext: Jeff King
Message 14 of 28 in “commit-graph: use 'struct object_directory *' everywhere”
  1. 0/6 commit-graph: use 'struct object_directory *' everywhereTaylor Blau, Jan 30, 2020
  2. 1/6 t5318: don't pass non-object directory to '--object-dir'Taylor Blau, Jan 30, 2020
  3. 5/6 commit-graph.c: remove path normalization, comparisonTaylor Blau, Jan 30, 2020
  4. 6/6 commit-graph.h: use odb in 'load_commit_graph_one_fd_st'Taylor Blau, Jan 30, 2020
  5. 2/6 commit-graph.h: store object directory in 'struct commit_graph'Taylor Blau, Jan 30, 2020
  6. Martin ÅgrenJan 31, 2020
  7. Jeff KingJan 31, 2020
  8. Martin ÅgrenJan 31, 2020
  9. Taylor BlauFeb 3, 2020
  10. Jeff KingFeb 3, 2020
  11. Junio C HamanoJan 31, 2020
  12. Taylor BlauFeb 3, 2020
  13. 4/6 commit-graph.h: store an odb in 'struct write_commit_graph_context'Taylor Blau, Jan 30, 2020
  14. 3/6 builtin/commit-graph.c: die() with unknown '--object-dir'Taylor Blau, Jan 30, 2020
  15. Jeff KingJan 31, 2020
  16. Derrick StoleeJan 31, 2020
  17. Taylor BlauFeb 3, 2020
  18. 0/5 commit-graph: use 'struct object_directory *' everywhereTaylor Blau, Feb 3, 2020
  19. 1/5 t5318: don't pass non-object directory to '--object-dir'Taylor Blau, Feb 3, 2020
  20. 2/5 commit-graph.h: store an odb in 'struct write_commit_graph_context'Taylor Blau, Feb 3, 2020
  21. 2/5 commit-graph.h: store an odb in 'struct write_commit_graph_context'Taylor Blau, Feb 4, 2020
  22. Junio C HamanoFeb 4, 2020
  23. Taylor BlauFeb 4, 2020
  24. Junio C HamanoFeb 4, 2020
  25. 5/5 commit-graph.h: use odb in 'load_commit_graph_one_fd_st'Taylor Blau, Feb 3, 2020
  26. 4/5 commit-graph.c: remove path normalization, comparisonTaylor Blau, Feb 3, 2020
  27. 3/5 commit-graph.h: store object directory in 'struct commit_graph'Taylor Blau, Feb 3, 2020
  28. Jeff KingFeb 5, 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.