@matheusbsbs / matheus-bagliano / commits / 5d1a5c02e8
Fork 0

[PATCH] Better error reporting for "git status"

Instead of "git status" ignoring (and hiding) potential errors from the "git-update-index" call, make it exit if it fails, and show the error. In order to do this, use the "-q" flag (to ignore not-up-to-date files) and add a new "--unmerged" flag that allows unmerged entries in the index without any errors. This also avoids marking the index "changed" if an entry isn't actually modified, and makes sure that we exit with an understandable error message if the index is corrupt or unreadable. "read_cache()" no longer returns an error for the caller to check. Finally, make die() and usage() exit with recognizable error codes, if we ever want to check the failure reason in scripts. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Oct 1, 2005 at 13:24 UTC 5d1a5c02e8ac1c16688ea4a44512245f25a49f8a
4 files changed +24 -12
git-status.sh
+1 -1
@@ -37,7 +37,7 @@ refs/heads/master) ;;
37 *) echo "# On branch $branch" ;;
38 esac
39
40 -git-update-index --refresh >/dev/null 2>&1
40 +git-update-index -q --unmerged --refresh || exit
41
42 if GIT_DIR="$GIT_DIR" git-rev-parse --verify HEAD >/dev/null 2>&1
43 then
read-cache.c
+9 -5
@@ -464,11 +464,15 @@ int read_cache(void)
464
465 errno = EBUSY;
466 if (active_cache)
467 - return error("more than one cachefile");
467 + return active_nr;
468 +
469 errno = ENOENT;
470 fd = open(get_index_file(), O_RDONLY);
470 - if (fd < 0)
471 - return (errno == ENOENT) ? 0 : error("open failed");
471 + if (fd < 0) {
472 + if (errno == ENOENT)
473 + return 0;
474 + die("index file open failed (%s)", strerror(errno));
475 + }
476
477 size = 0; // avoid gcc warning
478 map = MAP_FAILED;
@@ -480,7 +484,7 @@ int read_cache(void)
484 }
485 close(fd);
486 if (map == MAP_FAILED)
483 - return error("mmap failed");
487 + die("index file mmap failed (%s)", strerror(errno));
488
489 hdr = map;
490 if (verify_hdr(hdr, size) < 0)
@@ -501,7 +505,7 @@ int read_cache(void)
505 unmap:
506 munmap(map, size);
507 errno = EINVAL;
504 - return error("verify header failed");
508 + die("index file corrupt");
509 }
510
511 #define WRITE_BUFFER_SIZE 8192
update-index.c
+12 -4
@@ -13,7 +13,7 @@
13 * like "git-update-index *" and suddenly having all the object
14 * files be revision controlled.
15 */
16 -static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0, quiet = 0, info_only = 0;
16 +static int allow_add = 0, allow_remove = 0, allow_replace = 0, allow_unmerged = 0, not_new = 0, quiet = 0, info_only = 0;
17 static int force_remove;
18
19 /* Three functions to allow overloaded pointer return; see linux/err.h */
@@ -135,7 +135,7 @@ static struct cache_entry *refresh_entry(struct cache_entry *ce)
135
136 changed = ce_match_stat(ce, &st);
137 if (!changed)
138 - return ce;
138 + return NULL;
139
140 if (ce_modified(ce, &st))
141 return ERR_PTR(-EINVAL);
@@ -156,16 +156,20 @@ static int refresh_cache(void)
156 struct cache_entry *ce, *new;
157 ce = active_cache[i];
158 if (ce_stage(ce)) {
159 - printf("%s: needs merge\n", ce->name);
160 - has_errors = 1;
159 while ((i < active_nr) &&
160 ! strcmp(active_cache[i]->name, ce->name))
161 i++;
162 i--;
163 + if (allow_unmerged)
164 + continue;
165 + printf("%s: needs merge\n", ce->name);
166 + has_errors = 1;
167 continue;
168 }
169
170 new = refresh_entry(ce);
171 + if (!new)
172 + continue;
173 if (IS_ERR(new)) {
174 if (not_new && PTR_ERR(new) == -ENOENT)
175 continue;
@@ -335,6 +339,10 @@ int main(int argc, const char **argv)
339 allow_remove = 1;
340 continue;
341 }
342 + if (!strcmp(path, "--unmerged")) {
343 + allow_unmerged = 1;
344 + continue;
345 + }
346 if (!strcmp(path, "--refresh")) {
347 has_errors |= refresh_cache();
348 continue;
usage.c
+2 -2
@@ -15,7 +15,7 @@ static void report(const char *prefix, const char *err, va_list params)
15 void usage(const char *err)
16 {
17 fprintf(stderr, "usage: %s\n", err);
18 - exit(1);
18 + exit(129);
19 }
20
21 void die(const char *err, ...)
@@ -25,7 +25,7 @@ void die(const char *err, ...)
25 va_start(params, err);
26 report("fatal: ", err, params);
27 va_end(params);
28 - exit(1);
28 + exit(128);
29 }
30
31 int error(const char *err, ...)