Commit 442a3ddc59a2c8a06621dd008f5449481939cbc4

Stefan Sperling 2018-04-23T09:38:37

try to infer repository path from work tree

diff --git a/got/got.c b/got/got.c
index 3666d5b..c710058 100644
--- a/got/got.c
+++ b/got/got.c
@@ -449,7 +449,7 @@ cmd_log(int argc, char *argv[])
 	const char *errstr;
 
 #ifndef PROFILE
-	if (pledge("stdio rpath wpath cpath proc", NULL) == -1)
+	if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
 		err(1, "pledge");
 #endif
 
@@ -623,7 +623,7 @@ cmd_diff(int argc, char *argv[])
 	int ch;
 
 #ifndef PROFILE
-	if (pledge("stdio rpath wpath cpath proc", NULL) == -1)
+	if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
 		err(1, "pledge");
 #endif
 
diff --git a/lib/repository.c b/lib/repository.c
index 611e952..fa438db 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -27,6 +27,7 @@
 #include "got_error.h"
 #include "got_reference.h"
 #include "got_repository.h"
+#include "got_worktree.h"
 
 #include "got_lib_path.h"
 #include "got_lib_delta.h"
@@ -34,6 +35,7 @@
 #include "got_lib_object.h"
 #include "got_lib_pack.h"
 #include "got_lib_repository.h"
+#include "got_lib_worktree.h"
 
 #ifndef nitems
 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
@@ -184,6 +186,28 @@ got_repo_open(struct got_repository **ret, const char *path)
 			goto done;
 		}
 		if (!is_git_repo(repo)) {
+			struct got_worktree *worktree;
+			if (got_worktree_open(&worktree, repo->path) == NULL) {
+				free(repo->path_git_dir);
+				repo->path_git_dir =
+				    strdup(worktree->repo_path);
+				if (repo->path_git_dir == NULL) {
+					err = got_error_from_errno();
+					goto done;
+				}
+				if (!is_git_repo(repo)) {
+					free(repo->path_git_dir);
+					if (asprintf(&repo->path_git_dir,
+					    "%s/%s", worktree->repo_path,
+					    GOT_GIT_DIR) == -1) {
+						err = got_error_from_errno();
+						goto done;
+					}
+				}
+				got_worktree_close(worktree);
+			}
+		}
+		if (!is_git_repo(repo)) {
 			err = got_error(GOT_ERR_NOT_GIT_REPO);
 			goto done;
 		}
diff --git a/regress/repository/Makefile b/regress/repository/Makefile
index 0058276..605897b 100644
--- a/regress/repository/Makefile
+++ b/regress/repository/Makefile
@@ -2,7 +2,8 @@
 
 PROG = repository_test
 SRCS = path.c repository.c error.c reference.c object.c sha1.c diff.c \
-	diffreg.c pack.c privsep.c delta.c zbuf.c repository_test.c
+	diffreg.c pack.c privsep.c delta.c fileindex.c worktree.c \
+	zbuf.c repository_test.c
 
 CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
 LDADD = -lutil -lz