1 #include "git-compat-util.h"
2 #include "cache-tree.h"
6 #include "run-command.h"
9 #include "unpack-trees.h"
12 static int update_refs(const struct reset_head_opts
*opts
,
13 const struct object_id
*oid
,
14 const struct object_id
*head
)
16 unsigned detach_head
= opts
->flags
& RESET_HEAD_DETACH
;
17 unsigned run_hook
= opts
->flags
& RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
18 unsigned update_orig_head
= opts
->flags
& RESET_ORIG_HEAD
;
19 const struct object_id
*orig_head
= opts
->orig_head
;
20 const char *switch_to_branch
= opts
->branch
;
21 const char *reflog_branch
= opts
->branch_msg
;
22 const char *reflog_head
= opts
->head_msg
;
23 const char *reflog_orig_head
= opts
->orig_head_msg
;
24 const char *default_reflog_action
= opts
->default_reflog_action
;
25 struct object_id
*old_orig
= NULL
, oid_old_orig
;
26 struct strbuf msg
= STRBUF_INIT
;
27 const char *reflog_action
;
31 if ((update_orig_head
&& !reflog_orig_head
) || !reflog_head
) {
32 if (!default_reflog_action
)
33 BUG("default_reflog_action must be given when reflog messages are omitted");
34 reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
35 strbuf_addf(&msg
, "%s: ", reflog_action
? reflog_action
:
36 default_reflog_action
);
40 if (update_orig_head
) {
41 if (!repo_get_oid(the_repository
, "ORIG_HEAD", &oid_old_orig
))
42 old_orig
= &oid_old_orig
;
44 if (!reflog_orig_head
) {
45 strbuf_addstr(&msg
, "updating ORIG_HEAD");
46 reflog_orig_head
= msg
.buf
;
48 update_ref(reflog_orig_head
, "ORIG_HEAD",
49 orig_head
? orig_head
: head
,
50 old_orig
, 0, UPDATE_REFS_MSG_ON_ERR
);
52 delete_ref(NULL
, "ORIG_HEAD", old_orig
, 0);
56 strbuf_setlen(&msg
, prefix_len
);
57 strbuf_addstr(&msg
, "updating HEAD");
58 reflog_head
= msg
.buf
;
60 if (!switch_to_branch
)
61 ret
= update_ref(reflog_head
, "HEAD", oid
, head
,
62 detach_head
? REF_NO_DEREF
: 0,
63 UPDATE_REFS_MSG_ON_ERR
);
65 ret
= update_ref(reflog_branch
? reflog_branch
: reflog_head
,
66 switch_to_branch
, oid
, NULL
, 0,
67 UPDATE_REFS_MSG_ON_ERR
);
69 ret
= create_symref("HEAD", switch_to_branch
,
73 run_hooks_l("post-checkout",
74 oid_to_hex(head
? head
: null_oid()),
75 oid_to_hex(oid
), "1", NULL
);
80 int reset_head(struct repository
*r
, const struct reset_head_opts
*opts
)
82 const struct object_id
*oid
= opts
->oid
;
83 const char *switch_to_branch
= opts
->branch
;
84 unsigned reset_hard
= opts
->flags
& RESET_HEAD_HARD
;
85 unsigned refs_only
= opts
->flags
& RESET_HEAD_REFS_ONLY
;
86 unsigned update_orig_head
= opts
->flags
& RESET_ORIG_HEAD
;
87 struct object_id
*head
= NULL
, head_oid
;
88 struct tree_desc desc
[2] = { { NULL
}, { NULL
} };
89 struct lock_file lock
= LOCK_INIT
;
90 struct unpack_trees_options unpack_tree_opts
= { 0 };
95 if (switch_to_branch
&& !starts_with(switch_to_branch
, "refs/"))
96 BUG("Not a fully qualified branch: '%s'", switch_to_branch
);
98 if (opts
->orig_head_msg
&& !update_orig_head
)
99 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
101 if (opts
->branch_msg
&& !opts
->branch
)
102 BUG("branch reflog message given without a branch");
104 if (!refs_only
&& repo_hold_locked_index(r
, &lock
, LOCK_REPORT_ON_ERROR
) < 0) {
106 goto leave_reset_head
;
109 if (!repo_get_oid(the_repository
, "HEAD", &head_oid
)) {
111 } else if (!oid
|| !reset_hard
) {
112 ret
= error(_("could not determine HEAD revision"));
113 goto leave_reset_head
;
120 return update_refs(opts
, oid
, head
);
122 action
= reset_hard
? "reset" : "checkout";
123 setup_unpack_trees_porcelain(&unpack_tree_opts
, action
);
124 unpack_tree_opts
.head_idx
= 1;
125 unpack_tree_opts
.src_index
= r
->index
;
126 unpack_tree_opts
.dst_index
= r
->index
;
127 unpack_tree_opts
.fn
= reset_hard
? oneway_merge
: twoway_merge
;
128 unpack_tree_opts
.update
= 1;
129 unpack_tree_opts
.merge
= 1;
130 unpack_tree_opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
131 unpack_tree_opts
.skip_cache_tree_update
= 1;
132 init_checkout_metadata(&unpack_tree_opts
.meta
, switch_to_branch
, oid
, NULL
);
134 unpack_tree_opts
.reset
= UNPACK_RESET_PROTECT_UNTRACKED
;
136 if (repo_read_index_unmerged(r
) < 0) {
137 ret
= error(_("could not read index"));
138 goto leave_reset_head
;
141 if (!reset_hard
&& !fill_tree_descriptor(r
, &desc
[nr
++], &head_oid
)) {
142 ret
= error(_("failed to find tree of %s"),
143 oid_to_hex(&head_oid
));
144 goto leave_reset_head
;
147 if (!fill_tree_descriptor(r
, &desc
[nr
++], oid
)) {
148 ret
= error(_("failed to find tree of %s"), oid_to_hex(oid
));
149 goto leave_reset_head
;
152 if (unpack_trees(nr
, desc
, &unpack_tree_opts
)) {
154 goto leave_reset_head
;
157 tree
= parse_tree_indirect(oid
);
158 prime_cache_tree(r
, r
->index
, tree
);
160 if (write_locked_index(r
->index
, &lock
, COMMIT_LOCK
) < 0) {
161 ret
= error(_("could not write index"));
162 goto leave_reset_head
;
165 if (oid
!= &head_oid
|| update_orig_head
|| switch_to_branch
)
166 ret
= update_refs(opts
, oid
, head
);
169 rollback_lock_file(&lock
);
170 clear_unpack_trees_porcelain(&unpack_tree_opts
);
172 free((void *)desc
[--nr
].buffer
);