Commit b70703ad8ae920fd55bd7976d1632e05975e5c63

Stefan Sperling 2019-03-18T17:16:10

fix bugs in got_repo_map_path() and add more related tests

diff --git a/lib/repository.c b/lib/repository.c
index afdfaad..fac0482 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -434,7 +434,6 @@ got_repo_map_path(char **in_repo_path, struct got_repository *repo,
 {
 	const struct got_error *err = NULL;
 	const char *repo_abspath = NULL;
-	struct stat sb;
 	size_t repolen, cwdlen, len;
 	char *cwd, *canonpath, *path = NULL;
 
@@ -455,27 +454,30 @@ got_repo_map_path(char **in_repo_path, struct got_repository *repo,
 
 	repo_abspath = got_repo_get_path(repo);
 
-	/* TODO: Call "get in-repository path of work-tree node" API. */
-
-	if (!check_disk)
+	if (!check_disk) {
 		path = strdup(canonpath);
-	else if (lstat(canonpath, &sb) != 0) {
-		if (errno != ENOENT) {
+		if (path == NULL) {
 			err = got_error_from_errno();
 			goto done;
 		}
-		/*
-		 * Path is not on disk.
-		 * Assume it is already relative to repository root.
-		 */
-		path = strdup(canonpath);
 	} else {
 		int is_repo_child = 0, is_cwd_child = 0;
 
 		path = realpath(canonpath, NULL);
 		if (path == NULL) {
-			err = got_error_from_errno();
-			goto done;
+			if (errno != ENOENT) {
+				err = got_error_from_errno();
+				goto done;
+			}
+			/*
+			 * Path is not on disk.
+			 * Assume it is already relative to repository root.
+			 */
+			path = strdup(canonpath);
+			if (path == NULL) {
+				err = got_error_from_errno();
+				goto done;
+			}
 		}
 
 		repolen = strlen(repo_abspath);
@@ -496,7 +498,6 @@ got_repo_map_path(char **in_repo_path, struct got_repository *repo,
 			}
 		} else if (is_repo_child && is_cwd_child) {
 			char *child;
-			/* TODO: Is path inside a got worktree? */
 			/* Strip common prefix with repository path. */
 			err = got_path_skip_common_ancestor(&child,
 			    repo_abspath, path);
@@ -523,7 +524,6 @@ got_repo_map_path(char **in_repo_path, struct got_repository *repo,
 			}
 		} else if (is_cwd_child) {
 			char *child;
-			/* TODO: Is path inside a got worktree? */
 			/* Strip common prefix with cwd. */
 			err = got_path_skip_common_ancestor(&child, cwd,
 			    path);
diff --git a/regress/cmdline/log.sh b/regress/cmdline/log.sh
index 2d890ea..a5272f0 100755
--- a/regress/cmdline/log.sh
+++ b/regress/cmdline/log.sh
@@ -34,6 +34,39 @@ function test_log_in_repo {
 		fi
 	done
 
+	for p in "" "." zeta; do
+		(cd $testroot/repo/epsilon && got log $p | \
+			grep ^commit > $testroot/stdout)
+		cmp $testroot/stdout.expected $testroot/stdout
+		ret="$?"
+		if [ "$ret" != "0" ]; then
+			diff -u $testroot/stdout.expected $testroot/stdout
+			test_done "$testroot" "$ret"
+			return 1
+		fi
+	done
+
+	test_done "$testroot" "0"
+}
+
+function test_log_in_bare_repo {
+	local testroot=`test_init log_in_bare_repo`
+	local head_rev=`git_show_head $testroot/repo`
+
+	echo "commit $head_rev (master)" > $testroot/stdout.expected
+
+	for p in "" "." alpha epsilon epsilon/zeta; do
+		(cd $testroot/repo/.git && got log $p | \
+			grep ^commit > $testroot/stdout)
+		cmp $testroot/stdout.expected $testroot/stdout
+		ret="$?"
+		if [ "$ret" != "0" ]; then
+			diff -u $testroot/stdout.expected $testroot/stdout
+			test_done "$testroot" "$ret"
+			return 1
+		fi
+	done
+
 	test_done "$testroot" "0"
 }
 
@@ -78,4 +111,5 @@ function test_log_in_worktree {
 }
 
 run_test test_log_in_repo
+run_test test_log_in_bare_repo
 run_test test_log_in_worktree