summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2019-07-28 13:21:33 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2019-07-28 13:21:33 +0100
commit801b1a160558ed8b862e683c7aaf5092b0c1adb2 (patch)
treecdfd96d7f5164195e722f2c0d6df70c1613fa357 /src
parentReplace hard-coded test values with properties (diff)
downloadnetfs-gitfs-801b1a160558ed8b862e683c7aaf5092b0c1adb2.tar.bz2
netfs-gitfs-801b1a160558ed8b862e683c7aaf5092b0c1adb2.tar.xz
netfs-gitfs-801b1a160558ed8b862e683c7aaf5092b0c1adb2.zip
Support working tree based on a commit, tag or branch
Diffstat (limited to 'src')
-rw-r--r--src/git.cpp10
-rw-r--r--src/git.h7
-rw-r--r--src/repo.cpp16
-rw-r--r--src/repo.h4
4 files changed, 33 insertions, 4 deletions
diff --git a/src/git.cpp b/src/git.cpp
index 045d146..b169fa2 100644
--- a/src/git.cpp
+++ b/src/git.cpp
@@ -63,6 +63,16 @@ namespace GitFS {
return gitSafeGet<NetFS::SystemError>(
git_tree_entry_bypath, git_tree_entry_free, tree.get(), path.c_str() + 1);
}
+ RefPtr Commitish(const RepositoryPtr & repo, const std::string & name)
+ {
+ return gitSafeGet<NetFS::ConfigError>(
+ git_reference_dwim, git_reference_free, repo.get(), name.c_str());
+ }
+ RefPtr Resolve(const RefPtr & ref)
+ {
+ return gitSafeGet<NetFS::ConfigError>(
+ git_reference_resolve, git_reference_free, ref.get());
+ }
}
}
diff --git a/src/git.h b/src/git.h
index 56a7cf6..068e8cf 100644
--- a/src/git.h
+++ b/src/git.h
@@ -14,7 +14,7 @@ namespace GitFS {
void
gitSafe(int (*func)(P...), A ... p)
{
- if (int _giterror = func(p...); _giterror != 0) {
+ if (int _giterror = func(p...)) {
throwError<E>(_giterror);
}
}
@@ -61,6 +61,11 @@ namespace GitFS {
using TreeEntryPtr = decltype(gitSafeGet<std::exception>(
git_tree_entry_bypath, git_tree_entry_free, nullptr, nullptr));
TreeEntryPtr TreeEntryByPath(const TreePtr & tree, const std::string & path);
+
+ using RefPtr = decltype(gitSafeGet<std::exception>(
+ git_reference_dwim, git_reference_free, nullptr, nullptr));
+ RefPtr Commitish(const RepositoryPtr & repo, const std::string & name);
+ RefPtr Resolve(const RefPtr &);
}
}
diff --git a/src/repo.cpp b/src/repo.cpp
index 4059897..0da7e4a 100644
--- a/src/repo.cpp
+++ b/src/repo.cpp
@@ -11,11 +11,23 @@ std::string operator/(const std::string & a, const std::string & b)
GitFS::Repo::Repo(const PropertyReader & properties) :
repo(Git::RepositoryOpenBare(properties("gitdir"))),
- commit(Git::CommitLookup(repo, Git::OidParse(properties("commit")))),
- tree(Git::TreeLookup(repo, *git_commit_tree_id(commit.get()))),
+ commitish(properties("commitish") / "master"),
gid(properties("gid") / "root"),
uid(properties("uid") / "root")
{
+ git_oid commitId;
+ if (commitish.length() == GIT_OID_HEXSZ) {
+ commitId = Git::OidParse(commitish);
+ }
+ else {
+ auto ref = Git::Commitish(repo, commitish);
+ isBranch = git_reference_is_branch(ref.get());
+ ref = Git::Resolve(ref);
+ commitId = *git_reference_target(ref.get());
+ }
+
+ commit = Git::CommitLookup(repo, commitId);
+ tree = Git::TreeLookup(repo, *git_commit_tree_id(commit.get()));
}
void
diff --git a/src/repo.h b/src/repo.h
index 18b1057..adeac94 100644
--- a/src/repo.h
+++ b/src/repo.h
@@ -35,7 +35,9 @@ namespace GitFS {
private:
friend class Directory;
friend class Blob;
- Git::RepositoryPtr repo;
+ const Git::RepositoryPtr repo;
+ const std::string commitish;
+ bool isBranch;
Git::CommitPtr commit;
Git::TreePtr tree;
const std::string gid, uid;