move out-of-dateness check to a helper function
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
diff --git a/lib/worktree.c b/lib/worktree.c
index e0ae4e6..c00c98a 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2778,6 +2778,31 @@ done:
return err;
}
+static const struct got_error *
+check_ct_out_of_date(struct commitable *ct, struct got_repository *repo,
+ struct got_object_id *head_commit_id)
+{
+ const struct got_error *err = NULL;
+ struct got_object_id *id_in_head;
+
+ err = got_object_id_by_path(&id_in_head, repo,
+ head_commit_id, ct->in_repo_path);
+ if (err) {
+ if (err->code != GOT_ERR_NO_TREE_ENTRY)
+ return err;
+ if (ct->status != GOT_STATUS_ADD)
+ return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
+ err = NULL;
+ id_in_head = NULL;
+ }
+
+ if (id_in_head && got_object_id_cmp(id_in_head, ct->base_id) != 0)
+ err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
+
+ free(id_in_head);
+ return err;
+}
+
const struct got_error *
got_worktree_commit(struct got_object_id **new_commit_id,
struct got_worktree *worktree, const char *ondisk_path,
@@ -2833,33 +2858,11 @@ got_worktree_commit(struct got_object_id **new_commit_id,
if (err)
goto done;
- /* Out-of-dateness check against branch head. */
TAILQ_FOREACH(pe, &commitable_paths, entry) {
struct commitable *ct = pe->data;
- struct got_object_id *id_in_head;
-
- err = got_object_id_by_path(&id_in_head, repo,
- head_commit_id, ct->in_repo_path);
- if (err) {
- if (err->code == GOT_ERR_NO_TREE_ENTRY) {
- if (ct->status == GOT_STATUS_ADD) {
- err = NULL;
- id_in_head = NULL;
- } else {
- err = got_error(
- GOT_ERR_COMMIT_OUT_OF_DATE);
- goto done;
- }
- } else
- goto done;
- }
- if (id_in_head &&
- got_object_id_cmp(id_in_head, ct->base_id) != 0) {
- err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
- free(id_in_head);
+ err = check_ct_out_of_date(ct, repo, head_commit_id);
+ if (err)
goto done;
- }
- free(id_in_head);
}
err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);