add support for symlinks in the repository to 'got diff'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
diff --git a/lib/diff.c b/lib/diff.c
index 152352c..7e5ee06 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -114,16 +114,25 @@ diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
if (outfile) {
char *modestr1 = NULL, *modestr2 = NULL;
+ int modebits;
if (mode1 && mode1 != mode2) {
+ if (S_ISLNK(mode1))
+ modebits = S_IFLNK;
+ else
+ modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
if (asprintf(&modestr1, " (mode %o)",
- mode1 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
+ mode1 & modebits) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
}
if (mode2 && mode1 != mode2) {
+ if (S_ISLNK(mode2))
+ modebits = S_IFLNK;
+ else
+ modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
if (asprintf(&modestr2, " (mode %o)",
- mode2 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
+ mode2 & modebits) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
@@ -550,9 +559,11 @@ diff_entry_old_new(struct got_tree_entry *te1,
if (!id_match)
return diff_modified_tree(&te1->id, &te2->id,
label1, label2, repo, cb, cb_arg, diff_content);
- } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
+ } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
+ (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
if (!id_match ||
- (te1->mode & S_IXUSR) != (te2->mode & S_IXUSR)) {
+ ((te1->mode & (S_IFLNK | S_IXUSR))) !=
+ (te2->mode & (S_IFLNK | S_IXUSR))) {
if (diff_content)
return diff_modified_blob(&te1->id, &te2->id,
label1, label2, te1->mode, te2->mode,
diff --git a/lib/object.c b/lib/object.c
index cfc0d4b..eaff057 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1701,6 +1701,14 @@ normalize_mode_for_comparison(mode_t mode)
if (S_ISDIR(mode))
return mode & S_IFDIR;
+ /*
+ * For symlinks, the only relevant bit is the IFLNK bit.
+ * This allows us to detect paths changing from a symlinks
+ * to a file or directory and vice versa.
+ */
+ if (S_ISLNK(mode))
+ return mode & S_IFLNK;
+
/* For files, the only change we care about is the executable bit. */
return mode & S_IXUSR;
}
diff --git a/regress/cmdline/diff.sh b/regress/cmdline/diff.sh
index 649ab25..dbc3958 100755
--- a/regress/cmdline/diff.sh
+++ b/regress/cmdline/diff.sh
@@ -466,6 +466,123 @@ function test_diff_symlinks_in_work_tree {
test_done "$testroot" "$ret"
}
+function test_diff_symlinks_in_repo {
+ local testroot=`test_init diff_symlinks_in_repo`
+
+ (cd $testroot/repo && ln -s alpha alpha.link)
+ (cd $testroot/repo && ln -s epsilon epsilon.link)
+ (cd $testroot/repo && ln -s /etc/passwd passwd.link)
+ (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
+ (cd $testroot/repo && ln -s nonexistent nonexistent.link)
+ (cd $testroot/repo && ln -s .got/foo dotgotfoo.link)
+ (cd $testroot/repo && git add .)
+ git_commit $testroot/repo -m "add symlinks"
+ local commit_id1=`git_show_head $testroot/repo`
+
+ (cd $testroot/repo && ln -sf beta alpha.link)
+ (cd $testroot/repo && ln -sfh gamma epsilon.link)
+ (cd $testroot/repo && ln -sf ../gamma/delta epsilon/beta.link)
+ (cd $testroot/repo && ln -sf .got/bar $testroot/repo/dotgotfoo.link)
+ (cd $testroot/repo && git rm -q nonexistent.link)
+ (cd $testroot/repo && ln -sf epsilon/zeta zeta.link)
+ (cd $testroot/repo && git add .)
+ git_commit $testroot/repo -m "change symlinks"
+ local commit_id2=`git_show_head $testroot/repo`
+
+ got diff -r $testroot/repo $commit_id1 $commit_id2 > $testroot/stdout
+
+ echo "diff $commit_id1 $commit_id2" > $testroot/stdout.expected
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id1 -i | \
+ grep 'alpha.link@ -> alpha$' | \
+ cut -d' ' -f 1 >> $testroot/stdout.expected
+ echo -n 'blob + ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id2 -i | \
+ grep 'alpha.link@ -> beta$' | \
+ cut -d' ' -f 1 >> $testroot/stdout.expected
+ echo '--- alpha.link' >> $testroot/stdout.expected
+ echo '+++ alpha.link' >> $testroot/stdout.expected
+ echo '@@ -1 +1 @@' >> $testroot/stdout.expected
+ echo '-alpha' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+ echo '+beta' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id1 -i | \
+ grep 'dotgotfoo.link@ -> .got/foo$' | \
+ cut -d' ' -f 1 >> $testroot/stdout.expected
+ echo -n 'blob + ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id2 -i | \
+ grep 'dotgotfoo.link@ -> .got/bar$' | \
+ cut -d' ' -f 1 >> $testroot/stdout.expected
+ echo '--- dotgotfoo.link' >> $testroot/stdout.expected
+ echo '+++ dotgotfoo.link' >> $testroot/stdout.expected
+ echo '@@ -1 +1 @@' >> $testroot/stdout.expected
+ echo '-.got/foo' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+ echo '+.got/bar' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id1 -i epsilon | \
+ grep 'beta.link@ -> ../beta$' | \
+ cut -d' ' -f 1 >> $testroot/stdout.expected
+ echo -n 'blob + ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id2 -i epsilon | \
+ grep 'beta.link@ -> ../gamma/delta$' | \
+ cut -d' ' -f 1 >> $testroot/stdout.expected
+ echo '--- epsilon/beta.link' >> $testroot/stdout.expected
+ echo '+++ epsilon/beta.link' >> $testroot/stdout.expected
+ echo '@@ -1 +1 @@' >> $testroot/stdout.expected
+ echo '-../beta' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+ echo '+../gamma/delta' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id1 -i | \
+ grep 'epsilon.link@ -> epsilon$' | \
+ cut -d' ' -f 1 >> $testroot/stdout.expected
+ echo -n 'blob + ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id2 -i | \
+ grep 'epsilon.link@ -> gamma$' | \
+ cut -d' ' -f 1 >> $testroot/stdout.expected
+ echo '--- epsilon.link' >> $testroot/stdout.expected
+ echo '+++ epsilon.link' >> $testroot/stdout.expected
+ echo '@@ -1 +1 @@' >> $testroot/stdout.expected
+ echo '-epsilon' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+ echo '+gamma' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id1 -i | \
+ grep 'nonexistent.link@ -> nonexistent$' | \
+ cut -d' ' -f 1 | sed -e 's/$/ (mode 120000)/' \
+ >> $testroot/stdout.expected
+ echo 'blob + /dev/null' >> $testroot/stdout.expected
+ echo '--- nonexistent.link' >> $testroot/stdout.expected
+ echo '+++ /dev/null' >> $testroot/stdout.expected
+ echo '@@ -1 +0,0 @@' >> $testroot/stdout.expected
+ echo '-nonexistent' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+ echo 'blob - /dev/null' >> $testroot/stdout.expected
+ echo -n 'blob + ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -c $commit_id2 -i | \
+ grep 'zeta.link@ -> epsilon/zeta$' | \
+ cut -d' ' -f 1 | sed -e 's/$/ (mode 120000)/' \
+ >> $testroot/stdout.expected
+ echo '--- /dev/null' >> $testroot/stdout.expected
+ echo '+++ zeta.link' >> $testroot/stdout.expected
+ echo '@@ -0,0 +1 @@' >> $testroot/stdout.expected
+ echo '+epsilon/zeta' >> $testroot/stdout.expected
+ echo '\ No newline at end of file' >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
run_test test_diff_basic
run_test test_diff_shows_conflict
run_test test_diff_tag
@@ -473,3 +590,4 @@ run_test test_diff_lightweight_tag
run_test test_diff_ignore_whitespace
run_test test_diff_submodule_of_same_repo
run_test test_diff_symlinks_in_work_tree
+run_test test_diff_symlinks_in_repo