Commit 49520a3227e82d18f80658e593e3300434fa8625

Stefan Sperling 2018-12-29T15:51:56

verify path prefix when checking out into existing work tree

diff --git a/got/got.c b/got/got.c
index 238a09f..00586f4 100644
--- a/got/got.c
+++ b/got/got.c
@@ -283,6 +283,11 @@ cmd_checkout(int argc, char *argv[])
 	if (error != NULL)
 		goto done;
 
+	if (strcmp(path_prefix, got_worktree_get_path_prefix(worktree)) != 0) {
+		error = got_error(GOT_ERR_PATH_PREFIX);
+		goto done;
+	}
+
 	error = got_worktree_checkout_files(worktree, repo,
 	    checkout_progress, worktree_path, checkout_cancel, NULL);
 	if (error != NULL)
diff --git a/include/got_error.h b/include/got_error.h
index e7d37e6..7720ed2 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -67,6 +67,7 @@
 #define GOT_ERR_FILEIDX_SIG	51
 #define GOT_ERR_FILEIDX_VER	52
 #define GOT_ERR_FILEIDX_CSUM	53
+#define GOT_ERR_PATH_PREFIX	54
 
 static const struct got_error {
 	int code;
@@ -122,6 +123,8 @@ static const struct got_error {
 	{ GOT_ERR_FILEIDX_SIG,	"bad file index signature" },
 	{ GOT_ERR_FILEIDX_VER,	"unknown file index format version" },
 	{ GOT_ERR_FILEIDX_CSUM,	"bad file index checksum" },
+	{ GOT_ERR_PATH_PREFIX,	"worktree already contains items from a "
+				"different path prefix" },
 };
 
 /*
diff --git a/include/got_worktree.h b/include/got_worktree.h
index aa7c545..80de9bd 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -47,6 +47,12 @@ void got_worktree_close(struct got_worktree *);
 char *got_worktree_get_repo_path(struct got_worktree *);
 
 /*
+ * Get the path prefix associated with a worktree.
+ * The caller must dispose of it with free(3).
+ */
+const char *got_worktree_get_path_prefix(struct got_worktree *);
+
+/*
  * Get the name of a work tree's HEAD reference.
  * The caller must dispose of it with free(3).
  */
diff --git a/lib/worktree.c b/lib/worktree.c
index 1a57978..c55f3e7 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -376,6 +376,12 @@ got_worktree_get_repo_path(struct got_worktree *worktree)
 	return strdup(worktree->repo_path);
 }
 
+const char *
+got_worktree_get_path_prefix(struct got_worktree *worktree)
+{
+	return worktree->repo_path;
+}
+
 char *
 got_worktree_get_head_ref_name(struct got_worktree *worktree)
 {