Fix git(1) link to git-index-pack
[git.git] / apply.c
diff --git a/apply.c b/apply.c
index cf8aa87..590adc6 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -23,10 +23,11 @@ static int numstat = 0;
 static int summary = 0;
 static int check = 0;
 static int apply = 1;
+static int no_add = 0;
 static int show_index_info = 0;
 static int line_termination = '\n';
 static const char apply_usage[] =
-"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--index-info] [-z] <patch>...";
+"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [-z] <patch>...";
 
 /*
  * For "diff-stat" like behaviour, we keep track of the biggest change
@@ -369,7 +370,7 @@ static int gitdiff_index(const char *line, struct patch *patch)
        int len;
 
        ptr = strchr(line, '.');
-       if (!ptr || ptr[1] != '.' || 40 <= ptr - line)
+       if (!ptr || ptr[1] != '.' || 40 < ptr - line)
                return 0;
        len = ptr - line;
        memcpy(patch->old_sha1_prefix, line, len);
@@ -383,7 +384,7 @@ static int gitdiff_index(const char *line, struct patch *patch)
                ptr = eol;
        len = ptr - line;
 
-       if (40 <= len)
+       if (40 < len)
                return 0;
        memcpy(patch->new_sha1_prefix, line, len);
        patch->new_sha1_prefix[len] = 0;
@@ -894,7 +895,8 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
                static const char binhdr[] = "Binary files ";
 
                if (sizeof(binhdr) - 1 < size - offset - hdrsize &&
-                   !memcmp(binhdr, buffer + hdrsize, sizeof(binhdr)-1))
+                   !memcmp(binhdr, buffer + hdrsize + offset,
+                           sizeof(binhdr)-1))
                        patch->is_binary = 1;
 
                if (patch->is_binary && !apply && !check)
@@ -1112,8 +1114,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
                                break;
                /* Fall-through for ' ' */
                case '+':
-                       memcpy(new + newsize, patch + 1, plen);
-                       newsize += plen;
+                       if (*patch != '+' || !no_add) {
+                               memcpy(new + newsize, patch + 1, plen);
+                               newsize += plen;
+                       }
                        break;
                case '@': case '\\':
                        /* Ignore it, we already handled it */
@@ -1710,6 +1714,10 @@ int main(int argc, char **argv)
                        excludes = x;
                        continue;
                }
+               if (!strcmp(arg, "--no-add")) {
+                       no_add = 1;
+                       continue;
+               }
                if (!strcmp(arg, "--stat")) {
                        apply = 0;
                        diffstat = 1;