Commit 5e54fb308c7ad735d944068c063de6ac5276b93a

Stefan Sperling 2019-05-31T14:57:57

test and fix 'got revert' with a path in a sub-directory Relax input path requirements of got_object_id_by_path() to make things easier to callers. Allows the revert code to pass a path from the file index in lieu of a repository path.

diff --git a/lib/object.c b/lib/object.c
index 8491d04..1c9eb57 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1384,16 +1384,12 @@ got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
 
 	*id = NULL;
 
-	/* We are expecting an absolute in-repository path. */
-	if (path[0] != '/')
-		return got_error(GOT_ERR_NOT_ABSPATH);
-
 	err = got_object_open_as_commit(&commit, repo, commit_id);
 	if (err)
 		goto done;
 
 	/* Handle opening of root of commit's tree. */
-	if (path[1] == '\0') {
+	if (got_path_is_root_dir(path)) {
 		*id = got_object_id_dup(commit->tree_id);
 		if (*id == NULL)
 			err = got_error_from_errno("got_object_id_dup");
@@ -1405,7 +1401,8 @@ got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
 		goto done;
 
 	s = path;
-	s++; /* skip leading '/' */
+	while (s[0] == '/')
+		s++;
 	seg = s;
 	seglen = 0;
 	while (*s) {
diff --git a/regress/cmdline/revert.sh b/regress/cmdline/revert.sh
index 584c34e..eec57c2 100755
--- a/regress/cmdline/revert.sh
+++ b/regress/cmdline/revert.sh
@@ -26,11 +26,11 @@ function test_revert_basic {
 		return 1
 	fi
 
-	echo "modified alpha" > $testroot/wt/alpha
+	echo "modified epsilon/zeta" > $testroot/wt/epsilon/zeta
 
-	echo 'R  alpha' > $testroot/stdout.expected
+	echo 'R  epsilon/zeta' > $testroot/stdout.expected
 
-	(cd $testroot/wt && got revert alpha > $testroot/stdout)
+	(cd $testroot/wt && got revert epsilon/zeta > $testroot/stdout)
 
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret="$?"
@@ -40,8 +40,8 @@ function test_revert_basic {
 		return 1
 	fi
 
-	echo "alpha" > $testroot/content.expected
-	cat $testroot/wt/alpha > $testroot/content
+	echo "zeta" > $testroot/content.expected
+	cat $testroot/wt/epsilon/zeta > $testroot/content
 
 	cmp -s $testroot/content.expected $testroot/content
 	ret="$?"