]> git.ipfire.org Git - thirdparty/git.git/blame - apply.c
git-apply: find offset fragments, and really apply them
[thirdparty/git.git] / apply.c
CommitLineData
c1bb9350
LT
1/*
2 * apply.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This applies patches on top of some (arbitrary) version of the SCM.
7 *
8 * NOTE! It does all its work in the index file, and only cares about
9 * the files in the working directory if you tell it to "merge" the
10 * patch apply.
11 *
12 * Even when merging it always takes the source from the index, and
13 * uses the working tree as a "branch" for a 3-way merge.
14 */
15#include <ctype.h>
16
17#include "cache.h"
18
19// We default to the merge behaviour, since that's what most people would
a577284a
LT
20// expect.
21//
22// --check turns on checking that the working tree matches the
23// files that are being modified, but doesn't apply the patch
24// --stat does just a diffstat, and doesn't actually apply
25// --show-files shows the directory changes
26//
c1bb9350 27static int merge_patch = 1;
3cca928d 28static int check_index = 0;
fab2c257 29static int diffstat = 0;
a577284a
LT
30static int check = 0;
31static int apply = 1;
32static int show_files = 0;
33static const char apply_usage[] = "git-apply [--stat] [--check] [--show-files] <patch>";
c1bb9350 34
3f40315a
LT
35/*
36 * For "diff-stat" like behaviour, we keep track of the biggest change
37 * we've seen, and the longest filename. That allows us to do simple
38 * scaling.
39 */
40static int max_change, max_len;
41
a4acb0eb
LT
42/*
43 * Various "current state", notably line numbers and what
44 * file (and how) we're patching right now.. The "is_xxxx"
45 * things are flags, where -1 means "don't know yet".
46 */
46979f56 47static int linenr = 1;
19c58fb8
LT
48
49struct fragment {
50 unsigned long oldpos, oldlines;
51 unsigned long newpos, newlines;
52 const char *patch;
53 int size;
54 struct fragment *next;
55};
56
57struct patch {
5041aa70 58 char *new_name, *old_name, *def_name;
19c58fb8
LT
59 unsigned int old_mode, new_mode;
60 int is_rename, is_copy, is_new, is_delete;
3f40315a 61 int lines_added, lines_deleted;
19c58fb8 62 struct fragment *fragments;
3cca928d
LT
63 const char *result;
64 unsigned long resultsize;
19c58fb8
LT
65 struct patch *next;
66};
46979f56 67
c1bb9350 68#define CHUNKSIZE (8192)
a4acb0eb 69#define SLOP (16)
c1bb9350
LT
70
71static void *read_patch_file(int fd, unsigned long *sizep)
72{
73 unsigned long size = 0, alloc = CHUNKSIZE;
74 void *buffer = xmalloc(alloc);
75
76 for (;;) {
77 int nr = alloc - size;
78 if (nr < 1024) {
79 alloc += CHUNKSIZE;
80 buffer = xrealloc(buffer, alloc);
81 nr = alloc - size;
82 }
83 nr = read(fd, buffer + size, nr);
84 if (!nr)
85 break;
86 if (nr < 0) {
87 if (errno == EAGAIN)
88 continue;
89 die("git-apply: read returned %s", strerror(errno));
90 }
91 size += nr;
92 }
93 *sizep = size;
a4acb0eb
LT
94
95 /*
96 * Make sure that we have some slop in the buffer
97 * so that we can do speculative "memcmp" etc, and
98 * see to it that it is NUL-filled.
99 */
100 if (alloc < size + SLOP)
101 buffer = xrealloc(buffer, size + SLOP);
102 memset(buffer + size, 0, SLOP);
c1bb9350
LT
103 return buffer;
104}
105
3cca928d 106static unsigned long linelen(const char *buffer, unsigned long size)
c1bb9350
LT
107{
108 unsigned long len = 0;
109 while (size--) {
110 len++;
111 if (*buffer++ == '\n')
112 break;
113 }
114 return len;
115}
116
a4acb0eb
LT
117static int is_dev_null(const char *str)
118{
119 return !memcmp("/dev/null", str, 9) && isspace(str[9]);
120}
121
381ca9a3
LT
122#define TERM_SPACE 1
123#define TERM_TAB 2
9a4a100e
LT
124
125static int name_terminate(const char *name, int namelen, int c, int terminate)
126{
127 if (c == ' ' && !(terminate & TERM_SPACE))
128 return 0;
129 if (c == '\t' && !(terminate & TERM_TAB))
130 return 0;
131
9a4a100e
LT
132 return 1;
133}
134
135static char * find_name(const char *line, char *def, int p_value, int terminate)
c1bb9350 136{
a4acb0eb
LT
137 int len;
138 const char *start = line;
139 char *name;
140
c1bb9350 141 for (;;) {
a4acb0eb 142 char c = *line;
9a4a100e
LT
143
144 if (isspace(c)) {
145 if (c == '\n')
146 break;
147 if (name_terminate(start, line-start, c, terminate))
148 break;
149 }
a4acb0eb
LT
150 line++;
151 if (c == '/' && !--p_value)
152 start = line;
153 }
154 if (!start)
155 return def;
156 len = line - start;
157 if (!len)
158 return def;
159
160 /*
161 * Generally we prefer the shorter name, especially
162 * if the other one is just a variation of that with
163 * something else tacked on to the end (ie "file.orig"
164 * or "file~").
165 */
166 if (def) {
167 int deflen = strlen(def);
168 if (deflen < len && !strncmp(start, def, deflen))
169 return def;
c1bb9350 170 }
a4acb0eb
LT
171
172 name = xmalloc(len + 1);
173 memcpy(name, start, len);
174 name[len] = 0;
175 free(def);
176 return name;
177}
178
179/*
180 * Get the name etc info from the --/+++ lines of a traditional patch header
181 *
182 * NOTE! This hardcodes "-p1" behaviour in filename detection.
9a4a100e
LT
183 *
184 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
185 * files, we can happily check the index for a match, but for creating a
186 * new file we should try to match whatever "patch" does. I have no idea.
a4acb0eb 187 */
19c58fb8 188static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
a4acb0eb
LT
189{
190 int p_value = 1;
191 char *name;
192
193 first += 4; // skip "--- "
194 second += 4; // skip "+++ "
195 if (is_dev_null(first)) {
19c58fb8
LT
196 patch->is_new = 1;
197 patch->is_delete = 0;
5041aa70 198 name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
19c58fb8 199 patch->new_name = name;
a4acb0eb 200 } else if (is_dev_null(second)) {
19c58fb8
LT
201 patch->is_new = 0;
202 patch->is_delete = 1;
381ca9a3 203 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
19c58fb8 204 patch->old_name = name;
a4acb0eb 205 } else {
381ca9a3
LT
206 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
207 name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
19c58fb8 208 patch->old_name = patch->new_name = name;
a4acb0eb
LT
209 }
210 if (!name)
211 die("unable to find filename in patch at line %d", linenr);
a4acb0eb
LT
212}
213
19c58fb8 214static int gitdiff_hdrend(const char *line, struct patch *patch)
a4acb0eb
LT
215{
216 return -1;
217}
218
1e3f6b6e
LT
219/*
220 * We're anal about diff header consistency, to make
221 * sure that we don't end up having strange ambiguous
222 * patches floating around.
223 *
224 * As a result, gitdiff_{old|new}name() will check
225 * their names against any previous information, just
226 * to make sure..
227 */
228static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
229{
230 int len;
231 const char *name;
232
233 if (!orig_name && !isnull)
234 return find_name(line, NULL, 1, 0);
235
236 name = "/dev/null";
237 len = 9;
238 if (orig_name) {
239 name = orig_name;
240 len = strlen(name);
241 if (isnull)
242 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
243 }
244
245 if (*name == '/')
246 goto absolute_path;
247
248 for (;;) {
249 char c = *line++;
250 if (c == '\n')
251 break;
252 if (c != '/')
253 continue;
254absolute_path:
255 if (memcmp(line, name, len) || line[len] != '\n')
256 break;
257 return orig_name;
258 }
259 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
260 return NULL;
261}
262
19c58fb8 263static int gitdiff_oldname(const char *line, struct patch *patch)
a4acb0eb 264{
19c58fb8 265 patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
a4acb0eb
LT
266 return 0;
267}
268
19c58fb8 269static int gitdiff_newname(const char *line, struct patch *patch)
a4acb0eb 270{
19c58fb8 271 patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
a4acb0eb
LT
272 return 0;
273}
274
19c58fb8 275static int gitdiff_oldmode(const char *line, struct patch *patch)
a4acb0eb 276{
19c58fb8 277 patch->old_mode = strtoul(line, NULL, 8);
a4acb0eb
LT
278 return 0;
279}
280
19c58fb8 281static int gitdiff_newmode(const char *line, struct patch *patch)
a4acb0eb 282{
19c58fb8 283 patch->new_mode = strtoul(line, NULL, 8);
a4acb0eb
LT
284 return 0;
285}
286
19c58fb8 287static int gitdiff_delete(const char *line, struct patch *patch)
a4acb0eb 288{
19c58fb8 289 patch->is_delete = 1;
5041aa70 290 patch->old_name = patch->def_name;
19c58fb8 291 return gitdiff_oldmode(line, patch);
a4acb0eb
LT
292}
293
19c58fb8 294static int gitdiff_newfile(const char *line, struct patch *patch)
a4acb0eb 295{
19c58fb8 296 patch->is_new = 1;
5041aa70 297 patch->new_name = patch->def_name;
19c58fb8 298 return gitdiff_newmode(line, patch);
a4acb0eb
LT
299}
300
19c58fb8 301static int gitdiff_copysrc(const char *line, struct patch *patch)
a4acb0eb 302{
19c58fb8
LT
303 patch->is_copy = 1;
304 patch->old_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
305 return 0;
306}
307
19c58fb8 308static int gitdiff_copydst(const char *line, struct patch *patch)
a4acb0eb 309{
19c58fb8
LT
310 patch->is_copy = 1;
311 patch->new_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
312 return 0;
313}
314
19c58fb8 315static int gitdiff_renamesrc(const char *line, struct patch *patch)
a4acb0eb 316{
19c58fb8
LT
317 patch->is_rename = 1;
318 patch->old_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
319 return 0;
320}
321
19c58fb8 322static int gitdiff_renamedst(const char *line, struct patch *patch)
a4acb0eb 323{
19c58fb8
LT
324 patch->is_rename = 1;
325 patch->new_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
326 return 0;
327}
328
19c58fb8 329static int gitdiff_similarity(const char *line, struct patch *patch)
a4acb0eb
LT
330{
331 return 0;
c1bb9350
LT
332}
333
70aadac0
JH
334static int gitdiff_dissimilarity(const char *line, struct patch *patch)
335{
336 return 0;
337}
338
9a4a100e
LT
339/*
340 * This is normal for a diff that doesn't change anything: we'll fall through
341 * into the next diff. Tell the parser to break out.
342 */
19c58fb8 343static int gitdiff_unrecognized(const char *line, struct patch *patch)
9a4a100e
LT
344{
345 return -1;
346}
347
5041aa70
LT
348static char *git_header_name(char *line)
349{
350 int len;
351 char *name, *second;
352
353 /*
354 * Find the first '/'
355 */
356 name = line;
357 for (;;) {
358 char c = *name++;
359 if (c == '\n')
360 return NULL;
361 if (c == '/')
362 break;
363 }
364
365 /*
366 * We don't accept absolute paths (/dev/null) as possibly valid
367 */
368 if (name == line+1)
369 return NULL;
370
371 /*
372 * Accept a name only if it shows up twice, exactly the same
373 * form.
374 */
375 for (len = 0 ; ; len++) {
376 char c = name[len];
377
378 switch (c) {
379 default:
380 continue;
381 case '\n':
382 break;
383 case '\t': case ' ':
384 second = name+len;
385 for (;;) {
386 char c = *second++;
387 if (c == '\n')
388 return NULL;
389 if (c == '/')
390 break;
391 }
0e87e048 392 if (second[len] == '\n' && !memcmp(name, second, len)) {
5041aa70
LT
393 char *ret = xmalloc(len + 1);
394 memcpy(ret, name, len);
395 ret[len] = 0;
396 return ret;
397 }
398 }
399 }
400 return NULL;
401}
402
c1bb9350 403/* Verify that we recognize the lines following a git header */
19c58fb8 404static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
c1bb9350 405{
a4acb0eb
LT
406 unsigned long offset;
407
408 /* A git diff has explicit new/delete information, so we don't guess */
19c58fb8
LT
409 patch->is_new = 0;
410 patch->is_delete = 0;
a4acb0eb 411
5041aa70
LT
412 /*
413 * Some things may not have the old name in the
414 * rest of the headers anywhere (pure mode changes,
415 * or removing or adding empty files), so we get
416 * the default name from the header.
417 */
418 patch->def_name = git_header_name(line + strlen("diff --git "));
419
a4acb0eb
LT
420 line += len;
421 size -= len;
422 linenr++;
423 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
424 static const struct opentry {
425 const char *str;
19c58fb8 426 int (*fn)(const char *, struct patch *);
a4acb0eb
LT
427 } optable[] = {
428 { "@@ -", gitdiff_hdrend },
429 { "--- ", gitdiff_oldname },
430 { "+++ ", gitdiff_newname },
431 { "old mode ", gitdiff_oldmode },
432 { "new mode ", gitdiff_newmode },
433 { "deleted file mode ", gitdiff_delete },
434 { "new file mode ", gitdiff_newfile },
435 { "copy from ", gitdiff_copysrc },
436 { "copy to ", gitdiff_copydst },
437 { "rename from ", gitdiff_renamesrc },
438 { "rename to ", gitdiff_renamedst },
439 { "similarity index ", gitdiff_similarity },
70aadac0 440 { "dissimilarity index ", gitdiff_dissimilarity },
9a4a100e 441 { "", gitdiff_unrecognized },
a4acb0eb
LT
442 };
443 int i;
c1bb9350 444
c1bb9350 445 len = linelen(line, size);
a4acb0eb 446 if (!len || line[len-1] != '\n')
c1bb9350 447 break;
a4acb0eb
LT
448 for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) {
449 const struct opentry *p = optable + i;
450 int oplen = strlen(p->str);
451 if (len < oplen || memcmp(p->str, line, oplen))
452 continue;
19c58fb8 453 if (p->fn(line + oplen, patch) < 0)
a4acb0eb 454 return offset;
9a4a100e 455 break;
a4acb0eb 456 }
c1bb9350
LT
457 }
458
a4acb0eb 459 return offset;
c1bb9350
LT
460}
461
fab2c257 462static int parse_num(const char *line, unsigned long *p)
46979f56
LT
463{
464 char *ptr;
fab2c257
LT
465
466 if (!isdigit(*line))
467 return 0;
468 *p = strtoul(line, &ptr, 10);
469 return ptr - line;
470}
471
472static int parse_range(const char *line, int len, int offset, const char *expect,
473 unsigned long *p1, unsigned long *p2)
474{
46979f56
LT
475 int digits, ex;
476
477 if (offset < 0 || offset >= len)
478 return -1;
479 line += offset;
480 len -= offset;
481
fab2c257
LT
482 digits = parse_num(line, p1);
483 if (!digits)
46979f56 484 return -1;
46979f56
LT
485
486 offset += digits;
487 line += digits;
488 len -= digits;
489
fab2c257
LT
490 *p2 = *p1;
491 if (*line == ',') {
492 digits = parse_num(line+1, p2);
493 if (!digits)
494 return -1;
495
496 offset += digits+1;
497 line += digits+1;
498 len -= digits+1;
499 }
500
46979f56
LT
501 ex = strlen(expect);
502 if (ex > len)
503 return -1;
504 if (memcmp(line, expect, ex))
505 return -1;
506
507 return offset + ex;
508}
509
510/*
511 * Parse a unified diff fragment header of the
512 * form "@@ -a,b +c,d @@"
513 */
19c58fb8 514static int parse_fragment_header(char *line, int len, struct fragment *fragment)
46979f56
LT
515{
516 int offset;
517
518 if (!len || line[len-1] != '\n')
519 return -1;
520
521 /* Figure out the number of lines in a fragment */
fab2c257
LT
522 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
523 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
46979f56
LT
524
525 return offset;
526}
527
19c58fb8 528static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
c1bb9350
LT
529{
530 unsigned long offset, len;
531
19c58fb8
LT
532 patch->is_rename = patch->is_copy = 0;
533 patch->is_new = patch->is_delete = -1;
534 patch->old_mode = patch->new_mode = 0;
535 patch->old_name = patch->new_name = NULL;
46979f56 536 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
c1bb9350
LT
537 unsigned long nextlen;
538
539 len = linelen(line, size);
540 if (!len)
541 break;
542
543 /* Testing this early allows us to take a few shortcuts.. */
544 if (len < 6)
545 continue;
46979f56
LT
546
547 /*
548 * Make sure we don't find any unconnected patch fragmants.
549 * That's a sign that we didn't find a header, and that a
550 * patch has become corrupted/broken up.
551 */
552 if (!memcmp("@@ -", line, 4)) {
19c58fb8
LT
553 struct fragment dummy;
554 if (parse_fragment_header(line, len, &dummy) < 0)
46979f56
LT
555 continue;
556 error("patch fragment without header at line %d: %.*s", linenr, len-1, line);
557 }
558
c1bb9350
LT
559 if (size < len + 6)
560 break;
561
562 /*
563 * Git patch? It might not have a real patch, just a rename
564 * or mode change, so we handle that specially
565 */
566 if (!memcmp("diff --git ", line, 11)) {
19c58fb8 567 int git_hdr_len = parse_git_header(line, len, size, patch);
c1bb9350
LT
568 if (git_hdr_len < 0)
569 continue;
5041aa70
LT
570 if (!patch->old_name && !patch->new_name)
571 die("git diff header lacks filename information");
a4acb0eb 572 *hdrsize = git_hdr_len;
c1bb9350
LT
573 return offset;
574 }
575
576 /** --- followed by +++ ? */
577 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
578 continue;
579
580 /*
581 * We only accept unified patches, so we want it to
582 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
583 * minimum
584 */
585 nextlen = linelen(line + len, size - len);
586 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
587 continue;
588
589 /* Ok, we'll consider it a patch */
19c58fb8 590 parse_traditional_patch(line, line+len, patch);
c1bb9350 591 *hdrsize = len + nextlen;
46979f56 592 linenr += 2;
c1bb9350
LT
593 return offset;
594 }
595 return -1;
596}
597
c1bb9350
LT
598/*
599 * Parse a unified diff. Note that this really needs
600 * to parse each fragment separately, since the only
601 * way to know the difference between a "---" that is
602 * part of a patch, and a "---" that starts the next
603 * patch is to look at the line counts..
604 */
19c58fb8 605static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
c1bb9350 606{
3f40315a 607 int added, deleted;
c1bb9350 608 int len = linelen(line, size), offset;
46979f56 609 unsigned long pos[4], oldlines, newlines;
c1bb9350 610
19c58fb8 611 offset = parse_fragment_header(line, len, fragment);
c1bb9350
LT
612 if (offset < 0)
613 return -1;
19c58fb8
LT
614 oldlines = fragment->oldlines;
615 newlines = fragment->newlines;
c1bb9350 616
19c58fb8
LT
617 if (patch->is_new < 0 && (pos[0] || oldlines))
618 patch->is_new = 0;
619 if (patch->is_delete < 0 && (pos[1] || newlines))
620 patch->is_delete = 0;
a4acb0eb 621
c1bb9350
LT
622 /* Parse the thing.. */
623 line += len;
624 size -= len;
46979f56 625 linenr++;
3f40315a 626 added = deleted = 0;
46979f56 627 for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
c1bb9350
LT
628 if (!oldlines && !newlines)
629 break;
630 len = linelen(line, size);
631 if (!len || line[len-1] != '\n')
632 return -1;
633 switch (*line) {
634 default:
635 return -1;
636 case ' ':
637 oldlines--;
638 newlines--;
639 break;
640 case '-':
3f40315a 641 deleted++;
c1bb9350
LT
642 oldlines--;
643 break;
644 case '+':
3f40315a 645 added++;
c1bb9350
LT
646 newlines--;
647 break;
fab2c257
LT
648 /* We allow "\ No newline at end of file" */
649 case '\\':
3cca928d
LT
650 if (len < 12 || memcmp(line, "\\ No newline", 12))
651 return -1;
fab2c257 652 break;
c1bb9350
LT
653 }
654 }
3f40315a
LT
655 patch->lines_added += added;
656 patch->lines_deleted += deleted;
c1bb9350
LT
657 return offset;
658}
659
19c58fb8 660static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
c1bb9350
LT
661{
662 unsigned long offset = 0;
19c58fb8 663 struct fragment **fragp = &patch->fragments;
c1bb9350
LT
664
665 while (size > 4 && !memcmp(line, "@@ -", 4)) {
19c58fb8
LT
666 struct fragment *fragment;
667 int len;
668
669 fragment = xmalloc(sizeof(*fragment));
670 memset(fragment, 0, sizeof(*fragment));
671 len = parse_fragment(line, size, patch, fragment);
c1bb9350 672 if (len <= 0)
46979f56 673 die("corrupt patch at line %d", linenr);
c1bb9350 674
19c58fb8
LT
675 fragment->patch = line;
676 fragment->size = len;
677
678 *fragp = fragment;
679 fragp = &fragment->next;
c1bb9350
LT
680
681 offset += len;
682 line += len;
683 size -= len;
684 }
685 return offset;
686}
687
19c58fb8 688static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
c1bb9350
LT
689{
690 int hdrsize, patchsize;
19c58fb8 691 int offset = find_header(buffer, size, &hdrsize, patch);
c1bb9350
LT
692
693 if (offset < 0)
694 return offset;
c1bb9350 695
19c58fb8 696 patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
c1bb9350
LT
697
698 return offset + hdrsize + patchsize;
699}
700
3f40315a
LT
701const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
702const char minuses[]= "----------------------------------------------------------------------";
703
704static void show_stats(struct patch *patch)
705{
706 char *name = patch->old_name;
95bedc9e 707 int len, max, add, del, total;
3f40315a 708
5041aa70 709 if (!name)
3f40315a
LT
710 name = patch->new_name;
711
712 /*
713 * "scale" the filename
714 */
715 len = strlen(name);
716 max = max_len;
717 if (max > 50)
718 max = 50;
719 if (len > max)
720 name += len - max;
721 len = max;
722
723 /*
724 * scale the add/delete
725 */
726 max = max_change;
727 if (max + len > 70)
728 max = 70 - len;
95bedc9e
LT
729
730 add = patch->lines_added;
731 del = patch->lines_deleted;
732 total = add + del;
733
734 total = (total * max + max_change / 2) / max_change;
735 add = (add * max + max_change / 2) / max_change;
736 del = total - add;
3f40315a
LT
737 printf(" %-*s |%5d %.*s%.*s\n",
738 len, name, patch->lines_added + patch->lines_deleted,
739 add, pluses, del, minuses);
740}
741
3cca928d
LT
742static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
743{
744 int fd;
745 unsigned long got;
746
747 switch (st->st_mode & S_IFMT) {
748 case S_IFLNK:
749 return readlink(path, buf, size);
750 case S_IFREG:
751 fd = open(path, O_RDONLY);
752 if (fd < 0)
753 return error("unable to open %s", path);
754 got = 0;
755 for (;;) {
756 int ret = read(fd, buf + got, size - got);
757 if (ret < 0) {
758 if (errno == EAGAIN)
759 continue;
760 break;
761 }
762 if (!ret)
763 break;
764 got += ret;
765 }
766 close(fd);
767 return got;
768
769 default:
770 return -1;
771 }
772}
773
774static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line)
775{
6e7c92a9
LT
776 int i;
777 unsigned long start, backwards, forwards;
3cca928d
LT
778
779 if (fragsize > size)
780 return -1;
781
782 start = 0;
783 if (line > 1) {
3cca928d 784 unsigned long offset = 0;
6e7c92a9
LT
785 i = line-1;
786 while (offset + fragsize <= size) {
3cca928d
LT
787 if (buf[offset++] == '\n') {
788 start = offset;
6e7c92a9 789 if (!--i)
3cca928d
LT
790 break;
791 }
792 }
793 }
794
795 /* Exact line number? */
796 if (!memcmp(buf + start, fragment, fragsize))
797 return start;
798
6e7c92a9
LT
799 /*
800 * There's probably some smart way to do this, but I'll leave
801 * that to the smart and beautiful people. I'm simple and stupid.
802 */
803 backwards = start;
804 forwards = start;
805 for (i = 0; ; i++) {
806 unsigned long try;
807 int n;
808
809 /* "backward" */
810 if (i & 1) {
811 if (!backwards) {
812 if (forwards + fragsize > size)
813 break;
814 continue;
815 }
816 do {
817 --backwards;
818 } while (backwards && buf[backwards-1] != '\n');
819 try = backwards;
820 } else {
821 while (forwards + fragsize <= size) {
822 if (buf[forwards++] == '\n')
823 break;
824 }
825 try = forwards;
826 }
827
828 if (try + fragsize > size)
829 continue;
830 if (memcmp(buf + try, fragment, fragsize))
831 continue;
832 n = (i >> 1)+1;
833 if (i & 1)
834 n = -n;
835 fprintf(stderr, "Fragment applied at offset %d\n", n);
836 return try;
837 }
838
3cca928d
LT
839 /*
840 * We should start searching forward and backward.
841 */
842 return -1;
843}
844
6e7c92a9
LT
845struct buffer_desc {
846 char *buffer;
847 unsigned long size;
848 unsigned long alloc;
849};
850
851static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
3cca928d 852{
6e7c92a9 853 char *buf = desc->buffer;
3cca928d
LT
854 const char *patch = frag->patch;
855 int offset, size = frag->size;
856 char *old = xmalloc(size);
857 char *new = xmalloc(size);
858 int oldsize = 0, newsize = 0;
859
860 while (size > 0) {
861 int len = linelen(patch, size);
862 int plen;
863
864 if (!len)
865 break;
866
867 /*
868 * "plen" is how much of the line we should use for
869 * the actual patch data. Normally we just remove the
870 * first character on the line, but if the line is
871 * followed by "\ No newline", then we also remove the
872 * last one (which is the newline, of course).
873 */
874 plen = len-1;
875 if (len > size && patch[len] == '\\')
876 plen--;
877 switch (*patch) {
878 case ' ':
879 case '-':
880 memcpy(old + oldsize, patch + 1, plen);
881 oldsize += plen;
882 if (*patch == '-')
883 break;
884 /* Fall-through for ' ' */
885 case '+':
886 memcpy(new + newsize, patch + 1, plen);
887 newsize += plen;
888 break;
889 case '@': case '\\':
890 /* Ignore it, we already handled it */
891 break;
892 default:
893 return -1;
894 }
895 patch += len;
896 size -= len;
897 }
898
6e7c92a9 899 offset = find_offset(buf, desc->size, old, oldsize, frag->newpos);
3cca928d 900 if (offset >= 0) {
6e7c92a9
LT
901 int diff = newsize - oldsize;
902 unsigned long size = desc->size + diff;
903 unsigned long alloc = desc->alloc;
904
905 if (size > alloc) {
906 alloc = size + 8192;
907 desc->alloc = alloc;
908 buf = xrealloc(buf, alloc);
909 desc->buffer = buf;
910 }
911 desc->size = size;
912 memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
913 memcpy(buf + offset, new, newsize);
3cca928d
LT
914 offset = 0;
915 }
916
917 free(old);
918 free(new);
919 return offset;
920}
921
6e7c92a9 922static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
3cca928d
LT
923{
924 struct fragment *frag = patch->fragments;
925
926 while (frag) {
6e7c92a9 927 if (apply_one_fragment(desc, frag) < 0)
3cca928d
LT
928 return error("patch failed: %s:%d", patch->old_name, frag->oldpos);
929 frag = frag->next;
930 }
931}
932
933static int apply_data(struct patch *patch, struct stat *st)
934{
6e7c92a9
LT
935 char *buf;
936 unsigned long size, alloc;
937 struct buffer_desc desc;
3cca928d
LT
938
939 if (!patch->old_name || !patch->fragments)
940 return 0;
941 size = st->st_size;
6e7c92a9
LT
942 alloc = size + 8192;
943 buf = xmalloc(alloc);
944 if (read_old_data(st, patch->old_name, buf, alloc) != size)
3cca928d 945 return error("read of %s failed", patch->old_name);
6e7c92a9
LT
946
947 desc.size = size;
948 desc.alloc = alloc;
949 desc.buffer = buf;
950 if (apply_fragments(&desc, patch) < 0)
3cca928d 951 return -1;
6e7c92a9
LT
952 patch->result = desc.buffer;
953 patch->resultsize = desc.size;
3cca928d
LT
954 return 0;
955}
956
a577284a 957static int check_patch(struct patch *patch)
fab2c257 958{
a577284a 959 struct stat st;
fab2c257
LT
960 const char *old_name = patch->old_name;
961 const char *new_name = patch->new_name;
962
963 if (old_name) {
a577284a
LT
964 int changed;
965
a577284a
LT
966 if (lstat(old_name, &st) < 0)
967 return error("%s: %s\n", strerror(errno));
3cca928d
LT
968 if (check_index) {
969 int pos = cache_name_pos(old_name, strlen(old_name));
970 if (pos < 0)
971 return error("%s: does not exist in index", old_name);
972 changed = ce_match_stat(active_cache[pos], &st);
973 if (changed)
974 return error("%s: does not match index", old_name);
975 }
976 if (patch->is_new < 0)
977 patch->is_new = 0;
a577284a
LT
978 if (!patch->old_mode)
979 patch->old_mode = st.st_mode;
3cca928d
LT
980 if ((st.st_mode ^ patch->old_mode) & S_IFMT)
981 return error("%s: wrong type", old_name);
982 if (st.st_mode != patch->old_mode)
983 fprintf(stderr, "warning: %s has type %o, expected %o\n",
984 old_name, st.st_mode, patch->old_mode);
fab2c257 985 }
a577284a 986
fab2c257 987 if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
3cca928d 988 if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
a577284a
LT
989 return error("%s: already exists in index", new_name);
990 if (!lstat(new_name, &st))
991 return error("%s: already exists in working directory", new_name);
992 if (errno != ENOENT)
993 return error("%s: %s", new_name, strerror(errno));
fab2c257 994 }
3cca928d
LT
995
996 if (new_name && old_name) {
997 int same = !strcmp(old_name, new_name);
998 if (!patch->new_mode)
999 patch->new_mode = patch->old_mode;
1000 if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
1001 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1002 patch->new_mode, new_name, patch->old_mode,
1003 same ? "" : " of ", same ? "" : old_name);
1004 }
1005
1006 if (apply_data(patch, &st) < 0)
1007 return error("%s: patch does not apply", old_name);
a577284a 1008 return 0;
fab2c257
LT
1009}
1010
a577284a 1011static int check_patch_list(struct patch *patch)
19c58fb8 1012{
a577284a
LT
1013 int error = 0;
1014
1015 for (;patch ; patch = patch->next)
1016 error |= check_patch(patch);
1017 return error;
1018}
1019
1020static void show_file(int c, unsigned int mode, const char *name)
1021{
1022 printf("%c %o %s\n", c, mode, name);
1023}
3f40315a 1024
a577284a
LT
1025static void show_file_list(struct patch *patch)
1026{
1027 for (;patch ; patch = patch->next) {
1028 if (patch->is_rename) {
1029 show_file('-', patch->old_mode, patch->old_name);
1030 show_file('+', patch->new_mode, patch->new_name);
1031 continue;
1032 }
1033 if (patch->is_copy || patch->is_new) {
1034 show_file('+', patch->new_mode, patch->new_name);
1035 continue;
1036 }
1037 if (patch->is_delete) {
1038 show_file('-', patch->old_mode, patch->old_name);
1039 continue;
1040 }
1041 if (patch->old_mode && patch->new_mode && patch->old_mode != patch->new_mode) {
1042 printf("M %o:%o %s\n", patch->old_mode, patch->new_mode, patch->old_name);
1043 continue;
19c58fb8 1044 }
a577284a
LT
1045 printf("M %o %s\n", patch->old_mode, patch->old_name);
1046 }
1047}
fab2c257 1048
a577284a
LT
1049static void stat_patch_list(struct patch *patch)
1050{
1051 int files, adds, dels;
1052
1053 for (files = adds = dels = 0 ; patch ; patch = patch->next) {
1054 files++;
1055 adds += patch->lines_added;
1056 dels += patch->lines_deleted;
1057 show_stats(patch);
1058 }
1059
1060 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
3f40315a
LT
1061}
1062
1063static void patch_stats(struct patch *patch)
1064{
1065 int lines = patch->lines_added + patch->lines_deleted;
1066
1067 if (lines > max_change)
1068 max_change = lines;
1069 if (patch->old_name) {
1070 int len = strlen(patch->old_name);
1071 if (len > max_len)
1072 max_len = len;
1073 }
1074 if (patch->new_name) {
1075 int len = strlen(patch->new_name);
1076 if (len > max_len)
1077 max_len = len;
1078 }
19c58fb8
LT
1079}
1080
c1bb9350
LT
1081static int apply_patch(int fd)
1082{
1083 unsigned long offset, size;
1084 char *buffer = read_patch_file(fd, &size);
19c58fb8 1085 struct patch *list = NULL, **listp = &list;
c1bb9350
LT
1086
1087 if (!buffer)
1088 return -1;
1089 offset = 0;
1090 while (size > 0) {
19c58fb8
LT
1091 struct patch *patch;
1092 int nr;
1093
1094 patch = xmalloc(sizeof(*patch));
1095 memset(patch, 0, sizeof(*patch));
1096 nr = parse_chunk(buffer + offset, size, patch);
c1bb9350
LT
1097 if (nr < 0)
1098 break;
3f40315a 1099 patch_stats(patch);
19c58fb8
LT
1100 *listp = patch;
1101 listp = &patch->next;
c1bb9350
LT
1102 offset += nr;
1103 size -= nr;
1104 }
19c58fb8 1105
a577284a
LT
1106 if ((check || apply) && check_patch_list(list) < 0)
1107 exit(1);
1108
1109 if (show_files)
1110 show_file_list(list);
1111
1112 if (diffstat)
1113 stat_patch_list(list);
19c58fb8 1114
c1bb9350
LT
1115 free(buffer);
1116 return 0;
1117}
1118
1119int main(int argc, char **argv)
1120{
1121 int i;
4dfdbe10 1122 int read_stdin = 1;
c1bb9350
LT
1123
1124 if (read_cache() < 0)
1125 die("unable to read index file");
1126
1127 for (i = 1; i < argc; i++) {
1128 const char *arg = argv[i];
1129 int fd;
1130
1131 if (!strcmp(arg, "-")) {
1132 apply_patch(0);
4dfdbe10 1133 read_stdin = 0;
c1bb9350
LT
1134 continue;
1135 }
1136 if (!strcmp(arg, "--no-merge")) {
1137 merge_patch = 0;
1138 continue;
1139 }
fab2c257 1140 if (!strcmp(arg, "--stat")) {
a577284a 1141 apply = 0;
fab2c257
LT
1142 diffstat = 1;
1143 continue;
1144 }
a577284a
LT
1145 if (!strcmp(arg, "--check")) {
1146 apply = 0;
1147 check = 1;
1148 continue;
1149 }
3cca928d
LT
1150 if (!strcmp(arg, "--index")) {
1151 check_index = 1;
1152 continue;
1153 }
a577284a
LT
1154 if (!strcmp(arg, "--show-files")) {
1155 show_files = 1;
1156 continue;
1157 }
c1bb9350
LT
1158 fd = open(arg, O_RDONLY);
1159 if (fd < 0)
1160 usage(apply_usage);
4dfdbe10 1161 read_stdin = 0;
c1bb9350
LT
1162 apply_patch(fd);
1163 close(fd);
1164 }
4dfdbe10
LT
1165 if (read_stdin)
1166 apply_patch(0);
c1bb9350
LT
1167 return 0;
1168}