Skip to content

Commit c4a77ec

Browse files
authored
Merge pull request #337 from faster-cpython/codeflash/optimize-get_git_merge_base-m5rggte6
⚡️ Speed up function `get_git_merge_base` by 6%
2 parents b710f09 + 9600fb5 commit c4a77ec

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

Diff for: bench_runner/git.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@ def get_log(
3030
if extra is None:
3131
extra = []
3232

33-
if ref is None:
34-
ref_args = []
35-
else:
36-
ref_args = [ref]
37-
if n < 1:
38-
n_args = []
39-
else:
40-
n_args = ["-n", str(n)]
33+
ref_args = [] if ref is None else [ref]
34+
n_args = [] if n < 1 else ["-n", str(n)]
35+
4136
return subprocess.check_output(
4237
["git", "log", f"--pretty=format:{format}", *n_args, *ref_args, *extra],
4338
encoding="utf-8",
@@ -64,7 +59,9 @@ def get_git_merge_base(dirname: PathLike) -> str | None:
6459
# We need to make sure we have commits from main that are old enough to be
6560
# the base of this branch, but not so old that we waste a ton of bandwidth
6661
commit_date = datetime.datetime.fromisoformat(get_git_commit_date(dirname))
67-
commit_date = commit_date - datetime.timedelta(365 * 2)
62+
commit_date = commit_date - datetime.timedelta(days=365 * 2)
63+
64+
# Get current commit hash
6865
commit_hash = get_log("%H", dirname)
6966

7067
try:
@@ -79,10 +76,7 @@ def get_git_merge_base(dirname: PathLike) -> str | None:
7976
cwd=dirname,
8077
)
8178
except subprocess.CalledProcessError as e:
82-
if e.returncode in (3, 128):
83-
# Remote may already exist, that's ok
84-
pass
85-
else:
79+
if e.returncode not in (3, 128):
8680
raise
8781

8882
subprocess.check_call(
@@ -96,6 +90,7 @@ def get_git_merge_base(dirname: PathLike) -> str | None:
9690
],
9791
cwd=dirname,
9892
)
93+
9994
try:
10095
merge_base = subprocess.check_output(
10196
["git", "merge-base", "upstream/main", "HEAD"],
@@ -107,9 +102,10 @@ def get_git_merge_base(dirname: PathLike) -> str | None:
107102
return None
108103

109104
if merge_base == commit_hash:
105+
# Get the parent commit if the merge base is the same as the current commit
110106
return get_log("%H", dirname, "HEAD^")
111-
else:
112-
return merge_base
107+
108+
return merge_base
113109

114110

115111
def get_tags(dirname: PathLike) -> list[str]:

0 commit comments

Comments
 (0)