4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
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
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.
19 // We default to the merge behaviour, since that's what most people would
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
27 static int merge_patch
= 1;
28 static int check_index
= 0;
29 static int write_index
= 0;
30 static int diffstat
= 0;
31 static int summary
= 0;
34 static int show_files
= 0;
35 static const char apply_usage
[] =
36 "git-apply [--no-merge] [--stat] [--summary] [--check] [--index] [--apply] [--show-files] <patch>...";
39 * For "diff-stat" like behaviour, we keep track of the biggest change
40 * we've seen, and the longest filename. That allows us to do simple
43 static int max_change
, max_len
;
46 * Various "current state", notably line numbers and what
47 * file (and how) we're patching right now.. The "is_xxxx"
48 * things are flags, where -1 means "don't know yet".
50 static int linenr
= 1;
53 unsigned long oldpos
, oldlines
;
54 unsigned long newpos
, newlines
;
57 struct fragment
*next
;
61 char *new_name
, *old_name
, *def_name
;
62 unsigned int old_mode
, new_mode
;
63 int is_rename
, is_copy
, is_new
, is_delete
;
64 int lines_added
, lines_deleted
;
66 struct fragment
*fragments
;
68 unsigned long resultsize
;
72 #define CHUNKSIZE (8192)
75 static void *read_patch_file(int fd
, unsigned long *sizep
)
77 unsigned long size
= 0, alloc
= CHUNKSIZE
;
78 void *buffer
= xmalloc(alloc
);
81 int nr
= alloc
- size
;
84 buffer
= xrealloc(buffer
, alloc
);
87 nr
= read(fd
, buffer
+ size
, nr
);
93 die("git-apply: read returned %s", strerror(errno
));
100 * Make sure that we have some slop in the buffer
101 * so that we can do speculative "memcmp" etc, and
102 * see to it that it is NUL-filled.
104 if (alloc
< size
+ SLOP
)
105 buffer
= xrealloc(buffer
, size
+ SLOP
);
106 memset(buffer
+ size
, 0, SLOP
);
110 static unsigned long linelen(const char *buffer
, unsigned long size
)
112 unsigned long len
= 0;
115 if (*buffer
++ == '\n')
121 static int is_dev_null(const char *str
)
123 return !memcmp("/dev/null", str
, 9) && isspace(str
[9]);
129 static int name_terminate(const char *name
, int namelen
, int c
, int terminate
)
131 if (c
== ' ' && !(terminate
& TERM_SPACE
))
133 if (c
== '\t' && !(terminate
& TERM_TAB
))
139 static char * find_name(const char *line
, char *def
, int p_value
, int terminate
)
142 const char *start
= line
;
151 if (name_terminate(start
, line
-start
, c
, terminate
))
155 if (c
== '/' && !--p_value
)
165 * Generally we prefer the shorter name, especially
166 * if the other one is just a variation of that with
167 * something else tacked on to the end (ie "file.orig"
171 int deflen
= strlen(def
);
172 if (deflen
< len
&& !strncmp(start
, def
, deflen
))
176 name
= xmalloc(len
+ 1);
177 memcpy(name
, start
, len
);
184 * Get the name etc info from the --/+++ lines of a traditional patch header
186 * NOTE! This hardcodes "-p1" behaviour in filename detection.
188 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
189 * files, we can happily check the index for a match, but for creating a
190 * new file we should try to match whatever "patch" does. I have no idea.
192 static void parse_traditional_patch(const char *first
, const char *second
, struct patch
*patch
)
197 first
+= 4; // skip "--- "
198 second
+= 4; // skip "+++ "
199 if (is_dev_null(first
)) {
201 patch
->is_delete
= 0;
202 name
= find_name(second
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
203 patch
->new_name
= name
;
204 } else if (is_dev_null(second
)) {
206 patch
->is_delete
= 1;
207 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
208 patch
->old_name
= name
;
210 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
211 name
= find_name(second
, name
, p_value
, TERM_SPACE
| TERM_TAB
);
212 patch
->old_name
= patch
->new_name
= name
;
215 die("unable to find filename in patch at line %d", linenr
);
218 static int gitdiff_hdrend(const char *line
, struct patch
*patch
)
224 * We're anal about diff header consistency, to make
225 * sure that we don't end up having strange ambiguous
226 * patches floating around.
228 * As a result, gitdiff_{old|new}name() will check
229 * their names against any previous information, just
232 static char *gitdiff_verify_name(const char *line
, int isnull
, char *orig_name
, const char *oldnew
)
237 if (!orig_name
&& !isnull
)
238 return find_name(line
, NULL
, 1, 0);
246 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name
, linenr
);
259 if (memcmp(line
, name
, len
) || line
[len
] != '\n')
263 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew
, linenr
);
267 static int gitdiff_oldname(const char *line
, struct patch
*patch
)
269 patch
->old_name
= gitdiff_verify_name(line
, patch
->is_new
, patch
->old_name
, "old");
273 static int gitdiff_newname(const char *line
, struct patch
*patch
)
275 patch
->new_name
= gitdiff_verify_name(line
, patch
->is_delete
, patch
->new_name
, "new");
279 static int gitdiff_oldmode(const char *line
, struct patch
*patch
)
281 patch
->old_mode
= strtoul(line
, NULL
, 8);
285 static int gitdiff_newmode(const char *line
, struct patch
*patch
)
287 patch
->new_mode
= strtoul(line
, NULL
, 8);
291 static int gitdiff_delete(const char *line
, struct patch
*patch
)
293 patch
->is_delete
= 1;
294 patch
->old_name
= patch
->def_name
;
295 return gitdiff_oldmode(line
, patch
);
298 static int gitdiff_newfile(const char *line
, struct patch
*patch
)
301 patch
->new_name
= patch
->def_name
;
302 return gitdiff_newmode(line
, patch
);
305 static int gitdiff_copysrc(const char *line
, struct patch
*patch
)
308 patch
->old_name
= find_name(line
, NULL
, 0, 0);
312 static int gitdiff_copydst(const char *line
, struct patch
*patch
)
315 patch
->new_name
= find_name(line
, NULL
, 0, 0);
319 static int gitdiff_renamesrc(const char *line
, struct patch
*patch
)
321 patch
->is_rename
= 1;
322 patch
->old_name
= find_name(line
, NULL
, 0, 0);
326 static int gitdiff_renamedst(const char *line
, struct patch
*patch
)
328 patch
->is_rename
= 1;
329 patch
->new_name
= find_name(line
, NULL
, 0, 0);
333 static int gitdiff_similarity(const char *line
, struct patch
*patch
)
335 if ((patch
->score
= strtoul(line
, NULL
, 10)) == ULONG_MAX
)
340 static int gitdiff_dissimilarity(const char *line
, struct patch
*patch
)
342 if ((patch
->score
= strtoul(line
, NULL
, 10)) == ULONG_MAX
)
348 * This is normal for a diff that doesn't change anything: we'll fall through
349 * into the next diff. Tell the parser to break out.
351 static int gitdiff_unrecognized(const char *line
, struct patch
*patch
)
356 static char *git_header_name(char *line
)
374 * We don't accept absolute paths (/dev/null) as possibly valid
380 * Accept a name only if it shows up twice, exactly the same
383 for (len
= 0 ; ; len
++) {
400 if (second
[len
] == '\n' && !memcmp(name
, second
, len
)) {
401 char *ret
= xmalloc(len
+ 1);
402 memcpy(ret
, name
, len
);
411 /* Verify that we recognize the lines following a git header */
412 static int parse_git_header(char *line
, int len
, unsigned int size
, struct patch
*patch
)
414 unsigned long offset
;
416 /* A git diff has explicit new/delete information, so we don't guess */
418 patch
->is_delete
= 0;
421 * Some things may not have the old name in the
422 * rest of the headers anywhere (pure mode changes,
423 * or removing or adding empty files), so we get
424 * the default name from the header.
426 patch
->def_name
= git_header_name(line
+ strlen("diff --git "));
431 for (offset
= len
; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
432 static const struct opentry
{
434 int (*fn
)(const char *, struct patch
*);
436 { "@@ -", gitdiff_hdrend
},
437 { "--- ", gitdiff_oldname
},
438 { "+++ ", gitdiff_newname
},
439 { "old mode ", gitdiff_oldmode
},
440 { "new mode ", gitdiff_newmode
},
441 { "deleted file mode ", gitdiff_delete
},
442 { "new file mode ", gitdiff_newfile
},
443 { "copy from ", gitdiff_copysrc
},
444 { "copy to ", gitdiff_copydst
},
445 { "rename old ", gitdiff_renamesrc
},
446 { "rename new ", gitdiff_renamedst
},
447 { "rename from ", gitdiff_renamesrc
},
448 { "rename to ", gitdiff_renamedst
},
449 { "similarity index ", gitdiff_similarity
},
450 { "dissimilarity index ", gitdiff_dissimilarity
},
451 { "", gitdiff_unrecognized
},
455 len
= linelen(line
, size
);
456 if (!len
|| line
[len
-1] != '\n')
458 for (i
= 0; i
< sizeof(optable
) / sizeof(optable
[0]); i
++) {
459 const struct opentry
*p
= optable
+ i
;
460 int oplen
= strlen(p
->str
);
461 if (len
< oplen
|| memcmp(p
->str
, line
, oplen
))
463 if (p
->fn(line
+ oplen
, patch
) < 0)
472 static int parse_num(const char *line
, unsigned long *p
)
478 *p
= strtoul(line
, &ptr
, 10);
482 static int parse_range(const char *line
, int len
, int offset
, const char *expect
,
483 unsigned long *p1
, unsigned long *p2
)
487 if (offset
< 0 || offset
>= len
)
492 digits
= parse_num(line
, p1
);
502 digits
= parse_num(line
+1, p2
);
514 if (memcmp(line
, expect
, ex
))
521 * Parse a unified diff fragment header of the
522 * form "@@ -a,b +c,d @@"
524 static int parse_fragment_header(char *line
, int len
, struct fragment
*fragment
)
528 if (!len
|| line
[len
-1] != '\n')
531 /* Figure out the number of lines in a fragment */
532 offset
= parse_range(line
, len
, 4, " +", &fragment
->oldpos
, &fragment
->oldlines
);
533 offset
= parse_range(line
, len
, offset
, " @@", &fragment
->newpos
, &fragment
->newlines
);
538 static int find_header(char *line
, unsigned long size
, int *hdrsize
, struct patch
*patch
)
540 unsigned long offset
, len
;
542 patch
->is_rename
= patch
->is_copy
= 0;
543 patch
->is_new
= patch
->is_delete
= -1;
544 patch
->old_mode
= patch
->new_mode
= 0;
545 patch
->old_name
= patch
->new_name
= NULL
;
546 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
547 unsigned long nextlen
;
549 len
= linelen(line
, size
);
553 /* Testing this early allows us to take a few shortcuts.. */
558 * Make sure we don't find any unconnected patch fragmants.
559 * That's a sign that we didn't find a header, and that a
560 * patch has become corrupted/broken up.
562 if (!memcmp("@@ -", line
, 4)) {
563 struct fragment dummy
;
564 if (parse_fragment_header(line
, len
, &dummy
) < 0)
566 error("patch fragment without header at line %d: %.*s", linenr
, (int)len
-1, line
);
573 * Git patch? It might not have a real patch, just a rename
574 * or mode change, so we handle that specially
576 if (!memcmp("diff --git ", line
, 11)) {
577 int git_hdr_len
= parse_git_header(line
, len
, size
, patch
);
578 if (git_hdr_len
<= len
)
580 if (!patch
->old_name
&& !patch
->new_name
) {
581 if (!patch
->def_name
)
582 die("git diff header lacks filename information (line %d)", linenr
);
583 patch
->old_name
= patch
->new_name
= patch
->def_name
;
585 *hdrsize
= git_hdr_len
;
589 /** --- followed by +++ ? */
590 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
594 * We only accept unified patches, so we want it to
595 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
598 nextlen
= linelen(line
+ len
, size
- len
);
599 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
602 /* Ok, we'll consider it a patch */
603 parse_traditional_patch(line
, line
+len
, patch
);
604 *hdrsize
= len
+ nextlen
;
612 * Parse a unified diff. Note that this really needs
613 * to parse each fragment separately, since the only
614 * way to know the difference between a "---" that is
615 * part of a patch, and a "---" that starts the next
616 * patch is to look at the line counts..
618 static int parse_fragment(char *line
, unsigned long size
, struct patch
*patch
, struct fragment
*fragment
)
621 int len
= linelen(line
, size
), offset
;
622 unsigned long oldlines
, newlines
;
624 offset
= parse_fragment_header(line
, len
, fragment
);
627 oldlines
= fragment
->oldlines
;
628 newlines
= fragment
->newlines
;
630 if (patch
->is_new
< 0) {
631 patch
->is_new
= !oldlines
;
633 patch
->old_name
= NULL
;
635 if (patch
->is_delete
< 0) {
636 patch
->is_delete
= !newlines
;
638 patch
->new_name
= NULL
;
641 if (patch
->is_new
!= !oldlines
)
642 return error("new file depends on old contents");
643 if (patch
->is_delete
!= !newlines
) {
645 return error("deleted file still has contents");
646 fprintf(stderr
, "** warning: file %s becomes empty but is not deleted\n", patch
->new_name
);
649 /* Parse the thing.. */
654 for (offset
= len
; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
655 if (!oldlines
&& !newlines
)
657 len
= linelen(line
, size
);
658 if (!len
|| line
[len
-1] != '\n')
676 /* We allow "\ No newline at end of file". Depending
677 * on locale settings when the patch was produced we
678 * don't know what this line looks like. The only
679 * thing we do know is that it begins with "\ ".
680 * Checking for 12 is just for sanity check -- any
681 * l10n of "\ No newline..." is at least that long.
684 if (len
< 12 || memcmp(line
, "\\ ", 2))
689 /* If a fragment ends with an incomplete line, we failed to include
690 * it in the above loop because we hit oldlines == newlines == 0
693 if (12 < size
&& !memcmp(line
, "\\ ", 2))
694 offset
+= linelen(line
, size
);
696 patch
->lines_added
+= added
;
697 patch
->lines_deleted
+= deleted
;
701 static int parse_single_patch(char *line
, unsigned long size
, struct patch
*patch
)
703 unsigned long offset
= 0;
704 struct fragment
**fragp
= &patch
->fragments
;
706 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
707 struct fragment
*fragment
;
710 fragment
= xmalloc(sizeof(*fragment
));
711 memset(fragment
, 0, sizeof(*fragment
));
712 len
= parse_fragment(line
, size
, patch
, fragment
);
714 die("corrupt patch at line %d", linenr
);
716 fragment
->patch
= line
;
717 fragment
->size
= len
;
720 fragp
= &fragment
->next
;
729 static inline int metadata_changes(struct patch
*patch
)
731 return patch
->is_rename
> 0 ||
732 patch
->is_copy
> 0 ||
735 (patch
->old_mode
&& patch
->new_mode
&&
736 patch
->old_mode
!= patch
->new_mode
);
739 static int parse_chunk(char *buffer
, unsigned long size
, struct patch
*patch
)
741 int hdrsize
, patchsize
;
742 int offset
= find_header(buffer
, size
, &hdrsize
, patch
);
747 patchsize
= parse_single_patch(buffer
+ offset
+ hdrsize
, size
- offset
- hdrsize
, patch
);
749 if (!patchsize
&& !metadata_changes(patch
))
750 die("patch with only garbage at line %d", linenr
);
752 return offset
+ hdrsize
+ patchsize
;
755 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
756 static const char minuses
[]= "----------------------------------------------------------------------";
758 static void show_stats(struct patch
*patch
)
760 const char *prefix
= "";
761 char *name
= patch
->new_name
;
762 int len
, max
, add
, del
, total
;
765 name
= patch
->old_name
;
768 * "scale" the filename
779 slash
= strchr(name
, '/');
786 * scale the add/delete
792 add
= patch
->lines_added
;
793 del
= patch
->lines_deleted
;
796 if (max_change
> 0) {
797 total
= (total
* max
+ max_change
/ 2) / max_change
;
798 add
= (add
* max
+ max_change
/ 2) / max_change
;
801 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
802 len
, name
, patch
->lines_added
+ patch
->lines_deleted
,
803 add
, pluses
, del
, minuses
);
806 static int read_old_data(struct stat
*st
, const char *path
, void *buf
, unsigned long size
)
811 switch (st
->st_mode
& S_IFMT
) {
813 return readlink(path
, buf
, size
);
815 fd
= open(path
, O_RDONLY
);
817 return error("unable to open %s", path
);
820 int ret
= read(fd
, buf
+ got
, size
- got
);
838 static int find_offset(const char *buf
, unsigned long size
, const char *fragment
, unsigned long fragsize
, int line
)
841 unsigned long start
, backwards
, forwards
;
848 unsigned long offset
= 0;
850 while (offset
+ fragsize
<= size
) {
851 if (buf
[offset
++] == '\n') {
859 /* Exact line number? */
860 if (!memcmp(buf
+ start
, fragment
, fragsize
))
864 * There's probably some smart way to do this, but I'll leave
865 * that to the smart and beautiful people. I'm simple and stupid.
876 if (forwards
+ fragsize
> size
)
882 } while (backwards
&& buf
[backwards
-1] != '\n');
885 while (forwards
+ fragsize
<= size
) {
886 if (buf
[forwards
++] == '\n')
892 if (try + fragsize
> size
)
894 if (memcmp(buf
+ try, fragment
, fragsize
))
903 * We should start searching forward and backward.
914 static int apply_one_fragment(struct buffer_desc
*desc
, struct fragment
*frag
)
916 char *buf
= desc
->buffer
;
917 const char *patch
= frag
->patch
;
918 int offset
, size
= frag
->size
;
919 char *old
= xmalloc(size
);
920 char *new = xmalloc(size
);
921 int oldsize
= 0, newsize
= 0;
924 int len
= linelen(patch
, size
);
931 * "plen" is how much of the line we should use for
932 * the actual patch data. Normally we just remove the
933 * first character on the line, but if the line is
934 * followed by "\ No newline", then we also remove the
935 * last one (which is the newline, of course).
938 if (len
< size
&& patch
[len
] == '\\')
943 memcpy(old
+ oldsize
, patch
+ 1, plen
);
947 /* Fall-through for ' ' */
949 memcpy(new + newsize
, patch
+ 1, plen
);
953 /* Ignore it, we already handled it */
962 offset
= find_offset(buf
, desc
->size
, old
, oldsize
, frag
->newpos
);
964 int diff
= newsize
- oldsize
;
965 unsigned long size
= desc
->size
+ diff
;
966 unsigned long alloc
= desc
->alloc
;
971 buf
= xrealloc(buf
, alloc
);
975 memmove(buf
+ offset
+ newsize
, buf
+ offset
+ oldsize
, size
- offset
- newsize
);
976 memcpy(buf
+ offset
, new, newsize
);
985 static int apply_fragments(struct buffer_desc
*desc
, struct patch
*patch
)
987 struct fragment
*frag
= patch
->fragments
;
990 if (apply_one_fragment(desc
, frag
) < 0)
991 return error("patch failed: %s:%ld", patch
->old_name
, frag
->oldpos
);
997 static int apply_data(struct patch
*patch
, struct stat
*st
)
1000 unsigned long size
, alloc
;
1001 struct buffer_desc desc
;
1006 if (patch
->old_name
) {
1008 alloc
= size
+ 8192;
1009 buf
= xmalloc(alloc
);
1010 if (read_old_data(st
, patch
->old_name
, buf
, alloc
) != size
)
1011 return error("read of %s failed", patch
->old_name
);
1017 if (apply_fragments(&desc
, patch
) < 0)
1019 patch
->result
= desc
.buffer
;
1020 patch
->resultsize
= desc
.size
;
1022 if (patch
->is_delete
&& patch
->resultsize
)
1023 return error("removal patch leaves file contents");
1028 static int check_patch(struct patch
*patch
)
1031 const char *old_name
= patch
->old_name
;
1032 const char *new_name
= patch
->new_name
;
1036 int stat_ret
= lstat(old_name
, &st
);
1039 int pos
= cache_name_pos(old_name
, strlen(old_name
));
1041 return error("%s: does not exist in index",
1044 struct checkout costate
;
1045 if (errno
!= ENOENT
)
1046 return error("%s: %s", old_name
,
1049 costate
.base_dir
= "";
1050 costate
.base_dir_len
= 0;
1053 costate
.not_new
= 0;
1054 costate
.refresh_cache
= 1;
1055 if (checkout_entry(active_cache
[pos
],
1057 lstat(old_name
, &st
))
1061 changed
= ce_match_stat(active_cache
[pos
], &st
);
1063 return error("%s: does not match index",
1066 else if (stat_ret
< 0)
1067 return error("%s: %s", old_name
, strerror(errno
));
1069 if (patch
->is_new
< 0)
1071 st
.st_mode
= ntohl(create_ce_mode(st
.st_mode
));
1072 if (!patch
->old_mode
)
1073 patch
->old_mode
= st
.st_mode
;
1074 if ((st
.st_mode
^ patch
->old_mode
) & S_IFMT
)
1075 return error("%s: wrong type", old_name
);
1076 if (st
.st_mode
!= patch
->old_mode
)
1077 fprintf(stderr
, "warning: %s has type %o, expected %o\n",
1078 old_name
, st
.st_mode
, patch
->old_mode
);
1081 if (new_name
&& (patch
->is_new
| patch
->is_rename
| patch
->is_copy
)) {
1082 if (check_index
&& cache_name_pos(new_name
, strlen(new_name
)) >= 0)
1083 return error("%s: already exists in index", new_name
);
1084 if (!lstat(new_name
, &st
))
1085 return error("%s: already exists in working directory", new_name
);
1086 if (errno
!= ENOENT
)
1087 return error("%s: %s", new_name
, strerror(errno
));
1088 if (!patch
->new_mode
) {
1090 patch
->new_mode
= S_IFREG
| 0644;
1092 patch
->new_mode
= patch
->old_mode
;
1096 if (new_name
&& old_name
) {
1097 int same
= !strcmp(old_name
, new_name
);
1098 if (!patch
->new_mode
)
1099 patch
->new_mode
= patch
->old_mode
;
1100 if ((patch
->old_mode
^ patch
->new_mode
) & S_IFMT
)
1101 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1102 patch
->new_mode
, new_name
, patch
->old_mode
,
1103 same
? "" : " of ", same
? "" : old_name
);
1106 if (apply_data(patch
, &st
) < 0)
1107 return error("%s: patch does not apply", old_name
);
1111 static int check_patch_list(struct patch
*patch
)
1115 for (;patch
; patch
= patch
->next
)
1116 error
|= check_patch(patch
);
1120 static void show_file(int c
, unsigned int mode
, const char *name
)
1122 printf("%c %o %s\n", c
, mode
, name
);
1125 static void show_file_list(struct patch
*patch
)
1127 for (;patch
; patch
= patch
->next
) {
1128 if (patch
->is_rename
) {
1129 show_file('-', patch
->old_mode
, patch
->old_name
);
1130 show_file('+', patch
->new_mode
, patch
->new_name
);
1133 if (patch
->is_copy
|| patch
->is_new
) {
1134 show_file('+', patch
->new_mode
, patch
->new_name
);
1137 if (patch
->is_delete
) {
1138 show_file('-', patch
->old_mode
, patch
->old_name
);
1141 if (patch
->old_mode
&& patch
->new_mode
&& patch
->old_mode
!= patch
->new_mode
) {
1142 printf("M %o:%o %s\n", patch
->old_mode
, patch
->new_mode
, patch
->old_name
);
1145 printf("M %o %s\n", patch
->old_mode
, patch
->old_name
);
1149 static void stat_patch_list(struct patch
*patch
)
1151 int files
, adds
, dels
;
1153 for (files
= adds
= dels
= 0 ; patch
; patch
= patch
->next
) {
1155 adds
+= patch
->lines_added
;
1156 dels
+= patch
->lines_deleted
;
1160 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files
, adds
, dels
);
1163 static void show_file_mode_name(const char *newdelete
, unsigned int mode
, const char *name
)
1166 printf(" %s mode %06o %s\n", newdelete
, mode
, name
);
1168 printf(" %s %s\n", newdelete
, name
);
1171 static void show_mode_change(struct patch
*p
, int show_name
)
1173 if (p
->old_mode
&& p
->new_mode
&& p
->old_mode
!= p
->new_mode
) {
1175 printf(" mode change %06o => %06o %s\n",
1176 p
->old_mode
, p
->new_mode
, p
->new_name
);
1178 printf(" mode change %06o => %06o\n",
1179 p
->old_mode
, p
->new_mode
);
1183 static void show_rename_copy(struct patch
*p
)
1185 const char *renamecopy
= p
->is_rename
? "rename" : "copy";
1186 const char *old
, *new;
1188 /* Find common prefix */
1192 const char *slash_old
, *slash_new
;
1193 slash_old
= strchr(old
, '/');
1194 slash_new
= strchr(new, '/');
1197 slash_old
- old
!= slash_new
- new ||
1198 memcmp(old
, new, slash_new
- new))
1200 old
= slash_old
+ 1;
1201 new = slash_new
+ 1;
1203 /* p->old_name thru old is the common prefix, and old and new
1204 * through the end of names are renames
1206 if (old
!= p
->old_name
)
1207 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
1208 (int)(old
- p
->old_name
), p
->old_name
,
1209 old
, new, p
->score
);
1211 printf(" %s %s => %s (%d%%)\n", renamecopy
,
1212 p
->old_name
, p
->new_name
, p
->score
);
1213 show_mode_change(p
, 0);
1216 static void summary_patch_list(struct patch
*patch
)
1220 for (p
= patch
; p
; p
= p
->next
) {
1222 show_file_mode_name("create", p
->new_mode
, p
->new_name
);
1223 else if (p
->is_delete
)
1224 show_file_mode_name("delete", p
->old_mode
, p
->old_name
);
1226 if (p
->is_rename
|| p
->is_copy
)
1227 show_rename_copy(p
);
1230 printf(" rewrite %s (%d%%)\n",
1231 p
->new_name
, p
->score
);
1232 show_mode_change(p
, 0);
1235 show_mode_change(p
, 1);
1241 static void patch_stats(struct patch
*patch
)
1243 int lines
= patch
->lines_added
+ patch
->lines_deleted
;
1245 if (lines
> max_change
)
1247 if (patch
->old_name
) {
1248 int len
= strlen(patch
->old_name
);
1252 if (patch
->new_name
) {
1253 int len
= strlen(patch
->new_name
);
1259 static void remove_file(struct patch
*patch
)
1262 if (remove_file_from_cache(patch
->old_name
) < 0)
1263 die("unable to remove %s from index", patch
->old_name
);
1265 unlink(patch
->old_name
);
1268 static void add_index_file(const char *path
, unsigned mode
, void *buf
, unsigned long size
)
1271 struct cache_entry
*ce
;
1272 int namelen
= strlen(path
);
1273 unsigned ce_size
= cache_entry_size(namelen
);
1278 ce
= xmalloc(ce_size
);
1279 memset(ce
, 0, ce_size
);
1280 memcpy(ce
->name
, path
, namelen
);
1281 ce
->ce_mode
= create_ce_mode(mode
);
1282 ce
->ce_flags
= htons(namelen
);
1283 if (lstat(path
, &st
) < 0)
1284 die("unable to stat newly created file %s", path
);
1285 fill_stat_cache_info(ce
, &st
);
1286 if (write_sha1_file(buf
, size
, "blob", ce
->sha1
) < 0)
1287 die("unable to create backing store for newly created file %s", path
);
1288 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
) < 0)
1289 die("unable to add cache entry for %s", path
);
1292 static void create_subdirectories(const char *path
)
1294 int len
= strlen(path
);
1295 char *buf
= xmalloc(len
+ 1);
1296 const char *slash
= path
;
1298 while ((slash
= strchr(slash
+1, '/')) != NULL
) {
1300 memcpy(buf
, path
, len
);
1302 if (mkdir(buf
, 0777) < 0) {
1303 if (errno
!= EEXIST
)
1310 static int try_create_file(const char *path
, unsigned int mode
, const char *buf
, unsigned long size
)
1315 return symlink(buf
, path
);
1316 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
| O_TRUNC
, (mode
& 0100) ? 0777 : 0666);
1320 int written
= write(fd
, buf
, size
);
1322 if (errno
== EINTR
|| errno
== EAGAIN
)
1324 die("writing file %s: %s", path
, strerror(errno
));
1327 die("out of space writing file %s", path
);
1332 die("closing file %s: %s", path
, strerror(errno
));
1337 * We optimistically assume that the directories exist,
1338 * which is true 99% of the time anyway. If they don't,
1339 * we create them and try again.
1341 static void create_one_file(const char *path
, unsigned mode
, const char *buf
, unsigned long size
)
1343 if (!try_create_file(path
, mode
, buf
, size
))
1346 if (errno
== ENOENT
) {
1347 create_subdirectories(path
);
1348 if (!try_create_file(path
, mode
, buf
, size
))
1352 if (errno
== EEXIST
) {
1353 unsigned int nr
= getpid();
1356 const char *newpath
;
1357 newpath
= mkpath("%s~%u", path
, nr
);
1358 if (!try_create_file(newpath
, mode
, buf
, size
)) {
1359 if (!rename(newpath
, path
))
1364 if (errno
!= EEXIST
)
1368 die("unable to write file %s mode %o", path
, mode
);
1371 static void create_file(struct patch
*patch
)
1373 const char *path
= patch
->new_name
;
1374 unsigned mode
= patch
->new_mode
;
1375 unsigned long size
= patch
->resultsize
;
1376 char *buf
= patch
->result
;
1379 mode
= S_IFREG
| 0644;
1380 create_one_file(path
, mode
, buf
, size
);
1381 add_index_file(path
, mode
, buf
, size
);
1384 static void write_out_one_result(struct patch
*patch
)
1386 if (patch
->is_delete
> 0) {
1390 if (patch
->is_new
> 0 || patch
->is_copy
) {
1395 * Rename or modification boils down to the same
1396 * thing: remove the old, write the new
1402 static void write_out_results(struct patch
*list
, int skipped_patch
)
1404 if (!list
&& !skipped_patch
)
1408 write_out_one_result(list
);
1413 static struct cache_file cache_file
;
1415 static struct excludes
{
1416 struct excludes
*next
;
1420 static int use_patch(struct patch
*p
)
1422 const char *pathname
= p
->new_name
? p
->new_name
: p
->old_name
;
1423 struct excludes
*x
= excludes
;
1425 if (fnmatch(x
->path
, pathname
, 0) == 0)
1432 static int apply_patch(int fd
)
1435 unsigned long offset
, size
;
1436 char *buffer
= read_patch_file(fd
, &size
);
1437 struct patch
*list
= NULL
, **listp
= &list
;
1438 int skipped_patch
= 0;
1444 struct patch
*patch
;
1447 patch
= xmalloc(sizeof(*patch
));
1448 memset(patch
, 0, sizeof(*patch
));
1449 nr
= parse_chunk(buffer
+ offset
, size
, patch
);
1452 if (use_patch(patch
)) {
1455 listp
= &patch
->next
;
1457 /* perhaps free it a bit better? */
1466 write_index
= check_index
&& apply
;
1468 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
1470 if (read_cache() < 0)
1471 die("unable to read index file");
1474 if ((check
|| apply
) && check_patch_list(list
) < 0)
1478 write_out_results(list
, skipped_patch
);
1481 if (write_cache(newfd
, active_cache
, active_nr
) ||
1482 commit_index_file(&cache_file
))
1483 die("Unable to write new cachefile");
1487 show_file_list(list
);
1490 stat_patch_list(list
);
1493 summary_patch_list(list
);
1499 int main(int argc
, char **argv
)
1504 for (i
= 1; i
< argc
; i
++) {
1505 const char *arg
= argv
[i
];
1508 if (!strcmp(arg
, "-")) {
1513 if (!strncmp(arg
, "--exclude=", 10)) {
1514 struct excludes
*x
= xmalloc(sizeof(*x
));
1520 /* NEEDSWORK: this does not do anything at this moment. */
1521 if (!strcmp(arg
, "--no-merge")) {
1525 if (!strcmp(arg
, "--stat")) {
1530 if (!strcmp(arg
, "--summary")) {
1535 if (!strcmp(arg
, "--check")) {
1540 if (!strcmp(arg
, "--index")) {
1544 if (!strcmp(arg
, "--apply")) {
1548 if (!strcmp(arg
, "--show-files")) {
1552 fd
= open(arg
, O_RDONLY
);