got patch: handle git-style rename diffs extend the support for git-style diffs to include the "pure rename" case, i.e. when a file is renamed without any edits. ok stsp@
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 199 200 201 202 203 204 205 206 207
diff --git a/libexec/got-read-patch/got-read-patch.c b/libexec/got-read-patch/got-read-patch.c
index 879b77f..c002b7f 100644
--- a/libexec/got-read-patch/got-read-patch.c
+++ b/libexec/got-read-patch/got-read-patch.c
@@ -123,14 +123,14 @@ filename(const char *at, char **name)
}
static const struct got_error *
-find_patch(FILE *fp)
+find_patch(int *empty, FILE *fp)
{
const struct got_error *err = NULL;
char *old = NULL, *new = NULL;
char *line = NULL;
size_t linesize = 0;
ssize_t linelen;
- int create, git = 0;
+ int create, rename = 0, git = 0;
while ((linelen = getline(&line, &linesize, fp)) != -1) {
/*
@@ -141,15 +141,34 @@ find_patch(FILE *fp)
if (!strncmp(line, "--- ", 4)) {
free(old);
err = filename(line+4, &old);
+ } else if (rename && !strncmp(line, "rename from ", 12)) {
+ free(old);
+ err = filename(line+12, &old);
} else if (!strncmp(line, "+++ ", 4)) {
free(new);
err = filename(line+4, &new);
- } else if (!strncmp(line, "diff --git a/", 13))
+ } else if (rename && !strncmp(line, "rename to ", 10)) {
+ free(new);
+ err = filename(line + 10, &new);
+ } else if (git && !strncmp(line, "similarity index 100%", 21))
+ rename = 1;
+ else if (!strncmp(line, "diff --git a/", 13))
git = 1;
if (err)
break;
+ /*
+ * Git-style diffs with "similarity index 100%" don't
+ * have any hunks and ends with the "rename to foobar"
+ * line.
+ */
+ if (rename && old != NULL && new != NULL) {
+ *empty = 1;
+ err = send_patch(old, new, git);
+ break;
+ }
+
if (!strncmp(line, "@@ -", 4)) {
create = !strncmp(line+4, "0,0", 3);
if ((old == NULL && new == NULL) ||
@@ -412,7 +431,7 @@ read_patch(struct imsgbuf *ibuf, int fd)
{
const struct got_error *err = NULL;
FILE *fp;
- int ok, patch_found = 0;
+ int patch_found = 0;
if ((fp = fdopen(fd, "r")) == NULL) {
err = got_error_from_errno("fdopen");
@@ -421,16 +440,19 @@ read_patch(struct imsgbuf *ibuf, int fd)
}
while (!feof(fp)) {
- err = find_patch(fp);
+ int empty = 0, ok = 1;
+
+ err = find_patch(&empty, fp);
if (err)
goto done;
patch_found = 1;
for (;;) {
- err = parse_hunk(fp, &ok);
+ if (!empty)
+ err = parse_hunk(fp, &ok);
if (err)
goto done;
- if (!ok) {
+ if (!ok || empty) {
err = send_patch_done();
if (err)
goto done;
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index 4398159..b30e9e0 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -647,14 +647,22 @@ test_patch_rename() {
fi
cat <<EOF > $testroot/wt/patch
+diff --git a/beta b/iota
+similarity index 100%
+rename from beta
+rename to iota
diff --git a/alpha b/eta
--- a/alpha
+++ b/eta
-@@ -0,0 +0,0 @@
+@@ -1 +1 @@
+-alpha
++eta
EOF
- echo 'D alpha' > $testroot/stdout.expected
- echo 'A eta' >> $testroot/stdout.expected
+ echo 'D beta' > $testroot/stdout.expected
+ echo 'A iota' >> $testroot/stdout.expected
+ echo 'D alpha' >> $testroot/stdout.expected
+ echo 'A eta' >> $testroot/stdout.expected
(cd $testroot/wt && got patch patch) > $testroot/stdout
ret=$?
@@ -671,77 +679,35 @@ EOF
return 1
fi
- if [ -f $testroot/wt/alpha ]; then
- echo "alpha was not removed" >&2
+ if [ -f $testroot/wt/alpha -o -f $testroot/wt/beta ]; then
+ echo "alpha or beta were not removed" >&2
test_done $testroot 1
return 1
fi
- if [ ! -f $testroot/wt/eta ]; then
- echo "eta was not created" >&2
+ if [ ! -f $testroot/wt/iota -o ! -f $testroot/wt/eta ]; then
+ echo "iota or eta were not created" >&2
test_done $testroot 1
return 1
fi
- echo alpha > $testroot/wt/eta.expected
- cmp -s $testroot/wt/eta.expected $testroot/wt/eta
- ret=$?
- if [ $ret -ne 0 ]; then
- diff -u $testroot/wt/eta.expected $testroot/wt/eta
- test_done $testroot $ret
- return 1
- fi
-
- # revert the changes and try again with a rename + edit
- (cd $testroot/wt && got revert alpha eta) > /dev/null
+ echo beta > $testroot/wt/iota.expected
+ cmp -s $testroot/wt/iota.expected $testroot/wt/iota
ret=$?
if [ $ret -ne 0 ]; then
+ diff -u $testroot/wt/iota.expected $testroot/wt/iota
test_done $testroot $ret
return 1
fi
- rm $testroot/wt/eta
- cat <<EOF > $testroot/wt/patch
-diff --git a/alpha b/eta
---- a/alpha
-+++ b/eta
-@@ -1 +1,2 @@
- alpha
-+but now is eta
-EOF
-
- (cd $testroot/wt && got patch patch) > $testroot/stdout
- ret=$?
- if [ $ret -ne 0 ]; then
- test_done $testroot $ret
- return 1
- fi
-
- cmp -s $testroot/stdout.expected $testroot/stdout
+ echo eta > $testroot/wt/eta.expected
+ cmp -s $testroot/wt/eta.expected $testroot/wt/eta
ret=$?
if [ $ret -ne 0 ]; then
- diff -u $testroot/stdout.expected $testroot/stdout
+ diff -u $testroot/wt/eta.expected $testroot/wt/eta
test_done $testroot $ret
return 1
fi
- if [ -f $testroot/wt/alpha ]; then
- echo "alpha was not removed" >&2
- test_done $testroot 1
- return 1
- fi
- if [ ! -f $testroot/wt/eta ]; then
- echo "eta was not created" >&2
- test_done $testroot 1
- return 1
- fi
-
- echo alpha > $testroot/wt/eta.expected
- echo 'but now is eta' >> $testroot/wt/eta.expected
- cmp -s $testroot/wt/eta.expected $testroot/wt/eta
- ret=$?
- if [ $ret -ne 0 ]; then
- diff -u $testroot/wt/eta.expected $testroot/wt/eta
- fi
test_done $testroot $ret
}