From aa674b1b772fbecaab6538e81446b36d49c38c69 Mon Sep 17 00:00:00 2001 From: Pawel Lipski Date: Sun, 15 Dec 2019 17:30:12 +0100 Subject: [PATCH] Fix corner cases of ASCII-only mode of status --- RELEASE_NOTES.md | 2 ++ git_machete/cmd.py | 11 ++++++++--- hook_samples/machete-status-branch | 8 ++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index ad5849603..fd33831b1 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -2,6 +2,8 @@ ## New in git-machete 2.12.4 +- fixed: remove stray ANSI escape characters in ASCII-only mode +- improved: `machete-status-branch` hook now receives `ASCII_ONLY` env var depending on whether `status` runs in ASCII-only mode - fixed: artifact upload to Github Releases - added: creation and upload of Debian packages to Github Releases diff --git a/git_machete/cmd.py b/git_machete/cmd.py index 6415eaa80..d876808c3 100644 --- a/git_machete/cmd.py +++ b/git_machete/cmd.py @@ -1485,7 +1485,8 @@ def print_line_prefix(b_, suffix): fp_suffix = (colored(" " + right_arrow() + " fork point ???", RED) + " commit " + short_sha(fp_sha(b)) + " found in reflog of " + fp_branches_formatted) else: fp_suffix = '' - print_line_prefix(b, vertical_bar() + " " + ENDC + dim(msg) + fp_suffix + "\n") + print_line_prefix(b, vertical_bar()) + write_unicode(" " + dim(msg) + fp_suffix + "\n") elbow_ascii_only = {DIM: "m-", RED: "x-", GREEN: "o-", YELLOW: "?-"} elbow = u"└─" if not ascii_only else elbow_ascii_only[edge_color[b]] print_line_prefix(b, elbow) @@ -1512,7 +1513,8 @@ def print_line_prefix(b_, suffix): hook_output = "" if hook_executable: debug("status()", "running machete-status-branch hook (%s) for branch %s" % (hook_path, b)) - status_code, stdout, stderr = popen_cmd(hook_path, b, cwd=get_root_dir()) + hook_env = dict(os.environ, ASCII_ONLY=str(ascii_only).lower()) + status_code, stdout, stderr = popen_cmd(hook_path, b, cwd=get_root_dir(), env=hook_env) if status_code == 0: if not stdout.isspace(): hook_output = " " + stdout.rstrip() @@ -1726,6 +1728,9 @@ def usage(c=None): The standard output of this hook is displayed at the end of the line, after branch name, (optionally) custom annotation and (optionally) remote sync-ness status. Standard error is ignored. If the hook returns a non-zero status, both stdout and stderr are ignored, and printing the status continues as usual. + Note: the hook is always invoked with 'ASCII_ONLY' variable passed into the environment. + If 'status' runs in ASCII-only mode (i.e. if '--color=auto' and stdout is not a terminal, or if '--color=never'), then 'ASCII_ONLY=true', otherwise 'ASCII_ONLY=false'. + Please see hook_samples/ directory for examples (also includes an example of using the standard git post-commit hook to 'git machete add' branches automatically). """, "list": """ @@ -1849,7 +1854,7 @@ def usage(c=None): Options: -l, --list-commits Additionally lists the messages of commits introduced on each branch. - --color=WHEN Colorize the output; WHEN can be 'always', 'auto', or 'never'. + --color=WHEN Colorize the output; WHEN can be 'always', 'auto' (default; i.e. only if stdout is a terminal), or 'never'. """, "traverse": """ Usage: git machete traverse [-l|--list-commits] diff --git a/hook_samples/machete-status-branch b/hook_samples/machete-status-branch index e81a7a550..2895bde30 100755 --- a/hook_samples/machete-status-branch +++ b/hook_samples/machete-status-branch @@ -21,8 +21,12 @@ submod_commit=$(git ls-tree -d "$branch" -- "$submod_rel_path" | grep -Po '(?<=1 # Check which submodule's local branches (and/or submodule's HEAD) point at submod_commit. refs=$(git --git-dir=.git/modules/"$submod_rel_path" log --no-walk --format=%D --decorate --decorate-refs-exclude=refs/remotes/ "$submod_commit") -dim='\033[2m' -endc='\033[0m' +function ansi_esc { + [[ $ASCII_ONLY = true ]] || echo -n "$1" +} +dim=$(ansi_esc '\033[2m') +endc=$(ansi_esc '\033[0m') + echo -ne "${dim}[$(basename "$submod_rel_path") at $endc" if [ -n "$refs" ] && [ "$refs" != HEAD ]; then echo -n "$refs"