make 'got histedit' collapse folded add+delete operations into a no-op If a merged commit wants to delete a locally added file, and this locally added file matches the content which was deleted in the commit being merged, we can go ahead with the deletion because there is no risk of data loss. fixes the histedit problem reported by jrick on freenode
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
diff --git a/lib/got_lib_object_create.h b/lib/got_lib_object_create.h
index 899d18e..6767660 100644
--- a/lib/got_lib_object_create.h
+++ b/lib/got_lib_object_create.h
@@ -14,6 +14,8 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+const struct got_error *got_object_blob_file_create(struct got_object_id **,
+ FILE **, const char *);
const struct got_error *got_object_blob_create(struct got_object_id **,
const char *, struct got_repository *);
diff --git a/lib/object_create.c b/lib/object_create.c
index ed77449..73b108d 100644
--- a/lib/object_create.c
+++ b/lib/object_create.c
@@ -112,18 +112,18 @@ done:
}
const struct got_error *
-got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
- struct got_repository *repo)
+got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
+ const char *ondisk_path)
{
const struct got_error *err = NULL;
char *header = NULL;
- FILE *blobfile = NULL;
int fd = -1;
struct stat sb;
SHA1_CTX sha1_ctx;
size_t headerlen = 0, n;
*id = NULL;
+ *blobfile = NULL;
SHA1Init(&sha1_ctx);
@@ -149,15 +149,15 @@ got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
headerlen = strlen(header) + 1;
SHA1Update(&sha1_ctx, header, headerlen);
- blobfile = got_opentemp();
- if (blobfile == NULL) {
+ *blobfile = got_opentemp();
+ if (*blobfile == NULL) {
err = got_error_from_errno("got_opentemp");
goto done;
}
- n = fwrite(header, 1, headerlen, blobfile);
+ n = fwrite(header, 1, headerlen, *blobfile);
if (n != headerlen) {
- err = got_ferror(blobfile, GOT_ERR_IO);
+ err = got_ferror(*blobfile, GOT_ERR_IO);
goto done;
}
for (;;) {
@@ -180,9 +180,9 @@ got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
if (inlen == 0)
break; /* EOF */
SHA1Update(&sha1_ctx, buf, inlen);
- n = fwrite(buf, 1, inlen, blobfile);
+ n = fwrite(buf, 1, inlen, *blobfile);
if (n != inlen) {
- err = got_ferror(blobfile, GOT_ERR_IO);
+ err = got_ferror(*blobfile, GOT_ERR_IO);
goto done;
}
if (S_ISLNK(sb.st_mode))
@@ -196,18 +196,39 @@ got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
}
SHA1Final((*id)->sha1, &sha1_ctx);
- if (fflush(blobfile) != 0) {
+ if (fflush(*blobfile) != 0) {
err = got_error_from_errno("fflush");
goto done;
}
- rewind(blobfile);
-
- err = create_object_file(*id, blobfile, repo);
+ rewind(*blobfile);
done:
free(header);
if (fd != -1 && close(fd) != 0 && err == NULL)
err = got_error_from_errno("close");
- if (blobfile && fclose(blobfile) != 0 && err == NULL)
+ if (err) {
+ free(*id);
+ *id = NULL;
+ if (*blobfile) {
+ fclose(*blobfile);
+ *blobfile = NULL;
+ }
+ }
+ return err;
+}
+
+const struct got_error *
+got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
+ struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ FILE *blobfile = NULL;
+
+ err = got_object_blob_file_create(id, &blobfile, ondisk_path);
+ if (err)
+ return err;
+
+ err = create_object_file(*id, blobfile, repo);
+ if (fclose(blobfile) == EOF && err == NULL)
err = got_error_from_errno("fclose");
if (err) {
free(*id);
diff --git a/lib/worktree.c b/lib/worktree.c
index cade3cf..d376968 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2799,7 +2799,39 @@ merge_file_cb(void *arg, struct got_blob_object *blob1,
if (ie)
got_fileindex_entry_mark_deleted_from_disk(ie);
break;
- case GOT_STATUS_ADD:
+ case GOT_STATUS_ADD: {
+ struct got_object_id *id;
+ FILE *blob1_f;
+ /*
+ * Delete the added file only if its content already
+ * exists in the repository.
+ */
+ err = got_object_blob_file_create(&id, &blob1_f, path1);
+ if (err)
+ goto done;
+ if (got_object_id_cmp(id, id1) == 0) {
+ err = (*a->progress_cb)(a->progress_arg,
+ GOT_STATUS_DELETE, path1);
+ if (err)
+ goto done;
+ err = remove_ondisk_file(a->worktree->root_path,
+ path1);
+ if (err)
+ goto done;
+ if (ie)
+ got_fileindex_entry_remove(a->fileindex,
+ ie);
+ } else {
+ err = (*a->progress_cb)(a->progress_arg,
+ GOT_STATUS_CANNOT_DELETE, path1);
+ }
+ if (fclose(blob1_f) == EOF && err == NULL)
+ err = got_error_from_errno("fclose");
+ free(id);
+ if (err)
+ goto done;
+ break;
+ }
case GOT_STATUS_MODIFY:
case GOT_STATUS_CONFLICT:
err = (*a->progress_cb)(a->progress_arg,
diff --git a/regress/cmdline/histedit.sh b/regress/cmdline/histedit.sh
index d3da701..dc2761f 100755
--- a/regress/cmdline/histedit.sh
+++ b/regress/cmdline/histedit.sh
@@ -1388,8 +1388,8 @@ test_histedit_fold_add_delete() {
echo "G epsilon/psi" >> $testroot/stdout.expected
echo "$short_old_commit2 -> fold commit: editing psi" \
>> $testroot/stdout.expected
- echo "d epsilon/psi" >> $testroot/stdout.expected
- echo "$short_old_commit3 -> $short_new_commit1: folded changes" \
+ echo "D epsilon/psi" >> $testroot/stdout.expected
+ echo "$short_old_commit3 -> no-op change: folded changes" \
>> $testroot/stdout.expected
echo "Switching work tree to refs/heads/master" \
>> $testroot/stdout.expected
@@ -1403,9 +1403,8 @@ test_histedit_fold_add_delete() {
fi
if [ -e $testroot/wt/epsilon/psi ]; then
- #echo "removed file psi still exists on disk" >&2
- ret="xfail: removed file psi still exists on disk"
- test_done "$testroot" "$ret"
+ echo "removed file psi still exists on disk" >&2
+ test_done "$testroot" "1"
return 1
fi
@@ -1422,7 +1421,6 @@ test_histedit_fold_add_delete() {
(cd $testroot/wt && got log -l3 | grep ^commit > $testroot/stdout)
echo "commit $new_commit1 (master)" > $testroot/stdout.expected
- echo "commit $orig_commit" >> $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then