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;
33 static int show_files
= 0;
34 static const char apply_usage
[] = "git-apply [--stat] [--check] [--show-files] <patch>";
37 * For "diff-stat" like behaviour, we keep track of the biggest change
38 * we've seen, and the longest filename. That allows us to do simple
41 static int max_change
, max_len
;
44 * Various "current state", notably line numbers and what
45 * file (and how) we're patching right now.. The "is_xxxx"
46 * things are flags, where -1 means "don't know yet".
48 static int linenr
= 1;
51 unsigned long oldpos
, oldlines
;
52 unsigned long newpos
, newlines
;
55 struct fragment
*next
;
59 char *new_name
, *old_name
, *def_name
;
60 unsigned int old_mode
, new_mode
;
61 int is_rename
, is_copy
, is_new
, is_delete
;
62 int lines_added
, lines_deleted
;
63 struct fragment
*fragments
;
65 unsigned long resultsize
;
69 #define CHUNKSIZE (8192)
72 static void *read_patch_file(int fd
, unsigned long *sizep
)
74 unsigned long size
= 0, alloc
= CHUNKSIZE
;
75 void *buffer
= xmalloc(alloc
);
78 int nr
= alloc
- size
;
81 buffer
= xrealloc(buffer
, alloc
);
84 nr
= read(fd
, buffer
+ size
, nr
);
90 die("git-apply: read returned %s", strerror(errno
));
97 * Make sure that we have some slop in the buffer
98 * so that we can do speculative "memcmp" etc, and
99 * see to it that it is NUL-filled.
101 if (alloc
< size
+ SLOP
)
102 buffer
= xrealloc(buffer
, size
+ SLOP
);
103 memset(buffer
+ size
, 0, SLOP
);
107 static unsigned long linelen(const char *buffer
, unsigned long size
)
109 unsigned long len
= 0;
112 if (*buffer
++ == '\n')
118 static int is_dev_null(const char *str
)
120 return !memcmp("/dev/null", str
, 9) && isspace(str
[9]);
126 static int name_terminate(const char *name
, int namelen
, int c
, int terminate
)
128 if (c
== ' ' && !(terminate
& TERM_SPACE
))
130 if (c
== '\t' && !(terminate
& TERM_TAB
))
136 static char * find_name(const char *line
, char *def
, int p_value
, int terminate
)
139 const char *start
= line
;
148 if (name_terminate(start
, line
-start
, c
, terminate
))
152 if (c
== '/' && !--p_value
)
162 * Generally we prefer the shorter name, especially
163 * if the other one is just a variation of that with
164 * something else tacked on to the end (ie "file.orig"
168 int deflen
= strlen(def
);
169 if (deflen
< len
&& !strncmp(start
, def
, deflen
))
173 name
= xmalloc(len
+ 1);
174 memcpy(name
, start
, len
);
181 * Get the name etc info from the --/+++ lines of a traditional patch header
183 * NOTE! This hardcodes "-p1" behaviour in filename detection.
185 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
186 * files, we can happily check the index for a match, but for creating a
187 * new file we should try to match whatever "patch" does. I have no idea.
189 static void parse_traditional_patch(const char *first
, const char *second
, struct patch
*patch
)
194 first
+= 4; // skip "--- "
195 second
+= 4; // skip "+++ "
196 if (is_dev_null(first
)) {
198 patch
->is_delete
= 0;
199 name
= find_name(second
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
200 patch
->new_name
= name
;
201 } else if (is_dev_null(second
)) {
203 patch
->is_delete
= 1;
204 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
205 patch
->old_name
= name
;
207 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
208 name
= find_name(second
, name
, p_value
, TERM_SPACE
| TERM_TAB
);
209 patch
->old_name
= patch
->new_name
= name
;
212 die("unable to find filename in patch at line %d", linenr
);
215 static int gitdiff_hdrend(const char *line
, struct patch
*patch
)
221 * We're anal about diff header consistency, to make
222 * sure that we don't end up having strange ambiguous
223 * patches floating around.
225 * As a result, gitdiff_{old|new}name() will check
226 * their names against any previous information, just
229 static char *gitdiff_verify_name(const char *line
, int isnull
, char *orig_name
, const char *oldnew
)
234 if (!orig_name
&& !isnull
)
235 return find_name(line
, NULL
, 1, 0);
243 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name
, linenr
);
256 if (memcmp(line
, name
, len
) || line
[len
] != '\n')
260 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew
, linenr
);
264 static int gitdiff_oldname(const char *line
, struct patch
*patch
)
266 patch
->old_name
= gitdiff_verify_name(line
, patch
->is_new
, patch
->old_name
, "old");
270 static int gitdiff_newname(const char *line
, struct patch
*patch
)
272 patch
->new_name
= gitdiff_verify_name(line
, patch
->is_delete
, patch
->new_name
, "new");
276 static int gitdiff_oldmode(const char *line
, struct patch
*patch
)
278 patch
->old_mode
= strtoul(line
, NULL
, 8);
282 static int gitdiff_newmode(const char *line
, struct patch
*patch
)
284 patch
->new_mode
= strtoul(line
, NULL
, 8);
288 static int gitdiff_delete(const char *line
, struct patch
*patch
)
290 patch
->is_delete
= 1;
291 patch
->old_name
= patch
->def_name
;
292 return gitdiff_oldmode(line
, patch
);
295 static int gitdiff_newfile(const char *line
, struct patch
*patch
)
298 patch
->new_name
= patch
->def_name
;
299 return gitdiff_newmode(line
, patch
);
302 static int gitdiff_copysrc(const char *line
, struct patch
*patch
)
305 patch
->old_name
= find_name(line
, NULL
, 0, 0);
309 static int gitdiff_copydst(const char *line
, struct patch
*patch
)
312 patch
->new_name
= find_name(line
, NULL
, 0, 0);
316 static int gitdiff_renamesrc(const char *line
, struct patch
*patch
)
318 patch
->is_rename
= 1;
319 patch
->old_name
= find_name(line
, NULL
, 0, 0);
323 static int gitdiff_renamedst(const char *line
, struct patch
*patch
)
325 patch
->is_rename
= 1;
326 patch
->new_name
= find_name(line
, NULL
, 0, 0);
330 static int gitdiff_similarity(const char *line
, struct patch
*patch
)
335 static int gitdiff_dissimilarity(const char *line
, struct patch
*patch
)
341 * This is normal for a diff that doesn't change anything: we'll fall through
342 * into the next diff. Tell the parser to break out.
344 static int gitdiff_unrecognized(const char *line
, struct patch
*patch
)
349 static char *git_header_name(char *line
)
367 * We don't accept absolute paths (/dev/null) as possibly valid
373 * Accept a name only if it shows up twice, exactly the same
376 for (len
= 0 ; ; len
++) {
393 if (second
[len
] == '\n' && !memcmp(name
, second
, len
)) {
394 char *ret
= xmalloc(len
+ 1);
395 memcpy(ret
, name
, len
);
404 /* Verify that we recognize the lines following a git header */
405 static int parse_git_header(char *line
, int len
, unsigned int size
, struct patch
*patch
)
407 unsigned long offset
;
409 /* A git diff has explicit new/delete information, so we don't guess */
411 patch
->is_delete
= 0;
414 * Some things may not have the old name in the
415 * rest of the headers anywhere (pure mode changes,
416 * or removing or adding empty files), so we get
417 * the default name from the header.
419 patch
->def_name
= git_header_name(line
+ strlen("diff --git "));
424 for (offset
= len
; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
425 static const struct opentry
{
427 int (*fn
)(const char *, struct patch
*);
429 { "@@ -", gitdiff_hdrend
},
430 { "--- ", gitdiff_oldname
},
431 { "+++ ", gitdiff_newname
},
432 { "old mode ", gitdiff_oldmode
},
433 { "new mode ", gitdiff_newmode
},
434 { "deleted file mode ", gitdiff_delete
},
435 { "new file mode ", gitdiff_newfile
},
436 { "copy from ", gitdiff_copysrc
},
437 { "copy to ", gitdiff_copydst
},
438 { "rename old ", gitdiff_renamesrc
},
439 { "rename new ", gitdiff_renamedst
},
440 { "rename from ", gitdiff_renamesrc
},
441 { "rename to ", gitdiff_renamedst
},
442 { "similarity index ", gitdiff_similarity
},
443 { "dissimilarity index ", gitdiff_dissimilarity
},
444 { "", gitdiff_unrecognized
},
448 len
= linelen(line
, size
);
449 if (!len
|| line
[len
-1] != '\n')
451 for (i
= 0; i
< sizeof(optable
) / sizeof(optable
[0]); i
++) {
452 const struct opentry
*p
= optable
+ i
;
453 int oplen
= strlen(p
->str
);
454 if (len
< oplen
|| memcmp(p
->str
, line
, oplen
))
456 if (p
->fn(line
+ oplen
, patch
) < 0)
465 static int parse_num(const char *line
, unsigned long *p
)
471 *p
= strtoul(line
, &ptr
, 10);
475 static int parse_range(const char *line
, int len
, int offset
, const char *expect
,
476 unsigned long *p1
, unsigned long *p2
)
480 if (offset
< 0 || offset
>= len
)
485 digits
= parse_num(line
, p1
);
495 digits
= parse_num(line
+1, p2
);
507 if (memcmp(line
, expect
, ex
))
514 * Parse a unified diff fragment header of the
515 * form "@@ -a,b +c,d @@"
517 static int parse_fragment_header(char *line
, int len
, struct fragment
*fragment
)
521 if (!len
|| line
[len
-1] != '\n')
524 /* Figure out the number of lines in a fragment */
525 offset
= parse_range(line
, len
, 4, " +", &fragment
->oldpos
, &fragment
->oldlines
);
526 offset
= parse_range(line
, len
, offset
, " @@", &fragment
->newpos
, &fragment
->newlines
);
531 static int find_header(char *line
, unsigned long size
, int *hdrsize
, struct patch
*patch
)
533 unsigned long offset
, len
;
535 patch
->is_rename
= patch
->is_copy
= 0;
536 patch
->is_new
= patch
->is_delete
= -1;
537 patch
->old_mode
= patch
->new_mode
= 0;
538 patch
->old_name
= patch
->new_name
= NULL
;
539 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
540 unsigned long nextlen
;
542 len
= linelen(line
, size
);
546 /* Testing this early allows us to take a few shortcuts.. */
551 * Make sure we don't find any unconnected patch fragmants.
552 * That's a sign that we didn't find a header, and that a
553 * patch has become corrupted/broken up.
555 if (!memcmp("@@ -", line
, 4)) {
556 struct fragment dummy
;
557 if (parse_fragment_header(line
, len
, &dummy
) < 0)
559 error("patch fragment without header at line %d: %.*s", linenr
, len
-1, line
);
566 * Git patch? It might not have a real patch, just a rename
567 * or mode change, so we handle that specially
569 if (!memcmp("diff --git ", line
, 11)) {
570 int git_hdr_len
= parse_git_header(line
, len
, size
, patch
);
571 if (git_hdr_len
<= len
)
573 if (!patch
->old_name
&& !patch
->new_name
) {
574 if (!patch
->def_name
)
575 die("git diff header lacks filename information (line %d)", linenr
);
576 patch
->old_name
= patch
->new_name
= patch
->def_name
;
578 *hdrsize
= git_hdr_len
;
582 /** --- followed by +++ ? */
583 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
587 * We only accept unified patches, so we want it to
588 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
591 nextlen
= linelen(line
+ len
, size
- len
);
592 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
595 /* Ok, we'll consider it a patch */
596 parse_traditional_patch(line
, line
+len
, patch
);
597 *hdrsize
= len
+ nextlen
;
605 * Parse a unified diff. Note that this really needs
606 * to parse each fragment separately, since the only
607 * way to know the difference between a "---" that is
608 * part of a patch, and a "---" that starts the next
609 * patch is to look at the line counts..
611 static int parse_fragment(char *line
, unsigned long size
, struct patch
*patch
, struct fragment
*fragment
)
614 int len
= linelen(line
, size
), offset
;
615 unsigned long oldlines
, newlines
;
617 offset
= parse_fragment_header(line
, len
, fragment
);
620 oldlines
= fragment
->oldlines
;
621 newlines
= fragment
->newlines
;
623 if (patch
->is_new
< 0) {
624 patch
->is_new
= !oldlines
;
626 patch
->old_name
= NULL
;
628 if (patch
->is_delete
< 0) {
629 patch
->is_delete
= !newlines
;
631 patch
->new_name
= NULL
;
634 if (patch
->is_new
!= !oldlines
)
635 return error("new file depends on old contents");
636 if (patch
->is_delete
!= !newlines
) {
638 return error("deleted file still has contents");
639 fprintf(stderr
, "** warning: file %s becomes empty but is not deleted\n", patch
->new_name
);
642 /* Parse the thing.. */
647 for (offset
= len
; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
648 if (!oldlines
&& !newlines
)
650 len
= linelen(line
, size
);
651 if (!len
|| line
[len
-1] != '\n')
668 /* We allow "\ No newline at end of file" */
670 if (len
< 12 || memcmp(line
, "\\ No newline", 12))
675 patch
->lines_added
+= added
;
676 patch
->lines_deleted
+= deleted
;
680 static int parse_single_patch(char *line
, unsigned long size
, struct patch
*patch
)
682 unsigned long offset
= 0;
683 struct fragment
**fragp
= &patch
->fragments
;
685 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
686 struct fragment
*fragment
;
689 fragment
= xmalloc(sizeof(*fragment
));
690 memset(fragment
, 0, sizeof(*fragment
));
691 len
= parse_fragment(line
, size
, patch
, fragment
);
693 die("corrupt patch at line %d", linenr
);
695 fragment
->patch
= line
;
696 fragment
->size
= len
;
699 fragp
= &fragment
->next
;
708 static int parse_chunk(char *buffer
, unsigned long size
, struct patch
*patch
)
710 int hdrsize
, patchsize
;
711 int offset
= find_header(buffer
, size
, &hdrsize
, patch
);
716 patchsize
= parse_single_patch(buffer
+ offset
+ hdrsize
, size
- offset
- hdrsize
, patch
);
718 return offset
+ hdrsize
+ patchsize
;
721 const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
722 const char minuses
[]= "----------------------------------------------------------------------";
724 static void show_stats(struct patch
*patch
)
726 char *name
= patch
->old_name
;
727 int len
, max
, add
, del
, total
;
730 name
= patch
->new_name
;
733 * "scale" the filename
744 * scale the add/delete
750 add
= patch
->lines_added
;
751 del
= patch
->lines_deleted
;
754 total
= (total
* max
+ max_change
/ 2) / max_change
;
755 add
= (add
* max
+ max_change
/ 2) / max_change
;
757 printf(" %-*s |%5d %.*s%.*s\n",
758 len
, name
, patch
->lines_added
+ patch
->lines_deleted
,
759 add
, pluses
, del
, minuses
);
762 static int read_old_data(struct stat
*st
, const char *path
, void *buf
, unsigned long size
)
767 switch (st
->st_mode
& S_IFMT
) {
769 return readlink(path
, buf
, size
);
771 fd
= open(path
, O_RDONLY
);
773 return error("unable to open %s", path
);
776 int ret
= read(fd
, buf
+ got
, size
- got
);
794 static int find_offset(const char *buf
, unsigned long size
, const char *fragment
, unsigned long fragsize
, int line
)
797 unsigned long start
, backwards
, forwards
;
804 unsigned long offset
= 0;
806 while (offset
+ fragsize
<= size
) {
807 if (buf
[offset
++] == '\n') {
815 /* Exact line number? */
816 if (!memcmp(buf
+ start
, fragment
, fragsize
))
820 * There's probably some smart way to do this, but I'll leave
821 * that to the smart and beautiful people. I'm simple and stupid.
832 if (forwards
+ fragsize
> size
)
838 } while (backwards
&& buf
[backwards
-1] != '\n');
841 while (forwards
+ fragsize
<= size
) {
842 if (buf
[forwards
++] == '\n')
848 if (try + fragsize
> size
)
850 if (memcmp(buf
+ try, fragment
, fragsize
))
855 fprintf(stderr
, "Fragment applied at offset %d\n", n
);
860 * We should start searching forward and backward.
871 static int apply_one_fragment(struct buffer_desc
*desc
, struct fragment
*frag
)
873 char *buf
= desc
->buffer
;
874 const char *patch
= frag
->patch
;
875 int offset
, size
= frag
->size
;
876 char *old
= xmalloc(size
);
877 char *new = xmalloc(size
);
878 int oldsize
= 0, newsize
= 0;
881 int len
= linelen(patch
, size
);
888 * "plen" is how much of the line we should use for
889 * the actual patch data. Normally we just remove the
890 * first character on the line, but if the line is
891 * followed by "\ No newline", then we also remove the
892 * last one (which is the newline, of course).
895 if (len
> size
&& patch
[len
] == '\\')
900 memcpy(old
+ oldsize
, patch
+ 1, plen
);
904 /* Fall-through for ' ' */
906 memcpy(new + newsize
, patch
+ 1, plen
);
910 /* Ignore it, we already handled it */
919 offset
= find_offset(buf
, desc
->size
, old
, oldsize
, frag
->newpos
);
921 int diff
= newsize
- oldsize
;
922 unsigned long size
= desc
->size
+ diff
;
923 unsigned long alloc
= desc
->alloc
;
928 buf
= xrealloc(buf
, alloc
);
932 memmove(buf
+ offset
+ newsize
, buf
+ offset
+ oldsize
, size
- offset
- newsize
);
933 memcpy(buf
+ offset
, new, newsize
);
942 static int apply_fragments(struct buffer_desc
*desc
, struct patch
*patch
)
944 struct fragment
*frag
= patch
->fragments
;
947 if (apply_one_fragment(desc
, frag
) < 0)
948 return error("patch failed: %s:%d", patch
->old_name
, frag
->oldpos
);
954 static int apply_data(struct patch
*patch
, struct stat
*st
)
957 unsigned long size
, alloc
;
958 struct buffer_desc desc
;
963 if (patch
->old_name
) {
966 buf
= xmalloc(alloc
);
967 if (read_old_data(st
, patch
->old_name
, buf
, alloc
) != size
)
968 return error("read of %s failed", patch
->old_name
);
974 if (apply_fragments(&desc
, patch
) < 0)
976 patch
->result
= desc
.buffer
;
977 patch
->resultsize
= desc
.size
;
979 if (patch
->is_delete
&& patch
->resultsize
)
980 return error("removal patch leaves file contents");
985 static int check_patch(struct patch
*patch
)
988 const char *old_name
= patch
->old_name
;
989 const char *new_name
= patch
->new_name
;
994 if (lstat(old_name
, &st
) < 0)
995 return error("%s: %s", old_name
, strerror(errno
));
997 int pos
= cache_name_pos(old_name
, strlen(old_name
));
999 return error("%s: does not exist in index", old_name
);
1000 changed
= ce_match_stat(active_cache
[pos
], &st
);
1002 return error("%s: does not match index", old_name
);
1004 if (patch
->is_new
< 0)
1006 st
.st_mode
= ntohl(create_ce_mode(st
.st_mode
));
1007 if (!patch
->old_mode
)
1008 patch
->old_mode
= st
.st_mode
;
1009 if ((st
.st_mode
^ patch
->old_mode
) & S_IFMT
)
1010 return error("%s: wrong type", old_name
);
1011 if (st
.st_mode
!= patch
->old_mode
)
1012 fprintf(stderr
, "warning: %s has type %o, expected %o\n",
1013 old_name
, st
.st_mode
, patch
->old_mode
);
1016 if (new_name
&& (patch
->is_new
| patch
->is_rename
| patch
->is_copy
)) {
1017 if (check_index
&& cache_name_pos(new_name
, strlen(new_name
)) >= 0)
1018 return error("%s: already exists in index", new_name
);
1019 if (!lstat(new_name
, &st
))
1020 return error("%s: already exists in working directory", new_name
);
1021 if (errno
!= ENOENT
)
1022 return error("%s: %s", new_name
, strerror(errno
));
1023 if (!patch
->new_mode
)
1024 patch
->new_mode
= S_IFREG
| 0644;
1027 if (new_name
&& old_name
) {
1028 int same
= !strcmp(old_name
, new_name
);
1029 if (!patch
->new_mode
)
1030 patch
->new_mode
= patch
->old_mode
;
1031 if ((patch
->old_mode
^ patch
->new_mode
) & S_IFMT
)
1032 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1033 patch
->new_mode
, new_name
, patch
->old_mode
,
1034 same
? "" : " of ", same
? "" : old_name
);
1037 if (apply_data(patch
, &st
) < 0)
1038 return error("%s: patch does not apply", old_name
);
1042 static int check_patch_list(struct patch
*patch
)
1046 for (;patch
; patch
= patch
->next
)
1047 error
|= check_patch(patch
);
1051 static void show_file(int c
, unsigned int mode
, const char *name
)
1053 printf("%c %o %s\n", c
, mode
, name
);
1056 static void show_file_list(struct patch
*patch
)
1058 for (;patch
; patch
= patch
->next
) {
1059 if (patch
->is_rename
) {
1060 show_file('-', patch
->old_mode
, patch
->old_name
);
1061 show_file('+', patch
->new_mode
, patch
->new_name
);
1064 if (patch
->is_copy
|| patch
->is_new
) {
1065 show_file('+', patch
->new_mode
, patch
->new_name
);
1068 if (patch
->is_delete
) {
1069 show_file('-', patch
->old_mode
, patch
->old_name
);
1072 if (patch
->old_mode
&& patch
->new_mode
&& patch
->old_mode
!= patch
->new_mode
) {
1073 printf("M %o:%o %s\n", patch
->old_mode
, patch
->new_mode
, patch
->old_name
);
1076 printf("M %o %s\n", patch
->old_mode
, patch
->old_name
);
1080 static void stat_patch_list(struct patch
*patch
)
1082 int files
, adds
, dels
;
1084 for (files
= adds
= dels
= 0 ; patch
; patch
= patch
->next
) {
1086 adds
+= patch
->lines_added
;
1087 dels
+= patch
->lines_deleted
;
1091 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files
, adds
, dels
);
1094 static void patch_stats(struct patch
*patch
)
1096 int lines
= patch
->lines_added
+ patch
->lines_deleted
;
1098 if (lines
> max_change
)
1100 if (patch
->old_name
) {
1101 int len
= strlen(patch
->old_name
);
1105 if (patch
->new_name
) {
1106 int len
= strlen(patch
->new_name
);
1112 static void remove_file(struct patch
*patch
)
1115 if (remove_file_from_cache(patch
->old_name
) < 0)
1116 die("unable to remove %s from index", patch
->old_name
);
1118 unlink(patch
->old_name
);
1121 static void add_index_file(const char *path
, unsigned mode
, void *buf
, unsigned long size
)
1124 struct cache_entry
*ce
;
1125 int namelen
= strlen(path
);
1126 unsigned ce_size
= cache_entry_size(namelen
);
1131 ce
= xmalloc(ce_size
);
1132 memset(ce
, 0, ce_size
);
1133 memcpy(ce
->name
, path
, namelen
);
1134 ce
->ce_mode
= create_ce_mode(mode
);
1135 ce
->ce_flags
= htons(namelen
);
1136 if (lstat(path
, &st
) < 0)
1137 die("unable to stat newly created file %s", path
);
1138 fill_stat_cache_info(ce
, &st
);
1139 if (write_sha1_file(buf
, size
, "blob", ce
->sha1
) < 0)
1140 die("unable to create backing store for newly created file %s", path
);
1141 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
) < 0)
1142 die("unable to add cache entry for %s", path
);
1145 static void create_file(struct patch
*patch
)
1147 const char *path
= patch
->new_name
;
1148 unsigned mode
= patch
->new_mode
;
1149 unsigned long size
= patch
->resultsize
;
1150 char *buf
= patch
->result
;
1153 mode
= S_IFREG
| 0644;
1154 if (S_ISREG(mode
)) {
1156 mode
= (mode
& 0100) ? 0777 : 0666;
1157 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
1159 die("unable to create file %s (%s)", path
, strerror(errno
));
1160 if (write(fd
, buf
, size
) != size
)
1161 die("unable to write file %s", path
);
1163 add_index_file(path
, mode
, buf
, size
);
1166 if (S_ISLNK(mode
)) {
1167 if (size
&& buf
[size
-1] == '\n')
1170 if (symlink(buf
, path
) < 0)
1171 die("unable to write symlink %s", path
);
1172 add_index_file(path
, mode
, buf
, size
);
1175 die("unable to write file mode %o", mode
);
1178 static void write_out_one_result(struct patch
*patch
)
1180 if (patch
->is_delete
> 0) {
1184 if (patch
->is_new
> 0 || patch
->is_copy
) {
1189 * Rename or modification boils down to the same
1190 * thing: remove the old, write the new
1196 static void write_out_results(struct patch
*list
)
1202 write_out_one_result(list
);
1207 static struct cache_file cache_file
;
1209 static int apply_patch(int fd
)
1212 unsigned long offset
, size
;
1213 char *buffer
= read_patch_file(fd
, &size
);
1214 struct patch
*list
= NULL
, **listp
= &list
;
1220 struct patch
*patch
;
1223 patch
= xmalloc(sizeof(*patch
));
1224 memset(patch
, 0, sizeof(*patch
));
1225 nr
= parse_chunk(buffer
+ offset
, size
, patch
);
1230 listp
= &patch
->next
;
1236 write_index
= check_index
&& apply
;
1238 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
1240 if (read_cache() < 0)
1241 die("unable to read index file");
1244 if ((check
|| apply
) && check_patch_list(list
) < 0)
1248 write_out_results(list
);
1251 if (write_cache(newfd
, active_cache
, active_nr
) ||
1252 commit_index_file(&cache_file
))
1253 die("Unable to write new cachefile");
1257 show_file_list(list
);
1260 stat_patch_list(list
);
1266 int main(int argc
, char **argv
)
1271 for (i
= 1; i
< argc
; i
++) {
1272 const char *arg
= argv
[i
];
1275 if (!strcmp(arg
, "-")) {
1280 if (!strcmp(arg
, "--no-merge")) {
1284 if (!strcmp(arg
, "--stat")) {
1289 if (!strcmp(arg
, "--check")) {
1294 if (!strcmp(arg
, "--index")) {
1298 if (!strcmp(arg
, "--show-files")) {
1302 fd
= open(arg
, O_RDONLY
);