Merge branch 'jk/commit-use-no-divider-with-interpret-trailers'
[alt-git.git] / builtin / merge-index.c
blobab16e70f23d532d769f074312352d15ead4ae893
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "hex.h"
4 #include "repository.h"
5 #include "run-command.h"
7 static const char *pgm;
8 static int one_shot, quiet;
9 static int err;
11 static int merge_entry(int pos, const char *path)
13 int found;
14 const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
15 char hexbuf[4][GIT_MAX_HEXSZ + 1];
16 char ownbuf[4][60];
17 struct child_process cmd = CHILD_PROCESS_INIT;
19 if (pos >= the_index.cache_nr)
20 die("git merge-index: %s not in the cache", path);
21 found = 0;
22 do {
23 const struct cache_entry *ce = the_index.cache[pos];
24 int stage = ce_stage(ce);
26 if (strcmp(ce->name, path))
27 break;
28 found++;
29 oid_to_hex_r(hexbuf[stage], &ce->oid);
30 xsnprintf(ownbuf[stage], sizeof(ownbuf[stage]), "%o", ce->ce_mode);
31 arguments[stage] = hexbuf[stage];
32 arguments[stage + 4] = ownbuf[stage];
33 } while (++pos < the_index.cache_nr);
34 if (!found)
35 die("git merge-index: %s not in the cache", path);
37 strvec_pushv(&cmd.args, arguments);
38 if (run_command(&cmd)) {
39 if (one_shot)
40 err++;
41 else {
42 if (!quiet)
43 die("merge program failed");
44 exit(1);
47 return found;
50 static void merge_one_path(const char *path)
52 int pos = index_name_pos(&the_index, path, strlen(path));
55 * If it already exists in the cache as stage0, it's
56 * already merged and there is nothing to do.
58 if (pos < 0)
59 merge_entry(-pos-1, path);
62 static void merge_all(void)
64 int i;
65 /* TODO: audit for interaction with sparse-index. */
66 ensure_full_index(&the_index);
67 for (i = 0; i < the_index.cache_nr; i++) {
68 const struct cache_entry *ce = the_index.cache[i];
69 if (!ce_stage(ce))
70 continue;
71 i += merge_entry(i, ce->name)-1;
75 int cmd_merge_index(int argc, const char **argv, const char *prefix UNUSED)
77 int i, force_file = 0;
79 /* Without this we cannot rely on waitpid() to tell
80 * what happened to our children.
82 signal(SIGCHLD, SIG_DFL);
84 if (argc < 3)
85 usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
87 repo_read_index(the_repository);
89 /* TODO: audit for interaction with sparse-index. */
90 ensure_full_index(&the_index);
92 i = 1;
93 if (!strcmp(argv[i], "-o")) {
94 one_shot = 1;
95 i++;
97 if (!strcmp(argv[i], "-q")) {
98 quiet = 1;
99 i++;
101 pgm = argv[i++];
102 for (; i < argc; i++) {
103 const char *arg = argv[i];
104 if (!force_file && *arg == '-') {
105 if (!strcmp(arg, "--")) {
106 force_file = 1;
107 continue;
109 if (!strcmp(arg, "-a")) {
110 merge_all();
111 continue;
113 die("git merge-index: unknown option %s", arg);
115 merge_one_path(arg);
117 if (err && !quiet)
118 die("merge program failed");
119 return err;