1 #include "git-compat-util.h"
2 #include "cache-tree.h"
6 #include "object-name.h"
11 #include "unpack-trees.h"
14 static int update_refs(const struct reset_head_opts
*opts
,
15 const struct object_id
*oid
,
16 const struct object_id
*head
)
18 unsigned detach_head
= opts
->flags
& RESET_HEAD_DETACH
;
19 unsigned run_hook
= opts
->flags
& RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
20 unsigned update_orig_head
= opts
->flags
& RESET_ORIG_HEAD
;
21 const struct object_id
*orig_head
= opts
->orig_head
;
22 const char *switch_to_branch
= opts
->branch
;
23 const char *reflog_branch
= opts
->branch_msg
;
24 const char *reflog_head
= opts
->head_msg
;
25 const char *reflog_orig_head
= opts
->orig_head_msg
;
26 const char *default_reflog_action
= opts
->default_reflog_action
;
27 struct object_id
*old_orig
= NULL
, oid_old_orig
;
28 struct strbuf msg
= STRBUF_INIT
;
29 const char *reflog_action
;
33 if ((update_orig_head
&& !reflog_orig_head
) || !reflog_head
) {
34 if (!default_reflog_action
)
35 BUG("default_reflog_action must be given when reflog messages are omitted");
36 reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
37 strbuf_addf(&msg
, "%s: ", reflog_action
? reflog_action
:
38 default_reflog_action
);
42 if (update_orig_head
) {
43 if (!repo_get_oid(the_repository
, "ORIG_HEAD", &oid_old_orig
))
44 old_orig
= &oid_old_orig
;
46 if (!reflog_orig_head
) {
47 strbuf_addstr(&msg
, "updating ORIG_HEAD");
48 reflog_orig_head
= msg
.buf
;
50 refs_update_ref(get_main_ref_store(the_repository
),
51 reflog_orig_head
, "ORIG_HEAD",
52 orig_head
? orig_head
: head
,
53 old_orig
, 0, UPDATE_REFS_MSG_ON_ERR
);
55 refs_delete_ref(get_main_ref_store(the_repository
),
56 NULL
, "ORIG_HEAD", old_orig
, 0);
60 strbuf_setlen(&msg
, prefix_len
);
61 strbuf_addstr(&msg
, "updating HEAD");
62 reflog_head
= msg
.buf
;
64 if (!switch_to_branch
)
65 ret
= refs_update_ref(get_main_ref_store(the_repository
),
66 reflog_head
, "HEAD", oid
, head
,
67 detach_head
? REF_NO_DEREF
: 0,
68 UPDATE_REFS_MSG_ON_ERR
);
70 ret
= refs_update_ref(get_main_ref_store(the_repository
),
71 reflog_branch
? reflog_branch
: reflog_head
,
72 switch_to_branch
, oid
, NULL
, 0,
73 UPDATE_REFS_MSG_ON_ERR
);
75 ret
= refs_update_symref(get_main_ref_store(the_repository
),
76 "HEAD", switch_to_branch
,
80 run_hooks_l("post-checkout",
81 oid_to_hex(head
? head
: null_oid()),
82 oid_to_hex(oid
), "1", NULL
);
87 int reset_head(struct repository
*r
, const struct reset_head_opts
*opts
)
89 const struct object_id
*oid
= opts
->oid
;
90 const char *switch_to_branch
= opts
->branch
;
91 unsigned reset_hard
= opts
->flags
& RESET_HEAD_HARD
;
92 unsigned refs_only
= opts
->flags
& RESET_HEAD_REFS_ONLY
;
93 unsigned update_orig_head
= opts
->flags
& RESET_ORIG_HEAD
;
94 struct object_id
*head
= NULL
, head_oid
;
95 struct tree_desc desc
[2] = { { NULL
}, { NULL
} };
96 struct lock_file lock
= LOCK_INIT
;
97 struct unpack_trees_options unpack_tree_opts
= { 0 };
102 if (switch_to_branch
&& !starts_with(switch_to_branch
, "refs/"))
103 BUG("Not a fully qualified branch: '%s'", switch_to_branch
);
105 if (opts
->orig_head_msg
&& !update_orig_head
)
106 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
108 if (opts
->branch_msg
&& !opts
->branch
)
109 BUG("branch reflog message given without a branch");
111 if (!refs_only
&& repo_hold_locked_index(r
, &lock
, LOCK_REPORT_ON_ERROR
) < 0) {
113 goto leave_reset_head
;
116 if (!repo_get_oid(r
, "HEAD", &head_oid
)) {
118 } else if (!oid
|| !reset_hard
) {
119 ret
= error(_("could not determine HEAD revision"));
120 goto leave_reset_head
;
127 return update_refs(opts
, oid
, head
);
129 action
= reset_hard
? "reset" : "checkout";
130 setup_unpack_trees_porcelain(&unpack_tree_opts
, action
);
131 unpack_tree_opts
.head_idx
= 1;
132 unpack_tree_opts
.src_index
= r
->index
;
133 unpack_tree_opts
.dst_index
= r
->index
;
134 unpack_tree_opts
.fn
= reset_hard
? oneway_merge
: twoway_merge
;
135 unpack_tree_opts
.update
= 1;
136 unpack_tree_opts
.merge
= 1;
137 unpack_tree_opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
138 unpack_tree_opts
.skip_cache_tree_update
= 1;
139 init_checkout_metadata(&unpack_tree_opts
.meta
, switch_to_branch
, oid
, NULL
);
141 unpack_tree_opts
.reset
= UNPACK_RESET_PROTECT_UNTRACKED
;
143 if (repo_read_index_unmerged(r
) < 0) {
144 ret
= error(_("could not read index"));
145 goto leave_reset_head
;
148 if (!reset_hard
&& !fill_tree_descriptor(r
, &desc
[nr
++], &head_oid
)) {
149 ret
= error(_("failed to find tree of %s"),
150 oid_to_hex(&head_oid
));
151 goto leave_reset_head
;
154 if (!fill_tree_descriptor(r
, &desc
[nr
++], oid
)) {
155 ret
= error(_("failed to find tree of %s"), oid_to_hex(oid
));
156 goto leave_reset_head
;
159 if (unpack_trees(nr
, desc
, &unpack_tree_opts
)) {
161 goto leave_reset_head
;
164 tree
= parse_tree_indirect(oid
);
166 ret
= error(_("unable to read tree (%s)"), oid_to_hex(oid
));
167 goto leave_reset_head
;
170 prime_cache_tree(r
, r
->index
, tree
);
172 if (write_locked_index(r
->index
, &lock
, COMMIT_LOCK
) < 0) {
173 ret
= error(_("could not write index"));
174 goto leave_reset_head
;
177 if (oid
!= &head_oid
|| update_orig_head
|| switch_to_branch
)
178 ret
= update_refs(opts
, oid
, head
);
181 rollback_lock_file(&lock
);
182 clear_unpack_trees_porcelain(&unpack_tree_opts
);
184 free((void *)desc
[--nr
].buffer
);