{"thread":{"id":"55100","subject":"[PATCH] status: learn --color for piping colored output","startedAt":"2021-02-03T21:41:23Z","lastAt":"2021-02-03T21:41:23Z","messageCount":1,"participants":["Lance Ward via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"416097","messageId":"pull.955.git.git.1612388438497.gitgitgadget@gmail.com","threadId":"55100","inReplyTo":null,"subject":"[PATCH] status: learn --color for piping colored output","fromName":"Lance Ward via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-02-03T21:40:38Z","receivedAt":"2021-02-03T21:41:23Z","isPatch":true,"sender":{"key":"ljward10@gmail.com","avatar":null},"body":"From: Lance Ward <ljward10@gmail.com>\n\nMany users like to pipe colored results of git status to other commands\nsuch as more or less, but by default colors are lost when piping without\nchanging the user's git configuration.  Many other commands such as diff,\nshow, log and grep have a --color option to easily override this behavior.\nThis allows the status command to have a similar --color option providing\na simpler mechanism for temporarily forcing piped colored output.\n\nSigned-off-by: Lance Ward <ljward10@gmail.com>\n---\n    status: learn --color command line option\n    \n    Many users like to pipe colored results of git status to other commands\n    such as more or less, but by default colors are lost when piping without\n    changing the user's git configuration. Many other commands such as diff,\n    show, log and grep have a --color option to easily override this\n    behavior. This allows the status command to have a similar --color\n    option providing a simpler mechanism for temporarily forcing piped\n    colored output.\n    \n    Signed-off-by: Lance Ward ljward10@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-955%2Fljward10%2Flw-add-status-color-option-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-955/ljward10/lw-add-status-color-option-v1\nPull-Request: https://github.com/git/git/pull/955\n\n Documentation/git-status.txt   |  9 ++++++++\n builtin/commit.c               |  6 +++++\n t/t7528-status-color-option.sh | 40 ++++++++++++++++++++++++++++++++++\n 3 files changed, 55 insertions(+)\n create mode 100755 t/t7528-status-color-option.sh\n\ndiff --git a/Documentation/git-status.txt b/Documentation/git-status.txt\nindex c0764e850a44..d5655d051841 100644\n--- a/Documentation/git-status.txt\n+++ b/Documentation/git-status.txt\n@@ -32,6 +32,15 @@ OPTIONS\n --branch::\n \tShow the branch and tracking info even in short-format.\n \n+--color[=<when>]::\n+\tColor status output, overriding configuration file setting.\n+\tThe value must be always (the default), never, or auto.\n+\n+--no-color::\n+\tTurn off color ouput, even when the configuration file gives\n+\tthe default to color output.\n+\tSame as `--color=never`.\n+\n --show-stash::\n \tShow the number of entries currently stashed away.\n \ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 739110c5a7f6..7f0a9d07a0ee 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1355,6 +1355,7 @@ static int git_status_config(const char *k, const char *v, void *cb)\n int cmd_status(int argc, const char **argv, const char *prefix)\n {\n \tstatic int no_renames = -1;\n+\tstatic int use_color = GIT_COLOR_AUTO;\n \tstatic const char *rename_score_arg = (const char *)-1;\n \tstatic struct wt_status s;\n \tunsigned int progress_flag = 0;\n@@ -1378,6 +1379,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \t\t\t    STATUS_FORMAT_LONG),\n \t\tOPT_BOOL('z', \"null\", &s.null_termination,\n \t\t\t N_(\"terminate entries with NUL\")),\n+\t\tOPT__COLOR(&use_color, N_(\"use colored output\")),\n \t\t{ OPTION_STRING, 'u', \"untracked-files\", &untracked_files_arg,\n \t\t  N_(\"mode\"),\n \t\t  N_(\"show untracked files, optional modes: all, normal, no. (Default: all)\"),\n@@ -1410,6 +1412,10 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \thandle_untracked_files_arg(&s);\n \thandle_ignored_arg(&s);\n \n+\tif (use_color != GIT_COLOR_AUTO) {\n+\t\ts.use_color=use_color;\n+\t}\n+\n \tif (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&\n \t    s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)\n \t\tdie(_(\"Unsupported combination of ignored and untracked-files arguments\"));\ndiff --git a/t/t7528-status-color-option.sh b/t/t7528-status-color-option.sh\nnew file mode 100755\nindex 000000000000..d14ea03f92c3\n--- /dev/null\n+++ b/t/t7528-status-color-option.sh\n@@ -0,0 +1,40 @@\n+#!/bin/sh\n+\n+test_description='git status color option'\n+\n+. ./test-lib.sh\n+\n+test_expect_success setup '\n+\techo 1 >original &&\n+\tgit add .\n+'\n+\n+# Normal git status does not pipe colors\n+test_expect_success 'git status' '\n+\tgit status >raw &&\n+\ttest_decode_color <raw >out &&\n+\tgrep \"original$\" out\n+'\n+\n+# Test new color option with never (expect same as above)\n+test_expect_success 'git status --color=never' '\n+\tgit status --color=never >raw &&\n+\ttest_decode_color <raw >out &&\n+\tgrep \"original$\" out\n+'\n+\n+# Test new color (default is always)\n+test_expect_success 'git status --color' '\n+\tgit status --color >raw &&\n+\ttest_decode_color <raw >out &&\n+\tgrep \"original<RESET>$\" out\n+'\n+\n+# Test new color option with always\n+test_expect_success 'git status --color=always' '\n+\tgit status --color=always >raw &&\n+\ttest_decode_color <raw >out &&\n+\tgrep \"original<RESET>$\" out\n+'\n+\n+test_done\n\nbase-commit: e6362826a0409539642a5738db61827e5978e2e4\n-- \ngitgitgadget\n"}]}