From 6c510bee2013022fbce52f4b0ec0cc593fc0cc48 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Tue, 13 Feb 2007 11:07:23 -0800 Subject: [PATCH 01/14] Lazy man's auto-CRLF It currently does NOT know about file attributes, so it does its conversion purely based on content. Maybe that is more in the "git philosophy" anyway, since content is king, but I think we should try to do the file attributes to turn it off on demand. Anyway, BY DEFAULT it is off regardless, because it requires a [core] AutoCRLF = true in your config file to be enabled. We could make that the default for Windows, of course, the same way we do some other things (filemode etc). But you can actually enable it on UNIX, and it will cause: - "git update-index" will write blobs without CRLF - "git diff" will diff working tree files without CRLF - "git checkout" will write files to the working tree _with_ CRLF and things work fine. Funnily, it actually shows an odd file in git itself: git clone -n git test-crlf cd test-crlf git config core.autocrlf true git checkout git diff shows a diff for "Documentation/docbook-xsl.css". Why? Because we have actually checked in that file *with* CRLF! So when "core.autocrlf" is true, we'll always generate a *different* hash for it in the index, because the index hash will be for the content _without_ CRLF. Is this complete? I dunno. It seems to work for me. It doesn't use the filename at all right now, and that's probably a deficiency (we could certainly make the "is_binary()" heuristics also take standard filename heuristics into account). I don't pass in the filename at all for the "index_fd()" case (git-update-index), so that would need to be passed around, but this actually works fine. NOTE NOTE NOTE! The "is_binary()" heuristics are totally made-up by yours truly. I will not guarantee that they work at all reasonable. Caveat emptor. But it _is_ simple, and it _is_ safe, since it's all off by default. The patch is pretty simple - the biggest part is the new "convert.c" file, but even that is really just basic stuff that anybody can write in "Teaching C 101" as a final project for their first class in programming. Not to say that it's bug-free, of course - but at least we're not talking about rocket surgery here. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- Makefile | 3 +- cache.h | 5 ++ config.c | 5 ++ convert.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++ diff.c | 17 ++++- entry.c | 16 ++++- environment.c | 1 + sha1_file.c | 23 ++++++- 8 files changed, 251 insertions(+), 5 deletions(-) create mode 100644 convert.c diff --git a/Makefile b/Makefile index 40bdcff696..60496ff957 100644 --- a/Makefile +++ b/Makefile @@ -262,7 +262,8 @@ LIB_OBJS = \ revision.o pager.o tree-walk.o xdiff-interface.o \ write_or_die.o trace.o list-objects.o grep.o \ alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \ - color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o + color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \ + convert.o BUILTIN_OBJS = \ builtin-add.o \ diff --git a/cache.h b/cache.h index c62b0b090d..9c019e8bba 100644 --- a/cache.h +++ b/cache.h @@ -201,6 +201,7 @@ extern const char *apply_default_whitespace; extern int zlib_compression_level; extern size_t packed_git_window_size; extern size_t packed_git_limit; +extern int auto_crlf; #define GIT_REPO_VERSION 0 extern int repository_format_version; @@ -468,4 +469,8 @@ extern int nfvasprintf(char **str, const char *fmt, va_list va); extern void trace_printf(const char *format, ...); extern void trace_argv_printf(const char **argv, int count, const char *format, ...); +/* convert.c */ +extern int convert_to_git(const char *path, char **bufp, unsigned long *sizep); +extern int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep); + #endif /* CACHE_H */ diff --git a/config.c b/config.c index d82107124a..ffe02129a5 100644 --- a/config.c +++ b/config.c @@ -324,6 +324,11 @@ int git_default_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.autocrlf")) { + auto_crlf = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "user.name")) { strlcpy(git_default_name, value, sizeof(git_default_name)); return 0; diff --git a/convert.c b/convert.c new file mode 100644 index 0000000000..13beb70582 --- /dev/null +++ b/convert.c @@ -0,0 +1,186 @@ +#include "cache.h" +/* + * convert.c - convert a file when checking it out and checking it in. + * + * This should use the pathname to decide on whether it wants to do some + * more interesting conversions (automatic gzip/unzip, general format + * conversions etc etc), but by default it just does automatic CRLF<->LF + * translation when the "auto_crlf" option is set. + */ + +struct text_stat { + /* CR, LF and CRLF counts */ + unsigned cr, lf, crlf; + + /* These are just approximations! */ + unsigned printable, nonprintable; +}; + +static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats) +{ + unsigned long i; + + memset(stats, 0, sizeof(*stats)); + + for (i = 0; i < size; i++) { + unsigned char c = buf[i]; + if (c == '\r') { + stats->cr++; + if (i+1 < size && buf[i+1] == '\n') + stats->crlf++; + continue; + } + if (c == '\n') { + stats->lf++; + continue; + } + if (c == 127) + /* DEL */ + stats->nonprintable++; + else if (c < 32) { + switch (c) { + /* BS, HT, ESC and FF */ + case '\b': case '\t': case '\033': case '\014': + stats->printable++; + break; + default: + stats->nonprintable++; + } + } + else + stats->printable++; + } +} + +/* + * The same heuristics as diff.c::mmfile_is_binary() + */ +static int is_binary(unsigned long size, struct text_stat *stats) +{ + + if ((stats->printable >> 7) < stats->nonprintable) + return 1; + /* + * Other heuristics? Average line length might be relevant, + * as might LF vs CR vs CRLF counts.. + * + * NOTE! It might be normal to have a low ratio of CRLF to LF + * (somebody starts with a LF-only file and edits it with an editor + * that adds CRLF only to lines that are added..). But do we + * want to support CR-only? Probably not. + */ + return 0; +} + +int convert_to_git(const char *path, char **bufp, unsigned long *sizep) +{ + char *buffer, *nbuf; + unsigned long size, nsize; + struct text_stat stats; + + /* + * FIXME! Other pluggable conversions should go here, + * based on filename patterns. Right now we just do the + * stupid auto-CRLF one. + */ + if (!auto_crlf) + return 0; + + size = *sizep; + if (!size) + return 0; + buffer = *bufp; + + gather_stats(buffer, size, &stats); + + /* No CR? Nothing to convert, regardless. */ + if (!stats.cr) + return 0; + + /* + * We're currently not going to even try to convert stuff + * that has bare CR characters. Does anybody do that crazy + * stuff? + */ + if (stats.cr != stats.crlf) + return 0; + + /* + * And add some heuristics for binary vs text, of course... + */ + if (is_binary(size, &stats)) + return 0; + + /* + * Ok, allocate a new buffer, fill it in, and return true + * to let the caller know that we switched buffers on it. + */ + nsize = size - stats.crlf; + nbuf = xmalloc(nsize); + *bufp = nbuf; + *sizep = nsize; + do { + unsigned char c = *buffer++; + if (c != '\r') + *nbuf++ = c; + } while (--size); + + return 1; +} + +int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep) +{ + char *buffer, *nbuf; + unsigned long size, nsize; + struct text_stat stats; + unsigned char last; + + /* + * FIXME! Other pluggable conversions should go here, + * based on filename patterns. Right now we just do the + * stupid auto-CRLF one. + */ + if (!auto_crlf) + return 0; + + size = *sizep; + if (!size) + return 0; + buffer = *bufp; + + gather_stats(buffer, size, &stats); + + /* No LF? Nothing to convert, regardless. */ + if (!stats.lf) + return 0; + + /* Was it already in CRLF format? */ + if (stats.lf == stats.crlf) + return 0; + + /* If we have any bare CR characters, we're not going to touch it */ + if (stats.cr != stats.crlf) + return 0; + + if (is_binary(size, &stats)) + return 0; + + /* + * Ok, allocate a new buffer, fill it in, and return true + * to let the caller know that we switched buffers on it. + */ + nsize = size + stats.lf - stats.crlf; + nbuf = xmalloc(nsize); + *bufp = nbuf; + *sizep = nsize; + last = 0; + do { + unsigned char c = *buffer++; + if (c == '\n' && last != '\r') + *nbuf++ = '\r'; + *nbuf++ = c; + last = c; + } while (--size); + + return 1; +} diff --git a/diff.c b/diff.c index 13b9b6c560..561587cace 100644 --- a/diff.c +++ b/diff.c @@ -1332,6 +1332,9 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only) reuse_worktree_file(s->path, s->sha1, 0)) { struct stat st; int fd; + char *buf; + unsigned long size; + if (lstat(s->path, &st) < 0) { if (errno == ENOENT) { err_empty: @@ -1364,7 +1367,19 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only) s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); s->should_munmap = 1; - /* FIXME! CRLF -> LF conversion goes here, based on "s->path" */ + + /* + * Convert from working tree format to canonical git format + */ + buf = s->data; + size = s->size; + if (convert_to_git(s->path, &buf, &size)) { + munmap(s->data, s->size); + s->should_munmap = 0; + s->data = buf; + s->size = size; + s->should_free = 1; + } } else { char type[20]; diff --git a/entry.c b/entry.c index c2641ddefd..472a9ef321 100644 --- a/entry.c +++ b/entry.c @@ -78,6 +78,9 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat path, sha1_to_hex(ce->sha1)); } switch (ntohl(ce->ce_mode) & S_IFMT) { + char *buf; + unsigned long nsize; + case S_IFREG: if (to_tempfile) { strcpy(path, ".merge_file_XXXXXX"); @@ -89,7 +92,18 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat return error("git-checkout-index: unable to create file %s (%s)", path, strerror(errno)); } - /* FIXME: LF -> CRLF conversion goes here, based on "ce->name" */ + + /* + * Convert from git internal format to working tree format + */ + buf = new; + nsize = size; + if (convert_to_working_tree(ce->name, &buf, &nsize)) { + free(new); + new = buf; + size = nsize; + } + wrote = write_in_full(fd, new, size); close(fd); free(new); diff --git a/environment.c b/environment.c index 54c22f8248..2fa0960412 100644 --- a/environment.c +++ b/environment.c @@ -28,6 +28,7 @@ size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE; size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT; int pager_in_use; int pager_use_color = 1; +int auto_crlf = 0; static const char *git_dir; static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file; diff --git a/sha1_file.c b/sha1_file.c index 8ad7fad825..6ec67b2923 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -2082,7 +2082,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con { unsigned long size = st->st_size; void *buf; - int ret; + int ret, re_allocated = 0; buf = ""; if (size) @@ -2091,11 +2091,30 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con if (!type) type = blob_type; - /* FIXME: CRLF -> LF conversion here for blobs! We'll need the path! */ + + /* + * Convert blobs to git internal format + */ + if (!strcmp(type, blob_type)) { + unsigned long nsize = size; + char *nbuf = buf; + if (convert_to_git(NULL, &nbuf, &nsize)) { + if (size) + munmap(buf, size); + size = nsize; + buf = nbuf; + re_allocated = 1; + } + } + if (write_object) ret = write_sha1_file(buf, size, type, sha1); else ret = hash_sha1_file(buf, size, type, sha1); + if (re_allocated) { + free(buf); + return ret; + } if (size) munmap(buf, size); return ret; From d7f4633405acf3dc09798a759463c616c7c49dfd Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Tue, 13 Feb 2007 18:16:12 -0800 Subject: [PATCH 02/14] Make AutoCRLF ternary variable. This allows you to do: [core] AutoCRLF = input and it should do only the CRLF->LF translation (ie it simplifies CRLF only when reading working tree files, but when checking out files, it leaves the LF alone, and doesn't turn it into a CRLF). Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- config.c | 4 ++++ convert.c | 2 +- environment.c | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/config.c b/config.c index ffe02129a5..e8ae919b52 100644 --- a/config.c +++ b/config.c @@ -325,6 +325,10 @@ int git_default_config(const char *var, const char *value) } if (!strcmp(var, "core.autocrlf")) { + if (value && !strcasecmp(value, "input")) { + auto_crlf = -1; + return 0; + } auto_crlf = git_config_bool(var, value); return 0; } diff --git a/convert.c b/convert.c index 13beb70582..898bfe3eb2 100644 --- a/convert.c +++ b/convert.c @@ -140,7 +140,7 @@ int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep) * based on filename patterns. Right now we just do the * stupid auto-CRLF one. */ - if (!auto_crlf) + if (auto_crlf <= 0) return 0; size = *sizep; diff --git a/environment.c b/environment.c index 2fa0960412..570e32ac3c 100644 --- a/environment.c +++ b/environment.c @@ -28,7 +28,7 @@ size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE; size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT; int pager_in_use; int pager_use_color = 1; -int auto_crlf = 0; +int auto_crlf = 0; /* 1: both ways, -1: only when adding git objects */ static const char *git_dir; static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file; From 634ede32ae7d4c76e96e88f9cd5c1b3a70ea08ac Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 14 Feb 2007 14:54:00 -0800 Subject: [PATCH 03/14] t0020: add test for auto-crlf This tests lowlevel of update/checkout codepaths and some patch application. Currently, variants of "git apply" that look at the working tree files does not work, so it does not test the patch application without parameter and with --index parameter when autocrlf is set to produce CRLF files. We should add test for diff generation too. Signed-off-by: Junio C Hamano --- t/t0020-crlf.sh | 206 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100755 t/t0020-crlf.sh diff --git a/t/t0020-crlf.sh b/t/t0020-crlf.sh new file mode 100755 index 0000000000..58a4d86df3 --- /dev/null +++ b/t/t0020-crlf.sh @@ -0,0 +1,206 @@ +#!/bin/sh + +test_description='CRLF conversion' + +. ./test-lib.sh + +append_cr () { + sed -e 's/$/Q/' | tr Q '\015' +} + +remove_cr () { + tr '\015' Q <"$1" | grep Q >/dev/null && + tr '\015' Q <"$1" | sed -ne 's/Q$//p' +} + +test_expect_success setup ' + + git repo-config core.autocrlf false && + + for w in Hello world how are you; do echo $w; done >one && + mkdir dir && + for w in I am very very fine thank you; do echo $w; done >dir/two && + git add . && + + git commit -m initial && + + one=`git rev-parse HEAD:one` && + dir=`git rev-parse HEAD:dir` && + two=`git rev-parse HEAD:dir/two` && + + for w in Some extra lines here; do echo $w; done >>one && + git diff >patch.file && + patched=`git hash-object --stdin tmp && mv -f tmp $f && + git update-index -- $f || { + echo Oops + false + break + } + done && + + differs=`git diff-index --cached HEAD` && + test -z "$differs" || { + echo Oops "$differs" + false + } + +' + +test_expect_success 'update with autocrlf=true' ' + + rm -f tmp one dir/two && + git read-tree --reset -u HEAD && + git repo-config core.autocrlf true && + + for f in one dir/two + do + append_cr <$f >tmp && mv -f tmp $f && + git update-index -- $f || { + echo "Oops $f" + false + break + } + done && + + differs=`git diff-index --cached HEAD` && + test -z "$differs" || { + echo Oops "$differs" + false + } + +' + +test_expect_success 'checkout with autocrlf=true' ' + + rm -f tmp one dir/two && + git repo-config core.autocrlf true && + git read-tree --reset -u HEAD && + + for f in one dir/two + do + remove_cr "$f" >tmp && mv -f tmp $f && + git update-index -- $f || { + echo "Eh? $f" + false + break + } + done && + test "$one" = `git hash-object --stdin /dev/null + then + echo "Eh? $f" + false + break + else + git update-index -- $f + fi + done && + test "$one" = `git hash-object --stdin tmp && mv -f tmp one && + + git apply patch.file && + test "$patched" = "`git hash-object --stdin Date: Sat, 17 Feb 2007 13:12:52 -0800 Subject: [PATCH 04/14] Teach 'git apply' to look at $GIT_DIR/config When neither --index nor --cached was used, git-apply did not try calling setup_git_directory(), which means it did not look at configuration files at all. This fixes it to call the setup function but still allow the command to be run in a directory not controlled by git. The bug probably meant that 'git apply', not moving up to the toplevel, did not apply properly formatted diffs from the toplevel when you are inside a subdirectory, even though 'git apply --index' would. As a side effect, this patch fixes it as well. Signed-off-by: Junio C Hamano --- builtin-apply.c | 21 ++++++---- t/t4119-apply-config.sh | 90 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 8 deletions(-) create mode 100755 t/t4119-apply-config.sh diff --git a/builtin-apply.c b/builtin-apply.c index 3fefdacd94..fc1d6730d9 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -2595,9 +2595,18 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) int read_stdin = 1; int inaccurate_eof = 0; int errs = 0; + int is_not_gitdir = 0; const char *whitespace_option = NULL; + prefix = setup_git_directory_gently(&is_not_gitdir); + prefix_length = prefix ? strlen(prefix) : 0; + if (!is_not_gitdir) { + git_config(git_apply_config); + if (apply_default_whitespace) + parse_whitespace_option(apply_default_whitespace); + } + for (i = 1; i < argc; i++) { const char *arg = argv[i]; char *end; @@ -2648,10 +2657,14 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) continue; } if (!strcmp(arg, "--index")) { + if (is_not_gitdir) + die("--index outside a repository"); check_index = 1; continue; } if (!strcmp(arg, "--cached")) { + if (is_not_gitdir) + die("--cached outside a repository"); check_index = 1; cached = 1; continue; @@ -2700,14 +2713,6 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) inaccurate_eof = 1; continue; } - - if (check_index && prefix_length < 0) { - prefix = setup_git_directory(); - prefix_length = prefix ? strlen(prefix) : 0; - git_config(git_apply_config); - if (!whitespace_option && apply_default_whitespace) - parse_whitespace_option(apply_default_whitespace); - } if (0 < prefix_length) arg = prefix_filename(prefix, prefix_length, arg); diff --git a/t/t4119-apply-config.sh b/t/t4119-apply-config.sh new file mode 100755 index 0000000000..0e8ea7e2b8 --- /dev/null +++ b/t/t4119-apply-config.sh @@ -0,0 +1,90 @@ +#!/bin/sh +# +# Copyright (c) 2007 Junio C Hamano +# + +test_description='git-apply --whitespace=strip and configuration file. + +' + +. ./test-lib.sh + +test_expect_success setup ' + echo A >file1 && + cp file1 saved && + git add file1 && + echo "B " >file1 && + git diff >patch.file +' + +test_expect_success 'apply --whitespace=strip' ' + + cp saved file1 && + git update-index --refresh && + + git apply --whitespace=strip patch.file && + if grep " " file1 + then + echo "Eh?" + false + else + echo Happy + fi +' + +test_expect_success 'apply --whitespace=strip from config' ' + + cp saved file1 && + git update-index --refresh && + + git config apply.whitespace strip && + git apply patch.file && + if grep " " file1 + then + echo "Eh?" + false + else + echo Happy + fi +' + +mkdir sub +D=`pwd` + +test_expect_success 'apply --whitespace=strip in subdir' ' + + cd "$D" && + git config --unset-all apply.whitespace + cp saved file1 && + git update-index --refresh && + + cd sub && + git apply --whitespace=strip ../patch.file && + if grep " " ../file1 + then + echo "Eh?" + false + else + echo Happy + fi +' + +test_expect_success 'apply --whitespace=strip from config in subdir' ' + + cd "$D" && + git config apply.whitespace strip && + cp saved file1 && + git update-index --refresh && + + cd sub && + git apply ../patch.file && + if grep " " file1 + then + echo "Eh?" + false + else + echo Happy + fi +' + +test_done From 6716027108f426c83038b05baf3f20ceefe6fbd1 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 17 Feb 2007 12:37:25 -0800 Subject: [PATCH 05/14] Teach core.autocrlf to 'git apply' This teaches git-apply that the data read from and written to the filesystem might need to get converted to adjust for local line-ending convention. Signed-off-by: Junio C Hamano --- builtin-apply.c | 34 ++++++++++++++++++++++++++++------ t/t0020-crlf.sh | 19 +++++++++++++++---- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index 3fefdacd94..45c4acbd20 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -1393,28 +1393,39 @@ static void show_stats(struct patch *patch) free(qname); } -static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size) +static int read_old_data(struct stat *st, const char *path, char **buf_p, unsigned long *alloc_p, unsigned long *size_p) { int fd; unsigned long got; + unsigned long nsize; + char *nbuf; + unsigned long size = *size_p; + char *buf = *buf_p; switch (st->st_mode & S_IFMT) { case S_IFLNK: - return readlink(path, buf, size); + return readlink(path, buf, size) != size; case S_IFREG: fd = open(path, O_RDONLY); if (fd < 0) return error("unable to open %s", path); got = 0; for (;;) { - int ret = xread(fd, (char *) buf + got, size - got); + int ret = xread(fd, buf + got, size - got); if (ret <= 0) break; got += ret; } close(fd); - return got; - + nsize = got; + nbuf = buf; + if (convert_to_git(path, &nbuf, &nsize)) { + free(buf); + *buf_p = nbuf; + *alloc_p = nsize; + *size_p = nsize; + } + return got != size; default: return -1; } @@ -1910,7 +1921,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry * size = st->st_size; alloc = size + 8192; buf = xmalloc(alloc); - if (read_old_data(st, patch->old_name, buf, alloc) != size) + if (read_old_data(st, patch->old_name, &buf, &alloc, &size)) return error("read of %s failed", patch->old_name); } @@ -2282,12 +2293,22 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size) { int fd; + char *nbuf; + unsigned long nsize; if (S_ISLNK(mode)) /* Although buf:size is counted string, it also is NUL * terminated. */ return symlink(buf, path); + nsize = size; + nbuf = (char *) buf; + if (convert_to_working_tree(path, &nbuf, &nsize)) { + free((char *) buf); + buf = nbuf; + size = nsize; + } + fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666); if (fd < 0) return -1; @@ -2598,6 +2619,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) const char *whitespace_option = NULL; + for (i = 1; i < argc; i++) { const char *arg = argv[i]; char *end; diff --git a/t/t0020-crlf.sh b/t/t0020-crlf.sh index 58a4d86df3..723b29ad17 100755 --- a/t/t0020-crlf.sh +++ b/t/t0020-crlf.sh @@ -180,11 +180,8 @@ test_expect_success 'apply patch (autocrlf=true)' ' git repo-config core.autocrlf true && git read-tree --reset -u HEAD && - # Sore thumb - remove_cr one >tmp && mv -f tmp one && - git apply patch.file && - test "$patched" = "`git hash-object --stdin Date: Sat, 17 Feb 2007 18:12:46 -0800 Subject: [PATCH 06/14] Teach 'git apply' to look at $HOME/.gitconfig even outside of a repository Signed-off-by: Junio C Hamano --- builtin-apply.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index fc1d6730d9..2784db2efb 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -2601,11 +2601,9 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) prefix = setup_git_directory_gently(&is_not_gitdir); prefix_length = prefix ? strlen(prefix) : 0; - if (!is_not_gitdir) { - git_config(git_apply_config); - if (apply_default_whitespace) - parse_whitespace_option(apply_default_whitespace); - } + git_config(git_apply_config); + if (apply_default_whitespace) + parse_whitespace_option(apply_default_whitespace); for (i = 1; i < argc; i++) { const char *arg = argv[i]; From aea1945744214bf84908586af8be1c098a6f346d Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 19 Feb 2007 17:58:58 -0800 Subject: [PATCH 07/14] git-apply: do not lose cwd when run from a subdirectory. When a patch modifies (not deletes) the last file in a directory, because we treat a modification just as deletion followed by creation, and deleting the last file in a directory automatically rmdir(2)'s that directory, we ended up removing the directory, which can potentially be the cwd, and then recreating the same directory to create the patch result. Avoid the rmdir step when remove_file() is called only because we are replacing it with the result by later calling create_file(). Signed-off-by: Junio C Hamano --- builtin-apply.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index 2784db2efb..3f829fb661 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -2232,7 +2232,7 @@ static void patch_stats(struct patch *patch) } } -static void remove_file(struct patch *patch) +static void remove_file(struct patch *patch, int rmdir_empty) { if (write_index) { if (remove_file_from_cache(patch->old_name) < 0) @@ -2240,7 +2240,7 @@ static void remove_file(struct patch *patch) cache_tree_invalidate_path(active_cache_tree, patch->old_name); } if (!cached) { - if (!unlink(patch->old_name)) { + if (!unlink(patch->old_name) && rmdir_empty) { char *name = xstrdup(patch->old_name); char *end = strrchr(name, '/'); while (end) { @@ -2373,7 +2373,7 @@ static void write_out_one_result(struct patch *patch, int phase) { if (patch->is_delete > 0) { if (phase == 0) - remove_file(patch); + remove_file(patch, 1); return; } if (patch->is_new > 0 || patch->is_copy) { @@ -2386,7 +2386,7 @@ static void write_out_one_result(struct patch *patch, int phase) * thing: remove the old, write the new */ if (phase == 0) - remove_file(patch); + remove_file(patch, 0); if (phase == 1) create_file(patch); } From 56185f49d03cae28048146e902089ea366c6cd6c Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 19 Feb 2007 17:57:29 -0800 Subject: [PATCH 08/14] git-apply: require -p when working in a subdirectory. git-apply running inside a subdirectory, with or without --index, used to always assume that the patch is formatted in such a way to apply with -p1 from the toplevel, but it is more useful and consistent with the use of "GNU patch -p1" if it defaulted to assume that its input is meant to apply at the level it is invoked in. This changes the behaviour. It used to be that the patch generated this way would apply without any trick: edit Documentation/Makefile git diff >patch.file cd Documentation git apply ../patch.file You need to give an explicit -p2 to git-apply now. On the other hand, if you got a patch from somebody else who did not follow "patch is to apply from the top with -p1" convention, the input patch would start with: diff -u Makefile.old Makefile --- Makefile.old +++ Makefile and in such a case, you can apply it with: git apply -p0 patch.file Signed-off-by: Junio C Hamano --- builtin-apply.c | 42 +++++++++++++++++++++++++++-------------- t/t4119-apply-config.sh | 32 +++++++++++++++++-------------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index 3f829fb661..053511e2ee 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -238,7 +238,7 @@ static int name_terminate(const char *name, int namelen, int c, int terminate) return 1; } -static char * find_name(const char *line, char *def, int p_value, int terminate) +static char *find_name(const char *line, char *def, int p_value, int terminate) { int len; const char *start = line; @@ -362,7 +362,7 @@ 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) { if (!orig_name && !isnull) - return find_name(line, NULL, 1, TERM_TAB); + return find_name(line, NULL, p_value, TERM_TAB); if (orig_name) { int len; @@ -372,7 +372,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, len = strlen(name); if (isnull) die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr); - another = find_name(line, NULL, 1, TERM_TAB); + another = find_name(line, NULL, p_value, TERM_TAB); if (!another || memcmp(another, name, len)) die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr); free(another); @@ -427,28 +427,28 @@ static int gitdiff_newfile(const char *line, struct patch *patch) static int gitdiff_copysrc(const char *line, struct patch *patch) { patch->is_copy = 1; - patch->old_name = find_name(line, NULL, 0, 0); + patch->old_name = find_name(line, NULL, p_value-1, 0); return 0; } static int gitdiff_copydst(const char *line, struct patch *patch) { patch->is_copy = 1; - patch->new_name = find_name(line, NULL, 0, 0); + patch->new_name = find_name(line, NULL, p_value-1, 0); return 0; } static int gitdiff_renamesrc(const char *line, struct patch *patch) { patch->is_rename = 1; - patch->old_name = find_name(line, NULL, 0, 0); + patch->old_name = find_name(line, NULL, p_value-1, 0); return 0; } static int gitdiff_renamedst(const char *line, struct patch *patch) { patch->is_rename = 1; - patch->new_name = find_name(line, NULL, 0, 0); + patch->new_name = find_name(line, NULL, p_value-1, 0); return 0; } @@ -2499,15 +2499,26 @@ static int use_patch(struct patch *p) return 0; x = x->next; } - if (0 < prefix_length) { - int pathlen = strlen(pathname); - if (pathlen <= prefix_length || - memcmp(prefix, pathname, prefix_length)) - return 0; - } return 1; } +static char *prefix_one(char *name) +{ + if (!name) + return name; + return xstrdup(prefix_filename(prefix, prefix_length, name)); +} + +static void prefix_patches(struct patch *p) +{ + if (!prefix) + return; + for ( ; p; p = p->next) { + p->new_name = prefix_one(p->new_name); + p->old_name = prefix_one(p->old_name); + } +} + static int apply_patch(int fd, const char *filename, int inaccurate_eof) { unsigned long offset, size; @@ -2530,11 +2541,14 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof) break; if (apply_in_reverse) reverse_patches(patch); + if (prefix) + prefix_patches(patch); if (use_patch(patch)) { patch_stats(patch); *listp = patch; listp = &patch->next; - } else { + } + else { /* perhaps free it a bit better? */ free(patch); skipped_patch++; diff --git a/t/t4119-apply-config.sh b/t/t4119-apply-config.sh index 0e8ea7e2b8..816b5b8fb2 100755 --- a/t/t4119-apply-config.sh +++ b/t/t4119-apply-config.sh @@ -10,20 +10,22 @@ test_description='git-apply --whitespace=strip and configuration file. . ./test-lib.sh test_expect_success setup ' - echo A >file1 && - cp file1 saved && - git add file1 && - echo "B " >file1 && + mkdir sub && + echo A >sub/file1 && + cp sub/file1 saved && + git add sub/file1 && + echo "B " >sub/file1 && git diff >patch.file ' test_expect_success 'apply --whitespace=strip' ' - cp saved file1 && + rm -f sub/file1 && + cp saved sub/file1 && git update-index --refresh && git apply --whitespace=strip patch.file && - if grep " " file1 + if grep " " sub/file1 then echo "Eh?" false @@ -34,12 +36,13 @@ test_expect_success 'apply --whitespace=strip' ' test_expect_success 'apply --whitespace=strip from config' ' - cp saved file1 && + rm -f sub/file1 && + cp saved sub/file1 && git update-index --refresh && git config apply.whitespace strip && git apply patch.file && - if grep " " file1 + if grep " " sub/file1 then echo "Eh?" false @@ -48,19 +51,19 @@ test_expect_success 'apply --whitespace=strip from config' ' fi ' -mkdir sub D=`pwd` test_expect_success 'apply --whitespace=strip in subdir' ' cd "$D" && git config --unset-all apply.whitespace - cp saved file1 && + rm -f sub/file1 && + cp saved sub/file1 && git update-index --refresh && cd sub && - git apply --whitespace=strip ../patch.file && - if grep " " ../file1 + git apply --whitespace=strip -p2 ../patch.file && + if grep " " file1 then echo "Eh?" false @@ -73,11 +76,12 @@ test_expect_success 'apply --whitespace=strip from config in subdir' ' cd "$D" && git config apply.whitespace strip && - cp saved file1 && + rm -f sub/file1 && + cp saved sub/file1 && git update-index --refresh && cd sub && - git apply ../patch.file && + git apply -p2 ../patch.file && if grep " " file1 then echo "Eh?" From eac70c4f64a618744e05d4a5be61a356c0011033 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 20 Feb 2007 03:45:49 +0100 Subject: [PATCH 09/14] apply: fix memory leak in prefix_one() Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin-apply.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index 053511e2ee..2a40af3ff0 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -2502,11 +2502,13 @@ static int use_patch(struct patch *p) return 1; } -static char *prefix_one(char *name) +static void prefix_one(char **name) { - if (!name) - return name; - return xstrdup(prefix_filename(prefix, prefix_length, name)); + char *old_name = *name; + if (!old_name) + return; + *name = xstrdup(prefix_filename(prefix, prefix_length, *name)); + free(old_name); } static void prefix_patches(struct patch *p) @@ -2514,8 +2516,9 @@ static void prefix_patches(struct patch *p) if (!prefix) return; for ( ; p; p = p->next) { - p->new_name = prefix_one(p->new_name); - p->old_name = prefix_one(p->old_name); + if (p->new_name != p->old_name) + prefix_one(&p->new_name); + prefix_one(&p->old_name); } } From c24e9757e9f608ad3985ee9093d28ca5cd37f052 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 21 Feb 2007 01:14:22 -0800 Subject: [PATCH 10/14] t4119: add test for traditional patch and different p_value Signed-off-by: Junio C Hamano --- t/t4119-apply-config.sh | 53 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/t/t4119-apply-config.sh b/t/t4119-apply-config.sh index 816b5b8fb2..f9b9425153 100755 --- a/t/t4119-apply-config.sh +++ b/t/t4119-apply-config.sh @@ -18,6 +18,15 @@ test_expect_success setup ' git diff >patch.file ' +# Also handcraft GNU diff output; note this has trailing whitespace. +cat >gpatch.file <<\EOF +--- file1 2007-02-21 01:04:24.000000000 -0800 ++++ file1+ 2007-02-21 01:07:44.000000000 -0800 +@@ -1 +1 @@ +-A ++B +EOF + test_expect_success 'apply --whitespace=strip' ' rm -f sub/file1 && @@ -29,8 +38,12 @@ test_expect_success 'apply --whitespace=strip' ' then echo "Eh?" false - else + elif grep B sub/file1 + then echo Happy + else + echo "Huh?" + false fi ' @@ -46,6 +59,9 @@ test_expect_success 'apply --whitespace=strip from config' ' then echo "Eh?" false + elif grep B sub/file1 + then + echo Happy else echo Happy fi @@ -67,8 +83,12 @@ test_expect_success 'apply --whitespace=strip in subdir' ' then echo "Eh?" false - else + elif grep B file1 + then echo Happy + else + echo "Huh?" + false fi ' @@ -86,8 +106,35 @@ test_expect_success 'apply --whitespace=strip from config in subdir' ' then echo "Eh?" false - else + elif grep B file1 + then echo Happy + else + echo "Huh?" + false + fi +' + +test_expect_success 'same in subdir but with traditional patch input' ' + + cd "$D" && + git config apply.whitespace strip && + rm -f sub/file1 && + cp saved sub/file1 && + git update-index --refresh && + + cd sub && + git apply -p0 ../gpatch.file && + if grep " " file1 + then + echo "Eh?" + false + elif grep B file1 + then + echo Happy + else + echo "Huh?" + false fi ' From 6c912f5b04e3216a5487e03235a8454b754a464e Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 21 Feb 2007 00:58:18 -0800 Subject: [PATCH 11/14] Fix botched "leak fix" When (new_name == old_name), the previous one prefixed old_name alone, leaving new_name untouched, and worse yet, left it dangling pointing at an already freed memory location. Signed-off-by: Junio C Hamano --- builtin-apply.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index 2a40af3ff0..1beebe5ff1 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -2516,9 +2516,15 @@ static void prefix_patches(struct patch *p) if (!prefix) return; for ( ; p; p = p->next) { - if (p->new_name != p->old_name) + if (p->new_name == p->old_name) { + char *prefixed = p->new_name; + prefix_one(&prefixed); + p->new_name = p->old_name = prefixed; + } + else { prefix_one(&p->new_name); - prefix_one(&p->old_name); + prefix_one(&p->old_name); + } } } From 9987d7c58a847ab1605ae3216ff1ca95b19f0ad1 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 21 Feb 2007 14:31:10 -0800 Subject: [PATCH 12/14] git-apply: notice "diff --git" patch again Earlier one that tried to be too consistent with GNU patch by not stripping the leading path when we _know_ we are in a subdirectory and the patch is relative to the toplevel was a mistake. This fixes it. - No change to behaviour when it is run from the toplevel of the repository. - When run from a subdirectory to apply a git-generated patch, it uses the right -p value automatically, with or without --index nor --cached option. - When run from a subdirectory to apply a randomly generated patch, it wants the right -p value to be given by the user. The second one is a pure improvement to correct inconsistency between --index and non --index case, compared with 1.5.0. The third point could be further improved to guess what the right value for -p should be by looking at the patch, but should be a topic of a separate patch. Signed-off-by: Junio C Hamano --- builtin-apply.c | 23 ++++++++++++++++------- t/t4119-apply-config.sh | 4 ++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index 1beebe5ff1..12f00e38db 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -144,6 +144,7 @@ struct patch { unsigned long deflate_origlen; int lines_added, lines_deleted; int score; + unsigned int is_toplevel_relative:1; unsigned int inaccurate_eof:1; unsigned int is_binary:1; unsigned int is_copy:1; @@ -362,7 +363,7 @@ 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) { if (!orig_name && !isnull) - return find_name(line, NULL, p_value, TERM_TAB); + return find_name(line, NULL, 1, TERM_TAB); if (orig_name) { int len; @@ -372,7 +373,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, len = strlen(name); if (isnull) die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr); - another = find_name(line, NULL, p_value, TERM_TAB); + another = find_name(line, NULL, 1, TERM_TAB); if (!another || memcmp(another, name, len)) die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr); free(another); @@ -427,28 +428,28 @@ static int gitdiff_newfile(const char *line, struct patch *patch) static int gitdiff_copysrc(const char *line, struct patch *patch) { patch->is_copy = 1; - patch->old_name = find_name(line, NULL, p_value-1, 0); + patch->old_name = find_name(line, NULL, 0, 0); return 0; } static int gitdiff_copydst(const char *line, struct patch *patch) { patch->is_copy = 1; - patch->new_name = find_name(line, NULL, p_value-1, 0); + patch->new_name = find_name(line, NULL, 0, 0); return 0; } static int gitdiff_renamesrc(const char *line, struct patch *patch) { patch->is_rename = 1; - patch->old_name = find_name(line, NULL, p_value-1, 0); + patch->old_name = find_name(line, NULL, 0, 0); return 0; } static int gitdiff_renamedst(const char *line, struct patch *patch) { patch->is_rename = 1; - patch->new_name = find_name(line, NULL, p_value-1, 0); + patch->new_name = find_name(line, NULL, 0, 0); return 0; } @@ -787,6 +788,7 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc { unsigned long offset, len; + patch->is_toplevel_relative = 0; patch->is_rename = patch->is_copy = 0; patch->is_new = patch->is_delete = -1; patch->old_mode = patch->new_mode = 0; @@ -831,6 +833,7 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc die("git diff header lacks filename information (line %d)", linenr); patch->old_name = patch->new_name = patch->def_name; } + patch->is_toplevel_relative = 1; *hdrsize = git_hdr_len; return offset; } @@ -2499,6 +2502,12 @@ static int use_patch(struct patch *p) return 0; x = x->next; } + if (0 < prefix_length) { + int pathlen = strlen(pathname); + if (pathlen <= prefix_length || + memcmp(prefix, pathname, prefix_length)) + return 0; + } return 1; } @@ -2513,7 +2522,7 @@ static void prefix_one(char **name) static void prefix_patches(struct patch *p) { - if (!prefix) + if (!prefix || p->is_toplevel_relative) return; for ( ; p; p = p->next) { if (p->new_name == p->old_name) { diff --git a/t/t4119-apply-config.sh b/t/t4119-apply-config.sh index f9b9425153..32e0d7172e 100755 --- a/t/t4119-apply-config.sh +++ b/t/t4119-apply-config.sh @@ -78,7 +78,7 @@ test_expect_success 'apply --whitespace=strip in subdir' ' git update-index --refresh && cd sub && - git apply --whitespace=strip -p2 ../patch.file && + git apply --whitespace=strip ../patch.file && if grep " " file1 then echo "Eh?" @@ -101,7 +101,7 @@ test_expect_success 'apply --whitespace=strip from config in subdir' ' git update-index --refresh && cd sub && - git apply -p2 ../patch.file && + git apply ../patch.file && if grep " " file1 then echo "Eh?" From 3e8a5db966c26a0a986161103d59683b909a6c78 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 21 Feb 2007 16:05:56 -0800 Subject: [PATCH 13/14] git-apply: guess correct -p value for non-git patches. This enhances the third point in the previous commit. When applying a non-git patch that begins like this: --- 2.6.orig/mm/slab.c +++ 2.6/mm/slab.c @@ -N,M +L,K @@@ ... and if you are in 'mm' subdirectory, we notice that -p2 is the right option to use to apply the patch in file slab.c in the current directory (i.e. mm/slab.c) The guess function also knows about this pattern, where you would need to use -p0 if applying from the top-level: --- mm/slab.c +++ mm/slab.c @@ -N,M +L,K @@@ ... Signed-off-by: Junio C Hamano --- builtin-apply.c | 59 +++++++++++++++++++++++++++++++++++++++-- t/t4119-apply-config.sh | 56 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 4 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index 12f00e38db..c7d4bdd474 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -28,6 +28,7 @@ static int newfd = -1; static int unidiff_zero; static int p_value = 1; +static int p_value_known; static int check_index; static int write_index; static int cached; @@ -312,11 +313,54 @@ static char *find_name(const char *line, char *def, int p_value, int terminate) return name; } +static int count_slashes(const char *cp) +{ + int cnt = 0; + char ch; + + while ((ch = *cp++)) + if (ch == '/') + cnt++; + return cnt; +} + +/* + * Given the string after "--- " or "+++ ", guess the appropriate + * p_value for the given patch. + */ +static int guess_p_value(const char *nameline) +{ + char *name, *cp; + int val = -1; + + if (is_dev_null(nameline)) + return -1; + name = find_name(nameline, NULL, 0, TERM_SPACE | TERM_TAB); + if (!name) + return -1; + cp = strchr(name, '/'); + if (!cp) + val = 0; + else if (prefix) { + /* + * Does it begin with "a/$our-prefix" and such? Then this is + * very likely to apply to our directory. + */ + if (!strncmp(name, prefix, prefix_length)) + val = count_slashes(prefix); + else { + cp++; + if (!strncmp(cp, prefix, prefix_length)) + val = count_slashes(prefix) + 1; + } + } + free(name); + return val; +} + /* * Get the name etc info from the --/+++ lines of a traditional patch header * - * NOTE! This hardcodes "-p1" behaviour in filename detection. - * * FIXME! The end-of-filename heuristics are kind of screwy. For existing * files, we can happily check the index for a match, but for creating a * new file we should try to match whatever "patch" does. I have no idea. @@ -327,6 +371,16 @@ static void parse_traditional_patch(const char *first, const char *second, struc first += 4; /* skip "--- " */ second += 4; /* skip "+++ " */ + if (!p_value_known) { + int p, q; + p = guess_p_value(first); + q = guess_p_value(second); + if (p < 0) p = q; + if (0 <= p && p == q) { + p_value = p; + p_value_known = 1; + } + } if (is_dev_null(first)) { patch->is_new = 1; patch->is_delete = 0; @@ -2656,6 +2710,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) } if (!strncmp(arg, "-p", 2)) { p_value = atoi(arg + 2); + p_value_known = 1; continue; } if (!strcmp(arg, "--no-add")) { diff --git a/t/t4119-apply-config.sh b/t/t4119-apply-config.sh index 32e0d7172e..55f46737c6 100755 --- a/t/t4119-apply-config.sh +++ b/t/t4119-apply-config.sh @@ -19,7 +19,7 @@ test_expect_success setup ' ' # Also handcraft GNU diff output; note this has trailing whitespace. -cat >gpatch.file <<\EOF +cat >gpatch.file <<\EOF && --- file1 2007-02-21 01:04:24.000000000 -0800 +++ file1+ 2007-02-21 01:07:44.000000000 -0800 @@ -1 +1 @@ @@ -27,6 +27,12 @@ cat >gpatch.file <<\EOF +B EOF +sed -e 's|file1|sub/&|' gpatch.file >gpatch-sub.file && +sed -e ' + /^--- /s|file1|a/sub/&| + /^+++ /s|file1|b/sub/&| +' gpatch.file >gpatch-ab-sub.file && + test_expect_success 'apply --whitespace=strip' ' rm -f sub/file1 && @@ -124,7 +130,53 @@ test_expect_success 'same in subdir but with traditional patch input' ' git update-index --refresh && cd sub && - git apply -p0 ../gpatch.file && + git apply ../gpatch.file && + if grep " " file1 + then + echo "Eh?" + false + elif grep B file1 + then + echo Happy + else + echo "Huh?" + false + fi +' + +test_expect_success 'same but with traditional patch input of depth 1' ' + + cd "$D" && + git config apply.whitespace strip && + rm -f sub/file1 && + cp saved sub/file1 && + git update-index --refresh && + + cd sub && + git apply ../gpatch-sub.file && + if grep " " file1 + then + echo "Eh?" + false + elif grep B file1 + then + echo Happy + else + echo "Huh?" + false + fi +' + +test_expect_success 'same but with traditional patch input of depth 2' ' + + cd "$D" && + git config apply.whitespace strip && + rm -f sub/file1 && + cp saved sub/file1 && + git update-index --refresh && + + cd sub && + git apply ../gpatch-ab-sub.file && if grep " " file1 then echo "Eh?" From fe6e0eecb03379e6acb742f77b0b5f589a7b7422 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 21 Feb 2007 16:18:45 -0800 Subject: [PATCH 14/14] t4119: test autocomputing -p for traditional diff input. Signed-off-by: Junio C Hamano --- t/t4119-apply-config.sh | 121 +++++++++++++++------------------------- 1 file changed, 45 insertions(+), 76 deletions(-) diff --git a/t/t4119-apply-config.sh b/t/t4119-apply-config.sh index 55f46737c6..620a9207bf 100755 --- a/t/t4119-apply-config.sh +++ b/t/t4119-apply-config.sh @@ -33,6 +33,20 @@ sed -e ' /^+++ /s|file1|b/sub/&| ' gpatch.file >gpatch-ab-sub.file && +check_result () { + if grep " " "$1" + then + echo "Eh?" + false + elif grep B "$1" + then + echo Happy + else + echo "Huh?" + false + fi +} + test_expect_success 'apply --whitespace=strip' ' rm -f sub/file1 && @@ -40,17 +54,7 @@ test_expect_success 'apply --whitespace=strip' ' git update-index --refresh && git apply --whitespace=strip patch.file && - if grep " " sub/file1 - then - echo "Eh?" - false - elif grep B sub/file1 - then - echo Happy - else - echo "Huh?" - false - fi + check_result sub/file1 ' test_expect_success 'apply --whitespace=strip from config' ' @@ -61,16 +65,7 @@ test_expect_success 'apply --whitespace=strip from config' ' git config apply.whitespace strip && git apply patch.file && - if grep " " sub/file1 - then - echo "Eh?" - false - elif grep B sub/file1 - then - echo Happy - else - echo Happy - fi + check_result sub/file1 ' D=`pwd` @@ -85,17 +80,7 @@ test_expect_success 'apply --whitespace=strip in subdir' ' cd sub && git apply --whitespace=strip ../patch.file && - if grep " " file1 - then - echo "Eh?" - false - elif grep B file1 - then - echo Happy - else - echo "Huh?" - false - fi + check_result file1 ' test_expect_success 'apply --whitespace=strip from config in subdir' ' @@ -108,17 +93,7 @@ test_expect_success 'apply --whitespace=strip from config in subdir' ' cd sub && git apply ../patch.file && - if grep " " file1 - then - echo "Eh?" - false - elif grep B file1 - then - echo Happy - else - echo "Huh?" - false - fi + check_result file1 ' test_expect_success 'same in subdir but with traditional patch input' ' @@ -131,17 +106,7 @@ test_expect_success 'same in subdir but with traditional patch input' ' cd sub && git apply ../gpatch.file && - if grep " " file1 - then - echo "Eh?" - false - elif grep B file1 - then - echo Happy - else - echo "Huh?" - false - fi + check_result file1 ' test_expect_success 'same but with traditional patch input of depth 1' ' @@ -154,17 +119,7 @@ test_expect_success 'same but with traditional patch input of depth 1' ' cd sub && git apply ../gpatch-sub.file && - if grep " " file1 - then - echo "Eh?" - false - elif grep B file1 - then - echo Happy - else - echo "Huh?" - false - fi + check_result file1 ' test_expect_success 'same but with traditional patch input of depth 2' ' @@ -177,17 +132,31 @@ test_expect_success 'same but with traditional patch input of depth 2' ' cd sub && git apply ../gpatch-ab-sub.file && - if grep " " file1 - then - echo "Eh?" - false - elif grep B file1 - then - echo Happy - else - echo "Huh?" - false - fi + check_result file1 +' + +test_expect_success 'same but with traditional patch input of depth 1' ' + + cd "$D" && + git config apply.whitespace strip && + rm -f sub/file1 && + cp saved sub/file1 && + git update-index --refresh && + + git apply -p0 gpatch-sub.file && + check_result sub/file1 +' + +test_expect_success 'same but with traditional patch input of depth 2' ' + + cd "$D" && + git config apply.whitespace strip && + rm -f sub/file1 && + cp saved sub/file1 && + git update-index --refresh && + + git apply gpatch-ab-sub.file && + check_result sub/file1 ' test_done