]> git.e2factory.org Git - e2factory.git/commitdiff
generic_git: add current_branch()
authorTobias Ulmer <tu@emlix.com>
Tue, 9 Apr 2019 14:04:40 +0000 (16:04 +0200)
committerTobias Ulmer <tu@emlix.com>
Tue, 9 Apr 2019 14:04:40 +0000 (16:04 +0200)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/generic_git.lua

index 3417ba5189244a23667556305fa85ffc875c4ade..d57d22e45907fd1592239cab9b381f747c4c9ab8 100644 (file)
@@ -293,6 +293,28 @@ function generic_git.lookup_id(git_dir, remote, ref)
     return true, nil, false
 end
 
+--- Get the current branch of gitrepo.
+-- Special cases: Returns an empty string when the repo has no HEAD (fresh init)
+-- Returns "HEAD" when the repo is in detached head state.
+-- @param git_dir Path to GIT_DIR.
+-- @return Short current branch name, "", "HEAD", or false on error.
+-- @return Error object on failure.
+function generic_git.current_branch(git_dir)
+    local rc, re, argv, out
+
+    argv = generic_git.git_new_argv(git_dir, false, "rev-parse",
+        "--abbrev-ref=strict", "HEAD", "--")
+    rc, re, out = generic_git.git(argv)
+    if not rc then
+        if out:find("bad revision 'HEAD'") then
+            return ""
+        end
+
+        return false, err.new("getting current git branch failed"):append(re)
+    end
+    return trim(out)
+end
+
 --- Search ref for a given commit id in either local or remote repository.
 -- The first matching ref is returned. Use filter to check for specific refs.