refactor got_patch / got_worktree_patch_complete let got_patch own fileindex_path and call got_worktree_patch_complete only if got_wokrtree_patch_prepare hasn't failed. suggested by 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
diff --git a/include/got_worktree.h b/include/got_worktree.h
index df7a870..610e03a 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -572,4 +572,4 @@ got_worktree_patch_schedule_rm(const char *, struct got_repository *,
/* Complete the patch operation. */
const struct got_error *
-got_worktree_patch_complete(struct got_fileindex *, char *);
+got_worktree_patch_complete(struct got_fileindex *, const char *);
diff --git a/lib/patch.c b/lib/patch.c
index cf2c864..5567c9d 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -710,7 +710,7 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
{
- const struct got_error *err = NULL, *complete_err;
+ const struct got_error *err = NULL, *complete_err = NULL;
struct got_fileindex *fileindex = NULL;
char *fileindex_path = NULL;
char *oldpath, *newpath;
@@ -798,9 +798,12 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
}
done:
- complete_err = got_worktree_patch_complete(fileindex, fileindex_path);
+ if (fileindex != NULL)
+ complete_err = got_worktree_patch_complete(fileindex,
+ fileindex_path);
if (complete_err && err == NULL)
err = complete_err;
+ free(fileindex_path);
if (fd != -1 && close(fd) == -1 && err == NULL)
err = got_error_from_errno("close");
if (ibuf != NULL)
diff --git a/lib/worktree.c b/lib/worktree.c
index e01fe52..ca8e4d6 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -8952,15 +8952,12 @@ got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
const struct got_error *
got_worktree_patch_complete(struct got_fileindex *fileindex,
- char *fileindex_path)
+ const char *fileindex_path)
{
const struct got_error *err = NULL;
- if (fileindex) {
- err = sync_fileindex(fileindex, fileindex_path);
- got_fileindex_free(fileindex);
- }
+ err = sync_fileindex(fileindex, fileindex_path);
+ got_fileindex_free(fileindex);
- free(fileindex_path);
return err;
}