1 #include "git-compat-util.h"
2 #include "cache-tree.h"
7 #include "run-command.h"
10 #include "unpack-trees.h"
13 static int update_refs(const struct reset_head_opts
*opts
,
14 const struct object_id
*oid
,
15 const struct object_id
*head
)
17 unsigned detach_head
= opts
->flags
& RESET_HEAD_DETACH
;
18 unsigned run_hook
= opts
->flags
& RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
19 unsigned update_orig_head
= opts
->flags
& RESET_ORIG_HEAD
;
20 const struct object_id
*orig_head
= opts
->orig_head
;
21 const char *switch_to_branch
= opts
->branch
;
22 const char *reflog_branch
= opts
->branch_msg
;
23 const char *reflog_head
= opts
->head_msg
;
24 const char *reflog_orig_head
= opts
->orig_head_msg
;
25 const char *default_reflog_action
= opts
->default_reflog_action
;
26 struct object_id
*old_orig
= NULL
, oid_old_orig
;
27 struct strbuf msg
= STRBUF_INIT
;
28 const char *reflog_action
;
32 if ((update_orig_head
&& !reflog_orig_head
) || !reflog_head
) {
33 if (!default_reflog_action
)
34 BUG("default_reflog_action must be given when reflog messages are omitted");
35 reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
36 strbuf_addf(&msg
, "%s: ", reflog_action
? reflog_action
:
37 default_reflog_action
);
41 if (update_orig_head
) {
42 if (!get_oid("ORIG_HEAD", &oid_old_orig
))
43 old_orig
= &oid_old_orig
;
45 if (!reflog_orig_head
) {
46 strbuf_addstr(&msg
, "updating ORIG_HEAD");
47 reflog_orig_head
= msg
.buf
;
49 update_ref(reflog_orig_head
, "ORIG_HEAD",
50 orig_head
? orig_head
: head
,
51 old_orig
, 0, UPDATE_REFS_MSG_ON_ERR
);
53 delete_ref(NULL
, "ORIG_HEAD", old_orig
, 0);
57 strbuf_setlen(&msg
, prefix_len
);
58 strbuf_addstr(&msg
, "updating HEAD");
59 reflog_head
= msg
.buf
;
61 if (!switch_to_branch
)
62 ret
= update_ref(reflog_head
, "HEAD", oid
, head
,
63 detach_head
? REF_NO_DEREF
: 0,
64 UPDATE_REFS_MSG_ON_ERR
);
66 ret
= update_ref(reflog_branch
? reflog_branch
: reflog_head
,
67 switch_to_branch
, oid
, NULL
, 0,
68 UPDATE_REFS_MSG_ON_ERR
);
70 ret
= create_symref("HEAD", switch_to_branch
,
74 run_hooks_l("post-checkout",
75 oid_to_hex(head
? head
: null_oid()),
76 oid_to_hex(oid
), "1", NULL
);
81 int reset_head(struct repository
*r
, const struct reset_head_opts
*opts
)
83 const struct object_id
*oid
= opts
->oid
;
84 const char *switch_to_branch
= opts
->branch
;
85 unsigned reset_hard
= opts
->flags
& RESET_HEAD_HARD
;
86 unsigned refs_only
= opts
->flags
& RESET_HEAD_REFS_ONLY
;
87 unsigned update_orig_head
= opts
->flags
& RESET_ORIG_HEAD
;
88 struct object_id
*head
= NULL
, head_oid
;
89 struct tree_desc desc
[2] = { { NULL
}, { NULL
} };
90 struct lock_file lock
= LOCK_INIT
;
91 struct unpack_trees_options unpack_tree_opts
= { 0 };
96 if (switch_to_branch
&& !starts_with(switch_to_branch
, "refs/"))
97 BUG("Not a fully qualified branch: '%s'", switch_to_branch
);
99 if (opts
->orig_head_msg
&& !update_orig_head
)
100 BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
102 if (opts
->branch_msg
&& !opts
->branch
)
103 BUG("branch reflog message given without a branch");
105 if (!refs_only
&& repo_hold_locked_index(r
, &lock
, LOCK_REPORT_ON_ERROR
) < 0) {
107 goto leave_reset_head
;
110 if (!get_oid("HEAD", &head_oid
)) {
112 } else if (!oid
|| !reset_hard
) {
113 ret
= error(_("could not determine HEAD revision"));
114 goto leave_reset_head
;
121 return update_refs(opts
, oid
, head
);
123 action
= reset_hard
? "reset" : "checkout";
124 setup_unpack_trees_porcelain(&unpack_tree_opts
, action
);
125 unpack_tree_opts
.head_idx
= 1;
126 unpack_tree_opts
.src_index
= r
->index
;
127 unpack_tree_opts
.dst_index
= r
->index
;
128 unpack_tree_opts
.fn
= reset_hard
? oneway_merge
: twoway_merge
;
129 unpack_tree_opts
.update
= 1;
130 unpack_tree_opts
.merge
= 1;
131 unpack_tree_opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
132 unpack_tree_opts
.skip_cache_tree_update
= 1;
133 init_checkout_metadata(&unpack_tree_opts
.meta
, switch_to_branch
, oid
, NULL
);
135 unpack_tree_opts
.reset
= UNPACK_RESET_PROTECT_UNTRACKED
;
137 if (repo_read_index_unmerged(r
) < 0) {
138 ret
= error(_("could not read index"));
139 goto leave_reset_head
;
142 if (!reset_hard
&& !fill_tree_descriptor(r
, &desc
[nr
++], &head_oid
)) {
143 ret
= error(_("failed to find tree of %s"),
144 oid_to_hex(&head_oid
));
145 goto leave_reset_head
;
148 if (!fill_tree_descriptor(r
, &desc
[nr
++], oid
)) {
149 ret
= error(_("failed to find tree of %s"), oid_to_hex(oid
));
150 goto leave_reset_head
;
153 if (unpack_trees(nr
, desc
, &unpack_tree_opts
)) {
155 goto leave_reset_head
;
158 tree
= parse_tree_indirect(oid
);
159 prime_cache_tree(r
, r
->index
, tree
);
161 if (write_locked_index(r
->index
, &lock
, COMMIT_LOCK
) < 0) {
162 ret
= error(_("could not write index"));
163 goto leave_reset_head
;
166 if (oid
!= &head_oid
|| update_orig_head
|| switch_to_branch
)
167 ret
= update_refs(opts
, oid
, head
);
170 rollback_lock_file(&lock
);
171 clear_unpack_trees_porcelain(&unpack_tree_opts
);
173 free((void *)desc
[--nr
].buffer
);