1 #define USE_THE_INDEX_VARIABLE
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
;
13 static int merge_entry(int pos
, const char *path
)
16 const char *arguments
[] = { pgm
, "", "", "", path
, "", "", "", NULL
};
17 char hexbuf
[4][GIT_MAX_HEXSZ
+ 1];
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
);
25 const struct cache_entry
*ce
= the_index
.cache
[pos
];
26 int stage
= ce_stage(ce
);
28 if (strcmp(ce
->name
, path
))
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
);
37 die("git merge-index: %s not in the cache", path
);
39 strvec_pushv(&cmd
.args
, arguments
);
40 if (run_command(&cmd
)) {
45 die("merge program failed");
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.
61 merge_entry(-pos
-1, path
);
64 static void merge_all(void)
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
];
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
);
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
);
95 if (!strcmp(argv
[i
], "-o")) {
99 if (!strcmp(argv
[i
], "-q")) {
104 for (; i
< argc
; i
++) {
105 const char *arg
= argv
[i
];
106 if (!force_file
&& *arg
== '-') {
107 if (!strcmp(arg
, "--")) {
111 if (!strcmp(arg
, "-a")) {
115 die("git merge-index: unknown option %s", arg
);
120 die("merge program failed");