aboutsummaryrefslogtreecommitdiff
path: root/b4/__init__.py
diff options
context:
space:
mode:
authorKonstantin Ryabitsev <konstantin@linuxfoundation.org>2020-12-14 13:27:59 -0500
committerKonstantin Ryabitsev <konstantin@linuxfoundation.org>2020-12-14 13:27:59 -0500
commit4e396d2091fe70d2217865a1836695e00b5067f2 (patch)
treea4ff1768db4250bfe71becca5ab989db84395655 /b4/__init__.py
parent7fd417718e2cde644fd6b6529ca30bfb10ba3a01 (diff)
downloadb4-4e396d2091fe70d2217865a1836695e00b5067f2.tar.gz
Fix crasher when we don't use -g with b4 pr
If we're not passing -g to "b4 pr -e", then we should try to see if we are inside a git checkout and use that as our source. Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
Diffstat (limited to 'b4/__init__.py')
-rw-r--r--b4/__init__.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/b4/__init__.py b/b4/__init__.py
index 05bd9bb..432a8cd 100644
--- a/b4/__init__.py
+++ b/b4/__init__.py
@@ -2021,6 +2021,15 @@ def git_temp_worktree(gitdir=None, commitish=None):
@contextmanager
def git_temp_clone(gitdir=None):
"""Context manager that creates a temporary shared clone."""
+ if gitdir is None:
+ topdir = git_get_toplevel()
+ if topdir and os.path.isdir(os.path.join(topdir, '.git')):
+ gitdir = os.path.join(topdir, '.git')
+
+ if not gitdir:
+ logger.critical('Current directory is not a git checkout. Try using -g.')
+ return None
+
with TemporaryDirectory() as dfn:
gitargs = ['clone', '--mirror', '--shared', gitdir, dfn]
git_run_command(None, gitargs)
@@ -2352,6 +2361,16 @@ def git_branch_contains(gitdir, commit_id):
return lines
+def git_get_toplevel(path=None):
+ topdir = None
+ # Are we in a git tree and if so, what is our toplevel?
+ gitargs = ['rev-parse', '--show-toplevel']
+ lines = git_get_command_lines(path, gitargs)
+ if len(lines) == 1:
+ topdir = lines[0]
+ return topdir
+
+
def format_addrs(pairs):
addrs = set()
for pair in pairs: