From 34484f3ba27f8b19b04c35beadfc55868498c98e Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 29 Nov 2023 13:49:15 -0500 Subject: Sigh. More complicated than it should be. --- src/lpm.c | 28 ++++++++++++++++++++++------ src/lpm.lua | 22 +++++++++++++++++----- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/lpm.c b/src/lpm.c index 2b6faf2..00d58be 100644 --- a/src/lpm.c +++ b/src/lpm.c @@ -307,16 +307,31 @@ static int git_get_id(git_oid* commit_id, git_repository* repository, const char int is_hex = 1; for (int i = 0; is_hex && i < length; ++i) is_hex = isxdigit(name[i]); + git_reference* ref; + if (!git_reference_dwim(&ref, repository, name)) { + git_object* object; + fprintf(stderr, "WAT\n"); + if (!git_reference_peel(&object, ref, GIT_OBJ_ANY)) { + if (git_object_type(object) == GIT_OBJ_COMMIT) { + git_oid_cpy(commit_id, git_object_id(object)); + git_object_free(object); + git_reference_free(ref); + return 0; + } + } + git_object_free(object); + git_reference_free(ref); + } else { + fprintf(stderr, "WATA: %s\n", git_error_last_string()); + } if (!is_hex || length < 3) return git_reference_name_to_id(commit_id, repository, name); if (length < GIT_OID_SHA1_SIZE*2) { - if (length % 2 != 0) - return -1; git_commit* commit; git_oid oid = {0}; - for (int i = 0; i < length/2; ++i) - oid.id[i] |= (name[i] - '0') << ((i % 2) * 4); - int ret = git_commit_lookup_prefix(&commit, repository, &oid, length/2); + if (git_oid_fromstrp(&oid, name) != 0) + return -1; + int ret = git_commit_lookup_prefix(&commit, repository, &oid, length); if (ret) return ret; git_oid_cpy(commit_id, git_commit_id(commit)); @@ -387,8 +402,9 @@ static int lpm_reset(lua_State* L) { static int lpm_revparse(lua_State* L) { git_init(); git_repository* repository = luaL_checkgitrepo(L, 1); + const char* ref = luaL_optstring(L, 2, "HEAD"); git_oid commit_id; - int got_commit = git_get_id(&commit_id, repository, "HEAD"); + int got_commit = git_get_id(&commit_id, repository, ref); git_repository_free(repository); if (got_commit) return luaL_error(L, "git retrieve commit error: %s", git_error_last_string()); diff --git a/src/lpm.lua b/src/lpm.lua index 3195f10..a330c4b 100644 --- a/src/lpm.lua +++ b/src/lpm.lua @@ -353,9 +353,8 @@ function json.decode(str) end -- End JSON library. -local function is_commit_hash(hash) - return #hash == 40 and not hash:find("[^a-z0-9]") -end +local function is_commit_hash(hash) return #hash == 40 and not hash:find("[^a-f0-9]") end +local function is_probably_commit_hash(hash) return #hash > 5 and not hash:find("[^a-f0-9]") end @@ -452,10 +451,12 @@ function common.rename(src, dst) end end function common.reset(path, ref, type) + print("RESET", path, ref) if is_commit_hash(ref) then system.reset(path, ref, type) else - if not pcall(system.reset, path, "refs/tags/" .. ref, type) then system.reset(path, "refs/remotes/origin/" .. ref, type) end + --if not pcall(system.reset, path, "refs/tags/" .. ref, type) then system.reset(path, "refs/remotes/origin/" .. ref, type) end + system.reset(path, ref, type) end end function common.chdir(dir, callback) @@ -1174,7 +1175,17 @@ function Repository:fetch() if self.commit then system.fetch(temporary_path or path, write_progress_bar, self.commit) elseif self.branch then - system.fetch(temporary_path or path, write_progress_bar, "+refs/heads/" .. self.branch .. ":refs/remotes/origin/" .. self.branch) + if is_probably_commit_hash(self.branch) then + print("BRANCH", self.branch) + system.fetch(temporary_path or path, write_progress_bar, "+refs/heads/*:refs/remotes/origin/*") + self.commit = system.revparse(temporary_path or path, self.branch) + self.branch = nil + self.local_path = self.repo_path .. PATHSEP .. self.commit + path = self.local_path + else + print("OTHERWISE", self.branch) + system.fetch(temporary_path or path, write_progress_bar, "+refs/heads/" .. self.branch .. ":refs/remotes/origin/" .. self.branch) + end end common.reset(temporary_path or path, self.commit or self.branch, "hard") end @@ -1183,6 +1194,7 @@ function Repository:fetch() if temporary_path then common.mkdirp(common.dirname(path)) common.rename(temporary_path, path) + self.local_path = path end end) if not status then -- cgit v1.2.3