Commit graph

77 commits

Author SHA1 Message Date
Shawn O. Pearce dc49cd769b Cast 64 bit off_t to 32 bit size_t
Some systems have sizeof(off_t) == 8 while sizeof(size_t) == 4.
This implies that we are able to access and work on files whose
maximum length is around 2^63-1 bytes, but we can only malloc or
mmap somewhat less than 2^32-1 bytes of memory.

On such a system an implicit conversion of off_t to size_t can cause
the size_t to wrap, resulting in unexpected and exciting behavior.
Right now we are working around all gcc warnings generated by the
-Wshorten-64-to-32 option by passing the off_t through xsize_t().

In the future we should make xsize_t on such problematic platforms
detect the wrapping and die if such a file is accessed.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-03-07 11:15:26 -08:00
Johannes Sixt 78a8d641c1 Add core.symlinks to mark filesystems that do not support symbolic links.
Some file systems that can host git repositories and their working copies
do not support symbolic links. But then if the repository contains a symbolic
link, it is impossible to check out the working copy.

This patch enables partial support of symbolic links so that it is possible
to check out a working copy on such a file system.  A new flag
core.symlinks (which is true by default) can be set to false to indicate
that the filesystem does not support symbolic links. In this case, symbolic
links that exist in the trees are checked out as small plain files, and
checking in modifications of these files preserve the symlink property in
the database (as long as an entry exists in the index).

Of course, this does not magically make symbolic links work on such defective
file systems; hence, this solution does not help if the working copy relies
on that an entry is a real symbolic link.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-03-02 16:58:05 -08:00
Nicolas Pitre 21666f1aae convert object type handling from a string to a number
We currently have two parallel notation for dealing with object types
in the code: a string and a numerical value.  One of them is obviously
redundent, and the most used one requires more stack space and a bunch
of strcmp() all over the place.

This is an initial step for the removal of the version using a char array
found in object reading code paths.  The patch is unfortunately large but
there is no sane way to split it in smaller parts without breaking the
system.

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-27 01:34:21 -08:00
Junio C Hamano 4e4b55dd0f Merge branch 'maint'
* maint:
  git-apply: do not fix whitespaces on context lines.
  diff --cc: integer overflow given a 2GB-or-larger file
  mailinfo: do not get confused with logical lines that are too long.
2007-02-27 01:33:52 -08:00
Junio C Hamano 63e50d492c git-apply: do not fix whitespaces on context lines.
Internal function apply_line() is called to copy both context lines
and added lines to the output buffer, while possibly fixing the
whitespace breakages depending on --whitespace=strip settings.
However, it did its fix-up on both context lines and added lines.

This resulted in two symptoms:

 (1) The number of lines reported to have been fixed up included
     these context lines.

 (2) However, the lines actually shown were limited to the added
     lines that had whitespace breakages.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-27 01:33:14 -08:00
Junio C Hamano 7bd59dee5b Merge branch 'js/apply'
* js/apply:
  apply: make --verbose a little more useful
2007-02-24 02:00:32 -08:00
Junio C Hamano ef1a5c2fa8 Merge branches 'lt/crlf' and 'jc/apply-config'
* lt/crlf:
  Teach core.autocrlf to 'git apply'
  t0020: add test for auto-crlf
  Make AutoCRLF ternary variable.
  Lazy man's auto-CRLF

* jc/apply-config:
  t4119: test autocomputing -p<n> for traditional diff input.
  git-apply: guess correct -p<n> value for non-git patches.
  git-apply: notice "diff --git" patch again
  Fix botched "leak fix"
  t4119: add test for traditional patch and different p_value
  apply: fix memory leak in prefix_one()
  git-apply: require -p<n> when working in a subdirectory.
  git-apply: do not lose cwd when run from a subdirectory.
  Teach 'git apply' to look at $HOME/.gitconfig even outside of a repository
  Teach 'git apply' to look at $GIT_DIR/config
2007-02-22 21:34:36 -08:00
Johannes Schindelin aeabfa0725 apply: make --verbose a little more useful
When a patch fails, I automatically add '-v' to the command line
to see what fails.

This patch makes -v a synonym to --verbose, and actually tells
the user which text was not found.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-22 20:58:36 -08:00
Junio C Hamano 3e8a5db966 git-apply: guess correct -p<n> 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 <junkio@cox.net>
2007-02-21 16:05:56 -08:00
Junio C Hamano 9987d7c58a 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<n> 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<n> 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<n> should be by looking at the patch, but should be
a topic of a separate patch.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-21 14:42:15 -08:00
Junio C Hamano 6c912f5b04 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 <junkio@cox.net>
2007-02-21 01:14:34 -08:00
Junio C Hamano cc44c7655f Mechanical conversion to use prefixcmp()
This mechanically converts strncmp() to use prefixcmp(), but only when
the parameters match specific patterns, so that they can be verified
easily.  Leftover from this will be fixed in a separate step, including
idiotic conversions like

    if (!strncmp("foo", arg, 3))

  =>

    if (!(-prefixcmp(arg, "foo")))

This was done by using this script in px.perl

   #!/usr/bin/perl -i.bak -p
   if (/strncmp\(([^,]+), "([^\\"]*)", (\d+)\)/ && (length($2) == $3)) {
           s|strncmp\(([^,]+), "([^\\"]*)", (\d+)\)|prefixcmp($1, "$2")|;
   }
   if (/strncmp\("([^\\"]*)", ([^,]+), (\d+)\)/ && (length($1) == $3)) {
           s|strncmp\("([^\\"]*)", ([^,]+), (\d+)\)|(-prefixcmp($2, "$1"))|;
   }

