From: Piotr Krukowiecki <piotr.krukowiecki@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH] Added --exit-code option to git-status
Date: Sat, 05 Mar 2011 16:20:43 +0100 [thread overview]
Message-ID: <4D7254CB.5040704@gmail.com> (raw)
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(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 355b2cb..283e32a 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -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)
diff --git a/wt-status.c b/wt-status.c
index a82b11d..b55f997 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -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;
+}
+
diff --git a/wt-status.h b/wt-status.h
index 7d16c51..845c5b1 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -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 */
--
1.7.4.1.179.gb9a20
next reply other threads:[~2011-03-05 15:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-05 15:20 Piotr Krukowiecki [this message]
2011-03-05 16:10 ` [PATCH] Added --exit-code option to git-status Junio C Hamano
2011-03-05 16:32 ` Piotr Krukowiecki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4D7254CB.5040704@gmail.com \
--to=piotr.krukowiecki@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.