Added --exit-code option to git-status
PATCH 1/1 Added --exit-code option to git-status
When this option is specified git-status returns non-zero exit code when there are untracked, modifed or staged files. The exit code is a combination of following:
2 - untracked files
4 - modified files
8 - staged files
---
Recently there was a question on irc how to check if there are any changes in working tree reliably, to be used in scripts. So I've added the --exit-code to git-status.
Some items I'm not sure of:
- does die() return 1 everywhere? I've skipped this value in codes
- are there more possible states of working tree?
I also have a test written, what's left is documentation.
Comments?
builtin/commit.c | 8 +++++++- wt-status.c | 23 +++++++++++++++++++++++ wt-status.h | 11 +++++++++++ 3 files changed, 41 insertions(+), 1 deletions(-)
builtin/commit.c +7 -1
@@ -100,6 +100,7 @@ static enum { STATUS_FORMAT_PORCELAIN } status_format =STATUS_FORMAT_LONG ;static int status_show_branch; +static int status_use_exit_code;static int opt_parse_m (const struct option *opt,const char *arg,int unset) {
@@ -1085,6 +1086,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)struct wt_status s;int fd;unsigned char sha1[20 ]; +int exit_code =0 ;static struct option builtin_status_options[] = {OPT__VERBOSE (&verbose,"be verbose" ), OPT_SET_INT('s', "short", &status_format,
@@ -1105,6 +1107,8 @@ int cmd_status(int argc, const char **argv, const char *prefix) {OPTION_STRING ,0 ,"ignore-submodules" , &ignore_submodule_arg,"when" ,"ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)" , PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, +OPT_BOOLEAN (0 ,"exit-code" , &status_use_exit_code, +"use exit code to specify status" ), OPT_END(), };
@@ -1164,7 +1168,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)wt_status_print (&s);break ; } -return 0 ; +if (status_use_exit_code) + exit_code =wt_status_exit_code (&s); +return exit_code; }static void print_summary (const char *prefix,const unsigned char *sha1)
wt-status.c +23 -0
@@ -882,3 +882,26 @@ void wt_porcelain_print(struct wt_status *s, int null_termination) s->prefix =NULL ;wt_shortstatus_print (s, null_termination,0 ); } + +int wt_status_exit_code (struct wt_status *s) +{ +int ec =0 ; +int dirty_submodules =0 ; + +if (!s) +return ec; + +if (s->untracked.nr) + ec +=STATUS_EC_UNTRACKED ; + +if (wt_status_check_worktree_changes (s, &dirty_submodules)) + ec +=STATUS_EC_MODIFIED ; + +if (s->commitable) + ec +=STATUS_EC_STAGED ; + +/* TODO: what about dirty_submodules ? */ + +return ec; +} +
wt-status.h +11 -0
@@ -68,4 +68,15 @@ void wt_status_collect(struct wt_status *s);void wt_shortstatus_print (struct wt_status *s,int null_termination,int show_branch);void wt_porcelain_print (struct wt_status *s,int null_termination); +/* Status of the working tree/index. Can be combined, for example 6 means +* there are some untracked and some modified files. +* 1 not used explicitly - used by die() ? */ +#define STATUS_EC_NO_CHANGES 0 +#define STATUS_EC_UNTRACKED 2/* There are untracked files */ +#define STATUS_EC_MODIFIED 4/* There are modified files (not staged) */ +#define STATUS_EC_STAGED 8/* There are staged files (added) */ + +/* Returns status exit code - see description of STATUS_EC_* defines */ +int wt_status_exit_code (struct wt_status *s); +#endif /* STATUS_H */
Signature · 1 line
1.7.4.1.179.gb9a20
Quoted 4 lines · Piotr Krukowiecki
When this option is specified git-status returns non-zero exit code when there are untracked, modifed or staged files.
...
Comments?
Knowing the history of "commit --dry-run" vs "status", my gut reaction was that adding anything to "status" is going totally backwards.
Quoted 9 lines · Junio C Hamano
Quoted 4 lines · Piotr Krukowiecki
When this option is specified git-status returns non-zero exit code when there are untracked, modifed or staged files.
...
Comments?Knowing the history of "commit --dry-run" vs "status", my gut reaction was that adding anything to "status" is going totally backwards.
What's the history?
Signature · 1 line
Piotrek