summaryrefslogtreecommitdiff
path: root/apply.c
blob: 85d7965daa09d01f05a65f5bdde8d889fd21744b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * apply.c
 *
 * Copyright (C) Linus Torvalds, 2005
 *
 * This applies patches on top of some (arbitrary) version of the SCM.
 *
 * NOTE! It does all its work in the index file, and only cares about
 * the files in the working directory if you tell it to "merge" the
 * patch apply.
 *
 * Even when merging it always takes the source from the index, and
 * uses the working tree as a "branch" for a 3-way merge.
 */
#include <ctype.h>
 
#include "cache.h"
 
// We default to the merge behaviour, since that's what most people would
// expect
static int merge_patch = 1;
static const char apply_usage[] = "git-apply <patch>";
 
#define CHUNKSIZE (8192)
 
static void *read_patch_file(int fd, unsigned long *sizep)
{
	unsigned long size = 0, alloc = CHUNKSIZE;
	void *buffer = xmalloc(alloc);
 
	for (;;) {
		int nr = alloc - size;
		if (nr < 1024) {
			alloc += CHUNKSIZE;
			buffer = xrealloc(buffer, alloc);
			nr = alloc - size;
		}
		nr = read(fd, buffer + size, nr);
		if (!nr)
			break;
		if (nr < 0) {
			if (errno == EAGAIN)
				continue;
			die("git-apply: read returned %s", strerror(errno));
		}
		size += nr;
	}
	*sizep = size;
	return buffer;
}
 
static unsigned long linelen(char *buffer, unsigned long size)
{
	unsigned long len = 0;
	while (size--) {
		len++;
		if (*buffer++ == '\n')
			break;
	}
	return len;
}
 
static int match_word(const char *line, const char *match)
{
	for (;;) {
		char c = *match++;
		if (!c)
			break;
		if (*line++ != c)
			return 0;
	}
	return *line == ' ';
}
 
/* Verify that we recognize the lines following a git header */
static int parse_git_header(char *line, unsigned int size)
{
	unsigned long offset, len;
 
	for (offset = 0 ; size > 0 ; offset += len, size -= len, line += len) {
		len = linelen(line, size);
		if (!len)
			break;
		if (line[len-1] != '\n')
			return -1;
		if (len < 4)
			break;
		if (!memcmp(line, "@@ -", 4))
			return offset;
		if (match_word(line, "new file mode"))
			continue;
		if (match_word(line, "deleted file mode"))
			continue;
		if (match_word(line, "copy"))
			continue;
		if (match_word(line, "rename"))
			continue;
		if (match_word(line, "similarity index"))
			continue;
		break;
	}
 
	/* We want either a patch _or_ something real */
	return offset ? :-1;
}
 
static int find_header(char *line, unsigned long size, int *hdrsize)
{
	unsigned long offset, len;
 
	for (offset = 0; size > 0; offset += len, size -= len, line += len) {
		unsigned long nextlen;
 
		len = linelen(line, size);
		if (!len)
			break;
 
		/* Testing this early allows us to take a few shortcuts.. */
		if (len < 6)
			continue;
		if (size < len + 6)
			break;
 
		/*
		 * Git patch? It might not have a real patch, just a rename
		 * or mode change, so we handle that specially
		 */
		if (!memcmp("diff --git ", line, 11)) {
			int git_hdr_len = parse_git_header(line + len, size - len);
			if (git_hdr_len < 0)
				continue;
 
			*hdrsize = len + git_hdr_len;
			return offset;
		}
 
		/** --- followed by +++ ? */
		if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
			continue;
 
		/*
		 * We only accept unified patches, so we want it to
		 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
		 * minimum
		 */
		nextlen = linelen(line + len, size - len);
		if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
			continue;
 
		/* Ok, we'll consider it a patch */
		*hdrsize = len + nextlen;
		return offset;
	}
	return -1;
}
 
static int parse_num(const char *line, int len, int offset, const char *expect, unsigned long *p)
{
	char *ptr;
	int digits, ex;
 
	if (offset < 0 || offset >= len)
		return -1;
	line += offset;
	len -= offset;
 
	if (!isdigit(*line))
		return -1;
	*p = strtoul(line, &ptr, 10);
 
	digits = ptr - line;
 
	offset += digits;
	line += digits;
	len -= digits;
 
	ex = strlen(expect);
	if (ex > len)
		return -1;
	if (memcmp(line, expect, ex))
		return -1;
 
	return offset + ex;
}
 
/*
 * Parse a unified diff. Note that this really needs
 * to parse each fragment separately, since the only
 * way to know the difference between a "---" that is
 * part of a patch, and a "---" that starts the next
 * patch is to look at the line counts..
 */
static int apply_fragment(char *line, unsigned long size)
{
	int len = linelen(line, size), offset;
	unsigned long oldpos, oldlines, newpos, newlines;
 
	if (!len || line[len-1] != '\n')
		return -1;
 
	/* Figure out the number of lines in a fragment */
	offset = parse_num(line, len, 4, ",", &oldpos);
	offset = parse_num(line, len, offset, " +", &oldlines);
	offset = parse_num(line, len, offset, ",", &newpos);
	offset = parse_num(line, len, offset, " @@", &newlines);
	if (offset < 0)
		return -1;
 
	/* Parse the thing.. */
	line += len;
	size -= len;
	for (offset = len; size > 0; offset += len, size -= len, line += len) {
		if (!oldlines && !newlines)
			break;
		len = linelen(line, size);
		if (!len || line[len-1] != '\n')
			return -1;
		switch (*line) {
		default:
			return -1;
		case ' ':
			oldlines--;
			newlines--;
			break;
		case '-':
			oldlines--;
			break;
		case '+':
			newlines--;
			break;
		}
	}
	return offset;
}
 
static int apply_single_patch(char *line, unsigned long size)
{
	unsigned long offset = 0;
 
	while (size > 4 && !memcmp(line, "@@ -", 4)) {
		int len = apply_fragment(line, size);
		if (len <= 0)
			break;
 
printf("applying fragment:\n%.*s\n\n", len, line);
 
		offset += len;
		line += len;
		size -= len;
	}
	return offset;
}
 
static int apply_chunk(char *buffer, unsigned long size)
{
	int hdrsize, patchsize;
	int offset = find_header(buffer, size, &hdrsize);
	char *header, *patch;
 
	if (offset < 0)
		return offset;
	header = buffer + offset;
 
printf("Found header:\n%.*s\n\n", hdrsize, header);
 
	patch = header + hdrsize;
	patchsize = apply_single_patch(patch, size - offset - hdrsize);
 
	return offset + hdrsize + patchsize;
}
 
static int apply_patch(int fd)
{
	unsigned long offset, size;
	char *buffer = read_patch_file(fd, &size);
 
	if (!buffer)
		return -1;
	offset = 0;
	while (size > 0) {
		int nr = apply_chunk(buffer + offset, size);
		if (nr < 0)
			break;
		offset += nr;
		size -= nr;
	}
	free(buffer);
	return 0;
}
 
int main(int argc, char **argv)
{
	int i;
 
	if (read_cache() < 0)
		die("unable to read index file");
 
	for (i = 1; i < argc; i++) {
		const char *arg = argv[i];
		int fd;
 
		if (!strcmp(arg, "-")) {
			apply_patch(0);
			continue;
		}
		if (!strcmp(arg, "--no-merge")) {
			merge_patch = 0;
			continue;
		}
		fd = open(arg, O_RDONLY);
		if (fd < 0)
			usage(apply_usage);
		apply_patch(fd);
		close(fd);
	}
	return 0;
}