diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64777da --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +.DS_Store diff --git a/.vscode/settings.json b/.vscode/settings.json index 2d77f4a..19700d2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,8 @@ "cSpell.words": [ "hashify", "printenv", - "pyright" + "pyright", + "worktree", + "worktrees", ] } diff --git a/git-grok b/git-grok index 24733f0..3c11fda 100755 --- a/git-grok +++ b/git-grok @@ -1173,19 +1173,31 @@ class Main: # Returns the current remote branch name on GitHub. # def git_get_current_remote_base_branch(self) -> str: - branch = self.shell(["git", "branch", "--show-current"]) - if not branch: - path = ".git/rebase-merge/head-name" - if file := find_file_in_parents(path): - branch = open(file).readline().strip() - if not (m := re.match(r"refs/heads/(.+)", branch)): - raise UserException(f"File {path} doesn't contain refs/heads/*") - branch = m.group(1) - else: + # Worktrees: Favor upstream in case of local/remote name mismatch. + [_, upstream, _] = self.shell_no_throw( + ["git", "rev-parse", "--abbrev-ref", "@{upstream}"] + ) + if upstream: + if not (m := re.match(r"^[^/]+/(.+)$", upstream)): raise UserException( - f"You must be on a branch or in an interactive rebase mode." + f"Upstream {upstream} doesn't match / format." ) - return branch + return m.group(1) + + branch = self.shell(["git", "branch", "--show-current"]) + if branch: + return branch + + path = ".git/rebase-merge/head-name" + if file := find_file_in_parents(path): + branch = open(file).readline().strip() + if not (m := re.match(r"refs/heads/(.+)", branch)): + raise UserException(f"File {path} doesn't contain refs/heads/*") + return m.group(1) + else: + raise UserException( + f"You must be on a branch or in an interactive rebase mode." + ) # # Returns parsed commits between two refs in reverse-chronological order