Coverage Report

Created: 2024-09-08 06:24

/src/git/merge.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "gettext.h"
5
#include "hash.h"
6
#include "hex.h"
7
#include "lockfile.h"
8
#include "merge.h"
9
#include "commit.h"
10
#include "repository.h"
11
#include "run-command.h"
12
#include "resolve-undo.h"
13
#include "tree.h"
14
#include "tree-walk.h"
15
#include "unpack-trees.h"
16
17
static const char *merge_argument(struct commit *commit)
18
0
{
19
0
  return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
20
0
}
21
22
int try_merge_command(struct repository *r,
23
          const char *strategy, size_t xopts_nr,
24
          const char **xopts, struct commit_list *common,
25
          const char *head_arg, struct commit_list *remotes)
26
0
{
27
0
  struct child_process cmd = CHILD_PROCESS_INIT;
28
0
  int i, ret;
29
0
  struct commit_list *j;
30
31
0
  strvec_pushf(&cmd.args, "merge-%s", strategy);
32
0
  for (i = 0; i < xopts_nr; i++)
33
0
    strvec_pushf(&cmd.args, "--%s", xopts[i]);
34
0
  for (j = common; j; j = j->next)
35
0
    strvec_push(&cmd.args, merge_argument(j->item));
36
0
  strvec_push(&cmd.args, "--");
37
0
  strvec_push(&cmd.args, head_arg);
38
0
  for (j = remotes; j; j = j->next)
39
0
    strvec_push(&cmd.args, merge_argument(j->item));
40
41
0
  cmd.git_cmd = 1;
42
0
  ret = run_command(&cmd);
43
44
0
  discard_index(r->index);
45
0
  if (repo_read_index(r) < 0)
46
0
    die(_("failed to read the cache"));
47
0
  resolve_undo_clear_index(r->index);
48
49
0
  return ret;
50
0
}
51
52
int checkout_fast_forward(struct repository *r,
53
        const struct object_id *head,
54
        const struct object_id *remote,
55
        int overwrite_ignore)
56
0
{
57
0
  struct tree *trees[MAX_UNPACK_TREES];
58
0
  struct unpack_trees_options opts;
59
0
  struct tree_desc t[MAX_UNPACK_TREES];
60
0
  int i, nr_trees = 0;
61
0
  struct lock_file lock_file = LOCK_INIT;
62
63
0
  refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
64
65
0
  if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
66
0
    return -1;
67
68
0
  memset(&trees, 0, sizeof(trees));
69
0
  memset(&t, 0, sizeof(t));
70
71
0
  trees[nr_trees] = parse_tree_indirect(head);
72
0
  if (!trees[nr_trees++]) {
73
0
    rollback_lock_file(&lock_file);
74
0
    return -1;
75
0
  }
76
0
  trees[nr_trees] = parse_tree_indirect(remote);
77
0
  if (!trees[nr_trees++]) {
78
0
    rollback_lock_file(&lock_file);
79
0
    return -1;
80
0
  }
81
0
  for (i = 0; i < nr_trees; i++) {
82
0
    if (parse_tree(trees[i]) < 0) {
83
0
      rollback_lock_file(&lock_file);
84
0
      return -1;
85
0
    }
86
0
    init_tree_desc(t+i, &trees[i]->object.oid,
87
0
             trees[i]->buffer, trees[i]->size);
88
0
  }
89
90
0
  memset(&opts, 0, sizeof(opts));
91
0
  opts.preserve_ignored = !overwrite_ignore;
92
93
0
  opts.head_idx = 1;
94
0
  opts.src_index = r->index;
95
0
  opts.dst_index = r->index;
96
0
  opts.update = 1;
97
0
  opts.verbose_update = 1;
98
0
  opts.merge = 1;
99
0
  opts.fn = twoway_merge;
100
0
  init_checkout_metadata(&opts.meta, NULL, remote, NULL);
101
0
  setup_unpack_trees_porcelain(&opts, "merge");
102
103
0
  if (unpack_trees(nr_trees, t, &opts)) {
104
0
    rollback_lock_file(&lock_file);
105
0
    clear_unpack_trees_porcelain(&opts);
106
0
    return -1;
107
0
  }
108
0
  clear_unpack_trees_porcelain(&opts);
109
110
0
  if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
111
0
    return error(_("unable to write new index file"));
112
0
  return 0;
113
0
}