add support for opening bare repositories
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
diff --git a/lib/got_repository_lib.h b/lib/got_repository_lib.h
index 104167f..2eecc5f 100644
--- a/lib/got_repository_lib.h
+++ b/lib/got_repository_lib.h
@@ -18,6 +18,7 @@
struct got_repository {
char *path;
+ char *path_git_dir;
/* The pack index cache speeds up search for packed objects. */
struct got_packidx_v2_hdr *packidx_cache[GOT_PACKIDX_CACHE_SIZE];
diff --git a/lib/repository.c b/lib/repository.c
index c13711c..96fa4ce 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -60,12 +60,7 @@ got_repo_get_path(struct got_repository *repo)
char *
got_repo_get_path_git_dir(struct got_repository *repo)
{
- char *path_git;
-
- if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
- return NULL;
-
- return path_git;
+ return strdup(repo->path_git_dir);
}
static char *
@@ -73,7 +68,7 @@ get_path_git_child(struct got_repository *repo, const char *basename)
{
char *path_child;
- if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
+ if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
basename) == -1)
return NULL;
@@ -169,15 +164,28 @@ got_repo_open(struct got_repository **ret, const char *path)
goto done;
}
- if (!is_git_repo(repo)) {
- err = got_error(GOT_ERR_NOT_GIT_REPO);
+ repo->path_git_dir = strdup(repo->path);
+ if (repo->path_git_dir == NULL) {
+ err = got_error(GOT_ERR_NO_MEM);
goto done;
}
+ if (!is_git_repo(repo)) {
+ free(repo->path_git_dir);
+ if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
+ GOT_GIT_DIR) == -1) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+ if (!is_git_repo(repo)) {
+ err = got_error(GOT_ERR_NOT_GIT_REPO);
+ goto done;
+ }
+ }
*ret = repo;
done:
if (err)
- free(repo);
+ got_repo_close(repo);
free(abspath);
return err;
}
@@ -193,5 +201,6 @@ got_repo_close(struct got_repository *repo)
got_packidx_close(repo->packidx_cache[i]);
}
free(repo->path);
+ free(repo->path_git_dir);
free(repo);
}