From 4d13407c1c6c7ae6ac944f6e02a607742eb26120 Mon Sep 17 00:00:00 2001 From: Jerry Zhang Date: Mon, 10 Oct 2022 11:25:46 -0700 Subject: [PATCH] shell: Fixes for windows Switch to using a windows compatible "which" function and properly catch the error if "man" is not found. Topic: win Reviewers: brian-k Fixes: 66 --- revup/git.py | 10 +++++++--- revup/revup.py | 13 ++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/revup/git.py b/revup/git.py index fb8b1bf..183d6fa 100644 --- a/revup/git.py +++ b/revup/git.py @@ -3,6 +3,7 @@ import logging import os import re +import shutil import tempfile from dataclasses import dataclass from typing import Any, Dict, List, Optional, Pattern, Tuple @@ -131,8 +132,11 @@ def is_commit_hash(commit_ish: GitCommitHash) -> bool: return re.match(RE_COMMIT_HASH, commit_ish) is not None -async def get_default_git(sh: shell.Shell) -> str: - return (await sh.sh("/usr/bin/which", "git"))[1].strip() +def get_default_git() -> str: + ret = shutil.which("git") + if not ret: + raise RevupUsageException("Could not find a 'git' binary on the current PATH.") + return ret async def make_git( @@ -146,7 +150,7 @@ async def make_git( editor: str = "", ) -> "Git": if not git_path: - git_path = await get_default_git(sh) + git_path = get_default_git() git_ctx = Git(sh, git_path, remote_name, main_branch, base_branch_globs, keep_temp) diff --git a/revup/revup.py b/revup/revup.py index 6e7ad75..442b4b1 100755 --- a/revup/revup.py +++ b/revup/revup.py @@ -8,6 +8,7 @@ import subprocess import sys from argparse import _StoreAction, _StoreFalseAction, _StoreTrueAction +from builtins import FileNotFoundError from contextlib import asynccontextmanager from typing import Any, AsyncGenerator, Tuple @@ -26,8 +27,12 @@ class HelpAction(argparse.Action): def __call__(self, parser: Any, namespace: Any, values: Any, option_string: Any = None) -> None: source_dir = os.path.dirname(os.path.abspath(__file__)) man_cmd = ("man", "-M", source_dir, parser.prog.split()[-1]) - if subprocess.call(man_cmd) != 0: - print("Couldn't format man page, is 'man' available?") + try: + if subprocess.call(man_cmd) != 0: + print("Error in showing man page") + print(parser.format_help()) + except FileNotFoundError: + print("'man' binary not found.") print(parser.format_help()) sys.exit(0) @@ -122,9 +127,7 @@ async def get_config() -> config.Config: # There's a chicken/egg problem in getting git path from config when we need git # to find the path of the config file. Just this once, we use the default. sh = shell.Shell() - repo_root = (await sh.sh(await git.get_default_git(sh), "rev-parse", "--show-toplevel"))[ - 1 - ].rstrip() + repo_root = (await sh.sh(git.get_default_git(), "rev-parse", "--show-toplevel"))[1].rstrip() conf = config.Config(config_path, os.path.join(repo_root, config_file_name)) conf.read() return conf