Skip to content

Commit

Permalink
branch: compatible with old Git
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Feb 8, 2021
1 parent c8cb719 commit 5640f14
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = gitutils
version = 1.8.1
version = 1.8.2
author = Michael Hirsch, Ph.D.
author_email = [email protected]
description = concurrent, pipelined, platform-agnostic Git utilities for managing a large number of Git repositories
Expand Down
19 changes: 12 additions & 7 deletions src/gitutils/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

BRANCH_REV = ["rev-parse", "--abbrev-ref", "HEAD"]
BRANCH_SYM = ["symbolic-ref", "--short", "HEAD"]
BRANCH_NAME = ["name-rev", "--name-only", "HEAD"]

BRANCH_NAME = ["name-rev", "--name-only", "HEAD"] # old Git
BRANCH_SIMPLE = ["branch", "--show-current"] # Git >= 2.22
SWITCH = ["switch"]

SWITCH = ["checkout"] # Git < 2.23


async def different_branch(main: T.Sequence[str], path: Path) -> T.Tuple[str, str]:
Expand All @@ -42,11 +44,11 @@ async def different_branch(main: T.Sequence[str], path: Path) -> T.Tuple[str, st
"""

proc = await asyncio.create_subprocess_exec(
*[GITEXE, "-C", str(path)] + BRANCH_SIMPLE, stdout=asyncio.subprocess.PIPE
*[GITEXE, "-C", str(path)] + BRANCH_NAME, stdout=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
if proc.returncode != 0:
logging.error(f"{path.name} return code {proc.returncode} {BRANCH_SIMPLE}")
logging.error(f"{path.name} return code {proc.returncode} {BRANCH_NAME}")
logging.info(str(path))

branch_name = stdout.decode("utf8").strip()
Expand Down Expand Up @@ -75,15 +77,15 @@ async def git_branch(branch: T.Sequence[str], path: Path) -> T.List[T.Tuple[str,
def branch_switch(path: Path, old_branch: str, new_branch: str):

for d in gitdirs(path):
cmd = [GITEXE, "-C", str(d)] + BRANCH_SIMPLE
cmd = [GITEXE, "-C", str(d)] + BRANCH_NAME
current = subprocess.check_output(cmd, text=True, timeout=5).strip()
if current == new_branch:
continue
if current != old_branch:
logging.info(f"not changing: {d.name}: {current} != {old_branch}")
continue

cmd = [GITEXE, "-C", str(d)] + BRANCH_SIMPLE
cmd = [GITEXE, "-C", str(d)] + BRANCH_NAME
ret = subprocess.run(
[GITEXE, "-C", str(d)] + SWITCH + [new_branch],
stderr=subprocess.PIPE,
Expand All @@ -92,7 +94,10 @@ def branch_switch(path: Path, old_branch: str, new_branch: str):
)
if ret.returncode != 0:
stderr = ret.stderr.strip()
if stderr == f"fatal: invalid reference: {new_branch}":
if stderr in {
f"fatal: invalid reference: {new_branch}", # switch
f"error: pathspec '{new_branch}' did not match any file(s) known to git",
}: # checkout
continue
else:
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion src/gitutils/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@pytest.fixture(scope="function")
def git_init(tmp_path):
subprocess.check_call(["git", "-C", str(tmp_path), "init"])
subprocess.check_call(["git", "-C", str(tmp_path), "init", "-b", "main"])
return tmp_path


Expand Down
15 changes: 8 additions & 7 deletions src/gitutils/tests/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
from gitutils.branch import git_branch


@pytest.mark.parametrize("name, N", [("master", False), ("fake", True)])
def test_script_branch(name, N, git_init):
p = git_init
@pytest.mark.parametrize("name, N", [("main", False), ("fake", True)])
def test_script_branch(name, N, git_commit):
p = git_commit

ret = subprocess.check_output(["gitbranch", str(p), "-main", name], text=True)

if N:
assert ret
else:
assert not ret


@pytest.mark.parametrize("name,N", [("master", 0), ("fake", 1)])
def test_branch(name, N, git_init):
p = git_init
@pytest.mark.parametrize("name,N", [("main", 0), ("fake", 1)])
def test_branch(name, N, git_commit):
p = git_commit
branches = asyncio.run(git_branch(name, p))
assert len(branches) == N
assert len(branches) == N, f"{branches}"

0 comments on commit 5640f14

Please sign in to comment.