Introduce 'struct repo' an object used to represent a repository.

Signed-off-by: Brandon Williams <[email protected]>
---
 Makefile |  1 +
 repo.c   | 42 ++++++++++++++++++++++++++++++++++++++++++
 repo.h   | 15 +++++++++++++++
 3 files changed, 58 insertions(+)
 create mode 100644 repo.c
 create mode 100644 repo.h

diff --git a/Makefile b/Makefile
index e35542e63..a49d2f96a 100644
--- a/Makefile
+++ b/Makefile
@@ -821,6 +821,7 @@ LIB_OBJS += refs/ref-cache.o
 LIB_OBJS += ref-filter.o
 LIB_OBJS += remote.o
 LIB_OBJS += replace_object.o
+LIB_OBJS += repo.o
 LIB_OBJS += rerere.o
 LIB_OBJS += resolve-undo.o
 LIB_OBJS += revision.o
diff --git a/repo.c b/repo.c
new file mode 100644
index 000000000..d47e98d95
--- /dev/null
+++ b/repo.c
@@ -0,0 +1,42 @@
+#include "cache.h"
+#include "repo.h"
+
+int
+repo_init(struct repo *repo, const char *gitdir, const char *worktree)
+{
+       int error = 0;
+       char *abspath = real_pathdup(gitdir, 1);
+       char *suspect = xstrfmt("%s/.git", abspath);
+       const char *resolved_gitdir;
+
+       memset(repo, 0, sizeof(struct repo));
+
+       /* First assume 'gitdir' references the gitdir directly */
+       resolved_gitdir = resolve_gitdir_gently(abspath, &error);
+       /* otherwise; try 'gitdir'.git */
+       if (!resolved_gitdir) {
+               resolved_gitdir = resolve_gitdir_gently(suspect, &error);
+               if (!resolved_gitdir) {
+                       die("'%s' is not a repository\n", abspath);
+                       return error;
+               }
+       }
+
+       repo->gitdir = xstrdup(resolved_gitdir);
+       /* Maybe need a check to verify that a worktree is indeed a worktree? */
+       repo->worktree = xstrdup_or_null(worktree);
+
+       free(abspath);
+       free(suspect);
+
+       return error;
+}
+
+void
+repo_clear(struct repo *repo)
+{
+       free(repo->gitdir);
+       repo->gitdir = NULL;
+       free(repo->worktree);
+       repo->worktree = NULL;
+}
diff --git a/repo.h b/repo.h
new file mode 100644
index 000000000..55f2dbec6
--- /dev/null
+++ b/repo.h
@@ -0,0 +1,15 @@
+#ifndef REPO_H
+#define REPO_H
+
+struct index_state;
+
+struct repo {
+       char *gitdir;
+       char *worktree;
+       const char *submodule_prefix;
+};
+
+extern int repo_init(struct repo *repo, const char *gitdir, const char 
*worktree);
+extern void repo_clear(struct repo *repo);
+
+#endif /* REPO_H */
-- 
2.13.0.303.g4ebf302169-goog

Reply via email to