Skip to content

Commit 3baca68

Browse files
committed
Verify gh min version on start
Pull Request: #22 (main)
1 parent 6bcdadc commit 3baca68

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

git-grok

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ INTERNAL_SKIP_UPDATE_PRS_VAR = f"{INTERNAL_ENV_VAR_PREFIX}_skip_update_prs"
4242
DEBUG_FILE = "/tmp/git-grok.log"
4343
DEBUG_COLOR_GRAY_1 = (128, 128, 128)
4444
DEBUG_COLOR_GRAY_2 = (92, 92, 92)
45+
GH_MIN_VERSION = "2.29.0"
4546

4647
try:
4748
TERMINAL_SIZE = os.get_terminal_size()
@@ -158,9 +159,10 @@ class Main:
158159
self.debug_force_push_branches = args.debug_force_push_branches
159160

160161
if not in_rebase_interactive:
162+
self.gh_verify_version()
163+
self.git_verify_version()
161164
self.self_update()
162165
# For debug purposes only.
163-
self.shell_no_throw(["git", "--version"])
164166
self.shell_no_throw(["git", "status"])
165167
self.shell_no_throw(["git", "log", "--graph", "--decorate", "-3"])
166168

@@ -276,15 +278,9 @@ class Main:
276278
return
277279

278280
task_upsert_labels = Task(self.gh_upsert_labels)
279-
tasks_gh_get_pr = (
280-
dict(
281-
[
282-
(url, Task(self.gh_get_pr, url=url))
283-
for url in (c.url for c in reversed(commits) if c.url)
284-
]
285-
)
286-
if not self.debug_force_push_branches
287-
else {}
281+
tasks_gh_get_pr = dict(
282+
(url, Task(self.gh_get_pr, url=url))
283+
for url in (c.url for c in reversed(commits) if c.url)
288284
)
289285
task_upsert_labels.wait()
290286
prs_by_url = dict((url, task.wait()) for (url, task) in tasks_gh_get_pr.items())
@@ -515,12 +511,30 @@ class Main:
515511
pr_number_current=pr_number_current,
516512
)
517513

514+
#
515+
# Verifies that the gh supports e.g. autoMergeRequest field.
516+
#
517+
def gh_verify_version(self):
518+
out = self.shell(["gh", "--version"])
519+
if not (m := re.search(r"(version\s+|v)(\d+(\.\d+)+)", out)) or parse_version(
520+
m.group(2)
521+
) < parse_version(GH_MIN_VERSION):
522+
raise UserException(
523+
f"The tool requires gh version {GH_MIN_VERSION} or higher; please upgrade. Got:\n{out.strip()}"
524+
)
525+
518526
#
519527
# Returns current GitHub login.
520528
#
521529
def gh_get_current_login(self) -> str:
522530
return self.shell(["gh", "api", "user", "--jq", ".login"])
523531

532+
#
533+
# Verifies Git version.
534+
#
535+
def git_verify_version(self):
536+
self.shell_no_throw(["git", "--version"])
537+
524538
#
525539
# Returns current git folder remote (most often "origin").
526540
#
@@ -1366,6 +1380,13 @@ def hashify(text: str) -> str:
13661380
)
13671381

13681382

1383+
#
1384+
# Parses a version string into a tuple of integers.
1385+
#
1386+
def parse_version(v: str) -> tuple[int, ...]:
1387+
return tuple(map(int, (v.split("."))))
1388+
1389+
13691390
#
13701391
# Runs a function in a separate thread. To wait for its termination and get its
13711392
# return value, call wait() method. If the function raises an exception, it will

0 commit comments

Comments
 (0)