diff --git a/diff-cache.c b/diff-cache.c index ef522cd233..7e87d28f3a 100644 --- a/diff-cache.c +++ b/diff-cache.c @@ -57,14 +57,17 @@ static void show_new_file(struct cache_entry *new) show_file("+", new, sha1, mode); } -static int show_modified(struct cache_entry *old, struct cache_entry *new) +static int show_modified(struct cache_entry *old, + struct cache_entry *new, + int report_missing) { unsigned int mode, oldmode; unsigned char *sha1; unsigned char old_sha1_hex[60]; if (get_stat_data(new, &sha1, &mode) < 0) { - show_file("-", old, old->sha1, old->ce_mode); + if (report_missing) + show_file("-", old, old->sha1, old->ce_mode); return -1; } @@ -101,7 +104,7 @@ static int diff_cache(struct cache_entry **ac, int entries) break; } /* Show difference between old and new */ - show_modified(ac[1], ce); + show_modified(ac[1], ce, 1); break; case 1: /* No stage 3 (merge) entry? That means it's been deleted */ @@ -109,7 +112,19 @@ static int diff_cache(struct cache_entry **ac, int entries) show_file("-", ce, ce->sha1, ce->ce_mode); break; } - /* Otherwise we fall through to the "unmerged" case */ + /* We come here with ce pointing at stage 1 + * (original tree) and ac[1] pointing at stage + * 3 (unmerged). show-modified with + * report-mising set to false does not say the + * file is deleted but reports true if work + * tree does not have it, in which case we + * fall through to report the unmerged state. + * Otherwise, we show the differences between + * the original tree and the work tree. + */ + if (!cached_only && !show_modified(ce, ac[1], 0)) + break; + /* fallthru */ case 3: if (generate_patch) diff_unmerge(ce->name); diff --git a/diff.c b/diff.c index 8dfa624432..95488cdd9f 100644 --- a/diff.c +++ b/diff.c @@ -125,9 +125,16 @@ static void builtin_diff(const char *name, printf("Created: %s (mode:%s)\n", name, temp[1].mode); else if (!path1[1][0]) printf("Deleted: %s\n", name); - else if (strcmp(temp[0].mode, temp[1].mode)) + else if (strcmp(temp[0].mode, temp[1].mode)) { printf("Mode changed: %s (%s->%s)\n", name, temp[0].mode, temp[1].mode); + /* Be careful. We do not want to diff between + * symlink and a file. + */ + if (strncmp(temp[0].mode, "120", 3) != + strncmp(temp[1].mode, "120", 3)) + exit(0); + } fflush(NULL); execlp("/bin/sh","sh", "-c", cmd, NULL); } @@ -163,13 +170,35 @@ static int work_tree_matches(const char *name, const unsigned char *sha1) if (pos < 0) return 0; ce = active_cache[pos]; - if ((stat(name, &st) < 0) || + if ((lstat(name, &st) < 0) || + !S_ISREG(st.st_mode) || cache_match_stat(ce, &st) || memcmp(sha1, ce->sha1, 20)) return 0; return 1; } +static void prep_temp_blob(struct diff_tempfile *temp, + void *blob, + unsigned long size, + unsigned char *sha1, + int mode) +{ + int fd; + + strcpy(temp->tmp_path, ".diff_XXXXXX"); + fd = mkstemp(temp->tmp_path); + if (fd < 0) + die("unable to create temp-file"); + if (write(fd, blob, size) != size) + die("unable to write temp-file"); + close(fd); + temp->name = temp->tmp_path; + strcpy(temp->hex, sha1_to_hex(sha1)); + temp->hex[40] = 0; + sprintf(temp->mode, "%06o", mode); +} + static void prepare_temp_file(const char *name, struct diff_tempfile *temp, struct diff_spec *one) @@ -196,20 +225,36 @@ static void prepare_temp_file(const char *name, if (!one->sha1_valid || use_work_tree) { struct stat st; temp->name = name; - if (stat(temp->name, &st) < 0) { + if (lstat(temp->name, &st) < 0) { if (errno == ENOENT) goto not_a_valid_file; die("stat(%s): %s", temp->name, strerror(errno)); } - if (!one->sha1_valid) - strcpy(temp->hex, sha1_to_hex(null_sha1)); - else - strcpy(temp->hex, sha1_to_hex(one->blob_sha1)); - sprintf(temp->mode, "%06o", - S_IFREG |ce_permissions(st.st_mode)); + if (S_ISLNK(st.st_mode)) { + int ret; + char *buf, buf_[1024]; + buf = ((sizeof(buf_) < st.st_size) ? + xmalloc(st.st_size) : buf_); + ret = readlink(name, buf, st.st_size); + if (ret < 0) + die("readlink(%s)", name); + prep_temp_blob(temp, buf, st.st_size, + (one->sha1_valid ? + one->blob_sha1 : null_sha1), + (one->sha1_valid ? + one->mode : S_IFLNK)); + } + else { + if (!one->sha1_valid) + strcpy(temp->hex, sha1_to_hex(null_sha1)); + else + strcpy(temp->hex, sha1_to_hex(one->blob_sha1)); + sprintf(temp->mode, "%06o", + S_IFREG |ce_permissions(st.st_mode)); + } + return; } else { - int fd; void *blob; char type[20]; unsigned long size; @@ -218,19 +263,8 @@ static void prepare_temp_file(const char *name, if (!blob || strcmp(type, "blob")) die("unable to read blob object for %s (%s)", name, sha1_to_hex(one->blob_sha1)); - - strcpy(temp->tmp_path, ".diff_XXXXXX"); - fd = mkstemp(temp->tmp_path); - if (fd < 0) - die("unable to create temp-file"); - if (write(fd, blob, size) != size) - die("unable to write temp-file"); - close(fd); + prep_temp_blob(temp, blob, size, one->blob_sha1, one->mode); free(blob); - temp->name = temp->tmp_path; - strcpy(temp->hex, sha1_to_hex(one->blob_sha1)); - temp->hex[40] = 0; - sprintf(temp->mode, "%06o", one->mode); } } diff --git a/git-prune-script b/git-prune-script old mode 100644 new mode 100755 diff --git a/update-cache.c b/update-cache.c index 05d584ed48..893ba8679d 100644 --- a/update-cache.c +++ b/update-cache.c @@ -15,6 +15,12 @@ */ static int allow_add = 0, allow_remove = 0, not_new = 0; +/* + * update-cache --refresh may not touch anything at all, in which case + * writing 1.6MB of the same thing is a waste. + */ +static int cache_changed = 0; + /* Three functions to allow overloaded pointer return; see linux/err.h */ static inline void *ERR_PTR(long error) { @@ -51,7 +57,7 @@ static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st) ce->ce_size = htonl(st->st_size); } -static int add_file_to_cache(char *path) +static int add_file_to_cache_1(char *path) { int size, namelen; struct cache_entry *ce; @@ -93,9 +99,35 @@ static int add_file_to_cache(char *path) default: return -1; } + if (!cache_changed) { + /* If we have not smudged the cache, be careful + * to keep it clean. Find out if we have a matching + * cache entry that add_cache_entry would replace with, + * and if it matches then do not bother calling it. + */ + int pos = cache_name_pos(ce->name, namelen); + if ((0 <= pos) && + !memcmp(active_cache[pos], ce, sizeof(*ce))) { + free(ce); + /* magic to tell add_file_to_cache that + * we have not updated anything. + */ + return 999; + } + } return add_cache_entry(ce, allow_add); } +static int add_file_to_cache(char *path) +{ + int ret = add_file_to_cache_1(path); + if (ret == 0) + cache_changed = 1; + else if (ret == 999) + ret = 0; + return ret; +} + static int match_data(int fd, void *buffer, unsigned long size) { while (size) { @@ -165,6 +197,7 @@ static struct cache_entry *refresh_entry(struct cache_entry *ce) if (compare_data(ce, st.st_size)) return ERR_PTR(-EINVAL); + cache_changed = 1; size = ce_size(ce); updated = xmalloc(size); memcpy(updated, ce, size); @@ -245,6 +278,7 @@ static int add_cacheinfo(char *arg1, char *arg2, char *arg3) if (!verify_path(arg3)) return -1; + cache_changed = 1; len = strlen(arg3); size = cache_entry_size(len); ce = xmalloc(size); @@ -339,9 +373,13 @@ int main(int argc, char **argv) if (add_file_to_cache(path)) die("Unable to add %s to database", path); } - if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile)) + + if (!cache_changed) + unlink(lockfile); + else if (write_cache(newfd, active_cache, active_nr) || + rename(lockfile, indexfile)) die("Unable to write new cachefile"); lockfile_name = NULL; - return has_errors; + return has_errors ? 1 : 0; }