t0612: add tests to exercise Git/JGit reftable compatibility
[alt-git.git] / builtin / merge-index.c
blob270d5f644ac22e1a8a1b43b282b9ebf0f5cb12b0
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "hex.h"
4 #include "read-cache-ll.h"
5 #include "repository.h"
6 #include "run-command.h"
7 #include "sparse-index.h"
9 static const char *pgm;
10 static int one_shot, quiet;
11 static int err;
13 static int merge_entry(int pos, const char *path)
15 int found;
16 const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
17 char hexbuf[4][GIT_MAX_HEXSZ + 1];
18 char ownbuf[4][60];
19 struct child_process cmd = CHILD_PROCESS_INIT;
21 if (pos >= the_index.cache_nr)
22 die("git merge-index: %s not in the cache", path);
23 found = 0;
24 do {
25 const struct cache_entry *ce = the_index.cache[pos];
26 int stage = ce_stage(ce);
28 if (strcmp(ce->name, path))
29 break;
30 found++;
31 oid_to_hex_r(hexbuf[stage], &ce->oid);
32 xsnprintf(ownbuf[stage], sizeof(ownbuf[stage]), "%o", ce->ce_mode);
33 arguments[stage] = hexbuf[stage];
34 arguments[stage + 4] = ownbuf[stage];
35 } while (++pos < the_index.cache_nr);
36 if (!found)
37 die("git merge-index: %s not in the cache", path);
39 strvec_pushv(&cmd.args, arguments);
40 if (run_command(&cmd)) {
41 if (one_shot)
42 err++;
43 else {
44 if (!quiet)
45 die("merge program failed");
46 exit(1);
49 return found;
52 static void merge_one_path(const char *path)
54 int pos = index_name_pos(&the_index, path, strlen(path));
57 * If it already exists in the cache as stage0, it's
58 * already merged and there is nothing to do.
60 if (pos < 0)
61 merge_entry(-pos-1, path);
64 static void merge_all(void)
66 int i;
67 /* TODO: audit for interaction with sparse-index. */
68 ensure_full_index(&the_index);
69 for (i = 0; i < the_index.cache_nr; i++) {
70 const struct cache_entry *ce = the_index.cache[i];
71 if (!ce_stage(ce))
72 continue;
73 i += merge_entry(i, ce->name)-1;
77 int cmd_merge_index(int argc, const char **argv, const char *prefix UNUSED)
79 int i, force_file = 0;
81 /* Without this we cannot rely on waitpid() to tell
82 * what happened to our children.
84 signal(SIGCHLD, SIG_DFL);
86 if (argc < 3)
87 usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
89 repo_read_index(the_repository);
91 /* TODO: audit for interaction with sparse-index. */
92 ensure_full_index(&the_index);
94 i = 1;
95 if (!strcmp(argv[i], "-o")) {
96 one_shot = 1;
97 i++;
99 if (!strcmp(argv[i], "-q")) {
100 quiet = 1;
101 i++;
103 pgm = argv[i++];
104 for (; i < argc; i++) {
105 const char *arg = argv[i];
106 if (!force_file && *arg == '-') {
107 if (!strcmp(arg, "--")) {
108 force_file = 1;
109 continue;
111 if (!strcmp(arg, "-a")) {
112 merge_all();
113 continue;
115 die("git merge-index: unknown option %s", arg);
117 merge_one_path(arg);
119 if (err && !quiet)
120 die("merge program failed");
121 return err;