Skip to content

Commit c9cddab

Browse files
author
Linus Torvalds
committed
diff-cache: handle modified new files correctly
Junio pointed out that diff-cache didn't handle the case of a new file that was different from its index entry correctly. It needs to check the working copy the same way the modified file case did.
1 parent 91f192a commit c9cddab

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

diff-cache.c

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,68 @@ static int generate_patch = 0;
66
static int line_termination = '\n';
77

88
/* A file entry went away or appeared */
9-
static void show_file(const char *prefix, struct cache_entry *ce)
9+
static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
1010
{
1111
if (generate_patch)
12-
diff_addremove(prefix[0], ntohl(ce->ce_mode),
13-
ce->sha1, ce->name, NULL);
12+
diff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);
1413
else
15-
printf("%s%06o\tblob\t%s\t%s%c", prefix, ntohl(ce->ce_mode),
16-
sha1_to_hex(ce->sha1), ce->name,
17-
line_termination);
14+
printf("%s%06o\tblob\t%s\t%s%c", prefix, ntohl(mode),
15+
sha1_to_hex(sha1), ce->name, line_termination);
1816
}
1917

20-
static int show_modified(struct cache_entry *old, struct cache_entry *new)
18+
static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)
2119
{
22-
unsigned int mode = ntohl(new->ce_mode), oldmode;
23-
unsigned char *sha1 = new->sha1;
24-
unsigned char old_sha1_hex[60];
20+
unsigned char *sha1 = ce->sha1;
21+
unsigned int mode = ce->ce_mode;
2522

2623
if (!cached_only) {
2724
static unsigned char no_sha1[20];
2825
int changed;
2926
struct stat st;
30-
if (stat(new->name, &st) < 0) {
31-
show_file("-", old);
27+
if (stat(ce->name, &st) < 0)
3228
return -1;
33-
}
34-
changed = cache_match_stat(new, &st);
29+
changed = cache_match_stat(ce, &st);
3530
if (changed) {
36-
mode = st.st_mode;
31+
mode = create_ce_mode(st.st_mode);
3732
sha1 = no_sha1;
3833
}
3934
}
4035

41-
oldmode = ntohl(old->ce_mode);
36+
*sha1p = sha1;
37+
*modep = mode;
38+
return 0;
39+
}
40+
41+
static int show_new_file(struct cache_entry *new)
42+
{
43+
unsigned char *sha1;
44+
unsigned int mode;
45+
46+
/* New file in the index: it might actually be different in the working copy */
47+
if (get_stat_data(new, &sha1, &mode) < 0)
48+
return -1;
49+
50+
show_file("+", new, sha1, mode);
51+
}
52+
53+
static int show_modified(struct cache_entry *old, struct cache_entry *new)
54+
{
55+
unsigned int mode, oldmode;
56+
unsigned char *sha1;
57+
unsigned char old_sha1_hex[60];
58+
59+
if (get_stat_data(new, &sha1, &mode) < 0) {
60+
show_file("-", old, old->sha1, old->ce_mode);
61+
return -1;
62+
}
63+
64+
oldmode = old->ce_mode;
4265
if (mode == oldmode && !memcmp(sha1, old->sha1, 20))
4366
return 0;
4467

68+
mode = ntohl(mode);
69+
oldmode = ntohl(oldmode);
70+
4571
if (generate_patch)
4672
diff_change(oldmode, mode,
4773
old->sha1, sha1, old->name, NULL);
@@ -64,7 +90,7 @@ static int diff_cache(struct cache_entry **ac, int entries)
6490
case 0:
6591
/* No stage 1 entry? That means it's a new file */
6692
if (!same) {
67-
show_file("+", ce);
93+
show_new_file(ce);
6894
break;
6995
}
7096
/* Show difference between old and new */
@@ -73,7 +99,7 @@ static int diff_cache(struct cache_entry **ac, int entries)
7399
case 1:
74100
/* No stage 3 (merge) entry? That means it's been deleted */
75101
if (!same) {
76-
show_file("-", ce);
102+
show_file("-", ce, ce->sha1, ce->ce_mode);
77103
break;
78104
}
79105
/* Otherwise we fall through to the "unmerged" case */

0 commit comments

Comments
 (0)