[PATCH] Added --exit-code option to git-status

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]