fix bug where 'revert -p' would delete all lines following a reverted change
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
diff --git a/lib/worktree.c b/lib/worktree.c
index d648267..96f3f30 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2893,7 +2893,7 @@ copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
(*line_cur2)++;
}
/* Skip over old file's replaced lines. */
- while (!feof(f1) && *line_cur1 <= end_new) {
+ while (!feof(f1) && *line_cur1 <= end_old) {
if (rejectfile)
err = copy_one_line(f1, NULL, rejectfile);
else
@@ -2902,12 +2902,33 @@ copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
return err;
(*line_cur1)++;
}
- /* Copy old file's lines after patch. */
- while (!feof(f1) && *line_cur1 <= end_old) {
- err = copy_one_line(f1, outfile, rejectfile);
- if (err)
- return err;
- (*line_cur1)++;
+
+ return NULL;
+}
+
+static const struct got_error *
+copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
+ FILE *outfile, FILE *rejectfile)
+{
+ const struct got_error *err;
+
+ if (outfile) {
+ /* Copy old file's lines until EOF. */
+ while (!feof(f1)) {
+ err = copy_one_line(f1, outfile, NULL);
+ if (err)
+ return err;
+ (*line_cur1)++;
+ }
+ }
+ if (rejectfile) {
+ /* Copy new file's lines until EOF. */
+ while (!feof(f2)) {
+ err = copy_one_line(f2, NULL, rejectfile);
+ if (err)
+ return err;
+ (*line_cur2)++;
+ }
}
return NULL;
@@ -2968,24 +2989,6 @@ apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
end_old, start_new, end_new, rejectfile, outfile);
break;
case GOT_PATCH_CHOICE_QUIT:
- if (outfile) {
- /* Copy old file's lines until EOF. */
- while (!feof(f1)) {
- err = copy_one_line(f1, outfile, NULL);
- if (err)
- goto done;
- (*line_cur1)++;
- }
- }
- if (rejectfile) {
- /* Copy new file's lines until EOF. */
- while (!feof(f2)) {
- err = copy_one_line(f2, NULL, rejectfile);
- if (err)
- goto done;
- (*line_cur2)++;
- }
- }
break;
default:
err = got_error(GOT_ERR_PATCH_CHOICE);
@@ -3086,6 +3089,10 @@ create_patched_content(char **path_outfile, int reverse_patch,
else if (choice == GOT_PATCH_CHOICE_QUIT)
break;
}
+ if (have_content)
+ err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
+ reverse_patch ? NULL : outfile,
+ reverse_patch ? outfile : NULL);
done:
free(id_str);
if (blob)
@@ -5882,6 +5889,9 @@ create_unstaged_content(char **path_unstaged_content,
if (choice == GOT_PATCH_CHOICE_QUIT)
break;
}
+ if (have_content || have_rejected_content)
+ err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
+ outfile, rejectfile);
done:
free(label1);
if (blob)
diff --git a/regress/cmdline/revert.sh b/regress/cmdline/revert.sh
index e2eeccf..d66beb7 100755
--- a/regress/cmdline/revert.sh
+++ b/regress/cmdline/revert.sh
@@ -794,6 +794,74 @@ function test_revert_patch_removed {
test_done "$testroot" "$ret"
}
+function test_revert_patch_one_change {
+ local testroot=`test_init revert_patch_one_change`
+
+ jot 16 > $testroot/repo/numbers
+ (cd $testroot/repo && git add numbers)
+ git_commit $testroot/repo -m "added numbers file"
+ local commit_id=`git_show_head $testroot/repo`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ sed -i -e 's/^2$/a/' $testroot/wt/numbers
+
+ # revert change with -p
+ printf "y\n" > $testroot/patchscript
+ (cd $testroot/wt && got revert -F $testroot/patchscript -p \
+ numbers > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got revert command failed unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+ cat > $testroot/stdout.expected <<EOF
+-----------------------------------------------
+@@ -1,5 +1,5 @@
+ 1
+-2
++a
+ 3
+ 4
+ 5
+-----------------------------------------------
+M numbers (change 1 of 1)
+revert this change? [y/n/q] y
+EOF
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+ echo -n > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got diff > $testroot/stdout)
+ echo -n > $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_revert_basic
run_test test_revert_rm
run_test test_revert_add
@@ -804,3 +872,4 @@ run_test test_revert_directory
run_test test_revert_patch
run_test test_revert_patch_added
run_test test_revert_patch_removed
+run_test test_revert_patch_one_change