Commit 5175b31a9e97f249cc1e6621f50d05b3c3dca974

Stefan Sperling 2020-01-04T21:51:24

improve commit graph's error handling of non-existent paths

diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index ce6e873..a205e68 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -497,6 +497,16 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
 			return err;
 	}
 
+	if (graph->iter_node == NULL) {
+		const char *path;
+		if (got_path_is_root_dir(graph->path))
+			return got_error_no_obj(id);
+		path = graph->path;
+		while (path[0] == '/')
+			path++;
+		return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
+	}
+
 	return NULL;
 }
 
diff --git a/regress/cmdline/log.sh b/regress/cmdline/log.sh
old mode 100755
new mode 100744
index 8e9f809..f4d3997
--- a/regress/cmdline/log.sh
+++ b/regress/cmdline/log.sh
@@ -265,9 +265,44 @@ function test_log_limit {
 	test_done "$testroot" "0"
 }
 
+function test_log_nonexistent_path {
+	local testroot=`test_init log_nonexistent_path`
+	local head_rev=`git_show_head $testroot/repo`
+
+	echo "commit $head_rev (master)" > $testroot/stdout.expected
+
+	(cd $testroot/repo && got log this/does/not/exist \
+		> $testroot/stdout 2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "log command succeeded unexpectedly" >&2
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "got: this/does/not/exist: no such entry found in tree" \
+		> $testroot/stderr.expected
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+	fi
+	test_done "$testroot" "$ret"
+}
+
 run_test test_log_in_repo
 run_test test_log_in_bare_repo
 run_test test_log_in_worktree
 run_test test_log_in_worktree_with_path_prefix
 run_test test_log_tag
 run_test test_log_limit
+run_test test_log_nonexistent_path