1 #define USE_THE_INDEX_VARIABLE
4 #include "repository.h"
5 #include "run-command.h"
7 static const char *pgm
;
8 static int one_shot
, quiet
;
11 static int merge_entry(int pos
, const char *path
)
14 const char *arguments
[] = { pgm
, "", "", "", path
, "", "", "", NULL
};
15 char hexbuf
[4][GIT_MAX_HEXSZ
+ 1];
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
);
23 const struct cache_entry
*ce
= the_index
.cache
[pos
];
24 int stage
= ce_stage(ce
);
26 if (strcmp(ce
->name
, path
))
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
);
35 die("git merge-index: %s not in the cache", path
);
37 strvec_pushv(&cmd
.args
, arguments
);
38 if (run_command(&cmd
)) {
43 die("merge program failed");
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.
59 merge_entry(-pos
-1, path
);
62 static void merge_all(void)
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
];
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
);
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
);
93 if (!strcmp(argv
[i
], "-o")) {
97 if (!strcmp(argv
[i
], "-q")) {
102 for (; i
< argc
; i
++) {
103 const char *arg
= argv
[i
];
104 if (!force_file
&& *arg
== '-') {
105 if (!strcmp(arg
, "--")) {
109 if (!strcmp(arg
, "-a")) {
113 die("git merge-index: unknown option %s", arg
);
118 die("merge program failed");