Commit 2e8c69d19fae38dc738e3dfb02687b1503029a78

Stefan Sperling 2020-05-04T19:37:36

fix "no such entry found in tree" error with got log -p and an added path ok millert@

diff --git a/got/got.c b/got/got.c
index 7bf9457..874e65a 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2923,13 +2923,16 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 			err = got_object_id_by_path(&obj_id1, repo,
 			    qid->id, path);
 			if (err) {
-				free(obj_id2);
-				goto done;
-			}
-			err = got_object_id_str(&id_str1, obj_id1);
-			if (err) {
-				free(obj_id2);
-				goto done;
+				if (err->code != GOT_ERR_NO_TREE_ENTRY) {
+					free(obj_id2);
+					goto done;
+				}
+			} else {
+				err = got_object_id_str(&id_str1, obj_id1);
+				if (err) {
+					free(obj_id2);
+					goto done;
+				}
 			}
 		}
 		err = got_object_get_type(&obj_type, repo, obj_id2);
diff --git a/regress/cmdline/log.sh b/regress/cmdline/log.sh
index ea958f5..b05b0f4 100755
--- a/regress/cmdline/log.sh
+++ b/regress/cmdline/log.sh
@@ -283,6 +283,40 @@ function test_log_limit {
 	test_done "$testroot" "0"
 }
 
+function test_log_patch_added_file {
+	local testroot=`test_init log_patch_added_file`
+	local commit_id0=`git_show_head $testroot/repo`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "new file" > $testroot/wt/new
+	(cd $testroot/wt && got add new >/dev/null)
+	(cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
+	local commit_id1=`git_show_head $testroot/repo`
+
+	echo "commit $commit_id1 (master)" > $testroot/stdout.expected
+	# This used to fail with 'got: no such entry found in tree'
+	(cd $testroot/wt && got log -l1 -p new > $testroot/stdout.patch)
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got log command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	grep ^commit $testroot/stdout.patch > $testroot/stdout
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
 function test_log_nonexistent_path {
 	local testroot=`test_init log_nonexistent_path`
 	local head_rev=`git_show_head $testroot/repo`
@@ -618,6 +652,7 @@ 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_patch_added_file
 run_test test_log_nonexistent_path
 run_test test_log_end_at_commit
 run_test test_log_reverse_display