From 5255f01561f5cf204d2c796bf80469f9e59da535 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Tue, 9 Apr 2019 16:04:40 +0200 Subject: [PATCH] generic_git: add current_branch() Signed-off-by: Tobias Ulmer --- generic/generic_git.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/generic/generic_git.lua b/generic/generic_git.lua index 3417ba5..d57d22e 100644 --- a/generic/generic_git.lua +++ b/generic/generic_git.lua @@ -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. -- 2.39.5