| 14 | #include "hook.h" |
| 15 | |
| 16 | static int update_refs(const struct reset_head_opts *opts, |
| 17 | const struct object_id *oid, |
| 18 | const struct object_id *head) |
| 19 | { |
| 20 | unsigned detach_head = opts->flags & RESET_HEAD_DETACH; |
| 21 | unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; |
| 22 | unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; |
| 23 | const struct object_id *orig_head = opts->orig_head; |
| 24 | const char *switch_to_branch = opts->branch; |
| 25 | const char *reflog_branch = opts->branch_msg; |
| 26 | const char *reflog_head = opts->head_msg; |
| 27 | const char *reflog_orig_head = opts->orig_head_msg; |
| 28 | const char *default_reflog_action = opts->default_reflog_action; |
| 29 | struct object_id *old_orig = NULL, oid_old_orig; |
| 30 | struct strbuf msg = STRBUF_INIT; |
| 31 | const char *reflog_action; |
| 32 | size_t prefix_len; |
| 33 | int ret; |
| 34 | |
| 35 | if ((update_orig_head && !reflog_orig_head) || !reflog_head) { |
| 36 | if (!default_reflog_action) |
| 37 | BUG("default_reflog_action must be given when reflog messages are omitted"); |
| 38 | reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); |
| 39 | strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : |
| 40 | default_reflog_action); |
| 41 | } |
| 42 | prefix_len = msg.len; |
| 43 | |
| 44 | if (update_orig_head) { |
| 45 | if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig)) |
| 46 | old_orig = &oid_old_orig; |
| 47 | if (head) { |
| 48 | if (!reflog_orig_head) { |
| 49 | strbuf_addstr(&msg, "updating ORIG_HEAD"); |
| 50 | reflog_orig_head = msg.buf; |
| 51 | } |
| 52 | refs_update_ref(get_main_ref_store(the_repository), |
| 53 | reflog_orig_head, "ORIG_HEAD", |
| 54 | orig_head ? orig_head : head, |
| 55 | old_orig, 0, UPDATE_REFS_MSG_ON_ERR); |
| 56 | } else if (old_orig) |
| 57 | refs_delete_ref(get_main_ref_store(the_repository), |
| 58 | NULL, "ORIG_HEAD", old_orig, 0); |
| 59 | } |
| 60 | |
| 61 | if (!reflog_head) { |
| 62 | strbuf_setlen(&msg, prefix_len); |
| 63 | strbuf_addstr(&msg, "updating HEAD"); |
| 64 | reflog_head = msg.buf; |
| 65 | } |
| 66 | if (!switch_to_branch) |
| 67 | ret = refs_update_ref(get_main_ref_store(the_repository), |
| 68 | reflog_head, "HEAD", oid, head, |
| 69 | detach_head ? REF_NO_DEREF : 0, |
| 70 | UPDATE_REFS_MSG_ON_ERR); |
| 71 | else { |
| 72 | ret = refs_update_ref(get_main_ref_store(the_repository), |
| 73 | reflog_branch ? reflog_branch : reflog_head, |
no test coverage detected