Skip to content

Commit 5d1a5c0

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
[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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 455a7f3 commit 5d1a5c0

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

git-status.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ refs/heads/master) ;;
3737
*) echo "# On branch $branch" ;;
3838
esac
3939

40-
git-update-index --refresh >/dev/null 2>&1
40+
git-update-index -q --unmerged --refresh || exit
4141

4242
if GIT_DIR="$GIT_DIR" git-rev-parse --verify HEAD >/dev/null 2>&1
4343
then

read-cache.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,15 @@ int read_cache(void)
464464

465465
errno = EBUSY;
466466
if (active_cache)
467-
return error("more than one cachefile");
467+
return active_nr;
468+
468469
errno = ENOENT;
469470
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+
}
472476

473477
size = 0; // avoid gcc warning
474478
map = MAP_FAILED;
@@ -480,7 +484,7 @@ int read_cache(void)
480484
}
481485
close(fd);
482486
if (map == MAP_FAILED)
483-
return error("mmap failed");
487+
die("index file mmap failed (%s)", strerror(errno));
484488

485489
hdr = map;
486490
if (verify_hdr(hdr, size) < 0)
@@ -501,7 +505,7 @@ int read_cache(void)
501505
unmap:
502506
munmap(map, size);
503507
errno = EINVAL;
504-
return error("verify header failed");
508+
die("index file corrupt");
505509
}
506510

507511
#define WRITE_BUFFER_SIZE 8192

update-index.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* like "git-update-index *" and suddenly having all the object
1414
* files be revision controlled.
1515
*/
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;
1717
static int force_remove;
1818

1919
/* 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)
135135

136136
changed = ce_match_stat(ce, &st);
137137
if (!changed)
138-
return ce;
138+
return NULL;
139139

140140
if (ce_modified(ce, &st))
141141
return ERR_PTR(-EINVAL);
@@ -156,16 +156,20 @@ static int refresh_cache(void)
156156
struct cache_entry *ce, *new;
157157
ce = active_cache[i];
158158
if (ce_stage(ce)) {
159-
printf("%s: needs merge\n", ce->name);
160-
has_errors = 1;
161159
while ((i < active_nr) &&
162160
! strcmp(active_cache[i]->name, ce->name))
163161
i++;
164162
i--;
163+
if (allow_unmerged)
164+
continue;
165+
printf("%s: needs merge\n", ce->name);
166+
has_errors = 1;
165167
continue;
166168
}
167169

168170
new = refresh_entry(ce);
171+
if (!new)
172+
continue;
169173
if (IS_ERR(new)) {
170174
if (not_new && PTR_ERR(new) == -ENOENT)
171175
continue;
@@ -335,6 +339,10 @@ int main(int argc, const char **argv)
335339
allow_remove = 1;
336340
continue;
337341
}
342+
if (!strcmp(path, "--unmerged")) {
343+
allow_unmerged = 1;
344+
continue;
345+
}
338346
if (!strcmp(path, "--refresh")) {
339347
has_errors |= refresh_cache();
340348
continue;

usage.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static void report(const char *prefix, const char *err, va_list params)
1515
void usage(const char *err)
1616
{
1717
fprintf(stderr, "usage: %s\n", err);
18-
exit(1);
18+
exit(129);
1919
}
2020

2121
void die(const char *err, ...)
@@ -25,7 +25,7 @@ void die(const char *err, ...)
2525
va_start(params, err);
2626
report("fatal: ", err, params);
2727
va_end(params);
28-
exit(1);
28+
exit(128);
2929
}
3030

3131
int error(const char *err, ...)

0 commit comments

Comments
 (0)