git

Форк
0
/
merge-index.c 
121 строка · 2.7 Кб
1
#include "builtin.h"
2
#include "hex.h"
3
#include "read-cache-ll.h"
4
#include "repository.h"
5
#include "run-command.h"
6
#include "sparse-index.h"
7

8
static const char *pgm;
9
static int one_shot, quiet;
10
static int err;
11

12
static int merge_entry(int pos, const char *path)
13
{
14
	int found;
15
	const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
16
	char hexbuf[4][GIT_MAX_HEXSZ + 1];
17
	char ownbuf[4][60];
18
	struct child_process cmd = CHILD_PROCESS_INIT;
19

20
	if (pos >= the_repository->index->cache_nr)
21
		die("git merge-index: %s not in the cache", path);
22
	found = 0;
23
	do {
24
		const struct cache_entry *ce = the_repository->index->cache[pos];
25
		int stage = ce_stage(ce);
26

27
		if (strcmp(ce->name, path))
28
			break;
29
		found++;
30
		oid_to_hex_r(hexbuf[stage], &ce->oid);
31
		xsnprintf(ownbuf[stage], sizeof(ownbuf[stage]), "%o", ce->ce_mode);
32
		arguments[stage] = hexbuf[stage];
33
		arguments[stage + 4] = ownbuf[stage];
34
	} while (++pos < the_repository->index->cache_nr);
35
	if (!found)
36
		die("git merge-index: %s not in the cache", path);
37

38
	strvec_pushv(&cmd.args, arguments);
39
	if (run_command(&cmd)) {
40
		if (one_shot)
41
			err++;
42
		else {
43
			if (!quiet)
44
				die("merge program failed");
45
			exit(1);
46
		}
47
	}
48
	return found;
49
}
50

51
static void merge_one_path(const char *path)
52
{
53
	int pos = index_name_pos(the_repository->index, path, strlen(path));
54

55
	/*
56
	 * If it already exists in the cache as stage0, it's
57
	 * already merged and there is nothing to do.
58
	 */
59
	if (pos < 0)
60
		merge_entry(-pos-1, path);
61
}
62

63
static void merge_all(void)
64
{
65
	int i;
66
	/* TODO: audit for interaction with sparse-index. */
67
	ensure_full_index(the_repository->index);
68
	for (i = 0; i < the_repository->index->cache_nr; i++) {
69
		const struct cache_entry *ce = the_repository->index->cache[i];
70
		if (!ce_stage(ce))
71
			continue;
72
		i += merge_entry(i, ce->name)-1;
73
	}
74
}
75

76
int cmd_merge_index(int argc, const char **argv, const char *prefix UNUSED)
77
{
78
	int i, force_file = 0;
79

80
	/* Without this we cannot rely on waitpid() to tell
81
	 * what happened to our children.
82
	 */
83
	signal(SIGCHLD, SIG_DFL);
84

85
	if (argc < 3)
86
		usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
87

88
	repo_read_index(the_repository);
89

90
	/* TODO: audit for interaction with sparse-index. */
91
	ensure_full_index(the_repository->index);
92

93
	i = 1;
94
	if (!strcmp(argv[i], "-o")) {
95
		one_shot = 1;
96
		i++;
97
	}
98
	if (!strcmp(argv[i], "-q")) {
99
		quiet = 1;
100
		i++;
101
	}
102
	pgm = argv[i++];
103
	for (; i < argc; i++) {
104
		const char *arg = argv[i];
105
		if (!force_file && *arg == '-') {
106
			if (!strcmp(arg, "--")) {
107
				force_file = 1;
108
				continue;
109
			}
110
			if (!strcmp(arg, "-a")) {
111
				merge_all();
112
				continue;
113
			}
114
			die("git merge-index: unknown option %s", arg);
115
		}
116
		merge_one_path(arg);
117
	}
118
	if (err && !quiet)
119
		die("merge program failed");
120
	return err;
121
}
122

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.