and running:

   $ git grep -l strncmp -- '*.c' | xargs perl px.perl

Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-20 22:03:15 -08:00
Johannes Schindelin eac70c4f64 apply: fix memory leak in prefix_one()
Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-19 19:02:12 -08:00
Junio C Hamano 56185f49d0 git-apply: require -p<n> 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 <junkio@cox.net>
2007-02-19 18:44:59 -08:00
Junio C Hamano aea1945744 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 <junkio@cox.net>
2007-02-19 18:44:21 -08:00
Junio C Hamano 700ea47936 Teach 'git apply' to look at $HOME/.gitconfig even outside of a repository
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-18 00:54:04 -08:00
Junio C Hamano 6716027108 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 <junkio@cox.net>
2007-02-17 15:27:47 -08:00
Junio C Hamano dc7b24364d 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 <junkio@cox.net>
2007-02-17 13:13:32 -08:00
Junio C Hamano 185c975faa Do not take mode bits from index after type change.
When we do not trust executable bit from lstat(2), we copied
existing ce_mode bits without checking if the filesystem object
is a regular file (which is the only thing we apply the "trust
executable bit" business) nor if the blob in the index is a
regular file (otherwise, we should do the same as registering a
new regular file, which is to default non-executable).

Noticed by Johannes Sixt.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-16 22:56:06 -08:00
Johannes Schindelin cd554bb173 apply --cached: fix crash in subdirectory
The static variable "prefix" was shadowed by an unused parameter
of the same name. In case of execution in a subdirectory, the
static variable was accessed, leading to a crash.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
2007-01-20 19:05:20 -08:00
Alexandre Julliard d234b21c69 git-apply: Remove directories that have become empty after deleting a file.
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-01-09 16:05:00 -08:00
Junio C Hamano 240897e908 Merge branch 'maint'
* maint:
  Fix "Do not ignore a detected patchfile brokenness."
  Do not ignore a detected patchfile brokenness.
2007-01-09 12:04:30 -08:00
Junio C Hamano 6534141151 Fix "Do not ignore a detected patchfile brokenness."
Returning negative value from there does not stop the caller from using
the earlier part.

Noticed by Linus.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-01-09 11:50:53 -08:00
Junio C Hamano 5a17b54ad5 Do not ignore a detected patchfile brokenness.
find_header() function is used to read and parse the patchfile
and it detects errors in the patch, but one place ignored the
error and went ahead, which was quite bad.

Noticed by Jeff Garzik.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-01-09 02:56:43 -08:00
Junio C Hamano 85023577a8 simplify inclusion of system header files.
This is a mechanical clean-up of the way *.c files include
system header files.

 (1) sources under compat/, platform sha-1 implementations, and
     xdelta code are exempt from the following rules;

 (2) the first #include must be "git-compat-util.h" or one of
     our own header file that includes it first (e.g. config.h,
     builtin.h, pkt-line.h);

 (3) system headers that are included in "git-compat-util.h"
     need not be included in individual C source files.

 (4) "git-compat-util.h" does not have to include subsystem
     specific header files (e.g. expat.h).

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-12-20 09:51:35 -08:00
Junio C Hamano 54acddce99 Merge branch 'jc/numstat'
* jc/numstat:
  apply --numstat: mark binary diffstat with - -, not 0 0
2006-11-24 03:46:40 -08:00
Rene Scharfe 3dad11bfdb git-apply: slightly clean up bitfield usage
This patch fixes a sparse warning about inaccurate_eof being a
"dubious one-bit signed bitfield", makes three more binary
variables members of this (now unsigned) bitfield and adds a
short comment to indicate the nature of two ternary variables.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-11-18 11:40:11 -08:00
Junio C Hamano ef58d9587e apply --numstat: mark binary diffstat with - -, not 0 0
We do not even know number of lines so showing it as 0 0 is
lying.  This would also help Porcelains like cvsexportcommit.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-11-14 22:23:18 -08:00
Junio C Hamano 82cc8d839b Merge branch 'maint'
* maint:
  Remove unsupported C99 style struct initializers in git-archive.
  Remove SIMPLE_PROGRAMS and make git-daemon a normal program.
  Use ULONG_MAX rather than implicit cast of -1.
2006-11-04 23:52:32 -08:00
Shawn O. Pearce bf8675d314 Use ULONG_MAX rather than implicit cast of -1.
At least one (older) version of the Solaris C compiler won't allow
'unsigned long x = -1' without explicitly casting -1 to a type of
unsigned long.  So instead use ULONG_MAX, which is really the
correct constant anyway.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-11-04 23:41:02 -08:00
Junio C Hamano a622f6b35e Merge branch 'maint'
* maint:
  apply: handle "traditional" creation/deletion diff correctly.
2006-11-04 03:54:20 -08:00
Junio C Hamano 6f9f3b263b apply: handle "traditional" creation/deletion diff correctly.
We deduced a GNU diff output that does not use /dev/null convention
as creation (deletion) diff correctly by looking at the lack of context
and deleted lines (added lines), but forgot to reset the new (old) name
field properly.

This was a regression when we added a workaround for --unified=0 insanity.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-11-04 02:35:17 -08:00
Junio C Hamano e19343ad54 Merge branch 'maint'
* maint:
  git-apply: prepare for upcoming GNU diff -u format change.
2006-10-19 21:28:12 -07:00
Linus Torvalds b507b465f7 git-apply: prepare for upcoming GNU diff -u format change.
The latest GNU diff from CVS emits an empty line to express
an empty context line, instead of more traditional "single
white space followed by a newline".  Do not get broken by it.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-10-19 21:28:06 -07:00
Junio C Hamano 17250ac172 Merge early part of branch 'jc/diff-apply-patch' 2006-10-18 22:08:46 -07:00
Rene Scharfe abdc3fc842 Add hash_sha1_file()
Most callers of write_sha1_file_prepare() are only interested in the
resulting hash but don't care about the returned file name or the header.
This patch adds a simple wrapper named hash_sha1_file() which does just
that, and converts potential callers.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-10-14 11:49:52 -07:00
Junio C Hamano 854de5a534 apply --numstat -z: line termination fix.
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-10-12 02:57:39 -07:00
Junio C Hamano ce74618d95 git-diff/git-apply: make diff output a bit friendlier to GNU patch (part 1)
Somebody was wondering on #git channel why a git generated diff
does not apply with GNU patch when the filename contains a SP.
It is because GNU patch expects to find TAB (and trailing timestamp)
on ---/+++ (old_name and new_name) lines after the filenames.

The "diff --git" output format was carefully designed to be
compatible with GNU patch where it can, but whitespace
characters were always a pain.

We can make our output a bit more GNU patch friendly by adding an
extra TAB (but not trailing timestamp) to old/new name lines when
the filename as a SP in it.  This updates git-apply to prepare
ourselves to accept such a patch, but we still do not generate
output that is patch friendly yet.  That change needs to wait
until everybody has this change.

When a filename contains a real tab, "diff --git" format
always c-quotes it as discussed on the list with GNU patch
maintainer previously:

	http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2

so there should be no downside.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-29 19:20:27 -07:00
Junio C Hamano d0c25035df git-apply: second war on whitespace.
This makes --whitespace={warn,error,strip} option to also notice
the leading whitespace errors in addition to the trailing
whitespace errors.  Spaces that are followed by a tab in indent
are detected as errors, and --whitespace=strip option fixes them.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-24 00:12:44 -07:00
Junio C Hamano 4be609625e apply --unidiff-zero: loosen sanity checks for --unidiff=0 patches
In "git-apply", we have a few sanity checks and heuristics that
expects that the patch fed to us is a unified diff with at least
one line of context.

 * When there is no leading context line in a hunk, the hunk
   must apply at the beginning of the preimage.  Similarly, no
   trailing context means that the hunk is anchored at the end.

 * We learn a patch deletes the file from a hunk that has no
   resulting line (i.e. all lines are prefixed with '-') if it
   has not otherwise been known if the patch deletes the file.
   Similarly, no old line means the file is being created.

And we declare an error condition when the file created by a
creation patch already exists, and/or when a deletion patch
still leaves content in the file.

These sanity checks are good safety measures, but breaks down
when people feed a diff generated with --unified=0.  This was
recently noticed first by Matthew Wilcox and Gerrit Pape.

This adds a new flag, --unified-zero, to allow bypassing these
checks.  If you are in control of the patch generation process,
you should not use --unified=0 patch and fix it up with this
flag; rather you should try work with a patch with context.  But
if all you have to work with is a patch without context, this
flag may come handy as the last resort.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-17 01:12:37 -07:00
Junio C Hamano 2b6eef943f Make apply --binary a no-op.
Historically we did not allow binary patch applied without an
explicit permission from the user, and this flag was the way to
do so.  This makes the flag a no-op by always allowing binary
patch application.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-07 02:44:40 -07:00
Shawn Pearce 9befac470b Replace uses of strdup with xstrdup.
Like xmalloc and xrealloc xstrdup dies with a useful message if
the native strdup() implementation returns NULL rather than a
valid pointer.

I just tried to use xstrdup in new code and found it to be missing.
However I expected it to be present as xmalloc and xrealloc are
already commonly used throughout the code.

[jc: removed the part that deals with last_XXX, which I am
 finding more and more dubious these days.]

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-09-02 03:24:37 -07:00
Junio C Hamano 4cac42b132 free(NULL) is perfectly valid.
Jonas noticed some places say "if (X) free(X)" which is totally
unnecessary.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-08-27 21:19:39 -07:00
Junio C Hamano b32d37a3a6 Merge branch 'jc/apply'
* jc/apply:
  git-apply --reject: finishing touches.
  apply --reject: count hunks starting from 1, not 0
  git-apply --verbose
  git-apply --reject: send rejects to .rej files.
  git-apply --reject
  apply --reverse: tie it all together.
  diff.c: make binary patch reversible.
  builtin-apply --reverse: two bugfixes.
2006-08-27 17:51:05 -07:00
Junio C Hamano 8938045a4e git-apply --reject: finishing touches.
After a failed "git am" attempt:

	git apply --reject --verbose .dotest/patch

applies hunks that are applicable and leaves *.rej files the
rejected hunks, and it reports what it is doing.  With --index,
files with a rejected hunk do not get their index entries
updated at all, so "git diff" will show the hunks that
successfully got applied.

Without --verbose to remind the user that the patch updated some
other paths cleanly, it is very easy to lose track of the status
of the working tree, so --reject implies --verbose.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-08-27 15:53:20 -07:00
Pierre Habouzit dd305c8462 use name[len] in switch directly, instead of creating a shadowed variable.
builtin-apply.c defines a local variable 'c' which is used only
once and then later gets shadowed by another instance of 'c'.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-08-23 18:47:39 -07:00
Pierre Habouzit 60b7f38e0e avoid to use error that shadows the function name, use err instead.
builtin-apply.c and builtin-push.c uses a local variable called 'error'
which shadows the error() function.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-08-23 18:47:38 -07:00
Junio C Hamano 0e9ee32358 apply --reject: count hunks starting from 1, not 0
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-08-22 15:49:28 -07:00
Junio C Hamano a2bf404e28 git-apply --verbose
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-08-18 03:14:48 -07:00
Junio C Hamano 82e2765f59 git-apply --reject: send rejects to .rej files.
... just like everybody else does, instead of sending it to the standard
output, which was just silly.

Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-08-18 03:10:19 -07:00