Coverage Report

Created: 2024-09-08 06:24

/src/git/reset.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "cache-tree.h"
5
#include "gettext.h"
6
#include "hex.h"
7
#include "lockfile.h"
8
#include "object-name.h"
9
#include "refs.h"
10
#include "reset.h"
11
#include "tree-walk.h"
12
#include "tree.h"
13
#include "unpack-trees.h"
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
0
{
20
0
  unsigned detach_head = opts->flags & RESET_HEAD_DETACH;
21
0
  unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
22
0
  unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
23
0
  const struct object_id *orig_head = opts->orig_head;
24
0
  const char *switch_to_branch = opts->branch;
25
0
  const char *reflog_branch = opts->branch_msg;
26
0
  const char *reflog_head = opts->head_msg;
27
0
  const char *reflog_orig_head = opts->orig_head_msg;
28
0
  const char *default_reflog_action = opts->default_reflog_action;
29
0
  struct object_id *old_orig = NULL, oid_old_orig;
30
0
  struct strbuf msg = STRBUF_INIT;
31
0
  const char *reflog_action;
32
0
  size_t prefix_len;
33
0
  int ret;
34
35
0
  if ((update_orig_head && !reflog_orig_head) || !reflog_head) {
36
0
    if (!default_reflog_action)
37
0
      BUG("default_reflog_action must be given when reflog messages are omitted");
38
0
    reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
39
0
    strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action :
40
0
                default_reflog_action);
41
0
  }
42
0
  prefix_len = msg.len;
43
44
0
  if (update_orig_head) {
45
0
    if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig))
46
0
      old_orig = &oid_old_orig;
47
0
    if (head) {
48
0
      if (!reflog_orig_head) {
49
0
        strbuf_addstr(&msg, "updating ORIG_HEAD");
50
0
        reflog_orig_head = msg.buf;
51
0
      }
52
0
      refs_update_ref(get_main_ref_store(the_repository),
53
0
          reflog_orig_head, "ORIG_HEAD",
54
0
          orig_head ? orig_head : head,
55
0
          old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
56
0
    } else if (old_orig)
57
0
      refs_delete_ref(get_main_ref_store(the_repository),
58
0
          NULL, "ORIG_HEAD", old_orig, 0);
59
0
  }
60
61
0
  if (!reflog_head) {
62
0
    strbuf_setlen(&msg, prefix_len);
63
0
    strbuf_addstr(&msg, "updating HEAD");
64
0
    reflog_head = msg.buf;
65
0
  }
66
0
  if (!switch_to_branch)
67
0
    ret = refs_update_ref(get_main_ref_store(the_repository),
68
0
              reflog_head, "HEAD", oid, head,
69
0
              detach_head ? REF_NO_DEREF : 0,
70
0
              UPDATE_REFS_MSG_ON_ERR);
71
0
  else {
72
0
    ret = refs_update_ref(get_main_ref_store(the_repository),
73
0
              reflog_branch ? reflog_branch : reflog_head,
74
0
              switch_to_branch, oid, NULL, 0,
75
0
              UPDATE_REFS_MSG_ON_ERR);
76
0
    if (!ret)
77
0
      ret = refs_update_symref(get_main_ref_store(the_repository),
78
0
             "HEAD", switch_to_branch,
79
0
             reflog_head);
80
0
  }
81
0
  if (!ret && run_hook)
82
0
    run_hooks_l(the_repository, "post-checkout",
83
0
          oid_to_hex(head ? head : null_oid()),
84
0
          oid_to_hex(oid), "1", NULL);
85
0
  strbuf_release(&msg);
86
0
  return ret;
87
0
}
88
89
int reset_head(struct repository *r, const struct reset_head_opts *opts)
90
0
{
91
0
  const struct object_id *oid = opts->oid;
92
0
  const char *switch_to_branch = opts->branch;
93
0
  unsigned reset_hard = opts->flags & RESET_HEAD_HARD;
94
0
  unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;
95
0
  unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
96
0
  struct object_id *head = NULL, head_oid;
97
0
  struct tree_desc desc[2] = { { NULL }, { NULL } };
98
0
  struct lock_file lock = LOCK_INIT;
99
0
  struct unpack_trees_options unpack_tree_opts = { 0 };
100
0
  struct tree *tree;
101
0
  const char *action;
102
0
  int ret = 0, nr = 0;
103
104
0
  if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
105
0
    BUG("Not a fully qualified branch: '%s'", switch_to_branch);
106
107
0
  if (opts->orig_head_msg && !update_orig_head)
108
0
    BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
109
110
0
  if (opts->branch_msg && !opts->branch)
111
0
    BUG("branch reflog message given without a branch");
112
113
0
  if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
114
0
    ret = -1;
115
0
    goto leave_reset_head;
116
0
  }
117
118
0
  if (!repo_get_oid(r, "HEAD", &head_oid)) {
119
0
    head = &head_oid;
120
0
  } else if (!oid || !reset_hard) {
121
0
    ret = error(_("could not determine HEAD revision"));
122
0
    goto leave_reset_head;
123
0
  }
124
125
0
  if (!oid)
126
0
    oid = &head_oid;
127
128
0
  if (refs_only)
129
0
    return update_refs(opts, oid, head);
130
131
0
  action = reset_hard ? "reset" : "checkout";
132
0
  setup_unpack_trees_porcelain(&unpack_tree_opts, action);
133
0
  unpack_tree_opts.head_idx = 1;
134
0
  unpack_tree_opts.src_index = r->index;
135
0
  unpack_tree_opts.dst_index = r->index;
136
0
  unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
137
0
  unpack_tree_opts.update = 1;
138
0
  unpack_tree_opts.merge = 1;
139
0
  unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
140
0
  unpack_tree_opts.skip_cache_tree_update = 1;
141
0
  init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
142
0
  if (reset_hard)
143
0
    unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
144
145
0
  if (repo_read_index_unmerged(r) < 0) {
146
0
    ret = error(_("could not read index"));
147
0
    goto leave_reset_head;
148
0
  }
149
150
0
  if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
151
0
    ret = error(_("failed to find tree of %s"),
152
0
          oid_to_hex(&head_oid));
153
0
    goto leave_reset_head;
154
0
  }
155
156
0
  if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
157
0
    ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
158
0
    goto leave_reset_head;
159
0
  }
160
161
0
  if (unpack_trees(nr, desc, &unpack_tree_opts)) {
162
0
    ret = -1;
163
0
    goto leave_reset_head;
164
0
  }
165
166
0
  tree = parse_tree_indirect(oid);
167
0
  if (!tree) {
168
0
    ret = error(_("unable to read tree (%s)"), oid_to_hex(oid));
169
0
    goto leave_reset_head;
170
0
  }
171
172
0
  prime_cache_tree(r, r->index, tree);
173
174
0
  if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
175
0
    ret = error(_("could not write index"));
176
0
    goto leave_reset_head;
177
0
  }
178
179
0
  if (oid != &head_oid || update_orig_head || switch_to_branch)
180
0
    ret = update_refs(opts, oid, head);
181
182
0
leave_reset_head:
183
0
  rollback_lock_file(&lock);
184
0
  clear_unpack_trees_porcelain(&unpack_tree_opts);
185
0
  while (nr)
186
0
    free((void *)desc[--nr].buffer);
187
0
  return ret;
188
189
0
}