Merge pull request #1476 from libgit2/vmg/bare-open Add `git_repository_open_bare`
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
diff --git a/include/git2/repository.h b/include/git2/repository.h
index e75c8b1..e332097 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -124,6 +124,21 @@ GIT_EXTERN(int) git_repository_open_ext(
const char *ceiling_dirs);
/**
+ * Open a bare repository on the serverside.
+ *
+ * This is a fast open for bare repositories that will come in handy
+ * if you're e.g. hosting git repositories and need to access them
+ * efficiently
+ *
+ * @param out Pointer to the repo which will be opened.
+ * @param bare_path Direct path to the bare repository
+ * @return 0 on success, or an error code
+ */
+GIT_EXTERN(int) git_repository_open_bare(
+ git_repository **out,
+ const char *bare_path);
+
+/**
* Free a previously allocated repository
*
* Note that after a repository is free'd, all the objects it has spawned
diff --git a/src/repository.c b/src/repository.c
index 0ad7449..64ab2f4 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -368,6 +368,37 @@ static int find_repo(
return error;
}
+int git_repository_open_bare(
+ git_repository **repo_ptr,
+ const char *bare_path)
+{
+ int error;
+ git_buf path = GIT_BUF_INIT;
+ git_repository *repo = NULL;
+
+ if ((error = git_path_prettify_dir(&path, bare_path, NULL)) < 0)
+ return error;
+
+ if (!valid_repository_path(&path)) {
+ git_buf_free(&path);
+ giterr_set(GITERR_REPOSITORY, "Path is not a repository: %s", bare_path);
+ return GIT_ENOTFOUND;
+ }
+
+ repo = repository_alloc();
+ GITERR_CHECK_ALLOC(repo);
+
+ repo->path_repository = git_buf_detach(&path);
+ GITERR_CHECK_ALLOC(repo->path_repository);
+
+ /* of course we're bare! */
+ repo->is_bare = 1;
+ repo->workdir = NULL;
+
+ *repo_ptr = repo;
+ return 0;
+}
+
int git_repository_open_ext(
git_repository **repo_ptr,
const char *start_path,