@matheusbsbs / matheus-bagliano / commits / ec1fcc16af
Fork 0

Show original and resulting blob object info in diff output.

This adds more cruft to diff --git header to record the blob SHA1 and the mode the patch/diff is intended to be applied against, to help the receiving end fall back on a three-way merge. The new header looks like this: diff --git a/apply.c b/apply.c index 7be5041..8366082 100644 --- a/apply.c +++ b/apply.c @@ -14,6 +14,7 @@ // files that are being modified, but doesn't apply the patch // --stat does just a diffstat, and doesn't actually apply +// --show-index-info shows the old and new index info for... ... Upon receiving such a patch, if the patch did not apply cleanly to the target tree, the recipient can try to find the matching old objects in her object database and create a temporary tree, apply the patch to that temporary tree, and attempt a 3-way merge between the patched temporary tree and the target tree using the original temporary tree as the common ancestor. The patch lifts the code to compute the hash for an on-filesystem object from update-index.c and makes it available to the diff output routine. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 7, 2005 at 03:42 UTC ec1fcc16af94810dd822c6da533f58fa2750f14a
8 files changed +114 -55
cache.h
+1
@@ -165,6 +165,7 @@ extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
165 extern int ce_modified(struct cache_entry *ce, struct stat *st);
166 extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
167 extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type);
168 +extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
169 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
170
171 struct cache_file {
diff.c
+57 -20
@@ -596,15 +596,31 @@ static void run_external_diff(const char *pgm,
596 remove_tempfile();
597 }
598
599 +static void diff_fill_sha1_info(struct diff_filespec *one)
600 +{
601 + if (DIFF_FILE_VALID(one)) {
602 + if (!one->sha1_valid) {
603 + struct stat st;
604 + if (stat(one->path, &st) < 0)
605 + die("stat %s", one->path);
606 + if (index_path(one->sha1, one->path, &st, 0))
607 + die("cannot hash %s\n", one->path);
608 + }
609 + }
610 + else
611 + memset(one->sha1, 0, 20);
612 +}
613 +
614 static void run_diff(struct diff_filepair *p)
615 {
616 const char *pgm = external_diff();
602 - char msg_[PATH_MAX*2+200], *xfrm_msg;
617 + char msg[PATH_MAX*2+300], *xfrm_msg;
618 struct diff_filespec *one;
619 struct diff_filespec *two;
620 const char *name;
621 const char *other;
622 int complete_rewrite = 0;
623 + int len;
624
625 if (DIFF_PAIR_UNMERGED(p)) {
626 /* unmerged */
@@ -616,39 +632,60 @@ static void run_diff(struct diff_filepair *p)
632 name = p->one->path;
633 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
634 one = p->one; two = p->two;
635 +
636 + diff_fill_sha1_info(one);
637 + diff_fill_sha1_info(two);
638 +
639 + len = 0;
640 switch (p->status) {
641 case DIFF_STATUS_COPIED:
621 - sprintf(msg_,
622 - "similarity index %d%%\n"
623 - "copy from %s\n"
624 - "copy to %s",
625 - (int)(0.5 + p->score * 100.0/MAX_SCORE),
626 - name, other);
627 - xfrm_msg = msg_;
642 + len += snprintf(msg + len, sizeof(msg) - len,
643 + "similarity index %d%%\n"
644 + "copy from %s\n"
645 + "copy to %s\n",
646 + (int)(0.5 + p->score * 100.0/MAX_SCORE),
647 + name, other);
648 break;
649 case DIFF_STATUS_RENAMED:
630 - sprintf(msg_,
631 - "similarity index %d%%\n"
632 - "rename from %s\n"
633 - "rename to %s",
634 - (int)(0.5 + p->score * 100.0/MAX_SCORE),
635 - name, other);
636 - xfrm_msg = msg_;
650 + len += snprintf(msg + len, sizeof(msg) - len,
651 + "similarity index %d%%\n"
652 + "rename from %s\n"
653 + "rename to %s\n",
654 + (int)(0.5 + p->score * 100.0/MAX_SCORE),
655 + name, other);
656 break;
657 case DIFF_STATUS_MODIFIED:
658 if (p->score) {
640 - sprintf(msg_,
641 - "dissimilarity index %d%%",
642 - (int)(0.5 + p->score * 100.0/MAX_SCORE));
643 - xfrm_msg = msg_;
659 + len += snprintf(msg + len, sizeof(msg) - len,
660 + "dissimilarity index %d%%\n",
661 + (int)(0.5 + p->score *
662 + 100.0/MAX_SCORE));
663 complete_rewrite = 1;
664 break;
665 }
666 /* fallthru */
667 default:
649 - xfrm_msg = NULL;
668 + /* nothing */
669 + ;
670 }
671
672 + if (memcmp(one->sha1, two->sha1, 20)) {
673 + char one_sha1[41];
674 + memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
675 +
676 + len += snprintf(msg + len, sizeof(msg) - len,
677 + "index %.7s..%.7s", one_sha1,
678 + sha1_to_hex(two->sha1));
679 + if (one->mode == two->mode)
680 + len += snprintf(msg + len, sizeof(msg) - len,
681 + " %06o", one->mode);
682 + len += snprintf(msg + len, sizeof(msg) - len, "\n");
683 + }
684 +
685 + if (len)
686 + msg[--len] = 0;
687 + xfrm_msg = len ? msg : NULL;
688 +
689 if (!pgm &&
690 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
691 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
sha1_file.c
+39
@@ -1545,3 +1545,42 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con
1545 munmap(buf, size);
1546 return ret;
1547 }
1548 +
1549 +int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
1550 +{
1551 + int fd;
1552 + char *target;
1553 +
1554 + switch (st->st_mode & S_IFMT) {
1555 + case S_IFREG:
1556 + fd = open(path, O_RDONLY);
1557 + if (fd < 0)
1558 + return error("open(\"%s\"): %s", path,
1559 + strerror(errno));
1560 + if (index_fd(sha1, fd, st, write_object, NULL) < 0)
1561 + return error("%s: failed to insert into database",
1562 + path);
1563 + break;
1564 + case S_IFLNK:
1565 + target = xmalloc(st->st_size+1);
1566 + if (readlink(path, target, st->st_size+1) != st->st_size) {
1567 + char *errstr = strerror(errno);
1568 + free(target);
1569 + return error("readlink(\"%s\"): %s", path,
1570 + errstr);
1571 + }
1572 + if (!write_object) {
1573 + unsigned char hdr[50];
1574 + int hdrlen;
1575 + write_sha1_file_prepare(target, st->st_size, "blob",
1576 + sha1, hdr, &hdrlen);
1577 + } else if (write_sha1_file(target, st->st_size, "blob", sha1))
1578 + return error("%s: failed to insert into database",
1579 + path);
1580 + free(target);
1581 + break;
1582 + default:
1583 + return error("%s: unsupported file type", path);
1584 + }
1585 + return 0;
1586 +}
t/diff-lib.sh
+8 -2
@@ -29,7 +29,13 @@ compare_diff_raw_z () {
29 compare_diff_patch () {
30 # When heuristics are improved, the score numbers would change.
31 # Ignore them while comparing.
32 - sed -e '/^[dis]*imilarity index [0-9]*%$/d' <"$1" >.tmp-1
33 - sed -e '/^[dis]*imilarity index [0-9]*%$/d' <"$2" >.tmp-2
32 + sed -e '
33 + /^[dis]*imilarity index [0-9]*%$/d
34 + /^index [0-9a-f]*\.\.[0-9a-f]/d
35 + ' <"$1" >.tmp-1
36 + sed -e '
37 + /^[dis]*imilarity index [0-9]*%$/d
38 + /^index [0-9a-f]*\.\.[0-9a-f]/d
39 + ' <"$2" >.tmp-2
40 diff -u .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
41 }
t/t4000-diff-format.sh
+2 -1
@@ -7,6 +7,7 @@ test_description='Test built-in diff output engine.
7
8 '
9 . ./test-lib.sh
10 +. ../diff-lib.sh
11
12 echo >path0 'Line 1
13 Line 2
@@ -48,6 +49,6 @@ EOF
49
50 test_expect_success \
51 'validate git-diff-files -p output.' \
51 - 'cmp -s current expected'
52 + 'compare_diff_patch current expected'
53
54 test_done
t/t4001-diff-rename.sh
+2 -1
@@ -7,6 +7,7 @@ test_description='Test rename detection in diff engine.
7
8 '
9 . ./test-lib.sh
10 +. ../diff-lib.sh
11
12 echo >path0 'Line 1
13 Line 2
@@ -61,6 +62,6 @@ EOF
62
63 test_expect_success \
64 'validate the output.' \
64 - 'diff -I "similarity.*" >/dev/null current expected'
65 + 'compare_diff_patch current expected'
66
67 test_done
t/t4004-diff-rename-symlink.sh
+2 -1
@@ -10,6 +10,7 @@ copy of symbolic links, but should not produce rename/copy followed
10 by an edit for them.
11 '
12 . ./test-lib.sh
13 +. ../diff-lib.sh
14
15 test_expect_success \
16 'prepare reference tree' \
@@ -61,6 +62,6 @@ EOF
62
63 test_expect_success \
64 'validate diff output' \
64 - 'diff -u current expected'
65 + 'compare_diff_patch current expected'
66
67 test_done
update-index.c
+3 -30
@@ -37,8 +37,6 @@ static int add_file_to_cache(const char *path)
37 int size, namelen, option, status;
38 struct cache_entry *ce;
39 struct stat st;
40 - int fd;
41 - char *target;
40
41 status = lstat(path, &st);
42 if (status < 0 || S_ISDIR(st.st_mode)) {
@@ -77,34 +75,9 @@ static int add_file_to_cache(const char *path)
75 fill_stat_cache_info(ce, &st);
76 ce->ce_mode = create_ce_mode(st.st_mode);
77 ce->ce_flags = htons(namelen);
80 - switch (st.st_mode & S_IFMT) {
81 - case S_IFREG:
82 - fd = open(path, O_RDONLY);
83 - if (fd < 0)
84 - return error("open(\"%s\"): %s", path, strerror(errno));
85 - if (index_fd(ce->sha1, fd, &st, !info_only, NULL) < 0)
86 - return error("%s: failed to insert into database", path);
87 - break;
88 - case S_IFLNK:
89 - target = xmalloc(st.st_size+1);
90 - if (readlink(path, target, st.st_size+1) != st.st_size) {
91 - char *errstr = strerror(errno);
92 - free(target);
93 - return error("readlink(\"%s\"): %s", path,
94 - errstr);
95 - }
96 - if (info_only) {
97 - unsigned char hdr[50];
98 - int hdrlen;
99 - write_sha1_file_prepare(target, st.st_size, "blob",
100 - ce->sha1, hdr, &hdrlen);
101 - } else if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
102 - return error("%s: failed to insert into database", path);
103 - free(target);
104 - break;
105 - default:
106 - return error("%s: unsupported file type", path);
107 - }
78 +
79 + if (index_path(ce->sha1, path, &st, !info_only))
80 + return -1;
81 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
82 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
83 if (add_cache_entry(ce, option))