From 56d33b1105e63590577672de9afb920f6e7fd22c Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 3 Oct 2005 13:16:39 -0700 Subject: git-apply: allow operating in sparsely populated working tree. This patch teaches 'git-apply --index' to automatically check out a file being patched. This happens only when the working tree does not have it checked out. Signed-off-by: Junio C Hamano --- apply.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'apply.c') diff --git a/apply.c b/apply.c index f8862722fd..79c5d8a8f8 100644 --- a/apply.c +++ b/apply.c @@ -676,7 +676,10 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s /* We allow "\ No newline at end of file". Depending * on locale settings when the patch was produced we * don't know what this line looks like. The only - * thing we do know is that it begins with "\ ". */ + * thing we do know is that it begins with "\ ". + * Checking for 12 is just for sanity check -- any + * l10n of "\ No newline..." is at least that long. + */ case '\\': if (len < 12 || memcmp(line, "\\ ", 2)) return -1; @@ -1030,17 +1033,39 @@ static int check_patch(struct patch *patch) if (old_name) { int changed; + int stat_ret = lstat(old_name, &st); - if (lstat(old_name, &st) < 0) - return error("%s: %s", old_name, strerror(errno)); if (check_index) { int pos = cache_name_pos(old_name, strlen(old_name)); if (pos < 0) - return error("%s: does not exist in index", old_name); + return error("%s: does not exist in index", + old_name); + if (stat_ret < 0) { + struct checkout costate; + if (errno != ENOENT) + return error("%s: %s", old_name, + strerror(errno)); + /* checkout */ + costate.base_dir = ""; + costate.base_dir_len = 0; + costate.force = 0; + costate.quiet = 0; + costate.not_new = 0; + costate.refresh_cache = 1; + if (checkout_entry(active_cache[pos], + &costate) || + lstat(old_name, &st)) + return -1; + } + changed = ce_match_stat(active_cache[pos], &st); if (changed) - return error("%s: does not match index", old_name); + return error("%s: does not match index", + old_name); } + else if (stat_ret < 0) + return error("%s: %s", old_name, strerror(errno)); + if (patch->is_new < 0) patch->is_new = 0; st.st_mode = ntohl(create_ce_mode(st.st_mode)); -- cgit v1.3-5-g9baa From c2d5036de5c2e21fb736002ae5b385ceb7c6b23b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 3 Oct 2005 22:28:45 -0700 Subject: git-apply: retire unused/unimplemented --no-merge flag. The original plan was to do 3-way merge between local working tree, index and the patch being applied, but that was never implemented. Retire the flag to control its behaviour. Signed-off-by: Junio C Hamano --- Documentation/git-apply.txt | 7 +------ apply.c | 17 +---------------- 2 files changed, 2 insertions(+), 22 deletions(-) (limited to 'apply.c') diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt index 4f7e1195de..cb676348f8 100644 --- a/Documentation/git-apply.txt +++ b/Documentation/git-apply.txt @@ -9,7 +9,7 @@ git-apply - Apply patch on a GIT index file and a work tree SYNOPSIS -------- -'git-apply' [--no-merge] [--stat] [--summary] [--check] [--index] [--show-files] [--apply] [...] +'git-apply' [--stat] [--summary] [--check] [--index] [--show-files] [--apply] [...] DESCRIPTION ----------- @@ -22,11 +22,6 @@ OPTIONS The files to read patch from. '-' can be used to read from the standard input. ---no-merge:: - The default mode of operation is the merge behaviour - which is not implemented yet. This flag explicitly - tells the program not to use the merge behaviour. - --stat:: Instead of applying the patch, output diffstat for the input. Turns off "apply". diff --git a/apply.c b/apply.c index 79c5d8a8f8..7be5041353 100644 --- a/apply.c +++ b/apply.c @@ -5,26 +5,16 @@ * * 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 #include #include "cache.h" -// We default to the merge behaviour, since that's what most people would -// expect. -// // --check turns on checking that the working tree matches the // files that are being modified, but doesn't apply the patch // --stat does just a diffstat, and doesn't actually apply // --show-files shows the directory changes // -static int merge_patch = 1; static int check_index = 0; static int write_index = 0; static int diffstat = 0; @@ -33,7 +23,7 @@ static int check = 0; static int apply = 1; static int show_files = 0; static const char apply_usage[] = -"git-apply [--no-merge] [--stat] [--summary] [--check] [--index] [--apply] [--show-files] ..."; +"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] ..."; /* * For "diff-stat" like behaviour, we keep track of the biggest change @@ -1517,11 +1507,6 @@ int main(int argc, char **argv) excludes = x; continue; } - /* NEEDSWORK: this does not do anything at this moment. */ - if (!strcmp(arg, "--no-merge")) { - merge_patch = 0; - continue; - } if (!strcmp(arg, "--stat")) { apply = 0; diffstat = 1; -- cgit v1.3-5-g9baa From 2cf67f1e35d3a2175e426b4b2841374e68105c2c Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 7 Oct 2005 03:42:00 -0700 Subject: git-apply: parse index information Add an new option --show-index-info to git-apply command to summarize the index information new git-diff outputs. The command shows something similar to git-ls-files --stage output for the pre-change image: 100644 7be5041... apply.c 100644 ec2a161... cache.h ... Signed-off-by: Junio C Hamano --- apply.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) (limited to 'apply.c') diff --git a/apply.c b/apply.c index 7be5041353..155fbe84da 100644 --- a/apply.c +++ b/apply.c @@ -14,6 +14,7 @@ // files that are being modified, but doesn't apply the patch // --stat does just a diffstat, and doesn't actually apply // --show-files shows the directory changes +// --show-index-info shows the old and new index info for paths if available. // static int check_index = 0; static int write_index = 0; @@ -22,8 +23,9 @@ static int summary = 0; static int check = 0; static int apply = 1; static int show_files = 0; +static int show_index_info = 0; static const char apply_usage[] = -"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] ..."; +"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--show-index-info] ..."; /* * For "diff-stat" like behaviour, we keep track of the biggest change @@ -56,6 +58,8 @@ struct patch { struct fragment *fragments; char *result; unsigned long resultsize; + char old_sha1_prefix[41]; + char new_sha1_prefix[41]; struct patch *next; }; @@ -334,6 +338,38 @@ static int gitdiff_dissimilarity(const char *line, struct patch *patch) return 0; } +static int gitdiff_index(const char *line, struct patch *patch) +{ + /* index line is N hexadecimal, "..", N hexadecimal, + * and optional space with octal mode. + */ + const char *ptr, *eol; + int len; + + ptr = strchr(line, '.'); + if (!ptr || ptr[1] != '.' || 40 <= ptr - line) + return 0; + len = ptr - line; + memcpy(patch->old_sha1_prefix, line, len); + patch->old_sha1_prefix[len] = 0; + + line = ptr + 2; + ptr = strchr(line, ' '); + eol = strchr(line, '\n'); + + if (!ptr || eol < ptr) + ptr = eol; + len = ptr - line; + + if (40 <= len) + return 0; + memcpy(patch->new_sha1_prefix, line, len); + patch->new_sha1_prefix[len] = 0; + if (*ptr == ' ') + patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8); + return 0; +} + /* * This is normal for a diff that doesn't change anything: we'll fall through * into the next diff. Tell the parser to break out. @@ -438,6 +474,7 @@ static int parse_git_header(char *line, int len, unsigned int size, struct patch { "rename to ", gitdiff_renamedst }, { "similarity index ", gitdiff_similarity }, { "dissimilarity index ", gitdiff_dissimilarity }, + { "index ", gitdiff_index }, { "", gitdiff_unrecognized }, }; int i; @@ -1136,6 +1173,36 @@ static void show_file_list(struct patch *patch) } } +static inline int is_null_sha1(const unsigned char *sha1) +{ + return !memcmp(sha1, null_sha1, 20); +} + +static void show_index_list(struct patch *list) +{ + struct patch *patch; + + /* Once we start supporting the reverse patch, it may be + * worth showing the new sha1 prefix, but until then... + */ + for (patch = list; patch; patch = patch->next) { + const unsigned char *sha1_ptr; + unsigned char sha1[20]; + const char *name; + + name = patch->old_name ? patch->old_name : patch->new_name; + if (patch->is_new) + sha1_ptr = null_sha1; + else if (get_sha1(patch->old_sha1_prefix, sha1)) + die("sha1 information is lacking or useless (%s).", + name); + else + sha1_ptr = sha1; + printf("%06o %s %s\n",patch->old_mode, + sha1_to_hex(sha1_ptr), name); + } +} + static void stat_patch_list(struct patch *patch) { int files, adds, dels; @@ -1476,6 +1543,9 @@ static int apply_patch(int fd) if (show_files) show_file_list(list); + if (show_index_info) + show_index_list(list); + if (diffstat) stat_patch_list(list); @@ -1534,6 +1604,11 @@ int main(int argc, char **argv) show_files = 1; continue; } + if (!strcmp(arg, "--show-index-info")) { + apply = 0; + show_index_info = 1; + continue; + } fd = open(arg, O_RDONLY); if (fd < 0) usage(apply_usage); -- cgit v1.3-5-g9baa From 4546738b58a0134eef154231b07d60fc174d56e3 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 13 Oct 2005 11:03:18 -0700 Subject: Unlocalized isspace and friends Do our own ctype.h, just to get the sane semantics: we want locale-independence, _and_ we want the right signed behaviour. Plus we only use a very small subset of ctype.h anyway (isspace, isalpha, isdigit and isalnum). Signed-off-by: Junio C Hamano --- Makefile | 3 ++- apply.c | 1 - cache.h | 26 ++++++++++++++++++++++++++ commit-tree.c | 1 - commit.c | 1 - config.c | 1 - convert-objects.c | 1 - ctype.c | 23 +++++++++++++++++++++++ date.c | 1 - diff-tree.c | 1 - ident.c | 1 - mailsplit.c | 1 - pack-objects.c | 1 - patch-id.c | 1 - refs.c | 1 - update-ref.c | 1 - 16 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 ctype.c (limited to 'apply.c') diff --git a/Makefile b/Makefile index e2e87f6beb..9fe65ba2be 100644 --- a/Makefile +++ b/Makefile @@ -159,7 +159,8 @@ LIB_OBJS = \ object.o pack-check.o patch-delta.o path.o pkt-line.o \ quote.o read-cache.o refs.o run-command.o \ server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \ - tag.o tree.o usage.o config.o environment.o $(DIFF_OBJS) + tag.o tree.o usage.o config.o environment.o ctype.o \ + $(DIFF_OBJS) LIBS = $(LIB_FILE) LIBS += -lz diff --git a/apply.c b/apply.c index 155fbe84da..f4d00f2835 100644 --- a/apply.c +++ b/apply.c @@ -6,7 +6,6 @@ * This applies patches on top of some (arbitrary) version of the SCM. * */ -#include #include #include "cache.h" diff --git a/cache.h b/cache.h index 328658235b..f1d15ab3c9 100644 --- a/cache.h +++ b/cache.h @@ -387,4 +387,30 @@ extern int git_config_bool(const char *, const char *); extern char git_default_email[MAX_GITNAME]; extern char git_default_name[MAX_GITNAME]; +/* Sane ctype - no locale, and works with signed chars */ +#undef isspace +#undef isdigit +#undef isalpha +#undef isalnum +#undef tolower +#undef toupper +extern unsigned char sane_ctype[256]; +#define GIT_SPACE 0x01 +#define GIT_DIGIT 0x02 +#define GIT_ALPHA 0x04 +#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0) +#define isspace(x) sane_istest(x,GIT_SPACE) +#define isdigit(x) sane_istest(x,GIT_DIGIT) +#define isalpha(x) sane_istest(x,GIT_ALPHA) +#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) +#define tolower(x) sane_case((unsigned char)(x), 0x20) +#define toupper(x) sane_case((unsigned char)(x), 0) + +static inline int sane_case(int x, int high) +{ + if (sane_istest(x, GIT_ALPHA)) + x = (x & ~0x20) | high; + return x; +} + #endif /* CACHE_H */ diff --git a/commit-tree.c b/commit-tree.c index 030fb704e5..ea0fdd44e2 100644 --- a/commit-tree.c +++ b/commit-tree.c @@ -7,7 +7,6 @@ #include #include -#include #define BLOCKING (1ul << 14) diff --git a/commit.c b/commit.c index f735f981bb..8f403180e5 100644 --- a/commit.c +++ b/commit.c @@ -1,4 +1,3 @@ -#include #include "tag.h" #include "commit.h" #include "cache.h" diff --git a/config.c b/config.c index 9b7c6f2942..519fecfee4 100644 --- a/config.c +++ b/config.c @@ -1,4 +1,3 @@ -#include #include "cache.h" diff --git a/convert-objects.c b/convert-objects.c index 9ad0c77678..a892013f0f 100644 --- a/convert-objects.c +++ b/convert-objects.c @@ -1,6 +1,5 @@ #define _XOPEN_SOURCE /* glibc2 needs this */ #include -#include #include "cache.h" struct entry { diff --git a/ctype.c b/ctype.c new file mode 100644 index 0000000000..56bdffa636 --- /dev/null +++ b/ctype.c @@ -0,0 +1,23 @@ +/* + * Sane locale-independent, ASCII ctype. + * + * No surprises, and works with signed and unsigned chars. + */ +#include "cache.h" + +#define SS GIT_SPACE +#define AA GIT_ALPHA +#define DD GIT_DIGIT + +unsigned char sane_ctype[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, SS, SS, 0, 0, SS, 0, 0, /* 0-15 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16-15 */ + SS, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 32-15 */ + DD, DD, DD, DD, DD, DD, DD, DD, DD, DD, 0, 0, 0, 0, 0, 0, /* 48-15 */ + 0, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, /* 64-15 */ + AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, 0, 0, 0, 0, 0, /* 80-15 */ + 0, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, /* 96-15 */ + AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, 0, 0, 0, 0, 0, /* 112-15 */ + /* Nothing in the 128.. range */ +}; + diff --git a/date.c b/date.c index b21cadc4d6..63f5a09197 100644 --- a/date.c +++ b/date.c @@ -4,7 +4,6 @@ * Copyright (C) Linus Torvalds, 2005 */ -#include #include #include "cache.h" diff --git a/diff-tree.c b/diff-tree.c index 2203fa56d0..851722037d 100644 --- a/diff-tree.c +++ b/diff-tree.c @@ -1,4 +1,3 @@ -#include #include "cache.h" #include "diff.h" #include "commit.h" diff --git a/ident.c b/ident.c index 7a9f5672eb..1bfbc6ff35 100644 --- a/ident.c +++ b/ident.c @@ -9,7 +9,6 @@ #include #include -#include static char git_default_date[50]; diff --git a/mailsplit.c b/mailsplit.c index 0f8100dcca..189f4ed724 100644 --- a/mailsplit.c +++ b/mailsplit.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include "cache.h" diff --git a/pack-objects.c b/pack-objects.c index 8a1ee746e0..b3e6152033 100644 --- a/pack-objects.c +++ b/pack-objects.c @@ -1,4 +1,3 @@ -#include #include "cache.h" #include "object.h" #include "delta.h" diff --git a/patch-id.c b/patch-id.c index 960e7cedf9..edbc4aa3e8 100644 --- a/patch-id.c +++ b/patch-id.c @@ -1,4 +1,3 @@ -#include #include "cache.h" static void flush_current_id(int patchlen, unsigned char *id, SHA_CTX *c) diff --git a/refs.c b/refs.c index 5a8cbd4ef3..42240d2769 100644 --- a/refs.c +++ b/refs.c @@ -2,7 +2,6 @@ #include "cache.h" #include -#include /* We allow "recursive" symbolic refs. Only within reason, though */ #define MAXDEPTH 5 diff --git a/update-ref.c b/update-ref.c index 4a1704c1a5..65dc3d6385 100644 --- a/update-ref.c +++ b/update-ref.c @@ -1,6 +1,5 @@ #include "cache.h" #include "refs.h" -#include static const char git_update_ref_usage[] = "git-update-ref []"; -- cgit v1.3-5-g9baa From 22943f1a52142b0ef669c959f8b4367e579da968 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 14 Oct 2005 21:54:52 -0700 Subject: Update git-apply to use C-style quoting for funny pathnames. Signed-off-by: Junio C Hamano --- apply.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++++++------------ git-am.sh | 4 +- 2 files changed, 186 insertions(+), 46 deletions(-) (limited to 'apply.c') diff --git a/apply.c b/apply.c index f4d00f2835..a7ab972940 100644 --- a/apply.c +++ b/apply.c @@ -8,12 +8,13 @@ */ #include #include "cache.h" +#include "quote.h" // --check turns on checking that the working tree matches the // files that are being modified, but doesn't apply the patch // --stat does just a diffstat, and doesn't actually apply // --show-files shows the directory changes -// --show-index-info shows the old and new index info for paths if available. +// --index-info shows the old and new index info for paths if available. // static int check_index = 0; static int write_index = 0; @@ -23,8 +24,9 @@ static int check = 0; static int apply = 1; static int show_files = 0; static int show_index_info = 0; +static int line_termination = '\n'; static const char apply_usage[] = -"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--show-index-info] ..."; +"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--index-info] [-z] ..."; /* * For "diff-stat" like behaviour, we keep track of the biggest change @@ -135,6 +137,35 @@ static char * find_name(const char *line, char *def, int p_value, int terminate) const char *start = line; char *name; + if (*line == '"') { + /* Proposed "new-style" GNU patch/diff format; see + * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2 + */ + name = unquote_c_style(line, NULL); + if (name) { + char *cp = name; + while (p_value) { + cp = strchr(name, '/'); + if (!cp) + break; + cp++; + p_value--; + } + if (cp) { + /* name can later be freed, so we need + * to memmove, not just return cp + */ + memmove(name, cp, strlen(cp) + 1); + free(def); + return name; + } + else { + free(name); + name = NULL; + } + } + } + for (;;) { char c = *line; @@ -224,37 +255,29 @@ static int gitdiff_hdrend(const char *line, struct patch *patch) */ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew) { - int len; - const char *name; - if (!orig_name && !isnull) return find_name(line, NULL, 1, 0); - name = "/dev/null"; - len = 9; if (orig_name) { + int len; + const char *name; + char *another; name = orig_name; len = strlen(name); if (isnull) die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr); - } - - if (*name == '/') - goto absolute_path; - - for (;;) { - char c = *line++; - if (c == '\n') - break; - if (c != '/') - continue; -absolute_path: - if (memcmp(line, name, len) || line[len] != '\n') - break; + another = find_name(line, NULL, 1, 0); + if (!another || memcmp(another, name, len)) + die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr); + free(another); return orig_name; } - die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr); - return NULL; + else { + /* expect "/dev/null" */ + if (memcmp("/dev/null", line, 9) || line[9] != '\n') + die("git-apply: bad git-diff - expected /dev/null on line %d", linenr); + return NULL; + } } static int gitdiff_oldname(const char *line, struct patch *patch) @@ -378,29 +401,124 @@ static int gitdiff_unrecognized(const char *line, struct patch *patch) return -1; } -static char *git_header_name(char *line) +static const char *stop_at_slash(const char *line, int llen) +{ + int i; + + for (i = 0; i < llen; i++) { + int ch = line[i]; + if (ch == '/') + return line + i; + } + return NULL; +} + +/* This is to extract the same name that appears on "diff --git" + * line. We do not find and return anything if it is a rename + * patch, and it is OK because we will find the name elsewhere. + * We need to reliably find name only when it is mode-change only, + * creation or deletion of an empty file. In any of these cases, + * both sides are the same name under a/ and b/ respectively. + */ +static char *git_header_name(char *line, int llen) { int len; - char *name, *second; + const char *name; + const char *second = NULL; - /* - * Find the first '/' - */ - name = line; - for (;;) { - char c = *name++; - if (c == '\n') + line += strlen("diff --git "); + llen -= strlen("diff --git "); + + if (*line == '"') { + const char *cp; + char *first = unquote_c_style(line, &second); + if (!first) return NULL; - if (c == '/') - break; + + /* advance to the first slash */ + cp = stop_at_slash(first, strlen(first)); + if (!cp || cp == first) { + /* we do not accept absolute paths */ + free_first_and_fail: + free(first); + return NULL; + } + len = strlen(cp+1); + memmove(first, cp+1, len+1); /* including NUL */ + + /* second points at one past closing dq of name. + * find the second name. + */ + while ((second < line + llen) && isspace(*second)) + second++; + + if (line + llen <= second) + goto free_first_and_fail; + if (*second == '"') { + char *sp = unquote_c_style(second, NULL); + if (!sp) + goto free_first_and_fail; + cp = stop_at_slash(sp, strlen(sp)); + if (!cp || cp == sp) { + free_both_and_fail: + free(sp); + goto free_first_and_fail; + } + /* They must match, otherwise ignore */ + if (strcmp(cp+1, first)) + goto free_both_and_fail; + free(sp); + return first; + } + + /* unquoted second */ + cp = stop_at_slash(second, line + llen - second); + if (!cp || cp == second) + goto free_first_and_fail; + cp++; + if (line + llen - cp != len + 1 || + memcmp(first, cp, len)) + goto free_first_and_fail; + return first; } - /* - * We don't accept absolute paths (/dev/null) as possibly valid - */ - if (name == line+1) + /* unquoted first name */ + name = stop_at_slash(line, llen); + if (!name || name == line) return NULL; + name++; + + /* since the first name is unquoted, a dq if exists must be + * the beginning of the second name. + */ + for (second = name; second < line + llen; second++) { + if (*second == '"') { + const char *cp = second; + const char *np; + char *sp = unquote_c_style(second, NULL); + + if (!sp) + return NULL; + np = stop_at_slash(sp, strlen(sp)); + if (!np || np == sp) { + free_second_and_fail: + free(sp); + return NULL; + } + np++; + len = strlen(np); + if (len < cp - name && + !strncmp(np, name, len) && + isspace(name[len])) { + /* Good */ + memmove(sp, np, len + 1); + return sp; + } + goto free_second_and_fail; + } + } + /* * Accept a name only if it shows up twice, exactly the same * form. @@ -448,7 +566,7 @@ static int parse_git_header(char *line, int len, unsigned int size, struct patch * or removing or adding empty files), so we get * the default name from the header. */ - patch->def_name = git_header_name(line + strlen("diff --git ")); + patch->def_name = git_header_name(line, len); line += len; size -= len; @@ -785,11 +903,18 @@ static void show_stats(struct patch *patch) { const char *prefix = ""; char *name = patch->new_name; + char *qname = NULL; int len, max, add, del, total; if (!name) name = patch->old_name; + if (0 < (len = quote_c_style(name, NULL, NULL, 0))) { + qname = xmalloc(len + 1); + quote_c_style(name, qname, NULL, 0); + name = qname; + } + /* * "scale" the filename */ @@ -827,6 +952,8 @@ static void show_stats(struct patch *patch) printf(" %s%-*s |%5d %.*s%.*s\n", prefix, len, name, patch->lines_added + patch->lines_deleted, add, pluses, del, minuses); + if (qname) + free(qname); } static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size) @@ -1197,8 +1324,13 @@ static void show_index_list(struct patch *list) name); else sha1_ptr = sha1; - printf("%06o %s %s\n",patch->old_mode, - sha1_to_hex(sha1_ptr), name); + + printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr)); + if (line_termination && quote_c_style(name, NULL, NULL, 0)) + quote_c_style(name, NULL, stdout, 0); + else + fputs(name, stdout); + putchar(line_termination); } } @@ -1301,12 +1433,16 @@ static void patch_stats(struct patch *patch) if (lines > max_change) max_change = lines; if (patch->old_name) { - int len = strlen(patch->old_name); + int len = quote_c_style(patch->old_name, NULL, NULL, 0); + if (!len) + len = strlen(patch->old_name); if (len > max_len) max_len = len; } if (patch->new_name) { - int len = strlen(patch->new_name); + int len = quote_c_style(patch->new_name, NULL, NULL, 0); + if (!len) + len = strlen(patch->new_name); if (len > max_len) max_len = len; } @@ -1603,11 +1739,15 @@ int main(int argc, char **argv) show_files = 1; continue; } - if (!strcmp(arg, "--show-index-info")) { + if (!strcmp(arg, "--index-info")) { apply = 0; show_index_info = 1; continue; } + if (!strcmp(arg, "-z")) { + line_termination = 0; + continue; + } fd = open(arg, O_RDONLY); if (fd < 0) usage(apply_usage); diff --git a/git-am.sh b/git-am.sh index c562159a18..cb4bef5adb 100755 --- a/git-am.sh +++ b/git-am.sh @@ -35,10 +35,10 @@ fall_back_3way () { mkdir "$dotest/patch-merge-tmp-dir" # First see if the patch records the index info that we can use. - if git-apply --show-index-info "$dotest/patch" \ + if git-apply -z --index-info "$dotest/patch" \ >"$dotest/patch-merge-index-info" 2>/dev/null && GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ - git-update-index --index-info <"$dotest/patch-merge-index-info" && + git-update-index -z --index-info <"$dotest/patch-merge-index-info" && GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ git-write-tree >"$dotest/patch-merge-base+" && # index has the base tree now. -- cgit v1.3-5-g9baa From 58452f9442a1fa20e0fde5d200091b7f02e62cea Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 16 Oct 2005 22:06:16 -0700 Subject: git-apply: remove unused --show-files flag. Linus says he does not use it (and the thinking behind its initial introduction), and neither Cogito nor StGIT uses it. Signed-off-by: Junio C Hamano --- Documentation/git-apply.txt | 5 +---- apply.c | 40 +--------------------------------------- 2 files changed, 2 insertions(+), 43 deletions(-) (limited to 'apply.c') diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt index e928088e44..fd38ee5b19 100644 --- a/Documentation/git-apply.txt +++ b/Documentation/git-apply.txt @@ -8,7 +8,7 @@ git-apply - Apply patch on a git index file and a work tree SYNOPSIS -------- -'git-apply' [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--index-info] [-z] [...] +'git-apply' [--stat] [--summary] [--check] [--index] [--apply] [--index-info] [-z] [...] DESCRIPTION ----------- @@ -45,9 +45,6 @@ OPTIONS up-to-date, it is flagged as an error. This flag also causes the index file to be updated. ---show-files:: - Show summary of files that are affected by the patch. - --index-info:: Newer git-diff output has embedded 'index information' for each blob to help identify the original version that diff --git a/apply.c b/apply.c index a7ab972940..e5c0b7d074 100644 --- a/apply.c +++ b/apply.c @@ -13,7 +13,6 @@ // --check turns on checking that the working tree matches the // files that are being modified, but doesn't apply the patch // --stat does just a diffstat, and doesn't actually apply -// --show-files shows the directory changes // --index-info shows the old and new index info for paths if available. // static int check_index = 0; @@ -22,11 +21,10 @@ static int diffstat = 0; static int summary = 0; static int check = 0; static int apply = 1; -static int show_files = 0; static int show_index_info = 0; static int line_termination = '\n'; static const char apply_usage[] = -"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--index-info] [-z] ..."; +"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--index-info] [-z] ..."; /* * For "diff-stat" like behaviour, we keep track of the biggest change @@ -1270,35 +1268,6 @@ static int check_patch_list(struct patch *patch) return error; } -static void show_file(int c, unsigned int mode, const char *name) -{ - printf("%c %o %s\n", c, mode, name); -} - -static void show_file_list(struct patch *patch) -{ - for (;patch ; patch = patch->next) { - if (patch->is_rename) { - show_file('-', patch->old_mode, patch->old_name); - show_file('+', patch->new_mode, patch->new_name); - continue; - } - if (patch->is_copy || patch->is_new) { - show_file('+', patch->new_mode, patch->new_name); - continue; - } - if (patch->is_delete) { - show_file('-', patch->old_mode, patch->old_name); - continue; - } - if (patch->old_mode && patch->new_mode && patch->old_mode != patch->new_mode) { - printf("M %o:%o %s\n", patch->old_mode, patch->new_mode, patch->old_name); - continue; - } - printf("M %o %s\n", patch->old_mode, patch->old_name); - } -} - static inline int is_null_sha1(const unsigned char *sha1) { return !memcmp(sha1, null_sha1, 20); @@ -1675,9 +1644,6 @@ static int apply_patch(int fd) die("Unable to write new cachefile"); } - if (show_files) - show_file_list(list); - if (show_index_info) show_index_list(list); @@ -1735,10 +1701,6 @@ int main(int argc, char **argv) apply = 1; continue; } - if (!strcmp(arg, "--show-files")) { - show_files = 1; - continue; - } if (!strcmp(arg, "--index-info")) { apply = 0; show_index_info = 1; -- cgit v1.3-5-g9baa From 7d8b7c21c9c0bee0d94fcf3b350e24ebdb920b74 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 28 Oct 2005 02:43:31 -0700 Subject: git-apply --numstat The new option, --numstat, shows number of inserted and deleted lines for each path. It is similar to --stat output but is meant to be more machine friendly by giving number of added and deleted lines and unabbreviated paths. Signed-off-by: Junio C Hamano --- Documentation/git-apply.txt | 8 +++++++- apply.c | 26 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) (limited to 'apply.c') diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt index fd38ee5b19..eb8f906837 100644 --- a/Documentation/git-apply.txt +++ b/Documentation/git-apply.txt @@ -8,7 +8,7 @@ git-apply - Apply patch on a git index file and a work tree SYNOPSIS -------- -'git-apply' [--stat] [--summary] [--check] [--index] [--apply] [--index-info] [-z] [...] +'git-apply' [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--index-info] [-z] [...] DESCRIPTION ----------- @@ -25,6 +25,12 @@ OPTIONS Instead of applying the patch, output diffstat for the input. Turns off "apply". +--numstat:: + Similar to \--stat, but shows number of added and + deleted lines in decimal notation and pathname without + abbreviation, to make it more machine friendly. Turns + off "apply". + --summary:: Instead of applying the patch, output a condensed summary of information obtained from git diff extended diff --git a/apply.c b/apply.c index e5c0b7d074..3e53b34381 100644 --- a/apply.c +++ b/apply.c @@ -13,18 +13,20 @@ // --check turns on checking that the working tree matches the // files that are being modified, but doesn't apply the patch // --stat does just a diffstat, and doesn't actually apply +// --numstat does numeric diffstat, and doesn't actually apply // --index-info shows the old and new index info for paths if available. // static int check_index = 0; static int write_index = 0; static int diffstat = 0; +static int numstat = 0; static int summary = 0; static int check = 0; static int apply = 1; static int show_index_info = 0; static int line_termination = '\n'; static const char apply_usage[] = -"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--index-info] [-z] ..."; +"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--index-info] [-z] ..."; /* * For "diff-stat" like behaviour, we keep track of the biggest change @@ -1317,6 +1319,20 @@ static void stat_patch_list(struct patch *patch) printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels); } +static void numstat_patch_list(struct patch *patch) +{ + for ( ; patch; patch = patch->next) { + const char *name; + name = patch->old_name ? patch->old_name : patch->new_name; + printf("%d\t%d\t", patch->lines_added, patch->lines_deleted); + if (line_termination && quote_c_style(name, NULL, NULL, 0)) + quote_c_style(name, NULL, stdout, 0); + else + fputs(name, stdout); + putchar('\n'); + } +} + static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name) { if (mode) @@ -1650,6 +1666,9 @@ static int apply_patch(int fd) if (diffstat) stat_patch_list(list); + if (numstat) + numstat_patch_list(list); + if (summary) summary_patch_list(list); @@ -1683,6 +1702,11 @@ int main(int argc, char **argv) diffstat = 1; continue; } + if (!strcmp(arg, "--numstat")) { + apply = 0; + numstat = 1; + continue; + } if (!strcmp(arg, "--summary")) { apply = 0; summary = 1; -- cgit v1.3-5-g9baa