git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Added --exit-code option to git-status
@ 2011-03-05 15:20 Piotr Krukowiecki
  2011-03-05 16:10 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Piotr Krukowiecki @ 2011-03-05 15:20 UTC (permalink / raw)
  To: git

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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Added --exit-code option to git-status
  2011-03-05 15:20 [PATCH] Added --exit-code option to git-status Piotr Krukowiecki
@ 2011-03-05 16:10 ` Junio C Hamano
  2011-03-05 16:32   ` Piotr Krukowiecki
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2011-03-05 16:10 UTC (permalink / raw)
  To: Piotr Krukowiecki; +Cc: git

Piotr Krukowiecki <piotr.krukowiecki@gmail.com> writes:

> 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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Added --exit-code option to git-status
  2011-03-05 16:10 ` Junio C Hamano
@ 2011-03-05 16:32   ` Piotr Krukowiecki
  0 siblings, 0 replies; 3+ messages in thread
From: Piotr Krukowiecki @ 2011-03-05 16:32 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Sat, Mar 5, 2011 at 5:10 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Piotr Krukowiecki <piotr.krukowiecki@gmail.com> writes:
>
>> 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?


-- 
Piotrek

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-03-05 16:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-03-05 15:20 [PATCH] Added --exit-code option to git-status Piotr Krukowiecki
2011-03-05 16:10 ` Junio C Hamano
2011-03-05 16:32   ` Piotr Krukowiecki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).