{"thread":{"id":"26667","subject":"[PATCH] Added --exit-code option to git-status","startedAt":"2011-03-05T15:20:43Z","lastAt":"2011-03-05T16:32:18Z","messageCount":3,"participants":["Piotr Krukowiecki","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"162827","messageId":"4D7254CB.5040704@gmail.com","threadId":"26667","inReplyTo":null,"subject":"[PATCH] Added --exit-code option to git-status","fromName":"Piotr Krukowiecki","fromEmail":"piotr.krukowiecki@gmail.com","sentAt":"2011-03-05T15:20:43Z","receivedAt":"2011-03-05T15:20:43Z","isPatch":true,"sender":{"key":"piotr.krukowiecki@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3259959?v=4"},"body":"When this option is specified git-status returns non-zero\nexit code when there are untracked, modifed or staged files.\nThe exit code is a combination of following:\n2 - untracked files\n4 - modified files\n8 - staged files\n---\n\nRecently there was a question on irc how to check if there are\nany changes in working tree reliably, to be used in scripts.\nSo I've added the --exit-code to git-status.\n\nSome items I'm not sure of:\n- does die() return 1 everywhere? I've skipped this value in codes\n- are there more possible states of working tree?\n\nI also have a test written, what's left is documentation.\n\nComments?\n\n builtin/commit.c |    8 +++++++-\n wt-status.c      |   23 +++++++++++++++++++++++\n wt-status.h      |   11 +++++++++++\n 3 files changed, 41 insertions(+), 1 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 355b2cb..283e32a 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -100,6 +100,7 @@ static enum {\n \tSTATUS_FORMAT_PORCELAIN\n } status_format = STATUS_FORMAT_LONG;\n static int status_show_branch;\n+static int status_use_exit_code;\n \n static int opt_parse_m(const struct option *opt, const char *arg, int unset)\n {\n@@ -1085,6 +1086,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \tstruct wt_status s;\n \tint fd;\n \tunsigned char sha1[20];\n+\tint exit_code = 0;\n \tstatic struct option builtin_status_options[] = {\n \t\tOPT__VERBOSE(&verbose, \"be verbose\"),\n \t\tOPT_SET_INT('s', \"short\", &status_format,\n@@ -1105,6 +1107,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \t\t{ OPTION_STRING, 0, \"ignore-submodules\", &ignore_submodule_arg, \"when\",\n \t\t  \"ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)\",\n \t\t  PARSE_OPT_OPTARG, NULL, (intptr_t)\"all\" },\n+\t\tOPT_BOOLEAN(0, \"exit-code\", &status_use_exit_code,\n+\t\t\t    \"use exit code to specify status\"),\n \t\tOPT_END(),\n \t};\n \n@@ -1164,7 +1168,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \t\twt_status_print(&s);\n \t\tbreak;\n \t}\n-\treturn 0;\n+\tif (status_use_exit_code)\n+\t\texit_code = wt_status_exit_code(&s);\n+\treturn exit_code;\n }\n \n static void print_summary(const char *prefix, const unsigned char *sha1)\ndiff --git a/wt-status.c b/wt-status.c\nindex a82b11d..b55f997 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -882,3 +882,26 @@ void wt_porcelain_print(struct wt_status *s, int null_termination)\n \ts->prefix = NULL;\n \twt_shortstatus_print(s, null_termination, 0);\n }\n+\n+int wt_status_exit_code(struct wt_status *s)\n+{\n+\tint ec = 0;\n+\tint dirty_submodules = 0;\n+\n+\tif (!s)\n+\t\treturn ec;\n+\n+\tif (s->untracked.nr)\n+\t\tec += STATUS_EC_UNTRACKED;\n+\n+\tif (wt_status_check_worktree_changes(s, &dirty_submodules))\n+\t\tec += STATUS_EC_MODIFIED;\n+\n+\tif (s->commitable)\n+\t\tec += STATUS_EC_STAGED;\n+\n+\t/* TODO: what about dirty_submodules ? */\n+\n+\treturn ec;\n+}\n+\ndiff --git a/wt-status.h b/wt-status.h\nindex 7d16c51..845c5b1 100644\n--- a/wt-status.h\n+++ b/wt-status.h\n@@ -68,4 +68,15 @@ void wt_status_collect(struct wt_status *s);\n void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch);\n void wt_porcelain_print(struct wt_status *s, int null_termination);\n \n+/* Status of the working tree/index. Can be combined, for example 6 means\n+ * there are some untracked and some modified files.\n+ * 1 not used explicitly - used by die() ? */\n+#define STATUS_EC_NO_CHANGES 0\n+#define STATUS_EC_UNTRACKED  2 /* There are untracked files */ \n+#define STATUS_EC_MODIFIED   4 /* There are modified files (not staged) */\n+#define STATUS_EC_STAGED     8 /* There are staged files (added) */\n+\n+/* Returns status exit code - see description of STATUS_EC_* defines */\n+int wt_status_exit_code(struct wt_status *s);\n+\n #endif /* STATUS_H */\n-- \n1.7.4.1.179.gb9a20\n"},{"id":"162828","messageId":"7v4o7h5z1i.fsf@alter.siamese.dyndns.org","threadId":"26667","inReplyTo":"4D7254CB.5040704@gmail.com","subject":"Re: [PATCH] Added --exit-code option to git-status","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-03-05T16:10:01Z","receivedAt":"2011-03-05T16:10:01Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Piotr Krukowiecki <piotr.krukowiecki@gmail.com> writes:\n\n> When this option is specified git-status returns non-zero\n> exit code when there are untracked, modifed or staged files.\n> ...\n> Comments?\n\nKnowing the history of \"commit --dry-run\" vs \"status\", my gut reaction was\nthat adding anything to \"status\" is going totally backwards.\n"},{"id":"162829","messageId":"AANLkTimeE0yscLFTkJ-824wi55eQQme8X+qobx80NVby@mail.gmail.com","threadId":"26667","inReplyTo":"7v4o7h5z1i.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH] Added --exit-code option to git-status","fromName":"Piotr Krukowiecki","fromEmail":"piotr.krukowiecki@gmail.com","sentAt":"2011-03-05T16:32:18Z","receivedAt":"2011-03-05T16:32:18Z","isPatch":true,"sender":{"key":"piotr.krukowiecki@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3259959?v=4"},"body":"On Sat, Mar 5, 2011 at 5:10 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Piotr Krukowiecki <piotr.krukowiecki@gmail.com> writes:\n>\n>> When this option is specified git-status returns non-zero\n>> exit code when there are untracked, modifed or staged files.\n>> ...\n>> Comments?\n>\n> Knowing the history of \"commit --dry-run\" vs \"status\", my gut reaction was\n> that adding anything to \"status\" is going totally backwards.\n\nWhat's the history?\n\n\n-- \nPiotrek\n"}]}