Merge branch 'jc/rw-prefix'
authorJunio C Hamano <junkio@cox.net>
Sun, 18 Jun 2006 00:56:52 +0000 (17:56 -0700)
committerJunio C Hamano <junkio@cox.net>
Sun, 18 Jun 2006 00:56:52 +0000 (17:56 -0700)
* jc/rw-prefix:
  read-tree: reorganize bind_merge code.
  write-tree: --prefix=<path>
  read-tree: --prefix=<path>/ option.

286 files changed:
.gitignore
Documentation/Makefile
Documentation/SubmittingPatches
Documentation/callouts.xsl [new file with mode: 0644]
Documentation/config.txt
Documentation/core-tutorial.txt
Documentation/cvs-migration.txt
Documentation/diff-options.txt
Documentation/everyday.txt
Documentation/git-add.txt
Documentation/git-apply.txt
Documentation/git-blame.txt
Documentation/git-branch.txt
Documentation/git-cat-file.txt
Documentation/git-check-ref-format.txt
Documentation/git-checkout-index.txt
Documentation/git-checkout.txt
Documentation/git-cherry.txt
Documentation/git-clean.txt
Documentation/git-clone.txt
Documentation/git-commit.txt
Documentation/git-count-objects.txt
Documentation/git-cvsexportcommit.txt
Documentation/git-cvsserver.txt
Documentation/git-daemon.txt
Documentation/git-describe.txt
Documentation/git-diff-index.txt
Documentation/git-diff-tree.txt
Documentation/git-diff.txt
Documentation/git-format-patch.txt
Documentation/git-fsck-objects.txt
Documentation/git-grep.txt
Documentation/git-imap-send.txt
Documentation/git-init-db.txt
Documentation/git-log.txt
Documentation/git-ls-files.txt
Documentation/git-ls-tree.txt
Documentation/git-merge-base.txt
Documentation/git-merge-index.txt
Documentation/git-name-rev.txt
Documentation/git-p4import.txt [new file with mode: 0644]
Documentation/git-patch-id.txt
Documentation/git-prune.txt
Documentation/git-quiltimport.txt [new file with mode: 0644]
Documentation/git-read-tree.txt
Documentation/git-rebase.txt
Documentation/git-repack.txt
Documentation/git-repo-config.txt
Documentation/git-reset.txt
Documentation/git-rev-list.txt
Documentation/git-rev-parse.txt
Documentation/git-rm.txt
Documentation/git-send-email.txt
Documentation/git-send-pack.txt
Documentation/git-sh-setup.txt
Documentation/git-shortlog.txt
Documentation/git-tar-tree.txt
Documentation/git-tools.txt
Documentation/git-unpack-objects.txt
Documentation/git-update-index.txt
Documentation/git-update-ref.txt
Documentation/git-upload-tar.txt [new file with mode: 0644]
Documentation/git-var.txt
Documentation/git-verify-pack.txt
Documentation/git-whatchanged.txt
Documentation/git.txt
Documentation/gitk.txt
Documentation/glossary.txt
Documentation/hooks.txt
Documentation/install-webdoc.sh
Documentation/repository-layout.txt
Documentation/sort_glossary.pl
Documentation/technical/pack-format.txt
Documentation/technical/pack-heuristics.txt
Documentation/tutorial-2.txt [new file with mode: 0644]
Documentation/tutorial.txt
GIT-VERSION-GEN
Makefile
apply.c [deleted file]
base85.c [new file with mode: 0644]
blame.c
builtin-add.c [new file with mode: 0644]
builtin-apply.c [new file with mode: 0644]
builtin-cat-file.c [new file with mode: 0644]
builtin-check-ref-format.c [new file with mode: 0644]
builtin-commit-tree.c [new file with mode: 0644]
builtin-count.c [new file with mode: 0644]
builtin-diff-files.c [new file with mode: 0644]
builtin-diff-index.c [new file with mode: 0644]
builtin-diff-stages.c [new file with mode: 0644]
builtin-diff-tree.c [new file with mode: 0644]
builtin-diff.c [new file with mode: 0644]
builtin-grep.c [new file with mode: 0644]
builtin-help.c
builtin-init-db.c [new file with mode: 0644]
builtin-log.c
builtin-ls-files.c [new file with mode: 0644]
builtin-ls-tree.c [new file with mode: 0644]
builtin-push.c [new file with mode: 0644]
builtin-read-tree.c [new file with mode: 0644]
builtin-rev-list.c [new file with mode: 0644]
builtin-rev-parse.c [new file with mode: 0644]
builtin-rm.c [new file with mode: 0644]
builtin-show-branch.c [new file with mode: 0644]
builtin-tar-tree.c [new file with mode: 0644]
builtin-upload-tar.c [new file with mode: 0644]
builtin.h
cache-tree.c
cache.h
cat-file.c [deleted file]
check-ref-format.c [deleted file]
checkout-index.c
combine-diff.c
commit-tree.c [deleted file]
commit.c
commit.h
compat/inet_ntop.c [new file with mode: 0644]
config.c
connect.c
contrib/git-svn/Makefile
contrib/git-svn/git-svn.perl
contrib/git-svn/git-svn.txt
contrib/git-svn/t/lib-git-svn.sh [new file with mode: 0644]
contrib/git-svn/t/t0000-contrib-git-svn.sh
contrib/git-svn/t/t0001-contrib-git-svn-props.sh [new file with mode: 0644]
contrib/gitview/gitview
contrib/gitview/gitview.txt
contrib/remotes2config.sh [new file with mode: 0644]
convert-objects.c
daemon.c
date.c
delta.h
describe.c
diff-delta.c
diff-files.c [deleted file]
diff-index.c [deleted file]
diff-stages.c [deleted file]
diff-tree.c [deleted file]
diff.c
diff.h
dir.c [new file with mode: 0644]
dir.h [new file with mode: 0644]
dump-cache-tree.c
environment.c
exec_cmd.c
fetch-pack.c
fetch.c
fetch.h
fsck-objects.c
generate-cmdlist.sh
get-tar-commit-id.c [deleted file]
git-add.sh [deleted file]
git-am.sh
git-annotate.perl
git-applypatch.sh
git-branch.sh
git-checkout.sh
git-clean.sh
git-clone.sh
git-commit.sh
git-count-objects.sh [deleted file]
git-cvsexportcommit.perl
git-cvsimport.perl [changed mode: 0755->0644]
git-cvsserver.perl
git-diff.sh [deleted file]
git-fetch.sh
git-format-patch.sh [deleted file]
git-grep.sh [deleted file]
git-ls-remote.sh
git-merge.sh
git-p4import.py [new file with mode: 0644]
git-parse-remote.sh
git-quiltimport.sh [new file with mode: 0755]
git-rebase.sh
git-repack.sh
git-request-pull.sh
git-reset.sh
git-revert.sh
git-rm.sh [deleted file]
git-send-email.perl
git-svnimport.perl
git-tag.sh
git-whatchanged.sh [deleted file]
git.c
git.spec.in
gitk
gitweb/README [new file with mode: 0644]
gitweb/gitweb.cgi [new file with mode: 0755]
gitweb/test/Märchen [new file with mode: 0644]
gitweb/test/file with spaces [new file with mode: 0644]
gitweb/test/file+plus+sign [new file with mode: 0644]
http-fetch.c
http-push.c
http.c
ident.c
imap-send.c
index.c [deleted file]
init-db.c [deleted file]
local-fetch.c
lockfile.c [new file with mode: 0644]
log-tree.c
ls-files.c [deleted file]
ls-tree.c [deleted file]
mailinfo.c
mailsplit.c
merge-base.c
merge-tree.c
mktag.c
object.c
pack-check.c
pack-objects.c
patch-delta.c
path.c
read-cache.c
read-tree.c [deleted file]
refs.c
refs.h
repo-config.c
rev-list.c [deleted file]
rev-parse.c [deleted file]
revision.c
revision.h
rsh.c
setup.c
sha1_file.c
sha1_name.c
show-branch.c [deleted file]
ssh-fetch.c
ssh-upload.c
t/t1002-read-tree-m-u-2way.sh
t/t1300-repo-config.sh
t/t1400-update-ref.sh [new file with mode: 0755]
t/t2101-update-index-reupdate.sh [new file with mode: 0755]
t/t3200-branch.sh
t/t3300-funny-names.sh
t/t3500-cherry.sh
t/t3600-rm.sh
t/t4002-diff-basic.sh
t/t4012-diff-binary.sh [new file with mode: 0755]
t/t4101-apply-nonl.sh
t/t4101/diff.0-1 [new file with mode: 0644]
t/t4101/diff.0-2 [new file with mode: 0644]
t/t4101/diff.0-3 [new file with mode: 0644]
t/t4101/diff.1-0 [new file with mode: 0644]
t/t4101/diff.1-2 [new file with mode: 0644]
t/t4101/diff.1-3 [new file with mode: 0644]
t/t4101/diff.2-0 [new file with mode: 0644]
t/t4101/diff.2-1 [new file with mode: 0644]
t/t4101/diff.2-3 [new file with mode: 0644]
t/t4101/diff.3-0 [new file with mode: 0644]
t/t4101/diff.3-1 [new file with mode: 0644]
t/t4101/diff.3-2 [new file with mode: 0644]
t/t4113-apply-ending.sh [new file with mode: 0755]
t/t5100-mailinfo.sh [new file with mode: 0755]
t/t5100/info0001 [new file with mode: 0644]
t/t5100/info0002 [new file with mode: 0644]
t/t5100/info0003 [new file with mode: 0644]
t/t5100/info0004 [new file with mode: 0644]
t/t5100/info0005 [new file with mode: 0644]
t/t5100/msg0001 [new file with mode: 0644]
t/t5100/msg0002 [new file with mode: 0644]
t/t5100/msg0003 [new file with mode: 0644]
t/t5100/msg0004 [new file with mode: 0644]
t/t5100/msg0005 [new file with mode: 0644]
t/t5100/patch0001 [new file with mode: 0644]
t/t5100/patch0002 [new file with mode: 0644]
t/t5100/patch0003 [new file with mode: 0644]
t/t5100/patch0004 [new file with mode: 0644]
t/t5100/patch0005 [new file with mode: 0644]
t/t5100/sample.mbox [new file with mode: 0644]
t/t5500-fetch-pack.sh
t/t5700-clone-reference.sh [new file with mode: 0755]
t/t5710-info-alternate.sh [new file with mode: 0755]
t/t6000lib.sh
t/t6022-merge-rename.sh
t/t9001-send-email.sh [new file with mode: 0755]
t/test4012.png [new file with mode: 0644]
tar-tree.c [deleted file]
tree-walk.c
tree-walk.h
tree.c
tree.h
unpack-file.c
update-index.c
update-ref.c
write-tree.c

index 7906909..afd0876 100644 (file)
@@ -77,6 +77,7 @@ git-prune
 git-prune-packed
 git-pull
 git-push
+git-quiltimport
 git-read-tree
 git-rebase
 git-receive-pack
@@ -115,6 +116,7 @@ git-update-index
 git-update-ref
 git-update-server-info
 git-upload-pack
+git-upload-tar
 git-var
 git-verify-pack
 git-verify-tag
index f4cbf7e..2b0efe7 100644 (file)
@@ -7,6 +7,7 @@ MAN7_TXT=git.txt
 DOC_HTML=$(patsubst %.txt,%.html,$(MAN1_TXT) $(MAN7_TXT))
 
 ARTICLES = tutorial
+ARTICLES += tutorial-2
 ARTICLES += core-tutorial
 ARTICLES += cvs-migration
 ARTICLES += diffcore
@@ -51,9 +52,9 @@ man1: $(DOC_MAN1)
 man7: $(DOC_MAN7)
 
 install: man
-       $(INSTALL) -d -m755 $(DESTDIR)/$(man1) $(DESTDIR)/$(man7)
-       $(INSTALL) $(DOC_MAN1) $(DESTDIR)/$(man1)
-       $(INSTALL) $(DOC_MAN7) $(DESTDIR)/$(man7)
+       $(INSTALL) -d -m755 $(DESTDIR)$(man1) $(DESTDIR)$(man7)
+       $(INSTALL) $(DOC_MAN1) $(DESTDIR)$(man1)
+       $(INSTALL) $(DOC_MAN7) $(DESTDIR)$(man7)
 
 
 #
@@ -79,7 +80,7 @@ clean:
        asciidoc -b xhtml11 -d manpage -f asciidoc.conf $<
 
 %.1 %.7 : %.xml
-       xmlto man $<
+       xmlto -m callouts.xsl man $<
 
 %.xml : %.txt
        asciidoc -b docbook -d manpage -f asciidoc.conf $<
index 318b04f..8601949 100644 (file)
@@ -266,8 +266,8 @@ This recipe appears to work with the current [*1*] Thunderbird from Suse.
 The following Thunderbird extensions are needed:
        AboutConfig 0.5
                http://aboutconfig.mozdev.org/
-       External Editor 0.5.4
-               http://extensionroom.mozdev.org/more-info/exteditor
+       External Editor 0.7.2
+               http://globs.org/articles.php?lng=en&pg=8
 
 1) Prepare the patch as a text file using your method of choice.
 
diff --git a/Documentation/callouts.xsl b/Documentation/callouts.xsl
new file mode 100644 (file)
index 0000000..ad03755
--- /dev/null
@@ -0,0 +1,16 @@
+<!-- callout.xsl: converts asciidoc callouts to man page format -->
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
+<xsl:template match="co">
+       <xsl:value-of select="concat('\fB(',substring-after(@id,'-'),')\fR')"/>
+</xsl:template>
+<xsl:template match="calloutlist">
+       <xsl:text>.sp&#10;</xsl:text>
+       <xsl:apply-templates/>
+       <xsl:text>&#10;</xsl:text>
+</xsl:template>
+<xsl:template match="callout">
+       <xsl:value-of select="concat('\fB',substring-after(@arearefs,'-'),'. \fR')"/>
+       <xsl:apply-templates/>
+       <xsl:text>.br&#10;</xsl:text>
+</xsl:template>
+</xsl:stylesheet>
index b27b0d5..a04c5ad 100644 (file)
@@ -2,15 +2,15 @@ CONFIGURATION FILE
 ------------------
 
 The git configuration file contains a number of variables that affect
-the git commands behaviour. They can be used by both the git plumbing
-and the porcelains. The variables are divided to sections, where
+the git command's behavior. They can be used by both the git plumbing
+and the porcelains. The variables are divided into sections, where
 in the fully qualified variable name the variable itself is the last
 dot-separated segment and the section name is everything before the last
 dot. The variable names are case-insensitive and only alphanumeric
 characters are allowed. Some variables may appear multiple times.
 
 The syntax is fairly flexible and permissive; whitespaces are mostly
-ignored. The '#' and ';' characters begin commends to the end of line,
+ignored. The '#' and ';' characters begin comments to the end of line,
 blank lines are ignored, lines containing strings enclosed in square
 brackets start sections and all the other lines are recognized
 as setting variables, in the form 'name = value'. If there is no equal
@@ -35,8 +35,8 @@ Variables
 ~~~~~~~~~
 
 Note that this list is non-comprehensive and not necessarily complete.
-For command-specific variables, you will find more detailed description
-in the appropriate manual page. You will find description of non-core
+For command-specific variables, you will find more detailed description
+in the appropriate manual page. You will find description of non-core
 porcelain configuration variables in the respective porcelain documentation.
 
 core.fileMode::
@@ -52,10 +52,10 @@ core.gitProxy::
        on hostnames ending with the specified domain string. This variable
        may be set multiple times and is matched in the given order;
        the first match wins.
-
-       Can be overriden by the 'GIT_PROXY_COMMAND' environment variable
-       (which always applies universally, without the special "for"
-       handling).
++
+Can be overridden by the 'GIT_PROXY_COMMAND' environment variable
+(which always applies universally, without the special "for"
+handling).
 
 core.ignoreStat::
        The working copy files are assumed to stay unchanged until you
@@ -64,9 +64,19 @@ core.ignoreStat::
        slow, such as Microsoft Windows.  See gitlink:git-update-index[1].
        False by default.
 
-core.onlyUseSymrefs::
-       Always use the "symref" format instead of symbolic links for HEAD
-       and other symbolic reference files. True by default.
+core.preferSymlinkRefs::
+       Instead of the default "symref" format for HEAD
+       and other symbolic reference files, use symbolic links.
+       This is sometimes needed to work with old scripts that
+       expect HEAD to be a symbolic link.
+
+core.logAllRefUpdates::
+       If true, `git-update-ref` will append a line to
+       "$GIT_DIR/logs/<ref>" listing the new SHA1 and the date/time
+       of the update.  If the file does not exist it will be
+       created automatically.  This information can be used to
+       determine what commit was the tip of a branch "2 days ago".
+       This value is false by default (no logging).
 
 core.repositoryFormatVersion::
        Internal variable identifying the repository format and layout
@@ -81,6 +91,15 @@ core.warnAmbiguousRefs::
        If true, git will warn you if the ref name you passed it is ambiguous
        and might match multiple refs in the .git/refs/ tree. True by default.
 
+alias.*::
+       Command aliases for the gitlink:git[1] command wrapper - e.g.
+       after defining "alias.last = cat-file commit HEAD", the invocation
+       "git last" is equivalent to "git cat-file commit HEAD". To avoid
+       confusion and troubles with script usage, aliases that
+       hide existing git commands are ignored. Arguments are split by
+       spaces, the usual shell quoting and escaping is supported.
+       quote pair and a backslash can be used to quote them.
+
 apply.whitespace::
        Tells `git-apply` how to handle whitespaces, in the same way
        as the '--whitespace' option. See gitlink:git-apply[1].
@@ -103,37 +122,37 @@ gitcvs.logfile::
 
 http.sslVerify::
        Whether to verify the SSL certificate when fetching or pushing
-       over HTTPS. Can be overriden by the 'GIT_SSL_NO_VERIFY' environment
+       over HTTPS. Can be overridden by the 'GIT_SSL_NO_VERIFY' environment
        variable.
 
 http.sslCert::
        File containing the SSL certificate when fetching or pushing
-       over HTTPS. Can be overriden by the 'GIT_SSL_CERT' environment
+       over HTTPS. Can be overridden by the 'GIT_SSL_CERT' environment
        variable.
 
 http.sslKey::
        File containing the SSL private key when fetching or pushing
-       over HTTPS. Can be overriden by the 'GIT_SSL_KEY' environment
+       over HTTPS. Can be overridden by the 'GIT_SSL_KEY' environment
        variable.
 
 http.sslCAInfo::
        File containing the certificates to verify the peer with when
-       fetching or pushing over HTTPS. Can be overriden by the
+       fetching or pushing over HTTPS. Can be overridden by the
        'GIT_SSL_CAINFO' environment variable.
 
 http.sslCAPath::
        Path containing files with the CA certificates to verify the peer
-       with when fetching or pushing over HTTPS. Can be overriden
+       with when fetching or pushing over HTTPS. Can be overridden
        by the 'GIT_SSL_CAPATH' environment variable.
 
 http.maxRequests::
-       How many HTTP requests to launch in parallel. Can be overriden
+       How many HTTP requests to launch in parallel. Can be overridden
        by the 'GIT_HTTP_MAX_REQUESTS' environment variable. Default is 5.
 
 http.lowSpeedLimit, http.lowSpeedTime::
        If the HTTP transfer speed is less than 'http.lowSpeedLimit'
        for longer than 'http.lowSpeedTime' seconds, the transfer is aborted.
-       Can be overriden by the 'GIT_HTTP_LOW_SPEED_LIMIT' and
+       Can be overridden by the 'GIT_HTTP_LOW_SPEED_LIMIT' and
        'GIT_HTTP_LOW_SPEED_TIME' environment variables.
 
 i18n.commitEncoding::
@@ -164,12 +183,12 @@ showbranch.default::
 
 user.email::
        Your email address to be recorded in any newly created commits.
-       Can be overriden by the 'GIT_AUTHOR_EMAIL' and 'GIT_COMMITTER_EMAIL'
+       Can be overridden by the 'GIT_AUTHOR_EMAIL' and 'GIT_COMMITTER_EMAIL'
        environment variables.  See gitlink:git-commit-tree[1].
 
 user.name::
        Your full name to be recorded in any newly created commits.
-       Can be overriden by the 'GIT_AUTHOR_NAME' and 'GIT_COMMITTER_NAME'
+       Can be overridden by the 'GIT_AUTHOR_NAME' and 'GIT_COMMITTER_NAME'
        environment variables.  See gitlink:git-commit-tree[1].
 
 whatchanged.difftree::
index 4211c81..1185897 100644 (file)
@@ -1,5 +1,5 @@
-A short git tutorial
-====================
+A git core tutorial for developers
+==================================
 
 Introduction
 ------------
@@ -184,7 +184,7 @@ $ git-cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238
 ----------------
 
 where the `-t` tells `git-cat-file` to tell you what the "type" of the
-object is. git will tell you that you have a "blob" object (ie just a
+object is. git will tell you that you have a "blob" object (i.e., just a
 regular file), and you can see the contents with
 
 ----------------
@@ -619,7 +619,7 @@ $ git tag -s <tagname>
 ----------------
 
 which will sign the current `HEAD` (but you can also give it another
-argument that specifies the thing to tag, ie you could have tagged the
+argument that specifies the thing to tag, i.e., you could have tagged the
 current `mybranch` point by using `git tag <tagname> mybranch`).
 
 You normally only do signed tags for major releases or things
@@ -971,7 +971,7 @@ $ git show-branch --topo-order master mybranch
 The first two lines indicate that it is showing the two branches
 and the first line of the commit log message from their
 top-of-the-tree commits, you are currently on `master` branch
-(notice the asterisk `*` character), and the first column for
+(notice the asterisk `\*` character), and the first column for
 the later output lines is used to show commits contained in the
 `master` branch, and the second column for the `mybranch`
 branch. Three commits are shown along with their log messages.
@@ -1097,7 +1097,7 @@ commit object by downloading from `repo.git/objects/xx/xxx\...`
 using the object name of that commit object.  Then it reads the
 commit object to find out its parent commits and the associate
 tree object; it repeats this process until it gets all the
-necessary objects.  Because of this behaviour, they are
+necessary objects.  Because of this behavior, they are
 sometimes also called 'commit walkers'.
 +
 The 'commit walkers' are sometimes also called 'dumb
index fa94efd..1fbca83 100644 (file)
@@ -1,7 +1,7 @@
 git for CVS users
 =================
 
-So you're a CVS user. That's ok, it's a treatable condition.  The job of
+So you're a CVS user. That's OK, it's a treatable condition.  The job of
 this document is to put you on the road to recovery, by helping you
 convert an existing cvs repository to git, and by showing you how to use a
 git repository in a cvs-like fashion.
@@ -106,7 +106,7 @@ Make sure committers have a umask of at most 027, so that the directories
 they create are writable and searchable by other group members.
 
 Suppose this repository is now set up in /pub/repo.git on the host
-foo.com.  Then as an individual commiter you can clone the shared
+foo.com.  Then as an individual committer you can clone the shared
 repository:
 
 ------------------------------------------------
@@ -159,7 +159,7 @@ other than `master`.
 
 [NOTE]
 ============
-Because of this behaviour, if the shared repository and the developer's
+Because of this behavior, if the shared repository and the developer's
 repository both have branches named `origin`, then a push like the above
 attempts to update the `origin` branch in the shared repository from the
 developer's `origin` branch.  The results may be unexpected, so it's
index c183dc9..f523ec2 100644 (file)
 --stat::
        Generate a diffstat instead of a patch.
 
+--summary::
+       Output a condensed summary of extended header information
+       such as creations, renames and mode changes.
+
 --patch-with-stat::
        Generate patch and prepend its diffstat.
 
index 3ab9b91..b935c18 100644 (file)
@@ -45,7 +45,7 @@ Everybody uses these commands to feed and care git repositories.
 
   * gitlink:git-fsck-objects[1] to validate the repository.
 
-  * gitlink:git-prune[1] to garbage collect crufts in the
+  * gitlink:git-prune[1] to garbage collect cruft in the
     repository.
 
   * gitlink:git-repack[1] to pack loose objects for efficiency.
@@ -61,32 +61,32 @@ $ git prune
 $ git count-objects <2>
 $ git repack <3>
 $ git prune <4>
-
+------------
++
 <1> running without "--full" is usually cheap and assures the
 repository health reasonably well.
 <2> check how many loose objects there are and how much
-diskspace is wasted by not repacking.
+disk space is wasted by not repacking.
 <3> without "-a" repacks incrementally.  repacking every 4-5MB
 of loose objects accumulation may be a good rule of thumb.
 <4> after repack, prune removes the duplicate loose objects.
-------------
 
 Repack a small project into single pack.::
 +
 ------------
 $ git repack -a -d <1>
 $ git prune
-
+------------
++
 <1> pack all the objects reachable from the refs into one pack
 and remove unneeded other packs
-------------
 
 
 Individual Developer (Standalone)[[Individual Developer (Standalone)]]
 ----------------------------------------------------------------------
 
 A standalone individual developer does not exchange patches with
-other poeple, and works alone in a single repository, using the
+other people, and works alone in a single repository, using the
 following commands.
 
   * gitlink:git-show-branch[1] to see where you are.
@@ -129,10 +129,10 @@ $ git-init-db
 $ git add . <1>
 $ git commit -m 'import of frotz source tree.'
 $ git tag v2.43 <2>
-
+------------
++
 <1> add everything under the current directory.
 <2> make a lightweight, unannotated tag.
-------------
 
 Create a topic branch and develop.::
 +
@@ -153,7 +153,8 @@ $ git checkout master <9>
 $ git pull . alsa-audio <10>
 $ git log --since='3 days ago' <11>
 $ git log v2.43.. curses/ <12>
-
+------------
++
 <1> create a new topic branch.
 <2> revert your botched changes in "curses/ux_audio_oss.c".
 <3> you need to tell git if you added a new file; removal and
@@ -170,7 +171,6 @@ you originally wrote.
 combined and include --max-count=10 (show 10 commits), --until='2005-12-10'.
 <12> view only the changes that touch what's in curses/
 directory, since v2.43 tag.
-------------
 
 
 Individual Developer (Participant)[[Individual Developer (Participant)]]
@@ -208,7 +208,8 @@ $ git pull git://git.kernel.org/pub/.../jgarzik/libata-dev.git ALL <5>
 $ git reset --hard ORIG_HEAD <6>
 $ git prune <7>
 $ git fetch --tags <8>
-
+------------
++
 <1> repeat as needed.
 <2> extract patches from your branch for e-mail submission.
 <3> "pull" fetches from "origin" by default and merges into the
@@ -221,7 +222,6 @@ area we are interested in.
 <7> garbage collect leftover objects from reverted pull.
 <8> from time to time, obtain official tags from the "origin"
 and store them under .git/refs/tags/.
-------------
 
 
 Push into another repository.::
@@ -239,7 +239,8 @@ satellite$ git push origin <4>
 mothership$ cd frotz
 mothership$ git checkout master
 mothership$ git pull . satellite <5>
-
+------------
++
 <1> mothership machine has a frotz repository under your home
 directory; clone from it to start a repository on the satellite
 machine.
@@ -252,7 +253,6 @@ to local "origin" branch.
 mothership machine.  You could use this as a back-up method.
 <5> on mothership machine, merge the work done on the satellite
 machine into the master branch.
-------------
 
 Branch off of a specific tag.::
 +
@@ -262,12 +262,12 @@ $ edit/compile/test; git commit -a
 $ git checkout master
 $ git format-patch -k -m --stdout v2.6.14..private2.6.14 |
   git am -3 -k <2>
-
+------------
++
 <1> create a private branch based on a well known (but somewhat behind)
 tag.
 <2> forward port all changes in private2.6.14 branch to master branch
 without a formal "merging".
-------------
 
 
 Integrator[[Integrator]]
@@ -317,7 +317,8 @@ $ git tag -s -m 'GIT 0.99.9x' v0.99.9x <10>
 $ git fetch ko && git show-branch master maint 'tags/ko-*' <11>
 $ git push ko <12>
 $ git push ko v0.99.9x <13>
-
+------------
++
 <1> see what I was in the middle of doing, if any.
 <2> see what topic branches I have and think about how ready
 they are.
@@ -335,18 +336,22 @@ master, nor exposed as a part of a stable branch.
 <11> make sure I did not accidentally rewind master beyond what I
 already pushed out.  "ko" shorthand points at the repository I have
 at kernel.org, and looks like this:
-    $ cat .git/remotes/ko
-    URL: kernel.org:/pub/scm/git/git.git
-    Pull: master:refs/tags/ko-master
-    Pull: maint:refs/tags/ko-maint
-    Push: master
-    Push: +pu
-    Push: maint
++
+------------
+$ cat .git/remotes/ko
+URL: kernel.org:/pub/scm/git/git.git
+Pull: master:refs/tags/ko-master
+Pull: maint:refs/tags/ko-maint
+Push: master
+Push: +pu
+Push: maint
+------------
++
 In the output from "git show-branch", "master" should have
 everything "ko-master" has.
+
 <12> push out the bleeding edge.
 <13> push the tag out, too.
-------------
 
 
 Repository Administration[[Repository Administration]]
@@ -367,17 +372,39 @@ example of managing a shared central repository.
 
 Examples
 ~~~~~~~~
-
 Run git-daemon to serve /pub/scm from inetd.::
 +
 ------------
-$ grep git /etc/inet.conf
+$ grep git /etc/inetd.conf
 git    stream  tcp     nowait  nobody \
   /usr/bin/git-daemon git-daemon --inetd --syslog --export-all /pub/scm
 ------------
 +
 The actual configuration line should be on one line.
 
+Run git-daemon to serve /pub/scm from xinetd.::
++
+------------
+$ cat /etc/xinetd.d/git-daemon
+# default: off
+# description: The git server offers access to git repositories
+service git
+{
+        disable = no
+        type            = UNLISTED
+        port            = 9418
+        socket_type     = stream
+        wait            = no
+        user            = nobody
+        server          = /usr/bin/git-daemon
+        server_args     = --inetd --syslog --export-all --base-path=/pub/scm
+        log_on_failure  += USERID
+}
+------------
++
+Check your xinetd(8) documentation and setup, this is from a Fedora system.
+Others might be different.
+
 Give push/pull only access to developers.::
 +
 ------------
@@ -388,13 +415,13 @@ cindy:x:1002:1002::/home/cindy:/usr/bin/git-shell
 david:x:1003:1003::/home/david:/usr/bin/git-shell
 $ grep git /etc/shells <2>
 /usr/bin/git-shell
-
+------------
++
 <1> log-in shell is set to /usr/bin/git-shell, which does not
 allow anything but "git push" and "git pull".  The users should
 get an ssh access to the machine.
 <2> in many distributions /etc/shells needs to list what is used
 as the login shell.
-------------
 
 CVS-style shared repository.::
 +
@@ -419,7 +446,8 @@ $ cat info/allowed-users <4>
 refs/heads/master      alice\|cindy
 refs/heads/doc-update  bob
 refs/tags/v[0-9]*      david
-
+------------
++
 <1> place the developers into the same git group.
 <2> and make the shared repository writable by the group.
 <3> use update-hook example by Carl from Documentation/howto/
@@ -427,7 +455,6 @@ for branch policy control.
 <4> alice and cindy can push into master, only bob can push into doc-update.
 david is the release manager and is the only person who can
 create and push version tags.
-------------
 
 HTTP server to support dumb protocol transfer.::
 +
@@ -435,7 +462,7 @@ HTTP server to support dumb protocol transfer.::
 dev$ git update-server-info <1>
 dev$ ftp user@isp.example.com <2>
 ftp> cp -r .git /home/user/myproject.git
-
+------------
++
 <1> make sure your info/refs and objects/info/packs are up-to-date
 <2> upload to public HTTP server hosted by your ISP.
-------------
index ae24547..6342ea3 100644 (file)
@@ -14,11 +14,13 @@ DESCRIPTION
 A simple wrapper for git-update-index to add files to the index,
 for people used to do "cvs add".
 
+It only adds non-ignored files, to add ignored files use
+"git update-index --add".
 
 OPTIONS
 -------
 <file>...::
-       Files to add to the index.
+       Files to add to the index (see gitlink:git-ls-files[1]).
 
 -n::
         Don't actually add the file(s), just show if they exist.
@@ -26,7 +28,7 @@ OPTIONS
 -v::
         Be verbose.
 
---::
+\--::
        This option can be used to separate command-line options from
        the list of files, (useful when filenames might be mistaken
        for command-line options).
@@ -68,6 +70,7 @@ git-add git-*.sh::
 See Also
 --------
 gitlink:git-rm[1]
+gitlink:git-ls-files[1]
 
 Author
 ------
index e93ea1f..2ff7494 100644 (file)
@@ -76,7 +76,7 @@ OPTIONS
 -C<n>::
        Ensure at least <n> lines of surrounding context match before
        and after each change.  When fewer lines of surrounding
-       context exist they all most match.  By default no context is
+       context exist they all must match.  By default no context is
        ever ignored.
 
 --apply::
@@ -113,7 +113,7 @@ OPTIONS
        When `git-apply` is used for statistics and not applying a
        patch, it defaults to `nowarn`.
        You can use different `<option>` to control this
-       behaviour:
+       behavior:
 +
 * `nowarn` turns off the trailing whitespace warning.
 * `warn` outputs warnings for a few such errors, but applies the
index 5189878..bfed945 100644 (file)
@@ -16,11 +16,14 @@ which introduced the line. Start annotation from the given revision.
 
 OPTIONS
 -------
--c, --compability::
+-c, --compatibility::
        Use the same output mode as git-annotate (Default: off).
 
 -l, --long::
-       Show long rev (Defaults off).
+       Show long rev (Default: off).
+
+-t, --time::
+       Show raw timestamp (Default: off).
 
 -S, --rev-file <revs-file>::
        Use revs from revs-file instead of calling git-rev-list.
index 71ecd85..d43ef1d 100644 (file)
@@ -3,22 +3,28 @@ git-branch(1)
 
 NAME
 ----
-git-branch - Create a new branch, or remove an old one
+git-branch - List, create, or delete branches.
 
 SYNOPSIS
 --------
 [verse]
-'git-branch' [[-f] <branchname> [<start-point>]]
-'git-branch' (-d | -D) <branchname>
+'git-branch' [-r]
+'git-branch' [-l] [-f] <branchname> [<start-point>]
+'git-branch' (-d | -D) <branchname>...
 
 DESCRIPTION
 -----------
-If no argument is provided, show available branches and mark current
-branch with star. Otherwise, create a new branch of name <branchname>.
-If a starting point is also specified, that will be where the branch is
-created, otherwise it will be created at the current HEAD.
+With no arguments given (or just `-r`) a list of available branches
+will be shown, the current branch will be highlighted with an asterisk.
 
-With a `-d` or `-D` option, `<branchname>` will be deleted.
+In its second form, a new branch named <branchname> will be created.
+It will start out with a head equal to the one given as <start-point>.
+If no <start-point> is given, the branch will be created with a head
+equal to that of the currently checked out branch.
+
+With a `-d` or `-D` option, `<branchname>` will be deleted.  You may
+specify more than one branch for deletion.  If the branch currently
+has a ref log then the ref log will also be deleted.
 
 
 OPTIONS
@@ -29,41 +35,65 @@ OPTIONS
 -D::
        Delete a branch irrespective of its index status.
 
+-l::
+       Create the branch's ref log.  This activates recording of
+       all changes to made the branch ref, enabling use of date
+       based sha1 expressions such as "<branchname>@{yesterday}".
+
 -f::
-       Force a reset of <branchname> to <start-point> (or current head).
+       Force the creation of a new branch even if it means deleting
+       a branch that already exists with the same name.
+
+-r::
+       List only the "remote" branches.
 
 <branchname>::
        The name of the branch to create or delete.
+       The new branch name must pass all checks defined by
+       gitlink:git-check-ref-format[1].  Some of these checks
+       may restrict the characters allowed in a branch name.
 
 <start-point>::
-       Where to create the branch; defaults to HEAD. This
-       option has no meaning with -d and -D.
+       The new branch will be created with a HEAD equal to this.  It may
+       be given as a branch name, a commit-id, or a tag.  If this option
+       is omitted, the current branch is assumed.
+
 
 
 Examples
-~~~~~~~~
+--------
 
 Start development off of a known tag::
 +
 ------------
 $ git clone git://git.kernel.org/pub/scm/.../linux-2.6 my2.6
 $ cd my2.6
-$ git branch my2.6.14 v2.6.14 <1>
+$ git branch my2.6.14 v2.6.14   <1>
 $ git checkout my2.6.14
-
-<1> These two steps are the same as "checkout -b my2.6.14 v2.6.14".
 ------------
++
+<1> This step and the next one could be combined into a single step with
+"checkout -b my2.6.14 v2.6.14".
 
 Delete unneeded branch::
 +
 ------------
 $ git clone git://git.kernel.org/.../git.git my.git
 $ cd my.git
-$ git branch -D todo <1>
-
+$ git branch -D todo    <1>
+------------
++
 <1> delete todo branch even if the "master" branch does not have all
 commits from todo branch.
-------------
+
+
+Notes
+-----
+
+If you are creating a branch that you want to immediately checkout, it's
+easier to use the git checkout command with its `-b` option to create
+a branch and check it out with a single command.
+
 
 Author
 ------
index 504eb1b..5e9cbf8 100644 (file)
@@ -8,12 +8,12 @@ git-cat-file - Provide content or type information for repository objects
 
 SYNOPSIS
 --------
-'git-cat-file' [-t | -s | -e | <type>] <object>
+'git-cat-file' [-t | -s | -e | -p | <type>] <object>
 
 DESCRIPTION
 -----------
 Provides content or type of objects in the repository. The type
-is required unless '-t' is used to find the object type,
+is required unless '-t' or '-p' is used to find the object type,
 or '-s' is used to find the object size.
 
 OPTIONS
@@ -33,6 +33,9 @@ OPTIONS
        Suppress all output; instead exit with zero status if <object>
        exists and is a valid object.
 
+-p::
+       Pretty-print the contents of <object> based on its type.
+
 <type>::
        Typically this matches the real type of <object> but asking
        for a type that can trivially be dereferenced from the given
@@ -49,6 +52,8 @@ If '-s' is specified, the size of the <object> in bytes.
 
 If '-e' is specified, no output.
 
+If '-p' is specified, the contents of <object> are pretty-printed.
+
 Otherwise the raw (though uncompressed) contents of the <object> will
 be returned.
 
index 7dc1bdb..13a5f43 100644 (file)
@@ -19,8 +19,9 @@ branch head is stored under `$GIT_DIR/refs/heads` directory, and
 a tag is stored under `$GIT_DIR/refs/tags` directory.  git
 imposes the following rules on how refs are named:
 
-. It could be named hierarchically (i.e. separated with slash
-  `/`), but each of its component cannot begin with a dot `.`;
+. It can include slash `/` for hierarchical (directory)
+  grouping, but no slash-separated component can begin with a
+  dot `.`;
 
 . It cannot have two consecutive dots `..` anywhere;
 
@@ -45,6 +46,8 @@ refname expressions (see gitlink:git-rev-parse[1]).  Namely:
 
 . colon `:` is used as in `srcref:dstref` to mean "use srcref\'s
   value and store it in dstref" in fetch and push operations.
+  It may also be used to select a specific object such as with
+  gitlink:git-cat-file[1] "git-cat-file blob v1.3.3:refs.c".
 
 
 GIT
index 09bd6a5..765c173 100644 (file)
@@ -63,7 +63,7 @@ OPTIONS
        Only meaningful with `--stdin`; paths are separated with
        NUL character instead of LF.
 
---::
+\--::
        Do not interpret any more arguments as options.
 
 The order of the flags used to matter, but not anymore.
index 985bb2f..fbdbadc 100644 (file)
@@ -8,7 +8,7 @@ git-checkout - Checkout and switch to a branch
 SYNOPSIS
 --------
 [verse]
-'git-checkout' [-f] [-b <new_branch>] [-m] [<branch>]
+'git-checkout' [-f] [-b <new_branch> [-l]] [-m] [<branch>]
 'git-checkout' [-m] [<branch>] <paths>...
 
 DESCRIPTION
@@ -35,7 +35,15 @@ OPTIONS
        Force a re-read of everything.
 
 -b::
-       Create a new branch and start it at <branch>.
+       Create a new branch named <new_branch> and start it at
+       <branch>.  The new branch name must pass all checks defined
+       by gitlink:git-check-ref-format[1].  Some of these checks
+       may restrict the characters allowed in a branch name.
+
+-l::
+       Create the new branch's ref log.  This activates recording of
+       all changes to made the branch ref, enabling use of date
+       based sha1 expressions such as "<branchname>@{yesterday}".
 
 -m::
        If you have local modifications to one or more files that
@@ -66,19 +74,19 @@ the `Makefile` to two revisions back, deletes hello.c by
 mistake, and gets it back from the index.
 +
 ------------
-$ git checkout master <1>
-$ git checkout master~2 Makefile <2>
+$ git checkout master             <1>
+$ git checkout master~2 Makefile  <2>
 $ rm -f hello.c
-$ git checkout hello.c <3>
-
+$ git checkout hello.c            <3>
+------------
++
 <1> switch branch
 <2> take out a file out of other commit
-<3> or "git checkout -- hello.c", as in the next example.
-------------
+<3> restore hello.c from HEAD of current branch
 +
-If you have an unfortunate branch that is named `hello.c`, the
-last step above would be confused as an instruction to switch to
-that branch.  You should instead write:
+If you have an unfortunate branch that is named `hello.c`, this
+step would be confused as an instruction to switch to that branch.
+You should instead write:
 +
 ------------
 $ git checkout -- hello.c
index 9a5e371..893baaa 100644 (file)
@@ -11,11 +11,20 @@ SYNOPSIS
 
 DESCRIPTION
 -----------
-Each commit between the fork-point and <head> is examined, and compared against
-the change each commit between the fork-point and <upstream> introduces.
-Commits already included in upstream are prefixed with '-' (meaning "drop from
-my local pull"), while commits missing from upstream are prefixed with '+'
-(meaning "add to the updated upstream").
+The changeset (or "diff") of each commit between the fork-point and <head>
+is compared against each commit between the fork-point and <upstream>.
+
+Every commit with a changeset that doesn't exist in the other branch
+has its id (sha1) reported, prefixed by a symbol.  Those existing only
+in the <upstream> branch are prefixed with a minus (-) sign, and those
+that only exist in the <head> branch are prefixed with a plus (+) symbol.
+
+Because git-cherry compares the changeset rather than the commit id
+(sha1), you can use git-cherry to find out if a commit you made locally
+has been applied <upstream> under a different commit id.  For example,
+this will happen if you're feeding patches <upstream> via email rather
+than pushing or pulling commits directly.
+
 
 OPTIONS
 -------
index 36890c5..c61afbc 100644 (file)
@@ -8,7 +8,7 @@ git-clean - Remove untracked files from the working tree
 SYNOPSIS
 --------
 [verse]
-'git-clean' [-d] [-n] [-q] [-x | -X]
+'git-clean' [-d] [-n] [-q] [-x | -X] [--] <paths>...
 
 DESCRIPTION
 -----------
@@ -16,6 +16,9 @@ Removes files unknown to git.  This allows to clean the working tree
 from files that are not under version control.  If the '-x' option is
 specified, ignored files are also removed, allowing to remove all
 build products.
+When optional `<paths>...` arguments are given, the paths
+affected are further limited to those that match them.
+
 
 OPTIONS
 -------
index 131e445..a90521e 100644 (file)
@@ -9,9 +9,9 @@ git-clone - Clones a repository
 SYNOPSIS
 --------
 [verse]
-'git-clone' [-l [-s]] [-q] [-n] [--bare] [-o <name>] [-u <upload-pack>]
-         [--reference <repository>]
-         <repository> [<directory>]
+'git-clone' [--template=<template_directory>] [-l [-s]] [-q] [-n] [--bare]
+         [-o <name>] [-u <upload-pack>] [--reference <repository>]
+         [--use-separate-remote] <repository> [<directory>]
 
 DESCRIPTION
 -----------
@@ -73,7 +73,7 @@ OPTIONS
        files in `<directory>/.git`, make the `<directory>`
        itself the `$GIT_DIR`. This implies `-n` option.  When
        this option is used, neither the `origin` branch nor the
-       default `remotes/origin` file is created.
+       default `remotes/origin` file is created.
 
 -o <name>::
        Instead of using the branch name 'origin' to keep track
@@ -89,19 +89,29 @@ OPTIONS
        the command to specify non-default path for the command
        run on the other end.
 
+--template=<template_directory>::
+       Specify the directory from which templates will be used;
+       if unset the templates are taken from the installation
+       defined default, typically `/usr/share/git-core/templates`.
+
+--use-separate-remote::
+       Save remotes heads under `$GIT_DIR/remotes/origin/` instead
+       of `$GIT_DIR/refs/heads/`.  Only the master branch is saved
+       in the latter.
+
 <repository>::
        The (possibly remote) repository to clone from.  It can
        be any URL git-fetch supports.
 
 <directory>::
-       The name of a new directory to clone into.  The "humanish"
+       The name of a new directory to clone into.  The "humanish"
        part of the source repository is used if no directory is
        explicitly given ("repo" for "/path/to/repo.git" and "foo"
        for "host.xz:foo/.git").  Cloning into an existing directory
        is not allowed.
 
 Examples
-~~~~~~~~
+--------
 
 Clone from upstream::
 +
index 0a7365b..0fe66f2 100644 (file)
@@ -98,7 +98,7 @@ but can be used to amend a merge commit.
        Instead of committing only the files specified on the
        command line, update them in the index file and then
        commit the whole index.  This is the traditional
-       behaviour.
+       behavior.
 
 -o|--only::
        Commit only the files specified on the command line.
@@ -106,7 +106,7 @@ but can be used to amend a merge commit.
        index and the latest commit does not match on the
        specified paths to avoid confusion.
 
---::
+\--::
        Do not interpret any more arguments as options.
 
 <file>...::
index 47216f4..198ce77 100644 (file)
@@ -7,13 +7,23 @@ git-count-objects - Reports on unpacked objects
 
 SYNOPSIS
 --------
-'git-count-objects'
+'git-count-objects' [-v]
 
 DESCRIPTION
 -----------
 This counts the number of unpacked object files and disk space consumed by
 them, to help you decide when it is a good time to repack.
 
+
+OPTIONS
+-------
+-v::
+       In addition to the number of loose objects and disk
+       space consumed, it reports the number of in-pack
+       objects, and number of objects that can be removed by
+       running `git-prune-packed`.
+
+
 Author
 ------
 Written by Junio C Hamano <junkio@cox.net>
index d30435a..56bd3e5 100644 (file)
@@ -8,7 +8,7 @@ git-cvsexportcommit - Export a commit to a CVS checkout
 
 SYNOPSIS
 --------
-'git-cvsexportcommmit' [-h] [-v] [-c] [-p] [PARENTCOMMIT] COMMITID
+'git-cvsexportcommmit' [-h] [-v] [-c] [-p] [-f] [-m msgprefix] [PARENTCOMMIT] COMMITID
 
 
 DESCRIPTION
@@ -39,6 +39,13 @@ OPTIONS
        Be pedantic (paranoid) when applying patches. Invokes patch with 
        --fuzz=0
 
+-f::
+       Force the merge even if the files are not up to date.
+
+-m::
+       Prepend the commit message with the provided prefix. 
+       Useful for patch series and the like.
+
 -v::
        Verbose.
 
index 4dc13c3..e328db3 100644 (file)
@@ -36,49 +36,62 @@ INSTALLATION
 
 1. If you are going to offer anonymous CVS access via pserver, add a line in
    /etc/inetd.conf like
-
++
+--
+------
    cvspserver stream tcp nowait nobody git-cvsserver pserver
 
-   Note: In some cases, you need to pass the 'pserver' argument twice for
-   git-cvsserver to see it. So the line would look like
+------
+Note: In some cases, you need to pass the 'pserver' argument twice for
+git-cvsserver to see it. So the line would look like
 
+------
    cvspserver stream tcp nowait nobody git-cvsserver pserver pserver
 
-   No special setup is needed for SSH access, other than having GIT tools
-   in the PATH. If you have clients that do not accept the CVS_SERVER
-   env variable, you can rename git-cvsserver to cvs.
-
+------
+No special setup is needed for SSH access, other than having GIT tools
+in the PATH. If you have clients that do not accept the CVS_SERVER
+env variable, you can rename git-cvsserver to cvs.
+--
 2. For each repo that you want accessible from CVS you need to edit config in
    the repo and add the following section.
-
++
+--
+------
    [gitcvs]
         enabled=1
         # optional for debugging
         logfile=/path/to/logfile
 
-   Note: you need to ensure each user that is going to invoke git-cvsserver has
-   write access to the log file and to the git repository. When offering anon
-   access via pserver, this means that the nobody user should have write access
-   to at least the sqlite database at the root of the repository.
-
+------
+Note: you need to ensure each user that is going to invoke git-cvsserver has
+write access to the log file and to the git repository. When offering anon
+access via pserver, this means that the nobody user should have write access
+to at least the sqlite database at the root of the repository.
+--
 3. On the client machine you need to set the following variables.
    CVSROOT should be set as per normal, but the directory should point at the
    appropriate git repo. For example:
++
+--
+For SSH access, CVS_SERVER should be set to git-cvsserver
 
-   For SSH access, CVS_SERVER should be set to git-cvsserver
-
-   Example:
+Example:
 
+------
      export CVSROOT=:ext:user@server:/var/git/project.git
      export CVS_SERVER=git-cvsserver
-
+------
+--
 4. For SSH clients that will make commits, make sure their .bashrc file
    sets the GIT_AUTHOR and GIT_COMMITTER variables.
 
 5. Clients should now be able to check out the project. Use the CVS 'module'
    name to indicate what GIT 'head' you want to check out. Example:
-
++
+------
      cvs co -d project-master master
+------
 
 Eclipse CVS Client Notes
 ------------------------
@@ -94,7 +107,7 @@ To get a checkout with the Eclipse CVS client:
 4. Pick 'HEAD' when it asks what branch/tag to check out. Untick the
    "launch commit wizard" to avoid committing the .project file.
 
-Protocol notes: If you are using anonymous acces via pserver, just select that.
+Protocol notes: If you are using anonymous access via pserver, just select that.
 Those using SSH access should choose the 'ext' protocol, and configure 'ext'
 access on the Preferences->Team->CVS->ExtConnection pane. Set CVS_SERVER to
 'git-cvsserver'. Not that password support is not good when using 'ext',
index 924a676..4c357da 100644 (file)
@@ -20,7 +20,7 @@ aka 9418. It waits for a connection, and will just execute "git-upload-pack"
 when it gets one.
 
 It's careful in that there's a magic request-line that gives the command and
-what directory to upload, and it verifies that the directory is ok.
+what directory to upload, and it verifies that the directory is OK.
 
 It verifies that the directory has the magic file "git-daemon-export-ok", and
 it will refuse to export any git directory that hasn't explicitly been marked
@@ -28,7 +28,7 @@ for export this way (unless the '--export-all' parameter is specified). If you
 pass some directory paths as 'git-daemon' arguments, you can further restrict
 the offers to a whitelist comprising of those.
 
-This is ideally suited for read-only updates, ie pulling from git repositories.
+This is ideally suited for read-only updates, i.e., pulling from git repositories.
 
 OPTIONS
 -------
index 7a253ea..2700f35 100644 (file)
@@ -21,7 +21,7 @@ object name of the commit.
 OPTIONS
 -------
 <committish>::
-       The object name of the comittish. 
+       The object name of the committish.
 
 --all::
        Instead of using only the annotated tags, use any ref
index 5d2096a..9cd43f1 100644 (file)
@@ -101,7 +101,7 @@ have not actually done a "git-update-index" on it yet - there is no
   torvalds@ppc970:~/v2.6/linux> git-diff-index HEAD
   *100644->100664 blob    7476bb......->000000......      kernel/sched.c
 
-ie it shows that the tree has changed, and that `kernel/sched.c` has is
+i.e., it shows that the tree has changed, and that `kernel/sched.c` has is
 not up-to-date and may contain new stuff. The all-zero sha1 means that to
 get the real diff, you need to look at the object in the working directory
 directly rather than do an object-to-object diff.
index 2169169..f7e8ff2 100644 (file)
@@ -32,7 +32,7 @@ include::diff-options.txt[]
 <path>...::
        If provided, the results are limited to a subset of files
        matching one of these prefix strings.
-       ie file matches `/^<pattern1>|<pattern2>|.../`
+       i.e., file matches `/^<pattern1>|<pattern2>|.../`
        Note that this parameter does not provide any wildcard or regexp
        features.
 
@@ -54,7 +54,7 @@ include::diff-options.txt[]
 +
 When a single commit is given on one line of such input, it compares
 the commit with its parents.  The following flags further affects its
-behaviour.  This does not apply to the case where two <tree-ish>
+behavior.  This does not apply to the case where two <tree-ish>
 separated with a single space are given.
 
 -m::
@@ -92,7 +92,7 @@ separated with a single space are given.
        Furthermore, it lists only files which were modified
        from all parents.
 
--cc::
+--cc::
        This flag changes the way a merge commit patch is displayed,
        in a similar way to the '-c' option. It implies the '-c'
        and '-p' options and further compresses the patch output
index 890931c..7ab2080 100644 (file)
@@ -46,60 +46,60 @@ EXAMPLES
 Various ways to check your working tree::
 +
 ------------
-$ git diff <1>
-$ git diff --cached <2>
-$ git diff HEAD <3>
-
+$ git diff            <1>
+$ git diff --cached   <2>
+$ git diff HEAD       <3>
+------------
++
 <1> changes in the working tree since your last git-update-index.
 <2> changes between the index and your last commit; what you
 would be committing if you run "git commit" without "-a" option.
 <3> changes in the working tree since your last commit; what you
 would be committing if you run "git commit -a"
-------------
 
 Comparing with arbitrary commits::
 +
 ------------
-$ git diff test <1>
-$ git diff HEAD -- ./test <2>
-$ git diff HEAD^ HEAD <3>
-
+$ git diff test            <1>
+$ git diff HEAD -- ./test  <2>
+$ git diff HEAD^ HEAD      <3>
+------------
++
 <1> instead of using the tip of the current branch, compare with the
 tip of "test" branch.
 <2> instead of comparing with the tip of "test" branch, compare with
 the tip of the current branch, but limit the comparison to the
 file "test".
 <3> compare the version before the last commit and the last commit.
-------------
 
 
 Limiting the diff output::
 +
 ------------
-$ git diff --diff-filter=MRC <1>
-$ git diff --name-status -r <2>
-$ git diff arch/i386 include/asm-i386 <3>
-
+$ git diff --diff-filter=MRC            <1>
+$ git diff --name-status -r             <2>
+$ git diff arch/i386 include/asm-i386   <3>
+------------
++
 <1> show only modification, rename and copy, but not addition
 nor deletion.
 <2> show only names and the nature of change, but not actual
 diff output.  --name-status disables usual patch generation
-which in turn also disables recursive behaviour, so without -r
+which in turn also disables recursive behavior, so without -r
 you would only see the directory name if there is a change in a
 file in a subdirectory.
 <3> limit diff output to named subtrees.
-------------
 
 Munging the diff output::
 +
 ------------
-$ git diff --find-copies-harder -B -C <1>
-$ git diff -R <2>
-
+$ git diff --find-copies-harder -B -C  <1>
+$ git diff -R                          <2>
+------------
++
 <1> spend extra cycles to find renames, copies and complete
 rewrites (very expensive).
 <2> output diff in reverse.
-------------
 
 
 Author
index 7cc7faf..4ca0014 100644 (file)
@@ -9,26 +9,31 @@ git-format-patch - Prepare patches for e-mail submission
 SYNOPSIS
 --------
 [verse]
-'git-format-patch' [-n | -k] [-o <dir> | --stdout] [--attach] [-s] [-c]
-                [--diff-options] <his> [<mine>]
+'git-format-patch' [-n | -k] [-o <dir> | --stdout] [--attach]
+                  [-s | --signoff] [--diff-options] [--start-number <n>]
+                  <since>[..<until>]
 
 DESCRIPTION
 -----------
-Prepare each commit with its patch since <mine> head forked from
-<his> head, one file per patch formatted to resemble UNIX mailbox
-format, for e-mail submission or use with gitlink:git-am[1].
+
+Prepare each commit between <since> and <until> with its patch in
+one file per commit, formatted to resemble UNIX mailbox format.
+If ..<until> is not specified, the head of the current working
+tree is implied.
+
+The output of this command is convenient for e-mail submission or
+for use with gitlink:git-am[1].
 
 Each output file is numbered sequentially from 1, and uses the
-first line of the commit message (massaged for pathname safety)
-as the filename.
+first line of the commit message (massaged for pathname safety) as
+the filename. The names of the output files are printed to standard
+output, unless the --stdout option is specified.
 
-When -o is specified, output files are created in <dir>; otherwise
-they are created in the current working directory.  This option
-is ignored if --stdout is specified.
+If -o is specified, output files are created in <dir>.  Otherwise
+they are created in the current working directory.
 
-When -n is specified, instead of "[PATCH] Subject", the first
-line is formatted as "[PATCH N/M] Subject", unless you have only
-one patch.
+If -n is specified, instead of "[PATCH] Subject", the first line
+is formatted as "[PATCH n/m] Subject".
 
 
 OPTIONS
@@ -40,6 +45,9 @@ OPTIONS
 -n|--numbered::
        Name output in '[PATCH n/m]' format.
 
+--start-number <n>::
+       Start numbering the patches at <n> instead of 1.
+
 -k|--keep-subject::
        Do not strip/add '[PATCH]' from the first line of the
        commit log message.
@@ -48,17 +56,9 @@ OPTIONS
        Add `Signed-off-by:` line to the commit message, using
        the committer identity of yourself.
 
--c|--check::
-        Display suspicious lines in the patch.  The definition
-        of 'suspicious lines' is currently the lines that has
-        trailing whitespaces, and the lines whose indentation
-        has a SP character immediately followed by a TAB
-        character.
-
 --stdout::
-       This flag generates the mbox formatted output to the
-       standard output, instead of saving them into a file per
-       patch and implies --mbox.
+       Print all commits to the standard output in mbox format,
+       instead of creating a file for each one.
 
 --attach::
        Create attachments instead of inlining patches.
@@ -82,18 +82,18 @@ git-format-patch -k --stdout R1..R2 | git-am -3 -k::
        cherry-pick them.
 
 git-format-patch origin::
-       Extract commits the current branch accumulated since it
-       pulled from origin the last time in a patch form for
-       e-mail submission.
+       Extract all commits which are in the current branch but
+       not in the origin branch.  For each commit a separate file
+       is created in the current directory.
 
 git-format-patch -M -B origin::
-       The same as the previous one, except detect and handle
-       renames and complete rewrites intelligently to produce
-       renaming patch.  A renaming patch reduces the amount of
-       text output, and generally makes it easier to review
-       it.  Note that the "patch" program does not understand
-       renaming patch well, so use it only when you know the
-       recipient uses git to apply your patch.
+       The same as the previous one.  Additionally, it detects
+       and handles renames and complete rewrites intelligently to
+       produce a renaming patch.  A renaming patch reduces the
+       amount of text output, and generally makes it easier to
+       review it.  Note that the "patch" program does not
+       understand renaming patches, so use it only when you know
+       the recipient uses git to apply your patch.
 
 
 See Also
index 93ce9dc..d0af99d 100644 (file)
@@ -71,7 +71,7 @@ sorted properly etc), but on the whole if "git-fsck-objects" is happy, you
 do have a valid tree.
 
 Any corrupt objects you will have to find in backups or other archives
-(ie you can just remove them and do an "rsync" with some other site in
+(i.e., you can just remove them and do an "rsync" with some other site in
 the hopes that somebody else has the object you have corrupted).
 
 Of course, "valid tree" doesn't mean that it wasn't generated by some
index d55456a..7b810df 100644 (file)
@@ -8,43 +8,82 @@ git-grep - Print lines matching a pattern
 
 SYNOPSIS
 --------
-'git-grep' [<option>...] [-e] <pattern> [--] [<path>...]
+[verse]
+'git-grep' [--cached]
+          [-a | --text] [-I] [-i | --ignore-case] [-w | --word-regexp]
+          [-v | --invert-match]
+          [-E | --extended-regexp] [-G | --basic-regexp] [-F | --fixed-strings]
+          [-n] [-l | --files-with-matches] [-L | --files-without-match]
+          [-c | --count]
+          [-A <post-context>] [-B <pre-context>] [-C <context>]
+          [-f <file>] [-e <pattern>]
+          [<tree>...]
+          [--] [<path>...]
 
 DESCRIPTION
 -----------
-Searches list of files `git-ls-files` produces for lines
-containing a match to the given pattern.
+Look for specified patterns in the working tree files, blobs
+registered in the index file, or given tree objects.
 
 
 OPTIONS
 -------
-`--`::
-       Signals the end of options; the rest of the parameters
-       are <path> limiters.
+--cached::
+       Instead of searching in the working tree files, check
+       the blobs registered in the index file.
+
+-a | --text::
+       Process binary files as if they were text.
+
+-i | --ignore-case::
+       Ignore case differences between the patterns and the
+       files.
+
+-w | --word-regexp::
+       Match the pattern only at word boundary (either begin at the
+       beginning of a line, or preceded by a non-word character; end at
+       the end of a line or followed by a non-word character).
+
+-v | --invert-match::
+       Select non-matching lines.
+
+-E | --extended-regexp | -G | --basic-regexp::
+       Use POSIX extended/basic regexp for patterns.  Default
+       is to use basic regexp.
 
-<option>...::
-       Either an option to pass to `grep` or `git-ls-files`.
-+
-The following are the specific `git-ls-files` options
-that may be given: `-o`, `--cached`, `--deleted`, `--others`,
-`--killed`, `--ignored`, `--modified`, `--exclude=\*`,
-`--exclude-from=\*`, and `--exclude-per-directory=\*`.
-+
-All other options will be passed to `grep`.
+-n::
+       Prefix the line number to matching lines.
 
-<pattern>::
-       The pattern to look for.  The first non option is taken
-       as the pattern; if your pattern begins with a dash, use
-       `-e <pattern>`.
+-l | --files-with-matches | -L | --files-without-match::
+       Instead of showing every matched line, show only the
+       names of files that contain (or do not contain) matches.
 
-<path>...::
-       Optional paths to limit the set of files to be searched;
-       passed to `git-ls-files`.
+-c | --count::
+       Instead of showing every matched line, show the number of
+       lines that match.
+
+-[ABC] <context>::
+       Show `context` trailing (`A` -- after), or leading (`B`
+       -- before), or both (`C` -- context) lines, and place a
+       line containing `--` between contiguous groups of
+       matches.
+
+-f <file>::
+       Read patterns from <file>, one per line.
+
+`<tree>...`::
+       Search blobs in the trees for specified patterns.
+
+`--`::
+       Signals the end of options; the rest of the parameters
+       are <path> limiters.
 
 
 Author
 ------
-Written by Linus Torvalds <torvalds@osdl.org>
+Originally written by Linus Torvalds <torvalds@osdl.org>, later
+revamped by Junio C Hamano.
+
 
 Documentation
 --------------
index cfc0d88..eca9e9c 100644 (file)
@@ -29,6 +29,7 @@ CONFIGURATION
 git-imap-send requires the following values in the repository
 configuration file (shown with examples):
 
+..........................
 [imap]
     Folder = "INBOX.Drafts"
 
@@ -38,8 +39,9 @@ configuration file (shown with examples):
 [imap]
     Host = imap.server.com
     User = bob
-    Password = pwd
+    Pass = pwd
     Port = 143
+..........................
 
 
 BUGS
index aeb1115..8a150d8 100644 (file)
@@ -60,12 +60,12 @@ Start a new git repository for an existing code base::
 +
 ----------------
 $ cd /path/to/my/codebase
-$ git-init-db <1>
-$ git-add . <2>
-
+$ git-init-db   <1>
+$ git-add .     <2>
+----------------
++
 <1> prepare /path/to/my/codebase/.git directory
 <2> add all existing file to the index
-----------------
 
 
 Author
index 76cb894..c9ffff7 100644 (file)
@@ -14,13 +14,12 @@ DESCRIPTION
 -----------
 Shows the commit logs.
 
-The command takes options applicable to the gitlink::git-rev-list[1]
+The command takes options applicable to the gitlink:git-rev-list[1]
 command to control what is shown and how, and options applicable to
-the gitlink::git-diff-tree[1] commands to control how the change
+the gitlink:git-diff-tree[1] commands to control how the change
 each commit introduces are shown.
 
-This manual page describes only the most frequently used
-options.
+This manual page describes only the most frequently used options.
 
 
 OPTIONS
@@ -52,7 +51,7 @@ git log v2.6.12.. include/scsi drivers/scsi::
        Show all commits since version 'v2.6.12' that changed any file
        in the include/scsi or drivers/scsi subdirectories
 
-git log --since="2 weeks ago" -- gitk::
+git log --since="2 weeks ago" \-- gitk::
 
        Show the changes during the last two weeks to the file 'gitk'.
        The "--" is necessary to avoid confusion with the *branch* named
index 796d049..4d8a2ad 100644 (file)
@@ -106,7 +106,7 @@ OPTIONS
        lines, show only handful hexdigits prefix.
        Non default number of digits can be specified with --abbrev=<n>.
 
---::
+\--::
        Do not interpret any more arguments as options.
 
 <file>::
@@ -195,8 +195,7 @@ An exclude pattern is of the following format:
 
  - if it does not contain a slash '/', it is a shell glob
    pattern and used to match against the filename without
-   leading directories (i.e. the same way as the current
-   implementation).
+   leading directories.
 
  - otherwise, it is a shell glob pattern, suitable for
    consumption by fnmatch(3) with FNM_PATHNAME flag.  I.e. a
@@ -222,6 +221,19 @@ An example:
         --exclude-per-directory=.gitignore
 --------------------------------------------------------------
 
+Another example:
+
+--------------------------------------------------------------
+    $ cat .gitignore
+    vmlinux*
+    $ ls arch/foo/kernel/vm*
+    arch/foo/kernel/vmlinux.lds.S
+    $ echo '!/vmlinux*' >arch/foo/kernel/.gitignore
+--------------------------------------------------------------
+
+The second .gitignore keeps `arch/foo/kernel/vmlinux.lds.S` file
+from getting ignored.
+
 
 See Also
 --------
index 018c401..f283bac 100644 (file)
@@ -8,9 +8,10 @@ git-ls-tree - Lists the contents of a tree object
 
 SYNOPSIS
 --------
+[verse]
 'git-ls-tree' [-d] [-r] [-t] [-z]
-       [--name-only] [--name-status] [--full-name] [--abbrev=[<n>]]
-       <tree-ish> [paths...]
+           [--name-only] [--name-status] [--full-name] [--abbrev=[<n>]]
+           <tree-ish> [paths...]
 
 DESCRIPTION
 -----------
@@ -47,6 +48,10 @@ OPTIONS
        lines, show only handful hexdigits prefix.
        Non default number of digits can be specified with --abbrev=<n>.
 
+--full-name::
+       Instead of showing the path names relative to the current working
+       directory, show the full path names.
+
 paths::
        When paths are given, show them (note that this isn't really raw
        pathnames, but rather a list of patterns to match).  Otherwise
@@ -72,8 +77,6 @@ Documentation
 Documentation by David Greaves, Junio C Hamano and the git-list
 <git@vger.kernel.org>.
 
-This manual page is a stub. You can help the git documentation by expanding it.
-
 GIT
 ---
 Part of the gitlink:git[7] suite
index d1d56f1..6099be2 100644 (file)
@@ -8,16 +8,26 @@ git-merge-base - Finds as good a common ancestor as possible for a merge
 
 SYNOPSIS
 --------
-'git-merge-base' <commit> <commit>
+'git-merge-base' [--all] <commit> <commit>
 
 DESCRIPTION
 -----------
-"git-merge-base" finds as good a common ancestor as possible. Given a
-selection of equally good common ancestors it should not be relied on
-to decide in any particular way.
+
+"git-merge-base" finds as good a common ancestor as possible between
+the two commits. That is, given two commits A and B 'git-merge-base A
+B' will output a commit which is reachable from both A and B through
+the parent relationship.
+
+Given a selection of equally good common ancestors it should not be
+relied on to decide in any particular way.
 
 The "git-merge-base" algorithm is still in flux - use the source...
 
+OPTIONS
+-------
+--all::
+       Output all common ancestors for the two commits instead of
+       just one.
 
 Author
 ------
index fbc986a..6cd0601 100644 (file)
@@ -8,7 +8,7 @@ git-merge-index - Runs a merge for files needing merging
 
 SYNOPSIS
 --------
-'git-merge-index' [-o] [-q] <merge-program> (-a | -- | <file>\*)
+'git-merge-index' [-o] [-q] <merge-program> (-a | \-- | <file>\*)
 
 DESCRIPTION
 -----------
@@ -19,7 +19,7 @@ files are passed as arguments 5, 6 and 7.
 
 OPTIONS
 -------
---::
+\--::
        Do not interpret any more arguments as options.
 
 -a::
@@ -69,7 +69,7 @@ or
   fatal: merge program failed
 
 where the latter example shows how "git-merge-index" will stop trying to
-merge once anything has returned an error (ie "cat" returned an error
+merge once anything has returned an error (i.e., "cat" returned an error
 for the AA file, because it didn't exist in the original, and thus
 "git-merge-index" didn't even try to merge the MM thing).
 
index 6870708..39a1434 100644 (file)
@@ -8,7 +8,7 @@ git-name-rev - Find symbolic names for given revs
 
 SYNOPSIS
 --------
-'git-name-rev' [--tags] ( --all | --stdin | <commitish>... )
+'git-name-rev' [--tags] ( --all | --stdin | <committish>... )
 
 DESCRIPTION
 -----------
@@ -41,6 +41,7 @@ Enter git-name-rev:
 
 ------------
 % git name-rev 33db5f4d9027a10e477ccf054b2c1ab94f74c85a
+33db5f4d9027a10e477ccf054b2c1ab94f74c85a tags/v0.99^0~940
 ------------
 
 Now you are wiser, because you know that it happened 940 revisions before v0.99.
diff --git a/Documentation/git-p4import.txt b/Documentation/git-p4import.txt
new file mode 100644 (file)
index 0000000..0858e5e
--- /dev/null
@@ -0,0 +1,168 @@
+git-p4import(1)
+===============
+
+NAME
+----
+git-p4import - Import a Perforce repository into git
+
+
+SYNOPSIS
+--------
+`git-p4import` [-q|-v] [--notags] [--authors <file>] [-t <timezone>] <//p4repo/path> <branch>
+
+`git-p4import` --stitch <//p4repo/path>
+
+`git-p4import`
+
+
+DESCRIPTION
+-----------
+Import a Perforce repository into an existing git repository.  When
+a <//p4repo/path> and <branch> are specified a new branch with the
+given name will be created and the initial import will begin.
+
+Once the initial import is complete you can do an incremental import
+of new commits from the Perforce repository.  You do this by checking
+out the appropriate git branch and then running `git-p4import` without
+any options.
+
+The standard p4 client is used to communicate with the Perforce
+repository; it must be configured correctly in order for `git-p4import`
+to operate (see below).
+
+
+OPTIONS
+-------
+-q::
+       Do not display any progress information.
+
+-v::
+        Give extra progress information.
+
+\--authors::
+       Specify an authors file containing a mapping of Perforce user
+       ids to full names and email addresses (see Notes below).
+
+\--notags::
+       Do not create a tag for each imported commit.
+
+\--stitch::
+       Import the contents of the given perforce branch into the
+       currently checked out git branch.
+
+\--log::
+       Store debugging information in the specified file.
+
+-t::
+       Specify that the remote repository is in the specified timezone.
+       Timezone must be in the format "US/Pacific" or "Europe/London"
+       etc.  You only need to specify this once, it will be saved in
+       the git config file for the repository.
+
+<//p4repo/path>::
+       The Perforce path that will be imported into the specified branch.
+
+<branch>::
+       The new branch that will be created to hold the Perforce imports.
+
+
+P4 Client
+---------
+You must make the `p4` client command available in your $PATH and
+configure it to communicate with the target Perforce repository.
+Typically this means you must set the "$P4PORT" and "$P4CLIENT"
+environment variables.
+
+You must also configure a `p4` client "view" which maps the Perforce
+branch into the top level of your git repository, for example:
+
+------------
+Client: myhost
+
+Root:   /home/sean/import
+
+Options:   noallwrite clobber nocompress unlocked modtime rmdir
+
+View:
+        //public/jam/... //myhost/jam/...
+------------
+
+With the above `p4` client setup, you could import the "jam"
+perforce branch into a branch named "jammy", like so:
+
+------------
+$ mkdir -p /home/sean/import/jam
+$ cd /home/sean/import/jam
+$ git init-db
+$ git p4import //public/jam jammy
+------------
+
+
+Multiple Branches
+-----------------
+Note that by creating multiple "views" you can use `git-p4import`
+to import additional branches into the same git repository.
+However, the `p4` client has a limitation in that it silently
+ignores all but the last "view" that maps into the same local
+directory.  So the following will *not* work:
+
+------------
+View:
+        //public/jam/... //myhost/jam/...
+        //public/other/... //myhost/jam/...
+        //public/guest/... //myhost/jam/...
+------------
+
+If you want more than one Perforce branch to be imported into the
+same directory you must employ a workaround.  A simple option is
+to adjust your `p4` client before each import to only include a
+single view.
+
+Another option is to create multiple symlinks locally which all
+point to the same directory in your git repository and then use
+one per "view" instead of listing the actual directory.
+
+
+Tags
+----
+A git tag of the form p4/xx is created for every change imported from
+the Perforce repository where xx is the Perforce changeset number.
+Therefore after the import you can use git to access any commit by its
+Perforce number, eg. git show p4/327.
+
+The tag associated with the HEAD commit is also how `git-p4import`
+determines if there are new changes to incrementally import from the
+Perforce repository.
+
+If you import from a repository with many thousands of changes
+you will have an equal number of p4/xxxx git tags.  Git tags can
+be expensive in terms of disk space and repository operations.
+If you don't need to perform further incremental imports, you
+may delete the tags.
+
+
+Notes
+-----
+You can interrupt the import (eg. ctrl-c) at any time and restart it
+without worry.
+
+Author information is automatically determined by querying the
+Perforce "users" table using the id associated with each change.
+However, if you want to manually supply these mappings you can do
+so with the "--authors" option.  It accepts a file containing a list
+of mappings with each line containing one mapping in the format:
+
+------------
+    perforce_id = Full Name <email@address.com>
+------------
+
+
+Author
+------
+Written by Sean Estabrooks <seanlkml@sympatico.ca>
+
+
+GIT
+---
+Part of the gitlink:git[7] suite
+
index 723b8cc..5389097 100644 (file)
@@ -13,7 +13,7 @@ DESCRIPTION
 -----------
 A "patch ID" is nothing but a SHA1 of the diff associated with a patch, with
 whitespace and line numbers ignored.  As such, it's "reasonably stable", but at
-the same time also reasonably unique, ie two patches that have the same "patch
+the same time also reasonably unique, i.e., two patches that have the same "patch
 ID" are almost guaranteed to be the same thing.
 
 IOW, you can use this thing to look for likely duplicate commits.
index f694fcb..a11e303 100644 (file)
@@ -28,7 +28,7 @@ OPTIONS
        Do not remove anything; just report what it would
        remove.
 
---::
+\--::
        Do not interpret any more arguments as options.
 
 <head>...::
diff --git a/Documentation/git-quiltimport.txt b/Documentation/git-quiltimport.txt
new file mode 100644 (file)
index 0000000..6e9a8c3
--- /dev/null
@@ -0,0 +1,61 @@
+git-quiltimport(1)
+================
+
+NAME
+----
+git-quiltimport - Applies a quilt patchset onto the current branch
+
+
+SYNOPSIS
+--------
+[verse]
+'git-quiltimport' [--dry-run] [--author <author>] [--patches <dir>]
+
+
+DESCRIPTION
+-----------
+Applies a quilt patchset onto the current git branch, preserving
+the patch boundaries, patch order, and patch descriptions present
+in the quilt patchset.
+
+For each patch the code attempts to extract the author from the
+patch description.  If that fails it falls back to the author
+specified with --author.  If the --author flag was not given
+the patch description is displayed and the user is asked to
+interactively enter the author of the patch.
+
+If a subject is not found in the patch description the patch name is
+preserved as the 1 line subject in the git description.
+
+OPTIONS
+-------
+--dry-run::
+       Walk through the patches in the series and warn
+       if we cannot find all of the necessary information to commit
+       a patch.  At the time of this writing only missing author
+       information is warned about.
+
+--author Author Name <Author Email>::
+       The author name and email address to use when no author
+       information can be found in the patch description.
+
+--patches <dir>::
+       The directory to find the quilt patches and the
+       quilt series file.
+
+        The default for the patch directory is patches
+       or the value of the $QUILT_PATCHES environment
+       variable.
+
+Author
+------
+Written by Eric Biederman <ebiederm@lnxi.com>
+
+Documentation
+--------------
+Documentation by Eric Biederman <ebiederm@lnxi.com>
+
+GIT
+---
+Part of the gitlink:git[7] suite
+
index 1f21d95..11bd9c0 100644 (file)
@@ -214,7 +214,7 @@ The `git-write-tree` command refuses to write a nonsensical tree, and it
 will complain about unmerged entries if it sees a single entry that is not
 stage 0.
 
-Ok, this all sounds like a collection of totally nonsensical rules,
+OK, this all sounds like a collection of totally nonsensical rules,
 but it's actually exactly what you want in order to do a fast
 merge. The different stages represent the "result tree" (stage 0, aka
 "merged"), the original tree (stage 1, aka "orig"), and the two trees
@@ -235,7 +235,7 @@ populated.  Here is an outline of how the algorithm works:
 
 - the index file saves and restores with all this information, so you
   can merge things incrementally, but as long as it has entries in
-  stages 1/2/3 (ie "unmerged entries") you can't write the result. So
+  stages 1/2/3 (i.e., "unmerged entries") you can't write the result. So
   now the merge algorithm ends up being really simple:
 
   * you walk the index in order, and ignore all entries of stage 0,
@@ -266,7 +266,7 @@ file that does not match stage 2.
 This is done to prevent you from losing your work-in-progress
 changes, and mixing your random changes in an unrelated merge
 commit.  To illustrate, suppose you start from what has been
-commited last to your repository:
+committed last to your repository:
 
 ----------------
 $ JC=`git-rev-parse --verify "HEAD^0"`
index 4a7e67a..08ee4aa 100644 (file)
@@ -3,38 +3,53 @@ git-rebase(1)
 
 NAME
 ----
-git-rebase - Rebase local commits to new upstream head
+git-rebase - Rebase local commits to a new head
 
 SYNOPSIS
 --------
 'git-rebase' [--onto <newbase>] <upstream> [<branch>]
 
+'git-rebase' --continue | --skip | --abort
+
 DESCRIPTION
 -----------
-git-rebase applies to <upstream> (or optionally to <newbase>) commits
-from <branch> that do not appear in <upstream>. When <branch> is not
-specified it defaults to the current branch (HEAD).
-
-When git-rebase is complete, <branch> will be updated to point to the
-newly created line of commit objects, so the previous line will not be
-accessible unless there are other references to it already.
+git-rebase replaces <branch> with a new branch of the same name.  When
+the --onto option is provided the new branch starts out with a HEAD equal
+to <newbase>, otherwise it is equal to <upstream>.  It then attempts to
+create a new commit for each commit from the original <branch> that does
+not exist in the <upstream> branch.
+
+It is possible that a merge failure will prevent this process from being
+completely automatic.  You will have to resolve any such merge failure
+and run `git rebase --continue`.  Another option is to bypass the commit
+that caused the merge failure with `git rebase --skip`.  To restore the
+original <branch> and remove the .dotest working files, use the command
+`git rebase --abort` instead.
+
+Note that if <branch> is not specified on the command line, the currently
+checked out branch is used.
 
 Assume the following history exists and the current branch is "topic":
 
+------------
           A---B---C topic
          /
     D---E---F---G master
+------------
 
 From this point, the result of either of the following commands:
 
+
     git-rebase master
     git-rebase master topic
 
 would be:
 
+------------
                   A'--B'--C' topic
                  /
     D---E---F---G master
+------------
 
 While, starting from the same point, the result of either of the following
 commands:
@@ -44,21 +59,33 @@ commands:
 
 would be:
 
+------------
               A'--B'--C' topic
              /
     D---E---F---G master
+------------
 
 In case of conflict, git-rebase will stop at the first problematic commit
-and leave conflict markers in the tree.  After resolving the conflict manually
-and updating the index with the desired resolution, you can continue the
-rebasing process with
+and leave conflict markers in the tree.  You can use git diff to locate
+the markers (<<<<<<) and make edits to resolve the conflict.  For each
+file you edit, you need to tell git that the conflict has been resolved,
+typically this would be done with
+
+
+    git update-index <filename>
+
+
+After resolving the conflict manually and updating the index with the
+desired resolution, you can continue the rebasing process with
+
+
+    git rebase --continue
 
-    git am --resolved --3way
 
 Alternatively, you can undo the git-rebase with
 
-    git reset --hard ORIG_HEAD
-    rm -r .dotest
+
+    git rebase --abort
 
 OPTIONS
 -------
@@ -73,6 +100,28 @@ OPTIONS
 <branch>::
        Working branch; defaults to HEAD.
 
+--continue::
+       Restart the rebasing process after having resolved a merge conflict.
+
+--abort::
+       Restore the original branch and abort the rebase operation.
+
+NOTES
+-----
+When you rebase a branch, you are changing its history in a way that
+will cause problems for anyone who already has a copy of the branch
+in their repository and tries to pull updates from you.  You should
+understand the implications of using 'git rebase' on a repository that
+you share.
+
+When the git rebase command is run, it will first execute a "pre-rebase"
+hook if one exists.  You can use this hook to do sanity checks and
+reject the rebase if it isn't appropriate.  Please see the template
+pre-rebase hook script for an example.
+
+You must be in the top directory of your project to start (or continue)
+a rebase.  Upon completion, <branch> will be the current branch.
+
 Author
 ------
 Written by Junio C Hamano <junkio@cox.net>
index d2f9a44..9516227 100644 (file)
@@ -38,6 +38,7 @@ OPTIONS
 -d::
        After packing, if the newly created packs make some
        existing packs redundant, remove the redundant packs.
+       Also runs gitlink:git-prune-packed[1].
 
 -l::
         Pass the `--local` option to `git pack-objects`, see
index 71f96bd..d5142e0 100644 (file)
@@ -15,6 +15,7 @@ SYNOPSIS
 'git-repo-config' [type] --get-all name [value_regex]
 'git-repo-config' [type] --unset name [value_regex]
 'git-repo-config' [type] --unset-all name [value_regex]
+'git-repo-config' -l | --list
 
 DESCRIPTION
 -----------
@@ -22,10 +23,11 @@ You can query/set/replace/unset options with this command. The name is
 actually the section and the key separated by a dot, and the value will be
 escaped.
 
-If you want to set/unset an option which can occur on multiple lines, you
-should provide a POSIX regex for the value. If you want to handle the lines
-*not* matching the regex, just prepend a single exclamation mark in front
-(see EXAMPLES).
+If you want to set/unset an option which can occur on multiple
+lines, a POSIX regexp `value_regex` needs to be given.  Only the
+existing values that match the regexp are updated or unset.  If
+you want to handle the lines that do *not* match the regex, just
+prepend a single exclamation mark in front (see EXAMPLES).
 
 The type specifier can be either '--int' or '--bool', which will make
 'git-repo-config' ensure that the variable(s) are of the given type and
@@ -33,10 +35,10 @@ convert the value to the canonical form (simple decimal number for int,
 a "true" or "false" string for bool). If no type specifier is passed,
 no checks or transformations are performed on the value.
 
-This command will fail if
+This command will fail if:
 
-. .git/config is invalid,
-. .git/config can not be written to,
+. The .git/config file is invalid,
+. Can not write to .git/config,
 . no section was provided,
 . the section or key is invalid,
 . you try to unset an option which does not exist, or
@@ -47,8 +49,8 @@ OPTIONS
 -------
 
 --replace-all::
-       Default behaviour is to replace at most one line. This replaces
-       all lines matching the key (and optionally the value_regex)
+       Default behavior is to replace at most one line. This replaces
+       all lines matching the key (and optionally the value_regex).
 
 --get::
        Get the value for a given key (optionally filtered by a regex
@@ -58,12 +60,18 @@ OPTIONS
        Like get, but does not fail if the number of values for the key
        is not exactly one.
 
+--get-regexp::
+       Like --get-all, but interprets the name as a regular expression.
+
 --unset::
        Remove the line matching the key from .git/config.
 
 --unset-all::
        Remove all matching lines from .git/config.
 
+-l, --list::
+       List all variables set in .git/config.
+
 
 EXAMPLE
 -------
index b7b9798..73a0ffc 100644 (file)
@@ -24,7 +24,7 @@ gitlink:git-revert[1] is your friend.
 OPTIONS
 -------
 --mixed::
-       Resets the index but not the working tree (ie, the changed files
+       Resets the index but not the working tree (i.e., the changed files
        are preserved but not marked for commit) and reports what has not
        been updated. This is the default action.
 
@@ -43,16 +43,17 @@ OPTIONS
        Commit to make the current HEAD.
 
 Examples
-~~~~~~~~
+--------
 
 Undo a commit and redo::
 +
 ------------
 $ git commit ...
-$ git reset --soft HEAD^ <1>
-$ edit <2>
-$ git commit -a -c ORIG_HEAD <3>
-
+$ git reset --soft HEAD^      <1>
+$ edit                        <2>
+$ git commit -a -c ORIG_HEAD  <3>
+------------
++
 <1> This is most often done when you remembered what you
 just committed is incomplete, or you misspelled your commit
 message, or both.  Leaves working tree as it was before "reset".
@@ -60,43 +61,43 @@ message, or both.  Leaves working tree as it was before "reset".
 <3> "reset" copies the old head to .git/ORIG_HEAD; redo the
 commit by starting with its log message.  If you do not need to
 edit the message further, you can give -C option instead.
-------------
 
 Undo commits permanently::
 +
 ------------
 $ git commit ...
-$ git reset --hard HEAD~3 <1>
-
+$ git reset --hard HEAD~3   <1>
+------------
++
 <1> The last three commits (HEAD, HEAD^, and HEAD~2) were bad
 and you do not want to ever see them again.  Do *not* do this if
 you have already given these commits to somebody else.
-------------
 
 Undo a commit, making it a topic branch::
 +
 ------------
-$ git branch topic/wip <1>
-$ git reset --hard HEAD~3 <2>
-$ git checkout topic/wip <3>
-
+$ git branch topic/wip     <1>
+$ git reset --hard HEAD~3  <2>
+$ git checkout topic/wip   <3>
+------------
++
 <1> You have made some commits, but realize they were premature
 to be in the "master" branch.  You want to continue polishing
 them in a topic branch, so create "topic/wip" branch off of the
 current HEAD.
 <2> Rewind the master branch to get rid of those three commits.
 <3> Switch to "topic/wip" branch and keep working.
-------------
 
 Undo update-index::
 +
 ------------
-$ edit <1>
+$ edit                                     <1>
 $ git-update-index frotz.c filfre.c
-$ mailx <2>
-$ git reset <3>
-$ git pull git://info.example.com/ nitfol <4>
-
+$ mailx                                    <2>
+$ git reset                                <3>
+$ git pull git://info.example.com/ nitfol  <4>
+------------
++
 <1> you are happily working on something, and find the changes
 in these files are in good order.  You do not want to see them
 when you run "git diff", because you plan to work on other files
@@ -109,12 +110,11 @@ index changes for these two files.  Your changes in working tree
 remain there.
 <4> then you can pull and merge, leaving frotz.c and filfre.c
 changes still in the working tree.
-------------
 
 Undo a merge or pull::
 +
 ------------
-$ git pull <1>
+$ git pull                         <1>
 Trying really trivial in-index merge...
 fatal: Merge requires file-level merging
 Nope.
@@ -122,20 +122,19 @@ Nope.
 Auto-merging nitfol
 CONFLICT (content): Merge conflict in nitfol
 Automatic merge failed/prevented; fix up by hand
-$ git reset --hard <2>
-
+$ git reset --hard                 <2>
+$ git pull . topic/branch          <3>
+Updating from 41223... to 13134...
+Fast forward
+$ git reset --hard ORIG_HEAD       <4>
+------------
++
 <1> try to update from the upstream resulted in a lot of
 conflicts; you were not ready to spend a lot of time merging
 right now, so you decide to do that later.
 <2> "pull" has not made merge commit, so "git reset --hard"
 which is a synonym for "git reset --hard HEAD" clears the mess
 from the index file and the working tree.
-
-$ git pull . topic/branch <3>
-Updating from 41223... to 13134...
-Fast forward
-$ git reset --hard ORIG_HEAD <4>
-
 <3> merge a topic branch into the current branch, which resulted
 in a fast forward.
 <4> but you decided that the topic branch is not ready for public
@@ -143,7 +142,6 @@ consumption yet.  "pull" or "merge" always leaves the original
 tip of the current branch in ORIG_HEAD, so resetting hard to it
 brings your index file and the working tree back to that state,
 and resets the tip of the branch to that commit.
-------------
 
 Interrupted workflow::
 +
@@ -155,21 +153,21 @@ need to get to the other branch for a quick bugfix.
 ------------
 $ git checkout feature ;# you were working in "feature" branch and
 $ work work work       ;# got interrupted
-$ git commit -a -m 'snapshot WIP' <1>
+$ git commit -a -m 'snapshot WIP'                 <1>
 $ git checkout master
 $ fix fix fix
 $ git commit ;# commit with real log
 $ git checkout feature
-$ git reset --soft HEAD^ ;# go back to WIP state <2>
-$ git reset <3>
-
+$ git reset --soft HEAD^ ;# go back to WIP state  <2>
+$ git reset                                       <3>
+------------
++
 <1> This commit will get blown away so a throw-away log message is OK.
 <2> This removes the 'WIP' commit from the commit history, and sets
     your working tree to the state just before you made that snapshot.
-<3> After <2>, the index file still has all the WIP changes you
-    committed in <1>.  This sets it to the last commit you were
-    basing the WIP changes on.
-------------
+<3> At this point the index file still has all the WIP changes you
+    committed as 'snapshot WIP'.  This updates the index to show your
+    WIP files as uncommitted.
 
 Author
 ------
index 8255ae1..ad6d14c 100644 (file)
@@ -68,9 +68,10 @@ OPTIONS
 --bisect::
        Limit output to the one commit object which is roughly halfway
        between the included and excluded commits. Thus, if 'git-rev-list
-       --bisect foo ^bar ^baz' outputs 'midpoint', the output
-       of 'git-rev-list foo ^midpoint' and 'git-rev-list midpoint
-       ^bar ^baz' would be of roughly the same length. Finding the change
+       --bisect foo {caret}bar {caret}baz' outputs 'midpoint', the output
+       of 'git-rev-list foo {caret}midpoint' and 'git-rev-list midpoint
+       {caret}bar {caret}baz' would be of roughly the same length.
+       Finding the change
        which introduces a regression is thus reduced to a binary search:
        repeatedly generate and test new 'midpoint's until the commit chain
        is of length one.
index 8b95df0..627cde8 100644 (file)
@@ -13,7 +13,7 @@ SYNOPSIS
 DESCRIPTION
 -----------
 
-Many git Porcelainish commands take mixture of flags
+Many git porcelainish commands take mixture of flags
 (i.e. parameters that begin with a dash '-') and parameters
 meant for underlying `git-rev-list` command they use internally
 and flags and parameters for other commands they use as the
@@ -67,6 +67,15 @@ OPTIONS
 --all::
        Show all refs found in `$GIT_DIR/refs`.
 
+--branches::
+       Show branch refs found in `$GIT_DIR/refs/heads`.
+
+--tags::
+       Show tag refs found in `$GIT_DIR/refs/tags`.
+
+--remotes::
+       Show tag refs found in `$GIT_DIR/refs/remotes`.
+
 --show-prefix::
        When the command is invoked from a subdirectory, show the
        path of the current directory relative to the top-level
@@ -82,7 +91,7 @@ OPTIONS
 
 --short, --short=number::
        Instead of outputting the full SHA1 values of object names try to
-       abbriviate them to a shorter unique name. When no length is specified
+       abbreviate them to a shorter unique name. When no length is specified
        7 is used. The minimum length is 4.
 
 --since=datestring, --after=datestring::
@@ -115,6 +124,13 @@ syntax.
   happen to have both heads/master and tags/master, you can
   explicitly say 'heads/master' to tell git which one you mean.
 
+* A suffix '@' followed by a date specification enclosed in a brace
+  pair (e.g. '\{yesterday\}', '\{1 month 2 weeks 3 days 1 hour 1
+  second ago\}' or '\{1979-02-26 18:30:00\}') to specify the value
+  of the ref at a prior point in time.  This suffix may only be
+  used immediately following a ref name and the ref must have an
+  existing log ($GIT_DIR/logs/<ref>).
+
 * A suffix '{caret}' to a revision parameter means the first parent of
   that commit object.  '{caret}<n>' means the <n>th parent (i.e.
   'rev{caret}'
index c9c3088..66fc478 100644 (file)
@@ -32,7 +32,7 @@ OPTIONS
 -v::
         Be verbose.
 
---::
+\--::
        This option can be used to separate command-line options from
        the list of files, (useful when filenames might be mistaken
        for command-line options).
index 8c58685..ad1b9cf 100644 (file)
@@ -52,7 +52,7 @@ The options available are:
        is not set, this will be prompted for.
 
 --no-signed-off-by-cc::
-       Do not add emails foudn in Signed-off-by: lines to the cc list.
+       Do not add emails found in Signed-off-by: lines to the cc list.
 
 --quiet::
        Make git-send-email less verbose.  One line per email should be
index 08e0705..9e67f17 100644 (file)
@@ -53,7 +53,7 @@ Specifying the Refs
 There are three ways to specify which refs to update on the
 remote end.
 
-With '--all' flag, all refs that exist locally are transfered to
+With '--all' flag, all refs that exist locally are transferred to
 the remote side.  You cannot specify any '<ref>' if you use
 this flag.
 
index 6742c9b..79217d8 100644 (file)
@@ -13,7 +13,7 @@ DESCRIPTION
 -----------
 
 Sets up the normal git environment variables and a few helper functions
-(currently just "die()"), and returns ok if it all looks like a git archive.
+(currently just "die()"), and returns OK if it all looks like a git archive.
 So, to make the rest of the git scripts more careful and readable,
 use it as follows:
 
index 54fb922..7486ebe 100644 (file)
@@ -5,7 +5,6 @@ NAME
 ----
 git-shortlog - Summarize 'git log' output
 
-
 SYNOPSIS
 --------
 git-log --pretty=short | 'git-shortlog'
@@ -13,8 +12,22 @@ git-log --pretty=short | 'git-shortlog'
 DESCRIPTION
 -----------
 Summarizes 'git log' output in a format suitable for inclusion
-in release announcements.
-
+in release announcements. Each commit will be grouped by author
+the first line of the commit message will be shown.
+
+Additionally, "[PATCH]" will be stripped from the commit description.
+
+FILES
+-----
+'.mailmap'::
+       If this file exists, it will be used for mapping author email
+       addresses to a real author name. One mapping per line, first
+       the author name followed by the email address enclosed by
+       '<' and '>'. Use hash '#' for comments. Example:
+
+               # Keep alphabetized
+               Adam Morrow <adam@localhost.localdomain>
+               Eve Jones <eve@laptop.(none)>
 
 Author
 ------
index 2139b6f..831537b 100644 (file)
@@ -8,7 +8,7 @@ git-tar-tree - Creates a tar archive of the files in the named tree
 
 SYNOPSIS
 --------
-'git-tar-tree' <tree-ish> [ <base> ]
+'git-tar-tree' [--remote=<repo>] <tree-ish> [ <base> ]
 
 DESCRIPTION
 -----------
@@ -23,6 +23,35 @@ commit time as recorded in the referenced commit object is used instead.
 Additionally the commit ID is stored in a global extended pax header.
 It can be extracted using git-get-tar-commit-id.
 
+OPTIONS
+-------
+
+<tree-ish>::
+       The tree or commit to produce tar archive for.  If it is
+       the object name of a commit object.
+
+<base>::
+       Leading path to the files in the resulting tar archive.
+
+--remote=<repo>::
+       Instead of making a tar archive from local repository,
+       retrieve a tar archive from a remote repository.
+
+Examples
+--------
+git tar-tree HEAD | (cd /var/tmp/ && mkdir junk && tar Cxf junk -)::
+
+       Create a tar archive that contains the contents of the
+       latest commit on the current branch, and extracts it in
+       `/var/tmp/junk` directory.
+
+git tar-tree v2.6.17 linux-2.6.17 | gzip >linux-2.6.17.tar.gz
+
+       Create a tarball for v2.6.17 release.
+
+git tar-tree --remote=example.com:git.git v0.99 >git-0.99.tar
+
+       Get a tarball v0.99 from example.com.
 
 Author
 ------
index 00e57a6..d79523f 100644 (file)
@@ -12,7 +12,7 @@ This document presents a brief summary of each tool and the corresponding
 link.
 
 
-Alternative/Augmentative Procelains
+Alternative/Augmentative Porcelains
 -----------------------------------
 
    - *Cogito* (http://www.kernel.org/pub/software/scm/cogito/)
index 1828062..c20b38b 100644 (file)
@@ -13,9 +13,16 @@ SYNOPSIS
 
 DESCRIPTION
 -----------
-Reads a packed archive (.pack) from the standard input, and
-expands the objects contained in the pack into "one-file
-one-object" format in $GIT_OBJECT_DIRECTORY.
+Read a packed archive (.pack) from the standard input, expanding
+the objects contained within and writing them into the repository in
+"loose" (one object per file) format.
+
+Objects that already exist in the repository will *not* be unpacked
+from the pack-file.  Therefore, nothing will be unpacked if you use
+this command on a pack-file that exists within the target repository.
+
+Please see the `git-repack` documentation for options to generate
+new packs and replace existing ones.
 
 OPTIONS
 -------
index 0a1b0ad..3ae6e74 100644 (file)
@@ -10,12 +10,12 @@ SYNOPSIS
 --------
 [verse]
 'git-update-index'
-            [--add] [--remove | --force-remove] [--replace] 
-            [--refresh [-q] [--unmerged] [--ignore-missing]]
+            [--add] [--remove | --force-remove] [--replace]
+            [--refresh] [-q] [--unmerged] [--ignore-missing]
             [--cacheinfo <mode> <object> <file>]\*
             [--chmod=(+|-)x]
             [--assume-unchanged | --no-assume-unchanged]
-            [--really-refresh]
+            [--really-refresh] [--unresolve] [--again]
             [--info-only] [--index-info]
             [-z] [--stdin]
             [--verbose]
@@ -40,7 +40,7 @@ OPTIONS
 --remove::
        If a specified file is in the index but is missing then it's
        removed.
-       Default behaviour is to ignore removed file.
+       Default behavior is to ignore removed file.
 
 --refresh::
        Looks at the current index and checks to see if merges or
@@ -80,6 +80,14 @@ OPTIONS
        filesystem that has very slow lstat(2) system call
        (e.g. cifs).
 
+--again::
+       Runs `git-update-index` itself on the paths whose index
+       entries are different from those from the `HEAD` commit.
+
+--unresolve::
+       Restores the 'unmerged' or 'needs updating' state of a
+       file during a merge if it was cleared by accident.
+
 --info-only::
        Do not create objects in the object database for all
        <file> arguments that follow this flag; just insert
@@ -109,7 +117,7 @@ OPTIONS
        Only meaningful with `--stdin`; paths are separated with
        NUL character instead of LF.
 
---::
+\--::
        Do not interpret any more arguments as options.
 
 <file>::
@@ -247,34 +255,33 @@ To update and refresh only the files already checked out:
 $ git-checkout-index -n -f -a && git-update-index --ignore-missing --refresh
 ----------------
 
-On an inefficient filesystem with `core.ignorestat` set:
-
+On an inefficient filesystem with `core.ignorestat` set::
++
 ------------
-$ git update-index --really-refresh <1>
-$ git update-index --no-assume-unchanged foo.c <2>
-$ git diff --name-only <3>
+$ git update-index --really-refresh              <1>
+$ git update-index --no-assume-unchanged foo.c   <2>
+$ git diff --name-only                           <3>
 $ edit foo.c
-$ git diff --name-only <4>
+$ git diff --name-only                           <4>
 M foo.c
-$ git update-index foo.c <5>
-$ git diff --name-only <6>
+$ git update-index foo.c                         <5>
+$ git diff --name-only                           <6>
 $ edit foo.c
-$ git diff --name-only <7>
-$ git update-index --no-assume-unchanged foo.c <8>
-$ git diff --name-only <9>
+$ git diff --name-only                           <7>
+$ git update-index --no-assume-unchanged foo.c   <8>
+$ git diff --name-only                           <9>
 M foo.c
-
-<1> forces lstat(2) to set "assume unchanged" bits for paths
-    that match index.
+------------
++
+<1> forces lstat(2) to set "assume unchanged" bits for paths that match index.
 <2> mark the path to be edited.
 <3> this does lstat(2) and finds index matches the path.
-<4> this does lstat(2) and finds index does not match the path.
+<4> this does lstat(2) and finds index does *not* match the path.
 <5> registering the new version to index sets "assume unchanged" bit.
 <6> and it is assumed unchanged.
 <7> even after you edit it.
 <8> you can tell about the change after the fact.
 <9> now it checks with lstat(2) and finds it has been changed.
-------------
 
 
 Configuration
index 475237f..e062030 100644 (file)
@@ -7,7 +7,7 @@ git-update-ref - update the object name stored in a ref safely
 
 SYNOPSIS
 --------
-'git-update-ref' <ref> <newvalue> [<oldvalue>]
+'git-update-ref' [-m <reason>] <ref> <newvalue> [<oldvalue>]
 
 DESCRIPTION
 -----------
@@ -49,6 +49,32 @@ for reading but not for writing (so we'll never write through a
 ref symlink to some other tree, if you have copied a whole
 archive by creating a symlink tree).
 
+Logging Updates
+---------------
+If config parameter "core.logAllRefUpdates" is true or the file
+"$GIT_DIR/logs/<ref>" exists then `git-update-ref` will append
+a line to the log file "$GIT_DIR/logs/<ref>" (dereferencing all
+symbolic refs before creating the log name) describing the change
+in ref value.  Log lines are formatted as:
+
+    . oldsha1 SP newsha1 SP committer LF
++
+Where "oldsha1" is the 40 character hexadecimal value previously
+stored in <ref>, "newsha1" is the 40 character hexadecimal value of
+<newvalue> and "committer" is the committer's name, email address
+and date in the standard GIT committer ident format.
+
+Optionally with -m:
+
+    . oldsha1 SP newsha1 SP committer TAB message LF
++
+Where all fields are as described above and "message" is the
+value supplied to the -m option.
+
+An update will fail (without changing <ref>) if the current user is
+unable to create a new log file, append to the existing log file
+or does not have committer information available.
+
 Author
 ------
 Written by Linus Torvalds <torvalds@osdl.org>.
diff --git a/Documentation/git-upload-tar.txt b/Documentation/git-upload-tar.txt
new file mode 100644 (file)
index 0000000..a1019a0
--- /dev/null
@@ -0,0 +1,39 @@
+git-upload-tar(1)
+=================
+
+NAME
+----
+git-upload-tar - Send tar archive
+
+
+SYNOPSIS
+--------
+'git-upload-tar' <directory>
+
+DESCRIPTION
+-----------
+Invoked by 'git-tar-tree --remote' and sends a generated tar archive
+to the other end over the git protocol.
+
+This command is usually not invoked directly by the end user.
+The UI for the protocol is on the 'git-tar-tree' side, and the
+program pair is meant to be used to get a tar achive from a
+remote repository.
+
+
+OPTIONS
+-------
+<directory>::
+       The repository to get a tar archive from.
+
+Author
+------
+Written by Junio C Hamano <junio@kernel.org>
+
+Documentation
+--------------
+Documentation by Junio C Hamano.
+
+GIT
+---
+Part of the gitlink:git[7] suite
index 379571e..a5b1a0d 100644 (file)
@@ -19,7 +19,8 @@ OPTIONS
 -l::
        Cause the logical variables to be listed. In addition, all the
        variables of the git configuration file .git/config are listed
-       as well.
+       as well. (However, the configuration variables listing functionality
+       is deprecated in favor of `git-repo-config -l`.)
 
 EXAMPLE
 --------
index 4962d69..7a6132b 100644 (file)
@@ -25,7 +25,7 @@ OPTIONS
 -v::
        After verifying the pack, show list of objects contained
        in the pack.
---::
+\--::
        Do not interpret any more arguments as options.
 
 OUTPUT FORMAT
index 641cb7e..e8f21d0 100644 (file)
@@ -58,7 +58,7 @@ git-whatchanged -p v2.6.12.. include/scsi drivers/scsi::
        Show as patches the commits since version 'v2.6.12' that changed
        any file in the include/scsi or drivers/scsi subdirectories
 
-git-whatchanged --since="2 weeks ago" -- gitk::
+git-whatchanged --since="2 weeks ago" \-- gitk::
 
        Show the changes during the last two weeks to the file 'gitk'.
        The "--" is necessary to avoid confusion with the *branch* named
index 24ca55d..d4472b5 100644 (file)
@@ -21,6 +21,9 @@ link:everyday.html[Everyday Git] for a useful minimum set of commands, and
 "man git-commandname" for documentation of each command.  CVS users may
 also want to read link:cvs-migration.html[CVS migration].
 
+The COMMAND is either a name of a Git command (see below) or an alias
+as defined in the configuration file (see gitlink:git-repo-config[1]).
+
 OPTIONS
 -------
 --version::
@@ -237,6 +240,10 @@ gitlink:git-upload-pack[1]::
        Invoked by 'git-clone-pack' and 'git-fetch-pack' to push
        what are asked for.
 
+gitlink:git-upload-tar[1]::
+       Invoked by 'git-tar-tree --remote' to return the tar
+       archive the other end asked for.
+
 
 High-level commands (porcelain)
 -------------------------------
@@ -378,6 +385,9 @@ gitlink:git-merge-one-file[1]::
 gitlink:git-prune[1]::
        Prunes all unreachable objects from the object database.
 
+gitlink:git-quiltimport[1]::
+       Applies a quilt patchset onto the current branch.
+
 gitlink:git-relink[1]::
        Hardlink common objects in local repositories.
 
index eb126d7..cb482bf 100644 (file)
@@ -31,7 +31,7 @@ gitk v2.6.12.. include/scsi drivers/scsi::
        Show as the changes since version 'v2.6.12' that changed any
        file in the include/scsi or drivers/scsi subdirectories
 
-gitk --since="2 weeks ago" -- gitk::
+gitk --since="2 weeks ago" \-- gitk::
 
        Show the changes during the last two weeks to the file 'gitk'.
        The "--" is necessary to avoid confusion with the *branch* named
index 02a9d9c..116ddb7 100644 (file)
@@ -1,79 +1,57 @@
-object::
-       The unit of storage in git. It is uniquely identified by
-       the SHA1 of its contents. Consequently, an object can not
-       be changed.
-
-object name::
-       The unique identifier of an object. The hash of the object's contents
-       using the Secure Hash Algorithm 1 and usually represented by the 40
-       character hexadecimal encoding of the hash of the object (possibly
-       followed by a white space).
-
-SHA1::
-       Synonym for object name.
-
-object identifier::
-       Synonym for object name.
-
-hash::
-       In git's context, synonym to object name.
+alternate object database::
+       Via the alternates mechanism, a repository can inherit part of its
+       object database from another object database, which is called
+       "alternate".
 
-object database::
-       Stores a set of "objects", and an individual object is identified
-       by its object name. The objects usually live in `$GIT_DIR/objects/`.
+bare repository::
+       A bare repository is normally an appropriately named
+       directory with a `.git` suffix that does not have a
+       locally checked-out copy of any of the files under revision
+       control.  That is, all of the `git` administrative and
+       control files that would normally be present in the
+       hidden `.git` sub-directory are directly present in
+       the `repository.git` directory instead, and no other files
+       are present and checked out.  Usually publishers of public
+       repositories make bare repositories available.
 
 blob object::
        Untyped object, e.g. the contents of a file.
 
-tree object::
-       An object containing a list of file names and modes along with refs
-       to the associated blob and/or tree objects. A tree is equivalent
-       to a directory.
-
-tree::
-       Either a working tree, or a tree object together with the
-       dependent blob and tree objects (i.e. a stored representation
-       of a working tree).
-
-DAG::
-       Directed acyclic graph. The commit objects form a directed acyclic
-       graph, because they have parents (directed), and the graph of commit
-       objects is acyclic (there is no chain which begins and ends with the
-       same object).
-
-index::
-       A collection of files with stat information, whose contents are
-       stored as objects. The index is a stored version of your working
-       tree. Truth be told, it can also contain a second, and even a third
-       version of a working tree, which are used when merging.
-
-index entry::
-       The information regarding a particular file, stored in the index.
-       An index entry can be unmerged, if a merge was started, but not
-       yet finished (i.e. if the index contains multiple versions of
-       that file).
-
-unmerged index:
-       An index which contains unmerged index entries.
+branch::
+       A non-cyclical graph of revisions, i.e. the complete history of
+       a particular revision, which is called the branch head. The
+       branch heads are stored in `$GIT_DIR/refs/heads/`.
 
 cache::
        Obsolete for: index.
 
-working tree::
-       The set of files and directories currently being worked on,
-       i.e. you can work in your working tree without using git at all.
-
-directory::
-       The list you get with "ls" :-)
+chain::
+       A list of objects, where each object in the list contains a
+       reference to its successor (for example, the successor of a commit
+       could be one of its parents).
 
-revision::
-       A particular state of files and directories which was stored in
-       the object database. It is referenced by a commit object.
+changeset::
+       BitKeeper/cvsps speak for "commit". Since git does not store
+       changes, but states, it really does not make sense to use
+       the term "changesets" with git.
 
 checkout::
        The action of updating the working tree to a revision which was
        stored in the object database.
 
+cherry-picking::
+       In SCM jargon, "cherry pick" means to choose a subset of
+       changes out of a series of changes (typically commits)
+       and record them as a new series of changes on top of
+       different codebase.  In GIT, this is performed by
+       "git cherry-pick" command to extract the change
+       introduced by an existing commit and to record it based
+       on the tip of the current branch as a new commit.
+
+clean::
+       A working tree is clean, if it corresponds to the revision
+       referenced by the current head.  Also see "dirty".
+
 commit::
        As a verb: The action of storing the current state of the index in the
        object database. The result is a revision.
@@ -85,73 +63,97 @@ commit object::
        tree object which corresponds to the top directory of the
        stored revision.
 
-parent::
-       A commit object contains a (possibly empty) list of the logical
-       predecessor(s) in the line of development, i.e. its parents.
+core git::
+       Fundamental data structures and utilities of git. Exposes only
+       limited source code management tools.
 
-changeset::
-       BitKeeper/cvsps speak for "commit". Since git does not store
-       changes, but states, it really does not make sense to use
-       the term "changesets" with git.
+DAG::
+       Directed acyclic graph. The commit objects form a directed acyclic
+       graph, because they have parents (directed), and the graph of commit
+       objects is acyclic (there is no chain which begins and ends with the
+       same object).
 
-clean::
-       A working tree is clean, if it corresponds to the revision
-       referenced by the current head.
+dircache::
+       You are *waaaaay* behind.
 
 dirty::
        A working tree is said to be dirty if it contains modifications
        which have not been committed to the current branch.
 
-head::
-       The top of a branch. It contains a ref to the corresponding
-       commit object.
+directory::
+       The list you get with "ls" :-)
 
-branch::
-       A non-cyclical graph of revisions, i.e. the complete history of
-       a particular revision, which is called the branch head. The
-       branch heads are stored in `$GIT_DIR/refs/heads/`.
+ent::
+       Favorite synonym to "tree-ish" by some total geeks. See
+       `http://en.wikipedia.org/wiki/Ent_(Middle-earth)` for an in-depth
+       explanation.
 
-master::
-       The default branch. Whenever you create a git repository, a branch
-       named "master" is created, and becomes the active branch. In most
-       cases, this contains the local development.
+fast forward::
+       A fast-forward is a special type of merge where you have
+       a revision and you are "merging" another branch's changes
+       that happen to be a descendant of what you have.
+       In such these cases, you do not make a new merge commit but
+       instead just update to his revision. This will happen
+       frequently on a tracking branch of a remote repository.
 
-origin::
-       The default upstream branch. Most projects have one upstream
-       project which they track, and by default 'origin' is used for
-       that purpose.  New updates from upstream will be fetched into
-       this branch; you should never commit to it yourself.
+fetch::
+       Fetching a branch means to get the branch's head ref from a
+       remote repository, to find out which objects are missing from
+       the local object database, and to get them, too.
 
-ref::
-       A 40-byte hex representation of a SHA1 pointing to a particular
-       object. These may be stored in `$GIT_DIR/refs/`.
+file system::
+       Linus Torvalds originally designed git to be a user space file
+       system, i.e. the infrastructure to hold files and directories.
+       That ensured the efficiency and speed of git.
+
+git archive::
+       Synonym for repository (for arch people).
+
+grafts::
+       Grafts enables two otherwise different lines of development to be
+       joined together by recording fake ancestry information for commits.
+       This way you can make git pretend the set of parents a commit
+       has is different from what was recorded when the commit was created.
+       Configured via the `.git/info/grafts` file.
+
+hash::
+       In git's context, synonym to object name.
+
+head::
+       The top of a branch. It contains a ref to the corresponding
+       commit object.
 
 head ref::
        A ref pointing to a head. Often, this is abbreviated to "head".
        Head refs are stored in `$GIT_DIR/refs/heads/`.
 
-tree-ish::
-       A ref pointing to either a commit object, a tree object, or a
-       tag object pointing to a tag or commit or tree object.
+hook::
+       During the normal execution of several git commands,
+       call-outs are made to optional scripts that allow
+       a developer to add functionality or checking.
+       Typically, the hooks allow for a command to be pre-verified
+       and potentially aborted, and allow for a post-notification
+       after the operation is done.
+       The hook scripts are found in the `$GIT_DIR/hooks/` directory,
+       and are enabled by simply making them executable.
 
-ent::
-       Favorite synonym to "tree-ish" by some total geeks. See
-       `http://en.wikipedia.org/wiki/Ent_(Middle-earth)` for an in-depth
-       explanation.
+index::
+       A collection of files with stat information, whose contents are
+       stored as objects. The index is a stored version of your working
+       tree. Truth be told, it can also contain a second, and even a third
+       version of a working tree, which are used when merging.
 
-tag object::
-       An object containing a ref pointing to another object, which can
-       contain a message just like a commit object. It can also
-       contain a (PGP) signature, in which case it is called a "signed
-       tag object".
+index entry::
+       The information regarding a particular file, stored in the index.
+       An index entry can be unmerged, if a merge was started, but not
+       yet finished (i.e. if the index contains multiple versions of
+       that file).
 
-tag::
-       A ref pointing to a tag or commit object. In contrast to a head,
-       a tag is not changed by a commit. Tags (not tag objects) are
-       stored in `$GIT_DIR/refs/tags/`. A git tag has nothing to do with
-       a Lisp tag (which is called object type in git's context).
-       A tag is most typically used to mark a particular point in the
-       commit ancestry chain.
+master::
+       The default development branch. Whenever you create a git
+       repository, a branch named "master" is created, and becomes
+       the active branch. In most cases, this contains the local
+       development, though that is purely conventional and not required.
 
 merge::
        To merge branches means to try to accumulate the changes since a
@@ -159,55 +161,65 @@ merge::
        merge uses heuristics to accomplish that. Evidently, an automatic
        merge can fail.
 
-octopus::
-       To merge more than two branches. Also denotes an intelligent
-       predator.
+object::
+       The unit of storage in git. It is uniquely identified by
+       the SHA1 of its contents. Consequently, an object can not
+       be changed.
 
-resolve::
-       The action of fixing up manually what a failed automatic merge
-       left behind.
+object database::
+       Stores a set of "objects", and an individual object is identified
+       by its object name. The objects usually live in `$GIT_DIR/objects/`.
 
-rewind::
-       To throw away part of the development, i.e. to assign the head to
-       an earlier revision.
+object identifier::
+       Synonym for object name.
 
-rebase::
-       To clean a branch by starting from the head of the main line of
-       development ("master"), and reapply the (possibly cherry-picked)
-       changes from that branch.
+object name::
+       The unique identifier of an object. The hash of the object's contents
+       using the Secure Hash Algorithm 1 and usually represented by the 40
+       character hexadecimal encoding of the hash of the object (possibly
+       followed by a white space).
 
-repository::
-       A collection of refs together with an object database containing
-       all objects, which are reachable from the refs, possibly accompanied
-       by meta data from one or more porcelains. A repository can
-       share an object database with other repositories.
+object type:
+       One of the identifiers "commit","tree","tag" and "blob" describing
+       the type of an object.
 
-git archive::
-       Synonym for repository (for arch people).
+octopus::
+       To merge more than two branches. Also denotes an intelligent
+       predator.
 
-file system::
-       Linus Torvalds originally designed git to be a user space file
-       system, i.e. the infrastructure to hold files and directories.
-       That ensured the efficiency and speed of git.
+origin::
+       The default upstream tracking branch. Most projects have at
+       least one upstream project which they track. By default
+       'origin' is used for that purpose.  New upstream updates
+       will be fetched into this branch; you should never commit
+       to it yourself.
 
-alternate object database::
-       Via the alternates mechanism, a repository can inherit part of its
-       object database from another object database, which is called
-       "alternate".
+pack::
+       A set of objects which have been compressed into one file (to save
+       space or to transmit them efficiently).
 
-reachable::
-       An object is reachable from a ref/commit/tree/tag, if there is a
-       chain leading from the latter to the former.
+pack index::
+       The list of identifiers, and other information, of the objects in a
+       pack, to assist in efficiently accessing the contents of a pack.
 
-chain::
-       A list of objects, where each object in the list contains a
-       reference to its successor (for example, the successor of a commit
-       could be one of its parents).
+parent::
+       A commit object contains a (possibly empty) list of the logical
+       predecessor(s) in the line of development, i.e. its parents.
 
-fetch::
-       Fetching a branch means to get the branch's head ref from a
-       remote repository, to find out which objects are missing from
-       the local object database, and to get them, too.
+pickaxe::
+       The term pickaxe refers to an option to the diffcore routines
+       that help select changes that add or delete a given text string.
+       With the --pickaxe-all option, it can be used to view the
+       full changeset that introduced or removed, say, a particular
+       line of text.  See gitlink:git-diff[1].
+
+plumbing::
+       Cute name for core git.
+
+porcelain::
+       Cute name for programs and program suites depending on core git,
+       presenting a high level access to core git. Porcelains expose
+       more of a SCM interface than the plumbing.
 
 pull::
        Pulling a branch means to fetch it and merge it.
@@ -221,33 +233,101 @@ push::
        the remote head ref. If the remote head is not an ancestor to the
        local head, the push fails.
 
-pack::
-       A set of objects which have been compressed into one file (to save
-       space or to transmit them efficiently).
+reachable::
+       An object is reachable from a ref/commit/tree/tag, if there is a
+       chain leading from the latter to the former.
 
-pack index::
-       The list of identifiers, and other information, of the objects in a
-       pack, to assist in efficiently accessing the contents of a pack. 
+rebase::
+       To clean a branch by starting from the head of the main line of
+       development ("master"), and reapply the (possibly cherry-picked)
+       changes from that branch.
 
-core git::
-       Fundamental data structures and utilities of git. Exposes only
-       limited source code management tools.
+ref::
+       A 40-byte hex representation of a SHA1 or a name that denotes
+       a particular object. These may be stored in `$GIT_DIR/refs/`.
+
+refspec::
+       A refspec is used by fetch and push to describe the mapping
+       between remote ref and local ref.  They are combined with
+       a colon in the format <src>:<dst>, preceded by an optional
+       plus sign, +.  For example:
+       `git fetch $URL refs/heads/master:refs/heads/origin`
+       means "grab the master branch head from the $URL and store
+       it as my origin branch head".
+       And `git push $URL refs/heads/master:refs/heads/to-upstream`
+       means "publish my master branch head as to-upstream master head
+       at $URL".   See also gitlink:git-push[1]
 
-plumbing::
-       Cute name for core git.
+repository::
+       A collection of refs together with an object database containing
+       all objects, which are reachable from the refs, possibly accompanied
+       by meta data from one or more porcelains. A repository can
+       share an object database with other repositories.
 
-porcelain::
-       Cute name for programs and program suites depending on core git,
-       presenting a high level access to core git. Porcelains expose
-       more of a SCM interface than the plumbing.
+resolve::
+       The action of fixing up manually what a failed automatic merge
+       left behind.
 
-object type:
-       One of the identifiers "commit","tree","tag" and "blob" describing
-       the type of an object.
+revision::
+       A particular state of files and directories which was stored in
+       the object database. It is referenced by a commit object.
+
+rewind::
+       To throw away part of the development, i.e. to assign the head to
+       an earlier revision.
 
 SCM::
        Source code management (tool).
 
-dircache::
-       You are *waaaaay* behind.
+SHA1::
+       Synonym for object name.
+
+topic branch::
+       A regular git branch that is used by a developer to
+       identify a conceptual line of development.  Since branches
+       are very easy and inexpensive, it is often desirable to
+       have several small branches that each contain very well
+       defined concepts or small incremental yet related changes.
+
+tracking branch::
+       A regular git branch that is used to follow changes from
+       another repository.  A tracking branch should not contain
+       direct modifications or have local commits made to it.
+       A tracking branch can usually be identified as the
+       right-hand-side ref in a Pull: refspec.
+
+tree object::
+       An object containing a list of file names and modes along with refs
+       to the associated blob and/or tree objects. A tree is equivalent
+       to a directory.
+
+tree::
+       Either a working tree, or a tree object together with the
+       dependent blob and tree objects (i.e. a stored representation
+       of a working tree).
+
+tree-ish::
+       A ref pointing to either a commit object, a tree object, or a
+       tag object pointing to a tag or commit or tree object.
+
+tag object::
+       An object containing a ref pointing to another object, which can
+       contain a message just like a commit object. It can also
+       contain a (PGP) signature, in which case it is called a "signed
+       tag object".
+
+tag::
+       A ref pointing to a tag or commit object. In contrast to a head,
+       a tag is not changed by a commit. Tags (not tag objects) are
+       stored in `$GIT_DIR/refs/tags/`. A git tag has nothing to do with
+       a Lisp tag (which is called object type in git's context).
+       A tag is most typically used to mark a particular point in the
+       commit ancestry chain.
+
+unmerged index:
+       An index which contains unmerged index entries.
+
+working tree::
+       The set of files and directories currently being worked on,
+       i.e. you can work in your working tree without using git at all.
 
index 3824a95..898b4aa 100644 (file)
@@ -100,7 +100,7 @@ update
 This hook is invoked by `git-receive-pack` on the remote repository,
 which is happens when a `git push` is done on a local repository.
 Just before updating the ref on the remote repository, the update hook
-is invoked.  It's exit status determins the success or failure of
+is invoked.  Its exit status determines the success or failure of
 the ref update.
 
 The hook executes once for each ref to be updated, and takes
@@ -151,7 +151,7 @@ so it is a poor place to do log old..new.
 
 The default post-update hook, when enabled, runs
 `git-update-server-info` to keep the information used by dumb
-transports (eg, http) up-to-date.  If you are publishing
+transports (e.g., http) up-to-date.  If you are publishing
 a git repository that is accessible via http, you should
 probably enable this hook.
 
index 50638c7..60211a5 100755 (executable)
@@ -4,12 +4,16 @@ T="$1"
 
 for h in *.html *.txt howto/*.txt howto/*.html
 do
-       diff -u -I'Last updated [0-9][0-9]-[A-Z][a-z][a-z]-' "$T/$h" "$h" || {
+       if test -f "$T/$h" &&
+          diff -u -I'Last updated [0-9][0-9]-[A-Z][a-z][a-z]-' "$T/$h" "$h"
+       then
+               :; # up to date
+       else
                echo >&2 "# install $h $T/$h"
                rm -f "$T/$h"
                mkdir -p `dirname "$T/$h"`
                cp "$h" "$T/$h"
-       }
+       fi
 done
 strip_leading=`echo "$T/" | sed -e 's|.|.|g'`
 for th in "$T"/*.html "$T"/*.txt "$T"/howto/*.txt "$T"/howto/*.html
index 98fbe7d..b52dfdc 100644 (file)
@@ -128,3 +128,14 @@ remotes::
        Stores shorthands to be used to give URL and default
        refnames to interact with remote repository to `git
        fetch`, `git pull` and `git push` commands.
+
+logs::
+       Records of changes made to refs are stored in this
+       directory.  See the documentation on git-update-ref
+       for more information.
+
+logs/refs/heads/`name`::
+       Records all changes made to the branch tip named `name`.
+
+logs/refs/tags/`name`::
+       Records all changes made to the tag named `name`.
index e57dc78..e0bc552 100644 (file)
@@ -48,7 +48,7 @@ This list is sorted alphabetically:
 ';
 
 @keys=sort {uc($a) cmp uc($b)} keys %terms;
-$pattern='(\b'.join('\b|\b',reverse @keys).'\b)';
+$pattern='(\b(?<!link:git-)'.join('\b|\b(?<!link:git-)',reverse @keys).'\b)';
 foreach $key (@keys) {
        $terms{$key}=~s/$pattern/sprintf "<<ref_".no_spaces($1).",$1>>";/eg;
        print '[[ref_'.no_spaces($key).']]'.$key."::\n"
index ed2decc..0e1ffb2 100644 (file)
@@ -5,8 +5,13 @@ GIT pack format
 
    - The header appears at the beginning and consists of the following:
 
-     4-byte signature
-     4-byte version number (network byte order)
+     4-byte signature:
+         The signature is: {'P', 'A', 'C', 'K'}
+
+     4-byte version number (network byte order):
+         GIT currently accepts version number 2 or 3 but
+         generates version 2 only.
+
      4-byte number of objects contained in the pack (network byte order)
 
      Observation: we cannot have more than 4G versions ;-) and
@@ -41,7 +46,7 @@ GIT pack format
     8-byte integers to go beyond 4G objects per pack, but it is
     not strictly necessary.
 
-  - The header is followed by sorted 28-byte entries, one entry
+  - The header is followed by sorted 24-byte entries, one entry
     per object in the pack.  Each entry is:
 
     4-byte network byte order integer, recording where the
index eaab3ee..9aadd5c 100644 (file)
@@ -288,7 +288,7 @@ And of course there is the "Other Shoe" Factor too.
         - we actively try to generate deltas from a larger object to a
           smaller one
         - this means that the top-of-tree very seldom has deltas
-          (ie deltas in _practice_ are "backwards deltas")
+          (i.e. deltas in _practice_ are "backwards deltas")
 
 Again, we should reread that whole paragraph.  Not just because
 Linus has slipped Linus's Law in there on us, but because it is
diff --git a/Documentation/tutorial-2.txt b/Documentation/tutorial-2.txt
new file mode 100644 (file)
index 0000000..894ca5e
--- /dev/null
@@ -0,0 +1,391 @@
+A tutorial introduction to git: part two
+========================================
+
+You should work through link:tutorial.html[A tutorial introduction to
+git] before reading this tutorial.
+
+The goal of this tutorial is to introduce two fundamental pieces of
+git's architecture--the object database and the index file--and to
+provide the reader with everything necessary to understand the rest
+of the git documentation.
+
+The git object database
+-----------------------
+
+Let's start a new project and create a small amount of history:
+
+------------------------------------------------
+$ mkdir test-project
+$ cd test-project
+$ git init-db
+defaulting to local storage area
+$ echo 'hello world' > file.txt
+$ git add .
+$ git commit -a -m "initial commit"
+Committing initial tree 92b8b694ffb1675e5975148e1121810081dbdffe
+$ echo 'hello world!' >file.txt
+$ git commit -a -m "add emphasis"
+------------------------------------------------
+
+What are the 40 digits of hex that git responded to the first commit
+with?
+
+We saw in part one of the tutorial that commits have names like this.
+It turns out that every object in the git history is stored under
+such a 40-digit hex name.  That name is the SHA1 hash of the object's
+contents; among other things, this ensures that git will never store
+the same data twice (since identical data is given an identical SHA1
+name), and that the contents of a git object will never change (since
+that would change the object's name as well).
+
+We can ask git about this particular object with the cat-file
+command--just cut-and-paste from the reply to the initial commit, to
+save yourself typing all 40 hex digits:
+
+------------------------------------------------
+$ git cat-file -t 92b8b694ffb1675e5975148e1121810081dbdffe
+tree
+------------------------------------------------
+
+A tree can refer to one or more "blob" objects, each corresponding to
+a file.  In addition, a tree can also refer to other tree objects,
+thus creating a directory hierarchy.  You can examine the contents of
+any tree using ls-tree (remember that a long enough initial portion
+of the SHA1 will also work):
+
+------------------------------------------------
+$ git ls-tree 92b8b694
+100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad    file.txt
+------------------------------------------------
+
+Thus we see that this tree has one file in it.  The SHA1 hash is a
+reference to that file's data:
+
+------------------------------------------------
+$ git cat-file -t 3b18e512
+blob
+------------------------------------------------
+
+A "blob" is just file data, which we can also examine with cat-file:
+
+------------------------------------------------
+$ git cat-file blob 3b18e512
+hello world
+------------------------------------------------
+
+Note that this is the old file data; so the object that git named in
+its response to the initial tree was a tree with a snapshot of the
+directory state that was recorded by the first commit.
+
+All of these objects are stored under their SHA1 names inside the git
+directory:
+
+------------------------------------------------
+$ find .git/objects/
+.git/objects/
+.git/objects/pack
+.git/objects/info
+.git/objects/3b
+.git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad
+.git/objects/92
+.git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe
+.git/objects/54
+.git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7
+.git/objects/a0
+.git/objects/a0/423896973644771497bdc03eb99d5281615b51
+.git/objects/d0
+.git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59
+.git/objects/c4
+.git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241
+------------------------------------------------
+
+and the contents of these files is just the compressed data plus a
+header identifying their length and their type.  The type is either a
+blob, a tree, a commit, or a tag.  We've seen a blob and a tree now,
+so next we should look at a commit.
+
+The simplest commit to find is the HEAD commit, which we can find
+from .git/HEAD:
+
+------------------------------------------------
+$ cat .git/HEAD
+ref: refs/heads/master
+------------------------------------------------
+
+As you can see, this tells us which branch we're currently on, and it
+tells us this by naming a file under the .git directory, which itself
+contains a SHA1 name referring to a commit object, which we can
+examine with cat-file:
+
+------------------------------------------------
+$ cat .git/refs/heads/master
+c4d59f390b9cfd4318117afde11d601c1085f241
+$ git cat-file -t c4d59f39
+commit
+$ git cat-file commit c4d59f39
+tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
+parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
+author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500
+committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500
+
+add emphasis
+------------------------------------------------
+
+The "tree" object here refers to the new state of the tree:
+
+------------------------------------------------
+$ git ls-tree d0492b36
+100644 blob a0423896973644771497bdc03eb99d5281615b51    file.txt
+$ git cat-file blob a0423896
+hello world!
+------------------------------------------------
+
+and the "parent" object refers to the previous commit:
+
+------------------------------------------------
+$ git-cat-file commit 54196cc2
+tree 92b8b694ffb1675e5975148e1121810081dbdffe
+author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
+committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
+
+initial commit
+------------------------------------------------
+
+The tree object is the tree we examined first, and this commit is
+unusual in that it lacks any parent.
+
+Most commits have only one parent, but it is also common for a commit
+to have multiple parents.   In that case the commit represents a
+merge, with the parent references pointing to the heads of the merged
+branches.
+
+Besides blobs, trees, and commits, the only remaining type of object
+is a "tag", which we won't discuss here; refer to gitlink:git-tag[1]
+for details.
+
+So now we know how git uses the object database to represent a
+project's history:
+
+  * "commit" objects refer to "tree" objects representing the
+    snapshot of a directory tree at a particular point in the
+    history, and refer to "parent" commits to show how they're
+    connected into the project history.
+  * "tree" objects represent the state of a single directory,
+    associating directory names to "blob" objects containing file
+    data and "tree" objects containing subdirectory information.
+  * "blob" objects contain file data without any other structure.
+  * References to commit objects at the head of each branch are
+    stored in files under .git/refs/heads/.
+  * The name of the current branch is stored in .git/HEAD.
+
+Note, by the way, that lots of commands take a tree as an argument.
+But as we can see above, a tree can be referred to in many different
+ways--by the SHA1 name for that tree, by the name of a commit that
+refers to the tree, by the name of a branch whose head refers to that
+tree, etc.--and most such commands can accept any of these names.
+
+In command synopses, the word "tree-ish" is sometimes used to
+designate such an argument.
+
+The index file
+--------------
+
+The primary tool we've been using to create commits is "git commit
+-a", which creates a commit including every change you've made to
+your working tree.  But what if you want to commit changes only to
+certain files?  Or only certain changes to certain files?
+
+If we look at the way commits are created under the cover, we'll see
+that there are more flexible ways creating commits.
+
+Continuing with our test-project, let's modify file.txt again:
+
+------------------------------------------------
+$ echo "hello world, again" >>file.txt
+------------------------------------------------
+
+but this time instead of immediately making the commit, let's take an
+intermediate step, and ask for diffs along the way to keep track of
+what's happening:
+
+------------------------------------------------
+$ git diff
+--- a/file.txt
++++ b/file.txt
+@@ -1 +1,2 @@
+ hello world!
++hello world, again
+$ git update-index file.txt
+$ git diff
+------------------------------------------------
+
+The last diff is empty, but no new commits have been made, and the
+head still doesn't contain the new line:
+
+------------------------------------------------
+$ git-diff HEAD
+diff --git a/file.txt b/file.txt
+index a042389..513feba 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1 +1,2 @@
+ hello world!
++hello world, again
+------------------------------------------------
+
+So "git diff" is comparing against something other than the head.
+The thing that it's comparing against is actually the index file,
+which is stored in .git/index in a binary format, but whose contents
+we can examine with ls-files:
+
+------------------------------------------------
+$ git ls-files --stage
+100644 513feba2e53ebbd2532419ded848ba19de88ba00 0       file.txt
+$ git cat-file -t 513feba2
+blob
+$ git cat-file blob 513feba2
+hello world, again
+------------------------------------------------
+
+So what our "git update-index" did was store a new blob and then put
+a reference to it in the index file.  If we modify the file again,
+we'll see that the new modifications are reflected in the "git-diff"
+output:
+
+------------------------------------------------
+$ echo 'again?' >>file.txt
+$ git diff
+index 513feba..ba3da7b 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1,2 +1,3 @@
+ hello world!
+ hello world, again
++again?
+------------------------------------------------
+
+With the right arguments, git diff can also show us the difference
+between the working directory and the last commit, or between the
+index and the last commit:
+
+------------------------------------------------
+$ git diff HEAD
+diff --git a/file.txt b/file.txt
+index a042389..ba3da7b 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1 +1,3 @@
+ hello world!
++hello world, again
++again?
+$ git diff --cached
+diff --git a/file.txt b/file.txt
+index a042389..513feba 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1 +1,2 @@
+ hello world!
++hello world, again
+------------------------------------------------
+
+At any time, we can create a new commit using "git commit" (without
+the -a option), and verify that the state committed only includes the
+changes stored in the index file, not the additional change that is
+still only in our working tree:
+
+------------------------------------------------
+$ git commit -m "repeat"
+$ git diff HEAD
+diff --git a/file.txt b/file.txt
+index 513feba..ba3da7b 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1,2 +1,3 @@
+ hello world!
+ hello world, again
++again?
+------------------------------------------------
+
+So by default "git commit" uses the index to create the commit, not
+the working tree; the -a option to commit tells it to first update
+the index with all changes in the working tree.
+
+Finally, it's worth looking at the effect of "git add" on the index
+file:
+
+------------------------------------------------
+$ echo "goodbye, world" >closing.txt
+$ git add closing.txt
+------------------------------------------------
+
+The effect of the "git add" was to add one entry to the index file:
+
+------------------------------------------------
+$ git ls-files --stage
+100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0       closing.txt
+100644 513feba2e53ebbd2532419ded848ba19de88ba00 0       file.txt
+------------------------------------------------
+
+And, as you can see with cat-file, this new entry refers to the
+current contents of the file:
+
+------------------------------------------------
+$ git cat-file blob a6b11f7a
+goodbye, word
+------------------------------------------------
+
+The "status" command is a useful way to get a quick summary of the
+situation:
+
+------------------------------------------------
+$ git status
+#
+# Updated but not checked in:
+#   (will commit)
+#
+#       new file: closing.txt
+#
+#
+# Changed but not updated:
+#   (use git-update-index to mark for commit)
+#
+#       modified: file.txt
+#
+------------------------------------------------
+
+Since the current state of closing.txt is cached in the index file,
+it is listed as "updated but not checked in".  Since file.txt has
+changes in the working directory that aren't reflected in the index,
+it is marked "changed but not updated".  At this point, running "git
+commit" would create a commit that added closing.txt (with its new
+contents), but that didn't modify file.txt.
+
+Also, note that a bare "git diff" shows the changes to file.txt, but
+not the addition of closing.txt, because the version of closing.txt
+in the index file is identical to the one in the working directory.
+
+In addition to being the staging area for new commits, the index file
+is also populated from the object database when checking out a
+branch, and is used to hold the trees involved in a merge operation.
+See the link:core-tutorial.txt[core tutorial] and the relevant man
+pages for details.
+
+What next?
+----------
+
+At this point you should know everything necessary to read the man
+pages for any of the git commands; one good place to start would be
+with the commands mentioned in link:everyday.html[Everyday git].  You
+should be able to find any unknown jargon in the
+link:glossary.html[Glossary].
+
+The link:cvs-migration.html[CVS migration] document explains how to
+import a CVS repository into git, and shows how to use git in a
+CVS-like way.
+
+For some interesting examples of git use, see the
+link:howto-index.html[howtos].
+
+For git developers, the link:core-tutorial.html[Core tutorial] goes
+into detail on the lower-level git mechanisms involved in, for
+example, creating a new commit.
index fa79b01..554ee0a 100644 (file)
@@ -80,13 +80,13 @@ file; just remove it, then commit.
 At any point you can view the history of your changes using
 
 ------------------------------------------------
-$ git whatchanged
+$ git log
 ------------------------------------------------
 
 If you also want to see complete diffs at each step, use
 
 ------------------------------------------------
-$ git whatchanged -p
+$ git log -p
 ------------------------------------------------
 
 Managing branches
@@ -194,7 +194,7 @@ $ git clone /home/alice/project myrepo
 
 This creates a new directory "myrepo" containing a clone of Alice's
 repository.  The clone is on an equal footing with the original
-project, posessing its own copy of the original project's history.
+project, possessing its own copy of the original project's history.
 
 Bob then makes some changes and commits them:
 
@@ -216,7 +216,7 @@ This actually pulls changes from the branch in Bob's repository named
 "master".  Alice could request a different branch by adding the name
 of the branch to the end of the git pull command line.
 
-This merges Bob's changes into her repository; "git whatchanged" will
+This merges Bob's changes into her repository; "git log" will
 now show the new commits.  If Alice has made her own changes in the
 meantime, then Bob's changes will be merged in, and she will need to
 manually fix any conflicts.
@@ -234,13 +234,13 @@ named bob-incoming.  (Unlike git pull, git fetch just fetches a copy
 of Bob's line of development without doing any merging).  Then
 
 -------------------------------------
-$ git whatchanged -p master..bob-incoming
+$ git log -p master..bob-incoming
 -------------------------------------
 
 shows a list of all the changes that Bob made since he branched from
 Alice's master branch.
 
-After examing those changes, and possibly fixing things, Alice can
+After examining those changes, and possibly fixing things, Alice can
 pull the changes into her master branch:
 
 -------------------------------------
@@ -288,102 +288,187 @@ Git can also be used in a CVS-like mode, with a central repository
 that various users push changes to; see gitlink:git-push[1] and
 link:cvs-migration.html[git for CVS users].
 
-Keeping track of history
-------------------------
+Exploring history
+-----------------
 
-Git history is represented as a series of interrelated commits.  The
-most recent commit in the currently checked-out branch can always be
-referred to as HEAD, and the "parent" of any commit can always be
-referred to by appending a caret, "^", to the end of the name of the
-commit.  So, for example,
+Git history is represented as a series of interrelated commits.  We
+have already seen that the git log command can list those commits.
+Note that first line of each git log entry also gives a name for the
+commit:
 
 -------------------------------------
-git diff HEAD^ HEAD
+$ git log
+commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
+Author: Junio C Hamano <junkio@cox.net>
+Date:   Tue May 16 17:18:22 2006 -0700
+
+    merge-base: Clarify the comments on post processing.
 -------------------------------------
 
-shows the difference between the most-recently checked-in state of
-the tree and the previous state, and
+We can give this name to git show to see the details about this
+commit.
 
 -------------------------------------
-git diff HEAD^^ HEAD^
+$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
 -------------------------------------
 
-shows the difference between that previous state and the state two
-commits ago.  Also, HEAD~5 can be used as a shorthand for HEAD{caret}{caret}{caret}{caret}{caret},
-and more generally HEAD~n can refer to the nth previous commit.
-Commits representing merges have more than one parent, and you can
-specify which parent to follow in that case; see
-gitlink:git-rev-parse[1].
+But there other ways to refer to commits.  You can use any initial
+part of the name that is long enough to uniquely identify the commit:
+
+-------------------------------------
+$ git show c82a22c39c  # the first few characters of the name are
+                       # usually enough
+$ git show HEAD                # the tip of the current branch
+$ git show experimental        # the tip of the "experimental" branch
+-------------------------------------
 
-The name of a branch can also be used to refer to the most recent
-commit on that branch; so you can also say things like
+Every commit has at least one "parent" commit, which points to the
+previous state of the project:
 
 -------------------------------------
-git diff HEAD experimental
+$ git show HEAD^  # to see the parent of HEAD
+$ git show HEAD^^ # to see the grandparent of HEAD
+$ git show HEAD~4 # to see the great-great grandparent of HEAD
 -------------------------------------
 
-to see the difference between the most-recently committed tree in
-the current branch and the most-recently committed tree in the
-experimental branch.
+Note that merge commits may have more than one parent:
 
-But you may find it more useful to see the list of commits made in
-the experimental branch but not in the current branch, and
+-------------------------------------
+$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
+$ git show HEAD^2 # show the second parent of HEAD
+-------------------------------------
+
+You can also give commits names of your own; after running
 
 -------------------------------------
-git whatchanged HEAD..experimental
+$ git-tag v2.5 1b2e1d63ff
 -------------------------------------
 
-will do that, just as
+you can refer to 1b2e1d63ff by the name "v2.5".  If you intend to
+share this name with other people (for example, to identify a release
+version), you should create a "tag" object, and perhaps sign it; see
+gitlink:git-tag[1] for details.
+
+Any git command that needs to know a commit can take any of these
+names.  For example:
 
 -------------------------------------
-git whatchanged experimental..HEAD
+$ git diff v2.5 HEAD    # compare the current HEAD to v2.5
+$ git branch stable v2.5 # start a new branch named "stable" based
+                        # at v2.5
+$ git reset --hard HEAD^ # reset your current branch and working
+                        # directory to its state at HEAD^
 -------------------------------------
 
-will show the list of commits made on the HEAD but not included in
-experimental.
+Be careful with that last command: in addition to losing any changes
+in the working directory, it will also remove all later commits from
+this branch.  If this branch is the only branch containing those
+commits, they will be lost.  (Also, don't use "git reset" on a
+publicly-visible branch that other developers pull from, as git will
+be confused by history that disappears in this way.)
 
-You can also give commits convenient names of your own: after running
+The git grep command can search for strings in any version of your
+project, so
 
 -------------------------------------
-$ git-tag v2.5 HEAD^^
+$ git grep "hello" v2.5
 -------------------------------------
 
-you can refer to HEAD^^ by the name "v2.5".  If you intend to share
-this name with other people (for example, to identify a release
-version), you should create a "tag" object, and perhaps sign it; see
-gitlink:git-tag[1] for details.
+searches for all occurrences of "hello" in v2.5.
 
-You can revisit the old state of a tree, and make further
-modifications if you wish, using git branch: the command
+If you leave out the commit name, git grep will search any of the
+files it manages in your current directory.  So
 
 -------------------------------------
-$ git branch stable-release v2.5
+$ git grep "hello"
 -------------------------------------
 
-will create a new branch named "stable-release" starting from the
-commit which you tagged with the name v2.5.
+is a quick way to search just the files that are tracked by git.
 
-You can reset the state of any branch to an earlier commit at any
-time with
+Many git commands also take sets of commits, which can be specified
+in a number of ways.  Here are some examples with git log:
 
 -------------------------------------
-$ git reset --hard v2.5
+$ git log v2.5..v2.6            # commits between v2.5 and v2.6
+$ git log v2.5..                # commits since v2.5
+$ git log --since="2 weeks ago" # commits from the last 2 weeks
+$ git log v2.5.. Makefile       # commits since v2.5 which modify
+                               # Makefile
 -------------------------------------
 
-This will remove all later commits from this branch and reset the
-working tree to the state it had when the given commit was made.  If
-this branch is the only branch containing the later commits, those
-later changes will be lost.  Don't use "git reset" on a
-publicly-visible branch that other developers pull from, as git will
-be confused by history that disappears in this way.
+You can also give git log a "range" of commits where the first is not
+necessarily an ancestor of the second; for example, if the tips of
+the branches "stable-release" and "master" diverged from a common
+commit some time ago, then
+
+-------------------------------------
+$ git log stable..experimental
+-------------------------------------
+
+will list commits made in the experimental branch but not in the
+stable branch, while
+
+-------------------------------------
+$ git log experimental..stable
+-------------------------------------
+
+will show the list of commits made on the stable branch but not
+the experimental branch.
+
+The "git log" command has a weakness: it must present commits in a
+list.  When the history has lines of development that diverged and
+then merged back together, the order in which "git log" presents
+those commits is meaningless.
+
+Most projects with multiple contributors (such as the linux kernel,
+or git itself) have frequent merges, and gitk does a better job of
+visualizing their history.  For example,
+
+-------------------------------------
+$ gitk --since="2 weeks ago" drivers/
+-------------------------------------
+
+allows you to browse any commits from the last 2 weeks of commits
+that modified files under the "drivers" directory.  (Note: you can
+adjust gitk's fonts by holding down the control key while pressing
+"-" or "+".)
+
+Finally, most commands that take filenames will optionally allow you
+to precede any filename by a commit, to specify a particular version
+of the file:
+
+-------------------------------------
+$ git diff v2.5:Makefile HEAD:Makefile.in
+-------------------------------------
+
+You can also use "git cat-file -p" to see any such file:
+
+-------------------------------------
+$ git cat-file -p v2.5:Makefile
+-------------------------------------
 
 Next Steps
 ----------
 
-Some good commands to explore next:
+This tutorial should be enough to perform basic distributed revision
+control for your projects.  However, to fully understand the depth
+and power of git you need to understand two simple ideas on which it
+is based:
+
+  * The object database is the rather elegant system used to
+    store the history of your project--files, directories, and
+    commits.
+
+  * The index file is a cache of the state of a directory tree,
+    used to create commits, check out working directories, and
+    hold the various trees involved in a merge.
 
-  * gitlink:git-diff[1]: This flexible command does much more than
-    we've seen in the few examples above.
+link:tutorial-2.html[Part two of this tutorial] explains the object
+database, the index file, and a few other odds and ends that you'll
+need to make the most of git.
+
+If you don't want to consider with that right away, a few other
+digressions that may be interesting at this point are:
 
   * gitlink:git-format-patch[1], gitlink:git-am[1]: These convert
     series of git commits into emailed patches, and vice versa,
@@ -397,8 +482,6 @@ Some good commands to explore next:
     smart enough to perform a close-to-optimal search even in the
     case of complex non-linear history with lots of merged branches.
 
-Other good starting points include link:everyday.html[Everday GIT
-with 20 Commands Or So] and link:cvs-migration.html[git for CVS
-users].  Also, link:core-tutorial.html[A short git tutorial] gives an
-introduction to lower-level git commands for advanced users and
-developers.
+  * link:everyday.html[Everyday GIT with 20 Commands Or So]
+
+  * link:cvs-migration.html[git for CVS users].
index 7fcefcd..5d25b7e 100755 (executable)
@@ -1,11 +1,11 @@
 #!/bin/sh
 
 GVF=GIT-VERSION-FILE
-DEF_VER=v1.3.GIT
+DEF_VER=v1.4.GIT
 
 # First try git-describe, then see if there is a version file
 # (included in release tarballs), then default
-if VN=$(git-describe --abbrev=4 HEAD 2>/dev/null); then
+if VN=$(git describe --abbrev=4 HEAD 2>/dev/null); then
        VN=$(echo "$VN" | sed -e 's/-/./g');
 elif test -f version
 then
@@ -16,7 +16,7 @@ fi
 
 VN=$(expr "$VN" : v*'\(.*\)')
 
-dirty=$(sh -c 'git-diff-index --name-only HEAD' 2>/dev/null) || dirty=
+dirty=$(sh -c 'git diff-index --name-only HEAD' 2>/dev/null) || dirty=
 case "$dirty" in
 '')
        ;;
index d2cc01a..2a1e639 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -28,8 +28,8 @@ all:
 #
 # Define NO_SETENV if you don't have setenv in the C library.
 #
-# Define USE_SYMLINK_HEAD if you want .git/HEAD to be a symbolic link.
-# Don't enable it on Windows.
+# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
+# Enable it on Windows.  By default, symrefs are still used.
 #
 # Define PPC_SHA1 environment variable when running make to make use of
 # a bundled SHA1 routine optimized for PowerPC.
@@ -113,25 +113,26 @@ SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__
 ### --- END CONFIGURATION SECTION ---
 
 SCRIPT_SH = \
-       git-add.sh git-bisect.sh git-branch.sh git-checkout.sh \
+       git-bisect.sh git-branch.sh git-checkout.sh \
        git-cherry.sh git-clean.sh git-clone.sh git-commit.sh \
-       git-count-objects.sh git-diff.sh git-fetch.sh \
-       git-format-patch.sh git-ls-remote.sh \
+       git-fetch.sh \
+       git-ls-remote.sh \
        git-merge-one-file.sh git-parse-remote.sh \
-       git-prune.sh git-pull.sh git-push.sh git-rebase.sh \
+       git-prune.sh git-pull.sh git-rebase.sh \
        git-repack.sh git-request-pull.sh git-reset.sh \
-       git-resolve.sh git-revert.sh git-rm.sh git-sh-setup.sh \
-       git-tag.sh git-verify-tag.sh git-whatchanged.sh \
+       git-resolve.sh git-revert.sh git-sh-setup.sh \
+       git-tag.sh git-verify-tag.sh \
        git-applymbox.sh git-applypatch.sh git-am.sh \
        git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \
-       git-merge-resolve.sh git-merge-ours.sh git-grep.sh \
-       git-lost-found.sh
+       git-merge-resolve.sh git-merge-ours.sh \
+       git-lost-found.sh git-quiltimport.sh
 
 SCRIPT_PERL = \
        git-archimport.perl git-cvsimport.perl git-relink.perl \
        git-shortlog.perl git-fmt-merge-msg.perl git-rerere.perl \
        git-annotate.perl git-cvsserver.perl \
-       git-svnimport.perl git-mv.perl git-cvsexportcommit.perl
+       git-svnimport.perl git-mv.perl git-cvsexportcommit.perl \
+       git-send-email.perl
 
 SCRIPT_PYTHON = \
        git-merge-recursive.py
@@ -139,35 +140,39 @@ SCRIPT_PYTHON = \
 SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)) \
          $(patsubst %.perl,%,$(SCRIPT_PERL)) \
          $(patsubst %.py,%,$(SCRIPT_PYTHON)) \
-         git-cherry-pick git-show git-status
+         git-cherry-pick git-status
 
 # The ones that do not have to link with lcrypto, lz nor xdiff.
 SIMPLE_PROGRAMS = \
-       git-get-tar-commit-id$X git-mailsplit$X \
+       git-mailsplit$X \
        git-stripspace$X git-daemon$X
 
 # ... and all the rest that could be moved out of bindir to gitexecdir
 PROGRAMS = \
-       git-apply$X git-cat-file$X \
-       git-checkout-index$X git-clone-pack$X git-commit-tree$X \
-       git-convert-objects$X git-diff-files$X \
-       git-diff-index$X git-diff-stages$X \
-       git-diff-tree$X git-fetch-pack$X git-fsck-objects$X \
-       git-hash-object$X git-index-pack$X git-init-db$X git-local-fetch$X \
-       git-ls-files$X git-ls-tree$X git-mailinfo$X git-merge-base$X \
+       git-checkout-index$X git-clone-pack$X \
+       git-convert-objects$X git-fetch-pack$X git-fsck-objects$X \
+       git-hash-object$X git-index-pack$X git-local-fetch$X \
+       git-mailinfo$X git-merge-base$X \
        git-merge-index$X git-mktag$X git-mktree$X git-pack-objects$X git-patch-id$X \
-       git-peek-remote$X git-prune-packed$X git-read-tree$X \
-       git-receive-pack$X git-rev-list$X git-rev-parse$X \
-       git-send-pack$X git-show-branch$X git-shell$X \
+       git-peek-remote$X git-prune-packed$X git-receive-pack$X \
+       git-send-pack$X git-shell$X \
        git-show-index$X git-ssh-fetch$X \
-       git-ssh-upload$X git-tar-tree$X git-unpack-file$X \
+       git-ssh-upload$X git-unpack-file$X \
        git-unpack-objects$X git-update-index$X git-update-server-info$X \
        git-upload-pack$X git-verify-pack$X git-write-tree$X \
-       git-update-ref$X git-symbolic-ref$X git-check-ref-format$X \
+       git-update-ref$X git-symbolic-ref$X \
        git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \
        git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
 
-BUILT_INS = git-log$X
+BUILT_INS = git-log$X git-whatchanged$X git-show$X \
+       git-count-objects$X git-diff$X git-push$X \
+       git-grep$X git-add$X git-rm$X git-rev-list$X \
+       git-check-ref-format$X git-rev-parse$X \
+       git-init-db$X git-tar-tree$X git-upload-tar$X git-format-patch$X \
+       git-ls-files$X git-ls-tree$X git-get-tar-commit-id$X \
+       git-read-tree$X git-commit-tree$X \
+       git-apply$X git-show-branch$X git-diff-files$X \
+       git-diff-index$X git-diff-stages$X git-diff-tree$X git-cat-file$X
 
 # what 'all' will build and 'install' will install, in gitexecdir
 ALL_PROGRAMS = $(PROGRAMS) $(SIMPLE_PROGRAMS) $(SCRIPTS)
@@ -196,7 +201,7 @@ LIB_H = \
        blob.h cache.h commit.h csum-file.h delta.h \
        diff.h object.h pack.h pkt-line.h quote.h refs.h \
        run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
-       tree-walk.h log-tree.h
+       tree-walk.h log-tree.h dir.h
 
 DIFF_OBJS = \
        diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -204,17 +209,25 @@ DIFF_OBJS = \
        diffcore-delta.o log-tree.o
 
 LIB_OBJS = \
-       blob.o commit.o connect.o csum-file.o cache-tree.o \
-       date.o diff-delta.o entry.o exec_cmd.o ident.o index.o \
+       blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \
+       date.o diff-delta.o entry.o exec_cmd.o ident.o lockfile.o \
        object.o pack-check.o patch-delta.o path.o pkt-line.o \
-       quote.o read-cache.o refs.o run-command.o \
+       quote.o read-cache.o refs.o run-command.o dir.o \
        server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
        tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
        fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
        $(DIFF_OBJS)
 
 BUILTIN_OBJS = \
-       builtin-log.o builtin-help.o
+       builtin-log.o builtin-help.o builtin-count.o builtin-diff.o builtin-push.o \
+       builtin-grep.o builtin-add.o builtin-rev-list.o builtin-check-ref-format.o \
+       builtin-rm.o builtin-init-db.o builtin-rev-parse.o \
+       builtin-tar-tree.o builtin-upload-tar.o \
+       builtin-ls-files.o builtin-ls-tree.o \
+       builtin-read-tree.o builtin-commit-tree.o \
+       builtin-apply.o builtin-show-branch.o builtin-diff-files.o \
+       builtin-diff-index.o builtin-diff-stages.o builtin-diff-tree.o \
+       builtin-cat-file.o
 
 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
 LIBS = $(GITLIBS) -lz
@@ -263,6 +276,7 @@ ifeq ($(uname_O),Cygwin)
        NO_D_TYPE_IN_DIRENT = YesPlease
        NO_D_INO_IN_DIRENT = YesPlease
        NO_STRCASESTR = YesPlease
+       NO_SYMLINK_HEAD = YesPlease
        NEEDS_LIBICONV = YesPlease
        # There are conflicting reports about this.
        # On some boxes NO_MMAP is needed, and not so elsewhere.
@@ -283,7 +297,9 @@ ifeq ($(uname_S),OpenBSD)
        ALL_LDFLAGS += -L/usr/local/lib
 endif
 ifeq ($(uname_S),NetBSD)
-       NEEDS_LIBICONV = YesPlease
+       ifeq ($(shell expr "$(uname_R)" : '[01]\.'),2)
+               NEEDS_LIBICONV = YesPlease
+       endif
        ALL_CFLAGS += -I/usr/pkg/include
        ALL_LDFLAGS += -L/usr/pkg/lib -Wl,-rpath,/usr/pkg/lib
 endif
@@ -317,10 +333,6 @@ else
        endif
 endif
 
-ifdef WITH_SEND_EMAIL
-       SCRIPT_PERL += git-send-email.perl
-endif
-
 ifndef NO_CURL
        ifdef CURLDIR
                # This is still problematic -- gcc does not always want -R.
@@ -386,6 +398,9 @@ endif
 ifdef NO_D_INO_IN_DIRENT
        ALL_CFLAGS += -DNO_D_INO_IN_DIRENT
 endif
+ifdef NO_SYMLINK_HEAD
+       ALL_CFLAGS += -DNO_SYMLINK_HEAD
+endif
 ifdef NO_STRCASESTR
        COMPAT_CFLAGS += -DNO_STRCASESTR
        COMPAT_OBJS += compat/strcasestr.o
@@ -412,6 +427,9 @@ else
        ALL_CFLAGS += -Dsockaddr_storage=sockaddr_in6
 endif
 endif
+ifdef NO_INET_NTOP
+       LIB_OBJS += compat/inet_ntop.o
+endif
 
 ifdef NO_ICONV
        ALL_CFLAGS += -DNO_ICONV
@@ -453,6 +471,7 @@ PYTHON_PATH_SQ = $(subst ','\'',$(PYTHON_PATH))
 GIT_PYTHON_DIR_SQ = $(subst ','\'',$(GIT_PYTHON_DIR))
 
 ALL_CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER_SQ)' $(COMPAT_CFLAGS)
+ALL_CFLAGS += -DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"'
 LIB_OBJS += $(COMPAT_OBJS)
 export prefix TAR INSTALL DESTDIR SHELL_PATH template_dir
 ### Build rules
@@ -476,40 +495,43 @@ $(BUILT_INS): git$X
        rm -f $@ && ln git$X $@
 
 common-cmds.h: Documentation/git-*.txt
-       ./generate-cmdlist.sh > $@
+       ./generate-cmdlist.sh > $@+
+       mv $@+ $@
 
 $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh
-       rm -f $@
+       rm -f $@ $@+
        sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
            -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
            -e 's/@@NO_CURL@@/$(NO_CURL)/g' \
            -e 's/@@NO_PYTHON@@/$(NO_PYTHON)/g' \
-           $@.sh >$@
-       chmod +x $@
+           $@.sh >$@+
+       chmod +x $@+
+       mv $@+ $@
 
 $(patsubst %.perl,%,$(SCRIPT_PERL)) : % : %.perl
-       rm -f $@
+       rm -f $@ $@+
        sed -e '1s|#!.*perl|#!$(PERL_PATH_SQ)|' \
            -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
-           $@.perl >$@
-       chmod +x $@
+           $@.perl >$@+
+       chmod +x $@+
+       mv $@+ $@
 
 $(patsubst %.py,%,$(SCRIPT_PYTHON)) : % : %.py
-       rm -f $@
+       rm -f $@ $@+
        sed -e '1s|#!.*python|#!$(PYTHON_PATH_SQ)|' \
            -e 's|@@GIT_PYTHON_PATH@@|$(GIT_PYTHON_DIR_SQ)|g' \
            -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
-           $@.py >$@
-       chmod +x $@
+           $@.py >$@+
+       chmod +x $@+
+       mv $@+ $@
 
 git-cherry-pick: git-revert
-       cp $< $@
-
-git-show: git-whatchanged
-       cp $< $@
+       cp $< $@+
+       mv $@+ $@
 
 git-status: git-commit
-       cp $< $@
+       cp $< $@+
+       mv $@+ $@
 
 # These can record GIT_VERSION
 git$X git.spec \
@@ -530,7 +552,7 @@ http.o: http.c
        $(CC) -o $*.o -c $(ALL_CFLAGS) -DGIT_USER_AGENT='"git/$(GIT_VERSION)"' $<
 
 ifdef NO_EXPAT
-http-fetch.o: http-fetch.c
+http-fetch.o: http-fetch.c http.h
        $(CC) -o $*.o -c $(ALL_CFLAGS) -DNO_EXPAT $<
 endif
 
@@ -554,6 +576,7 @@ git-ssh-push$X: rsh.o
 
 git-imap-send$X: imap-send.o $(LIB_FILE)
 
+http.o http-fetch.o http-push.o: http.h
 git-http-fetch$X: fetch.o http.o http-fetch.o $(LIB_FILE)
        $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
                $(LIBS) $(CURL_LIBCURL) $(EXPAT_LIBEXPAT)
@@ -562,14 +585,6 @@ git-http-push$X: revision.o http.o http-push.o $(LIB_FILE)
        $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
                $(LIBS) $(CURL_LIBCURL) $(EXPAT_LIBEXPAT)
 
-git-rev-list$X: rev-list.o $(LIB_FILE)
-       $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
-               $(LIBS) $(OPENSSL_LIBSSL)
-
-init-db.o: init-db.c
-       $(CC) -c $(ALL_CFLAGS) \
-               -DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"' $*.c
-
 $(LIB_OBJS) $(BUILTIN_OBJS): $(LIB_H)
 $(patsubst git-%$X,%.o,$(PROGRAMS)): $(GITLIBS)
 $(DIFF_OBJS): diffcore.h
@@ -609,7 +624,7 @@ test-date$X: test-date.c date.o ctype.o
        $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) test-date.c date.o ctype.o
 
 test-delta$X: test-delta.c diff-delta.o patch-delta.o
-       $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $^ -lz
+       $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $^
 
 test-dump-cache-tree$X: dump-cache-tree.o $(GITLIBS)
        $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
@@ -629,7 +644,14 @@ install: all
        $(MAKE) -C templates install
        $(INSTALL) -d -m755 '$(DESTDIR_SQ)$(GIT_PYTHON_DIR_SQ)'
        $(INSTALL) $(PYMODULES) '$(DESTDIR_SQ)$(GIT_PYTHON_DIR_SQ)'
-       $(foreach p,$(BUILT_INS), rm -f '$(DESTDIR_SQ)$(bindir_SQ)/$p' && ln '$(DESTDIR_SQ)$(bindir_SQ)/git$X' '$(DESTDIR_SQ)$(bindir_SQ)/$p' ;)
+       if test 'z$(bindir_SQ)' != 'z$(gitexecdir_SQ)'; \
+       then \
+               ln -f '$(DESTDIR_SQ)$(bindir_SQ)/git$X' \
+                       '$(DESTDIR_SQ)$(gitexecdir_SQ)/git$X' || \
+               cp '$(DESTDIR_SQ)$(bindir_SQ)/git$X' \
+                       '$(DESTDIR_SQ)$(gitexecdir_SQ)/git$X'; \
+       fi
+       $(foreach p,$(BUILT_INS), rm -f '$(DESTDIR_SQ)$(gitexecdir_SQ)/$p' && ln '$(DESTDIR_SQ)$(gitexecdir_SQ)/git$X' '$(DESTDIR_SQ)$(gitexecdir_SQ)/$p' ;)
 
 install-doc:
        $(MAKE) -C Documentation install
@@ -640,7 +662,8 @@ install-doc:
 ### Maintainer's dist rules
 
 git.spec: git.spec.in
-       sed -e 's/@@VERSION@@/$(GIT_VERSION)/g' < $< > $@
+       sed -e 's/@@VERSION@@/$(GIT_VERSION)/g' < $< > $@+
+       mv $@+ $@
 
 GIT_TARNAME=git-$(GIT_VERSION)
 dist: git.spec git-tar-tree
@@ -656,6 +679,25 @@ dist: git.spec git-tar-tree
 rpm: dist
        $(RPMBUILD) -ta $(GIT_TARNAME).tar.gz
 
+htmldocs = git-htmldocs-$(GIT_VERSION)
+manpages = git-manpages-$(GIT_VERSION)
+dist-doc:
+       rm -fr .doc-tmp-dir
+       mkdir .doc-tmp-dir
+       $(MAKE) -C Documentation WEBDOC_DEST=../.doc-tmp-dir install-webdoc
+       cd .doc-tmp-dir && $(TAR) cf ../$(htmldocs).tar .
+       gzip -n -9 -f $(htmldocs).tar
+       :
+       rm -fr .doc-tmp-dir
+       mkdir .doc-tmp-dir .doc-tmp-dir/man1 .doc-tmp-dir/man7
+       $(MAKE) -C Documentation DESTDIR=./ \
+               man1=../.doc-tmp-dir/man1 \
+               man7=../.doc-tmp-dir/man7 \
+               install
+       cd .doc-tmp-dir && $(TAR) cf ../$(manpages).tar .
+       gzip -n -9 -f $(manpages).tar
+       rm -fr .doc-tmp-dir
+
 ### Cleaning rules
 
 clean:
@@ -663,8 +705,9 @@ clean:
                $(LIB_FILE) $(XDIFF_LIB)
        rm -f $(ALL_PROGRAMS) $(BUILT_INS) git$X
        rm -f *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h TAGS tags
-       rm -rf $(GIT_TARNAME)
+       rm -rf $(GIT_TARNAME) .doc-tmp-dir
        rm -f $(GIT_TARNAME).tar.gz git-core_$(GIT_VERSION)-*.tar.gz
+       rm -f $(htmldocs).tar.gz $(manpages).tar.gz
        $(MAKE) -C Documentation/ clean
        $(MAKE) -C templates clean
        $(MAKE) -C t/ clean
@@ -691,4 +734,3 @@ check-docs::
                *) echo "no link: $$v";; \
                esac ; \
        done | sort
-
diff --git a/apply.c b/apply.c
deleted file mode 100644 (file)
index acecf8d..0000000
--- a/apply.c
+++ /dev/null
@@ -1,2092 +0,0 @@
-/*
- * apply.c
- *
- * Copyright (C) Linus Torvalds, 2005
- *
- * This applies patches on top of some (arbitrary) version of the SCM.
- *
- */
-#include <fnmatch.h>
-#include "cache.h"
-#include "cache-tree.h"
-#include "quote.h"
-#include "blob.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
-//  --numstat does numeric diffstat, and doesn't actually apply
-//  --index-info shows the old and new index info for paths if available.
-//
-static const char *prefix;
-static int prefix_length = -1;
-
-static int p_value = 1;
-static int allow_binary_replacement = 0;
-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 no_add = 0;
-static int show_index_info = 0;
-static int line_termination = '\n';
-static unsigned long p_context = -1;
-static const char apply_usage[] =
-"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
-
-static enum whitespace_eol {
-       nowarn_whitespace,
-       warn_on_whitespace,
-       error_on_whitespace,
-       strip_whitespace,
-} new_whitespace = warn_on_whitespace;
-static int whitespace_error = 0;
-static int squelch_whitespace_errors = 5;
-static int applied_after_stripping = 0;
-static const char *patch_input_file = NULL;
-
-static void parse_whitespace_option(const char *option)
-{
-       if (!option) {
-               new_whitespace = warn_on_whitespace;
-               return;
-       }
-       if (!strcmp(option, "warn")) {
-               new_whitespace = warn_on_whitespace;
-               return;
-       }
-       if (!strcmp(option, "nowarn")) {
-               new_whitespace = nowarn_whitespace;
-               return;
-       }
-       if (!strcmp(option, "error")) {
-               new_whitespace = error_on_whitespace;
-               return;
-       }
-       if (!strcmp(option, "error-all")) {
-               new_whitespace = error_on_whitespace;
-               squelch_whitespace_errors = 0;
-               return;
-       }
-       if (!strcmp(option, "strip")) {
-               new_whitespace = strip_whitespace;
-               return;
-       }
-       die("unrecognized whitespace option '%s'", option);
-}
-
-static void set_default_whitespace_mode(const char *whitespace_option)
-{
-       if (!whitespace_option && !apply_default_whitespace) {
-               new_whitespace = (apply
-                                 ? warn_on_whitespace
-                                 : nowarn_whitespace);
-       }
-}
-
-/*
- * For "diff-stat" like behaviour, we keep track of the biggest change
- * we've seen, and the longest filename. That allows us to do simple
- * scaling.
- */
-static int max_change, max_len;
-
-/*
- * Various "current state", notably line numbers and what
- * file (and how) we're patching right now.. The "is_xxxx"
- * things are flags, where -1 means "don't know yet".
- */
-static int linenr = 1;
-
-struct fragment {
-       unsigned long leading, trailing;
-       unsigned long oldpos, oldlines;
-       unsigned long newpos, newlines;
-       const char *patch;
-       int size;
-       struct fragment *next;
-};
-
-struct patch {
-       char *new_name, *old_name, *def_name;
-       unsigned int old_mode, new_mode;
-       int is_rename, is_copy, is_new, is_delete, is_binary;
-       int lines_added, lines_deleted;
-       int score;
-       struct fragment *fragments;
-       char *result;
-       unsigned long resultsize;
-       char old_sha1_prefix[41];
-       char new_sha1_prefix[41];
-       struct patch *next;
-};
-
-#define CHUNKSIZE (8192)
-#define SLOP (16)
-
-static void *read_patch_file(int fd, unsigned long *sizep)
-{
-       unsigned long size = 0, alloc = CHUNKSIZE;
-       void *buffer = xmalloc(alloc);
-
-       for (;;) {
-               int nr = alloc - size;
-               if (nr < 1024) {
-                       alloc += CHUNKSIZE;
-                       buffer = xrealloc(buffer, alloc);
-                       nr = alloc - size;
-               }
-               nr = xread(fd, buffer + size, nr);
-               if (!nr)
-                       break;
-               if (nr < 0)
-                       die("git-apply: read returned %s", strerror(errno));
-               size += nr;
-       }
-       *sizep = size;
-
-       /*
-        * Make sure that we have some slop in the buffer
-        * so that we can do speculative "memcmp" etc, and
-        * see to it that it is NUL-filled.
-        */
-       if (alloc < size + SLOP)
-               buffer = xrealloc(buffer, size + SLOP);
-       memset(buffer + size, 0, SLOP);
-       return buffer;
-}
-
-static unsigned long linelen(const char *buffer, unsigned long size)
-{
-       unsigned long len = 0;
-       while (size--) {
-               len++;
-               if (*buffer++ == '\n')
-                       break;
-       }
-       return len;
-}
-
-static int is_dev_null(const char *str)
-{
-       return !memcmp("/dev/null", str, 9) && isspace(str[9]);
-}
-
-#define TERM_SPACE     1
-#define TERM_TAB       2
-
-static int name_terminate(const char *name, int namelen, int c, int terminate)
-{
-       if (c == ' ' && !(terminate & TERM_SPACE))
-               return 0;
-       if (c == '\t' && !(terminate & TERM_TAB))
-               return 0;
-
-       return 1;
-}
-
-static char * find_name(const char *line, char *def, int p_value, int terminate)
-{
-       int len;
-       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;
-
-               if (isspace(c)) {
-                       if (c == '\n')
-                               break;
-                       if (name_terminate(start, line-start, c, terminate))
-                               break;
-               }
-               line++;
-               if (c == '/' && !--p_value)
-                       start = line;
-       }
-       if (!start)
-               return def;
-       len = line - start;
-       if (!len)
-               return def;
-
-       /*
-        * Generally we prefer the shorter name, especially
-        * if the other one is just a variation of that with
-        * something else tacked on to the end (ie "file.orig"
-        * or "file~").
-        */
-       if (def) {
-               int deflen = strlen(def);
-               if (deflen < len && !strncmp(start, def, deflen))
-                       return def;
-       }
-
-       name = xmalloc(len + 1);
-       memcpy(name, start, len);
-       name[len] = 0;
-       free(def);
-       return name;
-}
-
-/*
- * 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.
- */
-static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
-{
-       char *name;
-
-       first += 4;     // skip "--- "
-       second += 4;    // skip "+++ "
-       if (is_dev_null(first)) {
-               patch->is_new = 1;
-               patch->is_delete = 0;
-               name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
-               patch->new_name = name;
-       } else if (is_dev_null(second)) {
-               patch->is_new = 0;
-               patch->is_delete = 1;
-               name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
-               patch->old_name = name;
-       } else {
-               name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
-               name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
-               patch->old_name = patch->new_name = name;
-       }
-       if (!name)
-               die("unable to find filename in patch at line %d", linenr);
-}
-
-static int gitdiff_hdrend(const char *line, struct patch *patch)
-{
-       return -1;
-}
-
-/*
- * We're anal about diff header consistency, to make
- * sure that we don't end up having strange ambiguous
- * patches floating around.
- *
- * As a result, gitdiff_{old|new}name() will check
- * their names against any previous information, just
- * to make sure..
- */
-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, 0);
-
-       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);
-               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;
-       }
-       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)
-{
-       patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
-       return 0;
-}
-
-static int gitdiff_newname(const char *line, struct patch *patch)
-{
-       patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
-       return 0;
-}
-
-static int gitdiff_oldmode(const char *line, struct patch *patch)
-{
-       patch->old_mode = strtoul(line, NULL, 8);
-       return 0;
-}
-
-static int gitdiff_newmode(const char *line, struct patch *patch)
-{
-       patch->new_mode = strtoul(line, NULL, 8);
-       return 0;
-}
-
-static int gitdiff_delete(const char *line, struct patch *patch)
-{
-       patch->is_delete = 1;
-       patch->old_name = patch->def_name;
-       return gitdiff_oldmode(line, patch);
-}
-
-static int gitdiff_newfile(const char *line, struct patch *patch)
-{
-       patch->is_new = 1;
-       patch->new_name = patch->def_name;
-       return gitdiff_newmode(line, patch);
-}
-
-static int gitdiff_copysrc(const char *line, struct patch *patch)
-{
-       patch->is_copy = 1;
-       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, 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, 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, 0, 0);
-       return 0;
-}
-
-static int gitdiff_similarity(const char *line, struct patch *patch)
-{
-       if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
-               patch->score = 0;
-       return 0;
-}
-
-static int gitdiff_dissimilarity(const char *line, struct patch *patch)
-{
-       if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
-               patch->score = 0;
-       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.
- */
-static int gitdiff_unrecognized(const char *line, struct patch *patch)
-{
-       return -1;
-}
-
-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;
-       const char *name;
-       const char *second = NULL;
-
-       line += strlen("diff --git ");
-       llen -= strlen("diff --git ");
-
-       if (*line == '"') {
-               const char *cp;
-               char *first = unquote_c_style(line, &second);
-               if (!first)
-                       return NULL;
-
-               /* 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;
-       }
-
-       /* 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.
-        */
-       for (len = 0 ; ; len++) {
-               char c = name[len];
-
-               switch (c) {
-               default:
-                       continue;
-               case '\n':
-                       return NULL;
-               case '\t': case ' ':
-                       second = name+len;
-                       for (;;) {
-                               char c = *second++;
-                               if (c == '\n')
-                                       return NULL;
-                               if (c == '/')
-                                       break;
-                       }
-                       if (second[len] == '\n' && !memcmp(name, second, len)) {
-                               char *ret = xmalloc(len + 1);
-                               memcpy(ret, name, len);
-                               ret[len] = 0;
-                               return ret;
-                       }
-               }
-       }
-       return NULL;
-}
-
-/* Verify that we recognize the lines following a git header */
-static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
-{
-       unsigned long offset;
-
-       /* A git diff has explicit new/delete information, so we don't guess */
-       patch->is_new = 0;
-       patch->is_delete = 0;
-
-       /*
-        * Some things may not have the old name in the
-        * rest of the headers anywhere (pure mode changes,
-        * or removing or adding empty files), so we get
-        * the default name from the header.
-        */
-       patch->def_name = git_header_name(line, len);
-
-       line += len;
-       size -= len;
-       linenr++;
-       for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
-               static const struct opentry {
-                       const char *str;
-                       int (*fn)(const char *, struct patch *);
-               } optable[] = {
-                       { "@@ -", gitdiff_hdrend },
-                       { "--- ", gitdiff_oldname },
-                       { "+++ ", gitdiff_newname },
-                       { "old mode ", gitdiff_oldmode },
-                       { "new mode ", gitdiff_newmode },
-                       { "deleted file mode ", gitdiff_delete },
-                       { "new file mode ", gitdiff_newfile },
-                       { "copy from ", gitdiff_copysrc },
-                       { "copy to ", gitdiff_copydst },
-                       { "rename old ", gitdiff_renamesrc },
-                       { "rename new ", gitdiff_renamedst },
-                       { "rename from ", gitdiff_renamesrc },
-                       { "rename to ", gitdiff_renamedst },
-                       { "similarity index ", gitdiff_similarity },
-                       { "dissimilarity index ", gitdiff_dissimilarity },
-                       { "index ", gitdiff_index },
-                       { "", gitdiff_unrecognized },
-               };
-               int i;
-
-               len = linelen(line, size);
-               if (!len || line[len-1] != '\n')
-                       break;
-               for (i = 0; i < ARRAY_SIZE(optable); i++) {
-                       const struct opentry *p = optable + i;
-                       int oplen = strlen(p->str);
-                       if (len < oplen || memcmp(p->str, line, oplen))
-                               continue;
-                       if (p->fn(line + oplen, patch) < 0)
-                               return offset;
-                       break;
-               }
-       }
-
-       return offset;
-}
-
-static int parse_num(const char *line, unsigned long *p)
-{
-       char *ptr;
-
-       if (!isdigit(*line))
-               return 0;
-       *p = strtoul(line, &ptr, 10);
-       return ptr - line;
-}
-
-static int parse_range(const char *line, int len, int offset, const char *expect,
-                       unsigned long *p1, unsigned long *p2)
-{
-       int digits, ex;
-
-       if (offset < 0 || offset >= len)
-               return -1;
-       line += offset;
-       len -= offset;
-
-       digits = parse_num(line, p1);
-       if (!digits)
-               return -1;
-
-       offset += digits;
-       line += digits;
-       len -= digits;
-
-       *p2 = 1;
-       if (*line == ',') {
-               digits = parse_num(line+1, p2);
-               if (!digits)
-                       return -1;
-
-               offset += digits+1;
-               line += digits+1;
-               len -= digits+1;
-       }
-
-       ex = strlen(expect);
-       if (ex > len)
-               return -1;
-       if (memcmp(line, expect, ex))
-               return -1;
-
-       return offset + ex;
-}
-
-/*
- * Parse a unified diff fragment header of the
- * form "@@ -a,b +c,d @@"
- */
-static int parse_fragment_header(char *line, int len, struct fragment *fragment)
-{
-       int offset;
-
-       if (!len || line[len-1] != '\n')
-               return -1;
-
-       /* Figure out the number of lines in a fragment */
-       offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
-       offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
-
-       return offset;
-}
-
-static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
-{
-       unsigned long offset, len;
-
-       patch->is_rename = patch->is_copy = 0;
-       patch->is_new = patch->is_delete = -1;
-       patch->old_mode = patch->new_mode = 0;
-       patch->old_name = patch->new_name = NULL;
-       for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
-               unsigned long nextlen;
-
-               len = linelen(line, size);
-               if (!len)
-                       break;
-
-               /* Testing this early allows us to take a few shortcuts.. */
-               if (len < 6)
-                       continue;
-
-               /*
-                * Make sure we don't find any unconnected patch fragmants.
-                * That's a sign that we didn't find a header, and that a
-                * patch has become corrupted/broken up.
-                */
-               if (!memcmp("@@ -", line, 4)) {
-                       struct fragment dummy;
-                       if (parse_fragment_header(line, len, &dummy) < 0)
-                               continue;
-                       error("patch fragment without header at line %d: %.*s", linenr, (int)len-1, line);
-               }
-
-               if (size < len + 6)
-                       break;
-
-               /*
-                * Git patch? It might not have a real patch, just a rename
-                * or mode change, so we handle that specially
-                */
-               if (!memcmp("diff --git ", line, 11)) {
-                       int git_hdr_len = parse_git_header(line, len, size, patch);
-                       if (git_hdr_len <= len)
-                               continue;
-                       if (!patch->old_name && !patch->new_name) {
-                               if (!patch->def_name)
-                                       die("git diff header lacks filename information (line %d)", linenr);
-                               patch->old_name = patch->new_name = patch->def_name;
-                       }
-                       *hdrsize = git_hdr_len;
-                       return offset;
-               }
-
-               /** --- followed by +++ ? */
-               if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
-                       continue;
-
-               /*
-                * We only accept unified patches, so we want it to
-                * at least have "@@ -a,b +c,d @@\n", which is 14 chars
-                * minimum
-                */
-               nextlen = linelen(line + len, size - len);
-               if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
-                       continue;
-
-               /* Ok, we'll consider it a patch */
-               parse_traditional_patch(line, line+len, patch);
-               *hdrsize = len + nextlen;
-               linenr += 2;
-               return offset;
-       }
-       return -1;
-}
-
-/*
- * Parse a unified diff. Note that this really needs
- * to parse each fragment separately, since the only
- * way to know the difference between a "---" that is
- * part of a patch, and a "---" that starts the next
- * patch is to look at the line counts..
- */
-static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
-{
-       int added, deleted;
-       int len = linelen(line, size), offset;
-       unsigned long oldlines, newlines;
-       unsigned long leading, trailing;
-
-       offset = parse_fragment_header(line, len, fragment);
-       if (offset < 0)
-               return -1;
-       oldlines = fragment->oldlines;
-       newlines = fragment->newlines;
-       leading = 0;
-       trailing = 0;
-
-       if (patch->is_new < 0) {
-               patch->is_new =  !oldlines;
-               if (!oldlines)
-                       patch->old_name = NULL;
-       }
-       if (patch->is_delete < 0) {
-               patch->is_delete = !newlines;
-               if (!newlines)
-                       patch->new_name = NULL;
-       }
-
-       if (patch->is_new && oldlines)
-               return error("new file depends on old contents");
-       if (patch->is_delete != !newlines) {
-               if (newlines)
-                       return error("deleted file still has contents");
-               fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name);
-       }
-
-       /* Parse the thing.. */
-       line += len;
-       size -= len;
-       linenr++;
-       added = deleted = 0;
-       for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
-               if (!oldlines && !newlines)
-                       break;
-               len = linelen(line, size);
-               if (!len || line[len-1] != '\n')
-                       return -1;
-               switch (*line) {
-               default:
-                       return -1;
-               case ' ':
-                       oldlines--;
-                       newlines--;
-                       if (!deleted && !added)
-                               leading++;
-                       trailing++;
-                       break;
-               case '-':
-                       deleted++;
-                       oldlines--;
-                       trailing = 0;
-                       break;
-               case '+':
-                       /*
-                        * We know len is at least two, since we have a '+' and
-                        * we checked that the last character was a '\n' above.
-                        * That is, an addition of an empty line would check
-                        * the '+' here.  Sneaky...
-                        */
-                       if ((new_whitespace != nowarn_whitespace) &&
-                           isspace(line[len-2])) {
-                               whitespace_error++;
-                               if (squelch_whitespace_errors &&
-                                   squelch_whitespace_errors <
-                                   whitespace_error)
-                                       ;
-                               else {
-                                       fprintf(stderr, "Adds trailing whitespace.\n%s:%d:%.*s\n",
-                                               patch_input_file,
-                                               linenr, len-2, line+1);
-                               }
-                       }
-                       added++;
-                       newlines--;
-                       trailing = 0;
-                       break;
-
-                /* 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 "\ ".
-                * 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;
-                       break;
-               }
-       }
-       if (oldlines || newlines)
-               return -1;
-       fragment->leading = leading;
-       fragment->trailing = trailing;
-
-       /* If a fragment ends with an incomplete line, we failed to include
-        * it in the above loop because we hit oldlines == newlines == 0
-        * before seeing it.
-        */
-       if (12 < size && !memcmp(line, "\\ ", 2))
-               offset += linelen(line, size);
-
-       patch->lines_added += added;
-       patch->lines_deleted += deleted;
-       return offset;
-}
-
-static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
-{
-       unsigned long offset = 0;
-       struct fragment **fragp = &patch->fragments;
-
-       while (size > 4 && !memcmp(line, "@@ -", 4)) {
-               struct fragment *fragment;
-               int len;
-
-               fragment = xcalloc(1, sizeof(*fragment));
-               len = parse_fragment(line, size, patch, fragment);
-               if (len <= 0)
-                       die("corrupt patch at line %d", linenr);
-
-               fragment->patch = line;
-               fragment->size = len;
-
-               *fragp = fragment;
-               fragp = &fragment->next;
-
-               offset += len;
-               line += len;
-               size -= len;
-       }
-       return offset;
-}
-
-static inline int metadata_changes(struct patch *patch)
-{
-       return  patch->is_rename > 0 ||
-               patch->is_copy > 0 ||
-               patch->is_new > 0 ||
-               patch->is_delete ||
-               (patch->old_mode && patch->new_mode &&
-                patch->old_mode != patch->new_mode);
-}
-
-static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
-{
-       int hdrsize, patchsize;
-       int offset = find_header(buffer, size, &hdrsize, patch);
-
-       if (offset < 0)
-               return offset;
-
-       patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
-
-       if (!patchsize) {
-               static const char *binhdr[] = {
-                       "Binary files ",
-                       "Files ",
-                       NULL,
-               };
-               int i;
-               int hd = hdrsize + offset;
-               unsigned long llen = linelen(buffer + hd, size - hd);
-
-               if (!memcmp(" differ\n", buffer + hd + llen - 8, 8))
-                       for (i = 0; binhdr[i]; i++) {
-                               int len = strlen(binhdr[i]);
-                               if (len < size - hd &&
-                                   !memcmp(binhdr[i], buffer + hd, len)) {
-                                       patch->is_binary = 1;
-                                       break;
-                               }
-                       }
-
-               /* Empty patch cannot be applied if:
-                * - it is a binary patch and we do not do binary_replace, or
-                * - text patch without metadata change
-                */
-               if ((apply || check) &&
-                   (patch->is_binary
-                    ? !allow_binary_replacement
-                    : !metadata_changes(patch)))
-                       die("patch with only garbage at line %d", linenr);
-       }
-
-       return offset + hdrsize + patchsize;
-}
-
-static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
-static const char minuses[]= "----------------------------------------------------------------------";
-
-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
-        */
-       len = strlen(name);
-       max = max_len;
-       if (max > 50)
-               max = 50;
-       if (len > max) {
-               char *slash;
-               prefix = "...";
-               max -= 3;
-               name += len - max;
-               slash = strchr(name, '/');
-               if (slash)
-                       name = slash;
-       }
-       len = max;
-
-       /*
-        * scale the add/delete
-        */
-       max = max_change;
-       if (max + len > 70)
-               max = 70 - len;
-
-       add = patch->lines_added;
-       del = patch->lines_deleted;
-       total = add + del;
-
-       if (max_change > 0) {
-               total = (total * max + max_change / 2) / max_change;
-               add = (add * max + max_change / 2) / max_change;
-               del = total - add;
-       }
-       if (patch->is_binary)
-               printf(" %s%-*s |  Bin\n", prefix, len, name);
-       else
-               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)
-{
-       int fd;
-       unsigned long got;
-
-       switch (st->st_mode & S_IFMT) {
-       case S_IFLNK:
-               return readlink(path, buf, 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, buf + got, size - got);
-                       if (ret <= 0)
-                               break;
-                       got += ret;
-               }
-               close(fd);
-               return got;
-
-       default:
-               return -1;
-       }
-}
-
-static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines)
-{
-       int i;
-       unsigned long start, backwards, forwards;
-
-       if (fragsize > size)
-               return -1;
-
-       start = 0;
-       if (line > 1) {
-               unsigned long offset = 0;
-               i = line-1;
-               while (offset + fragsize <= size) {
-                       if (buf[offset++] == '\n') {
-                               start = offset;
-                               if (!--i)
-                                       break;
-                       }
-               }
-       }
-
-       /* Exact line number? */
-       if (!memcmp(buf + start, fragment, fragsize))
-               return start;
-
-       /*
-        * There's probably some smart way to do this, but I'll leave
-        * that to the smart and beautiful people. I'm simple and stupid.
-        */
-       backwards = start;
-       forwards = start;
-       for (i = 0; ; i++) {
-               unsigned long try;
-               int n;
-
-               /* "backward" */
-               if (i & 1) {
-                       if (!backwards) {
-                               if (forwards + fragsize > size)
-                                       break;
-                               continue;
-                       }
-                       do {
-                               --backwards;
-                       } while (backwards && buf[backwards-1] != '\n');
-                       try = backwards;
-               } else {
-                       while (forwards + fragsize <= size) {
-                               if (buf[forwards++] == '\n')
-                                       break;
-                       }
-                       try = forwards;
-               }
-
-               if (try + fragsize > size)
-                       continue;
-               if (memcmp(buf + try, fragment, fragsize))
-                       continue;
-               n = (i >> 1)+1;
-               if (i & 1)
-                       n = -n;
-               *lines = n;
-               return try;
-       }
-
-       /*
-        * We should start searching forward and backward.
-        */
-       return -1;
-}
-
-static void remove_first_line(const char **rbuf, int *rsize)
-{
-       const char *buf = *rbuf;
-       int size = *rsize;
-       unsigned long offset;
-       offset = 0;
-       while (offset <= size) {
-               if (buf[offset++] == '\n')
-                       break;
-       }
-       *rsize = size - offset;
-       *rbuf = buf + offset;
-}
-
-static void remove_last_line(const char **rbuf, int *rsize)
-{
-       const char *buf = *rbuf;
-       int size = *rsize;
-       unsigned long offset;
-       offset = size - 1;
-       while (offset > 0) {
-               if (buf[--offset] == '\n')
-                       break;
-       }
-       *rsize = offset + 1;
-}
-
-struct buffer_desc {
-       char *buffer;
-       unsigned long size;
-       unsigned long alloc;
-};
-
-static int apply_line(char *output, const char *patch, int plen)
-{
-       /* plen is number of bytes to be copied from patch,
-        * starting at patch+1 (patch[0] is '+').  Typically
-        * patch[plen] is '\n'.
-        */
-       int add_nl_to_tail = 0;
-       if ((new_whitespace == strip_whitespace) &&
-           1 < plen && isspace(patch[plen-1])) {
-               if (patch[plen] == '\n')
-                       add_nl_to_tail = 1;
-               plen--;
-               while (0 < plen && isspace(patch[plen]))
-                       plen--;
-               applied_after_stripping++;
-       }
-       memcpy(output, patch + 1, plen);
-       if (add_nl_to_tail)
-               output[plen++] = '\n';
-       return plen;
-}
-
-static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
-{
-       char *buf = desc->buffer;
-       const char *patch = frag->patch;
-       int offset, size = frag->size;
-       char *old = xmalloc(size);
-       char *new = xmalloc(size);
-       const char *oldlines, *newlines;
-       int oldsize = 0, newsize = 0;
-       unsigned long leading, trailing;
-       int pos, lines;
-
-       while (size > 0) {
-               int len = linelen(patch, size);
-               int plen;
-
-               if (!len)
-                       break;
-
-               /*
-                * "plen" is how much of the line we should use for
-                * the actual patch data. Normally we just remove the
-                * first character on the line, but if the line is
-                * followed by "\ No newline", then we also remove the
-                * last one (which is the newline, of course).
-                */
-               plen = len-1;
-               if (len < size && patch[len] == '\\')
-                       plen--;
-               switch (*patch) {
-               case ' ':
-               case '-':
-                       memcpy(old + oldsize, patch + 1, plen);
-                       oldsize += plen;
-                       if (*patch == '-')
-                               break;
-               /* Fall-through for ' ' */
-               case '+':
-                       if (*patch != '+' || !no_add)
-                               newsize += apply_line(new + newsize, patch,
-                                                     plen);
-                       break;
-               case '@': case '\\':
-                       /* Ignore it, we already handled it */
-                       break;
-               default:
-                       return -1;
-               }
-               patch += len;
-               size -= len;
-       }
-
-#ifdef NO_ACCURATE_DIFF
-       if (oldsize > 0 && old[oldsize - 1] == '\n' &&
-                       newsize > 0 && new[newsize - 1] == '\n') {
-               oldsize--;
-               newsize--;
-       }
-#endif
-
-       oldlines = old;
-       newlines = new;
-       leading = frag->leading;
-       trailing = frag->trailing;
-       lines = 0;
-       pos = frag->newpos;
-       for (;;) {
-               offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines);
-               if (offset >= 0) {
-                       int diff = newsize - oldsize;
-                       unsigned long size = desc->size + diff;
-                       unsigned long alloc = desc->alloc;
-
-                       /* Warn if it was necessary to reduce the number
-                        * of context lines.
-                        */
-                       if ((leading != frag->leading) || (trailing != frag->trailing))
-                               fprintf(stderr, "Context reduced to (%ld/%ld) to apply fragment at %d\n",
-                                       leading, trailing, pos + lines);
-
-                       if (size > alloc) {
-                               alloc = size + 8192;
-                               desc->alloc = alloc;
-                               buf = xrealloc(buf, alloc);
-                               desc->buffer = buf;
-                       }
-                       desc->size = size;
-                       memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
-                       memcpy(buf + offset, newlines, newsize);
-                       offset = 0;
-
-                       break;
-               }
-
-               /* Am I at my context limits? */
-               if ((leading <= p_context) && (trailing <= p_context))
-                       break;
-               /* Reduce the number of context lines
-                * Reduce both leading and trailing if they are equal
-                * otherwise just reduce the larger context.
-                */
-               if (leading >= trailing) {
-                       remove_first_line(&oldlines, &oldsize);
-                       remove_first_line(&newlines, &newsize);
-                       pos--;
-                       leading--;
-               }
-               if (trailing > leading) {
-                       remove_last_line(&oldlines, &oldsize);
-                       remove_last_line(&newlines, &newsize);
-                       trailing--;
-               }
-       }
-
-       free(old);
-       free(new);
-       return offset;
-}
-
-static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
-{
-       struct fragment *frag = patch->fragments;
-       const char *name = patch->old_name ? patch->old_name : patch->new_name;
-
-       if (patch->is_binary) {
-               unsigned char sha1[20];
-
-               if (!allow_binary_replacement)
-                       return error("cannot apply binary patch to '%s' "
-                                    "without --allow-binary-replacement",
-                                    name);
-
-               /* For safety, we require patch index line to contain
-                * full 40-byte textual SHA1 for old and new, at least for now.
-                */
-               if (strlen(patch->old_sha1_prefix) != 40 ||
-                   strlen(patch->new_sha1_prefix) != 40 ||
-                   get_sha1_hex(patch->old_sha1_prefix, sha1) ||
-                   get_sha1_hex(patch->new_sha1_prefix, sha1))
-                       return error("cannot apply binary patch to '%s' "
-                                    "without full index line", name);
-
-               if (patch->old_name) {
-                       unsigned char hdr[50];
-                       int hdrlen;
-
-                       /* See if the old one matches what the patch
-                        * applies to.
-                        */
-                       write_sha1_file_prepare(desc->buffer, desc->size,
-                                               blob_type, sha1, hdr, &hdrlen);
-                       if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
-                               return error("the patch applies to '%s' (%s), "
-                                            "which does not match the "
-                                            "current contents.",
-                                            name, sha1_to_hex(sha1));
-               }
-               else {
-                       /* Otherwise, the old one must be empty. */
-                       if (desc->size)
-                               return error("the patch applies to an empty "
-                                            "'%s' but it is not empty", name);
-               }
-
-               /* For now, we do not record post-image data in the patch,
-                * and require the object already present in the recipient's
-                * object database.
-                */
-               if (desc->buffer) {
-                       free(desc->buffer);
-                       desc->alloc = desc->size = 0;
-               }
-               get_sha1_hex(patch->new_sha1_prefix, sha1);
-
-               if (memcmp(sha1, null_sha1, 20)) {
-                       char type[10];
-                       unsigned long size;
-
-                       desc->buffer = read_sha1_file(sha1, type, &size);
-                       if (!desc->buffer)
-                               return error("the necessary postimage %s for "
-                                            "'%s' does not exist",
-                                            patch->new_sha1_prefix, name);
-                       desc->alloc = desc->size = size;
-               }
-
-               return 0;
-       }
-
-       while (frag) {
-               if (apply_one_fragment(desc, frag) < 0)
-                       return error("patch failed: %s:%ld",
-                                    name, frag->oldpos);
-               frag = frag->next;
-       }
-       return 0;
-}
-
-static int apply_data(struct patch *patch, struct stat *st)
-{
-       char *buf;
-       unsigned long size, alloc;
-       struct buffer_desc desc;
-
-       size = 0;
-       alloc = 0;
-       buf = NULL;
-       if (patch->old_name) {
-               size = st->st_size;
-               alloc = size + 8192;
-               buf = xmalloc(alloc);
-               if (read_old_data(st, patch->old_name, buf, alloc) != size)
-                       return error("read of %s failed", patch->old_name);
-       }
-
-       desc.size = size;
-       desc.alloc = alloc;
-       desc.buffer = buf;
-       if (apply_fragments(&desc, patch) < 0)
-               return -1;
-       patch->result = desc.buffer;
-       patch->resultsize = desc.size;
-
-       if (patch->is_delete && patch->resultsize)
-               return error("removal patch leaves file contents");
-
-       return 0;
-}
-
-static int check_patch(struct patch *patch)
-{
-       struct stat st;
-       const char *old_name = patch->old_name;
-       const char *new_name = patch->new_name;
-       const char *name = old_name ? old_name : new_name;
-
-       if (old_name) {
-               int changed;
-               int stat_ret = lstat(old_name, &st);
-
-               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);
-                       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,
-                                                  NULL) ||
-                                   lstat(old_name, &st))
-                                       return -1;
-                       }
-
-                       changed = ce_match_stat(active_cache[pos], &st, 1);
-                       if (changed)
-                               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));
-               if (!patch->old_mode)
-                       patch->old_mode = st.st_mode;
-               if ((st.st_mode ^ patch->old_mode) & S_IFMT)
-                       return error("%s: wrong type", old_name);
-               if (st.st_mode != patch->old_mode)
-                       fprintf(stderr, "warning: %s has type %o, expected %o\n",
-                               old_name, st.st_mode, patch->old_mode);
-       }
-
-       if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
-               if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
-                       return error("%s: already exists in index", new_name);
-               if (!lstat(new_name, &st))
-                       return error("%s: already exists in working directory", new_name);
-               if (errno != ENOENT)
-                       return error("%s: %s", new_name, strerror(errno));
-               if (!patch->new_mode) {
-                       if (patch->is_new)
-                               patch->new_mode = S_IFREG | 0644;
-                       else
-                               patch->new_mode = patch->old_mode;
-               }
-       }
-
-       if (new_name && old_name) {
-               int same = !strcmp(old_name, new_name);
-               if (!patch->new_mode)
-                       patch->new_mode = patch->old_mode;
-               if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
-                       return error("new mode (%o) of %s does not match old mode (%o)%s%s",
-                               patch->new_mode, new_name, patch->old_mode,
-                               same ? "" : " of ", same ? "" : old_name);
-       }       
-
-       if (apply_data(patch, &st) < 0)
-               return error("%s: patch does not apply", name);
-       return 0;
-}
-
-static int check_patch_list(struct patch *patch)
-{
-       int error = 0;
-
-       for (;patch ; patch = patch->next)
-               error |= check_patch(patch);
-       return error;
-}
-
-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 ",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);
-       }
-}
-
-static void stat_patch_list(struct patch *patch)
-{
-       int files, adds, dels;
-
-       for (files = adds = dels = 0 ; patch ; patch = patch->next) {
-               files++;
-               adds += patch->lines_added;
-               dels += patch->lines_deleted;
-               show_stats(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)
-               printf(" %s mode %06o %s\n", newdelete, mode, name);
-       else
-               printf(" %s %s\n", newdelete, name);
-}
-
-static void show_mode_change(struct patch *p, int show_name)
-{
-       if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
-               if (show_name)
-                       printf(" mode change %06o => %06o %s\n",
-                              p->old_mode, p->new_mode, p->new_name);
-               else
-                       printf(" mode change %06o => %06o\n",
-                              p->old_mode, p->new_mode);
-       }
-}
-
-static void show_rename_copy(struct patch *p)
-{
-       const char *renamecopy = p->is_rename ? "rename" : "copy";
-       const char *old, *new;
-
-       /* Find common prefix */
-       old = p->old_name;
-       new = p->new_name;
-       while (1) {
-               const char *slash_old, *slash_new;
-               slash_old = strchr(old, '/');
-               slash_new = strchr(new, '/');
-               if (!slash_old ||
-                   !slash_new ||
-                   slash_old - old != slash_new - new ||
-                   memcmp(old, new, slash_new - new))
-                       break;
-               old = slash_old + 1;
-               new = slash_new + 1;
-       }
-       /* p->old_name thru old is the common prefix, and old and new
-        * through the end of names are renames
-        */
-       if (old != p->old_name)
-               printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
-                      (int)(old - p->old_name), p->old_name,
-                      old, new, p->score);
-       else
-               printf(" %s %s => %s (%d%%)\n", renamecopy,
-                      p->old_name, p->new_name, p->score);
-       show_mode_change(p, 0);
-}
-
-static void summary_patch_list(struct patch *patch)
-{
-       struct patch *p;
-
-       for (p = patch; p; p = p->next) {
-               if (p->is_new)
-                       show_file_mode_name("create", p->new_mode, p->new_name);
-               else if (p->is_delete)
-                       show_file_mode_name("delete", p->old_mode, p->old_name);
-               else {
-                       if (p->is_rename || p->is_copy)
-                               show_rename_copy(p);
-                       else {
-                               if (p->score) {
-                                       printf(" rewrite %s (%d%%)\n",
-                                              p->new_name, p->score);
-                                       show_mode_change(p, 0);
-                               }
-                               else
-                                       show_mode_change(p, 1);
-                       }
-               }
-       }
-}
-
-static void patch_stats(struct patch *patch)
-{
-       int lines = patch->lines_added + patch->lines_deleted;
-
-       if (lines > max_change)
-               max_change = lines;
-       if (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 = quote_c_style(patch->new_name, NULL, NULL, 0);
-               if (!len)
-                       len = strlen(patch->new_name);
-               if (len > max_len)
-                       max_len = len;
-       }
-}
-
-static void remove_file(struct patch *patch)
-{
-       if (write_index) {
-               if (remove_file_from_cache(patch->old_name) < 0)
-                       die("unable to remove %s from index", patch->old_name);
-               cache_tree_invalidate_path(active_cache_tree, patch->old_name);
-       }
-       unlink(patch->old_name);
-}
-
-static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
-{
-       struct stat st;
-       struct cache_entry *ce;
-       int namelen = strlen(path);
-       unsigned ce_size = cache_entry_size(namelen);
-
-       if (!write_index)
-               return;
-
-       ce = xcalloc(1, ce_size);
-       memcpy(ce->name, path, namelen);
-       ce->ce_mode = create_ce_mode(mode);
-       ce->ce_flags = htons(namelen);
-       if (lstat(path, &st) < 0)
-               die("unable to stat newly created file %s", path);
-       fill_stat_cache_info(ce, &st);
-       if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
-               die("unable to create backing store for newly created file %s", path);
-       if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
-               die("unable to add cache entry for %s", path);
-}
-
-static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
-{
-       int fd;
-
-       if (S_ISLNK(mode))
-               return symlink(buf, path);
-       fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
-       if (fd < 0)
-               return -1;
-       while (size) {
-               int written = xwrite(fd, buf, size);
-               if (written < 0)
-                       die("writing file %s: %s", path, strerror(errno));
-               if (!written)
-                       die("out of space writing file %s", path);
-               buf += written;
-               size -= written;
-       }
-       if (close(fd) < 0)
-               die("closing file %s: %s", path, strerror(errno));
-       return 0;
-}
-
-/*
- * We optimistically assume that the directories exist,
- * which is true 99% of the time anyway. If they don't,
- * we create them and try again.
- */
-static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
-{
-       if (!try_create_file(path, mode, buf, size))
-               return;
-
-       if (errno == ENOENT) {
-               if (safe_create_leading_directories(path))
-                       return;
-               if (!try_create_file(path, mode, buf, size))
-                       return;
-       }
-
-       if (errno == EEXIST) {
-               unsigned int nr = getpid();
-
-               for (;;) {
-                       const char *newpath;
-                       newpath = mkpath("%s~%u", path, nr);
-                       if (!try_create_file(newpath, mode, buf, size)) {
-                               if (!rename(newpath, path))
-                                       return;
-                               unlink(newpath);
-                               break;
-                       }
-                       if (errno != EEXIST)
-                               break;
-                       ++nr;
-               }
-       }
-       die("unable to write file %s mode %o", path, mode);
-}
-
-static void create_file(struct patch *patch)
-{
-       char *path = patch->new_name;
-       unsigned mode = patch->new_mode;
-       unsigned long size = patch->resultsize;
-       char *buf = patch->result;
-
-       if (!mode)
-               mode = S_IFREG | 0644;
-       create_one_file(path, mode, buf, size);
-       add_index_file(path, mode, buf, size);
-       cache_tree_invalidate_path(active_cache_tree, path);
-}
-
-static void write_out_one_result(struct patch *patch)
-{
-       if (patch->is_delete > 0) {
-               remove_file(patch);
-               return;
-       }
-       if (patch->is_new > 0 || patch->is_copy) {
-               create_file(patch);
-               return;
-       }
-       /*
-        * Rename or modification boils down to the same
-        * thing: remove the old, write the new
-        */
-       remove_file(patch);
-       create_file(patch);
-}
-
-static void write_out_results(struct patch *list, int skipped_patch)
-{
-       if (!list && !skipped_patch)
-               die("No changes");
-
-       while (list) {
-               write_out_one_result(list);
-               list = list->next;
-       }
-}
-
-static struct cache_file cache_file;
-
-static struct excludes {
-       struct excludes *next;
-       const char *path;
-} *excludes;
-
-static int use_patch(struct patch *p)
-{
-       const char *pathname = p->new_name ? p->new_name : p->old_name;
-       struct excludes *x = excludes;
-       while (x) {
-               if (fnmatch(x->path, pathname, 0) == 0)
-                       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 int apply_patch(int fd, const char *filename)
-{
-       int newfd;
-       unsigned long offset, size;
-       char *buffer = read_patch_file(fd, &size);
-       struct patch *list = NULL, **listp = &list;
-       int skipped_patch = 0;
-
-       patch_input_file = filename;
-       if (!buffer)
-               return -1;
-       offset = 0;
-       while (size > 0) {
-               struct patch *patch;
-               int nr;
-
-               patch = xcalloc(1, sizeof(*patch));
-               nr = parse_chunk(buffer + offset, size, patch);
-               if (nr < 0)
-                       break;
-               if (use_patch(patch)) {
-                       patch_stats(patch);
-                       *listp = patch;
-                       listp = &patch->next;
-               } else {
-                       /* perhaps free it a bit better? */
-                       free(patch);
-                       skipped_patch++;
-               }
-               offset += nr;
-               size -= nr;
-       }
-
-       newfd = -1;
-       if (whitespace_error && (new_whitespace == error_on_whitespace))
-               apply = 0;
-
-       write_index = check_index && apply;
-       if (write_index)
-               newfd = hold_index_file_for_update(&cache_file, get_index_file());
-       if (check_index) {
-               if (read_cache() < 0)
-                       die("unable to read index file");
-       }
-
-       if ((check || apply) && check_patch_list(list) < 0)
-               exit(1);
-
-       if (apply)
-               write_out_results(list, skipped_patch);
-
-       if (write_index) {
-               if (write_cache(newfd, active_cache, active_nr) ||
-                   commit_index_file(&cache_file))
-                       die("Unable to write new cachefile");
-       }
-
-       if (show_index_info)
-               show_index_list(list);
-
-       if (diffstat)
-               stat_patch_list(list);
-
-       if (numstat)
-               numstat_patch_list(list);
-
-       if (summary)
-               summary_patch_list(list);
-
-       free(buffer);
-       return 0;
-}
-
-static int git_apply_config(const char *var, const char *value)
-{
-       if (!strcmp(var, "apply.whitespace")) {
-               apply_default_whitespace = strdup(value);
-               return 0;
-       }
-       return git_default_config(var, value);
-}
-
-
-int main(int argc, char **argv)
-{
-       int i;
-       int read_stdin = 1;
-       const char *whitespace_option = NULL;
-
-       for (i = 1; i < argc; i++) {
-               const char *arg = argv[i];
-               char *end;
-               int fd;
-
-               if (!strcmp(arg, "-")) {
-                       apply_patch(0, "<stdin>");
-                       read_stdin = 0;
-                       continue;
-               }
-               if (!strncmp(arg, "--exclude=", 10)) {
-                       struct excludes *x = xmalloc(sizeof(*x));
-                       x->path = arg + 10;
-                       x->next = excludes;
-                       excludes = x;
-                       continue;
-               }
-               if (!strncmp(arg, "-p", 2)) {
-                       p_value = atoi(arg + 2);
-                       continue;
-               }
-               if (!strcmp(arg, "--no-add")) {
-                       no_add = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--stat")) {
-                       apply = 0;
-                       diffstat = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--allow-binary-replacement")) {
-                       allow_binary_replacement = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--numstat")) {
-                       apply = 0;
-                       numstat = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--summary")) {
-                       apply = 0;
-                       summary = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--check")) {
-                       apply = 0;
-                       check = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--index")) {
-                       check_index = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--apply")) {
-                       apply = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--index-info")) {
-                       apply = 0;
-                       show_index_info = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-z")) {
-                       line_termination = 0;
-                       continue;
-               }
-               if (!strncmp(arg, "-C", 2)) {
-                       p_context = strtoul(arg + 2, &end, 0);
-                       if (*end != '\0')
-                               die("unrecognized context count '%s'", arg + 2);
-                       continue;
-               }
-               if (!strncmp(arg, "--whitespace=", 13)) {
-                       whitespace_option = arg + 13;
-                       parse_whitespace_option(arg + 13);
-                       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);
-
-               fd = open(arg, O_RDONLY);
-               if (fd < 0)
-                       usage(apply_usage);
-               read_stdin = 0;
-               set_default_whitespace_mode(whitespace_option);
-               apply_patch(fd, arg);
-               close(fd);
-       }
-       set_default_whitespace_mode(whitespace_option);
-       if (read_stdin)
-               apply_patch(0, "<stdin>");
-       if (whitespace_error) {
-               if (squelch_whitespace_errors &&
-                   squelch_whitespace_errors < whitespace_error) {
-                       int squelched =
-                               whitespace_error - squelch_whitespace_errors;
-                       fprintf(stderr, "warning: squelched %d whitespace error%s\n",
-                               squelched,
-                               squelched == 1 ? "" : "s");
-               }
-               if (new_whitespace == error_on_whitespace)
-                       die("%d line%s add%s trailing whitespaces.",
-                           whitespace_error,
-                           whitespace_error == 1 ? "" : "s",
-                           whitespace_error == 1 ? "s" : "");
-               if (applied_after_stripping)
-                       fprintf(stderr, "warning: %d line%s applied after"
-                               " stripping trailing whitespaces.\n",
-                               applied_after_stripping,
-                               applied_after_stripping == 1 ? "" : "s");
-               else if (whitespace_error)
-                       fprintf(stderr, "warning: %d line%s add%s trailing"
-                               " whitespaces.\n",
-                               whitespace_error,
-                               whitespace_error == 1 ? "" : "s",
-                               whitespace_error == 1 ? "s" : "");
-       }
-       return 0;
-}
diff --git a/base85.c b/base85.c
new file mode 100644 (file)
index 0000000..a9e97f8
--- /dev/null
+++ b/base85.c
@@ -0,0 +1,140 @@
+#include "cache.h"
+
+#undef DEBUG_85
+
+#ifdef DEBUG_85
+#define say(a) fprintf(stderr, a)
+#define say1(a,b) fprintf(stderr, a, b)
+#define say2(a,b,c) fprintf(stderr, a, b, c)
+#else
+#define say(a) do {} while(0)
+#define say1(a,b) do {} while(0)
+#define say2(a,b,c) do {} while(0)
+#endif
+
+static const char en85[] = {
+       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
+       'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+       'U', 'V', 'W', 'X', 'Y', 'Z',
+       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+       'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
+       'u', 'v', 'w', 'x', 'y', 'z',
+       '!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
+       ';', '<', '=', '>', '?', '@', '^', '_', '`', '{',
+       '|', '}', '~'
+};
+
+static char de85[256];
+static void prep_base85(void)
+{
+       int i;
+       if (de85['Z'])
+               return;
+       for (i = 0; i < ARRAY_SIZE(en85); i++) {
+               int ch = en85[i];
+               de85[ch] = i + 1;
+       }
+}
+
+int decode_85(char *dst, char *buffer, int len)
+{
+       prep_base85();
+
+       say2("decode 85 <%.*s>", len/4*5, buffer);
+       while (len) {
+               unsigned acc = 0;
+               int de, cnt = 4;
+               unsigned char ch;
+               do {
+                       ch = *buffer++;
+                       de = de85[ch];
+                       if (--de < 0)
+                               return error("invalid base85 alphabet %c", ch);
+                       acc = acc * 85 + de;
+               } while (--cnt);
+               ch = *buffer++;
+               de = de85[ch];
+               if (--de < 0)
+                       return error("invalid base85 alphabet %c", ch);
+               /*
+                * Detect overflow.  The largest
+                * 5-letter possible is "|NsC0" to
+                * encode 0xffffffff, and "|NsC" gives
+                * 0x03030303 at this point (i.e.
+                * 0xffffffff = 0x03030303 * 85).
+                */
+               if (0x03030303 < acc ||
+                   0xffffffff - de < (acc *= 85))
+                       error("invalid base85 sequence %.5s", buffer-5);
+               acc += de;
+               say1(" %08x", acc);
+
+               cnt = (len < 4) ? len : 4;
+               len -= cnt;
+               do {
+                       acc = (acc << 8) | (acc >> 24);
+                       *dst++ = acc;
+               } while (--cnt);
+       }
+       say("\n");
+
+       return 0;
+}
+
+void encode_85(char *buf, unsigned char *data, int bytes)
+{
+       prep_base85();
+
+       say("encode 85");
+       while (bytes) {
+               unsigned acc = 0;
+               int cnt;
+               for (cnt = 24; cnt >= 0; cnt -= 8) {
+                       int ch = *data++;
+                       acc |= ch << cnt;
+                       if (--bytes == 0)
+                               break;
+               }
+               say1(" %08x", acc);
+               for (cnt = 4; cnt >= 0; cnt--) {
+                       int val = acc % 85;
+                       acc /= 85;
+                       buf[cnt] = en85[val];
+               }
+               buf += 5;
+       }
+       say("\n");
+
+       *buf = 0;
+}
+
+#ifdef DEBUG_85
+int main(int ac, char **av)
+{
+       char buf[1024];
+
+       if (!strcmp(av[1], "-e")) {
+               int len = strlen(av[2]);
+               encode_85(buf, av[2], len);
+               if (len <= 26) len = len + 'A' - 1;
+               else len = len + 'a' - 26 + 1;
+               printf("encoded: %c%s\n", len, buf);
+               return 0;
+       }
+       if (!strcmp(av[1], "-d")) {
+               int len = *av[2];
+               if ('A' <= len && len <= 'Z') len = len - 'A' + 1;
+               else len = len - 'a' + 26 + 1;
+               decode_85(buf, av[2]+1, len);
+               printf("decoded: %.*s\n", len, buf);
+               return 0;
+       }
+       if (!strcmp(av[1], "-t")) {
+               char t[4] = { -1,-1,-1,-1 };
+               encode_85(buf, t, 4);
+               printf("encoded: D%s\n", buf);
+               return 0;
+       }
+}
+#endif
diff --git a/blame.c b/blame.c
index 07d2d27..25d3bcf 100644 (file)
--- a/blame.c
+++ b/blame.c
 
 #define DEBUG 0
 
-static const char blame_usage[] = "[-c] [-l] [--] file [commit]\n"
+static const char blame_usage[] = "[-c] [-l] [-t] [-S <revs-file>] [--] file [commit]\n"
        "  -c, --compability Use the same output mode as git-annotate (Default: off)\n"
        "  -l, --long        Show long commit SHA1 (Default: off)\n"
+       "  -t, --time        Show raw timestamp (Default: off)\n"
+       "  -S, --revs-file   Use revisions from revs-file instead of calling git-rev-list\n"
        "  -h, --help        This message";
 
 static struct commit **blame_lines;
@@ -149,7 +151,7 @@ static void free_patch(struct patch *p)
        free(p);
 }
 
-static int get_blob_sha1_internal(unsigned char *sha1, const char *base,
+static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
                                  int baselen, const char *pathname,
                                  unsigned mode, int stage);
 
@@ -178,7 +180,7 @@ static int get_blob_sha1(struct tree *t, const char *pathname,
        return 0;
 }
 
-static int get_blob_sha1_internal(unsigned char *sha1, const char *base,
+static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
                                  int baselen, const char *pathname,
                                  unsigned mode, int stage)
 {
@@ -515,9 +517,9 @@ static int compare_tree_path(struct rev_info* revs,
        paths[1] = NULL;
 
        diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
-                             &revs->diffopt);
+                             &revs->pruning);
        ret = rev_compare_tree(revs, c1->tree, c2->tree);
-       diff_tree_release_paths(&revs->diffopt);
+       diff_tree_release_paths(&revs->pruning);
        return ret;
 }
 
@@ -531,9 +533,9 @@ static int same_tree_as_empty_path(struct rev_info *revs, struct tree* t1,
        paths[1] = NULL;
 
        diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
-                             &revs->diffopt);
+                             &revs->pruning);
        ret = rev_same_tree_as_empty(revs, t1);
-       diff_tree_release_paths(&revs->diffopt);
+       diff_tree_release_paths(&revs->pruning);
        return ret;
 }
 
@@ -680,13 +682,19 @@ static void get_commit_info(struct commit* commit, struct commit_info* ret)
        *tmp = 0;
 }
 
-static const char* format_time(unsigned long time, const char* tz_str)
+static const char* format_time(unsigned long time, const char* tz_str,
+                              int show_raw_time)
 {
        static char time_buf[128];
        time_t t = time;
        int minutes, tz;
        struct tm *tm;
 
+       if (show_raw_time) {
+               sprintf(time_buf, "%lu %s", time, tz_str);
+               return time_buf;
+       }
+
        tz = atoi(tz_str);
        minutes = tz < 0 ? -tz : tz;
        minutes = (minutes / 100)*60 + (minutes % 100);
@@ -740,6 +748,7 @@ int main(int argc, const char **argv)
        char filename_buf[256];
        int sha1_len = 8;
        int compability = 0;
+       int show_raw_time = 0;
        int options = 1;
        struct commit* start_commit;
 
@@ -768,6 +777,10 @@ int main(int argc, const char **argv)
                                  !strcmp(argv[i], "--compability")) {
                                compability = 1;
                                continue;
+                       } else if(!strcmp(argv[i], "-t") ||
+                                 !strcmp(argv[i], "--time")) {
+                               show_raw_time = 1;
+                               continue;
                        } else if(!strcmp(argv[i], "-S")) {
                                if (i + 1 < argc &&
                                    !read_ancestry(argv[i + 1], &sha1_p)) {
@@ -834,7 +847,7 @@ int main(int argc, const char **argv)
 
        args[0] = filename;
        args[1] = NULL;
-       diff_tree_setup_paths(args, &rev.diffopt);
+       diff_tree_setup_paths(args, &rev.pruning);
        prepare_revision_walk(&rev);
        process_commits(&rev, filename, &initial);
 
@@ -873,14 +886,17 @@ int main(int argc, const char **argv)
                fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
                if(compability) {
                        printf("\t(%10s\t%10s\t%d)", ci.author,
-                              format_time(ci.author_time, ci.author_tz), i+1);
+                              format_time(ci.author_time, ci.author_tz,
+                                          show_raw_time),
+                              i+1);
                } else {
                        if (found_rename)
                                printf(" %-*.*s", longest_file, longest_file,
                                       u->pathname);
                        printf(" (%-*.*s %10s %*d) ",
                               longest_author, longest_author, ci.author,
-                              format_time(ci.author_time, ci.author_tz),
+                              format_time(ci.author_time, ci.author_tz,
+                                          show_raw_time),
                               max_digits, i+1);
                }
 
diff --git a/builtin-add.c b/builtin-add.c
new file mode 100644 (file)
index 0000000..bfbbb1b
--- /dev/null
@@ -0,0 +1,189 @@
+/*
+ * "git add" builtin command
+ *
+ * Copyright (C) 2006 Linus Torvalds
+ */
+#include <fnmatch.h>
+
+#include "cache.h"
+#include "builtin.h"
+#include "dir.h"
+#include "cache-tree.h"
+
+static const char builtin_add_usage[] =
+"git-add [-n] [-v] <filepattern>...";
+
+static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
+{
+       char *seen;
+       int i, specs;
+       struct dir_entry **src, **dst;
+
+       for (specs = 0; pathspec[specs];  specs++)
+               /* nothing */;
+       seen = xmalloc(specs);
+       memset(seen, 0, specs);
+
+       src = dst = dir->entries;
+       i = dir->nr;
+       while (--i >= 0) {
+               struct dir_entry *entry = *src++;
+               if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
+                       free(entry);
+                       continue;
+               }
+               *dst++ = entry;
+       }
+       dir->nr = dst - dir->entries;
+
+       for (i = 0; i < specs; i++) {
+               struct stat st;
+               const char *match;
+               if (seen[i])
+                       continue;
+
+               /* Existing file? We must have ignored it */
+               match = pathspec[i];
+               if (!match[0] || !lstat(match, &st))
+                       continue;
+               die("pathspec '%s' did not match any files", match);
+       }
+}
+
+static void fill_directory(struct dir_struct *dir, const char **pathspec)
+{
+       const char *path, *base;
+       int baselen;
+
+       /* Set up the default git porcelain excludes */
+       memset(dir, 0, sizeof(*dir));
+       dir->exclude_per_dir = ".gitignore";
+       path = git_path("info/exclude");
+       if (!access(path, R_OK))
+               add_excludes_from_file(dir, path);
+
+       /*
+        * Calculate common prefix for the pathspec, and
+        * use that to optimize the directory walk
+        */
+       baselen = common_prefix(pathspec);
+       path = ".";
+       base = "";
+       if (baselen) {
+               char *common = xmalloc(baselen + 1);
+               common = xmalloc(baselen + 1);
+               memcpy(common, *pathspec, baselen);
+               common[baselen] = 0;
+               path = base = common;
+       }
+
+       /* Read the directory and prune it */
+       read_directory(dir, path, base, baselen);
+       if (pathspec)
+               prune_directory(dir, pathspec, baselen);
+}
+
+static int add_file_to_index(const char *path, int verbose)
+{
+       int size, namelen;
+       struct stat st;
+       struct cache_entry *ce;
+
+       if (lstat(path, &st))
+               die("%s: unable to stat (%s)", path, strerror(errno));
+
+       if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
+               die("%s: can only add regular files or symbolic links", path);
+
+       namelen = strlen(path);
+       size = cache_entry_size(namelen);
+       ce = xcalloc(1, size);
+       memcpy(ce->name, path, namelen);
+       ce->ce_flags = htons(namelen);
+       fill_stat_cache_info(ce, &st);
+
+       ce->ce_mode = create_ce_mode(st.st_mode);
+       if (!trust_executable_bit) {
+               /* If there is an existing entry, pick the mode bits
+                * from it.
+                */
+               int pos = cache_name_pos(path, namelen);
+               if (pos >= 0)
+                       ce->ce_mode = active_cache[pos]->ce_mode;
+       }
+
+       if (index_path(ce->sha1, path, &st, 1))
+               die("unable to index file %s", path);
+       if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD))
+               die("unable to add %s to index",path);
+       if (verbose)
+               printf("add '%s'\n", path);
+       cache_tree_invalidate_path(active_cache_tree, path);
+       return 0;
+}
+
+static struct lock_file lock_file;
+
+int cmd_add(int argc, const char **argv, char **envp)
+{
+       int i, newfd;
+       int verbose = 0, show_only = 0;
+       const char *prefix = setup_git_directory();
+       const char **pathspec;
+       struct dir_struct dir;
+
+       git_config(git_default_config);
+
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file());
+       if (newfd < 0)
+               die("unable to create new index file");
+
+       if (read_cache() < 0)
+               die("index file corrupt");
+
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+
+               if (arg[0] != '-')
+                       break;
+               if (!strcmp(arg, "--")) {
+                       i++;
+                       break;
+               }
+               if (!strcmp(arg, "-n")) {
+                       show_only = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-v")) {
+                       verbose = 1;
+                       continue;
+               }
+               die(builtin_add_usage);
+       }
+       git_config(git_default_config);
+       pathspec = get_pathspec(prefix, argv + i);
+
+       fill_directory(&dir, pathspec);
+
+       if (show_only) {
+               const char *sep = "", *eof = "";
+               for (i = 0; i < dir.nr; i++) {
+                       printf("%s%s", sep, dir.entries[i]->name);
+                       sep = " ";
+                       eof = "\n";
+               }
+               fputs(eof, stdout);
+               return 0;
+       }
+
+       for (i = 0; i < dir.nr; i++)
+               add_file_to_index(dir.entries[i]->name, verbose);
+
+       if (active_cache_changed) {
+               if (write_cache(newfd, active_cache, active_nr) ||
+                   commit_lock_file(&lock_file))
+                       die("Unable to write new index file");
+       }
+
+       return 0;
+}
diff --git a/builtin-apply.c b/builtin-apply.c
new file mode 100644 (file)
index 0000000..e113c74
--- /dev/null
@@ -0,0 +1,2324 @@
+/*
+ * apply.c
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ *
+ * This applies patches on top of some (arbitrary) version of the SCM.
+ *
+ */
+#include <fnmatch.h>
+#include "cache.h"
+#include "cache-tree.h"
+#include "quote.h"
+#include "blob.h"
+#include "delta.h"
+#include "builtin.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
+//  --numstat does numeric diffstat, and doesn't actually apply
+//  --index-info shows the old and new index info for paths if available.
+//  --index updates the cache as well.
+//  --cached updates only the cache without ever touching the working tree.
+//
+static const char *prefix;
+static int prefix_length = -1;
+static int newfd = -1;
+
+static int p_value = 1;
+static int allow_binary_replacement = 0;
+static int check_index = 0;
+static int write_index = 0;
+static int cached = 0;
+static int diffstat = 0;
+static int numstat = 0;
+static int summary = 0;
+static int check = 0;
+static int apply = 1;
+static int no_add = 0;
+static int show_index_info = 0;
+static int line_termination = '\n';
+static unsigned long p_context = -1;
+static const char apply_usage[] =
+"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
+
+static enum whitespace_eol {
+       nowarn_whitespace,
+       warn_on_whitespace,
+       error_on_whitespace,
+       strip_whitespace,
+} new_whitespace = warn_on_whitespace;
+static int whitespace_error = 0;
+static int squelch_whitespace_errors = 5;
+static int applied_after_stripping = 0;
+static const char *patch_input_file = NULL;
+
+static void parse_whitespace_option(const char *option)
+{
+       if (!option) {
+               new_whitespace = warn_on_whitespace;
+               return;
+       }
+       if (!strcmp(option, "warn")) {
+               new_whitespace = warn_on_whitespace;
+               return;
+       }
+       if (!strcmp(option, "nowarn")) {
+               new_whitespace = nowarn_whitespace;
+               return;
+       }
+       if (!strcmp(option, "error")) {
+               new_whitespace = error_on_whitespace;
+               return;
+       }
+       if (!strcmp(option, "error-all")) {
+               new_whitespace = error_on_whitespace;
+               squelch_whitespace_errors = 0;
+               return;
+       }
+       if (!strcmp(option, "strip")) {
+               new_whitespace = strip_whitespace;
+               return;
+       }
+       die("unrecognized whitespace option '%s'", option);
+}
+
+static void set_default_whitespace_mode(const char *whitespace_option)
+{
+       if (!whitespace_option && !apply_default_whitespace) {
+               new_whitespace = (apply
+                                 ? warn_on_whitespace
+                                 : nowarn_whitespace);
+       }
+}
+
+/*
+ * For "diff-stat" like behaviour, we keep track of the biggest change
+ * we've seen, and the longest filename. That allows us to do simple
+ * scaling.
+ */
+static int max_change, max_len;
+
+/*
+ * Various "current state", notably line numbers and what
+ * file (and how) we're patching right now.. The "is_xxxx"
+ * things are flags, where -1 means "don't know yet".
+ */
+static int linenr = 1;
+
+struct fragment {
+       unsigned long leading, trailing;
+       unsigned long oldpos, oldlines;
+       unsigned long newpos, newlines;
+       const char *patch;
+       int size;
+       struct fragment *next;
+};
+
+struct patch {
+       char *new_name, *old_name, *def_name;
+       unsigned int old_mode, new_mode;
+       int is_rename, is_copy, is_new, is_delete, is_binary;
+#define BINARY_DELTA_DEFLATED 1
+#define BINARY_LITERAL_DEFLATED 2
+       unsigned long deflate_origlen;
+       int lines_added, lines_deleted;
+       int score;
+       struct fragment *fragments;
+       char *result;
+       unsigned long resultsize;
+       char old_sha1_prefix[41];
+       char new_sha1_prefix[41];
+       struct patch *next;
+};
+
+#define CHUNKSIZE (8192)
+#define SLOP (16)
+
+static void *read_patch_file(int fd, unsigned long *sizep)
+{
+       unsigned long size = 0, alloc = CHUNKSIZE;
+       void *buffer = xmalloc(alloc);
+
+       for (;;) {
+               int nr = alloc - size;
+               if (nr < 1024) {
+                       alloc += CHUNKSIZE;
+                       buffer = xrealloc(buffer, alloc);
+                       nr = alloc - size;
+               }
+               nr = xread(fd, buffer + size, nr);
+               if (!nr)
+                       break;
+               if (nr < 0)
+                       die("git-apply: read returned %s", strerror(errno));
+               size += nr;
+       }
+       *sizep = size;
+
+       /*
+        * Make sure that we have some slop in the buffer
+        * so that we can do speculative "memcmp" etc, and
+        * see to it that it is NUL-filled.
+        */
+       if (alloc < size + SLOP)
+               buffer = xrealloc(buffer, size + SLOP);
+       memset(buffer + size, 0, SLOP);
+       return buffer;
+}
+
+static unsigned long linelen(const char *buffer, unsigned long size)
+{
+       unsigned long len = 0;
+       while (size--) {
+               len++;
+               if (*buffer++ == '\n')
+                       break;
+       }
+       return len;
+}
+
+static int is_dev_null(const char *str)
+{
+       return !memcmp("/dev/null", str, 9) && isspace(str[9]);
+}
+
+#define TERM_SPACE     1
+#define TERM_TAB       2
+
+static int name_terminate(const char *name, int namelen, int c, int terminate)
+{
+       if (c == ' ' && !(terminate & TERM_SPACE))
+               return 0;
+       if (c == '\t' && !(terminate & TERM_TAB))
+               return 0;
+
+       return 1;
+}
+
+static char * find_name(const char *line, char *def, int p_value, int terminate)
+{
+       int len;
+       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;
+
+               if (isspace(c)) {
+                       if (c == '\n')
+                               break;
+                       if (name_terminate(start, line-start, c, terminate))
+                               break;
+               }
+               line++;
+               if (c == '/' && !--p_value)
+                       start = line;
+       }
+       if (!start)
+               return def;
+       len = line - start;
+       if (!len)
+               return def;
+
+       /*
+        * Generally we prefer the shorter name, especially
+        * if the other one is just a variation of that with
+        * something else tacked on to the end (ie "file.orig"
+        * or "file~").
+        */
+       if (def) {
+               int deflen = strlen(def);
+               if (deflen < len && !strncmp(start, def, deflen))
+                       return def;
+       }
+
+       name = xmalloc(len + 1);
+       memcpy(name, start, len);
+       name[len] = 0;
+       free(def);
+       return name;
+}
+
+/*
+ * 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.
+ */
+static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
+{
+       char *name;
+
+       first += 4;     // skip "--- "
+       second += 4;    // skip "+++ "
+       if (is_dev_null(first)) {
+               patch->is_new = 1;
+               patch->is_delete = 0;
+               name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
+               patch->new_name = name;
+       } else if (is_dev_null(second)) {
+               patch->is_new = 0;
+               patch->is_delete = 1;
+               name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
+               patch->old_name = name;
+       } else {
+               name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
+               name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
+               patch->old_name = patch->new_name = name;
+       }
+       if (!name)
+               die("unable to find filename in patch at line %d", linenr);
+}
+
+static int gitdiff_hdrend(const char *line, struct patch *patch)
+{
+       return -1;
+}
+
+/*
+ * We're anal about diff header consistency, to make
+ * sure that we don't end up having strange ambiguous
+ * patches floating around.
+ *
+ * As a result, gitdiff_{old|new}name() will check
+ * their names against any previous information, just
+ * to make sure..
+ */
+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, 0);
+
+       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);
+               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;
+       }
+       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)
+{
+       patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
+       return 0;
+}
+
+static int gitdiff_newname(const char *line, struct patch *patch)
+{
+       patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
+       return 0;
+}
+
+static int gitdiff_oldmode(const char *line, struct patch *patch)
+{
+       patch->old_mode = strtoul(line, NULL, 8);
+       return 0;
+}
+
+static int gitdiff_newmode(const char *line, struct patch *patch)
+{
+       patch->new_mode = strtoul(line, NULL, 8);
+       return 0;
+}
+
+static int gitdiff_delete(const char *line, struct patch *patch)
+{
+       patch->is_delete = 1;
+       patch->old_name = patch->def_name;
+       return gitdiff_oldmode(line, patch);
+}
+
+static int gitdiff_newfile(const char *line, struct patch *patch)
+{
+       patch->is_new = 1;
+       patch->new_name = patch->def_name;
+       return gitdiff_newmode(line, patch);
+}
+
+static int gitdiff_copysrc(const char *line, struct patch *patch)
+{
+       patch->is_copy = 1;
+       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, 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, 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, 0, 0);
+       return 0;
+}
+
+static int gitdiff_similarity(const char *line, struct patch *patch)
+{
+       if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
+               patch->score = 0;
+       return 0;
+}
+
+static int gitdiff_dissimilarity(const char *line, struct patch *patch)
+{
+       if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
+               patch->score = 0;
+       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.
+ */
+static int gitdiff_unrecognized(const char *line, struct patch *patch)
+{
+       return -1;
+}
+
+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;
+       const char *name;
+       const char *second = NULL;
+
+       line += strlen("diff --git ");
+       llen -= strlen("diff --git ");
+
+       if (*line == '"') {
+               const char *cp;
+               char *first = unquote_c_style(line, &second);
+               if (!first)
+                       return NULL;
+
+               /* 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;
+       }
+
+       /* 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.
+        */
+       for (len = 0 ; ; len++) {
+               char c = name[len];
+
+               switch (c) {
+               default:
+                       continue;
+               case '\n':
+                       return NULL;
+               case '\t': case ' ':
+                       second = name+len;
+                       for (;;) {
+                               char c = *second++;
+                               if (c == '\n')
+                                       return NULL;
+                               if (c == '/')
+                                       break;
+                       }
+                       if (second[len] == '\n' && !memcmp(name, second, len)) {
+                               char *ret = xmalloc(len + 1);
+                               memcpy(ret, name, len);
+                               ret[len] = 0;
+                               return ret;
+                       }
+               }
+       }
+       return NULL;
+}
+
+/* Verify that we recognize the lines following a git header */
+static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
+{
+       unsigned long offset;
+
+       /* A git diff has explicit new/delete information, so we don't guess */
+       patch->is_new = 0;
+       patch->is_delete = 0;
+
+       /*
+        * Some things may not have the old name in the
+        * rest of the headers anywhere (pure mode changes,
+        * or removing or adding empty files), so we get
+        * the default name from the header.
+        */
+       patch->def_name = git_header_name(line, len);
+
+       line += len;
+       size -= len;
+       linenr++;
+       for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
+               static const struct opentry {
+                       const char *str;
+                       int (*fn)(const char *, struct patch *);
+               } optable[] = {
+                       { "@@ -", gitdiff_hdrend },
+                       { "--- ", gitdiff_oldname },
+                       { "+++ ", gitdiff_newname },
+                       { "old mode ", gitdiff_oldmode },
+                       { "new mode ", gitdiff_newmode },
+                       { "deleted file mode ", gitdiff_delete },
+                       { "new file mode ", gitdiff_newfile },
+                       { "copy from ", gitdiff_copysrc },
+                       { "copy to ", gitdiff_copydst },
+                       { "rename old ", gitdiff_renamesrc },
+                       { "rename new ", gitdiff_renamedst },
+                       { "rename from ", gitdiff_renamesrc },
+                       { "rename to ", gitdiff_renamedst },
+                       { "similarity index ", gitdiff_similarity },
+                       { "dissimilarity index ", gitdiff_dissimilarity },
+                       { "index ", gitdiff_index },
+                       { "", gitdiff_unrecognized },
+               };
+               int i;
+
+               len = linelen(line, size);
+               if (!len || line[len-1] != '\n')
+                       break;
+               for (i = 0; i < ARRAY_SIZE(optable); i++) {
+                       const struct opentry *p = optable + i;
+                       int oplen = strlen(p->str);
+                       if (len < oplen || memcmp(p->str, line, oplen))
+                               continue;
+                       if (p->fn(line + oplen, patch) < 0)
+                               return offset;
+                       break;
+               }
+       }
+
+       return offset;
+}
+
+static int parse_num(const char *line, unsigned long *p)
+{
+       char *ptr;
+
+       if (!isdigit(*line))
+               return 0;
+       *p = strtoul(line, &ptr, 10);
+       return ptr - line;
+}
+
+static int parse_range(const char *line, int len, int offset, const char *expect,
+                       unsigned long *p1, unsigned long *p2)
+{
+       int digits, ex;
+
+       if (offset < 0 || offset >= len)
+               return -1;
+       line += offset;
+       len -= offset;
+
+       digits = parse_num(line, p1);
+       if (!digits)
+               return -1;
+
+       offset += digits;
+       line += digits;
+       len -= digits;
+
+       *p2 = 1;
+       if (*line == ',') {
+               digits = parse_num(line+1, p2);
+               if (!digits)
+                       return -1;
+
+               offset += digits+1;
+               line += digits+1;
+               len -= digits+1;
+       }
+
+       ex = strlen(expect);
+       if (ex > len)
+               return -1;
+       if (memcmp(line, expect, ex))
+               return -1;
+
+       return offset + ex;
+}
+
+/*
+ * Parse a unified diff fragment header of the
+ * form "@@ -a,b +c,d @@"
+ */
+static int parse_fragment_header(char *line, int len, struct fragment *fragment)
+{
+       int offset;
+
+       if (!len || line[len-1] != '\n')
+               return -1;
+
+       /* Figure out the number of lines in a fragment */
+       offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
+       offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
+
+       return offset;
+}
+
+static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
+{
+       unsigned long offset, len;
+
+       patch->is_rename = patch->is_copy = 0;
+       patch->is_new = patch->is_delete = -1;
+       patch->old_mode = patch->new_mode = 0;
+       patch->old_name = patch->new_name = NULL;
+       for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
+               unsigned long nextlen;
+
+               len = linelen(line, size);
+               if (!len)
+                       break;
+
+               /* Testing this early allows us to take a few shortcuts.. */
+               if (len < 6)
+                       continue;
+
+               /*
+                * Make sure we don't find any unconnected patch fragmants.
+                * That's a sign that we didn't find a header, and that a
+                * patch has become corrupted/broken up.
+                */
+               if (!memcmp("@@ -", line, 4)) {
+                       struct fragment dummy;
+                       if (parse_fragment_header(line, len, &dummy) < 0)
+                               continue;
+                       error("patch fragment without header at line %d: %.*s", linenr, (int)len-1, line);
+               }
+
+               if (size < len + 6)
+                       break;
+
+               /*
+                * Git patch? It might not have a real patch, just a rename
+                * or mode change, so we handle that specially
+                */
+               if (!memcmp("diff --git ", line, 11)) {
+                       int git_hdr_len = parse_git_header(line, len, size, patch);
+                       if (git_hdr_len <= len)
+                               continue;
+                       if (!patch->old_name && !patch->new_name) {
+                               if (!patch->def_name)
+                                       die("git diff header lacks filename information (line %d)", linenr);
+                               patch->old_name = patch->new_name = patch->def_name;
+                       }
+                       *hdrsize = git_hdr_len;
+                       return offset;
+               }
+
+               /** --- followed by +++ ? */
+               if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
+                       continue;
+
+               /*
+                * We only accept unified patches, so we want it to
+                * at least have "@@ -a,b +c,d @@\n", which is 14 chars
+                * minimum
+                */
+               nextlen = linelen(line + len, size - len);
+               if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
+                       continue;
+
+               /* Ok, we'll consider it a patch */
+               parse_traditional_patch(line, line+len, patch);
+               *hdrsize = len + nextlen;
+               linenr += 2;
+               return offset;
+       }
+       return -1;
+}
+
+/*
+ * Parse a unified diff. Note that this really needs
+ * to parse each fragment separately, since the only
+ * way to know the difference between a "---" that is
+ * part of a patch, and a "---" that starts the next
+ * patch is to look at the line counts..
+ */
+static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
+{
+       int added, deleted;
+       int len = linelen(line, size), offset;
+       unsigned long oldlines, newlines;
+       unsigned long leading, trailing;
+
+       offset = parse_fragment_header(line, len, fragment);
+       if (offset < 0)
+               return -1;
+       oldlines = fragment->oldlines;
+       newlines = fragment->newlines;
+       leading = 0;
+       trailing = 0;
+
+       if (patch->is_new < 0) {
+               patch->is_new =  !oldlines;
+               if (!oldlines)
+                       patch->old_name = NULL;
+       }
+       if (patch->is_delete < 0) {
+               patch->is_delete = !newlines;
+               if (!newlines)
+                       patch->new_name = NULL;
+       }
+
+       if (patch->is_new && oldlines)
+               return error("new file depends on old contents");
+       if (patch->is_delete != !newlines) {
+               if (newlines)
+                       return error("deleted file still has contents");
+               fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name);
+       }
+
+       /* Parse the thing.. */
+       line += len;
+       size -= len;
+       linenr++;
+       added = deleted = 0;
+       for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
+               if (!oldlines && !newlines)
+                       break;
+               len = linelen(line, size);
+               if (!len || line[len-1] != '\n')
+                       return -1;
+               switch (*line) {
+               default:
+                       return -1;
+               case ' ':
+                       oldlines--;
+                       newlines--;
+                       if (!deleted && !added)
+                               leading++;
+                       trailing++;
+                       break;
+               case '-':
+                       deleted++;
+                       oldlines--;
+                       trailing = 0;
+                       break;
+               case '+':
+                       /*
+                        * We know len is at least two, since we have a '+' and
+                        * we checked that the last character was a '\n' above.
+                        * That is, an addition of an empty line would check
+                        * the '+' here.  Sneaky...
+                        */
+                       if ((new_whitespace != nowarn_whitespace) &&
+                           isspace(line[len-2])) {
+                               whitespace_error++;
+                               if (squelch_whitespace_errors &&
+                                   squelch_whitespace_errors <
+                                   whitespace_error)
+                                       ;
+                               else {
+                                       fprintf(stderr, "Adds trailing whitespace.\n%s:%d:%.*s\n",
+                                               patch_input_file,
+                                               linenr, len-2, line+1);
+                               }
+                       }
+                       added++;
+                       newlines--;
+                       trailing = 0;
+                       break;
+
+                /* 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 "\ ".
+                * 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;
+                       break;
+               }
+       }
+       if (oldlines || newlines)
+               return -1;
+       fragment->leading = leading;
+       fragment->trailing = trailing;
+
+       /* If a fragment ends with an incomplete line, we failed to include
+        * it in the above loop because we hit oldlines == newlines == 0
+        * before seeing it.
+        */
+       if (12 < size && !memcmp(line, "\\ ", 2))
+               offset += linelen(line, size);
+
+       patch->lines_added += added;
+       patch->lines_deleted += deleted;
+       return offset;
+}
+
+static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
+{
+       unsigned long offset = 0;
+       struct fragment **fragp = &patch->fragments;
+
+       while (size > 4 && !memcmp(line, "@@ -", 4)) {
+               struct fragment *fragment;
+               int len;
+
+               fragment = xcalloc(1, sizeof(*fragment));
+               len = parse_fragment(line, size, patch, fragment);
+               if (len <= 0)
+                       die("corrupt patch at line %d", linenr);
+
+               fragment->patch = line;
+               fragment->size = len;
+
+               *fragp = fragment;
+               fragp = &fragment->next;
+
+               offset += len;
+               line += len;
+               size -= len;
+       }
+       return offset;
+}
+
+static inline int metadata_changes(struct patch *patch)
+{
+       return  patch->is_rename > 0 ||
+               patch->is_copy > 0 ||
+               patch->is_new > 0 ||
+               patch->is_delete ||
+               (patch->old_mode && patch->new_mode &&
+                patch->old_mode != patch->new_mode);
+}
+
+static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
+{
+       /* We have read "GIT binary patch\n"; what follows is a line
+        * that says the patch method (currently, either "deflated
+        * literal" or "deflated delta") and the length of data before
+        * deflating; a sequence of 'length-byte' followed by base-85
+        * encoded data follows.
+        *
+        * Each 5-byte sequence of base-85 encodes up to 4 bytes,
+        * and we would limit the patch line to 66 characters,
+        * so one line can fit up to 13 groups that would decode
+        * to 52 bytes max.  The length byte 'A'-'Z' corresponds
+        * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
+        * The end of binary is signalled with an empty line.
+        */
+       int llen, used;
+       struct fragment *fragment;
+       char *data = NULL;
+
+       patch->fragments = fragment = xcalloc(1, sizeof(*fragment));
+
+       /* Grab the type of patch */
+       llen = linelen(buffer, size);
+       used = llen;
+       linenr++;
+
+       if (!strncmp(buffer, "delta ", 6)) {
+               patch->is_binary = BINARY_DELTA_DEFLATED;
+               patch->deflate_origlen = strtoul(buffer + 6, NULL, 10);
+       }
+       else if (!strncmp(buffer, "literal ", 8)) {
+               patch->is_binary = BINARY_LITERAL_DEFLATED;
+               patch->deflate_origlen = strtoul(buffer + 8, NULL, 10);
+       }
+       else
+               return error("unrecognized binary patch at line %d: %.*s",
+                            linenr-1, llen-1, buffer);
+       buffer += llen;
+       while (1) {
+               int byte_length, max_byte_length, newsize;
+               llen = linelen(buffer, size);
+               used += llen;
+               linenr++;
+               if (llen == 1)
+                       break;
+               /* Minimum line is "A00000\n" which is 7-byte long,
+                * and the line length must be multiple of 5 plus 2.
+                */
+               if ((llen < 7) || (llen-2) % 5)
+                       goto corrupt;
+               max_byte_length = (llen - 2) / 5 * 4;
+               byte_length = *buffer;
+               if ('A' <= byte_length && byte_length <= 'Z')
+                       byte_length = byte_length - 'A' + 1;
+               else if ('a' <= byte_length && byte_length <= 'z')
+                       byte_length = byte_length - 'a' + 27;
+               else
+                       goto corrupt;
+               /* if the input length was not multiple of 4, we would
+                * have filler at the end but the filler should never
+                * exceed 3 bytes
+                */
+               if (max_byte_length < byte_length ||
+                   byte_length <= max_byte_length - 4)
+                       goto corrupt;
+               newsize = fragment->size + byte_length;
+               data = xrealloc(data, newsize);
+               if (decode_85(data + fragment->size,
+                             buffer + 1,
+                             byte_length))
+                       goto corrupt;
+               fragment->size = newsize;
+               buffer += llen;
+               size -= llen;
+       }
+       fragment->patch = data;
+       return used;
+ corrupt:
+       return error("corrupt binary patch at line %d: %.*s",
+                    linenr-1, llen-1, buffer);
+}
+
+static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
+{
+       int hdrsize, patchsize;
+       int offset = find_header(buffer, size, &hdrsize, patch);
+
+       if (offset < 0)
+               return offset;
+
+       patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
+
+       if (!patchsize) {
+               static const char *binhdr[] = {
+                       "Binary files ",
+                       "Files ",
+                       NULL,
+               };
+               static const char git_binary[] = "GIT binary patch\n";
+               int i;
+               int hd = hdrsize + offset;
+               unsigned long llen = linelen(buffer + hd, size - hd);
+
+               if (llen == sizeof(git_binary) - 1 &&
+                   !memcmp(git_binary, buffer + hd, llen)) {
+                       int used;
+                       linenr++;
+                       used = parse_binary(buffer + hd + llen,
+                                           size - hd - llen, patch);
+                       if (used)
+                               patchsize = used + llen;
+                       else
+                               patchsize = 0;
+               }
+               else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
+                       for (i = 0; binhdr[i]; i++) {
+                               int len = strlen(binhdr[i]);
+                               if (len < size - hd &&
+                                   !memcmp(binhdr[i], buffer + hd, len)) {
+                                       linenr++;
+                                       patch->is_binary = 1;
+                                       patchsize = llen;
+                                       break;
+                               }
+                       }
+               }
+
+               /* Empty patch cannot be applied if:
+                * - it is a binary patch and we do not do binary_replace, or
+                * - text patch without metadata change
+                */
+               if ((apply || check) &&
+                   (patch->is_binary
+                    ? !allow_binary_replacement
+                    : !metadata_changes(patch)))
+                       die("patch with only garbage at line %d", linenr);
+       }
+
+       return offset + hdrsize + patchsize;
+}
+
+static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
+static const char minuses[]= "----------------------------------------------------------------------";
+
+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
+        */
+       len = strlen(name);
+       max = max_len;
+       if (max > 50)
+               max = 50;
+       if (len > max) {
+               char *slash;
+               prefix = "...";
+               max -= 3;
+               name += len - max;
+               slash = strchr(name, '/');
+               if (slash)
+                       name = slash;
+       }
+       len = max;
+
+       /*
+        * scale the add/delete
+        */
+       max = max_change;
+       if (max + len > 70)
+               max = 70 - len;
+
+       add = patch->lines_added;
+       del = patch->lines_deleted;
+       total = add + del;
+
+       if (max_change > 0) {
+               total = (total * max + max_change / 2) / max_change;
+               add = (add * max + max_change / 2) / max_change;
+               del = total - add;
+       }
+       if (patch->is_binary)
+               printf(" %s%-*s |  Bin\n", prefix, len, name);
+       else
+               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)
+{
+       int fd;
+       unsigned long got;
+
+       switch (st->st_mode & S_IFMT) {
+       case S_IFLNK:
+               return readlink(path, buf, 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, buf + got, size - got);
+                       if (ret <= 0)
+                               break;
+                       got += ret;
+               }
+               close(fd);
+               return got;
+
+       default:
+               return -1;
+       }
+}
+
+static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines)
+{
+       int i;
+       unsigned long start, backwards, forwards;
+
+       if (fragsize > size)
+               return -1;
+
+       start = 0;
+       if (line > 1) {
+               unsigned long offset = 0;
+               i = line-1;
+               while (offset + fragsize <= size) {
+                       if (buf[offset++] == '\n') {
+                               start = offset;
+                               if (!--i)
+                                       break;
+                       }
+               }
+       }
+
+       /* Exact line number? */
+       if (!memcmp(buf + start, fragment, fragsize))
+               return start;
+
+       /*
+        * There's probably some smart way to do this, but I'll leave
+        * that to the smart and beautiful people. I'm simple and stupid.
+        */
+       backwards = start;
+       forwards = start;
+       for (i = 0; ; i++) {
+               unsigned long try;
+               int n;
+
+               /* "backward" */
+               if (i & 1) {
+                       if (!backwards) {
+                               if (forwards + fragsize > size)
+                                       break;
+                               continue;
+                       }
+                       do {
+                               --backwards;
+                       } while (backwards && buf[backwards-1] != '\n');
+                       try = backwards;
+               } else {
+                       while (forwards + fragsize <= size) {
+                               if (buf[forwards++] == '\n')
+                                       break;
+                       }
+                       try = forwards;
+               }
+
+               if (try + fragsize > size)
+                       continue;
+               if (memcmp(buf + try, fragment, fragsize))
+                       continue;
+               n = (i >> 1)+1;
+               if (i & 1)
+                       n = -n;
+               *lines = n;
+               return try;
+       }
+
+       /*
+        * We should start searching forward and backward.
+        */
+       return -1;
+}
+
+static void remove_first_line(const char **rbuf, int *rsize)
+{
+       const char *buf = *rbuf;
+       int size = *rsize;
+       unsigned long offset;
+       offset = 0;
+       while (offset <= size) {
+               if (buf[offset++] == '\n')
+                       break;
+       }
+       *rsize = size - offset;
+       *rbuf = buf + offset;
+}
+
+static void remove_last_line(const char **rbuf, int *rsize)
+{
+       const char *buf = *rbuf;
+       int size = *rsize;
+       unsigned long offset;
+       offset = size - 1;
+       while (offset > 0) {
+               if (buf[--offset] == '\n')
+                       break;
+       }
+       *rsize = offset + 1;
+}
+
+struct buffer_desc {
+       char *buffer;
+       unsigned long size;
+       unsigned long alloc;
+};
+
+static int apply_line(char *output, const char *patch, int plen)
+{
+       /* plen is number of bytes to be copied from patch,
+        * starting at patch+1 (patch[0] is '+').  Typically
+        * patch[plen] is '\n'.
+        */
+       int add_nl_to_tail = 0;
+       if ((new_whitespace == strip_whitespace) &&
+           1 < plen && isspace(patch[plen-1])) {
+               if (patch[plen] == '\n')
+                       add_nl_to_tail = 1;
+               plen--;
+               while (0 < plen && isspace(patch[plen]))
+                       plen--;
+               applied_after_stripping++;
+       }
+       memcpy(output, patch + 1, plen);
+       if (add_nl_to_tail)
+               output[plen++] = '\n';
+       return plen;
+}
+
+static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
+{
+       int match_beginning, match_end;
+       char *buf = desc->buffer;
+       const char *patch = frag->patch;
+       int offset, size = frag->size;
+       char *old = xmalloc(size);
+       char *new = xmalloc(size);
+       const char *oldlines, *newlines;
+       int oldsize = 0, newsize = 0;
+       unsigned long leading, trailing;
+       int pos, lines;
+
+       while (size > 0) {
+               int len = linelen(patch, size);
+               int plen;
+
+               if (!len)
+                       break;
+
+               /*
+                * "plen" is how much of the line we should use for
+                * the actual patch data. Normally we just remove the
+                * first character on the line, but if the line is
+                * followed by "\ No newline", then we also remove the
+                * last one (which is the newline, of course).
+                */
+               plen = len-1;
+               if (len < size && patch[len] == '\\')
+                       plen--;
+               switch (*patch) {
+               case ' ':
+               case '-':
+                       memcpy(old + oldsize, patch + 1, plen);
+                       oldsize += plen;
+                       if (*patch == '-')
+                               break;
+               /* Fall-through for ' ' */
+               case '+':
+                       if (*patch != '+' || !no_add)
+                               newsize += apply_line(new + newsize, patch,
+                                                     plen);
+                       break;
+               case '@': case '\\':
+                       /* Ignore it, we already handled it */
+                       break;
+               default:
+                       return -1;
+               }
+               patch += len;
+               size -= len;
+       }
+
+#ifdef NO_ACCURATE_DIFF
+       if (oldsize > 0 && old[oldsize - 1] == '\n' &&
+                       newsize > 0 && new[newsize - 1] == '\n') {
+               oldsize--;
+               newsize--;
+       }
+#endif
+
+       oldlines = old;
+       newlines = new;
+       leading = frag->leading;
+       trailing = frag->trailing;
+
+       /*
+        * If we don't have any leading/trailing data in the patch,
+        * we want it to match at the beginning/end of the file.
+        */
+       match_beginning = !leading && (frag->oldpos == 1);
+       match_end = !trailing;
+
+       lines = 0;
+       pos = frag->newpos;
+       for (;;) {
+               offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines);
+               if (match_end && offset + oldsize != desc->size)
+                       offset = -1;
+               if (match_beginning && offset)
+                       offset = -1;
+               if (offset >= 0) {
+                       int diff = newsize - oldsize;
+                       unsigned long size = desc->size + diff;
+                       unsigned long alloc = desc->alloc;
+
+                       /* Warn if it was necessary to reduce the number
+                        * of context lines.
+                        */
+                       if ((leading != frag->leading) || (trailing != frag->trailing))
+                               fprintf(stderr, "Context reduced to (%ld/%ld) to apply fragment at %d\n",
+                                       leading, trailing, pos + lines);
+
+                       if (size > alloc) {
+                               alloc = size + 8192;
+                               desc->alloc = alloc;
+                               buf = xrealloc(buf, alloc);
+                               desc->buffer = buf;
+                       }
+                       desc->size = size;
+                       memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
+                       memcpy(buf + offset, newlines, newsize);
+                       offset = 0;
+
+                       break;
+               }
+
+               /* Am I at my context limits? */
+               if ((leading <= p_context) && (trailing <= p_context))
+                       break;
+               if (match_beginning || match_end) {
+                       match_beginning = match_end = 0;
+                       continue;
+               }
+               /* Reduce the number of context lines
+                * Reduce both leading and trailing if they are equal
+                * otherwise just reduce the larger context.
+                */
+               if (leading >= trailing) {
+                       remove_first_line(&oldlines, &oldsize);
+                       remove_first_line(&newlines, &newsize);
+                       pos--;
+                       leading--;
+               }
+               if (trailing > leading) {
+                       remove_last_line(&oldlines, &oldsize);
+                       remove_last_line(&newlines, &newsize);
+                       trailing--;
+               }
+       }
+
+       free(old);
+       free(new);
+       return offset;
+}
+
+static char *inflate_it(const void *data, unsigned long size,
+                       unsigned long inflated_size)
+{
+       z_stream stream;
+       void *out;
+       int st;
+
+       memset(&stream, 0, sizeof(stream));
+
+       stream.next_in = (unsigned char *)data;
+       stream.avail_in = size;
+       stream.next_out = out = xmalloc(inflated_size);
+       stream.avail_out = inflated_size;
+       inflateInit(&stream);
+       st = inflate(&stream, Z_FINISH);
+       if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
+               free(out);
+               return NULL;
+       }
+       return out;
+}
+
+static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
+{
+       unsigned long dst_size;
+       struct fragment *fragment = patch->fragments;
+       void *data;
+       void *result;
+
+       data = inflate_it(fragment->patch, fragment->size,
+                         patch->deflate_origlen);
+       if (!data)
+               return error("corrupt patch data");
+       switch (patch->is_binary) {
+       case BINARY_DELTA_DEFLATED:
+               result = patch_delta(desc->buffer, desc->size,
+                                    data,
+                                    patch->deflate_origlen,
+                                    &dst_size);
+               free(desc->buffer);
+               desc->buffer = result;
+               free(data);
+               break;
+       case BINARY_LITERAL_DEFLATED:
+               free(desc->buffer);
+               desc->buffer = data;
+               dst_size = patch->deflate_origlen;
+               break;
+       }
+       if (!desc->buffer)
+               return -1;
+       desc->size = desc->alloc = dst_size;
+       return 0;
+}
+
+static int apply_binary(struct buffer_desc *desc, struct patch *patch)
+{
+       const char *name = patch->old_name ? patch->old_name : patch->new_name;
+       unsigned char sha1[20];
+       unsigned char hdr[50];
+       int hdrlen;
+
+       if (!allow_binary_replacement)
+               return error("cannot apply binary patch to '%s' "
+                            "without --allow-binary-replacement",
+                            name);
+
+       /* For safety, we require patch index line to contain
+        * full 40-byte textual SHA1 for old and new, at least for now.
+        */
+       if (strlen(patch->old_sha1_prefix) != 40 ||
+           strlen(patch->new_sha1_prefix) != 40 ||
+           get_sha1_hex(patch->old_sha1_prefix, sha1) ||
+           get_sha1_hex(patch->new_sha1_prefix, sha1))
+               return error("cannot apply binary patch to '%s' "
+                            "without full index line", name);
+
+       if (patch->old_name) {
+               /* See if the old one matches what the patch
+                * applies to.
+                */
+               write_sha1_file_prepare(desc->buffer, desc->size,
+                                       blob_type, sha1, hdr, &hdrlen);
+               if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
+                       return error("the patch applies to '%s' (%s), "
+                                    "which does not match the "
+                                    "current contents.",
+                                    name, sha1_to_hex(sha1));
+       }
+       else {
+               /* Otherwise, the old one must be empty. */
+               if (desc->size)
+                       return error("the patch applies to an empty "
+                                    "'%s' but it is not empty", name);
+       }
+
+       get_sha1_hex(patch->new_sha1_prefix, sha1);
+       if (!memcmp(sha1, null_sha1, 20)) {
+               free(desc->buffer);
+               desc->alloc = desc->size = 0;
+               desc->buffer = NULL;
+               return 0; /* deletion patch */
+       }
+
+       if (has_sha1_file(sha1)) {
+               /* We already have the postimage */
+               char type[10];
+               unsigned long size;
+
+               free(desc->buffer);
+               desc->buffer = read_sha1_file(sha1, type, &size);
+               if (!desc->buffer)
+                       return error("the necessary postimage %s for "
+                                    "'%s' cannot be read",
+                                    patch->new_sha1_prefix, name);
+               desc->alloc = desc->size = size;
+       }
+       else {
+               /* We have verified desc matches the preimage;
+                * apply the patch data to it, which is stored
+                * in the patch->fragments->{patch,size}.
+                */
+               if (apply_binary_fragment(desc, patch))
+                       return error("binary patch does not apply to '%s'",
+                                    name);
+
+               /* verify that the result matches */
+               write_sha1_file_prepare(desc->buffer, desc->size, blob_type,
+                                       sha1, hdr, &hdrlen);
+               if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
+                       return error("binary patch to '%s' creates incorrect result", name);
+       }
+
+       return 0;
+}
+
+static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
+{
+       struct fragment *frag = patch->fragments;
+       const char *name = patch->old_name ? patch->old_name : patch->new_name;
+
+       if (patch->is_binary)
+               return apply_binary(desc, patch);
+
+       while (frag) {
+               if (apply_one_fragment(desc, frag) < 0)
+                       return error("patch failed: %s:%ld",
+                                    name, frag->oldpos);
+               frag = frag->next;
+       }
+       return 0;
+}
+
+static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
+{
+       char *buf;
+       unsigned long size, alloc;
+       struct buffer_desc desc;
+
+       size = 0;
+       alloc = 0;
+       buf = NULL;
+       if (cached) {
+               if (ce) {
+                       char type[20];
+                       buf = read_sha1_file(ce->sha1, type, &size);
+                       if (!buf)
+                               return error("read of %s failed",
+                                            patch->old_name);
+                       alloc = size;
+               }
+       }
+       else if (patch->old_name) {
+               size = st->st_size;
+               alloc = size + 8192;
+               buf = xmalloc(alloc);
+               if (read_old_data(st, patch->old_name, buf, alloc) != size)
+                       return error("read of %s failed", patch->old_name);
+       }
+
+       desc.size = size;
+       desc.alloc = alloc;
+       desc.buffer = buf;
+       if (apply_fragments(&desc, patch) < 0)
+               return -1;
+       patch->result = desc.buffer;
+       patch->resultsize = desc.size;
+
+       if (patch->is_delete && patch->resultsize)
+               return error("removal patch leaves file contents");
+
+       return 0;
+}
+
+static int check_patch(struct patch *patch)
+{
+       struct stat st;
+       const char *old_name = patch->old_name;
+       const char *new_name = patch->new_name;
+       const char *name = old_name ? old_name : new_name;
+       struct cache_entry *ce = NULL;
+
+       if (old_name) {
+               int changed = 0;
+               int stat_ret = 0;
+               unsigned st_mode = 0;
+
+               if (!cached)
+                       stat_ret = lstat(old_name, &st);
+               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);
+                       ce = active_cache[pos];
+                       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(ce,
+                                                  &costate,
+                                                  NULL) ||
+                                   lstat(old_name, &st))
+                                       return -1;
+                       }
+                       if (!cached)
+                               changed = ce_match_stat(ce, &st, 1);
+                       if (changed)
+                               return error("%s: does not match index",
+                                            old_name);
+                       if (cached)
+                               st_mode = ntohl(ce->ce_mode);
+               }
+               else if (stat_ret < 0)
+                       return error("%s: %s", old_name, strerror(errno));
+
+               if (!cached)
+                       st_mode = ntohl(create_ce_mode(st.st_mode));
+
+               if (patch->is_new < 0)
+                       patch->is_new = 0;
+               if (!patch->old_mode)
+                       patch->old_mode = st_mode;
+               if ((st_mode ^ patch->old_mode) & S_IFMT)
+                       return error("%s: wrong type", old_name);
+               if (st_mode != patch->old_mode)
+                       fprintf(stderr, "warning: %s has type %o, expected %o\n",
+                               old_name, st_mode, patch->old_mode);
+       }
+
+       if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
+               if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
+                       return error("%s: already exists in index", new_name);
+               if (!cached) {
+                       if (!lstat(new_name, &st))
+                               return error("%s: already exists in working directory", new_name);
+                       if (errno != ENOENT)
+                               return error("%s: %s", new_name, strerror(errno));
+               }
+               if (!patch->new_mode) {
+                       if (patch->is_new)
+                               patch->new_mode = S_IFREG | 0644;
+                       else
+                               patch->new_mode = patch->old_mode;
+               }
+       }
+
+       if (new_name && old_name) {
+               int same = !strcmp(old_name, new_name);
+               if (!patch->new_mode)
+                       patch->new_mode = patch->old_mode;
+               if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
+                       return error("new mode (%o) of %s does not match old mode (%o)%s%s",
+                               patch->new_mode, new_name, patch->old_mode,
+                               same ? "" : " of ", same ? "" : old_name);
+       }
+
+       if (apply_data(patch, &st, ce) < 0)
+               return error("%s: patch does not apply", name);
+       return 0;
+}
+
+static int check_patch_list(struct patch *patch)
+{
+       int error = 0;
+
+       for (;patch ; patch = patch->next)
+               error |= check_patch(patch);
+       return error;
+}
+
+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 ",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);
+       }
+}
+
+static void stat_patch_list(struct patch *patch)
+{
+       int files, adds, dels;
+
+       for (files = adds = dels = 0 ; patch ; patch = patch->next) {
+               files++;
+               adds += patch->lines_added;
+               dels += patch->lines_deleted;
+               show_stats(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->new_name ? patch->new_name : patch->old_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)
+               printf(" %s mode %06o %s\n", newdelete, mode, name);
+       else
+               printf(" %s %s\n", newdelete, name);
+}
+
+static void show_mode_change(struct patch *p, int show_name)
+{
+       if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
+               if (show_name)
+                       printf(" mode change %06o => %06o %s\n",
+                              p->old_mode, p->new_mode, p->new_name);
+               else
+                       printf(" mode change %06o => %06o\n",
+                              p->old_mode, p->new_mode);
+       }
+}
+
+static void show_rename_copy(struct patch *p)
+{
+       const char *renamecopy = p->is_rename ? "rename" : "copy";
+       const char *old, *new;
+
+       /* Find common prefix */
+       old = p->old_name;
+       new = p->new_name;
+       while (1) {
+               const char *slash_old, *slash_new;
+               slash_old = strchr(old, '/');
+               slash_new = strchr(new, '/');
+               if (!slash_old ||
+                   !slash_new ||
+                   slash_old - old != slash_new - new ||
+                   memcmp(old, new, slash_new - new))
+                       break;
+               old = slash_old + 1;
+               new = slash_new + 1;
+       }
+       /* p->old_name thru old is the common prefix, and old and new
+        * through the end of names are renames
+        */
+       if (old != p->old_name)
+               printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
+                      (int)(old - p->old_name), p->old_name,
+                      old, new, p->score);
+       else
+               printf(" %s %s => %s (%d%%)\n", renamecopy,
+                      p->old_name, p->new_name, p->score);
+       show_mode_change(p, 0);
+}
+
+static void summary_patch_list(struct patch *patch)
+{
+       struct patch *p;
+
+       for (p = patch; p; p = p->next) {
+               if (p->is_new)
+                       show_file_mode_name("create", p->new_mode, p->new_name);
+               else if (p->is_delete)
+                       show_file_mode_name("delete", p->old_mode, p->old_name);
+               else {
+                       if (p->is_rename || p->is_copy)
+                               show_rename_copy(p);
+                       else {
+                               if (p->score) {
+                                       printf(" rewrite %s (%d%%)\n",
+                                              p->new_name, p->score);
+                                       show_mode_change(p, 0);
+                               }
+                               else
+                                       show_mode_change(p, 1);
+                       }
+               }
+       }
+}
+
+static void patch_stats(struct patch *patch)
+{
+       int lines = patch->lines_added + patch->lines_deleted;
+
+       if (lines > max_change)
+               max_change = lines;
+       if (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 = quote_c_style(patch->new_name, NULL, NULL, 0);
+               if (!len)
+                       len = strlen(patch->new_name);
+               if (len > max_len)
+                       max_len = len;
+       }
+}
+
+static void remove_file(struct patch *patch)
+{
+       if (write_index) {
+               if (remove_file_from_cache(patch->old_name) < 0)
+                       die("unable to remove %s from index", patch->old_name);
+               cache_tree_invalidate_path(active_cache_tree, patch->old_name);
+       }
+       if (!cached)
+               unlink(patch->old_name);
+}
+
+static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
+{
+       struct stat st;
+       struct cache_entry *ce;
+       int namelen = strlen(path);
+       unsigned ce_size = cache_entry_size(namelen);
+
+       if (!write_index)
+               return;
+
+       ce = xcalloc(1, ce_size);
+       memcpy(ce->name, path, namelen);
+       ce->ce_mode = create_ce_mode(mode);
+       ce->ce_flags = htons(namelen);
+       if (!cached) {
+               if (lstat(path, &st) < 0)
+                       die("unable to stat newly created file %s", path);
+               fill_stat_cache_info(ce, &st);
+       }
+       if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
+               die("unable to create backing store for newly created file %s", path);
+       if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
+               die("unable to add cache entry for %s", path);
+}
+
+static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
+{
+       int fd;
+
+       if (S_ISLNK(mode))
+               return symlink(buf, path);
+       fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
+       if (fd < 0)
+               return -1;
+       while (size) {
+               int written = xwrite(fd, buf, size);
+               if (written < 0)
+                       die("writing file %s: %s", path, strerror(errno));
+               if (!written)
+                       die("out of space writing file %s", path);
+               buf += written;
+               size -= written;
+       }
+       if (close(fd) < 0)
+               die("closing file %s: %s", path, strerror(errno));
+       return 0;
+}
+
+/*
+ * We optimistically assume that the directories exist,
+ * which is true 99% of the time anyway. If they don't,
+ * we create them and try again.
+ */
+static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
+{
+       if (cached)
+               return;
+       if (!try_create_file(path, mode, buf, size))
+               return;
+
+       if (errno == ENOENT) {
+               if (safe_create_leading_directories(path))
+                       return;
+               if (!try_create_file(path, mode, buf, size))
+                       return;
+       }
+
+       if (errno == EEXIST) {
+               unsigned int nr = getpid();
+
+               for (;;) {
+                       const char *newpath;
+                       newpath = mkpath("%s~%u", path, nr);
+                       if (!try_create_file(newpath, mode, buf, size)) {
+                               if (!rename(newpath, path))
+                                       return;
+                               unlink(newpath);
+                               break;
+                       }
+                       if (errno != EEXIST)
+                               break;
+                       ++nr;
+               }
+       }
+       die("unable to write file %s mode %o", path, mode);
+}
+
+static void create_file(struct patch *patch)
+{
+       char *path = patch->new_name;
+       unsigned mode = patch->new_mode;
+       unsigned long size = patch->resultsize;
+       char *buf = patch->result;
+
+       if (!mode)
+               mode = S_IFREG | 0644;
+       create_one_file(path, mode, buf, size);
+       add_index_file(path, mode, buf, size);
+       cache_tree_invalidate_path(active_cache_tree, path);
+}
+
+static void write_out_one_result(struct patch *patch)
+{
+       if (patch->is_delete > 0) {
+               remove_file(patch);
+               return;
+       }
+       if (patch->is_new > 0 || patch->is_copy) {
+               create_file(patch);
+               return;
+       }
+       /*
+        * Rename or modification boils down to the same
+        * thing: remove the old, write the new
+        */
+       remove_file(patch);
+       create_file(patch);
+}
+
+static void write_out_results(struct patch *list, int skipped_patch)
+{
+       if (!list && !skipped_patch)
+               die("No changes");
+
+       while (list) {
+               write_out_one_result(list);
+               list = list->next;
+       }
+}
+
+static struct lock_file lock_file;
+
+static struct excludes {
+       struct excludes *next;
+       const char *path;
+} *excludes;
+
+static int use_patch(struct patch *p)
+{
+       const char *pathname = p->new_name ? p->new_name : p->old_name;
+       struct excludes *x = excludes;
+       while (x) {
+               if (fnmatch(x->path, pathname, 0) == 0)
+                       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 int apply_patch(int fd, const char *filename)
+{
+       unsigned long offset, size;
+       char *buffer = read_patch_file(fd, &size);
+       struct patch *list = NULL, **listp = &list;
+       int skipped_patch = 0;
+
+       patch_input_file = filename;
+       if (!buffer)
+               return -1;
+       offset = 0;
+       while (size > 0) {
+               struct patch *patch;
+               int nr;
+
+               patch = xcalloc(1, sizeof(*patch));
+               nr = parse_chunk(buffer + offset, size, patch);
+               if (nr < 0)
+                       break;
+               if (use_patch(patch)) {
+                       patch_stats(patch);
+                       *listp = patch;
+                       listp = &patch->next;
+               } else {
+                       /* perhaps free it a bit better? */
+                       free(patch);
+                       skipped_patch++;
+               }
+               offset += nr;
+               size -= nr;
+       }
+
+       if (whitespace_error && (new_whitespace == error_on_whitespace))
+               apply = 0;
+
+       write_index = check_index && apply;
+       if (write_index && newfd < 0) {
+               newfd = hold_lock_file_for_update(&lock_file,
+                                                 get_index_file());
+               if (newfd < 0)
+                       die("unable to create new index file");
+       }
+       if (check_index) {
+               if (read_cache() < 0)
+                       die("unable to read index file");
+       }
+
+       if ((check || apply) && check_patch_list(list) < 0)
+               exit(1);
+
+       if (apply)
+               write_out_results(list, skipped_patch);
+
+       if (show_index_info)
+               show_index_list(list);
+
+       if (diffstat)
+               stat_patch_list(list);
+
+       if (numstat)
+               numstat_patch_list(list);
+
+       if (summary)
+               summary_patch_list(list);
+
+       free(buffer);
+       return 0;
+}
+
+static int git_apply_config(const char *var, const char *value)
+{
+       if (!strcmp(var, "apply.whitespace")) {
+               apply_default_whitespace = strdup(value);
+               return 0;
+       }
+       return git_default_config(var, value);
+}
+
+
+int cmd_apply(int argc, const char **argv, char **envp)
+{
+       int i;
+       int read_stdin = 1;
+       const char *whitespace_option = NULL;
+
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+               char *end;
+               int fd;
+
+               if (!strcmp(arg, "-")) {
+                       apply_patch(0, "<stdin>");
+                       read_stdin = 0;
+                       continue;
+               }
+               if (!strncmp(arg, "--exclude=", 10)) {
+                       struct excludes *x = xmalloc(sizeof(*x));
+                       x->path = arg + 10;
+                       x->next = excludes;
+                       excludes = x;
+                       continue;
+               }
+               if (!strncmp(arg, "-p", 2)) {
+                       p_value = atoi(arg + 2);
+                       continue;
+               }
+               if (!strcmp(arg, "--no-add")) {
+                       no_add = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--stat")) {
+                       apply = 0;
+                       diffstat = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--allow-binary-replacement") ||
+                   !strcmp(arg, "--binary")) {
+                       allow_binary_replacement = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--numstat")) {
+                       apply = 0;
+                       numstat = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--summary")) {
+                       apply = 0;
+                       summary = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--check")) {
+                       apply = 0;
+                       check = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--index")) {
+                       check_index = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--cached")) {
+                       check_index = 1;
+                       cached = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--apply")) {
+                       apply = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--index-info")) {
+                       apply = 0;
+                       show_index_info = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-z")) {
+                       line_termination = 0;
+                       continue;
+               }
+               if (!strncmp(arg, "-C", 2)) {
+                       p_context = strtoul(arg + 2, &end, 0);
+                       if (*end != '\0')
+                               die("unrecognized context count '%s'", arg + 2);
+                       continue;
+               }
+               if (!strncmp(arg, "--whitespace=", 13)) {
+                       whitespace_option = arg + 13;
+                       parse_whitespace_option(arg + 13);
+                       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);
+
+               fd = open(arg, O_RDONLY);
+               if (fd < 0)
+                       usage(apply_usage);
+               read_stdin = 0;
+               set_default_whitespace_mode(whitespace_option);
+               apply_patch(fd, arg);
+               close(fd);
+       }
+       set_default_whitespace_mode(whitespace_option);
+       if (read_stdin)
+               apply_patch(0, "<stdin>");
+       if (whitespace_error) {
+               if (squelch_whitespace_errors &&
+                   squelch_whitespace_errors < whitespace_error) {
+                       int squelched =
+                               whitespace_error - squelch_whitespace_errors;
+                       fprintf(stderr, "warning: squelched %d whitespace error%s\n",
+                               squelched,
+                               squelched == 1 ? "" : "s");
+               }
+               if (new_whitespace == error_on_whitespace)
+                       die("%d line%s add%s trailing whitespaces.",
+                           whitespace_error,
+                           whitespace_error == 1 ? "" : "s",
+                           whitespace_error == 1 ? "s" : "");
+               if (applied_after_stripping)
+                       fprintf(stderr, "warning: %d line%s applied after"
+                               " stripping trailing whitespaces.\n",
+                               applied_after_stripping,
+                               applied_after_stripping == 1 ? "" : "s");
+               else if (whitespace_error)
+                       fprintf(stderr, "warning: %d line%s add%s trailing"
+                               " whitespaces.\n",
+                               whitespace_error,
+                               whitespace_error == 1 ? "" : "s",
+                               whitespace_error == 1 ? "s" : "");
+       }
+
+       if (write_index) {
+               if (write_cache(newfd, active_cache, active_nr) ||
+                   commit_lock_file(&lock_file))
+                       die("Unable to write new index file");
+       }
+
+       return 0;
+}
diff --git a/builtin-cat-file.c b/builtin-cat-file.c
new file mode 100644 (file)
index 0000000..4d36817
--- /dev/null
@@ -0,0 +1,167 @@
+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#include "cache.h"
+#include "exec_cmd.h"
+#include "tag.h"
+#include "tree.h"
+#include "builtin.h"
+
+static void flush_buffer(const char *buf, unsigned long size)
+{
+       while (size > 0) {
+               long ret = xwrite(1, buf, size);
+               if (ret < 0) {
+                       /* Ignore epipe */
+                       if (errno == EPIPE)
+                               break;
+                       die("git-cat-file: %s", strerror(errno));
+               } else if (!ret) {
+                       die("git-cat-file: disk full?");
+               }
+               size -= ret;
+               buf += ret;
+       }
+}
+
+static int pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
+{
+       /* the parser in tag.c is useless here. */
+       const char *endp = buf + size;
+       const char *cp = buf;
+
+       while (cp < endp) {
+               char c = *cp++;
+               if (c != '\n')
+                       continue;
+               if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
+                       const char *tagger = cp;
+
+                       /* Found the tagger line.  Copy out the contents
+                        * of the buffer so far.
+                        */
+                       flush_buffer(buf, cp - buf);
+
+                       /*
+                        * Do something intelligent, like pretty-printing
+                        * the date.
+                        */
+                       while (cp < endp) {
+                               if (*cp++ == '\n') {
+                                       /* tagger to cp is a line
+                                        * that has ident and time.
+                                        */
+                                       const char *sp = tagger;
+                                       char *ep;
+                                       unsigned long date;
+                                       long tz;
+                                       while (sp < cp && *sp != '>')
+                                               sp++;
+                                       if (sp == cp) {
+                                               /* give up */
+                                               flush_buffer(tagger,
+                                                            cp - tagger);
+                                               break;
+                                       }
+                                       while (sp < cp &&
+                                              !('0' <= *sp && *sp <= '9'))
+                                               sp++;
+                                       flush_buffer(tagger, sp - tagger);
+                                       date = strtoul(sp, &ep, 10);
+                                       tz = strtol(ep, NULL, 10);
+                                       sp = show_date(date, tz);
+                                       flush_buffer(sp, strlen(sp));
+                                       xwrite(1, "\n", 1);
+                                       break;
+                               }
+                       }
+                       break;
+               }
+               if (cp < endp && *cp == '\n')
+                       /* end of header */
+                       break;
+       }
+       /* At this point, we have copied out the header up to the end of
+        * the tagger line and cp points at one past \n.  It could be the
+        * next header line after the tagger line, or it could be another
+        * \n that marks the end of the headers.  We need to copy out the
+        * remainder as is.
+        */
+       if (cp < endp)
+               flush_buffer(cp, endp - cp);
+       return 0;
+}
+
+int cmd_cat_file(int argc, const char **argv, char **envp)
+{
+       unsigned char sha1[20];
+       char type[20];
+       void *buf;
+       unsigned long size;
+       int opt;
+
+       setup_git_directory();
+       git_config(git_default_config);
+       if (argc != 3)
+               usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
+       if (get_sha1(argv[2], sha1))
+               die("Not a valid object name %s", argv[2]);
+
+       opt = 0;
+       if ( argv[1][0] == '-' ) {
+               opt = argv[1][1];
+               if ( !opt || argv[1][2] )
+                       opt = -1; /* Not a single character option */
+       }
+
+       buf = NULL;
+       switch (opt) {
+       case 't':
+               if (!sha1_object_info(sha1, type, NULL)) {
+                       printf("%s\n", type);
+                       return 0;
+               }
+               break;
+
+       case 's':
+               if (!sha1_object_info(sha1, type, &size)) {
+                       printf("%lu\n", size);
+                       return 0;
+               }
+               break;
+
+       case 'e':
+               return !has_sha1_file(sha1);
+
+       case 'p':
+               if (sha1_object_info(sha1, type, NULL))
+                       die("Not a valid object name %s", argv[2]);
+
+               /* custom pretty-print here */
+               if (!strcmp(type, tree_type))
+                       return cmd_ls_tree(2, argv + 1, NULL);
+
+               buf = read_sha1_file(sha1, type, &size);
+               if (!buf)
+                       die("Cannot read object %s", argv[2]);
+               if (!strcmp(type, tag_type))
+                       return pprint_tag(sha1, buf, size);
+
+               /* otherwise just spit out the data */
+               break;
+       case 0:
+               buf = read_object_with_reference(sha1, argv[1], &size, NULL);
+               break;
+
+       default:
+               die("git-cat-file: unknown option: %s\n", argv[1]);
+       }
+
+       if (!buf)
+               die("git-cat-file %s: bad file", argv[2]);
+
+       flush_buffer(buf, size);
+       return 0;
+}
diff --git a/builtin-check-ref-format.c b/builtin-check-ref-format.c
new file mode 100644 (file)
index 0000000..4a23936
--- /dev/null
@@ -0,0 +1,14 @@
+/*
+ * GIT - The information manager from hell
+ */
+
+#include "cache.h"
+#include "refs.h"
+#include "builtin.h"
+
+int cmd_check_ref_format(int argc, const char **argv, char **envp)
+{
+       if (argc != 2)
+               usage("git check-ref-format refname");
+       return !!check_ref_format(argv[1]);
+}
diff --git a/builtin-commit-tree.c b/builtin-commit-tree.c
new file mode 100644 (file)
index 0000000..ec082bf
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#include "cache.h"
+#include "commit.h"
+#include "tree.h"
+#include "builtin.h"
+
+#define BLOCKING (1ul << 14)
+
+/*
+ * FIXME! Share the code with "write-tree.c"
+ */
+static void init_buffer(char **bufp, unsigned int *sizep)
+{
+       char *buf = xmalloc(BLOCKING);
+       *sizep = 0;
+       *bufp = buf;
+}
+
+static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
+{
+       char one_line[2048];
+       va_list args;
+       int len;
+       unsigned long alloc, size, newsize;
+       char *buf;
+
+       va_start(args, fmt);
+       len = vsnprintf(one_line, sizeof(one_line), fmt, args);
+       va_end(args);
+       size = *sizep;
+       newsize = size + len;
+       alloc = (size + 32767) & ~32767;
+       buf = *bufp;
+       if (newsize > alloc) {
+               alloc = (newsize + 32767) & ~32767;
+               buf = xrealloc(buf, alloc);
+               *bufp = buf;
+       }
+       *sizep = newsize;
+       memcpy(buf + size, one_line, len);
+}
+
+static void check_valid(unsigned char *sha1, const char *expect)
+{
+       char type[20];
+
+       if (sha1_object_info(sha1, type, NULL))
+               die("%s is not a valid object", sha1_to_hex(sha1));
+       if (expect && strcmp(type, expect))
+               die("%s is not a valid '%s' object", sha1_to_hex(sha1),
+                   expect);
+}
+
+/*
+ * Having more than two parents is not strange at all, and this is
+ * how multi-way merges are represented.
+ */
+#define MAXPARENT (16)
+static unsigned char parent_sha1[MAXPARENT][20];
+
+static const char commit_tree_usage[] = "git-commit-tree <sha1> [-p <sha1>]* < changelog";
+
+static int new_parent(int idx)
+{
+       int i;
+       unsigned char *sha1 = parent_sha1[idx];
+       for (i = 0; i < idx; i++) {
+               if (!memcmp(parent_sha1[i], sha1, 20)) {
+                       error("duplicate parent %s ignored", sha1_to_hex(sha1));
+                       return 0;
+               }
+       }
+       return 1;
+}
+
+int cmd_commit_tree(int argc, const char **argv, char **envp)
+{
+       int i;
+       int parents = 0;
+       unsigned char tree_sha1[20];
+       unsigned char commit_sha1[20];
+       char comment[1000];
+       char *buffer;
+       unsigned int size;
+
+       setup_ident();
+       setup_git_directory();
+
+       git_config(git_default_config);
+
+       if (argc < 2)
+               usage(commit_tree_usage);
+       if (get_sha1(argv[1], tree_sha1))
+               die("Not a valid object name %s", argv[1]);
+
+       check_valid(tree_sha1, tree_type);
+       for (i = 2; i < argc; i += 2) {
+               const char *a, *b;
+               a = argv[i]; b = argv[i+1];
+               if (!b || strcmp(a, "-p"))
+                       usage(commit_tree_usage);
+               if (get_sha1(b, parent_sha1[parents]))
+                       die("Not a valid object name %s", b);
+               check_valid(parent_sha1[parents], commit_type);
+               if (new_parent(parents))
+                       parents++;
+       }
+       if (!parents)
+               fprintf(stderr, "Committing initial tree %s\n", argv[1]);
+
+       init_buffer(&buffer, &size);
+       add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
+
+       /*
+        * NOTE! This ordering means that the same exact tree merged with a
+        * different order of parents will be a _different_ changeset even
+        * if everything else stays the same.
+        */
+       for (i = 0; i < parents; i++)
+               add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
+
+       /* Person/date information */
+       add_buffer(&buffer, &size, "author %s\n", git_author_info(1));
+       add_buffer(&buffer, &size, "committer %s\n\n", git_committer_info(1));
+
+       /* And add the comment */
+       while (fgets(comment, sizeof(comment), stdin) != NULL)
+               add_buffer(&buffer, &size, "%s", comment);
+
+       if (!write_sha1_file(buffer, size, commit_type, commit_sha1)) {
+               printf("%s\n", sha1_to_hex(commit_sha1));
+               return 0;
+       }
+       else
+               return 1;
+}
diff --git a/builtin-count.c b/builtin-count.c
new file mode 100644 (file)
index 0000000..5ee72df
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * Builtin "git count-objects".
+ *
+ * Copyright (c) 2006 Junio C Hamano
+ */
+
+#include "cache.h"
+#include "builtin.h"
+
+static const char count_objects_usage[] = "git-count-objects [-v]";
+
+static void count_objects(DIR *d, char *path, int len, int verbose,
+                         unsigned long *loose,
+                         unsigned long *loose_size,
+                         unsigned long *packed_loose,
+                         unsigned long *garbage)
+{
+       struct dirent *ent;
+       while ((ent = readdir(d)) != NULL) {
+               char hex[41];
+               unsigned char sha1[20];
+               const char *cp;
+               int bad = 0;
+
+               if ((ent->d_name[0] == '.') &&
+                   (ent->d_name[1] == 0 ||
+                    ((ent->d_name[1] == '.') && (ent->d_name[2] == 0))))
+                       continue;
+               for (cp = ent->d_name; *cp; cp++) {
+                       int ch = *cp;
+                       if (('0' <= ch && ch <= '9') ||
+                           ('a' <= ch && ch <= 'f'))
+                               continue;
+                       bad = 1;
+                       break;
+               }
+               if (cp - ent->d_name != 38)
+                       bad = 1;
+               else {
+                       struct stat st;
+                       memcpy(path + len + 3, ent->d_name, 38);
+                       path[len + 2] = '/';
+                       path[len + 41] = 0;
+                       if (lstat(path, &st) || !S_ISREG(st.st_mode))
+                               bad = 1;
+                       else
+                               (*loose_size) += st.st_blocks;
+               }
+               if (bad) {
+                       if (verbose) {
+                               error("garbage found: %.*s/%s",
+                                     len + 2, path, ent->d_name);
+                               (*garbage)++;
+                       }
+                       continue;
+               }
+               (*loose)++;
+               if (!verbose)
+                       continue;
+               memcpy(hex, path+len, 2);
+               memcpy(hex+2, ent->d_name, 38);
+               hex[40] = 0;
+               if (get_sha1_hex(hex, sha1))
+                       die("internal error");
+               if (has_sha1_pack(sha1))
+                       (*packed_loose)++;
+       }
+}
+
+int cmd_count_objects(int ac, const char **av, char **ep)
+{
+       int i;
+       int verbose = 0;
+       const char *objdir = get_object_directory();
+       int len = strlen(objdir);
+       char *path = xmalloc(len + 50);
+       unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0;
+       unsigned long loose_size = 0;
+
+       for (i = 1; i < ac; i++) {
+               const char *arg = av[i];
+               if (*arg != '-')
+                       break;
+               else if (!strcmp(arg, "-v"))
+                       verbose = 1;
+               else
+                       usage(count_objects_usage);
+       }
+
+       /* we do not take arguments other than flags for now */
+       if (i < ac)
+               usage(count_objects_usage);
+       memcpy(path, objdir, len);
+       if (len && objdir[len-1] != '/')
+               path[len++] = '/';
+       for (i = 0; i < 256; i++) {
+               DIR *d;
+               sprintf(path + len, "%02x", i);
+               d = opendir(path);
+               if (!d)
+                       continue;
+               count_objects(d, path, len, verbose,
+                             &loose, &loose_size, &packed_loose, &garbage);
+               closedir(d);
+       }
+       if (verbose) {
+               struct packed_git *p;
+               if (!packed_git)
+                       prepare_packed_git();
+               for (p = packed_git; p; p = p->next) {
+                       if (!p->pack_local)
+                               continue;
+                       packed += num_packed_objects(p);
+               }
+               printf("count: %lu\n", loose);
+               printf("size: %lu\n", loose_size / 2);
+               printf("in-pack: %lu\n", packed);
+               printf("prune-packable: %lu\n", packed_loose);
+               printf("garbage: %lu\n", garbage);
+       }
+       else
+               printf("%lu objects, %lu kilobytes\n",
+                      loose, loose_size / 2);
+       return 0;
+}
diff --git a/builtin-diff-files.c b/builtin-diff-files.c
new file mode 100644 (file)
index 0000000..cebda82
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#include "cache.h"
+#include "diff.h"
+#include "commit.h"
+#include "revision.h"
+#include "builtin.h"
+
+static const char diff_files_usage[] =
+"git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]"
+COMMON_DIFF_OPTIONS_HELP;
+
+int cmd_diff_files(int argc, const char **argv, char **envp)
+{
+       struct rev_info rev;
+       int silent = 0;
+
+       git_config(git_diff_config);
+       init_revisions(&rev);
+       rev.abbrev = 0;
+
+       argc = setup_revisions(argc, argv, &rev, NULL);
+       while (1 < argc && argv[1][0] == '-') {
+               if (!strcmp(argv[1], "--base"))
+                       rev.max_count = 1;
+               else if (!strcmp(argv[1], "--ours"))
+                       rev.max_count = 2;
+               else if (!strcmp(argv[1], "--theirs"))
+                       rev.max_count = 3;
+               else if (!strcmp(argv[1], "-q"))
+                       silent = 1;
+               else
+                       usage(diff_files_usage);
+               argv++; argc--;
+       }
+       /*
+        * Make sure there are NO revision (i.e. pending object) parameter,
+        * rev.max_count is reasonable (0 <= n <= 3),
+        * there is no other revision filtering parameters.
+        */
+       if (rev.pending_objects ||
+           rev.min_age != -1 || rev.max_age != -1)
+               usage(diff_files_usage);
+       /*
+        * Backward compatibility wart - "diff-files -s" used to
+        * defeat the common diff option "-s" which asked for
+        * DIFF_FORMAT_NO_OUTPUT.
+        */
+       if (rev.diffopt.output_format == DIFF_FORMAT_NO_OUTPUT)
+               rev.diffopt.output_format = DIFF_FORMAT_RAW;
+       return run_diff_files(&rev, silent);
+}
diff --git a/builtin-diff-index.c b/builtin-diff-index.c
new file mode 100644 (file)
index 0000000..1958580
--- /dev/null
@@ -0,0 +1,39 @@
+#include "cache.h"
+#include "diff.h"
+#include "commit.h"
+#include "revision.h"
+#include "builtin.h"
+
+static const char diff_cache_usage[] =
+"git-diff-index [-m] [--cached] "
+"[<common diff options>] <tree-ish> [<path>...]"
+COMMON_DIFF_OPTIONS_HELP;
+
+int cmd_diff_index(int argc, const char **argv, char **envp)
+{
+       struct rev_info rev;
+       int cached = 0;
+       int i;
+
+       git_config(git_diff_config);
+       init_revisions(&rev);
+       rev.abbrev = 0;
+
+       argc = setup_revisions(argc, argv, &rev, NULL);
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+                       
+               if (!strcmp(arg, "--cached"))
+                       cached = 1;
+               else
+                       usage(diff_cache_usage);
+       }
+       /*
+        * Make sure there is one revision (i.e. pending object),
+        * and there is no revision filtering parameters.
+        */
+       if (!rev.pending_objects || rev.pending_objects->next ||
+           rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
+               usage(diff_cache_usage);
+       return run_diff_index(&rev, cached);
+}
diff --git a/builtin-diff-stages.c b/builtin-diff-stages.c
new file mode 100644 (file)
index 0000000..7c157ca
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2005 Junio C Hamano
+ */
+
+#include "cache.h"
+#include "diff.h"
+#include "builtin.h"
+
+static struct diff_options diff_options;
+
+static const char diff_stages_usage[] =
+"git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
+COMMON_DIFF_OPTIONS_HELP;
+
+static void diff_stages(int stage1, int stage2, const char **pathspec)
+{
+       int i = 0;
+       while (i < active_nr) {
+               struct cache_entry *ce, *stages[4] = { NULL, };
+               struct cache_entry *one, *two;
+               const char *name;
+               int len, skip;
+
+               ce = active_cache[i];
+               skip = !ce_path_match(ce, pathspec);
+               len = ce_namelen(ce);
+               name = ce->name;
+               for (;;) {
+                       int stage = ce_stage(ce);
+                       stages[stage] = ce;
+                       if (active_nr <= ++i)
+                               break;
+                       ce = active_cache[i];
+                       if (ce_namelen(ce) != len ||
+                           memcmp(name, ce->name, len))
+                               break;
+               }
+               one = stages[stage1];
+               two = stages[stage2];
+
+               if (skip || (!one && !two))
+                       continue;
+               if (!one)
+                       diff_addremove(&diff_options, '+', ntohl(two->ce_mode),
+                                      two->sha1, name, NULL);
+               else if (!two)
+                       diff_addremove(&diff_options, '-', ntohl(one->ce_mode),
+                                      one->sha1, name, NULL);
+               else if (memcmp(one->sha1, two->sha1, 20) ||
+                        (one->ce_mode != two->ce_mode) ||
+                        diff_options.find_copies_harder)
+                       diff_change(&diff_options,
+                                   ntohl(one->ce_mode), ntohl(two->ce_mode),
+                                   one->sha1, two->sha1, name, NULL);
+       }
+}
+
+int cmd_diff_stages(int ac, const char **av, char **envp)
+{
+       int stage1, stage2;
+       const char *prefix = setup_git_directory();
+       const char **pathspec = NULL;
+
+       git_config(git_diff_config);
+       read_cache();
+       diff_setup(&diff_options);
+       while (1 < ac && av[1][0] == '-') {
+               const char *arg = av[1];
+               if (!strcmp(arg, "-r"))
+                       ; /* as usual */
+               else {
+                       int diff_opt_cnt;
+                       diff_opt_cnt = diff_opt_parse(&diff_options,
+                                                     av+1, ac-1);
+                       if (diff_opt_cnt < 0)
+                               usage(diff_stages_usage);
+                       else if (diff_opt_cnt) {
+                               av += diff_opt_cnt;
+                               ac -= diff_opt_cnt;
+                               continue;
+                       }
+                       else
+                               usage(diff_stages_usage);
+               }
+               ac--; av++;
+       }
+
+       if (ac < 3 ||
+           sscanf(av[1], "%d", &stage1) != 1 ||
+           ! (0 <= stage1 && stage1 <= 3) ||
+           sscanf(av[2], "%d", &stage2) != 1 ||
+           ! (0 <= stage2 && stage2 <= 3))
+               usage(diff_stages_usage);
+
+       av += 3; /* The rest from av[0] are for paths restriction. */
+       pathspec = get_pathspec(prefix, av);
+
+       if (diff_setup_done(&diff_options) < 0)
+               usage(diff_stages_usage);
+
+       diff_stages(stage1, stage2, pathspec);
+       diffcore_std(&diff_options);
+       diff_flush(&diff_options);
+       return 0;
+}
diff --git a/builtin-diff-tree.c b/builtin-diff-tree.c
new file mode 100644 (file)
index 0000000..58cf658
--- /dev/null
@@ -0,0 +1,152 @@
+#include "cache.h"
+#include "diff.h"
+#include "commit.h"
+#include "log-tree.h"
+#include "builtin.h"
+
+static struct rev_info log_tree_opt;
+
+static int diff_tree_commit_sha1(const unsigned char *sha1)
+{
+       struct commit *commit = lookup_commit_reference(sha1);
+       if (!commit)
+               return -1;
+       return log_tree_commit(&log_tree_opt, commit);
+}
+
+static int diff_tree_stdin(char *line)
+{
+       int len = strlen(line);
+       unsigned char sha1[20];
+       struct commit *commit;
+
+       if (!len || line[len-1] != '\n')
+               return -1;
+       line[len-1] = 0;
+       if (get_sha1_hex(line, sha1))
+               return -1;
+       commit = lookup_commit(sha1);
+       if (!commit || parse_commit(commit))
+               return -1;
+       if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
+               /* Graft the fake parents locally to the commit */
+               int pos = 41;
+               struct commit_list **pptr, *parents;
+
+               /* Free the real parent list */
+               for (parents = commit->parents; parents; ) {
+                       struct commit_list *tmp = parents->next;
+                       free(parents);
+                       parents = tmp;
+               }
+               commit->parents = NULL;
+               pptr = &(commit->parents);
+               while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
+                       struct commit *parent = lookup_commit(sha1);
+                       if (parent) {
+                               pptr = &commit_list_insert(parent, pptr)->next;
+                       }
+                       pos += 41;
+               }
+       }
+       return log_tree_commit(&log_tree_opt, commit);
+}
+
+static const char diff_tree_usage[] =
+"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
+"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
+"  -r            diff recursively\n"
+"  --root        include the initial commit as diff against /dev/null\n"
+COMMON_DIFF_OPTIONS_HELP;
+
+int cmd_diff_tree(int argc, const char **argv, char **envp)
+{
+       int nr_sha1;
+       char line[1000];
+       struct object *tree1, *tree2;
+       static struct rev_info *opt = &log_tree_opt;
+       struct object_list *list;
+       int read_stdin = 0;
+
+       git_config(git_diff_config);
+       nr_sha1 = 0;
+       init_revisions(opt);
+       opt->abbrev = 0;
+       opt->diff = 1;
+       argc = setup_revisions(argc, argv, opt, NULL);
+
+       while (--argc > 0) {
+               const char *arg = *++argv;
+
+               if (!strcmp(arg, "--stdin")) {
+                       read_stdin = 1;
+                       continue;
+               }
+               usage(diff_tree_usage);
+       }
+
+       /*
+        * NOTE! "setup_revisions()" will have inserted the revisions
+        * it parsed in reverse order. So if you do
+        *
+        *      git-diff-tree a b
+        *
+        * the commit list will be "b" -> "a" -> NULL, so we reverse
+        * the order of the objects if the first one is not marked
+        * UNINTERESTING.
+        */
+       nr_sha1 = 0;
+       list = opt->pending_objects;
+       if (list) {
+               nr_sha1++;
+               tree1 = list->item;
+               list = list->next;
+               if (list) {
+                       nr_sha1++;
+                       tree2 = tree1;
+                       tree1 = list->item;
+                       if (list->next)
+                               usage(diff_tree_usage);
+                       /* Switch them around if the second one was uninteresting.. */
+                       if (tree2->flags & UNINTERESTING) {
+                               struct object *tmp = tree2;
+                               tree2 = tree1;
+                               tree1 = tmp;
+                       }
+               }
+       }
+
+       switch (nr_sha1) {
+       case 0:
+               if (!read_stdin)
+                       usage(diff_tree_usage);
+               break;
+       case 1:
+               diff_tree_commit_sha1(tree1->sha1);
+               break;
+       case 2:
+               diff_tree_sha1(tree1->sha1,
+                              tree2->sha1,
+                              "", &opt->diffopt);
+               log_tree_diff_flush(opt);
+               break;
+       }
+
+       if (!read_stdin)
+               return 0;
+
+       if (opt->diffopt.detect_rename)
+               opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
+                                      DIFF_SETUP_USE_CACHE);
+       while (fgets(line, sizeof(line), stdin)) {
+               unsigned char sha1[20];
+
+               if (get_sha1_hex(line, sha1)) {
+                       fputs(line, stdout);
+                       fflush(stdout);
+               }
+               else
+                       diff_tree_stdin(line);
+       }
+       return 0;
+}
diff --git a/builtin-diff.c b/builtin-diff.c
new file mode 100644 (file)
index 0000000..27451d5
--- /dev/null
@@ -0,0 +1,370 @@
+/*
+ * Builtin "git diff"
+ *
+ * Copyright (c) 2006 Junio C Hamano
+ */
+#include "cache.h"
+#include "commit.h"
+#include "blob.h"
+#include "tag.h"
+#include "diff.h"
+#include "diffcore.h"
+#include "revision.h"
+#include "log-tree.h"
+#include "builtin.h"
+
+/* NEEDSWORK: struct object has place for name but we _do_
+ * know mode when we extracted the blob out of a tree, which
+ * we currently lose.
+ */
+struct blobinfo {
+       unsigned char sha1[20];
+       const char *name;
+};
+
+static const char builtin_diff_usage[] =
+"diff <options> <rev>{0,2} -- <path>*";
+
+static int builtin_diff_files(struct rev_info *revs,
+                             int argc, const char **argv)
+{
+       int silent = 0;
+       while (1 < argc) {
+               const char *arg = argv[1];
+               if (!strcmp(arg, "--base"))
+                       revs->max_count = 1;
+               else if (!strcmp(arg, "--ours"))
+                       revs->max_count = 2;
+               else if (!strcmp(arg, "--theirs"))
+                       revs->max_count = 3;
+               else if (!strcmp(arg, "-q"))
+                       silent = 1;
+               else if (!strcmp(arg, "--raw"))
+                       revs->diffopt.output_format = DIFF_FORMAT_RAW;
+               else
+                       usage(builtin_diff_usage);
+               argv++; argc--;
+       }
+       /*
+        * Make sure there are NO revision (i.e. pending object) parameter,
+        * specified rev.max_count is reasonable (0 <= n <= 3), and
+        * there is no other revision filtering parameter.
+        */
+       if (revs->pending_objects ||
+           revs->min_age != -1 ||
+           revs->max_age != -1 ||
+           3 < revs->max_count)
+               usage(builtin_diff_usage);
+       if (revs->max_count < 0 &&
+           (revs->diffopt.output_format == DIFF_FORMAT_PATCH))
+               revs->combine_merges = revs->dense_combined_merges = 1;
+       /*
+        * Backward compatibility wart - "diff-files -s" used to
+        * defeat the common diff option "-s" which asked for
+        * DIFF_FORMAT_NO_OUTPUT.
+        */
+       if (revs->diffopt.output_format == DIFF_FORMAT_NO_OUTPUT)
+               revs->diffopt.output_format = DIFF_FORMAT_RAW;
+       return run_diff_files(revs, silent);
+}
+
+static void stuff_change(struct diff_options *opt,
+                        unsigned old_mode, unsigned new_mode,
+                        const unsigned char *old_sha1,
+                        const unsigned char *new_sha1,
+                        const char *old_name,
+                        const char *new_name)
+{
+       struct diff_filespec *one, *two;
+
+       if (memcmp(null_sha1, old_sha1, 20) &&
+           memcmp(null_sha1, new_sha1, 20) &&
+           !memcmp(old_sha1, new_sha1, 20))
+               return;
+
+       if (opt->reverse_diff) {
+               unsigned tmp;
+               const unsigned char *tmp_u;
+               const char *tmp_c;
+               tmp = old_mode; old_mode = new_mode; new_mode = tmp;
+               tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
+               tmp_c = old_name; old_name = new_name; new_name = tmp_c;
+       }
+       one = alloc_filespec(old_name);
+       two = alloc_filespec(new_name);
+       fill_filespec(one, old_sha1, old_mode);
+       fill_filespec(two, new_sha1, new_mode);
+
+       /* NEEDSWORK: shouldn't this part of diffopt??? */
+       diff_queue(&diff_queued_diff, one, two);
+}
+
+static int builtin_diff_b_f(struct rev_info *revs,
+                           int argc, const char **argv,
+                           struct blobinfo *blob,
+                           const char *path)
+{
+       /* Blob vs file in the working tree*/
+       struct stat st;
+
+       while (1 < argc) {
+               const char *arg = argv[1];
+               if (!strcmp(arg, "--raw"))
+                       revs->diffopt.output_format = DIFF_FORMAT_RAW;
+               else
+                       usage(builtin_diff_usage);
+               argv++; argc--;
+       }
+       if (lstat(path, &st))
+               die("'%s': %s", path, strerror(errno));
+       if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
+               die("'%s': not a regular file or symlink", path);
+       stuff_change(&revs->diffopt,
+                    canon_mode(st.st_mode), canon_mode(st.st_mode),
+                    blob[0].sha1, null_sha1,
+                    path, path);
+       diffcore_std(&revs->diffopt);
+       diff_flush(&revs->diffopt);
+       return 0;
+}
+
+static int builtin_diff_blobs(struct rev_info *revs,
+                             int argc, const char **argv,
+                             struct blobinfo *blob)
+{
+       /* Blobs: the arguments are reversed when setup_revisions()
+        * picked them up.
+        */
+       unsigned mode = canon_mode(S_IFREG | 0644);
+
+       while (1 < argc) {
+               const char *arg = argv[1];
+               if (!strcmp(arg, "--raw"))
+                       revs->diffopt.output_format = DIFF_FORMAT_RAW;
+               else
+                       usage(builtin_diff_usage);
+               argv++; argc--;
+       }
+       stuff_change(&revs->diffopt,
+                    mode, mode,
+                    blob[1].sha1, blob[0].sha1,
+                    blob[0].name, blob[0].name);
+       diffcore_std(&revs->diffopt);
+       diff_flush(&revs->diffopt);
+       return 0;
+}
+
+static int builtin_diff_index(struct rev_info *revs,
+                             int argc, const char **argv)
+{
+       int cached = 0;
+       while (1 < argc) {
+               const char *arg = argv[1];
+               if (!strcmp(arg, "--cached"))
+                       cached = 1;
+               else if (!strcmp(arg, "--raw"))
+                       revs->diffopt.output_format = DIFF_FORMAT_RAW;
+               else
+                       usage(builtin_diff_usage);
+               argv++; argc--;
+       }
+       /*
+        * Make sure there is one revision (i.e. pending object),
+        * and there is no revision filtering parameters.
+        */
+       if (!revs->pending_objects || revs->pending_objects->next ||
+           revs->max_count != -1 || revs->min_age != -1 ||
+           revs->max_age != -1)
+               usage(builtin_diff_usage);
+       return run_diff_index(revs, cached);
+}
+
+static int builtin_diff_tree(struct rev_info *revs,
+                            int argc, const char **argv,
+                            struct object_list *ent)
+{
+       const unsigned char *(sha1[2]);
+       int swap = 1;
+       while (1 < argc) {
+               const char *arg = argv[1];
+               if (!strcmp(arg, "--raw"))
+                       revs->diffopt.output_format = DIFF_FORMAT_RAW;
+               else
+                       usage(builtin_diff_usage);
+               argv++; argc--;
+       }
+
+       /* We saw two trees, ent[0] and ent[1].
+        * unless ent[0] is unintesting, they are swapped
+        */
+       if (ent[0].item->flags & UNINTERESTING)
+               swap = 0;
+       sha1[swap] = ent[0].item->sha1;
+       sha1[1-swap] = ent[1].item->sha1;
+       diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
+       log_tree_diff_flush(revs);
+       return 0;
+}
+
+static int builtin_diff_combined(struct rev_info *revs,
+                                int argc, const char **argv,
+                                struct object_list *ent,
+                                int ents)
+{
+       const unsigned char (*parent)[20];
+       int i;
+
+       while (1 < argc) {
+               const char *arg = argv[1];
+               if (!strcmp(arg, "--raw"))
+                       revs->diffopt.output_format = DIFF_FORMAT_RAW;
+               else
+                       usage(builtin_diff_usage);
+               argv++; argc--;
+       }
+       if (!revs->dense_combined_merges && !revs->combine_merges)
+               revs->dense_combined_merges = revs->combine_merges = 1;
+       parent = xmalloc(ents * sizeof(*parent));
+       /* Again, the revs are all reverse */
+       for (i = 0; i < ents; i++)
+               memcpy(parent + i, ent[ents - 1 - i].item->sha1, 20);
+       diff_tree_combined(parent[0], parent + 1, ents - 1,
+                          revs->dense_combined_merges, revs);
+       return 0;
+}
+
+void add_head(struct rev_info *revs)
+{
+       unsigned char sha1[20];
+       struct object *obj;
+       if (get_sha1("HEAD", sha1))
+               return;
+       obj = parse_object(sha1);
+       if (!obj)
+               return;
+       add_object(obj, &revs->pending_objects, NULL, "HEAD");
+}
+
+int cmd_diff(int argc, const char **argv, char **envp)
+{
+       struct rev_info rev;
+       struct object_list *list, ent[100];
+       int ents = 0, blobs = 0, paths = 0;
+       const char *path = NULL;
+       struct blobinfo blob[2];
+
+       /*
+        * We could get N tree-ish in the rev.pending_objects list.
+        * Also there could be M blobs there, and P pathspecs.
+        *
+        * N=0, M=0:
+        *      cache vs files (diff-files)
+        * N=0, M=2:
+        *      compare two random blobs.  P must be zero.
+        * N=0, M=1, P=1:
+        *      compare a blob with a working tree file.
+        *
+        * N=1, M=0:
+        *      tree vs cache (diff-index --cached)
+        *
+        * N=2, M=0:
+        *      tree vs tree (diff-tree)
+        *
+        * Other cases are errors.
+        */
+
+       git_config(git_diff_config);
+       init_revisions(&rev);
+       rev.diffopt.output_format = DIFF_FORMAT_PATCH;
+
+       argc = setup_revisions(argc, argv, &rev, NULL);
+       /* Do we have --cached and not have a pending object, then
+        * default to HEAD by hand.  Eek.
+        */
+       if (!rev.pending_objects) {
+               int i;
+               for (i = 1; i < argc; i++) {
+                       const char *arg = argv[i];
+                       if (!strcmp(arg, "--"))
+                               break;
+                       else if (!strcmp(arg, "--cached")) {
+                               add_head(&rev);
+                               break;
+                       }
+               }
+       }
+
+       for (list = rev.pending_objects; list; list = list->next) {
+               struct object *obj = list->item;
+               const char *name = list->name;
+               int flags = (obj->flags & UNINTERESTING);
+               if (!obj->parsed)
+                       obj = parse_object(obj->sha1);
+               obj = deref_tag(obj, NULL, 0);
+               if (!obj)
+                       die("invalid object '%s' given.", name);
+               if (!strcmp(obj->type, commit_type))
+                       obj = &((struct commit *)obj)->tree->object;
+               if (!strcmp(obj->type, tree_type)) {
+                       if (ARRAY_SIZE(ent) <= ents)
+                               die("more than %d trees given: '%s'",
+                                   (int) ARRAY_SIZE(ent), name);
+                       obj->flags |= flags;
+                       ent[ents].item = obj;
+                       ent[ents].name = name;
+                       ents++;
+                       continue;
+               }
+               if (!strcmp(obj->type, blob_type)) {
+                       if (2 <= blobs)
+                               die("more than two blobs given: '%s'", name);
+                       memcpy(blob[blobs].sha1, obj->sha1, 20);
+                       blob[blobs].name = name;
+                       blobs++;
+                       continue;
+
+               }
+               die("unhandled object '%s' given.", name);
+       }
+       if (rev.prune_data) {
+               const char **pathspec = rev.prune_data;
+               while (*pathspec) {
+                       if (!path)
+                               path = *pathspec;
+                       paths++;
+                       pathspec++;
+               }
+       }
+
+       /*
+        * Now, do the arguments look reasonable?
+        */
+       if (!ents) {
+               switch (blobs) {
+               case 0:
+                       return builtin_diff_files(&rev, argc, argv);
+                       break;
+               case 1:
+                       if (paths != 1)
+                               usage(builtin_diff_usage);
+                       return builtin_diff_b_f(&rev, argc, argv, blob, path);
+                       break;
+               case 2:
+                       if (paths)
+                               usage(builtin_diff_usage);
+                       return builtin_diff_blobs(&rev, argc, argv, blob);
+                       break;
+               default:
+                       usage(builtin_diff_usage);
+               }
+       }
+       else if (blobs)
+               usage(builtin_diff_usage);
+       else if (ents == 1)
+               return builtin_diff_index(&rev, argc, argv);
+       else if (ents == 2)
+               return builtin_diff_tree(&rev, argc, argv, ent);
+       else
+               return builtin_diff_combined(&rev, argc, argv, ent, ents);
+       usage(builtin_diff_usage);
+}
diff --git a/builtin-grep.c b/builtin-grep.c
new file mode 100644 (file)
index 0000000..5fac570
--- /dev/null
@@ -0,0 +1,898 @@
+/*
+ * Builtin "git grep"
+ *
+ * Copyright (c) 2006 Junio C Hamano
+ */
+#include "cache.h"
+#include "blob.h"
+#include "tree.h"
+#include "commit.h"
+#include "tag.h"
+#include "tree-walk.h"
+#include "builtin.h"
+#include <regex.h>
+#include <fnmatch.h>
+#include <sys/wait.h>
+
+/*
+ * git grep pathspecs are somewhat different from diff-tree pathspecs;
+ * pathname wildcards are allowed.
+ */
+static int pathspec_matches(const char **paths, const char *name)
+{
+       int namelen, i;
+       if (!paths || !*paths)
+               return 1;
+       namelen = strlen(name);
+       for (i = 0; paths[i]; i++) {
+               const char *match = paths[i];
+               int matchlen = strlen(match);
+               const char *cp, *meta;
+
+               if ((matchlen <= namelen) &&
+                   !strncmp(name, match, matchlen) &&
+                   (match[matchlen-1] == '/' ||
+                    name[matchlen] == '\0' || name[matchlen] == '/'))
+                       return 1;
+               if (!fnmatch(match, name, 0))
+                       return 1;
+               if (name[namelen-1] != '/')
+                       continue;
+
+               /* We are being asked if the directory ("name") is worth
+                * descending into.
+                *
+                * Find the longest leading directory name that does
+                * not have metacharacter in the pathspec; the name
+                * we are looking at must overlap with that directory.
+                */
+               for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
+                       char ch = *cp;
+                       if (ch == '*' || ch == '[' || ch == '?') {
+                               meta = cp;
+                               break;
+                       }
+               }
+               if (!meta)
+                       meta = cp; /* fully literal */
+
+               if (namelen <= meta - match) {
+                       /* Looking at "Documentation/" and
+                        * the pattern says "Documentation/howto/", or
+                        * "Documentation/diff*.txt".  The name we
+                        * have should match prefix.
+                        */
+                       if (!memcmp(match, name, namelen))
+                               return 1;
+                       continue;
+               }
+
+               if (meta - match < namelen) {
+                       /* Looking at "Documentation/howto/" and
+                        * the pattern says "Documentation/h*";
+                        * match up to "Do.../h"; this avoids descending
+                        * into "Documentation/technical/".
+                        */
+                       if (!memcmp(match, name, meta - match))
+                               return 1;
+                       continue;
+               }
+       }
+       return 0;
+}
+
+struct grep_pat {
+       struct grep_pat *next;
+       const char *origin;
+       int no;
+       const char *pattern;
+       regex_t regexp;
+};
+
+struct grep_opt {
+       struct grep_pat *pattern_list;
+       struct grep_pat **pattern_tail;
+       regex_t regexp;
+       unsigned linenum:1;
+       unsigned invert:1;
+       unsigned name_only:1;
+       unsigned unmatch_name_only:1;
+       unsigned count:1;
+       unsigned word_regexp:1;
+       unsigned fixed:1;
+#define GREP_BINARY_DEFAULT    0
+#define GREP_BINARY_NOMATCH    1
+#define GREP_BINARY_TEXT       2
+       unsigned binary:2;
+       int regflags;
+       unsigned pre_context;
+       unsigned post_context;
+};
+
+static void add_pattern(struct grep_opt *opt, const char *pat,
+                       const char *origin, int no)
+{
+       struct grep_pat *p = xcalloc(1, sizeof(*p));
+       p->pattern = pat;
+       p->origin = origin;
+       p->no = no;
+       *opt->pattern_tail = p;
+       opt->pattern_tail = &p->next;
+       p->next = NULL;
+}
+
+static void compile_patterns(struct grep_opt *opt)
+{
+       struct grep_pat *p;
+       for (p = opt->pattern_list; p; p = p->next) {
+               int err = regcomp(&p->regexp, p->pattern, opt->regflags);
+               if (err) {
+                       char errbuf[1024];
+                       char where[1024];
+                       if (p->no)
+                               sprintf(where, "In '%s' at %d, ",
+                                       p->origin, p->no);
+                       else if (p->origin)
+                               sprintf(where, "%s, ", p->origin);
+                       else
+                               where[0] = 0;
+                       regerror(err, &p->regexp, errbuf, 1024);
+                       regfree(&p->regexp);
+                       die("%s'%s': %s", where, p->pattern, errbuf);
+               }
+       }
+}
+
+static char *end_of_line(char *cp, unsigned long *left)
+{
+       unsigned long l = *left;
+       while (l && *cp != '\n') {
+               l--;
+               cp++;
+       }
+       *left = l;
+       return cp;
+}
+
+static int word_char(char ch)
+{
+       return isalnum(ch) || ch == '_';
+}
+
+static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
+                     const char *name, unsigned lno, char sign)
+{
+       printf("%s%c", name, sign);
+       if (opt->linenum)
+               printf("%d%c", lno, sign);
+       printf("%.*s\n", (int)(eol-bol), bol);
+}
+
+/*
+ * NEEDSWORK: share code with diff.c
+ */
+#define FIRST_FEW_BYTES 8000
+static int buffer_is_binary(const char *ptr, unsigned long size)
+{
+       if (FIRST_FEW_BYTES < size)
+               size = FIRST_FEW_BYTES;
+       if (memchr(ptr, 0, size))
+               return 1;
+       return 0;
+}
+
+static int fixmatch(const char *pattern, char *line, regmatch_t *match)
+{
+       char *hit = strstr(line, pattern);
+       if (!hit) {
+               match->rm_so = match->rm_eo = -1;
+               return REG_NOMATCH;
+       }
+       else {
+               match->rm_so = hit - line;
+               match->rm_eo = match->rm_so + strlen(pattern);
+               return 0;
+       }
+}
+
+static int grep_buffer(struct grep_opt *opt, const char *name,
+                      char *buf, unsigned long size)
+{
+       char *bol = buf;
+       unsigned long left = size;
+       unsigned lno = 1;
+       struct pre_context_line {
+               char *bol;
+               char *eol;
+       } *prev = NULL, *pcl;
+       unsigned last_hit = 0;
+       unsigned last_shown = 0;
+       int binary_match_only = 0;
+       const char *hunk_mark = "";
+       unsigned count = 0;
+
+       if (buffer_is_binary(buf, size)) {
+               switch (opt->binary) {
+               case GREP_BINARY_DEFAULT:
+                       binary_match_only = 1;
+                       break;
+               case GREP_BINARY_NOMATCH:
+                       return 0; /* Assume unmatch */
+                       break;
+               default:
+                       break;
+               }
+       }
+
+       if (opt->pre_context)
+               prev = xcalloc(opt->pre_context, sizeof(*prev));
+       if (opt->pre_context || opt->post_context)
+               hunk_mark = "--\n";
+
+       while (left) {
+               regmatch_t pmatch[10];
+               char *eol, ch;
+               int hit = 0;
+               struct grep_pat *p;
+
+               eol = end_of_line(bol, &left);
+               ch = *eol;
+               *eol = 0;
+
+               for (p = opt->pattern_list; p; p = p->next) {
+                       if (!opt->fixed) {
+                               regex_t *exp = &p->regexp;
+                               hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
+                                              pmatch, 0);
+                       }
+                       else {
+                               hit = !fixmatch(p->pattern, bol, pmatch);
+                       }
+
+                       if (hit && opt->word_regexp) {
+                               /* Match beginning must be either
+                                * beginning of the line, or at word
+                                * boundary (i.e. the last char must
+                                * not be alnum or underscore).
+                                */
+                               if ((pmatch[0].rm_so < 0) ||
+                                   (eol - bol) <= pmatch[0].rm_so ||
+                                   (pmatch[0].rm_eo < 0) ||
+                                   (eol - bol) < pmatch[0].rm_eo)
+                                       die("regexp returned nonsense");
+                               if (pmatch[0].rm_so != 0 &&
+                                   word_char(bol[pmatch[0].rm_so-1]))
+                                       hit = 0;
+                               if (pmatch[0].rm_eo != (eol-bol) &&
+                                   word_char(bol[pmatch[0].rm_eo]))
+                                       hit = 0;
+                       }
+                       if (hit)
+                               break;
+               }
+               /* "grep -v -e foo -e bla" should list lines
+                * that do not have either, so inversion should
+                * be done outside.
+                */
+               if (opt->invert)
+                       hit = !hit;
+               if (opt->unmatch_name_only) {
+                       if (hit)
+                               return 0;
+                       goto next_line;
+               }
+               if (hit) {
+                       count++;
+                       if (binary_match_only) {
+                               printf("Binary file %s matches\n", name);
+                               return 1;
+                       }
+                       if (opt->name_only) {
+                               printf("%s\n", name);
+                               return 1;
+                       }
+                       /* Hit at this line.  If we haven't shown the
+                        * pre-context lines, we would need to show them.
+                        * When asked to do "count", this still show
+                        * the context which is nonsense, but the user
+                        * deserves to get that ;-).
+                        */
+                       if (opt->pre_context) {
+                               unsigned from;
+                               if (opt->pre_context < lno)
+                                       from = lno - opt->pre_context;
+                               else
+                                       from = 1;
+                               if (from <= last_shown)
+                                       from = last_shown + 1;
+                               if (last_shown && from != last_shown + 1)
+                                       printf(hunk_mark);
+                               while (from < lno) {
+                                       pcl = &prev[lno-from-1];
+                                       show_line(opt, pcl->bol, pcl->eol,
+                                                 name, from, '-');
+                                       from++;
+                               }
+                               last_shown = lno-1;
+                       }
+                       if (last_shown && lno != last_shown + 1)
+                               printf(hunk_mark);
+                       if (!opt->count)
+                               show_line(opt, bol, eol, name, lno, ':');
+                       last_shown = last_hit = lno;
+               }
+               else if (last_hit &&
+                        lno <= last_hit + opt->post_context) {
+                       /* If the last hit is within the post context,
+                        * we need to show this line.
+                        */
+                       if (last_shown && lno != last_shown + 1)
+                               printf(hunk_mark);
+                       show_line(opt, bol, eol, name, lno, '-');
+                       last_shown = lno;
+               }
+               if (opt->pre_context) {
+                       memmove(prev+1, prev,
+                               (opt->pre_context-1) * sizeof(*prev));
+                       prev->bol = bol;
+                       prev->eol = eol;
+               }
+
+       next_line:
+               *eol = ch;
+               bol = eol + 1;
+               if (!left)
+                       break;
+               left--;
+               lno++;
+       }
+
+       if (opt->unmatch_name_only) {
+               /* We did not see any hit, so we want to show this */
+               printf("%s\n", name);
+               return 1;
+       }
+
+       /* NEEDSWORK:
+        * The real "grep -c foo *.c" gives many "bar.c:0" lines,
+        * which feels mostly useless but sometimes useful.  Maybe
+        * make it another option?  For now suppress them.
+        */
+       if (opt->count && count)
+               printf("%s:%u\n", name, count);
+       return !!last_hit;
+}
+
+static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
+{
+       unsigned long size;
+       char *data;
+       char type[20];
+       int hit;
+       data = read_sha1_file(sha1, type, &size);
+       if (!data) {
+               error("'%s': unable to read %s", name, sha1_to_hex(sha1));
+               return 0;
+       }
+       hit = grep_buffer(opt, name, data, size);
+       free(data);
+       return hit;
+}
+
+static int grep_file(struct grep_opt *opt, const char *filename)
+{
+       struct stat st;
+       int i;
+       char *data;
+       if (lstat(filename, &st) < 0) {
+       err_ret:
+               if (errno != ENOENT)
+                       error("'%s': %s", filename, strerror(errno));
+               return 0;
+       }
+       if (!st.st_size)
+               return 0; /* empty file -- no grep hit */
+       if (!S_ISREG(st.st_mode))
+               return 0;
+       i = open(filename, O_RDONLY);
+       if (i < 0)
+               goto err_ret;
+       data = xmalloc(st.st_size + 1);
+       if (st.st_size != xread(i, data, st.st_size)) {
+               error("'%s': short read %s", filename, strerror(errno));
+               close(i);
+               free(data);
+               return 0;
+       }
+       close(i);
+       i = grep_buffer(opt, filename, data, st.st_size);
+       free(data);
+       return i;
+}
+
+static int exec_grep(int argc, const char **argv)
+{
+       pid_t pid;
+       int status;
+
+       argv[argc] = NULL;
+       pid = fork();
+       if (pid < 0)
+               return pid;
+       if (!pid) {
+               execvp("grep", (char **) argv);
+               exit(255);
+       }
+       while (waitpid(pid, &status, 0) < 0) {
+               if (errno == EINTR)
+                       continue;
+               return -1;
+       }
+       if (WIFEXITED(status)) {
+               if (!WEXITSTATUS(status))
+                       return 1;
+               return 0;
+       }
+       return -1;
+}
+
+#define MAXARGS 1000
+#define ARGBUF 4096
+#define push_arg(a) do { \
+       if (nr < MAXARGS) argv[nr++] = (a); \
+       else die("maximum number of args exceeded"); \
+       } while (0)
+
+static int external_grep(struct grep_opt *opt, const char **paths, int cached)
+{
+       int i, nr, argc, hit, len;
+       const char *argv[MAXARGS+1];
+       char randarg[ARGBUF];
+       char *argptr = randarg;
+       struct grep_pat *p;
+
+       len = nr = 0;
+       push_arg("grep");
+       if (opt->fixed)
+               push_arg("-F");
+       if (opt->linenum)
+               push_arg("-n");
+       if (opt->regflags & REG_EXTENDED)
+               push_arg("-E");
+       if (opt->regflags & REG_ICASE)
+               push_arg("-i");
+       if (opt->word_regexp)
+               push_arg("-w");
+       if (opt->name_only)
+               push_arg("-l");
+       if (opt->unmatch_name_only)
+               push_arg("-L");
+       if (opt->count)
+               push_arg("-c");
+       if (opt->post_context || opt->pre_context) {
+               if (opt->post_context != opt->pre_context) {
+                       if (opt->pre_context) {
+                               push_arg("-B");
+                               len += snprintf(argptr, sizeof(randarg)-len,
+                                               "%u", opt->pre_context);
+                               if (sizeof(randarg) <= len)
+                                       die("maximum length of args exceeded");
+                               push_arg(argptr);
+                               argptr += len;
+                       }
+                       if (opt->post_context) {
+                               push_arg("-A");
+                               len += snprintf(argptr, sizeof(randarg)-len,
+                                               "%u", opt->post_context);
+                               if (sizeof(randarg) <= len)
+                                       die("maximum length of args exceeded");
+                               push_arg(argptr);
+                               argptr += len;
+                       }
+               }
+               else {
+                       push_arg("-C");
+                       len += snprintf(argptr, sizeof(randarg)-len,
+                                       "%u", opt->post_context);
+                       if (sizeof(randarg) <= len)
+                               die("maximum length of args exceeded");
+                       push_arg(argptr);
+                       argptr += len;
+               }
+       }
+       for (p = opt->pattern_list; p; p = p->next) {
+               push_arg("-e");
+               push_arg(p->pattern);
+       }
+
+       /*
+        * To make sure we get the header printed out when we want it,
+        * add /dev/null to the paths to grep.  This is unnecessary
+        * (and wrong) with "-l" or "-L", which always print out the
+        * name anyway.
+        *
+        * GNU grep has "-H", but this is portable.
+        */
+       if (!opt->name_only && !opt->unmatch_name_only)
+               push_arg("/dev/null");
+
+       hit = 0;
+       argc = nr;
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce = active_cache[i];
+               char *name;
+               if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
+                       continue;
+               if (!pathspec_matches(paths, ce->name))
+                       continue;
+               name = ce->name;
+               if (name[0] == '-') {
+                       int len = ce_namelen(ce);
+                       name = xmalloc(len + 3);
+                       memcpy(name, "./", 2);
+                       memcpy(name + 2, ce->name, len + 1);
+               }
+               argv[argc++] = name;
+               if (argc < MAXARGS)
+                       continue;
+               hit += exec_grep(argc, argv);
+               argc = nr;
+       }
+       if (argc > nr)
+               hit += exec_grep(argc, argv);
+       return 0;
+}
+
+static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
+{
+       int hit = 0;
+       int nr;
+       read_cache();
+
+#ifdef __unix__
+       /*
+        * Use the external "grep" command for the case where
+        * we grep through the checked-out files. It tends to
+        * be a lot more optimized
+        */
+       if (!cached) {
+               hit = external_grep(opt, paths, cached);
+               if (hit >= 0)
+                       return hit;
+       }
+#endif
+
+       for (nr = 0; nr < active_nr; nr++) {
+               struct cache_entry *ce = active_cache[nr];
+               if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
+                       continue;
+               if (!pathspec_matches(paths, ce->name))
+                       continue;
+               if (cached)
+                       hit |= grep_sha1(opt, ce->sha1, ce->name);
+               else
+                       hit |= grep_file(opt, ce->name);
+       }
+       return hit;
+}
+
+static int grep_tree(struct grep_opt *opt, const char **paths,
+                    struct tree_desc *tree,
+                    const char *tree_name, const char *base)
+{
+       int len;
+       int hit = 0;
+       struct name_entry entry;
+       char *down;
+       char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
+
+       if (tree_name[0]) {
+               int offset = sprintf(path_buf, "%s:", tree_name);
+               down = path_buf + offset;
+               strcat(down, base);
+       }
+       else {
+               down = path_buf;
+               strcpy(down, base);
+       }
+       len = strlen(path_buf);
+
+       while (tree_entry(tree, &entry)) {
+               strcpy(path_buf + len, entry.path);
+
+               if (S_ISDIR(entry.mode))
+                       /* Match "abc/" against pathspec to
+                        * decide if we want to descend into "abc"
+                        * directory.
+                        */
+                       strcpy(path_buf + len + entry.pathlen, "/");
+
+               if (!pathspec_matches(paths, down))
+                       ;
+               else if (S_ISREG(entry.mode))
+                       hit |= grep_sha1(opt, entry.sha1, path_buf);
+               else if (S_ISDIR(entry.mode)) {
+                       char type[20];
+                       struct tree_desc sub;
+                       void *data;
+                       data = read_sha1_file(entry.sha1, type, &sub.size);
+                       if (!data)
+                               die("unable to read tree (%s)",
+                                   sha1_to_hex(entry.sha1));
+                       sub.buf = data;
+                       hit |= grep_tree(opt, paths, &sub, tree_name, down);
+                       free(data);
+               }
+       }
+       return hit;
+}
+
+static int grep_object(struct grep_opt *opt, const char **paths,
+                      struct object *obj, const char *name)
+{
+       if (!strcmp(obj->type, blob_type))
+               return grep_sha1(opt, obj->sha1, name);
+       if (!strcmp(obj->type, commit_type) ||
+           !strcmp(obj->type, tree_type)) {
+               struct tree_desc tree;
+               void *data;
+               int hit;
+               data = read_object_with_reference(obj->sha1, tree_type,
+                                                 &tree.size, NULL);
+               if (!data)
+                       die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
+               tree.buf = data;
+               hit = grep_tree(opt, paths, &tree, name, "");
+               free(data);
+               return hit;
+       }
+       die("unable to grep from object of type %s", obj->type);
+}
+
+static const char builtin_grep_usage[] =
+"git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
+
+int cmd_grep(int argc, const char **argv, char **envp)
+{
+       int hit = 0;
+       int cached = 0;
+       int seen_dashdash = 0;
+       struct grep_opt opt;
+       struct object_list *list, **tail, *object_list = NULL;
+       const char *prefix = setup_git_directory();
+       const char **paths = NULL;
+       int i;
+
+       memset(&opt, 0, sizeof(opt));
+       opt.pattern_tail = &opt.pattern_list;
+       opt.regflags = REG_NEWLINE;
+
+       /*
+        * If there is no -- then the paths must exist in the working
+        * tree.  If there is no explicit pattern specified with -e or
+        * -f, we take the first unrecognized non option to be the
+        * pattern, but then what follows it must be zero or more
+        * valid refs up to the -- (if exists), and then existing
+        * paths.  If there is an explicit pattern, then the first
+        * unrecocnized non option is the beginning of the refs list
+        * that continues up to the -- (if exists), and then paths.
+        */
+
+       tail = &object_list;
+       while (1 < argc) {
+               const char *arg = argv[1];
+               argc--; argv++;
+               if (!strcmp("--cached", arg)) {
+                       cached = 1;
+                       continue;
+               }
+               if (!strcmp("-a", arg) ||
+                   !strcmp("--text", arg)) {
+                       opt.binary = GREP_BINARY_TEXT;
+                       continue;
+               }
+               if (!strcmp("-i", arg) ||
+                   !strcmp("--ignore-case", arg)) {
+                       opt.regflags |= REG_ICASE;
+                       continue;
+               }
+               if (!strcmp("-I", arg)) {
+                       opt.binary = GREP_BINARY_NOMATCH;
+                       continue;
+               }
+               if (!strcmp("-v", arg) ||
+                   !strcmp("--invert-match", arg)) {
+                       opt.invert = 1;
+                       continue;
+               }
+               if (!strcmp("-E", arg) ||
+                   !strcmp("--extended-regexp", arg)) {
+                       opt.regflags |= REG_EXTENDED;
+                       continue;
+               }
+               if (!strcmp("-F", arg) ||
+                   !strcmp("--fixed-strings", arg)) {
+                       opt.fixed = 1;
+                       continue;
+               }
+               if (!strcmp("-G", arg) ||
+                   !strcmp("--basic-regexp", arg)) {
+                       opt.regflags &= ~REG_EXTENDED;
+                       continue;
+               }
+               if (!strcmp("-n", arg)) {
+                       opt.linenum = 1;
+                       continue;
+               }
+               if (!strcmp("-H", arg)) {
+                       /* We always show the pathname, so this
+                        * is a noop.
+                        */
+                       continue;
+               }
+               if (!strcmp("-l", arg) ||
+                   !strcmp("--files-with-matches", arg)) {
+                       opt.name_only = 1;
+                       continue;
+               }
+               if (!strcmp("-L", arg) ||
+                   !strcmp("--files-without-match", arg)) {
+                       opt.unmatch_name_only = 1;
+                       continue;
+               }
+               if (!strcmp("-c", arg) ||
+                   !strcmp("--count", arg)) {
+                       opt.count = 1;
+                       continue;
+               }
+               if (!strcmp("-w", arg) ||
+                   !strcmp("--word-regexp", arg)) {
+                       opt.word_regexp = 1;
+                       continue;
+               }
+               if (!strncmp("-A", arg, 2) ||
+                   !strncmp("-B", arg, 2) ||
+                   !strncmp("-C", arg, 2) ||
+                   (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
+                       unsigned num;
+                       const char *scan;
+                       switch (arg[1]) {
+                       case 'A': case 'B': case 'C':
+                               if (!arg[2]) {
+                                       if (argc <= 1)
+                                               usage(builtin_grep_usage);
+                                       scan = *++argv;
+                                       argc--;
+                               }
+                               else
+                                       scan = arg + 2;
+                               break;
+                       default:
+                               scan = arg + 1;
+                               break;
+                       }
+                       if (sscanf(scan, "%u", &num) != 1)
+                               usage(builtin_grep_usage);
+                       switch (arg[1]) {
+                       case 'A':
+                               opt.post_context = num;
+                               break;
+                       default:
+                       case 'C':
+                               opt.post_context = num;
+                       case 'B':
+                               opt.pre_context = num;
+                               break;
+                       }
+                       continue;
+               }
+               if (!strcmp("-f", arg)) {
+                       FILE *patterns;
+                       int lno = 0;
+                       char buf[1024];
+                       if (argc <= 1)
+                               usage(builtin_grep_usage);
+                       patterns = fopen(argv[1], "r");
+                       if (!patterns)
+                               die("'%s': %s", argv[1], strerror(errno));
+                       while (fgets(buf, sizeof(buf), patterns)) {
+                               int len = strlen(buf);
+                               if (buf[len-1] == '\n')
+                                       buf[len-1] = 0;
+                               /* ignore empty line like grep does */
+                               if (!buf[0])
+                                       continue;
+                               add_pattern(&opt, strdup(buf), argv[1], ++lno);
+                       }
+                       fclose(patterns);
+                       argv++;
+                       argc--;
+                       continue;
+               }
+               if (!strcmp("-e", arg)) {
+                       if (1 < argc) {
+                               add_pattern(&opt, argv[1], "-e option", 0);
+                               argv++;
+                               argc--;
+                               continue;
+                       }
+                       usage(builtin_grep_usage);
+               }
+               if (!strcmp("--", arg))
+                       break;
+               if (*arg == '-')
+                       usage(builtin_grep_usage);
+
+               /* First unrecognized non-option token */
+               if (!opt.pattern_list) {
+                       add_pattern(&opt, arg, "command line", 0);
+                       break;
+               }
+               else {
+                       /* We are looking at the first path or rev;
+                        * it is found at argv[1] after leaving the
+                        * loop.
+                        */
+                       argc++; argv--;
+                       break;
+               }
+       }
+
+       if (!opt.pattern_list)
+               die("no pattern given.");
+       if ((opt.regflags != REG_NEWLINE) && opt.fixed)
+               die("cannot mix --fixed-strings and regexp");
+       if (!opt.fixed)
+               compile_patterns(&opt);
+
+       /* Check revs and then paths */
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+               unsigned char sha1[20];
+               /* Is it a rev? */
+               if (!get_sha1(arg, sha1)) {
+                       struct object *object = parse_object(sha1);
+                       struct object_list *elem;
+                       if (!object)
+                               die("bad object %s", arg);
+                       elem = object_list_insert(object, tail);
+                       elem->name = arg;
+                       tail = &elem->next;
+                       continue;
+               }
+               if (!strcmp(arg, "--")) {
+                       i++;
+                       seen_dashdash = 1;
+               }
+               break;
+       }
+
+       /* The rest are paths */
+       if (!seen_dashdash) {
+               int j;
+               for (j = i; j < argc; j++)
+                       verify_filename(prefix, argv[j]);
+       }
+
+       if (i < argc)
+               paths = get_pathspec(prefix, argv + i);
+       else if (prefix) {
+               paths = xcalloc(2, sizeof(const char *));
+               paths[0] = prefix;
+               paths[1] = NULL;
+       }
+
+       if (!object_list)
+               return !grep_cache(&opt, paths, cached);
+
+       if (cached)
+               die("both --cached and trees are given.");
+
+       for (list = object_list; list; list = list->next) {
+               struct object *real_obj;
+               real_obj = deref_tag(list->item, NULL, 0);
+               if (grep_object(&opt, paths, real_obj, list->name))
+                       hit = 1;
+       }
+       return !hit;
+}
index 10a59cc..7470faa 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Builtin help-related commands (help, usage, version)
  */
+#include <sys/ioctl.h>
 #include "cache.h"
 #include "builtin.h"
 #include "exec_cmd.h"
diff --git a/builtin-init-db.c b/builtin-init-db.c
new file mode 100644 (file)
index 0000000..6a24e9b
--- /dev/null
@@ -0,0 +1,308 @@
+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#include "cache.h"
+#include "builtin.h"
+
+#ifndef DEFAULT_GIT_TEMPLATE_DIR
+#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
+#endif
+
+static void safe_create_dir(const char *dir, int share)
+{
+       if (mkdir(dir, 0777) < 0) {
+               if (errno != EEXIST) {
+                       perror(dir);
+                       exit(1);
+               }
+       }
+       else if (share && adjust_shared_perm(dir))
+               die("Could not make %s writable by group\n", dir);
+}
+
+static int copy_file(const char *dst, const char *src, int mode)
+{
+       int fdi, fdo, status;
+
+       mode = (mode & 0111) ? 0777 : 0666;
+       if ((fdi = open(src, O_RDONLY)) < 0)
+               return fdi;
+       if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
+               close(fdi);
+               return fdo;
+       }
+       status = copy_fd(fdi, fdo);
+       close(fdo);
+
+       if (!status && adjust_shared_perm(dst))
+               return -1;
+
+       return status;
+}
+
+static void copy_templates_1(char *path, int baselen,
+                            char *template, int template_baselen,
+                            DIR *dir)
+{
+       struct dirent *de;
+
+       /* Note: if ".git/hooks" file exists in the repository being
+        * re-initialized, /etc/core-git/templates/hooks/update would
+        * cause git-init-db to fail here.  I think this is sane but
+        * it means that the set of templates we ship by default, along
+        * with the way the namespace under .git/ is organized, should
+        * be really carefully chosen.
+        */
+       safe_create_dir(path, 1);
+       while ((de = readdir(dir)) != NULL) {
+               struct stat st_git, st_template;
+               int namelen;
+               int exists = 0;
+
+               if (de->d_name[0] == '.')
+                       continue;
+               namelen = strlen(de->d_name);
+               if ((PATH_MAX <= baselen + namelen) ||
+                   (PATH_MAX <= template_baselen + namelen))
+                       die("insanely long template name %s", de->d_name);
+               memcpy(path + baselen, de->d_name, namelen+1);
+               memcpy(template + template_baselen, de->d_name, namelen+1);
+               if (lstat(path, &st_git)) {
+                       if (errno != ENOENT)
+                               die("cannot stat %s", path);
+               }
+               else
+                       exists = 1;
+
+               if (lstat(template, &st_template))
+                       die("cannot stat template %s", template);
+
+               if (S_ISDIR(st_template.st_mode)) {
+                       DIR *subdir = opendir(template);
+                       int baselen_sub = baselen + namelen;
+                       int template_baselen_sub = template_baselen + namelen;
+                       if (!subdir)
+                               die("cannot opendir %s", template);
+                       path[baselen_sub++] =
+                               template[template_baselen_sub++] = '/';
+                       path[baselen_sub] =
+                               template[template_baselen_sub] = 0;
+                       copy_templates_1(path, baselen_sub,
+                                        template, template_baselen_sub,
+                                        subdir);
+                       closedir(subdir);
+               }
+               else if (exists)
+                       continue;
+               else if (S_ISLNK(st_template.st_mode)) {
+                       char lnk[256];
+                       int len;
+                       len = readlink(template, lnk, sizeof(lnk));
+                       if (len < 0)
+                               die("cannot readlink %s", template);
+                       if (sizeof(lnk) <= len)
+                               die("insanely long symlink %s", template);
+                       lnk[len] = 0;
+                       if (symlink(lnk, path))
+                               die("cannot symlink %s %s", lnk, path);
+               }
+               else if (S_ISREG(st_template.st_mode)) {
+                       if (copy_file(path, template, st_template.st_mode))
+                               die("cannot copy %s to %s", template, path);
+               }
+               else
+                       error("ignoring template %s", template);
+       }
+}
+
+static void copy_templates(const char *git_dir, int len, const char *template_dir)
+{
+       char path[PATH_MAX];
+       char template_path[PATH_MAX];
+       int template_len;
+       DIR *dir;
+
+       if (!template_dir)
+               template_dir = DEFAULT_GIT_TEMPLATE_DIR;
+       strcpy(template_path, template_dir);
+       template_len = strlen(template_path);
+       if (template_path[template_len-1] != '/') {
+               template_path[template_len++] = '/';
+               template_path[template_len] = 0;
+       }
+       dir = opendir(template_path);
+       if (!dir) {
+               fprintf(stderr, "warning: templates not found %s\n",
+                       template_dir);
+               return;
+       }
+
+       /* Make sure that template is from the correct vintage */
+       strcpy(template_path + template_len, "config");
+       repository_format_version = 0;
+       git_config_from_file(check_repository_format_version,
+                            template_path);
+       template_path[template_len] = 0;
+
+       if (repository_format_version &&
+           repository_format_version != GIT_REPO_VERSION) {
+               fprintf(stderr, "warning: not copying templates of "
+                       "a wrong format version %d from '%s'\n",
+                       repository_format_version,
+                       template_dir);
+               closedir(dir);
+               return;
+       }
+
+       memcpy(path, git_dir, len);
+       path[len] = 0;
+       copy_templates_1(path, len,
+                        template_path, template_len,
+                        dir);
+       closedir(dir);
+}
+
+static void create_default_files(const char *git_dir, const char *template_path)
+{
+       unsigned len = strlen(git_dir);
+       static char path[PATH_MAX];
+       unsigned char sha1[20];
+       struct stat st1;
+       char repo_version_string[10];
+
+       if (len > sizeof(path)-50)
+               die("insane git directory %s", git_dir);
+       memcpy(path, git_dir, len);
+
+       if (len && path[len-1] != '/')
+               path[len++] = '/';
+
+       /*
+        * Create .git/refs/{heads,tags}
+        */
+       strcpy(path + len, "refs");
+       safe_create_dir(path, 1);
+       strcpy(path + len, "refs/heads");
+       safe_create_dir(path, 1);
+       strcpy(path + len, "refs/tags");
+       safe_create_dir(path, 1);
+
+       /* First copy the templates -- we might have the default
+        * config file there, in which case we would want to read
+        * from it after installing.
+        */
+       path[len] = 0;
+       copy_templates(path, len, template_path);
+
+       git_config(git_default_config);
+
+       /*
+        * We would have created the above under user's umask -- under
+        * shared-repository settings, we would need to fix them up.
+        */
+       if (shared_repository) {
+               path[len] = 0;
+               adjust_shared_perm(path);
+               strcpy(path + len, "refs");
+               adjust_shared_perm(path);
+               strcpy(path + len, "refs/heads");
+               adjust_shared_perm(path);
+               strcpy(path + len, "refs/tags");
+               adjust_shared_perm(path);
+       }
+
+       /*
+        * Create the default symlink from ".git/HEAD" to the "master"
+        * branch, if it does not exist yet.
+        */
+       strcpy(path + len, "HEAD");
+       if (read_ref(path, sha1) < 0) {
+               if (create_symref(path, "refs/heads/master") < 0)
+                       exit(1);
+       }
+
+       /* This forces creation of new config file */
+       sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
+       git_config_set("core.repositoryformatversion", repo_version_string);
+
+       path[len] = 0;
+       strcpy(path + len, "config");
+
+       /* Check filemode trustability */
+       if (!lstat(path, &st1)) {
+               struct stat st2;
+               int filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
+                               !lstat(path, &st2) &&
+                               st1.st_mode != st2.st_mode);
+               git_config_set("core.filemode",
+                              filemode ? "true" : "false");
+       }
+}
+
+static const char init_db_usage[] =
+"git-init-db [--template=<template-directory>] [--shared]";
+
+/*
+ * If you want to, you can share the DB area with any number of branches.
+ * That has advantages: you can save space by sharing all the SHA1 objects.
+ * On the other hand, it might just make lookup slower and messier. You
+ * be the judge.  The default case is to have one DB per managed directory.
+ */
+int cmd_init_db(int argc, const char **argv, char **envp)
+{
+       const char *git_dir;
+       const char *sha1_dir;
+       const char *template_dir = NULL;
+       char *path;
+       int len, i;
+
+       for (i = 1; i < argc; i++, argv++) {
+               const char *arg = argv[1];
+               if (!strncmp(arg, "--template=", 11))
+                       template_dir = arg+11;
+               else if (!strcmp(arg, "--shared"))
+                       shared_repository = 1;
+               else
+                       die(init_db_usage);
+       }
+
+       /*
+        * Set up the default .git directory contents
+        */
+       git_dir = getenv(GIT_DIR_ENVIRONMENT);
+       if (!git_dir) {
+               git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
+               fprintf(stderr, "defaulting to local storage area\n");
+       }
+       safe_create_dir(git_dir, 0);
+
+       /* Check to see if the repository version is right.
+        * Note that a newly created repository does not have
+        * config file, so this will not fail.  What we are catching
+        * is an attempt to reinitialize new repository with an old tool.
+        */
+       check_repository_format();
+
+       create_default_files(git_dir, template_dir);
+
+       /*
+        * And set up the object store.
+        */
+       sha1_dir = get_object_directory();
+       len = strlen(sha1_dir);
+       path = xmalloc(len + 40);
+       memcpy(path, sha1_dir, len);
+
+       safe_create_dir(sha1_dir, 1);
+       strcpy(path+len, "/pack");
+       safe_create_dir(path, 1);
+       strcpy(path+len, "/info");
+       safe_create_dir(path, 1);
+
+       if (shared_repository)
+               git_config_set("core.sharedrepository", "true");
+
+       return 0;
+}
index 69f2911..f4d974a 100644 (file)
@@ -9,6 +9,10 @@
 #include "diff.h"
 #include "revision.h"
 #include "log-tree.h"
+#include "builtin.h"
+
+/* this is in builtin-diff.c */
+void add_head(struct rev_info *revs);
 
 static int cmd_log_wc(int argc, const char **argv, char **envp,
                      struct rev_info *rev)
@@ -19,6 +23,13 @@ static int cmd_log_wc(int argc, const char **argv, char **envp,
        rev->commit_format = CMIT_FMT_DEFAULT;
        rev->verbose_header = 1;
        argc = setup_revisions(argc, argv, rev, "HEAD");
+       if (rev->always_show_header) {
+               if (rev->diffopt.pickaxe || rev->diffopt.filter) {
+                       rev->always_show_header = 0;
+                       if (rev->diffopt.output_format == DIFF_FORMAT_RAW)
+                               rev->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
+               }
+       }
 
        if (argc > 1)
                die("unrecognized argument: %s", argv[1]);
@@ -40,6 +51,7 @@ int cmd_whatchanged(int argc, const char **argv, char **envp)
        init_revisions(&rev);
        rev.diff = 1;
        rev.diffopt.recursive = 1;
+       rev.simplify_history = 0;
        return cmd_log_wc(argc, argv, envp, &rev);
 }
 
@@ -67,3 +79,230 @@ int cmd_log(int argc, const char **argv, char **envp)
        rev.diffopt.recursive = 1;
        return cmd_log_wc(argc, argv, envp, &rev);
 }
+
+static int istitlechar(char c)
+{
+       return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
+               (c >= '0' && c <= '9') || c == '.' || c == '_';
+}
+
+static char *extra_headers = NULL;
+static int extra_headers_size = 0;
+
+static int git_format_config(const char *var, const char *value)
+{
+       if (!strcmp(var, "format.headers")) {
+               int len = strlen(value);
+               extra_headers_size += len + 1;
+               extra_headers = realloc(extra_headers, extra_headers_size);
+               extra_headers[extra_headers_size - len - 1] = 0;
+               strcat(extra_headers, value);
+               return 0;
+       }
+       return git_default_config(var, value);
+}
+
+
+static FILE *realstdout = NULL;
+static const char *output_directory = NULL;
+
+static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
+{
+       char filename[1024];
+       char *sol;
+       int len = 0;
+
+       if (output_directory) {
+               safe_strncpy(filename, output_directory, 1010);
+               len = strlen(filename);
+               if (filename[len - 1] != '/')
+                       filename[len++] = '/';
+       }
+
+       sprintf(filename + len, "%04d", nr);
+       len = strlen(filename);
+
+       sol = strstr(commit->buffer, "\n\n");
+       if (sol) {
+               int j, space = 1;
+
+               sol += 2;
+               /* strip [PATCH] or [PATCH blabla] */
+               if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
+                       char *eos = strchr(sol + 6, ']');
+                       if (eos) {
+                               while (isspace(*eos))
+                                       eos++;
+                               sol = eos;
+                       }
+               }
+
+               for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
+                       if (istitlechar(sol[j])) {
+                               if (space) {
+                                       filename[len++] = '-';
+                                       space = 0;
+                               }
+                               filename[len++] = sol[j];
+                               if (sol[j] == '.')
+                                       while (sol[j + 1] == '.')
+                                               j++;
+                       } else
+                               space = 1;
+               }
+               while (filename[len - 1] == '.' || filename[len - 1] == '-')
+                       len--;
+       }
+       strcpy(filename + len, ".txt");
+       fprintf(realstdout, "%s\n", filename);
+       freopen(filename, "w", stdout);
+}
+
+int cmd_format_patch(int argc, const char **argv, char **envp)
+{
+       struct commit *commit;
+       struct commit **list = NULL;
+       struct rev_info rev;
+       int nr = 0, total, i, j;
+       int use_stdout = 0;
+       int numbered = 0;
+       int start_number = -1;
+       int keep_subject = 0;
+       char *add_signoff = NULL;
+
+       init_revisions(&rev);
+       rev.commit_format = CMIT_FMT_EMAIL;
+       rev.verbose_header = 1;
+       rev.diff = 1;
+       rev.diffopt.with_raw = 0;
+       rev.diffopt.with_stat = 1;
+       rev.combine_merges = 0;
+       rev.ignore_merges = 1;
+       rev.diffopt.output_format = DIFF_FORMAT_PATCH;
+
+       git_config(git_format_config);
+       rev.extra_headers = extra_headers;
+
+       /*
+        * Parse the arguments before setup_revisions(), or something
+        * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
+        * possibly a valid SHA1.
+        */
+       for (i = 1, j = 1; i < argc; i++) {
+               if (!strcmp(argv[i], "--stdout"))
+                       use_stdout = 1;
+               else if (!strcmp(argv[i], "-n") ||
+                               !strcmp(argv[i], "--numbered"))
+                       numbered = 1;
+               else if (!strncmp(argv[i], "--start-number=", 15))
+                       start_number = strtol(argv[i] + 15, NULL, 10);
+               else if (!strcmp(argv[i], "--start-number")) {
+                       i++;
+                       if (i == argc)
+                               die("Need a number for --start-number");
+                       start_number = strtol(argv[i], NULL, 10);
+               }
+               else if (!strcmp(argv[i], "-k") ||
+                               !strcmp(argv[i], "--keep-subject")) {
+                       keep_subject = 1;
+                       rev.total = -1;
+               }
+               else if (!strcmp(argv[i], "--output-directory") ||
+                        !strcmp(argv[i], "-o")) {
+                       i++;
+                       if (argc <= i)
+                               die("Which directory?");
+                       if (output_directory)
+                               die("Two output directories?");
+                       output_directory = argv[i];
+               }
+               else if (!strcmp(argv[i], "--signoff") ||
+                        !strcmp(argv[i], "-s")) {
+                       const char *committer = git_committer_info(1);
+                       const char *endpos = strchr(committer, '>');
+                       if (!endpos)
+                               die("bogos committer info %s\n", committer);
+                       add_signoff = xmalloc(endpos - committer + 2);
+                       memcpy(add_signoff, committer, endpos - committer + 1);
+                       add_signoff[endpos - committer + 1] = 0;
+               }
+               else if (!strcmp(argv[i], "--attach"))
+                       rev.mime_boundary = git_version_string;
+               else if (!strncmp(argv[i], "--attach=", 9))
+                       rev.mime_boundary = argv[i] + 9;
+               else
+                       argv[j++] = argv[i];
+       }
+       argc = j;
+
+       if (start_number < 0)
+               start_number = 1;
+       if (numbered && keep_subject)
+               die ("-n and -k are mutually exclusive.");
+
+       argc = setup_revisions(argc, argv, &rev, "HEAD");
+       if (argc > 1)
+               die ("unrecognized argument: %s", argv[1]);
+
+       if (output_directory) {
+               if (use_stdout)
+                       die("standard output, or directory, which one?");
+               if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
+                       die("Could not create directory %s",
+                           output_directory);
+       }
+
+       if (rev.pending_objects && rev.pending_objects->next == NULL) {
+               rev.pending_objects->item->flags |= UNINTERESTING;
+               add_head(&rev);
+       }
+
+       if (!use_stdout)
+               realstdout = fdopen(dup(1), "w");
+
+       prepare_revision_walk(&rev);
+       while ((commit = get_revision(&rev)) != NULL) {
+               /* ignore merges */
+               if (commit->parents && commit->parents->next)
+                       continue;
+               nr++;
+               list = realloc(list, nr * sizeof(list[0]));
+               list[nr - 1] = commit;
+       }
+       total = nr;
+       if (numbered)
+               rev.total = total + start_number - 1;
+       rev.add_signoff = add_signoff;
+       while (0 <= --nr) {
+               int shown;
+               commit = list[nr];
+               rev.nr = total - nr + (start_number - 1);
+               if (!use_stdout)
+                       reopen_stdout(commit, rev.nr, keep_subject);
+               shown = log_tree_commit(&rev, commit);
+               free(commit->buffer);
+               commit->buffer = NULL;
+
+               /* We put one extra blank line between formatted
+                * patches and this flag is used by log-tree code
+                * to see if it needs to emit a LF before showing
+                * the log; when using one file per patch, we do
+                * not want the extra blank line.
+                */
+               if (!use_stdout)
+                       rev.shown_one = 0;
+               if (shown) {
+                       if (rev.mime_boundary)
+                               printf("\n--%s%s--\n\n\n",
+                                      mime_boundary_leader,
+                                      rev.mime_boundary);
+                       else
+                               printf("-- \n%s\n\n", git_version_string);
+               }
+               if (!use_stdout)
+                       fclose(stdout);
+       }
+       free(list);
+       return 0;
+}
+
diff --git a/builtin-ls-files.c b/builtin-ls-files.c
new file mode 100644 (file)
index 0000000..8dae9f7
--- /dev/null
@@ -0,0 +1,499 @@
+/*
+ * This merges the file listing in the directory cache index
+ * with the actual working directory list, and shows different
+ * combinations of the two.
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#include <fnmatch.h>
+
+#include "cache.h"
+#include "quote.h"
+#include "dir.h"
+#include "builtin.h"
+
+static int abbrev = 0;
+static int show_deleted = 0;
+static int show_cached = 0;
+static int show_others = 0;
+static int show_stage = 0;
+static int show_unmerged = 0;
+static int show_modified = 0;
+static int show_killed = 0;
+static int show_valid_bit = 0;
+static int line_terminator = '\n';
+
+static int prefix_len = 0, prefix_offset = 0;
+static const char *prefix = NULL;
+static const char **pathspec = NULL;
+static int error_unmatch = 0;
+static char *ps_matched = NULL;
+
+static const char *tag_cached = "";
+static const char *tag_unmerged = "";
+static const char *tag_removed = "";
+static const char *tag_other = "";
+static const char *tag_killed = "";
+static const char *tag_modified = "";
+
+
+/*
+ * Match a pathspec against a filename. The first "len" characters
+ * are the common prefix
+ */
+static int match(const char **spec, char *ps_matched,
+                const char *filename, int len)
+{
+       const char *m;
+
+       while ((m = *spec++) != NULL) {
+               int matchlen = strlen(m + len);
+
+               if (!matchlen)
+                       goto matched;
+               if (!strncmp(m + len, filename + len, matchlen)) {
+                       if (m[len + matchlen - 1] == '/')
+                               goto matched;
+                       switch (filename[len + matchlen]) {
+                       case '/': case '\0':
+                               goto matched;
+                       }
+               }
+               if (!fnmatch(m + len, filename + len, 0))
+                       goto matched;
+               if (ps_matched)
+                       ps_matched++;
+               continue;
+       matched:
+               if (ps_matched)
+                       *ps_matched = 1;
+               return 1;
+       }
+       return 0;
+}
+
+static void show_dir_entry(const char *tag, struct dir_entry *ent)
+{
+       int len = prefix_len;
+       int offset = prefix_offset;
+
+       if (len >= ent->len)
+               die("git-ls-files: internal error - directory entry not superset of prefix");
+
+       if (pathspec && !match(pathspec, ps_matched, ent->name, len))
+               return;
+
+       fputs(tag, stdout);
+       write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
+       putchar(line_terminator);
+}
+
+static void show_other_files(struct dir_struct *dir)
+{
+       int i;
+       for (i = 0; i < dir->nr; i++) {
+               /* We should not have a matching entry, but we
+                * may have an unmerged entry for this path.
+                */
+               struct dir_entry *ent = dir->entries[i];
+               int pos = cache_name_pos(ent->name, ent->len);
+               struct cache_entry *ce;
+               if (0 <= pos)
+                       die("bug in show-other-files");
+               pos = -pos - 1;
+               if (pos < active_nr) { 
+                       ce = active_cache[pos];
+                       if (ce_namelen(ce) == ent->len &&
+                           !memcmp(ce->name, ent->name, ent->len))
+                               continue; /* Yup, this one exists unmerged */
+               }
+               show_dir_entry(tag_other, ent);
+       }
+}
+
+static void show_killed_files(struct dir_struct *dir)
+{
+       int i;
+       for (i = 0; i < dir->nr; i++) {
+               struct dir_entry *ent = dir->entries[i];
+               char *cp, *sp;
+               int pos, len, killed = 0;
+
+               for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
+                       sp = strchr(cp, '/');
+                       if (!sp) {
+                               /* If ent->name is prefix of an entry in the
+                                * cache, it will be killed.
+                                */
+                               pos = cache_name_pos(ent->name, ent->len);
+                               if (0 <= pos)
+                                       die("bug in show-killed-files");
+                               pos = -pos - 1;
+                               while (pos < active_nr &&
+                                      ce_stage(active_cache[pos]))
+                                       pos++; /* skip unmerged */
+                               if (active_nr <= pos)
+                                       break;
+                               /* pos points at a name immediately after
+                                * ent->name in the cache.  Does it expect
+                                * ent->name to be a directory?
+                                */
+                               len = ce_namelen(active_cache[pos]);
+                               if ((ent->len < len) &&
+                                   !strncmp(active_cache[pos]->name,
+                                            ent->name, ent->len) &&
+                                   active_cache[pos]->name[ent->len] == '/')
+                                       killed = 1;
+                               break;
+                       }
+                       if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
+                               /* If any of the leading directories in
+                                * ent->name is registered in the cache,
+                                * ent->name will be killed.
+                                */
+                               killed = 1;
+                               break;
+                       }
+               }
+               if (killed)
+                       show_dir_entry(tag_killed, dir->entries[i]);
+       }
+}
+
+static void show_ce_entry(const char *tag, struct cache_entry *ce)
+{
+       int len = prefix_len;
+       int offset = prefix_offset;
+
+       if (len >= ce_namelen(ce))
+               die("git-ls-files: internal error - cache entry not superset of prefix");
+
+       if (pathspec && !match(pathspec, ps_matched, ce->name, len))
+               return;
+
+       if (tag && *tag && show_valid_bit &&
+           (ce->ce_flags & htons(CE_VALID))) {
+               static char alttag[4];
+               memcpy(alttag, tag, 3);
+               if (isalpha(tag[0]))
+                       alttag[0] = tolower(tag[0]);
+               else if (tag[0] == '?')
+                       alttag[0] = '!';
+               else {
+                       alttag[0] = 'v';
+                       alttag[1] = tag[0];
+                       alttag[2] = ' ';
+                       alttag[3] = 0;
+               }
+               tag = alttag;
+       }
+
+       if (!show_stage) {
+               fputs(tag, stdout);
+               write_name_quoted("", 0, ce->name + offset,
+                                 line_terminator, stdout);
+               putchar(line_terminator);
+       }
+       else {
+               printf("%s%06o %s %d\t",
+                      tag,
+                      ntohl(ce->ce_mode),
+                      abbrev ? find_unique_abbrev(ce->sha1,abbrev)
+                               : sha1_to_hex(ce->sha1),
+                      ce_stage(ce));
+               write_name_quoted("", 0, ce->name + offset,
+                                 line_terminator, stdout);
+               putchar(line_terminator);
+       }
+}
+
+static void show_files(struct dir_struct *dir)
+{
+       int i;
+
+       /* For cached/deleted files we don't need to even do the readdir */
+       if (show_others || show_killed) {
+               const char *path = ".", *base = "";
+               int baselen = prefix_len;
+
+               if (baselen)
+                       path = base = prefix;
+               read_directory(dir, path, base, baselen);
+               if (show_others)
+                       show_other_files(dir);
+               if (show_killed)
+                       show_killed_files(dir);
+       }
+       if (show_cached | show_stage) {
+               for (i = 0; i < active_nr; i++) {
+                       struct cache_entry *ce = active_cache[i];
+                       if (excluded(dir, ce->name) != dir->show_ignored)
+                               continue;
+                       if (show_unmerged && !ce_stage(ce))
+                               continue;
+                       show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
+               }
+       }
+       if (show_deleted | show_modified) {
+               for (i = 0; i < active_nr; i++) {
+                       struct cache_entry *ce = active_cache[i];
+                       struct stat st;
+                       int err;
+                       if (excluded(dir, ce->name) != dir->show_ignored)
+                               continue;
+                       err = lstat(ce->name, &st);
+                       if (show_deleted && err)
+                               show_ce_entry(tag_removed, ce);
+                       if (show_modified && ce_modified(ce, &st, 0))
+                               show_ce_entry(tag_modified, ce);
+               }
+       }
+}
+
+/*
+ * Prune the index to only contain stuff starting with "prefix"
+ */
+static void prune_cache(void)
+{
+       int pos = cache_name_pos(prefix, prefix_len);
+       unsigned int first, last;
+
+       if (pos < 0)
+               pos = -pos-1;
+       active_cache += pos;
+       active_nr -= pos;
+       first = 0;
+       last = active_nr;
+       while (last > first) {
+               int next = (last + first) >> 1;
+               struct cache_entry *ce = active_cache[next];
+               if (!strncmp(ce->name, prefix, prefix_len)) {
+                       first = next+1;
+                       continue;
+               }
+               last = next;
+       }
+       active_nr = last;
+}
+
+static void verify_pathspec(void)
+{
+       const char **p, *n, *prev;
+       char *real_prefix;
+       unsigned long max;
+
+       prev = NULL;
+       max = PATH_MAX;
+       for (p = pathspec; (n = *p) != NULL; p++) {
+               int i, len = 0;
+               for (i = 0; i < max; i++) {
+                       char c = n[i];
+                       if (prev && prev[i] != c)
+                               break;
+                       if (!c || c == '*' || c == '?')
+                               break;
+                       if (c == '/')
+                               len = i+1;
+               }
+               prev = n;
+               if (len < max) {
+                       max = len;
+                       if (!max)
+                               break;
+               }
+       }
+
+       if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
+               die("git-ls-files: cannot generate relative filenames containing '..'");
+
+       real_prefix = NULL;
+       prefix_len = max;
+       if (max) {
+               real_prefix = xmalloc(max + 1);
+               memcpy(real_prefix, prev, max);
+               real_prefix[max] = 0;
+       }
+       prefix = real_prefix;
+}
+
+static const char ls_files_usage[] =
+       "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
+       "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
+       "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
+       "[--] [<file>]*";
+
+int cmd_ls_files(int argc, const char **argv, char** envp)
+{
+       int i;
+       int exc_given = 0;
+       struct dir_struct dir;
+
+       memset(&dir, 0, sizeof(dir));
+       prefix = setup_git_directory();
+       if (prefix)
+               prefix_offset = strlen(prefix);
+       git_config(git_default_config);
+
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+
+               if (!strcmp(arg, "--")) {
+                       i++;
+                       break;
+               }
+               if (!strcmp(arg, "-z")) {
+                       line_terminator = 0;
+                       continue;
+               }
+               if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
+                       tag_cached = "H ";
+                       tag_unmerged = "M ";
+                       tag_removed = "R ";
+                       tag_modified = "C ";
+                       tag_other = "? ";
+                       tag_killed = "K ";
+                       if (arg[1] == 'v')
+                               show_valid_bit = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
+                       show_cached = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
+                       show_deleted = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
+                       show_modified = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
+                       show_others = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
+                       dir.show_ignored = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
+                       show_stage = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
+                       show_killed = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--directory")) {
+                       dir.show_other_directories = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--no-empty-directory")) {
+                       dir.hide_empty_directories = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
+                       /* There's no point in showing unmerged unless
+                        * you also show the stage information.
+                        */
+                       show_stage = 1;
+                       show_unmerged = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-x") && i+1 < argc) {
+                       exc_given = 1;
+                       add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
+                       continue;
+               }
+               if (!strncmp(arg, "--exclude=", 10)) {
+                       exc_given = 1;
+                       add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
+                       continue;
+               }
+               if (!strcmp(arg, "-X") && i+1 < argc) {
+                       exc_given = 1;
+                       add_excludes_from_file(&dir, argv[++i]);
+                       continue;
+               }
+               if (!strncmp(arg, "--exclude-from=", 15)) {
+                       exc_given = 1;
+                       add_excludes_from_file(&dir, arg+15);
+                       continue;
+               }
+               if (!strncmp(arg, "--exclude-per-directory=", 24)) {
+                       exc_given = 1;
+                       dir.exclude_per_dir = arg + 24;
+                       continue;
+               }
+               if (!strcmp(arg, "--full-name")) {
+                       prefix_offset = 0;
+                       continue;
+               }
+               if (!strcmp(arg, "--error-unmatch")) {
+                       error_unmatch = 1;
+                       continue;
+               }
+               if (!strncmp(arg, "--abbrev=", 9)) {
+                       abbrev = strtoul(arg+9, NULL, 10);
+                       if (abbrev && abbrev < MINIMUM_ABBREV)
+                               abbrev = MINIMUM_ABBREV;
+                       else if (abbrev > 40)
+                               abbrev = 40;
+                       continue;
+               }
+               if (!strcmp(arg, "--abbrev")) {
+                       abbrev = DEFAULT_ABBREV;
+                       continue;
+               }
+               if (*arg == '-')
+                       usage(ls_files_usage);
+               break;
+       }
+
+       pathspec = get_pathspec(prefix, argv + i);
+
+       /* Verify that the pathspec matches the prefix */
+       if (pathspec)
+               verify_pathspec();
+
+       /* Treat unmatching pathspec elements as errors */
+       if (pathspec && error_unmatch) {
+               int num;
+               for (num = 0; pathspec[num]; num++)
+                       ;
+               ps_matched = xcalloc(1, num);
+       }
+
+       if (dir.show_ignored && !exc_given) {
+               fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
+                       argv[0]);
+               exit(1);
+       }
+
+       /* With no flags, we default to showing the cached files */
+       if (!(show_stage | show_deleted | show_others | show_unmerged |
+             show_killed | show_modified))
+               show_cached = 1;
+
+       read_cache();
+       if (prefix)
+               prune_cache();
+       show_files(&dir);
+
+       if (ps_matched) {
+               /* We need to make sure all pathspec matched otherwise
+                * it is an error.
+                */
+               int num, errors = 0;
+               for (num = 0; pathspec[num]; num++) {
+                       if (ps_matched[num])
+                               continue;
+                       error("pathspec '%s' did not match any.",
+                             pathspec[num] + prefix_offset);
+                       errors++;
+               }
+               return errors ? 1 : 0;
+       }
+
+       return 0;
+}
diff --git a/builtin-ls-tree.c b/builtin-ls-tree.c
new file mode 100644 (file)
index 0000000..b8d0d88
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#include "cache.h"
+#include "blob.h"
+#include "tree.h"
+#include "quote.h"
+#include "builtin.h"
+
+static int line_termination = '\n';
+#define LS_RECURSIVE 1
+#define LS_TREE_ONLY 2
+#define LS_SHOW_TREES 4
+#define LS_NAME_ONLY 8
+static int abbrev = 0;
+static int ls_options = 0;
+static const char **pathspec;
+static int chomp_prefix = 0;
+static const char *prefix;
+
+static const char ls_tree_usage[] =
+       "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
+
+static int show_recursive(const char *base, int baselen, const char *pathname)
+{
+       const char **s;
+
+       if (ls_options & LS_RECURSIVE)
+               return 1;
+
+       s = pathspec;
+       if (!s)
+               return 0;
+
+       for (;;) {
+               const char *spec = *s++;
+               int len, speclen;
+
+               if (!spec)
+                       return 0;
+               if (strncmp(base, spec, baselen))
+                       continue;
+               len = strlen(pathname);
+               spec += baselen;
+               speclen = strlen(spec);
+               if (speclen <= len)
+                       continue;
+               if (memcmp(pathname, spec, len))
+                       continue;
+               return 1;
+       }
+}
+
+static int show_tree(const unsigned char *sha1, const char *base, int baselen,
+                    const char *pathname, unsigned mode, int stage)
+{
+       int retval = 0;
+       const char *type = blob_type;
+
+       if (S_ISDIR(mode)) {
+               if (show_recursive(base, baselen, pathname)) {
+                       retval = READ_TREE_RECURSIVE;
+                       if (!(ls_options & LS_SHOW_TREES))
+                               return retval;
+               }
+               type = tree_type;
+       }
+       else if (ls_options & LS_TREE_ONLY)
+               return 0;
+
+       if (chomp_prefix &&
+           (baselen < chomp_prefix || memcmp(prefix, base, chomp_prefix)))
+               return 0;
+
+       if (!(ls_options & LS_NAME_ONLY))
+               printf("%06o %s %s\t", mode, type,
+                               abbrev ? find_unique_abbrev(sha1,abbrev)
+                                       : sha1_to_hex(sha1));
+       write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
+                         pathname,
+                         line_termination, stdout);
+       putchar(line_termination);
+       return retval;
+}
+
+int cmd_ls_tree(int argc, const char **argv, char **envp)
+{
+       unsigned char sha1[20];
+       struct tree *tree;
+
+       prefix = setup_git_directory();
+       git_config(git_default_config);
+       if (prefix && *prefix)
+               chomp_prefix = strlen(prefix);
+       while (1 < argc && argv[1][0] == '-') {
+               switch (argv[1][1]) {
+               case 'z':
+                       line_termination = 0;
+                       break;
+               case 'r':
+                       ls_options |= LS_RECURSIVE;
+                       break;
+               case 'd':
+                       ls_options |= LS_TREE_ONLY;
+                       break;
+               case 't':
+                       ls_options |= LS_SHOW_TREES;
+                       break;
+               case '-':
+                       if (!strcmp(argv[1]+2, "name-only") ||
+                           !strcmp(argv[1]+2, "name-status")) {
+                               ls_options |= LS_NAME_ONLY;
+                               break;
+                       }
+                       if (!strcmp(argv[1]+2, "full-name")) {
+                               chomp_prefix = 0;
+                               break;
+                       }
+                       if (!strncmp(argv[1]+2, "abbrev=",7)) {
+                               abbrev = strtoul(argv[1]+9, NULL, 10);
+                               if (abbrev && abbrev < MINIMUM_ABBREV)
+                                       abbrev = MINIMUM_ABBREV;
+                               else if (abbrev > 40)
+                                       abbrev = 40;
+                               break;
+                       }
+                       if (!strcmp(argv[1]+2, "abbrev")) {
+                               abbrev = DEFAULT_ABBREV;
+                               break;
+                       }
+                       /* otherwise fallthru */
+               default:
+                       usage(ls_tree_usage);
+               }
+               argc--; argv++;
+       }
+       /* -d -r should imply -t, but -d by itself should not have to. */
+       if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
+           ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
+               ls_options |= LS_SHOW_TREES;
+
+       if (argc < 2)
+               usage(ls_tree_usage);
+       if (get_sha1(argv[1], sha1))
+               die("Not a valid object name %s", argv[1]);
+
+       pathspec = get_pathspec(prefix, argv + 2);
+       tree = parse_tree_indirect(sha1);
+       if (!tree)
+               die("not a tree object");
+       read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
+
+       return 0;
+}
diff --git a/builtin-push.c b/builtin-push.c
new file mode 100644 (file)
index 0000000..66b9407
--- /dev/null
@@ -0,0 +1,314 @@
+/*
+ * "git push"
+ */
+#include "cache.h"
+#include "refs.h"
+#include "run-command.h"
+#include "builtin.h"
+
+#define MAX_URI (16)
+
+static const char push_usage[] = "git push [--all] [--tags] [--force] <repository> [<refspec>...]";
+
+static int all = 0, tags = 0, force = 0, thin = 1;
+static const char *execute = NULL;
+
+#define BUF_SIZE (2084)
+static char buffer[BUF_SIZE];
+
+static const char **refspec = NULL;
+static int refspec_nr = 0;
+
+static void add_refspec(const char *ref)
+{
+       int nr = refspec_nr + 1;
+       refspec = xrealloc(refspec, nr * sizeof(char *));
+       refspec[nr-1] = ref;
+       refspec_nr = nr;
+}
+
+static int expand_one_ref(const char *ref, const unsigned char *sha1)
+{
+       /* Ignore the "refs/" at the beginning of the refname */
+       ref += 5;
+
+       if (strncmp(ref, "tags/", 5))
+               return 0;
+
+       add_refspec(strdup(ref));
+       return 0;
+}
+
+static void expand_refspecs(void)
+{
+       if (all) {
+               if (refspec_nr)
+                       die("cannot mix '--all' and a refspec");
+
+               /*
+                * No need to expand "--all" - we'll just use
+                * the "--all" flag to send-pack
+                */
+               return;
+       }
+       if (!tags)
+               return;
+       for_each_ref(expand_one_ref);
+}
+
+static void set_refspecs(const char **refs, int nr)
+{
+       if (nr) {
+               size_t bytes = nr * sizeof(char *);
+
+               refspec = xrealloc(refspec, bytes);
+               memcpy(refspec, refs, bytes);
+               refspec_nr = nr;
+       }
+       expand_refspecs();
+}
+
+static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
+{
+       int n = 0;
+       FILE *f = fopen(git_path("remotes/%s", repo), "r");
+       int has_explicit_refspec = refspec_nr || all || tags;
+
+       if (!f)
+               return -1;
+       while (fgets(buffer, BUF_SIZE, f)) {
+               int is_refspec;
+               char *s, *p;
+
+               if (!strncmp("URL: ", buffer, 5)) {
+                       is_refspec = 0;
+                       s = buffer + 5;
+               } else if (!strncmp("Push: ", buffer, 6)) {
+                       is_refspec = 1;
+                       s = buffer + 6;
+               } else
+                       continue;
+
+               /* Remove whitespace at the head.. */
+               while (isspace(*s))
+                       s++;
+               if (!*s)
+                       continue;
+
+               /* ..and at the end */
+               p = s + strlen(s);
+               while (isspace(p[-1]))
+                       *--p = 0;
+
+               if (!is_refspec) {
+                       if (n < MAX_URI)
+                               uri[n++] = strdup(s);
+                       else
+                               error("more than %d URL's specified, ignoreing the rest", MAX_URI);
+               }
+               else if (is_refspec && !has_explicit_refspec)
+                       add_refspec(strdup(s));
+       }
+       fclose(f);
+       if (!n)
+               die("remote '%s' has no URL", repo);
+       return n;
+}
+
+static const char **config_uri;
+static const char *config_repo;
+static int config_repo_len;
+static int config_current_uri;
+static int config_get_refspecs;
+
+static int get_remote_config(const char* key, const char* value)
+{
+       if (!strncmp(key, "remote.", 7) &&
+           !strncmp(key + 7, config_repo, config_repo_len)) {
+               if (!strcmp(key + 7 + config_repo_len, ".url")) {
+                       if (config_current_uri < MAX_URI)
+                               config_uri[config_current_uri++] = strdup(value);
+                       else
+                               error("more than %d URL's specified, ignoring the rest", MAX_URI);
+               }
+               else if (config_get_refspecs &&
+                        !strcmp(key + 7 + config_repo_len, ".push"))
+                       add_refspec(strdup(value));
+       }
+       return 0;
+}
+
+static int get_config_remotes_uri(const char *repo, const char *uri[MAX_URI])
+{
+       config_repo_len = strlen(repo);
+       config_repo = repo;
+       config_current_uri = 0;
+       config_uri = uri;
+       config_get_refspecs = !(refspec_nr || all || tags);
+
+       git_config(get_remote_config);
+       return config_current_uri;
+}
+
+static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
+{
+       const char *slash = strchr(repo, '/');
+       int n = slash ? slash - repo : 1000;
+       FILE *f = fopen(git_path("branches/%.*s", n, repo), "r");
+       char *s, *p;
+       int len;
+
+       if (!f)
+               return 0;
+       s = fgets(buffer, BUF_SIZE, f);
+       fclose(f);
+       if (!s)
+               return 0;
+       while (isspace(*s))
+               s++;
+       if (!*s)
+               return 0;
+       p = s + strlen(s);
+       while (isspace(p[-1]))
+               *--p = 0;
+       len = p - s;
+       if (slash)
+               len += strlen(slash);
+       p = xmalloc(len + 1);
+       strcpy(p, s);
+       if (slash)
+               strcat(p, slash);
+       uri[0] = p;
+       return 1;
+}
+
+/*
+ * Read remotes and branches file, fill the push target URI
+ * list.  If there is no command line refspecs, read Push: lines
+ * to set up the *refspec list as well.
+ * return the number of push target URIs
+ */
+static int read_config(const char *repo, const char *uri[MAX_URI])
+{
+       int n;
+
+       if (*repo != '/') {
+               n = get_remotes_uri(repo, uri);
+               if (n > 0)
+                       return n;
+
+               n = get_config_remotes_uri(repo, uri);
+               if (n > 0)
+                       return n;
+
+               n = get_branches_uri(repo, uri);
+               if (n > 0)
+                       return n;
+       }
+
+       uri[0] = repo;
+       return 1;
+}
+
+static int do_push(const char *repo)
+{
+       const char *uri[MAX_URI];
+       int i, n;
+       int common_argc;
+       const char **argv;
+       int argc;
+
+       n = read_config(repo, uri);
+       if (n <= 0)
+               die("bad repository '%s'", repo);
+
+       argv = xmalloc((refspec_nr + 10) * sizeof(char *));
+       argv[0] = "dummy-send-pack";
+       argc = 1;
+       if (all)
+               argv[argc++] = "--all";
+       if (force)
+               argv[argc++] = "--force";
+       if (execute)
+               argv[argc++] = execute;
+       common_argc = argc;
+
+       for (i = 0; i < n; i++) {
+               int error;
+               int dest_argc = common_argc;
+               int dest_refspec_nr = refspec_nr;
+               const char **dest_refspec = refspec;
+               const char *dest = uri[i];
+               const char *sender = "git-send-pack";
+               if (!strncmp(dest, "http://", 7) ||
+                   !strncmp(dest, "https://", 8))
+                       sender = "git-http-push";
+               else if (thin)
+                       argv[dest_argc++] = "--thin";
+               argv[0] = sender;
+               argv[dest_argc++] = dest;
+               while (dest_refspec_nr--)
+                       argv[dest_argc++] = *dest_refspec++;
+               argv[dest_argc] = NULL;
+               error = run_command_v(argc, argv);
+               if (!error)
+                       continue;
+               switch (error) {
+               case -ERR_RUN_COMMAND_FORK:
+                       die("unable to fork for %s", sender);
+               case -ERR_RUN_COMMAND_EXEC:
+                       die("unable to exec %s", sender);
+               case -ERR_RUN_COMMAND_WAITPID:
+               case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
+               case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
+               case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
+                       die("%s died with strange error", sender);
+               default:
+                       return -error;
+               }
+       }
+       return 0;
+}
+
+int cmd_push(int argc, const char **argv, char **envp)
+{
+       int i;
+       const char *repo = "origin";    // default repository
+
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+
+               if (arg[0] != '-') {
+                       repo = arg;
+                       i++;
+                       break;
+               }
+               if (!strcmp(arg, "--all")) {
+                       all = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--tags")) {
+                       tags = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--force")) {
+                       force = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--thin")) {
+                       thin = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--no-thin")) {
+                       thin = 0;
+                       continue;
+               }
+               if (!strncmp(arg, "--exec=", 7)) {
+                       execute = arg;
+                       continue;
+               }
+               usage(push_usage);
+       }
+       set_refspecs(argv + i, argc - i);
+       return do_push(repo);
+}
diff --git a/builtin-read-tree.c b/builtin-read-tree.c
new file mode 100644 (file)
index 0000000..04506da
--- /dev/null
@@ -0,0 +1,1045 @@
+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#define DBRT_DEBUG 1
+
+#include "cache.h"
+
+#include "object.h"
+#include "tree.h"
+#include "tree-walk.h"
+#include "cache-tree.h"
+#include <sys/time.h>
+#include <signal.h>
+#include "builtin.h"
+
+static int reset = 0;
+static int merge = 0;
+static int update = 0;
+static int index_only = 0;
+static int nontrivial_merge = 0;
+static int trivial_merges_only = 0;
+static int aggressive = 0;
+static int verbose_update = 0;
+static volatile int progress_update = 0;
+static const char *prefix = NULL;
+
+static int head_idx = -1;
+static int merge_size = 0;
+
+static struct object_list *trees = NULL;
+
+static struct cache_entry df_conflict_entry = {
+};
+
+struct tree_entry_list {
+       struct tree_entry_list *next;
+       unsigned directory : 1;
+       unsigned executable : 1;
+       unsigned symlink : 1;
+       unsigned int mode;
+       const char *name;
+       const unsigned char *sha1;
+};
+
+static struct tree_entry_list df_conflict_list = {
+       .name = NULL,
+       .next = &df_conflict_list
+};
+
+typedef int (*merge_fn_t)(struct cache_entry **src);
+
+static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
+{
+       struct tree_desc desc;
+       struct name_entry one;
+       struct tree_entry_list *ret = NULL;
+       struct tree_entry_list **list_p = &ret;
+
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+
+       while (tree_entry(&desc, &one)) {
+               struct tree_entry_list *entry;
+
+               entry = xmalloc(sizeof(struct tree_entry_list));
+               entry->name = one.path;
+               entry->sha1 = one.sha1;
+               entry->mode = one.mode;
+               entry->directory = S_ISDIR(one.mode) != 0;
+               entry->executable = (one.mode & S_IXUSR) != 0;
+               entry->symlink = S_ISLNK(one.mode) != 0;
+               entry->next = NULL;
+
+               *list_p = entry;
+               list_p = &entry->next;
+       }
+       return ret;
+}
+
+static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
+{
+       int len1 = strlen(name1);
+       int len2 = strlen(name2);
+       int len = len1 < len2 ? len1 : len2;
+       int ret = memcmp(name1, name2, len);
+       unsigned char c1, c2;
+       if (ret)
+               return ret;
+       c1 = name1[len];
+       c2 = name2[len];
+       if (!c1 && dir1)
+               c1 = '/';
+       if (!c2 && dir2)
+               c2 = '/';
+       ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
+       if (c1 && c2 && !ret)
+               ret = len1 - len2;
+       return ret;
+}
+
+static int unpack_trees_rec(struct tree_entry_list **posns, int len,
+                           const char *base, merge_fn_t fn, int *indpos)
+{
+       int baselen = strlen(base);
+       int src_size = len + 1;
+       do {
+               int i;
+               const char *first;
+               int firstdir = 0;
+               int pathlen;
+               unsigned ce_size;
+               struct tree_entry_list **subposns;
+               struct cache_entry **src;
+               int any_files = 0;
+               int any_dirs = 0;
+               char *cache_name;
+               int ce_stage;
+
+               /* Find the first name in the input. */
+
+               first = NULL;
+               cache_name = NULL;
+
+               /* Check the cache */
+               if (merge && *indpos < active_nr) {
+                       /* This is a bit tricky: */
+                       /* If the index has a subdirectory (with
+                        * contents) as the first name, it'll get a
+                        * filename like "foo/bar". But that's after
+                        * "foo", so the entry in trees will get
+                        * handled first, at which point we'll go into
+                        * "foo", and deal with "bar" from the index,
+                        * because the base will be "foo/". The only
+                        * way we can actually have "foo/bar" first of
+                        * all the things is if the trees don't
+                        * contain "foo" at all, in which case we'll
+                        * handle "foo/bar" without going into the
+                        * directory, but that's fine (and will return
+                        * an error anyway, with the added unknown
+                        * file case.
+                        */
+
+                       cache_name = active_cache[*indpos]->name;
+                       if (strlen(cache_name) > baselen &&
+                           !memcmp(cache_name, base, baselen)) {
+                               cache_name += baselen;
+                               first = cache_name;
+                       } else {
+                               cache_name = NULL;
+                       }
+               }
+
+#if DBRT_DEBUG > 1
+               if (first)
+                       printf("index %s\n", first);
+#endif
+               for (i = 0; i < len; i++) {
+                       if (!posns[i] || posns[i] == &df_conflict_list)
+                               continue;
+#if DBRT_DEBUG > 1
+                       printf("%d %s\n", i + 1, posns[i]->name);
+#endif
+                       if (!first || entcmp(first, firstdir,
+                                            posns[i]->name, 
+                                            posns[i]->directory) > 0) {
+                               first = posns[i]->name;
+                               firstdir = posns[i]->directory;
+                       }
+               }
+               /* No name means we're done */
+               if (!first)
+                       return 0;
+
+               pathlen = strlen(first);
+               ce_size = cache_entry_size(baselen + pathlen);
+
+               src = xcalloc(src_size, sizeof(struct cache_entry *));
+
+               subposns = xcalloc(len, sizeof(struct tree_list_entry *));
+
+               if (cache_name && !strcmp(cache_name, first)) {
+                       any_files = 1;
+                       src[0] = active_cache[*indpos];
+                       remove_cache_entry_at(*indpos);
+               }
+
+               for (i = 0; i < len; i++) {
+                       struct cache_entry *ce;
+
+                       if (!posns[i] ||
+                           (posns[i] != &df_conflict_list &&
+                            strcmp(first, posns[i]->name))) {
+                               continue;
+                       }
+
+                       if (posns[i] == &df_conflict_list) {
+                               src[i + merge] = &df_conflict_entry;
+                               continue;
+                       }
+
+                       if (posns[i]->directory) {
+                               struct tree *tree = lookup_tree(posns[i]->sha1);
+                               any_dirs = 1;
+                               parse_tree(tree);
+                               subposns[i] = create_tree_entry_list(tree);
+                               posns[i] = posns[i]->next;
+                               src[i + merge] = &df_conflict_entry;
+                               continue;
+                       }
+
+                       if (!merge)
+                               ce_stage = 0;
+                       else if (i + 1 < head_idx)
+                               ce_stage = 1;
+                       else if (i + 1 > head_idx)
+                               ce_stage = 3;
+                       else
+                               ce_stage = 2;
+
+                       ce = xcalloc(1, ce_size);
+                       ce->ce_mode = create_ce_mode(posns[i]->mode);
+                       ce->ce_flags = create_ce_flags(baselen + pathlen,
+                                                      ce_stage);
+                       memcpy(ce->name, base, baselen);
+                       memcpy(ce->name + baselen, first, pathlen + 1);
+
+                       any_files = 1;
+
+                       memcpy(ce->sha1, posns[i]->sha1, 20);
+                       src[i + merge] = ce;
+                       subposns[i] = &df_conflict_list;
+                       posns[i] = posns[i]->next;
+               }
+               if (any_files) {
+                       if (merge) {
+                               int ret;
+
+#if DBRT_DEBUG > 1
+                               printf("%s:\n", first);
+                               for (i = 0; i < src_size; i++) {
+                                       printf(" %d ", i);
+                                       if (src[i])
+                                               printf("%s\n", sha1_to_hex(src[i]->sha1));
+                                       else
+                                               printf("\n");
+                               }
+#endif
+                               ret = fn(src);
+                               
+#if DBRT_DEBUG > 1
+                               printf("Added %d entries\n", ret);
+#endif
+                               *indpos += ret;
+                       } else {
+                               for (i = 0; i < src_size; i++) {
+                                       if (src[i]) {
+                                               add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
+                                       }
+                               }
+                       }
+               }
+               if (any_dirs) {
+                       char *newbase = xmalloc(baselen + 2 + pathlen);
+                       memcpy(newbase, base, baselen);
+                       memcpy(newbase + baselen, first, pathlen);
+                       newbase[baselen + pathlen] = '/';
+                       newbase[baselen + pathlen + 1] = '\0';
+                       if (unpack_trees_rec(subposns, len, newbase, fn,
+                                            indpos))
+                               return -1;
+                       free(newbase);
+               }
+               free(subposns);
+               free(src);
+       } while (1);
+}
+
+static void reject_merge(struct cache_entry *ce)
+{
+       die("Entry '%s' would be overwritten by merge. Cannot merge.", 
+           ce->name);
+}
+
+/* Unlink the last component and attempt to remove leading
+ * directories, in case this unlink is the removal of the
+ * last entry in the directory -- empty directories are removed.
+ */
+static void unlink_entry(char *name)
+{
+       char *cp, *prev;
+
+       if (unlink(name))
+               return;
+       prev = NULL;
+       while (1) {
+               int status;
+               cp = strrchr(name, '/');
+               if (prev)
+                       *prev = '/';
+               if (!cp)
+                       break;
+
+               *cp = 0;
+               status = rmdir(name);
+               if (status) {
+                       *cp = '/';
+                       break;
+               }
+               prev = cp;
+       }
+}
+
+static void progress_interval(int signum)
+{
+       progress_update = 1;
+}
+
+static void setup_progress_signal(void)
+{
+       struct sigaction sa;
+       struct itimerval v;
+
+       memset(&sa, 0, sizeof(sa));
+       sa.sa_handler = progress_interval;
+       sigemptyset(&sa.sa_mask);
+       sa.sa_flags = SA_RESTART;
+       sigaction(SIGALRM, &sa, NULL);
+
+       v.it_interval.tv_sec = 1;
+       v.it_interval.tv_usec = 0;
+       v.it_value = v.it_interval;
+       setitimer(ITIMER_REAL, &v, NULL);
+}
+
+static void check_updates(struct cache_entry **src, int nr)
+{
+       static struct checkout state = {
+               .base_dir = "",
+               .force = 1,
+               .quiet = 1,
+               .refresh_cache = 1,
+       };
+       unsigned short mask = htons(CE_UPDATE);
+       unsigned last_percent = 200, cnt = 0, total = 0;
+
+       if (update && verbose_update) {
+               for (total = cnt = 0; cnt < nr; cnt++) {
+                       struct cache_entry *ce = src[cnt];
+                       if (!ce->ce_mode || ce->ce_flags & mask)
+                               total++;
+               }
+
+               /* Don't bother doing this for very small updates */
+               if (total < 250)
+                       total = 0;
+
+               if (total) {
+                       fprintf(stderr, "Checking files out...\n");
+                       setup_progress_signal();
+                       progress_update = 1;
+               }
+               cnt = 0;
+       }
+
+       while (nr--) {
+               struct cache_entry *ce = *src++;
+
+               if (total) {
+                       if (!ce->ce_mode || ce->ce_flags & mask) {
+                               unsigned percent;
+                               cnt++;
+                               percent = (cnt * 100) / total;
+                               if (percent != last_percent ||
+                                   progress_update) {
+                                       fprintf(stderr, "%4u%% (%u/%u) done\r",
+                                               percent, cnt, total);
+                                       last_percent = percent;
+                                       progress_update = 0;
+                               }
+                       }
+               }
+               if (!ce->ce_mode) {
+                       if (update)
+                               unlink_entry(ce->name);
+                       continue;
+               }
+               if (ce->ce_flags & mask) {
+                       ce->ce_flags &= ~mask;
+                       if (update)
+                               checkout_entry(ce, &state, NULL);
+               }
+       }
+       if (total) {
+               signal(SIGALRM, SIG_IGN);
+               fputc('\n', stderr);
+       }
+}
+
+static int unpack_trees(merge_fn_t fn)
+{
+       int indpos = 0;
+       unsigned len = object_list_length(trees);
+       struct tree_entry_list **posns;
+       int i;
+       struct object_list *posn = trees;
+       merge_size = len;
+
+       if (len) {
+               posns = xmalloc(len * sizeof(struct tree_entry_list *));
+               for (i = 0; i < len; i++) {
+                       posns[i] = create_tree_entry_list((struct tree *) posn->item);
+                       posn = posn->next;
+               }
+               if (unpack_trees_rec(posns, len, prefix ? prefix : "",
+                                    fn, &indpos))
+                       return -1;
+       }
+
+       if (trivial_merges_only && nontrivial_merge)
+               die("Merge requires file-level merging");
+
+       check_updates(active_cache, active_nr);
+       return 0;
+}
+
+static int list_tree(unsigned char *sha1)
+{
+       struct tree *tree = parse_tree_indirect(sha1);
+       if (!tree)
+               return -1;
+       object_list_append(&tree->object, &trees);
+       return 0;
+}
+
+static int same(struct cache_entry *a, struct cache_entry *b)
+{
+       if (!!a != !!b)
+               return 0;
+       if (!a && !b)
+               return 1;
+       return a->ce_mode == b->ce_mode && 
+               !memcmp(a->sha1, b->sha1, 20);
+}
+
+
+/*
+ * When a CE gets turned into an unmerged entry, we
+ * want it to be up-to-date
+ */
+static void verify_uptodate(struct cache_entry *ce)
+{
+       struct stat st;
+
+       if (index_only || reset)
+               return;
+
+       if (!lstat(ce->name, &st)) {
+               unsigned changed = ce_match_stat(ce, &st, 1);
+               if (!changed)
+                       return;
+               errno = 0;
+       }
+       if (reset) {
+               ce->ce_flags |= htons(CE_UPDATE);
+               return;
+       }
+       if (errno == ENOENT)
+               return;
+       die("Entry '%s' not uptodate. Cannot merge.", ce->name);
+}
+
+static void invalidate_ce_path(struct cache_entry *ce)
+{
+       if (ce)
+               cache_tree_invalidate_path(active_cache_tree, ce->name);
+}
+
+/*
+ * We do not want to remove or overwrite a working tree file that
+ * is not tracked.
+ */
+static void verify_absent(const char *path, const char *action)
+{
+       struct stat st;
+
+       if (index_only || reset || !update)
+               return;
+       if (!lstat(path, &st))
+               die("Untracked working tree file '%s' "
+                   "would be %s by merge.", path, action);
+}
+
+static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
+{
+       merge->ce_flags |= htons(CE_UPDATE);
+       if (old) {
+               /*
+                * See if we can re-use the old CE directly?
+                * That way we get the uptodate stat info.
+                *
+                * This also removes the UPDATE flag on
+                * a match.
+                */
+               if (same(old, merge)) {
+                       *merge = *old;
+               } else {
+                       verify_uptodate(old);
+                       invalidate_ce_path(old);
+               }
+       }
+       else {
+               verify_absent(merge->name, "overwritten");
+               invalidate_ce_path(merge);
+       }
+
+       merge->ce_flags &= ~htons(CE_STAGEMASK);
+       add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
+       return 1;
+}
+
+static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
+{
+       if (old)
+               verify_uptodate(old);
+       else
+               verify_absent(ce->name, "removed");
+       ce->ce_mode = 0;
+       add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
+       invalidate_ce_path(ce);
+       return 1;
+}
+
+static int keep_entry(struct cache_entry *ce)
+{
+       add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
+       return 1;
+}
+
+#if DBRT_DEBUG
+static void show_stage_entry(FILE *o,
+                            const char *label, const struct cache_entry *ce)
+{
+       if (!ce)
+               fprintf(o, "%s (missing)\n", label);
+       else
+               fprintf(o, "%s%06o %s %d\t%s\n",
+                       label,
+                       ntohl(ce->ce_mode),
+                       sha1_to_hex(ce->sha1),
+                       ce_stage(ce),
+                       ce->name);
+}
+#endif
+
+static int threeway_merge(struct cache_entry **stages)
+{
+       struct cache_entry *index;
+       struct cache_entry *head; 
+       struct cache_entry *remote = stages[head_idx + 1];
+       int count;
+       int head_match = 0;
+       int remote_match = 0;
+       const char *path = NULL;
+
+       int df_conflict_head = 0;
+       int df_conflict_remote = 0;
+
+       int any_anc_missing = 0;
+       int no_anc_exists = 1;
+       int i;
+
+       for (i = 1; i < head_idx; i++) {
+               if (!stages[i])
+                       any_anc_missing = 1;
+               else {
+                       if (!path)
+                               path = stages[i]->name;
+                       no_anc_exists = 0;
+               }
+       }
+
+       index = stages[0];
+       head = stages[head_idx];
+
+       if (head == &df_conflict_entry) {
+               df_conflict_head = 1;
+               head = NULL;
+       }
+
+       if (remote == &df_conflict_entry) {
+               df_conflict_remote = 1;
+               remote = NULL;
+       }
+
+       if (!path && index)
+               path = index->name;
+       if (!path && head)
+               path = head->name;
+       if (!path && remote)
+               path = remote->name;
+
+       /* First, if there's a #16 situation, note that to prevent #13
+        * and #14.
+        */
+       if (!same(remote, head)) {
+               for (i = 1; i < head_idx; i++) {
+                       if (same(stages[i], head)) {
+                               head_match = i;
+                       }
+                       if (same(stages[i], remote)) {
+                               remote_match = i;
+                       }
+               }
+       }
+
+       /* We start with cases where the index is allowed to match
+        * something other than the head: #14(ALT) and #2ALT, where it
+        * is permitted to match the result instead.
+        */
+       /* #14, #14ALT, #2ALT */
+       if (remote && !df_conflict_head && head_match && !remote_match) {
+               if (index && !same(index, remote) && !same(index, head))
+                       reject_merge(index);
+               return merged_entry(remote, index);
+       }
+       /*
+        * If we have an entry in the index cache, then we want to
+        * make sure that it matches head.
+        */
+       if (index && !same(index, head)) {
+               reject_merge(index);
+       }
+
+       if (head) {
+               /* #5ALT, #15 */
+               if (same(head, remote))
+                       return merged_entry(head, index);
+               /* #13, #3ALT */
+               if (!df_conflict_remote && remote_match && !head_match)
+                       return merged_entry(head, index);
+       }
+
+       /* #1 */
+       if (!head && !remote && any_anc_missing)
+               return 0;
+
+       /* Under the new "aggressive" rule, we resolve mostly trivial
+        * cases that we historically had git-merge-one-file resolve.
+        */
+       if (aggressive) {
+               int head_deleted = !head && !df_conflict_head;
+               int remote_deleted = !remote && !df_conflict_remote;
+               /*
+                * Deleted in both.
+                * Deleted in one and unchanged in the other.
+                */
+               if ((head_deleted && remote_deleted) ||
+                   (head_deleted && remote && remote_match) ||
+                   (remote_deleted && head && head_match)) {
+                       if (index)
+                               return deleted_entry(index, index);
+                       else if (path)
+                               verify_absent(path, "removed");
+                       return 0;
+               }
+               /*
+                * Added in both, identically.
+                */
+               if (no_anc_exists && head && remote && same(head, remote))
+                       return merged_entry(head, index);
+
+       }
+
+       /* Below are "no merge" cases, which require that the index be
+        * up-to-date to avoid the files getting overwritten with
+        * conflict resolution files. 
+        */
+       if (index) {
+               verify_uptodate(index);
+       }
+       else if (path)
+               verify_absent(path, "overwritten");
+
+       nontrivial_merge = 1;
+
+       /* #2, #3, #4, #6, #7, #9, #11. */
+       count = 0;
+       if (!head_match || !remote_match) {
+               for (i = 1; i < head_idx; i++) {
+                       if (stages[i]) {
+                               keep_entry(stages[i]);
+                               count++;
+                               break;
+                       }
+               }
+       }
+#if DBRT_DEBUG
+       else {
+               fprintf(stderr, "read-tree: warning #16 detected\n");
+               show_stage_entry(stderr, "head   ", stages[head_match]);
+               show_stage_entry(stderr, "remote ", stages[remote_match]);
+       }
+#endif
+       if (head) { count += keep_entry(head); }
+       if (remote) { count += keep_entry(remote); }
+       return count;
+}
+
+/*
+ * Two-way merge.
+ *
+ * The rule is to "carry forward" what is in the index without losing
+ * information across a "fast forward", favoring a successful merge
+ * over a merge failure when it makes sense.  For details of the
+ * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
+ *
+ */
+static int twoway_merge(struct cache_entry **src)
+{
+       struct cache_entry *current = src[0];
+       struct cache_entry *oldtree = src[1], *newtree = src[2];
+
+       if (merge_size != 2)
+               return error("Cannot do a twoway merge of %d trees",
+                            merge_size);
+
+       if (current) {
+               if ((!oldtree && !newtree) || /* 4 and 5 */
+                   (!oldtree && newtree &&
+                    same(current, newtree)) || /* 6 and 7 */
+                   (oldtree && newtree &&
+                    same(oldtree, newtree)) || /* 14 and 15 */
+                   (oldtree && newtree &&
+                    !same(oldtree, newtree) && /* 18 and 19*/
+                    same(current, newtree))) {
+                       return keep_entry(current);
+               }
+               else if (oldtree && !newtree && same(current, oldtree)) {
+                       /* 10 or 11 */
+                       return deleted_entry(oldtree, current);
+               }
+               else if (oldtree && newtree &&
+                        same(current, oldtree) && !same(current, newtree)) {
+                       /* 20 or 21 */
+                       return merged_entry(newtree, current);
+               }
+               else {
+                       /* all other failures */
+                       if (oldtree)
+                               reject_merge(oldtree);
+                       if (current)
+                               reject_merge(current);
+                       if (newtree)
+                               reject_merge(newtree);
+                       return -1;
+               }
+       }
+       else if (newtree)
+               return merged_entry(newtree, current);
+       else
+               return deleted_entry(oldtree, current);
+}
+
+/*
+ * Bind merge.
+ *
+ * Keep the index entries at stage0, collapse stage1 but make sure
+ * stage0 does not have anything there.
+ */
+static int bind_merge(struct cache_entry **src)
+{
+       struct cache_entry *old = src[0];
+       struct cache_entry *a = src[1];
+
+       if (merge_size != 1)
+               return error("Cannot do a bind merge of %d trees\n",
+                            merge_size);
+       if (a && old)
+               die("Entry '%s' overlaps.  Cannot bind.", a->name);
+       if (!a)
+               return keep_entry(old);
+       else
+               return merged_entry(a, NULL);
+}
+
+/*
+ * One-way merge.
+ *
+ * The rule is:
+ * - take the stat information from stage0, take the data from stage1
+ */
+static int oneway_merge(struct cache_entry **src)
+{
+       struct cache_entry *old = src[0];
+       struct cache_entry *a = src[1];
+
+       if (merge_size != 1)
+               return error("Cannot do a oneway merge of %d trees",
+                            merge_size);
+
+       if (!a)
+               return deleted_entry(old, old);
+       if (old && same(old, a)) {
+               if (reset) {
+                       struct stat st;
+                       if (lstat(old->name, &st) ||
+                           ce_match_stat(old, &st, 1))
+                               old->ce_flags |= htons(CE_UPDATE);
+               }
+               return keep_entry(old);
+       }
+       return merged_entry(a, old);
+}
+
+static int read_cache_unmerged(void)
+{
+       int i;
+       struct cache_entry **dst;
+       struct cache_entry *last = NULL;
+
+       read_cache();
+       dst = active_cache;
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce = active_cache[i];
+               if (ce_stage(ce)) {
+                       if (last && !strcmp(ce->name, last->name))
+                               continue;
+                       invalidate_ce_path(ce);
+                       last = ce;
+                       ce->ce_mode = 0;
+                       ce->ce_flags &= ~htons(CE_STAGEMASK);
+               }
+               *dst++ = ce;
+       }
+       active_nr = dst - active_cache;
+       return !!last;
+}
+
+static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
+{
+       struct tree_desc desc;
+       struct name_entry entry;
+       int cnt;
+
+       memcpy(it->sha1, tree->object.sha1, 20);
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+       cnt = 0;
+       while (tree_entry(&desc, &entry)) {
+               if (!S_ISDIR(entry.mode))
+                       cnt++;
+               else {
+                       struct cache_tree_sub *sub;
+                       struct tree *subtree = lookup_tree(entry.sha1);
+                       if (!subtree->object.parsed)
+                               parse_tree(subtree);
+                       sub = cache_tree_sub(it, entry.path);
+                       sub->cache_tree = cache_tree();
+                       prime_cache_tree_rec(sub->cache_tree, subtree);
+                       cnt += sub->cache_tree->entry_count;
+               }
+       }
+       it->entry_count = cnt;
+}
+
+static void prime_cache_tree(void)
+{
+       struct tree *tree = (struct tree *)trees->item;
+       if (!tree)
+               return;
+       active_cache_tree = cache_tree();
+       prime_cache_tree_rec(active_cache_tree, tree);
+
+}
+
+static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
+
+static struct lock_file lock_file;
+
+int cmd_read_tree(int argc, const char **argv, char **envp)
+{
+       int i, newfd, stage = 0;
+       unsigned char sha1[20];
+       merge_fn_t fn = NULL;
+
+       setup_git_directory();
+       git_config(git_default_config);
+
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file());
+       if (newfd < 0)
+               die("unable to create new index file");
+
+       git_config(git_default_config);
+
+       merge = 0;
+       reset = 0;
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+
+               /* "-u" means "update", meaning that a merge will update
+                * the working tree.
+                */
+               if (!strcmp(arg, "-u")) {
+                       update = 1;
+                       continue;
+               }
+
+               if (!strcmp(arg, "-v")) {
+                       verbose_update = 1;
+                       continue;
+               }
+
+               /* "-i" means "index only", meaning that a merge will
+                * not even look at the working tree.
+                */
+               if (!strcmp(arg, "-i")) {
+                       index_only = 1;
+                       continue;
+               }
+
+               /* "--prefix=<subdirectory>/" means keep the current index
+                *  entries and put the entries from the tree under the
+                * given subdirectory.
+                */
+               if (!strncmp(arg, "--prefix=", 9)) {
+                       if (stage || merge || prefix)
+                               usage(read_tree_usage);
+                       prefix = arg + 9;
+                       merge = 1;
+                       stage = 1;
+                       if (read_cache_unmerged())
+                               die("you need to resolve your current index first");
+                       continue;
+               }
+
+               /* This differs from "-m" in that we'll silently ignore
+                * unmerged entries and overwrite working tree files that
+                * correspond to them.
+                */
+               if (!strcmp(arg, "--reset")) {
+                       if (stage || merge || prefix)
+                               usage(read_tree_usage);
+                       reset = 1;
+                       merge = 1;
+                       stage = 1;
+                       read_cache_unmerged();
+                       continue;
+               }
+
+               if (!strcmp(arg, "--trivial")) {
+                       trivial_merges_only = 1;
+                       continue;
+               }
+
+               if (!strcmp(arg, "--aggressive")) {
+                       aggressive = 1;
+                       continue;
+               }
+
+               /* "-m" stands for "merge", meaning we start in stage 1 */
+               if (!strcmp(arg, "-m")) {
+                       if (stage || merge || prefix)
+                               usage(read_tree_usage);
+                       if (read_cache_unmerged())
+                               die("you need to resolve your current index first");
+                       stage = 1;
+                       merge = 1;
+                       continue;
+               }
+
+               /* using -u and -i at the same time makes no sense */
+               if (1 < index_only + update)
+                       usage(read_tree_usage);
+
+               if (get_sha1(arg, sha1))
+                       die("Not a valid object name %s", arg);
+               if (list_tree(sha1) < 0)
+                       die("failed to unpack tree object %s", arg);
+               stage++;
+       }
+       if ((update||index_only) && !merge)
+               usage(read_tree_usage);
+
+       if (prefix) {
+               int pfxlen = strlen(prefix);
+               int pos;
+               if (prefix[pfxlen-1] != '/')
+                       die("prefix must end with /");
+               if (stage != 2)
+                       die("binding merge takes only one tree");
+               pos = cache_name_pos(prefix, pfxlen);
+               if (0 <= pos)
+                       die("corrupt index file");
+               pos = -pos-1;
+               if (pos < active_nr &&
+                   !strncmp(active_cache[pos]->name, prefix, pfxlen))
+                       die("subdirectory '%s' already exists.", prefix);
+               pos = cache_name_pos(prefix, pfxlen-1);
+               if (0 <= pos)
+                       die("file '%.*s' already exists.", pfxlen-1, prefix);
+       }
+
+       if (merge) {
+               if (stage < 2)
+                       die("just how do you expect me to merge %d trees?", stage-1);
+               switch (stage - 1) {
+               case 1:
+                       fn = prefix ? bind_merge : oneway_merge;
+                       break;
+               case 2:
+                       fn = twoway_merge;
+                       break;
+               case 3:
+               default:
+                       fn = threeway_merge;
+                       cache_tree_free(&active_cache_tree);
+                       break;
+               }
+
+               if (stage - 1 >= 3)
+                       head_idx = stage - 2;
+               else
+                       head_idx = 1;
+       }
+
+       unpack_trees(fn);
+
+       /*
+        * When reading only one tree (either the most basic form,
+        * "-m ent" or "--reset ent" form), we can obtain a fully
+        * valid cache-tree because the index must match exactly
+        * what came from the tree.
+        */
+       if (trees && trees->item && !prefix && (!merge || (stage == 2))) {
+               cache_tree_free(&active_cache_tree);
+               prime_cache_tree();
+       }
+
+       if (write_cache(newfd, active_cache, active_nr) ||
+           commit_lock_file(&lock_file))
+               die("unable to write new index file");
+       return 0;
+}
diff --git a/builtin-rev-list.c b/builtin-rev-list.c
new file mode 100644 (file)
index 0000000..e885624
--- /dev/null
@@ -0,0 +1,362 @@
+#include "cache.h"
+#include "refs.h"
+#include "tag.h"
+#include "commit.h"
+#include "tree.h"
+#include "blob.h"
+#include "tree-walk.h"
+#include "diff.h"
+#include "revision.h"
+#include "builtin.h"
+
+/* bits #0-15 in revision.h */
+
+#define COUNTED                (1u<<16)
+
+static const char rev_list_usage[] =
+"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
+"  limiting output:\n"
+"    --max-count=nr\n"
+"    --max-age=epoch\n"
+"    --min-age=epoch\n"
+"    --sparse\n"
+"    --no-merges\n"
+"    --remove-empty\n"
+"    --all\n"
+"  ordering output:\n"
+"    --topo-order\n"
+"    --date-order\n"
+"  formatting output:\n"
+"    --parents\n"
+"    --objects | --objects-edge\n"
+"    --unpacked\n"
+"    --header | --pretty\n"
+"    --abbrev=nr | --no-abbrev\n"
+"    --abbrev-commit\n"
+"  special purpose:\n"
+"    --bisect"
+;
+
+static struct rev_info revs;
+
+static int bisect_list = 0;
+static int show_timestamp = 0;
+static int hdr_termination = 0;
+static const char *header_prefix;
+
+static void show_commit(struct commit *commit)
+{
+       if (show_timestamp)
+               printf("%lu ", commit->date);
+       if (header_prefix)
+               fputs(header_prefix, stdout);
+       if (commit->object.flags & BOUNDARY)
+               putchar('-');
+       if (revs.abbrev_commit && revs.abbrev)
+               fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
+                     stdout);
+       else
+               fputs(sha1_to_hex(commit->object.sha1), stdout);
+       if (revs.parents) {
+               struct commit_list *parents = commit->parents;
+               while (parents) {
+                       struct object *o = &(parents->item->object);
+                       parents = parents->next;
+                       if (o->flags & TMP_MARK)
+                               continue;
+                       printf(" %s", sha1_to_hex(o->sha1));
+                       o->flags |= TMP_MARK;
+               }
+               /* TMP_MARK is a general purpose flag that can
+                * be used locally, but the user should clean
+                * things up after it is done with them.
+                */
+               for (parents = commit->parents;
+                    parents;
+                    parents = parents->next)
+                       parents->item->object.flags &= ~TMP_MARK;
+       }
+       if (revs.commit_format == CMIT_FMT_ONELINE)
+               putchar(' ');
+       else
+               putchar('\n');
+
+       if (revs.verbose_header) {
+               static char pretty_header[16384];
+               pretty_print_commit(revs.commit_format, commit, ~0,
+                                   pretty_header, sizeof(pretty_header),
+                                   revs.abbrev, NULL, NULL);
+               printf("%s%c", pretty_header, hdr_termination);
+       }
+       fflush(stdout);
+}
+
+static struct object_list **process_blob(struct blob *blob,
+                                        struct object_list **p,
+                                        struct name_path *path,
+                                        const char *name)
+{
+       struct object *obj = &blob->object;
+
+       if (!revs.blob_objects)
+               return p;
+       if (obj->flags & (UNINTERESTING | SEEN))
+               return p;
+       obj->flags |= SEEN;
+       name = strdup(name);
+       return add_object(obj, p, path, name);
+}
+
+static struct object_list **process_tree(struct tree *tree,
+                                        struct object_list **p,
+                                        struct name_path *path,
+                                        const char *name)
+{
+       struct object *obj = &tree->object;
+       struct tree_desc desc;
+       struct name_entry entry;
+       struct name_path me;
+
+       if (!revs.tree_objects)
+               return p;
+       if (obj->flags & (UNINTERESTING | SEEN))
+               return p;
+       if (parse_tree(tree) < 0)
+               die("bad tree object %s", sha1_to_hex(obj->sha1));
+       obj->flags |= SEEN;
+       name = strdup(name);
+       p = add_object(obj, p, path, name);
+       me.up = path;
+       me.elem = name;
+       me.elem_len = strlen(name);
+
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+
+       while (tree_entry(&desc, &entry)) {
+               if (S_ISDIR(entry.mode))
+                       p = process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
+               else
+                       p = process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
+       }
+       free(tree->buffer);
+       tree->buffer = NULL;
+       return p;
+}
+
+static void show_commit_list(struct rev_info *revs)
+{
+       struct commit *commit;
+       struct object_list *objects = NULL, **p = &objects, *pending;
+
+       while ((commit = get_revision(revs)) != NULL) {
+               p = process_tree(commit->tree, p, NULL, "");
+               show_commit(commit);
+       }
+       for (pending = revs->pending_objects; pending; pending = pending->next) {
+               struct object *obj = pending->item;
+               const char *name = pending->name;
+               if (obj->flags & (UNINTERESTING | SEEN))
+                       continue;
+               if (obj->type == tag_type) {
+                       obj->flags |= SEEN;
+                       p = add_object(obj, p, NULL, name);
+                       continue;
+               }
+               if (obj->type == tree_type) {
+                       p = process_tree((struct tree *)obj, p, NULL, name);
+                       continue;
+               }
+               if (obj->type == blob_type) {
+                       p = process_blob((struct blob *)obj, p, NULL, name);
+                       continue;
+               }
+               die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
+       }
+       while (objects) {
+               /* An object with name "foo\n0000000..." can be used to
+                * confuse downstream git-pack-objects very badly.
+                */
+               const char *ep = strchr(objects->name, '\n');
+               if (ep) {
+                       printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
+                              (int) (ep - objects->name),
+                              objects->name);
+               }
+               else
+                       printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
+               objects = objects->next;
+       }
+}
+
+/*
+ * This is a truly stupid algorithm, but it's only
+ * used for bisection, and we just don't care enough.
+ *
+ * We care just barely enough to avoid recursing for
+ * non-merge entries.
+ */
+static int count_distance(struct commit_list *entry)
+{
+       int nr = 0;
+
+       while (entry) {
+               struct commit *commit = entry->item;
+               struct commit_list *p;
+
+               if (commit->object.flags & (UNINTERESTING | COUNTED))
+                       break;
+               if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
+                       nr++;
+               commit->object.flags |= COUNTED;
+               p = commit->parents;
+               entry = p;
+               if (p) {
+                       p = p->next;
+                       while (p) {
+                               nr += count_distance(p);
+                               p = p->next;
+                       }
+               }
+       }
+
+       return nr;
+}
+
+static void clear_distance(struct commit_list *list)
+{
+       while (list) {
+               struct commit *commit = list->item;
+               commit->object.flags &= ~COUNTED;
+               list = list->next;
+       }
+}
+
+static struct commit_list *find_bisection(struct commit_list *list)
+{
+       int nr, closest;
+       struct commit_list *p, *best;
+
+       nr = 0;
+       p = list;
+       while (p) {
+               if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
+                       nr++;
+               p = p->next;
+       }
+       closest = 0;
+       best = list;
+
+       for (p = list; p; p = p->next) {
+               int distance;
+
+               if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
+                       continue;
+
+               distance = count_distance(p);
+               clear_distance(list);
+               if (nr - distance < distance)
+                       distance = nr - distance;
+               if (distance > closest) {
+                       best = p;
+                       closest = distance;
+               }
+       }
+       if (best)
+               best->next = NULL;
+       return best;
+}
+
+static void mark_edge_parents_uninteresting(struct commit *commit)
+{
+       struct commit_list *parents;
+
+       for (parents = commit->parents; parents; parents = parents->next) {
+               struct commit *parent = parents->item;
+               if (!(parent->object.flags & UNINTERESTING))
+                       continue;
+               mark_tree_uninteresting(parent->tree);
+               if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
+                       parent->object.flags |= SHOWN;
+                       printf("-%s\n", sha1_to_hex(parent->object.sha1));
+               }
+       }
+}
+
+static void mark_edges_uninteresting(struct commit_list *list)
+{
+       for ( ; list; list = list->next) {
+               struct commit *commit = list->item;
+
+               if (commit->object.flags & UNINTERESTING) {
+                       mark_tree_uninteresting(commit->tree);
+                       continue;
+               }
+               mark_edge_parents_uninteresting(commit);
+       }
+}
+
+int cmd_rev_list(int argc, const char **argv, char **envp)
+{
+       struct commit_list *list;
+       int i;
+
+       init_revisions(&revs);
+       revs.abbrev = 0;
+       revs.commit_format = CMIT_FMT_UNSPECIFIED;
+       argc = setup_revisions(argc, argv, &revs, NULL);
+
+       for (i = 1 ; i < argc; i++) {
+               const char *arg = argv[i];
+
+               if (!strcmp(arg, "--header")) {
+                       revs.verbose_header = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--timestamp")) {
+                       show_timestamp = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--bisect")) {
+                       bisect_list = 1;
+                       continue;
+               }
+               usage(rev_list_usage);
+
+       }
+       if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
+               /* The command line has a --pretty  */
+               hdr_termination = '\n';
+               if (revs.commit_format == CMIT_FMT_ONELINE)
+                       header_prefix = "";
+               else
+                       header_prefix = "commit ";
+       }
+       else if (revs.verbose_header)
+               /* Only --header was specified */
+               revs.commit_format = CMIT_FMT_RAW;
+
+       list = revs.commits;
+
+       if ((!list &&
+            (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
+             !revs.pending_objects)) ||
+           revs.diff)
+               usage(rev_list_usage);
+
+       save_commit_buffer = revs.verbose_header;
+       track_object_refs = 0;
+       if (bisect_list)
+               revs.limited = 1;
+
+       prepare_revision_walk(&revs);
+       if (revs.tree_objects)
+               mark_edges_uninteresting(revs.commits);
+
+       if (bisect_list)
+               revs.commits = find_bisection(revs.commits);
+
+       show_commit_list(&revs);
+
+       return 0;
+}
diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c
new file mode 100644 (file)
index 0000000..b27a6d3
--- /dev/null
@@ -0,0 +1,365 @@
+/*
+ * rev-parse.c
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#include "cache.h"
+#include "commit.h"
+#include "refs.h"
+#include "quote.h"
+#include "builtin.h"
+
+#define DO_REVS                1
+#define DO_NOREV       2
+#define DO_FLAGS       4
+#define DO_NONFLAGS    8
+static int filter = ~0;
+
+static const char *def = NULL;
+
+#define NORMAL 0
+#define REVERSED 1
+static int show_type = NORMAL;
+static int symbolic = 0;
+static int abbrev = 0;
+static int output_sq = 0;
+
+static int revs_count = 0;
+
+/*
+ * Some arguments are relevant "revision" arguments,
+ * others are about output format or other details.
+ * This sorts it all out.
+ */
+static int is_rev_argument(const char *arg)
+{
+       static const char *rev_args[] = {
+               "--all",
+               "--bisect",
+               "--dense",
+               "--branches",
+               "--header",
+               "--max-age=",
+               "--max-count=",
+               "--min-age=",
+               "--no-merges",
+               "--objects",
+               "--objects-edge",
+               "--parents",
+               "--pretty",
+               "--remotes",
+               "--sparse",
+               "--tags",
+               "--topo-order",
+               "--date-order",
+               "--unpacked",
+               NULL
+       };
+       const char **p = rev_args;
+
+       /* accept -<digit>, like traditional "head" */
+       if ((*arg == '-') && isdigit(arg[1]))
+               return 1;
+
+       for (;;) {
+               const char *str = *p++;
+               int len;
+               if (!str)
+                       return 0;
+               len = strlen(str);
+               if (!strcmp(arg, str) ||
+                   (str[len-1] == '=' && !strncmp(arg, str, len)))
+                       return 1;
+       }
+}
+
+/* Output argument as a string, either SQ or normal */
+static void show(const char *arg)
+{
+       if (output_sq) {
+               int sq = '\'', ch;
+
+               putchar(sq);
+               while ((ch = *arg++)) {
+                       if (ch == sq)
+                               fputs("'\\'", stdout);
+                       putchar(ch);
+               }
+               putchar(sq);
+               putchar(' ');
+       }
+       else
+               puts(arg);
+}
+
+/* Output a revision, only if filter allows it */
+static void show_rev(int type, const unsigned char *sha1, const char *name)
+{
+       if (!(filter & DO_REVS))
+               return;
+       def = NULL;
+       revs_count++;
+
+       if (type != show_type)
+               putchar('^');
+       if (symbolic && name)
+               show(name);
+       else if (abbrev)
+               show(find_unique_abbrev(sha1, abbrev));
+       else
+               show(sha1_to_hex(sha1));
+}
+
+/* Output a flag, only if filter allows it. */
+static int show_flag(const char *arg)
+{
+       if (!(filter & DO_FLAGS))
+               return 0;
+       if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
+               show(arg);
+               return 1;
+       }
+       return 0;
+}
+
+static void show_default(void)
+{
+       const char *s = def;
+
+       if (s) {
+               unsigned char sha1[20];
+
+               def = NULL;
+               if (!get_sha1(s, sha1)) {
+                       show_rev(NORMAL, sha1, s);
+                       return;
+               }
+       }
+}
+
+static int show_reference(const char *refname, const unsigned char *sha1)
+{
+       show_rev(NORMAL, sha1, refname);
+       return 0;
+}
+
+static void show_datestring(const char *flag, const char *datestr)
+{
+       static char buffer[100];
+
+       /* date handling requires both flags and revs */
+       if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
+               return;
+       snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
+       show(buffer);
+}
+
+static int show_file(const char *arg)
+{
+       show_default();
+       if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
+               show(arg);
+               return 1;
+       }
+       return 0;
+}
+
+int cmd_rev_parse(int argc, const char **argv, char **envp)
+{
+       int i, as_is = 0, verify = 0;
+       unsigned char sha1[20];
+       const char *prefix = setup_git_directory();
+
+       git_config(git_default_config);
+
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+               char *dotdot;
+
+               if (as_is) {
+                       if (show_file(arg) && as_is < 2)
+                               verify_filename(prefix, arg);
+                       continue;
+               }
+               if (!strcmp(arg,"-n")) {
+                       if (++i >= argc)
+                               die("-n requires an argument");
+                       if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
+                               show(arg);
+                               show(argv[i]);
+                       }
+                       continue;
+               }
+               if (!strncmp(arg,"-n",2)) {
+                       if ((filter & DO_FLAGS) && (filter & DO_REVS))
+                               show(arg);
+                       continue;
+               }
+
+               if (*arg == '-') {
+                       if (!strcmp(arg, "--")) {
+                               as_is = 2;
+                               /* Pass on the "--" if we show anything but files.. */
+                               if (filter & (DO_FLAGS | DO_REVS))
+                                       show_file(arg);
+                               continue;
+                       }
+                       if (!strcmp(arg, "--default")) {
+                               def = argv[i+1];
+                               i++;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--revs-only")) {
+                               filter &= ~DO_NOREV;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--no-revs")) {
+                               filter &= ~DO_REVS;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--flags")) {
+                               filter &= ~DO_NONFLAGS;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--no-flags")) {
+                               filter &= ~DO_FLAGS;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--verify")) {
+                               filter &= ~(DO_FLAGS|DO_NOREV);
+                               verify = 1;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--short") ||
+                           !strncmp(arg, "--short=", 8)) {
+                               filter &= ~(DO_FLAGS|DO_NOREV);
+                               verify = 1;
+                               abbrev = DEFAULT_ABBREV;
+                               if (arg[7] == '=')
+                                       abbrev = strtoul(arg + 8, NULL, 10);
+                               if (abbrev < MINIMUM_ABBREV)
+                                       abbrev = MINIMUM_ABBREV;
+                               else if (40 <= abbrev)
+                                       abbrev = 40;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--sq")) {
+                               output_sq = 1;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--not")) {
+                               show_type ^= REVERSED;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--symbolic")) {
+                               symbolic = 1;
+                               continue;
+                       }
+                       if (!strcmp(arg, "--all")) {
+                               for_each_ref(show_reference);
+                               continue;
+                       }
+                       if (!strcmp(arg, "--branches")) {
+                               for_each_branch_ref(show_reference);
+                               continue;
+                       }
+                       if (!strcmp(arg, "--tags")) {
+                               for_each_tag_ref(show_reference);
+                               continue;
+                       }
+                       if (!strcmp(arg, "--remotes")) {
+                               for_each_remote_ref(show_reference);
+                               continue;
+                       }
+                       if (!strcmp(arg, "--show-prefix")) {
+                               if (prefix)
+                                       puts(prefix);
+                               continue;
+                       }
+                       if (!strcmp(arg, "--show-cdup")) {
+                               const char *pfx = prefix;
+                               while (pfx) {
+                                       pfx = strchr(pfx, '/');
+                                       if (pfx) {
+                                               pfx++;
+                                               printf("../");
+                                       }
+                               }
+                               putchar('\n');
+                               continue;
+                       }
+                       if (!strcmp(arg, "--git-dir")) {
+                               const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
+                               static char cwd[PATH_MAX];
+                               if (gitdir) {
+                                       puts(gitdir);
+                                       continue;
+                               }
+                               if (!prefix) {
+                                       puts(".git");
+                                       continue;
+                               }
+                               if (!getcwd(cwd, PATH_MAX))
+                                       die("unable to get current working directory");
+                               printf("%s/.git\n", cwd);
+                               continue;
+                       }
+                       if (!strncmp(arg, "--since=", 8)) {
+                               show_datestring("--max-age=", arg+8);
+                               continue;
+                       }
+                       if (!strncmp(arg, "--after=", 8)) {
+                               show_datestring("--max-age=", arg+8);
+                               continue;
+                       }
+                       if (!strncmp(arg, "--before=", 9)) {
+                               show_datestring("--min-age=", arg+9);
+                               continue;
+                       }
+                       if (!strncmp(arg, "--until=", 8)) {
+                               show_datestring("--min-age=", arg+8);
+                               continue;
+                       }
+                       if (show_flag(arg) && verify)
+                               die("Needed a single revision");
+                       continue;
+               }
+
+               /* Not a flag argument */
+               dotdot = strstr(arg, "..");
+               if (dotdot) {
+                       unsigned char end[20];
+                       char *next = dotdot + 2;
+                       const char *this = arg;
+                       *dotdot = 0;
+                       if (!*next)
+                               next = "HEAD";
+                       if (dotdot == arg)
+                               this = "HEAD";
+                       if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
+                               show_rev(NORMAL, end, next);
+                               show_rev(REVERSED, sha1, this);
+                               continue;
+                       }
+                       *dotdot = '.';
+               }
+               if (!get_sha1(arg, sha1)) {
+                       show_rev(NORMAL, sha1, arg);
+                       continue;
+               }
+               if (*arg == '^' && !get_sha1(arg+1, sha1)) {
+                       show_rev(REVERSED, sha1, arg+1);
+                       continue;
+               }
+               as_is = 1;
+               if (!show_file(arg))
+                       continue;
+               if (verify)
+                       die("Needed a single revision");
+               verify_filename(prefix, arg);
+       }
+       show_default();
+       if (verify && revs_count != 1)
+               die("Needed a single revision");
+       return 0;
+}
diff --git a/builtin-rm.c b/builtin-rm.c
new file mode 100644 (file)
index 0000000..4d56a1f
--- /dev/null
@@ -0,0 +1,155 @@
+/*
+ * "git rm" builtin command
+ *
+ * Copyright (C) Linus Torvalds 2006
+ */
+#include "cache.h"
+#include "builtin.h"
+#include "dir.h"
+#include "cache-tree.h"
+
+static const char builtin_rm_usage[] =
+"git-rm [-n] [-v] [-f] <filepattern>...";
+
+static struct {
+       int nr, alloc;
+       const char **name;
+} list;
+
+static void add_list(const char *name)
+{
+       if (list.nr >= list.alloc) {
+               list.alloc = alloc_nr(list.alloc);
+               list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
+       }
+       list.name[list.nr++] = name;
+}
+
+static int remove_file(const char *name)
+{
+       int ret;
+       char *slash;
+
+       ret = unlink(name);
+       if (!ret && (slash = strrchr(name, '/'))) {
+               char *n = strdup(name);
+               do {
+                       n[slash - name] = 0;
+                       name = n;
+               } while (!rmdir(name) && (slash = strrchr(name, '/')));
+       }
+       return ret;
+}
+
+static struct lock_file lock_file;
+
+int cmd_rm(int argc, const char **argv, char **envp)
+{
+       int i, newfd;
+       int verbose = 0, show_only = 0, force = 0;
+       const char *prefix = setup_git_directory();
+       const char **pathspec;
+       char *seen;
+
+       git_config(git_default_config);
+
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file());
+       if (newfd < 0)
+               die("unable to create new index file");
+
+       if (read_cache() < 0)
+               die("index file corrupt");
+
+       for (i = 1 ; i < argc ; i++) {
+               const char *arg = argv[i];
+
+               if (*arg != '-')
+                       break;
+               if (!strcmp(arg, "--")) {
+                       i++;
+                       break;
+               }
+               if (!strcmp(arg, "-n")) {
+                       show_only = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-v")) {
+                       verbose = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-f")) {
+                       force = 1;
+                       continue;
+               }
+               die(builtin_rm_usage);
+       }
+       if (argc <= i)
+               usage(builtin_rm_usage);
+
+       pathspec = get_pathspec(prefix, argv + i);
+       seen = NULL;
+       for (i = 0; pathspec[i] ; i++)
+               /* nothing */;
+       seen = xmalloc(i);
+       memset(seen, 0, i);
+
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce = active_cache[i];
+               if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
+                       continue;
+               add_list(ce->name);
+       }
+
+       if (pathspec) {
+               const char *match;
+               for (i = 0; (match = pathspec[i]) != NULL ; i++) {
+                       if (*match && !seen[i])
+                               die("pathspec '%s' did not match any files", match);
+               }
+       }
+
+       /*
+        * First remove the names from the index: we won't commit
+        * the index unless all of them succeed
+        */
+       for (i = 0; i < list.nr; i++) {
+               const char *path = list.name[i];
+               printf("rm '%s'\n", path);
+
+               if (remove_file_from_cache(path))
+                       die("git rm: unable to remove %s", path);
+               cache_tree_invalidate_path(active_cache_tree, path);
+       }
+
+       if (show_only)
+               return 0;
+
+       /*
+        * Then, if we used "-f", remove the filenames from the
+        * workspace. If we fail to remove the first one, we
+        * abort the "git rm" (but once we've successfully removed
+        * any file at all, we'll go ahead and commit to it all:
+        * by then we've already committed ourself and can't fail
+        * in the middle)
+        */
+       if (force) {
+               int removed = 0;
+               for (i = 0; i < list.nr; i++) {
+                       const char *path = list.name[i];
+                       if (!remove_file(path)) {
+                               removed = 1;
+                               continue;
+                       }
+                       if (!removed)
+                               die("git rm: %s: %s", path, strerror(errno));
+               }
+       }
+
+       if (active_cache_changed) {
+               if (write_cache(newfd, active_cache, active_nr) ||
+                   commit_lock_file(&lock_file))
+                       die("Unable to write new index file");
+       }
+
+       return 0;
+}
diff --git a/builtin-show-branch.c b/builtin-show-branch.c
new file mode 100644 (file)
index 0000000..2895140
--- /dev/null
@@ -0,0 +1,789 @@
+#include <stdlib.h>
+#include <fnmatch.h>
+#include "cache.h"
+#include "commit.h"
+#include "refs.h"
+#include "builtin.h"
+
+static const char show_branch_usage[] =
+"git-show-branch [--dense] [--current] [--all] [--heads] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...]";
+
+static int default_num = 0;
+static int default_alloc = 0;
+static const char **default_arg = NULL;
+
+#define UNINTERESTING  01
+
+#define REV_SHIFT       2
+#define MAX_REVS       29 /* should not exceed bits_per_int - REV_SHIFT */
+
+static struct commit *interesting(struct commit_list *list)
+{
+       while (list) {
+               struct commit *commit = list->item;
+               list = list->next;
+               if (commit->object.flags & UNINTERESTING)
+                       continue;
+               return commit;
+       }
+       return NULL;
+}
+
+static struct commit *pop_one_commit(struct commit_list **list_p)
+{
+       struct commit *commit;
+       struct commit_list *list;
+       list = *list_p;
+       commit = list->item;
+       *list_p = list->next;
+       free(list);
+       return commit;
+}
+
+struct commit_name {
+       const char *head_name; /* which head's ancestor? */
+       int generation; /* how many parents away from head_name */
+};
+
+/* Name the commit as nth generation ancestor of head_name;
+ * we count only the first-parent relationship for naming purposes.
+ */
+static void name_commit(struct commit *commit, const char *head_name, int nth)
+{
+       struct commit_name *name;
+       if (!commit->object.util)
+               commit->object.util = xmalloc(sizeof(struct commit_name));
+       name = commit->object.util;
+       name->head_name = head_name;
+       name->generation = nth;
+}
+
+/* Parent is the first parent of the commit.  We may name it
+ * as (n+1)th generation ancestor of the same head_name as
+ * commit is nth generation ancestor of, if that generation
+ * number is better than the name it already has.
+ */
+static void name_parent(struct commit *commit, struct commit *parent)
+{
+       struct commit_name *commit_name = commit->object.util;
+       struct commit_name *parent_name = parent->object.util;
+       if (!commit_name)
+               return;
+       if (!parent_name ||
+           commit_name->generation + 1 < parent_name->generation)
+               name_commit(parent, commit_name->head_name,
+                           commit_name->generation + 1);
+}
+
+static int name_first_parent_chain(struct commit *c)
+{
+       int i = 0;
+       while (c) {
+               struct commit *p;
+               if (!c->object.util)
+                       break;
+               if (!c->parents)
+                       break;
+               p = c->parents->item;
+               if (!p->object.util) {
+                       name_parent(c, p);
+                       i++;
+               }
+               c = p;
+       }
+       return i;
+}
+
+static void name_commits(struct commit_list *list,
+                        struct commit **rev,
+                        char **ref_name,
+                        int num_rev)
+{
+       struct commit_list *cl;
+       struct commit *c;
+       int i;
+
+       /* First give names to the given heads */
+       for (cl = list; cl; cl = cl->next) {
+               c = cl->item;
+               if (c->object.util)
+                       continue;
+               for (i = 0; i < num_rev; i++) {
+                       if (rev[i] == c) {
+                               name_commit(c, ref_name[i], 0);
+                               break;
+                       }
+               }
+       }
+
+       /* Then commits on the first parent ancestry chain */
+       do {
+               i = 0;
+               for (cl = list; cl; cl = cl->next) {
+                       i += name_first_parent_chain(cl->item);
+               }
+       } while (i);
+
+       /* Finally, any unnamed commits */
+       do {
+               i = 0;
+               for (cl = list; cl; cl = cl->next) {
+                       struct commit_list *parents;
+                       struct commit_name *n;
+                       int nth;
+                       c = cl->item;
+                       if (!c->object.util)
+                               continue;
+                       n = c->object.util;
+                       parents = c->parents;
+                       nth = 0;
+                       while (parents) {
+                               struct commit *p = parents->item;
+                               char newname[1000], *en;
+                               parents = parents->next;
+                               nth++;
+                               if (p->object.util)
+                                       continue;
+                               en = newname;
+                               switch (n->generation) {
+                               case 0:
+                                       en += sprintf(en, "%s", n->head_name);
+                                       break;
+                               case 1:
+                                       en += sprintf(en, "%s^", n->head_name);
+                                       break;
+                               default:
+                                       en += sprintf(en, "%s~%d",
+                                               n->head_name, n->generation);
+                                       break;
+                               }
+                               if (nth == 1)
+                                       en += sprintf(en, "^");
+                               else
+                                       en += sprintf(en, "^%d", nth);
+                               name_commit(p, strdup(newname), 0);
+                               i++;
+                               name_first_parent_chain(p);
+                       }
+               }
+       } while (i);
+}
+
+static int mark_seen(struct commit *commit, struct commit_list **seen_p)
+{
+       if (!commit->object.flags) {
+               insert_by_date(commit, seen_p);
+               return 1;
+       }
+       return 0;
+}
+
+static void join_revs(struct commit_list **list_p,
+                     struct commit_list **seen_p,
+                     int num_rev, int extra)
+{
+       int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
+       int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
+
+       while (*list_p) {
+               struct commit_list *parents;
+               int still_interesting = !!interesting(*list_p);
+               struct commit *commit = pop_one_commit(list_p);
+               int flags = commit->object.flags & all_mask;
+
+               if (!still_interesting && extra <= 0)
+                       break;
+
+               mark_seen(commit, seen_p);
+               if ((flags & all_revs) == all_revs)
+                       flags |= UNINTERESTING;
+               parents = commit->parents;
+
+               while (parents) {
+                       struct commit *p = parents->item;
+                       int this_flag = p->object.flags;
+                       parents = parents->next;
+                       if ((this_flag & flags) == flags)
+                               continue;
+                       if (!p->object.parsed)
+                               parse_commit(p);
+                       if (mark_seen(p, seen_p) && !still_interesting)
+                               extra--;
+                       p->object.flags |= flags;
+                       insert_by_date(p, list_p);
+               }
+       }
+
+       /*
+        * Postprocess to complete well-poisoning.
+        *
+        * At this point we have all the commits we have seen in
+        * seen_p list (which happens to be sorted chronologically but
+        * it does not really matter).  Mark anything that can be
+        * reached from uninteresting commits not interesting.
+        */
+       for (;;) {
+               int changed = 0;
+               struct commit_list *s;
+               for (s = *seen_p; s; s = s->next) {
+                       struct commit *c = s->item;
+                       struct commit_list *parents;
+
+                       if (((c->object.flags & all_revs) != all_revs) &&
+                           !(c->object.flags & UNINTERESTING))
+                               continue;
+
+                       /* The current commit is either a merge base or
+                        * already uninteresting one.  Mark its parents
+                        * as uninteresting commits _only_ if they are
+                        * already parsed.  No reason to find new ones
+                        * here.
+                        */
+                       parents = c->parents;
+                       while (parents) {
+                               struct commit *p = parents->item;
+                               parents = parents->next;
+                               if (!(p->object.flags & UNINTERESTING)) {
+                                       p->object.flags |= UNINTERESTING;
+                                       changed = 1;
+                               }
+                       }
+               }
+               if (!changed)
+                       break;
+       }
+}
+
+static void show_one_commit(struct commit *commit, int no_name)
+{
+       char pretty[256], *cp;
+       struct commit_name *name = commit->object.util;
+       if (commit->object.parsed)
+               pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
+                                   pretty, sizeof(pretty), 0, NULL, NULL);
+       else
+               strcpy(pretty, "(unavailable)");
+       if (!strncmp(pretty, "[PATCH] ", 8))
+               cp = pretty + 8;
+       else
+               cp = pretty;
+
+       if (!no_name) {
+               if (name && name->head_name) {
+                       printf("[%s", name->head_name);
+                       if (name->generation) {
+                               if (name->generation == 1)
+                                       printf("^");
+                               else
+                                       printf("~%d", name->generation);
+                       }
+                       printf("] ");
+               }
+               else
+                       printf("[%s] ",
+                              find_unique_abbrev(commit->object.sha1, 7));
+       }
+       puts(cp);
+}
+
+static char *ref_name[MAX_REVS + 1];
+static int ref_name_cnt;
+
+static const char *find_digit_prefix(const char *s, int *v)
+{
+       const char *p;
+       int ver;
+       char ch;
+
+       for (p = s, ver = 0;
+            '0' <= (ch = *p) && ch <= '9';
+            p++)
+               ver = ver * 10 + ch - '0';
+       *v = ver;
+       return p;
+}
+
+
+static int version_cmp(const char *a, const char *b)
+{
+       while (1) {
+               int va, vb;
+
+               a = find_digit_prefix(a, &va);
+               b = find_digit_prefix(b, &vb);
+               if (va != vb)
+                       return va - vb;
+
+               while (1) {
+                       int ca = *a;
+                       int cb = *b;
+                       if ('0' <= ca && ca <= '9')
+                               ca = 0;
+                       if ('0' <= cb && cb <= '9')
+                               cb = 0;
+                       if (ca != cb)
+                               return ca - cb;
+                       if (!ca)
+                               break;
+                       a++;
+                       b++;
+               }
+               if (!*a && !*b)
+                       return 0;
+       }
+}
+
+static int compare_ref_name(const void *a_, const void *b_)
+{
+       const char * const*a = a_, * const*b = b_;
+       return version_cmp(*a, *b);
+}
+
+static void sort_ref_range(int bottom, int top)
+{
+       qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
+             compare_ref_name);
+}
+
+static int append_ref(const char *refname, const unsigned char *sha1)
+{
+       struct commit *commit = lookup_commit_reference_gently(sha1, 1);
+       int i;
+
+       if (!commit)
+               return 0;
+       /* Avoid adding the same thing twice */
+       for (i = 0; i < ref_name_cnt; i++)
+               if (!strcmp(refname, ref_name[i]))
+                       return 0;
+
+       if (MAX_REVS <= ref_name_cnt) {
+               fprintf(stderr, "warning: ignoring %s; "
+                       "cannot handle more than %d refs\n",
+                       refname, MAX_REVS);
+               return 0;
+       }
+       ref_name[ref_name_cnt++] = strdup(refname);
+       ref_name[ref_name_cnt] = NULL;
+       return 0;
+}
+
+static int append_head_ref(const char *refname, const unsigned char *sha1)
+{
+       unsigned char tmp[20];
+       int ofs = 11;
+       if (strncmp(refname, "refs/heads/", ofs))
+               return 0;
+       /* If both heads/foo and tags/foo exists, get_sha1 would
+        * get confused.
+        */
+       if (get_sha1(refname + ofs, tmp) || memcmp(tmp, sha1, 20))
+               ofs = 5;
+       return append_ref(refname + ofs, sha1);
+}
+
+static int append_tag_ref(const char *refname, const unsigned char *sha1)
+{
+       if (strncmp(refname, "refs/tags/", 10))
+               return 0;
+       return append_ref(refname + 5, sha1);
+}
+
+static const char *match_ref_pattern = NULL;
+static int match_ref_slash = 0;
+static int count_slash(const char *s)
+{
+       int cnt = 0;
+       while (*s)
+               if (*s++ == '/')
+                       cnt++;
+       return cnt;
+}
+
+static int append_matching_ref(const char *refname, const unsigned char *sha1)
+{
+       /* we want to allow pattern hold/<asterisk> to show all
+        * branches under refs/heads/hold/, and v0.99.9? to show
+        * refs/tags/v0.99.9a and friends.
+        */
+       const char *tail;
+       int slash = count_slash(refname);
+       for (tail = refname; *tail && match_ref_slash < slash; )
+               if (*tail++ == '/')
+                       slash--;
+       if (!*tail)
+               return 0;
+       if (fnmatch(match_ref_pattern, tail, 0))
+               return 0;
+       if (!strncmp("refs/heads/", refname, 11))
+               return append_head_ref(refname, sha1);
+       if (!strncmp("refs/tags/", refname, 10))
+               return append_tag_ref(refname, sha1);
+       return append_ref(refname, sha1);
+}
+
+static void snarf_refs(int head, int tag)
+{
+       if (head) {
+               int orig_cnt = ref_name_cnt;
+               for_each_ref(append_head_ref);
+               sort_ref_range(orig_cnt, ref_name_cnt);
+       }
+       if (tag) {
+               int orig_cnt = ref_name_cnt;
+               for_each_ref(append_tag_ref);
+               sort_ref_range(orig_cnt, ref_name_cnt);
+       }
+}
+
+static int rev_is_head(char *head_path, int headlen, char *name,
+                      unsigned char *head_sha1, unsigned char *sha1)
+{
+       int namelen;
+       if ((!head_path[0]) ||
+           (head_sha1 && sha1 && memcmp(head_sha1, sha1, 20)))
+               return 0;
+       namelen = strlen(name);
+       if ((headlen < namelen) ||
+           memcmp(head_path + headlen - namelen, name, namelen))
+               return 0;
+       if (headlen == namelen ||
+           head_path[headlen - namelen - 1] == '/')
+               return 1;
+       return 0;
+}
+
+static int show_merge_base(struct commit_list *seen, int num_rev)
+{
+       int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
+       int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
+       int exit_status = 1;
+
+       while (seen) {
+               struct commit *commit = pop_one_commit(&seen);
+               int flags = commit->object.flags & all_mask;
+               if (!(flags & UNINTERESTING) &&
+                   ((flags & all_revs) == all_revs)) {
+                       puts(sha1_to_hex(commit->object.sha1));
+                       exit_status = 0;
+                       commit->object.flags |= UNINTERESTING;
+               }
+       }
+       return exit_status;
+}
+
+static int show_independent(struct commit **rev,
+                           int num_rev,
+                           char **ref_name,
+                           unsigned int *rev_mask)
+{
+       int i;
+
+       for (i = 0; i < num_rev; i++) {
+               struct commit *commit = rev[i];
+               unsigned int flag = rev_mask[i];
+
+               if (commit->object.flags == flag)
+                       puts(sha1_to_hex(commit->object.sha1));
+               commit->object.flags |= UNINTERESTING;
+       }
+       return 0;
+}
+
+static void append_one_rev(const char *av)
+{
+       unsigned char revkey[20];
+       if (!get_sha1(av, revkey)) {
+               append_ref(av, revkey);
+               return;
+       }
+       if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
+               /* glob style match */
+               int saved_matches = ref_name_cnt;
+               match_ref_pattern = av;
+               match_ref_slash = count_slash(av);
+               for_each_ref(append_matching_ref);
+               if (saved_matches == ref_name_cnt &&
+                   ref_name_cnt < MAX_REVS)
+                       error("no matching refs with %s", av);
+               if (saved_matches + 1 < ref_name_cnt)
+                       sort_ref_range(saved_matches, ref_name_cnt);
+               return;
+       }
+       die("bad sha1 reference %s", av);
+}
+
+static int git_show_branch_config(const char *var, const char *value)
+{
+       if (!strcmp(var, "showbranch.default")) {
+               if (default_alloc <= default_num + 1) {
+                       default_alloc = default_alloc * 3 / 2 + 20;
+                       default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc);
+               }
+               default_arg[default_num++] = strdup(value);
+               default_arg[default_num] = NULL;
+               return 0;
+       }
+
+       return git_default_config(var, value);
+}
+
+static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
+{
+       /* If the commit is tip of the named branches, do not
+        * omit it.
+        * Otherwise, if it is a merge that is reachable from only one
+        * tip, it is not that interesting.
+        */
+       int i, flag, count;
+       for (i = 0; i < n; i++)
+               if (rev[i] == commit)
+                       return 0;
+       flag = commit->object.flags;
+       for (i = count = 0; i < n; i++) {
+               if (flag & (1u << (i + REV_SHIFT)))
+                       count++;
+       }
+       if (count == 1)
+               return 1;
+       return 0;
+}
+
+int cmd_show_branch(int ac, const char **av, char **envp)
+{
+       struct commit *rev[MAX_REVS], *commit;
+       struct commit_list *list = NULL, *seen = NULL;
+       unsigned int rev_mask[MAX_REVS];
+       int num_rev, i, extra = 0;
+       int all_heads = 0, all_tags = 0;
+       int all_mask, all_revs;
+       int lifo = 1;
+       char head_path[128];
+       const char *head_path_p;
+       int head_path_len;
+       unsigned char head_sha1[20];
+       int merge_base = 0;
+       int independent = 0;
+       int no_name = 0;
+       int sha1_name = 0;
+       int shown_merge_point = 0;
+       int with_current_branch = 0;
+       int head_at = -1;
+       int topics = 0;
+       int dense = 1;
+
+       setup_git_directory();
+       git_config(git_show_branch_config);
+
+       /* If nothing is specified, try the default first */
+       if (ac == 1 && default_num) {
+               ac = default_num + 1;
+               av = default_arg - 1; /* ick; we would not address av[0] */
+       }
+
+       while (1 < ac && av[1][0] == '-') {
+               const char *arg = av[1];
+               if (!strcmp(arg, "--")) {
+                       ac--; av++;
+                       break;
+               }
+               else if (!strcmp(arg, "--all"))
+                       all_heads = all_tags = 1;
+               else if (!strcmp(arg, "--heads"))
+                       all_heads = 1;
+               else if (!strcmp(arg, "--tags"))
+                       all_tags = 1;
+               else if (!strcmp(arg, "--more"))
+                       extra = 1;
+               else if (!strcmp(arg, "--list"))
+                       extra = -1;
+               else if (!strcmp(arg, "--no-name"))
+                       no_name = 1;
+               else if (!strcmp(arg, "--current"))
+                       with_current_branch = 1;
+               else if (!strcmp(arg, "--sha1-name"))
+                       sha1_name = 1;
+               else if (!strncmp(arg, "--more=", 7))
+                       extra = atoi(arg + 7);
+               else if (!strcmp(arg, "--merge-base"))
+                       merge_base = 1;
+               else if (!strcmp(arg, "--independent"))
+                       independent = 1;
+               else if (!strcmp(arg, "--topo-order"))
+                       lifo = 1;
+               else if (!strcmp(arg, "--topics"))
+                       topics = 1;
+               else if (!strcmp(arg, "--sparse"))
+                       dense = 0;
+               else if (!strcmp(arg, "--date-order"))
+                       lifo = 0;
+               else
+                       usage(show_branch_usage);
+               ac--; av++;
+       }
+       ac--; av++;
+
+       /* Only one of these is allowed */
+       if (1 < independent + merge_base + (extra != 0))
+               usage(show_branch_usage);
+
+       /* If nothing is specified, show all branches by default */
+       if (ac + all_heads + all_tags == 0)
+               all_heads = 1;
+
+       if (all_heads + all_tags)
+               snarf_refs(all_heads, all_tags);
+       while (0 < ac) {
+               append_one_rev(*av);
+               ac--; av++;
+       }
+
+       head_path_p = resolve_ref(git_path("HEAD"), head_sha1, 1);
+       if (head_path_p) {
+               head_path_len = strlen(head_path_p);
+               memcpy(head_path, head_path_p, head_path_len + 1);
+       }
+       else {
+               head_path_len = 0;
+               head_path[0] = 0;
+       }
+
+       if (with_current_branch && head_path_p) {
+               int has_head = 0;
+               for (i = 0; !has_head && i < ref_name_cnt; i++) {
+                       /* We are only interested in adding the branch
+                        * HEAD points at.
+                        */
+                       if (rev_is_head(head_path,
+                                       head_path_len,
+                                       ref_name[i],
+                                       head_sha1, NULL))
+                               has_head++;
+               }
+               if (!has_head) {
+                       int pfxlen = strlen(git_path("refs/heads/"));
+                       append_one_rev(head_path + pfxlen);
+               }
+       }
+
+       if (!ref_name_cnt) {
+               fprintf(stderr, "No revs to be shown.\n");
+               exit(0);
+       }
+
+       for (num_rev = 0; ref_name[num_rev]; num_rev++) {
+               unsigned char revkey[20];
+               unsigned int flag = 1u << (num_rev + REV_SHIFT);
+
+               if (MAX_REVS <= num_rev)
+                       die("cannot handle more than %d revs.", MAX_REVS);
+               if (get_sha1(ref_name[num_rev], revkey))
+                       die("'%s' is not a valid ref.", ref_name[num_rev]);
+               commit = lookup_commit_reference(revkey);
+               if (!commit)
+                       die("cannot find commit %s (%s)",
+                           ref_name[num_rev], revkey);
+               parse_commit(commit);
+               mark_seen(commit, &seen);
+
+               /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
+                * and so on.  REV_SHIFT bits from bit 0 are used for
+                * internal bookkeeping.
+                */
+               commit->object.flags |= flag;
+               if (commit->object.flags == flag)
+                       insert_by_date(commit, &list);
+               rev[num_rev] = commit;
+       }
+       for (i = 0; i < num_rev; i++)
+               rev_mask[i] = rev[i]->object.flags;
+
+       if (0 <= extra)
+               join_revs(&list, &seen, num_rev, extra);
+
+       if (merge_base)
+               return show_merge_base(seen, num_rev);
+
+       if (independent)
+               return show_independent(rev, num_rev, ref_name, rev_mask);
+
+       /* Show list; --more=-1 means list-only */
+       if (1 < num_rev || extra < 0) {
+               for (i = 0; i < num_rev; i++) {
+                       int j;
+                       int is_head = rev_is_head(head_path,
+                                                 head_path_len,
+                                                 ref_name[i],
+                                                 head_sha1,
+                                                 rev[i]->object.sha1);
+                       if (extra < 0)
+                               printf("%c [%s] ",
+                                      is_head ? '*' : ' ', ref_name[i]);
+                       else {
+                               for (j = 0; j < i; j++)
+                                       putchar(' ');
+                               printf("%c [%s] ",
+                                      is_head ? '*' : '!', ref_name[i]);
+                       }
+                       /* header lines never need name */
+                       show_one_commit(rev[i], 1);
+                       if (is_head)
+                               head_at = i;
+               }
+               if (0 <= extra) {
+                       for (i = 0; i < num_rev; i++)
+                               putchar('-');
+                       putchar('\n');
+               }
+       }
+       if (extra < 0)
+               exit(0);
+
+       /* Sort topologically */
+       sort_in_topological_order(&seen, lifo);
+
+       /* Give names to commits */
+       if (!sha1_name && !no_name)
+               name_commits(seen, rev, ref_name, num_rev);
+
+       all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
+       all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
+
+       while (seen) {
+               struct commit *commit = pop_one_commit(&seen);
+               int this_flag = commit->object.flags;
+               int is_merge_point = ((this_flag & all_revs) == all_revs);
+
+               shown_merge_point |= is_merge_point;
+
+               if (1 < num_rev) {
+                       int is_merge = !!(commit->parents &&
+                                         commit->parents->next);
+                       if (topics &&
+                           !is_merge_point &&
+                           (this_flag & (1u << REV_SHIFT)))
+                               continue;
+                       if (dense && is_merge &&
+                           omit_in_dense(commit, rev, num_rev))
+                               continue;
+                       for (i = 0; i < num_rev; i++) {
+                               int mark;
+                               if (!(this_flag & (1u << (i + REV_SHIFT))))
+                                       mark = ' ';
+                               else if (is_merge)
+                                       mark = '-';
+                               else if (i == head_at)
+                                       mark = '*';
+                               else
+                                       mark = '+';
+                               putchar(mark);
+                       }
+                       putchar(' ');
+               }
+               show_one_commit(commit, no_name);
+
+               if (shown_merge_point && --extra < 0)
+                       break;
+       }
+       return 0;
+}
diff --git a/builtin-tar-tree.c b/builtin-tar-tree.c
new file mode 100644 (file)
index 0000000..f6310b9
--- /dev/null
@@ -0,0 +1,429 @@
+/*
+ * Copyright (c) 2005, 2006 Rene Scharfe
+ */
+#include <time.h>
+#include "cache.h"
+#include "tree-walk.h"
+#include "commit.h"
+#include "strbuf.h"
+#include "tar.h"
+#include "builtin.h"
+#include "pkt-line.h"
+
+#define RECORDSIZE     (512)
+#define BLOCKSIZE      (RECORDSIZE * 20)
+
+static const char tar_tree_usage[] =
+"git-tar-tree [--remote=<repo>] <ent> [basedir]";
+
+static char block[BLOCKSIZE];
+static unsigned long offset;
+
+static time_t archive_time;
+
+/* tries hard to write, either succeeds or dies in the attempt */
+static void reliable_write(void *buf, unsigned long size)
+{
+       while (size > 0) {
+               long ret = xwrite(1, buf, size);
+               if (ret < 0) {
+                       if (errno == EPIPE)
+                               exit(0);
+                       die("git-tar-tree: %s", strerror(errno));
+               } else if (!ret) {
+                       die("git-tar-tree: disk full?");
+               }
+               size -= ret;
+               buf += ret;
+       }
+}
+
+/* writes out the whole block, but only if it is full */
+static void write_if_needed(void)
+{
+       if (offset == BLOCKSIZE) {
+               reliable_write(block, BLOCKSIZE);
+               offset = 0;
+       }
+}
+
+/* acquire the next record from the buffer; user must call write_if_needed() */
+static char *get_record(void)
+{
+       char *p = block + offset;
+       memset(p, 0, RECORDSIZE);
+       offset += RECORDSIZE;
+       return p;
+}
+
+/*
+ * The end of tar archives is marked by 1024 nul bytes and after that
+ * follows the rest of the block (if any).
+ */
+static void write_trailer(void)
+{
+       get_record();
+       write_if_needed();
+       get_record();
+       write_if_needed();
+       while (offset) {
+               get_record();
+               write_if_needed();
+       }
+}
+
+/*
+ * queues up writes, so that all our write(2) calls write exactly one
+ * full block; pads writes to RECORDSIZE
+ */
+static void write_blocked(void *buf, unsigned long size)
+{
+       unsigned long tail;
+
+       if (offset) {
+               unsigned long chunk = BLOCKSIZE - offset;
+               if (size < chunk)
+                       chunk = size;
+               memcpy(block + offset, buf, chunk);
+               size -= chunk;
+               offset += chunk;
+               buf += chunk;
+               write_if_needed();
+       }
+       while (size >= BLOCKSIZE) {
+               reliable_write(buf, BLOCKSIZE);
+               size -= BLOCKSIZE;
+               buf += BLOCKSIZE;
+       }
+       if (size) {
+               memcpy(block + offset, buf, size);
+               offset += size;
+       }
+       tail = offset % RECORDSIZE;
+       if (tail)  {
+               memset(block + offset, 0, RECORDSIZE - tail);
+               offset += RECORDSIZE - tail;
+       }
+       write_if_needed();
+}
+
+static void strbuf_append_string(struct strbuf *sb, const char *s)
+{
+       int slen = strlen(s);
+       int total = sb->len + slen;
+       if (total > sb->alloc) {
+               sb->buf = xrealloc(sb->buf, total);
+               sb->alloc = total;
+       }
+       memcpy(sb->buf + sb->len, s, slen);
+       sb->len = total;
+}
+
+/*
+ * pax extended header records have the format "%u %s=%s\n".  %u contains
+ * the size of the whole string (including the %u), the first %s is the
+ * keyword, the second one is the value.  This function constructs such a
+ * string and appends it to a struct strbuf.
+ */
+static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
+                                     const char *value, unsigned int valuelen)
+{
+       char *p;
+       int len, total, tmp;
+
+       /* "%u %s=%s\n" */
+       len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
+       for (tmp = len; tmp > 9; tmp /= 10)
+               len++;
+
+       total = sb->len + len;
+       if (total > sb->alloc) {
+               sb->buf = xrealloc(sb->buf, total);
+               sb->alloc = total;
+       }
+
+       p = sb->buf;
+       p += sprintf(p, "%u %s=", len, keyword);
+       memcpy(p, value, valuelen);
+       p += valuelen;
+       *p = '\n';
+       sb->len = total;
+}
+
+static unsigned int ustar_header_chksum(const struct ustar_header *header)
+{
+       char *p = (char *)header;
+       unsigned int chksum = 0;
+       while (p < header->chksum)
+               chksum += *p++;
+       chksum += sizeof(header->chksum) * ' ';
+       p += sizeof(header->chksum);
+       while (p < (char *)header + sizeof(struct ustar_header))
+               chksum += *p++;
+       return chksum;
+}
+
+static int get_path_prefix(const struct strbuf *path, int maxlen)
+{
+       int i = path->len;
+       if (i > maxlen)
+               i = maxlen;
+       do {
+               i--;
+       } while (i > 0 && path->buf[i] != '/');
+       return i;
+}
+
+static void write_entry(const unsigned char *sha1, struct strbuf *path,
+                        unsigned int mode, void *buffer, unsigned long size)
+{
+       struct ustar_header header;
+       struct strbuf ext_header;
+
+       memset(&header, 0, sizeof(header));
+       ext_header.buf = NULL;
+       ext_header.len = ext_header.alloc = 0;
+
+       if (!sha1) {
+               *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
+               mode = 0100666;
+               strcpy(header.name, "pax_global_header");
+       } else if (!path) {
+               *header.typeflag = TYPEFLAG_EXT_HEADER;
+               mode = 0100666;
+               sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
+       } else {
+               if (S_ISDIR(mode)) {
+                       *header.typeflag = TYPEFLAG_DIR;
+                       mode |= 0777;
+               } else if (S_ISLNK(mode)) {
+                       *header.typeflag = TYPEFLAG_LNK;
+                       mode |= 0777;
+               } else if (S_ISREG(mode)) {
+                       *header.typeflag = TYPEFLAG_REG;
+                       mode |= (mode & 0100) ? 0777 : 0666;
+               } else {
+                       error("unsupported file mode: 0%o (SHA1: %s)",
+                             mode, sha1_to_hex(sha1));
+                       return;
+               }
+               if (path->len > sizeof(header.name)) {
+                       int plen = get_path_prefix(path, sizeof(header.prefix));
+                       int rest = path->len - plen - 1;
+                       if (plen > 0 && rest <= sizeof(header.name)) {
+                               memcpy(header.prefix, path->buf, plen);
+                               memcpy(header.name, path->buf + plen + 1, rest);
+                       } else {
+                               sprintf(header.name, "%s.data",
+                                       sha1_to_hex(sha1));
+                               strbuf_append_ext_header(&ext_header, "path",
+                                                        path->buf, path->len);
+                       }
+               } else
+                       memcpy(header.name, path->buf, path->len);
+       }
+
+       if (S_ISLNK(mode) && buffer) {
+               if (size > sizeof(header.linkname)) {
+                       sprintf(header.linkname, "see %s.paxheader",
+                               sha1_to_hex(sha1));
+                       strbuf_append_ext_header(&ext_header, "linkpath",
+                                                buffer, size);
+               } else
+                       memcpy(header.linkname, buffer, size);
+       }
+
+       sprintf(header.mode, "%07o", mode & 07777);
+       sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
+       sprintf(header.mtime, "%011lo", archive_time);
+
+       /* XXX: should we provide more meaningful info here? */
+       sprintf(header.uid, "%07o", 0);
+       sprintf(header.gid, "%07o", 0);
+       safe_strncpy(header.uname, "git", sizeof(header.uname));
+       safe_strncpy(header.gname, "git", sizeof(header.gname));
+       sprintf(header.devmajor, "%07o", 0);
+       sprintf(header.devminor, "%07o", 0);
+
+       memcpy(header.magic, "ustar", 6);
+       memcpy(header.version, "00", 2);
+
+       sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
+
+       if (ext_header.len > 0) {
+               write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
+               free(ext_header.buf);
+       }
+       write_blocked(&header, sizeof(header));
+       if (S_ISREG(mode) && buffer && size > 0)
+               write_blocked(buffer, size);
+}
+
+static void write_global_extended_header(const unsigned char *sha1)
+{
+       struct strbuf ext_header;
+       ext_header.buf = NULL;
+       ext_header.len = ext_header.alloc = 0;
+       strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
+       write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
+       free(ext_header.buf);
+}
+
+static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
+{
+       int pathlen = path->len;
+       struct name_entry entry;
+
+       while (tree_entry(tree, &entry)) {
+               void *eltbuf;
+               char elttype[20];
+               unsigned long eltsize;
+
+               eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
+               if (!eltbuf)
+                       die("cannot read %s", sha1_to_hex(entry.sha1));
+
+               path->len = pathlen;
+               strbuf_append_string(path, entry.path);
+               if (S_ISDIR(entry.mode))
+                       strbuf_append_string(path, "/");
+
+               write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
+
+               if (S_ISDIR(entry.mode)) {
+                       struct tree_desc subtree;
+                       subtree.buf = eltbuf;
+                       subtree.size = eltsize;
+                       traverse_tree(&subtree, path);
+               }
+               free(eltbuf);
+       }
+}
+
+static int generate_tar(int argc, const char **argv, char** envp)
+{
+       unsigned char sha1[20], tree_sha1[20];
+       struct commit *commit;
+       struct tree_desc tree;
+       struct strbuf current_path;
+
+       current_path.buf = xmalloc(PATH_MAX);
+       current_path.alloc = PATH_MAX;
+       current_path.len = current_path.eof = 0;
+
+       setup_git_directory();
+       git_config(git_default_config);
+
+       switch (argc) {
+       case 3:
+               strbuf_append_string(&current_path, argv[2]);
+               strbuf_append_string(&current_path, "/");
+               /* FALLTHROUGH */
+       case 2:
+               if (get_sha1(argv[1], sha1))
+                       die("Not a valid object name %s", argv[1]);
+               break;
+       default:
+               usage(tar_tree_usage);
+       }
+
+       commit = lookup_commit_reference_gently(sha1, 1);
+       if (commit) {
+               write_global_extended_header(commit->object.sha1);
+               archive_time = commit->date;
+       } else
+               archive_time = time(NULL);
+
+       tree.buf = read_object_with_reference(sha1, tree_type, &tree.size,
+                                             tree_sha1);
+       if (!tree.buf)
+               die("not a reference to a tag, commit or tree object: %s",
+                   sha1_to_hex(sha1));
+
+       if (current_path.len > 0)
+               write_entry(tree_sha1, &current_path, 040777, NULL, 0);
+       traverse_tree(&tree, &current_path);
+       write_trailer();
+       free(current_path.buf);
+       return 0;
+}
+
+static const char *exec = "git-upload-tar";
+
+static int remote_tar(int argc, const char **argv)
+{
+       int fd[2], ret, len;
+       pid_t pid;
+       char buf[1024];
+       char *url;
+
+       if (argc < 3 || 4 < argc)
+               usage(tar_tree_usage);
+
+       /* --remote=<repo> */
+       url = strdup(argv[1]+9);
+       pid = git_connect(fd, url, exec);
+       if (pid < 0)
+               return 1;
+
+       packet_write(fd[1], "want %s\n", argv[2]);
+       if (argv[3])
+               packet_write(fd[1], "base %s\n", argv[3]);
+       packet_flush(fd[1]);
+
+       len = packet_read_line(fd[0], buf, sizeof(buf));
+       if (!len)
+               die("git-tar-tree: expected ACK/NAK, got EOF");
+       if (buf[len-1] == '\n')
+               buf[--len] = 0;
+       if (strcmp(buf, "ACK")) {
+               if (5 < len && !strncmp(buf, "NACK ", 5))
+                       die("git-tar-tree: NACK %s", buf + 5);
+               die("git-tar-tree: protocol error");
+       }
+       /* expect a flush */
+       len = packet_read_line(fd[0], buf, sizeof(buf));
+       if (len)
+               die("git-tar-tree: expected a flush");
+
+       /* Now, start reading from fd[0] and spit it out to stdout */
+       ret = copy_fd(fd[0], 1);
+       close(fd[0]);
+
+       ret |= finish_connect(pid);
+       return !!ret;
+}
+
+int cmd_tar_tree(int argc, const char **argv, char **envp)
+{
+       if (argc < 2)
+               usage(tar_tree_usage);
+       if (!strncmp("--remote=", argv[1], 9))
+               return remote_tar(argc, argv);
+       return generate_tar(argc, argv, envp);
+}
+
+/* ustar header + extended global header content */
+#define HEADERSIZE (2 * RECORDSIZE)
+
+int cmd_get_tar_commit_id(int argc, const char **argv, char **envp)
+{
+       char buffer[HEADERSIZE];
+       struct ustar_header *header = (struct ustar_header *)buffer;
+       char *content = buffer + RECORDSIZE;
+       ssize_t n;
+
+       n = xread(0, buffer, HEADERSIZE);
+       if (n < HEADERSIZE)
+               die("git-get-tar-commit-id: read error");
+       if (header->typeflag[0] != 'g')
+               return 1;
+       if (memcmp(content, "52 comment=", 11))
+               return 1;
+
+       n = xwrite(1, content + 11, 41);
+       if (n < 41)
+               die("git-get-tar-commit-id: write error");
+
+       return 0;
+}
diff --git a/builtin-upload-tar.c b/builtin-upload-tar.c
new file mode 100644 (file)
index 0000000..d4fa7b5
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2006 Junio C Hamano
+ */
+#include "cache.h"
+#include "pkt-line.h"
+#include "exec_cmd.h"
+#include "builtin.h"
+
+static const char upload_tar_usage[] = "git-upload-tar <repo>";
+
+static int nak(const char *reason)
+{
+       packet_write(1, "NACK %s\n", reason);
+       packet_flush(1);
+       return 1;
+}
+
+int cmd_upload_tar(int argc, const char **argv, char **envp)
+{
+       int len;
+       const char *dir = argv[1];
+       char buf[8192];
+       unsigned char sha1[20];
+       char *base = NULL;
+       char hex[41];
+       int ac;
+       const char *av[4];
+
+       if (argc != 2)
+               usage(upload_tar_usage);
+       if (strlen(dir) < sizeof(buf)-1)
+               strcpy(buf, dir); /* enter-repo smudges its argument */
+       else
+               packet_write(1, "NACK insanely long repository name %s\n", dir);
+       if (!enter_repo(buf, 0)) {
+               packet_write(1, "NACK not a git archive %s\n", dir);
+               packet_flush(1);
+               return 1;
+       }
+
+       len = packet_read_line(0, buf, sizeof(buf));
+       if (len < 5 || strncmp("want ", buf, 5))
+               return nak("expected want");
+       if (buf[len-1] == '\n')
+               buf[--len] = 0;
+       if (get_sha1(buf + 5, sha1))
+               return nak("expected sha1");
+        strcpy(hex, sha1_to_hex(sha1));
+
+       len = packet_read_line(0, buf, sizeof(buf));
+       if (len) {
+               if (len < 5 || strncmp("base ", buf, 5))
+                       return nak("expected (optional) base");
+               if (buf[len-1] == '\n')
+                       buf[--len] = 0;
+               base = strdup(buf + 5);
+               len = packet_read_line(0, buf, sizeof(buf));
+       }
+       if (len)
+               return nak("expected flush");
+
+       packet_write(1, "ACK\n");
+       packet_flush(1);
+
+       ac = 0;
+       av[ac++] = "tar-tree";
+       av[ac++] = hex;
+       if (base)
+               av[ac++] = base;
+       av[ac++] = NULL;
+       execv_git_cmd(av);
+       /* should it return that is an error */
+       return 1;
+}
index 47408a0..b9f36be 100644 (file)
--- a/builtin.h
+++ b/builtin.h
@@ -19,5 +19,31 @@ extern int cmd_version(int argc, const char **argv, char **envp);
 extern int cmd_whatchanged(int argc, const char **argv, char **envp);
 extern int cmd_show(int argc, const char **argv, char **envp);
 extern int cmd_log(int argc, const char **argv, char **envp);
+extern int cmd_diff(int argc, const char **argv, char **envp);
+extern int cmd_format_patch(int argc, const char **argv, char **envp);
+extern int cmd_count_objects(int argc, const char **argv, char **envp);
+
+extern int cmd_push(int argc, const char **argv, char **envp);
+extern int cmd_grep(int argc, const char **argv, char **envp);
+extern int cmd_rm(int argc, const char **argv, char **envp);
+extern int cmd_add(int argc, const char **argv, char **envp);
+extern int cmd_rev_list(int argc, const char **argv, char **envp);
+extern int cmd_check_ref_format(int argc, const char **argv, char **envp);
+extern int cmd_init_db(int argc, const char **argv, char **envp);
+extern int cmd_tar_tree(int argc, const char **argv, char **envp);
+extern int cmd_upload_tar(int argc, const char **argv, char **envp);
+extern int cmd_get_tar_commit_id(int argc, const char **argv, char **envp);
+extern int cmd_ls_files(int argc, const char **argv, char **envp);
+extern int cmd_ls_tree(int argc, const char **argv, char **envp);
+extern int cmd_read_tree(int argc, const char **argv, char **envp);
+extern int cmd_commit_tree(int argc, const char **argv, char **envp);
+extern int cmd_apply(int argc, const char **argv, char **envp);
+extern int cmd_show_branch(int argc, const char **argv, char **envp);
+extern int cmd_diff_files(int argc, const char **argv, char **envp);
+extern int cmd_diff_index(int argc, const char **argv, char **envp);
+extern int cmd_diff_stages(int argc, const char **argv, char **envp);
+extern int cmd_diff_tree(int argc, const char **argv, char **envp);
+extern int cmd_cat_file(int argc, const char **argv, char **envp);
+extern int cmd_rev_parse(int argc, const char **argv, char **envp);
 
 #endif
index dae4399..d9f7e1e 100644 (file)
@@ -110,6 +110,10 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
        int namelen;
        struct cache_tree_sub *down;
 
+#if DEBUG
+       fprintf(stderr, "cache-tree invalidate <%s>\n", path);
+#endif
+
        if (!it)
                return;
        slash = strchr(path, '/');
@@ -335,7 +339,7 @@ static int update_one(struct cache_tree *it,
                offset += 20;
 
 #if DEBUG
-               fprintf(stderr, "cache-tree %o %.*s\n",
+               fprintf(stderr, "cache-tree update-one %o %.*s\n",
                        mode, entlen, path + baselen);
 #endif
        }
@@ -351,7 +355,7 @@ static int update_one(struct cache_tree *it,
        free(buffer);
        it->entry_count = i;
 #if DEBUG
-       fprintf(stderr, "cache-tree (%d ent, %d subtree) %s\n",
+       fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
                it->entry_count, it->subtree_nr,
                sha1_to_hex(it->sha1));
 #endif
diff --git a/cache.h b/cache.h
index 00b8804..f630cf4 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -136,12 +136,14 @@ extern const char *setup_git_directory(void);
 extern const char *prefix_path(const char *prefix, int len, const char *path);
 extern const char *prefix_filename(const char *prefix, int len, const char *path);
 extern void verify_filename(const char *prefix, const char *name);
+extern void verify_non_filename(const char *prefix, const char *name);
 
 #define alloc_nr(x) (((x)+16)*3/2)
 
 /* Initialize and use the cache information */
 extern int read_cache(void);
 extern int write_cache(int newfd, struct cache_entry **cache, int entries);
+extern int verify_path(const char *path);
 extern int cache_name_pos(const char *name, int namelen);
 #define ADD_CACHE_OK_TO_ADD 1          /* Ok to add */
 #define ADD_CACHE_OK_TO_REPLACE 2      /* Ok to replace file/directory */
@@ -154,22 +156,30 @@ extern int ce_match_stat(struct cache_entry *ce, struct stat *st, int);
 extern int ce_modified(struct cache_entry *ce, struct stat *st, int);
 extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
 extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type);
+extern int read_pipe(int fd, char** return_buf, unsigned long* return_size);
 extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
 extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
 
-struct cache_file {
-       struct cache_file *next;
-       char lockfile[PATH_MAX];
+#define REFRESH_REALLY         0x0001  /* ignore_valid */
+#define REFRESH_UNMERGED       0x0002  /* allow unmerged */
+#define REFRESH_QUIET          0x0004  /* be quiet about it */
+#define REFRESH_IGNORE_MISSING 0x0008  /* ignore non-existent */
+extern int refresh_cache(unsigned int flags);
+
+struct lock_file {
+       struct lock_file *next;
+       char filename[PATH_MAX];
 };
-extern int hold_index_file_for_update(struct cache_file *, const char *path);
-extern int commit_index_file(struct cache_file *);
-extern void rollback_index_file(struct cache_file *);
+extern int hold_lock_file_for_update(struct lock_file *, const char *path);
+extern int commit_lock_file(struct lock_file *);
+extern void rollback_lock_file(struct lock_file *);
 
 /* Environment bits from configuration mechanism */
 extern int trust_executable_bit;
 extern int assume_unchanged;
-extern int only_use_symrefs;
+extern int prefer_symlink_refs;
+extern int log_all_ref_updates;
 extern int warn_ambiguous_refs;
 extern int diff_rename_limit_default;
 extern int shared_repository;
@@ -200,7 +210,7 @@ int git_mkstemp(char *path, size_t n, const char *template);
 
 int adjust_shared_perm(const char *path);
 int safe_create_leading_directories(char *path);
-char *safe_strncpy(char *, const char *, size_t);
+size_t safe_strncpy(char *, const char *, size_t);
 char *enter_repo(char *path, int strict);
 
 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
@@ -251,6 +261,7 @@ extern void *read_object_with_reference(const unsigned char *sha1,
                                        unsigned char *sha1_ret);
 
 const char *show_date(unsigned long time, int timezone);
+const char *show_rfc2822_date(unsigned long time, int timezone);
 int parse_date(const char *date, char *buf, int bufsize);
 void datestamp(char *buf, int bufsize);
 unsigned long approxidate(const char *);
@@ -363,4 +374,8 @@ extern int receive_keep_pack(int fd[2], const char *me, int quiet);
 /* pager.c */
 extern void setup_pager(void);
 
+/* base85 */
+int decode_85(char *dst, char *line, int linelen);
+void encode_85(char *buf, unsigned char *data, int bytes);
+
 #endif /* CACHE_H */
diff --git a/cat-file.c b/cat-file.c
deleted file mode 100644 (file)
index 628f6ca..0000000
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * GIT - The information manager from hell
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#include "cache.h"
-#include "exec_cmd.h"
-#include "tag.h"
-#include "tree.h"
-
-static void flush_buffer(const char *buf, unsigned long size)
-{
-       while (size > 0) {
-               long ret = xwrite(1, buf, size);
-               if (ret < 0) {
-                       /* Ignore epipe */
-                       if (errno == EPIPE)
-                               break;
-                       die("git-cat-file: %s", strerror(errno));
-               } else if (!ret) {
-                       die("git-cat-file: disk full?");
-               }
-               size -= ret;
-               buf += ret;
-       }
-}
-
-static int pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
-{
-       /* the parser in tag.c is useless here. */
-       const char *endp = buf + size;
-       const char *cp = buf;
-
-       while (cp < endp) {
-               char c = *cp++;
-               if (c != '\n')
-                       continue;
-               if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
-                       const char *tagger = cp;
-
-                       /* Found the tagger line.  Copy out the contents
-                        * of the buffer so far.
-                        */
-                       flush_buffer(buf, cp - buf);
-
-                       /*
-                        * Do something intelligent, like pretty-printing
-                        * the date.
-                        */
-                       while (cp < endp) {
-                               if (*cp++ == '\n') {
-                                       /* tagger to cp is a line
-                                        * that has ident and time.
-                                        */
-                                       const char *sp = tagger;
-                                       char *ep;
-                                       unsigned long date;
-                                       long tz;
-                                       while (sp < cp && *sp != '>')
-                                               sp++;
-                                       if (sp == cp) {
-                                               /* give up */
-                                               flush_buffer(tagger,
-                                                            cp - tagger);
-                                               break;
-                                       }
-                                       while (sp < cp &&
-                                              !('0' <= *sp && *sp <= '9'))
-                                               sp++;
-                                       flush_buffer(tagger, sp - tagger);
-                                       date = strtoul(sp, &ep, 10);
-                                       tz = strtol(ep, NULL, 10);
-                                       sp = show_date(date, tz);
-                                       flush_buffer(sp, strlen(sp));
-                                       xwrite(1, "\n", 1);
-                                       break;
-                               }
-                       }
-                       break;
-               }
-               if (cp < endp && *cp == '\n')
-                       /* end of header */
-                       break;
-       }
-       /* At this point, we have copied out the header up to the end of
-        * the tagger line and cp points at one past \n.  It could be the
-        * next header line after the tagger line, or it could be another
-        * \n that marks the end of the headers.  We need to copy out the
-        * remainder as is.
-        */
-       if (cp < endp)
-               flush_buffer(cp, endp - cp);
-       return 0;
-}
-
-int main(int argc, char **argv)
-{
-       unsigned char sha1[20];
-       char type[20];
-       void *buf;
-       unsigned long size;
-       int opt;
-
-       setup_git_directory();
-       git_config(git_default_config);
-       if (argc != 3 || get_sha1(argv[2], sha1))
-               usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
-
-       opt = 0;
-       if ( argv[1][0] == '-' ) {
-               opt = argv[1][1];
-               if ( !opt || argv[1][2] )
-                       opt = -1; /* Not a single character option */
-       }
-
-       buf = NULL;
-       switch (opt) {
-       case 't':
-               if (!sha1_object_info(sha1, type, NULL)) {
-                       printf("%s\n", type);
-                       return 0;
-               }
-               break;
-
-       case 's':
-               if (!sha1_object_info(sha1, type, &size)) {
-                       printf("%lu\n", size);
-                       return 0;
-               }
-               break;
-
-       case 'e':
-               return !has_sha1_file(sha1);
-
-       case 'p':
-               if (get_sha1(argv[2], sha1) ||
-                   sha1_object_info(sha1, type, NULL))
-                       die("Not a valid object name %s", argv[2]);
-
-               /* custom pretty-print here */
-               if (!strcmp(type, tree_type))
-                       return execl_git_cmd("ls-tree", argv[2], NULL);
-
-               buf = read_sha1_file(sha1, type, &size);
-               if (!buf)
-                       die("Cannot read object %s", argv[2]);
-               if (!strcmp(type, tag_type))
-                       return pprint_tag(sha1, buf, size);
-
-               /* otherwise just spit out the data */
-               break;
-       case 0:
-               buf = read_object_with_reference(sha1, argv[1], &size, NULL);
-               break;
-
-       default:
-               die("git-cat-file: unknown option: %s\n", argv[1]);
-       }
-
-       if (!buf)
-               die("git-cat-file %s: bad file", argv[2]);
-
-       flush_buffer(buf, size);
-       return 0;
-}
diff --git a/check-ref-format.c b/check-ref-format.c
deleted file mode 100644 (file)
index a0adb3d..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * GIT - The information manager from hell
- */
-
-#include "cache.h"
-#include "refs.h"
-
-#include <stdio.h>
-
-int main(int ac, char **av)
-{
-       if (ac != 2)
-               usage("git-check-ref-format refname");
-       if (check_ref_format(av[1]))
-               exit(1);
-       return 0;
-}
index e56c354..ea40bc2 100644 (file)
@@ -168,7 +168,7 @@ static int checkout_all(void)
 static const char checkout_cache_usage[] =
 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 int main(int argc, char **argv)
 {
@@ -211,9 +211,8 @@ int main(int argc, char **argv)
                if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
                        state.refresh_cache = 1;
                        if (newfd < 0)
-                               newfd = hold_index_file_for_update
-                                       (&cache_file,
-                                        get_index_file());
+                               newfd = hold_lock_file_for_update
+                                       (&lock_file, get_index_file());
                        if (newfd < 0)
                                die("cannot open index.lock file.");
                        continue;
@@ -262,7 +261,7 @@ int main(int argc, char **argv)
                 */
                if (state.refresh_cache) {
                        close(newfd); newfd = -1;
-                       rollback_index_file(&cache_file);
+                       rollback_lock_file(&lock_file);
                }
                state.refresh_cache = 0;
        }
@@ -270,12 +269,16 @@ int main(int argc, char **argv)
        /* Check out named files first */
        for ( ; i < argc; i++) {
                const char *arg = argv[i];
+               const char *p;
 
                if (all)
                        die("git-checkout-index: don't mix '--all' and explicit filenames");
                if (read_from_stdin)
                        die("git-checkout-index: don't mix '--stdin' and explicit filenames");
-               checkout_file(prefix_path(prefix, prefix_length, arg));
+               p = prefix_path(prefix, prefix_length, arg);
+               checkout_file(p);
+               if (p < arg || p > arg + strlen(arg))
+                       free((char*)p);
        }
 
        if (read_from_stdin) {
@@ -285,6 +288,8 @@ int main(int argc, char **argv)
                strbuf_init(&buf);
                while (1) {
                        char *path_name;
+                       const char *p;
+
                        read_line(&buf, stdin, line_termination);
                        if (buf.eof)
                                break;
@@ -292,7 +297,10 @@ int main(int argc, char **argv)
                                path_name = unquote_c_style(buf.buf, NULL);
                        else
                                path_name = buf.buf;
-                       checkout_file(prefix_path(prefix, prefix_length, path_name));
+                       p = prefix_path(prefix, prefix_length, path_name);
+                       checkout_file(p);
+                       if (p < path_name || p > path_name + strlen(path_name))
+                               free((char *)p);
                        if (path_name != buf.buf)
                                free(path_name);
                }
@@ -303,7 +311,7 @@ int main(int argc, char **argv)
 
        if (0 <= newfd &&
            (write_cache(newfd, active_cache, active_nr) ||
-            commit_index_file(&cache_file)))
-               die("Unable to write new cachefile");
+            commit_lock_file(&lock_file)))
+               die("Unable to write new index file");
        return 0;
 }
index ca36f5d..64b20cc 100644 (file)
@@ -608,6 +608,7 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
        int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
        mmfile_t result_file;
 
+       context = opt->context;
        /* Read the result of merge first */
        if (!working_tree_file)
                result = grab_blob(elem->sha1, &result_size);
@@ -831,15 +832,16 @@ void show_combined_diff(struct combine_diff_path *p,
        }
 }
 
-void diff_tree_combined_merge(const unsigned char *sha1,
-                            int dense, struct rev_info *rev)
+void diff_tree_combined(const unsigned char *sha1,
+                       const unsigned char parent[][20],
+                       int num_parent,
+                       int dense,
+                       struct rev_info *rev)
 {
        struct diff_options *opt = &rev->diffopt;
-       struct commit *commit = lookup_commit(sha1);
        struct diff_options diffopts;
-       struct commit_list *parents;
        struct combine_diff_path *p, *paths = NULL;
-       int num_parent, i, num_paths;
+       int i, num_paths;
        int do_diffstat;
 
        do_diffstat = (opt->output_format == DIFF_FORMAT_DIFFSTAT ||
@@ -849,17 +851,8 @@ void diff_tree_combined_merge(const unsigned char *sha1,
        diffopts.with_stat = 0;
        diffopts.recursive = 1;
 
-       /* count parents */
-       for (parents = commit->parents, num_parent = 0;
-            parents;
-            parents = parents->next, num_parent++)
-               ; /* nothing */
-
        /* find set of paths that everybody touches */
-       for (parents = commit->parents, i = 0;
-            parents;
-            parents = parents->next, i++) {
-               struct commit *parent = parents->item;
+       for (i = 0; i < num_parent; i++) {
                /* show stat against the first parent even
                 * when doing combined diff.
                 */
@@ -867,8 +860,7 @@ void diff_tree_combined_merge(const unsigned char *sha1,
                        diffopts.output_format = DIFF_FORMAT_DIFFSTAT;
                else
                        diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
-               diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
-                              &diffopts);
+               diff_tree_sha1(parent[i], sha1, "", &diffopts);
                diffcore_std(&diffopts);
                paths = intersect_paths(paths, i, num_parent);
 
@@ -907,3 +899,25 @@ void diff_tree_combined_merge(const unsigned char *sha1,
                free(tmp);
        }
 }
+
+void diff_tree_combined_merge(const unsigned char *sha1,
+                            int dense, struct rev_info *rev)
+{
+       int num_parent;
+       const unsigned char (*parent)[20];
+       struct commit *commit = lookup_commit(sha1);
+       struct commit_list *parents;
+
+       /* count parents */
+       for (parents = commit->parents, num_parent = 0;
+            parents;
+            parents = parents->next, num_parent++)
+               ; /* nothing */
+
+       parent = xmalloc(num_parent * sizeof(*parent));
+       for (parents = commit->parents, num_parent = 0;
+            parents;
+            parents = parents->next, num_parent++)
+               memcpy(parent + num_parent, parents->item->object.sha1, 20);
+       diff_tree_combined(sha1, parent, num_parent, dense, rev);
+}
diff --git a/commit-tree.c b/commit-tree.c
deleted file mode 100644 (file)
index bad72e8..0000000
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * GIT - The information manager from hell
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#include "cache.h"
-#include "commit.h"
-#include "tree.h"
-
-#define BLOCKING (1ul << 14)
-
-/*
- * FIXME! Share the code with "write-tree.c"
- */
-static void init_buffer(char **bufp, unsigned int *sizep)
-{
-       char *buf = xmalloc(BLOCKING);
-       *sizep = 0;
-       *bufp = buf;
-}
-
-static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
-{
-       char one_line[2048];
-       va_list args;
-       int len;
-       unsigned long alloc, size, newsize;
-       char *buf;
-
-       va_start(args, fmt);
-       len = vsnprintf(one_line, sizeof(one_line), fmt, args);
-       va_end(args);
-       size = *sizep;
-       newsize = size + len;
-       alloc = (size + 32767) & ~32767;
-       buf = *bufp;
-       if (newsize > alloc) {
-               alloc = (newsize + 32767) & ~32767;
-               buf = xrealloc(buf, alloc);
-               *bufp = buf;
-       }
-       *sizep = newsize;
-       memcpy(buf + size, one_line, len);
-}
-
-static void check_valid(unsigned char *sha1, const char *expect)
-{
-       char type[20];
-
-       if (sha1_object_info(sha1, type, NULL))
-               die("%s is not a valid object", sha1_to_hex(sha1));
-       if (expect && strcmp(type, expect))
-               die("%s is not a valid '%s' object", sha1_to_hex(sha1),
-                   expect);
-}
-
-/*
- * Having more than two parents is not strange at all, and this is
- * how multi-way merges are represented.
- */
-#define MAXPARENT (16)
-static unsigned char parent_sha1[MAXPARENT][20];
-
-static const char commit_tree_usage[] = "git-commit-tree <sha1> [-p <sha1>]* < changelog";
-
-static int new_parent(int idx)
-{
-       int i;
-       unsigned char *sha1 = parent_sha1[idx];
-       for (i = 0; i < idx; i++) {
-               if (!memcmp(parent_sha1[i], sha1, 20)) {
-                       error("duplicate parent %s ignored", sha1_to_hex(sha1));
-                       return 0;
-               }
-       }
-       return 1;
-}
-
-int main(int argc, char **argv)
-{
-       int i;
-       int parents = 0;
-       unsigned char tree_sha1[20];
-       unsigned char commit_sha1[20];
-       char comment[1000];
-       char *buffer;
-       unsigned int size;
-
-       setup_ident();
-       setup_git_directory();
-
-       git_config(git_default_config);
-
-       if (argc < 2 || get_sha1(argv[1], tree_sha1) < 0)
-               usage(commit_tree_usage);
-
-       check_valid(tree_sha1, tree_type);
-       for (i = 2; i < argc; i += 2) {
-               char *a, *b;
-               a = argv[i]; b = argv[i+1];
-               if (!b || strcmp(a, "-p") || get_sha1(b, parent_sha1[parents]))
-                       usage(commit_tree_usage);
-               check_valid(parent_sha1[parents], commit_type);
-               if (new_parent(parents))
-                       parents++;
-       }
-       if (!parents)
-               fprintf(stderr, "Committing initial tree %s\n", argv[1]);
-
-       init_buffer(&buffer, &size);
-       add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
-
-       /*
-        * NOTE! This ordering means that the same exact tree merged with a
-        * different order of parents will be a _different_ changeset even
-        * if everything else stays the same.
-        */
-       for (i = 0; i < parents; i++)
-               add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
-
-       /* Person/date information */
-       add_buffer(&buffer, &size, "author %s\n", git_author_info(1));
-       add_buffer(&buffer, &size, "committer %s\n\n", git_committer_info(1));
-
-       /* And add the comment */
-       while (fgets(comment, sizeof(comment), stdin) != NULL)
-               add_buffer(&buffer, &size, "%s", comment);
-
-       if (!write_sha1_file(buffer, size, commit_type, commit_sha1)) {
-               printf("%s\n", sha1_to_hex(commit_sha1));
-               return 0;
-       }
-       else
-               return 1;
-}
index 2717dd8..94f470b 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -22,23 +22,34 @@ struct sort_node
 
 const char *commit_type = "commit";
 
+struct cmt_fmt_map {
+       const char *n;
+       size_t cmp_len;
+       enum cmit_fmt v;
+} cmt_fmts[] = {
+       { "raw",        1,      CMIT_FMT_RAW },
+       { "medium",     1,      CMIT_FMT_MEDIUM },
+       { "short",      1,      CMIT_FMT_SHORT },
+       { "email",      1,      CMIT_FMT_EMAIL },
+       { "full",       5,      CMIT_FMT_FULL },
+       { "fuller",     5,      CMIT_FMT_FULLER },
+       { "oneline",    1,      CMIT_FMT_ONELINE },
+};
+
 enum cmit_fmt get_commit_format(const char *arg)
 {
-       if (!*arg)
+       int i;
+
+       if (!arg || !*arg)
                return CMIT_FMT_DEFAULT;
-       if (!strcmp(arg, "=raw"))
-               return CMIT_FMT_RAW;
-       if (!strcmp(arg, "=medium"))
-               return CMIT_FMT_MEDIUM;
-       if (!strcmp(arg, "=short"))
-               return CMIT_FMT_SHORT;
-       if (!strcmp(arg, "=full"))
-               return CMIT_FMT_FULL;
-       if (!strcmp(arg, "=fuller"))
-               return CMIT_FMT_FULLER;
-       if (!strcmp(arg, "=oneline"))
-               return CMIT_FMT_ONELINE;
-       die("invalid --pretty format");
+       if (*arg == '=')
+               arg++;
+       for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
+               if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len))
+                       return cmt_fmts[i].v;
+       }
+
+       die("invalid --pretty format: %s", arg);
 }
 
 static struct commit *check_commit(struct object *obj,
@@ -411,6 +422,46 @@ static int get_one_line(const char *msg, unsigned long len)
        return ret;
 }
 
+static int is_rfc2047_special(char ch)
+{
+       return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
+}
+
+static int add_rfc2047(char *buf, const char *line, int len)
+{
+       char *bp = buf;
+       int i, needquote;
+       static const char q_utf8[] = "=?utf-8?q?";
+
+       for (i = needquote = 0; !needquote && i < len; i++) {
+               unsigned ch = line[i];
+               if (ch & 0x80)
+                       needquote++;
+               if ((i + 1 < len) &&
+                   (ch == '=' && line[i+1] == '?'))
+                       needquote++;
+       }
+       if (!needquote)
+               return sprintf(buf, "%.*s", len, line);
+
+       memcpy(bp, q_utf8, sizeof(q_utf8)-1);
+       bp += sizeof(q_utf8)-1;
+       for (i = 0; i < len; i++) {
+               unsigned ch = line[i];
+               if (is_rfc2047_special(ch)) {
+                       sprintf(bp, "=%02X", ch);
+                       bp += 3;
+               }
+               else if (ch == ' ')
+                       *bp++ = '_';
+               else
+                       *bp++ = ch;
+       }
+       memcpy(bp, "?=", 2);
+       bp += 2;
+       return bp - buf;
+}
+
 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
 {
        char *date;
@@ -428,13 +479,35 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const c
        time = strtoul(date, &date, 10);
        tz = strtol(date, NULL, 10);
 
-       ret = sprintf(buf, "%s: %.*s%.*s\n", what,
-                     (fmt == CMIT_FMT_FULLER) ? 4 : 0,
-                     filler, namelen, line);
+       if (fmt == CMIT_FMT_EMAIL) {
+               char *name_tail = strchr(line, '<');
+               int display_name_length;
+               if (!name_tail)
+                       return 0;
+               while (line < name_tail && isspace(name_tail[-1]))
+                       name_tail--;
+               display_name_length = name_tail - line;
+               filler = "";
+               strcpy(buf, "From: ");
+               ret = strlen(buf);
+               ret += add_rfc2047(buf + ret, line, display_name_length);
+               memcpy(buf + ret, name_tail, namelen - display_name_length);
+               ret += namelen - display_name_length;
+               buf[ret++] = '\n';
+       }
+       else {
+               ret = sprintf(buf, "%s: %.*s%.*s\n", what,
+                             (fmt == CMIT_FMT_FULLER) ? 4 : 0,
+                             filler, namelen, line);
+       }
        switch (fmt) {
        case CMIT_FMT_MEDIUM:
                ret += sprintf(buf + ret, "Date:   %s\n", show_date(time, tz));
                break;
+       case CMIT_FMT_EMAIL:
+               ret += sprintf(buf + ret, "Date: %s\n",
+                              show_rfc2822_date(time, tz));
+               break;
        case CMIT_FMT_FULLER:
                ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
                break;
@@ -445,10 +518,12 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const c
        return ret;
 }
 
-static int is_empty_line(const char *line, int len)
+static int is_empty_line(const char *line, int *len_p)
 {
+       int len = *len_p;
        while (len && isspace(line[len-1]))
                len--;
+       *len_p = len;
        return !len;
 }
 
@@ -457,7 +532,8 @@ static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *com
        struct commit_list *parent = commit->parents;
        int offset;
 
-       if ((fmt == CMIT_FMT_ONELINE) || !parent || !parent->next)
+       if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
+           !parent || !parent->next)
                return 0;
 
        offset = sprintf(buf, "Merge:");
@@ -476,13 +552,30 @@ static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *com
        return offset;
 }
 
-unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev)
+unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject)
 {
        int hdr = 1, body = 0;
        unsigned long offset = 0;
-       int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
+       int indent = 4;
        int parents_shown = 0;
        const char *msg = commit->buffer;
+       int plain_non_ascii = 0;
+
+       if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
+               indent = 0;
+
+       /* After-subject is used to pass in Content-Type: multipart
+        * MIME header; in that case we do not have to do the
+        * plaintext content type even if the commit message has
+        * non 7-bit ASCII character.  Otherwise, check if we need
+        * to say this is not a 7-bit ASCII.
+        */
+       if (fmt == CMIT_FMT_EMAIL && !after_subject) {
+               int i;
+               for (i = 0; !plain_non_ascii && msg[i] && i < len; i++)
+                       if (msg[i] & 0x80)
+                               plain_non_ascii = 1;
+       }
 
        for (;;) {
                const char *line = msg;
@@ -506,7 +599,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
                if (hdr) {
                        if (linelen == 1) {
                                hdr = 0;
-                               if (fmt != CMIT_FMT_ONELINE)
+                               if ((fmt != CMIT_FMT_ONELINE) && !subject)
                                        buf[offset++] = '\n';
                                continue;
                        }
@@ -544,20 +637,47 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
                        continue;
                }
 
-               if (is_empty_line(line, linelen)) {
+               if (is_empty_line(line, &linelen)) {
                        if (!body)
                                continue;
+                       if (subject)
+                               continue;
                        if (fmt == CMIT_FMT_SHORT)
                                break;
                } else {
                        body = 1;
                }
 
-               memset(buf + offset, ' ', indent);
-               memcpy(buf + offset + indent, line, linelen);
-               offset += linelen + indent;
+               if (subject) {
+                       int slen = strlen(subject);
+                       memcpy(buf + offset, subject, slen);
+                       offset += slen;
+                       offset += add_rfc2047(buf + offset, line, linelen);
+               }
+               else {
+                       memset(buf + offset, ' ', indent);
+                       memcpy(buf + offset + indent, line, linelen);
+                       offset += linelen + indent;
+               }
+               buf[offset++] = '\n';
                if (fmt == CMIT_FMT_ONELINE)
                        break;
+               if (subject && plain_non_ascii) {
+                       static const char header[] =
+                               "Content-Type: text/plain; charset=UTF-8\n"
+                               "Content-Transfer-Encoding: 8bit\n";
+                       memcpy(buf + offset, header, sizeof(header)-1);
+                       offset += sizeof(header)-1;
+               }
+               if (after_subject) {
+                       int slen = strlen(after_subject);
+                       if (slen > space - offset - 1)
+                               slen = space - offset - 1;
+                       memcpy(buf + offset, after_subject, slen);
+                       offset += slen;
+                       after_subject = NULL;
+               }
+               subject = NULL;
        }
        while (offset && isspace(buf[offset-1]))
                offset--;
index de142af..c9de167 100644 (file)
--- a/commit.h
+++ b/commit.h
@@ -45,12 +45,13 @@ enum cmit_fmt {
        CMIT_FMT_FULL,
        CMIT_FMT_FULLER,
        CMIT_FMT_ONELINE,
+       CMIT_FMT_EMAIL,
 
        CMIT_FMT_UNSPECIFIED,
 };
 
 extern enum cmit_fmt get_commit_format(const char *arg);
-extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev);
+extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject);
 
 /** Removes the first commit from a list sorted by date, and adds all
  * of its parents.
diff --git a/compat/inet_ntop.c b/compat/inet_ntop.c
new file mode 100644 (file)
index 0000000..ec8c1bf
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * Copyright (c) 1996-1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
+ * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
+ * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ */
+
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <string.h>
+
+#ifndef NS_INADDRSZ
+#define NS_INADDRSZ    4
+#endif
+#ifndef NS_IN6ADDRSZ
+#define NS_IN6ADDRSZ   16
+#endif
+#ifndef NS_INT16SZ
+#define NS_INT16SZ     2
+#endif
+
+/*
+ * WARNING: Don't even consider trying to compile this on a system where
+ * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
+ */
+
+/* const char *
+ * inet_ntop4(src, dst, size)
+ *     format an IPv4 address
+ * return:
+ *     `dst' (as a const)
+ * notes:
+ *     (1) uses no statics
+ *     (2) takes a u_char* not an in_addr as input
+ * author:
+ *     Paul Vixie, 1996.
+ */
+static const char *
+inet_ntop4(src, dst, size)
+       const u_char *src;
+       char *dst;
+       size_t size;
+{
+       static const char fmt[] = "%u.%u.%u.%u";
+       char tmp[sizeof "255.255.255.255"];
+       int nprinted;
+
+       nprinted = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
+       if (nprinted < 0)
+               return (NULL);  /* we assume "errno" was set by "snprintf()" */
+       if ((size_t)nprinted > size) {
+               errno = ENOSPC;
+               return (NULL);
+       }
+       strcpy(dst, tmp);
+       return (dst);
+}
+
+#ifndef NO_IPV6
+/* const char *
+ * inet_ntop6(src, dst, size)
+ *     convert IPv6 binary address into presentation (printable) format
+ * author:
+ *     Paul Vixie, 1996.
+ */
+static const char *
+inet_ntop6(src, dst, size)
+       const u_char *src;
+       char *dst;
+       size_t size;
+{
+       /*
+        * Note that int32_t and int16_t need only be "at least" large enough
+        * to contain a value of the specified size.  On some systems, like
+        * Crays, there is no such thing as an integer variable with 16 bits.
+        * Keep this in mind if you think this function should have been coded
+        * to use pointer overlays.  All the world's not a VAX.
+        */
+       char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
+       struct { int base, len; } best, cur;
+       u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
+       int i;
+
+       /*
+        * Preprocess:
+        *      Copy the input (bytewise) array into a wordwise array.
+        *      Find the longest run of 0x00's in src[] for :: shorthanding.
+        */
+       memset(words, '\0', sizeof words);
+       for (i = 0; i < NS_IN6ADDRSZ; i++)
+               words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
+       best.base = -1;
+       cur.base = -1;
+       for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+               if (words[i] == 0) {
+                       if (cur.base == -1)
+                               cur.base = i, cur.len = 1;
+                       else
+                               cur.len++;
+               } else {
+                       if (cur.base != -1) {
+                               if (best.base == -1 || cur.len > best.len)
+                                       best = cur;
+                               cur.base = -1;
+                       }
+               }
+       }
+       if (cur.base != -1) {
+               if (best.base == -1 || cur.len > best.len)
+                       best = cur;
+       }
+       if (best.base != -1 && best.len < 2)
+               best.base = -1;
+
+       /*
+        * Format the result.
+        */
+       tp = tmp;
+       for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+               /* Are we inside the best run of 0x00's? */
+               if (best.base != -1 && i >= best.base &&
+                   i < (best.base + best.len)) {
+                       if (i == best.base)
+                               *tp++ = ':';
+                       continue;
+               }
+               /* Are we following an initial run of 0x00s or any real hex? */
+               if (i != 0)
+                       *tp++ = ':';
+               /* Is this address an encapsulated IPv4? */
+               if (i == 6 && best.base == 0 &&
+                   (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
+                       if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
+                               return (NULL);
+                       tp += strlen(tp);
+                       break;
+               }
+               tp += snprintf(tp, sizeof tmp - (tp - tmp), "%x", words[i]);
+       }
+       /* Was it a trailing run of 0x00's? */
+       if (best.base != -1 && (best.base + best.len) ==
+           (NS_IN6ADDRSZ / NS_INT16SZ))
+               *tp++ = ':';
+       *tp++ = '\0';
+
+       /*
+        * Check for overflow, copy, and we're done.
+        */
+       if ((size_t)(tp - tmp) > size) {
+               errno = ENOSPC;
+               return (NULL);
+       }
+       strcpy(dst, tmp);
+       return (dst);
+}
+#endif
+
+/* char *
+ * inet_ntop(af, src, dst, size)
+ *     convert a network format address to presentation format.
+ * return:
+ *     pointer to presentation format address (`dst'), or NULL (see errno).
+ * author:
+ *     Paul Vixie, 1996.
+ */
+const char *
+inet_ntop(af, src, dst, size)
+       int af;
+       const void *src;
+       char *dst;
+       size_t size;
+{
+       switch (af) {
+       case AF_INET:
+               return (inet_ntop4(src, dst, size));
+#ifndef NO_IPV6
+       case AF_INET6:
+               return (inet_ntop6(src, dst, size));
+#endif
+       default:
+               errno = EAFNOSUPPORT;
+               return (NULL);
+       }
+       /* NOTREACHED */
+}
index 4e1f0c2..984c75f 100644 (file)
--- a/config.c
+++ b/config.c
@@ -60,6 +60,12 @@ static char *parse_value(void)
                        space = 1;
                        continue;
                }
+               if (!quote) {
+                       if (c == ';' || c == '#') {
+                               comment = 1;
+                               continue;
+                       }
+               }
                if (space) {
                        if (len)
                                value[len++] = ' ';
@@ -93,12 +99,6 @@ static char *parse_value(void)
                        quote = 1-quote;
                        continue;
                }
-               if (!quote) {
-                       if (c == ';' || c == '#') {
-                               comment = 1;
-                               continue;
-                       }
-               }
                value[len++] = c;
        }
 }
@@ -134,6 +134,41 @@ static int get_value(config_fn_t fn, char *name, unsigned int len)
        return fn(name, value);
 }
 
+static int get_extended_base_var(char *name, int baselen, int c)
+{
+       do {
+               if (c == '\n')
+                       return -1;
+               c = get_next_char();
+       } while (isspace(c));
+
+       /* We require the format to be '[base "extension"]' */
+       if (c != '"')
+               return -1;
+       name[baselen++] = '.';
+
+       for (;;) {
+               int c = get_next_char();
+               if (c == '\n')
+                       return -1;
+               if (c == '"')
+                       break;
+               if (c == '\\') {
+                       c = get_next_char();
+                       if (c == '\n')
+                               return -1;
+               }
+               name[baselen++] = c;
+               if (baselen > MAXNAME / 2)
+                       return -1;
+       }
+
+       /* Final ']' */
+       if (get_next_char() != ']')
+               return -1;
+       return baselen;
+}
+
 static int get_base_var(char *name)
 {
        int baselen = 0;
@@ -144,6 +179,8 @@ static int get_base_var(char *name)
                        return -1;
                if (c == ']')
                        return baselen;
+               if (isspace(c))
+                       return get_extended_base_var(name, baselen, c);
                if (!isalnum(c) && c != '.')
                        return -1;
                if (baselen > MAXNAME / 2)
@@ -227,8 +264,13 @@ int git_default_config(const char *var, const char *value)
                return 0;
        }
 
-       if (!strcmp(var, "core.symrefsonly")) {
-               only_use_symrefs = git_config_bool(var, value);
+       if (!strcmp(var, "core.prefersymlinkrefs")) {
+               prefer_symlink_refs = git_config_bool(var, value);
+               return 0;
+       }
+
+       if (!strcmp(var, "core.logallrefupdates")) {
+               log_all_ref_updates = git_config_bool(var, value);
                return 0;
        }
 
@@ -238,17 +280,17 @@ int git_default_config(const char *var, const char *value)
        }
 
        if (!strcmp(var, "user.name")) {
-               strncpy(git_default_name, value, sizeof(git_default_name));
+               safe_strncpy(git_default_name, value, sizeof(git_default_name));
                return 0;
        }
 
        if (!strcmp(var, "user.email")) {
-               strncpy(git_default_email, value, sizeof(git_default_email));
+               safe_strncpy(git_default_email, value, sizeof(git_default_email));
                return 0;
        }
 
        if (!strcmp(var, "i18n.commitencoding")) {
-               strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
+               safe_strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
                return 0;
        }
 
@@ -335,16 +377,43 @@ static int store_aux(const char* key, const char* value)
                        store.offset[store.seen] = ftell(config_file);
                        store.state = KEY_SEEN;
                        store.seen++;
-               } else if(!strncmp(key, store.key, store.baselen))
-                       store.state = SECTION_SEEN;
+               } else {
+                       if (strrchr(key, '.') - key == store.baselen &&
+                             !strncmp(key, store.key, store.baselen)) {
+                                       store.state = SECTION_SEEN;
+                                       store.offset[store.seen] = ftell(config_file);
+                       }
+               }
        }
        return 0;
 }
 
 static void store_write_section(int fd, const char* key)
 {
+       const char *dot = strchr(key, '.');
+       int len1 = store.baselen, len2 = -1;
+
+       dot = strchr(key, '.');
+       if (dot) {
+               int dotlen = dot - key;
+               if (dotlen < len1) {
+                       len2 = len1 - dotlen - 1;
+                       len1 = dotlen;
+               }
+       }
+
        write(fd, "[", 1);
-       write(fd, key, store.baselen);
+       write(fd, key, len1);
+       if (len2 >= 0) {
+               write(fd, " \"", 2);
+               while (--len2 >= 0) {
+                       unsigned char c = *++dot;
+                       if (c == '"')
+                               write(fd, "\\", 1);
+                       write(fd, &c, 1);
+               }
+               write(fd, "\"", 1);
+       }
        write(fd, "]\n", 2);
 }
 
@@ -418,8 +487,8 @@ int git_config_set(const char* key, const char* value)
 int git_config_set_multivar(const char* key, const char* value,
        const char* value_regex, int multi_replace)
 {
-       int i;
-       int fd, in_fd;
+       int i, dot;
+       int fd = -1, in_fd;
        int ret;
        char* config_filename = strdup(git_path("config"));
        char* lock_file = strdup(git_path("config.lock"));
@@ -443,16 +512,23 @@ int git_config_set_multivar(const char* key, const char* value,
         * Validate the key and while at it, lower case it for matching.
         */
        store.key = (char*)malloc(strlen(key)+1);
-       for (i = 0; key[i]; i++)
-               if (i != store.baselen &&
-                               ((!isalnum(key[i]) && key[i] != '.') ||
-                                (i == store.baselen+1 && !isalpha(key[i])))) {
-                       fprintf(stderr, "invalid key: %s\n", key);
-                       free(store.key);
-                       ret = 1;
-                       goto out_free;
-               } else
-                       store.key[i] = tolower(key[i]);
+       dot = 0;
+       for (i = 0; key[i]; i++) {
+               unsigned char c = key[i];
+               if (c == '.')
+                       dot = 1;
+               /* Leave the extended basename untouched.. */
+               if (!dot || i > store.baselen) {
+                       if (!isalnum(c) || (i == store.baselen+1 && !isalpha(c))) {
+                               fprintf(stderr, "invalid key: %s\n", key);
+                               free(store.key);
+                               ret = 1;
+                               goto out_free;
+                       }
+                       c = tolower(c);
+               }
+               store.key[i] = c;
+       }
        store.key[i] = 0;
 
        /*
@@ -460,7 +536,7 @@ int git_config_set_multivar(const char* key, const char* value,
         * contents of .git/config will be written into it.
         */
        fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
-       if (fd < 0) {
+       if (fd < 0 || adjust_shared_perm(lock_file)) {
                fprintf(stderr, "could not lock config file\n");
                free(store.key);
                ret = -1;
@@ -477,15 +553,11 @@ int git_config_set_multivar(const char* key, const char* value,
                if ( ENOENT != errno ) {
                        error("opening %s: %s", config_filename,
                              strerror(errno));
-                       close(fd);
-                       unlink(lock_file);
                        ret = 3; /* same as "invalid config file" */
                        goto out_free;
                }
                /* if nothing to unset, error out */
                if (value == NULL) {
-                       close(fd);
-                       unlink(lock_file);
                        ret = 5;
                        goto out_free;
                }
@@ -548,8 +620,6 @@ int git_config_set_multivar(const char* key, const char* value,
                /* if nothing to unset, or too many matches, error out */
                if ((store.seen == 0 && value == NULL) ||
                                (store.seen > 1 && multi_replace == 0)) {
-                       close(fd);
-                       unlink(lock_file);
                        ret = 5;
                        goto out_free;
                }
@@ -598,8 +668,6 @@ int git_config_set_multivar(const char* key, const char* value,
                unlink(config_filename);
        }
 
-       close(fd);
-
        if (rename(lock_file, config_filename) < 0) {
                fprintf(stderr, "Could not rename the lock file?\n");
                ret = 4;
@@ -609,10 +677,14 @@ int git_config_set_multivar(const char* key, const char* value,
        ret = 0;
 
 out_free:
+       if (0 <= fd)
+               close(fd);
        if (config_filename)
                free(config_filename);
-       if (lock_file)
+       if (lock_file) {
+               unlink(lock_file);
                free(lock_file);
+       }
        return ret;
 }
 
index 6a8f8a6..52d709e 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -100,7 +100,7 @@ int path_match(const char *path, int nr, char **match)
                if (pathlen > len && path[pathlen - len - 1] != '/')
                        continue;
                *s = 0;
-               return 1;
+               return (i + 1);
        }
        return 0;
 }
@@ -322,7 +322,10 @@ static enum protocol get_protocol(const char *name)
 
 #ifndef NO_IPV6
 
-static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
+/*
+ * Returns a connected socket() fd, or else die()s.
+ */
+static int git_tcp_connect_sock(char *host)
 {
        int sockfd = -1;
        char *colon, *end;
@@ -356,7 +359,8 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
                die("Unable to look up %s (%s)", host, gai_strerror(gai));
 
        for (ai0 = ai; ai; ai = ai->ai_next) {
-               sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+               sockfd = socket(ai->ai_family,
+                               ai->ai_socktype, ai->ai_protocol);
                if (sockfd < 0)
                        continue;
                if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
@@ -372,15 +376,15 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
        if (sockfd < 0)
                die("unable to connect a socket (%s)", strerror(errno));
 
-       fd[0] = sockfd;
-       fd[1] = sockfd;
-       packet_write(sockfd, "%s %s\n", prog, path);
-       return 0;
+       return sockfd;
 }
 
 #else /* NO_IPV6 */
 
-static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
+/*
+ * Returns a connected socket() fd, or else die()s.
+ */
+static int git_tcp_connect_sock(char *host)
 {
        int sockfd = -1;
        char *colon, *end;
@@ -407,7 +411,6 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
                port = colon + 1;
        }
 
-
        he = gethostbyname(host);
        if (!he)
                die("Unable to look up %s (%s)", host, hstrerror(h_errno));
@@ -441,13 +444,21 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
        if (sockfd < 0)
                die("unable to connect a socket (%s)", strerror(errno));
 
+       return sockfd;
+}
+
+#endif /* NO_IPV6 */
+
+
+static void git_tcp_connect(int fd[2],
+                           const char *prog, char *host, char *path)
+{
+       int sockfd = git_tcp_connect_sock(host);
+
        fd[0] = sockfd;
        fd[1] = sockfd;
-       packet_write(sockfd, "%s %s\n", prog, path);
-       return 0;
 }
 
-#endif /* NO_IPV6 */
 
 static char *git_proxy_command = NULL;
 static const char *rhost_name = NULL;
@@ -510,7 +521,8 @@ static int git_use_proxy(const char *host)
        return (git_proxy_command && *git_proxy_command);
 }
 
-static int git_proxy_connect(int fd[2], const char *prog, char *host, char *path)
+static void git_proxy_connect(int fd[2],
+                             const char *prog, char *host, char *path)
 {
        char *port = STR(DEFAULT_GIT_PORT);
        char *colon, *end;
@@ -547,12 +559,12 @@ static int git_proxy_connect(int fd[2], const char *prog, char *host, char *path
                execlp(git_proxy_command, git_proxy_command, host, port, NULL);
                die("exec failed");
        }
+       if (pid < 0)
+               die("fork failed");
        fd[0] = pipefd[0][0];
        fd[1] = pipefd[1][1];
        close(pipefd[0][1]);
        close(pipefd[1][0]);
-       packet_write(fd[1], "%s %s\n", prog, path);
-       return pid;
 }
 
 /*
@@ -620,19 +632,33 @@ int git_connect(int fd[2], char *url, const char *prog)
        }
 
        if (protocol == PROTO_GIT) {
-               int ret;
+               /* These underlying connection commands die() if they
+                * cannot connect.
+                */
+               char *target_host = strdup(host);
                if (git_use_proxy(host))
-                       ret = git_proxy_connect(fd, prog, host, path);
+                       git_proxy_connect(fd, prog, host, path);
                else
-                       ret = git_tcp_connect(fd, prog, host, path);
+                       git_tcp_connect(fd, prog, host, path);
+               /*
+                * Separate original protocol components prog and path
+                * from extended components with a NUL byte.
+                */
+               packet_write(fd[1],
+                            "%s %s%chost=%s%c",
+                            prog, path, 0,
+                            target_host, 0);
+               free(target_host);
                if (free_path)
                        free(path);
-               return ret;
+               return 0;
        }
 
        if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
                die("unable to create pipe pair for communication");
        pid = fork();
+       if (pid < 0)
+               die("unable to fork");
        if (!pid) {
                snprintf(command, sizeof(command), "%s %s", prog,
                         sq_quote(path));
index acedf73..6aedb10 100644 (file)
@@ -29,7 +29,17 @@ git-svn.html : git-svn.txt
        asciidoc -b xhtml11 -d manpage \
                -f ../../Documentation/asciidoc.conf $<
 test: git-svn
-       cd t && $(SHELL) ./t0000-contrib-git-svn.sh
+       cd t && $(SHELL) ./t0000-contrib-git-svn.sh $(TEST_FLAGS)
+       cd t && $(SHELL) ./t0001-contrib-git-svn-props.sh $(TEST_FLAGS)
+
+# we can test NO_OPTIMIZE_COMMITS independently of LC_ALL
+full-test:
+       $(MAKE) test GIT_SVN_NO_LIB=1 GIT_SVN_NO_OPTIMIZE_COMMITS=1 LC_ALL=C
+       $(MAKE) test GIT_SVN_NO_LIB=0 GIT_SVN_NO_OPTIMIZE_COMMITS=1 LC_ALL=C
+       $(MAKE) test GIT_SVN_NO_LIB=1 GIT_SVN_NO_OPTIMIZE_COMMITS=0 \
+                                                       LC_ALL=en_US.UTF-8
+       $(MAKE) test GIT_SVN_NO_LIB=0 GIT_SVN_NO_OPTIMIZE_COMMITS=0 \
+                                                       LC_ALL=en_US.UTF-8
 
 clean:
        rm -f git-svn *.xml *.html *.1
index 7c44450..da0ff9a 100755 (executable)
@@ -6,14 +6,16 @@ use strict;
 use vars qw/   $AUTHOR $VERSION
                $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
                $GIT_SVN_INDEX $GIT_SVN
-               $GIT_DIR $REV_DIR/;
+               $GIT_DIR $GIT_SVN_DIR $REVDB/;
 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
-$VERSION = '0.11.0';
+$VERSION = '1.1.1-broken';
 
 use Cwd qw/abs_path/;
 $GIT_DIR = abs_path($ENV{GIT_DIR} || '.git');
 $ENV{GIT_DIR} = $GIT_DIR;
 
+my $LC_ALL = $ENV{LC_ALL};
+my $TZ = $ENV{TZ};
 # make sure the svn binary gives consistent output between locales and TZs:
 $ENV{TZ} = 'UTC';
 $ENV{LC_ALL} = 'C';
@@ -26,36 +28,88 @@ use Carp qw/croak/;
 use IO::File qw//;
 use File::Basename qw/dirname basename/;
 use File::Path qw/mkpath/;
-use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
+use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
 use File::Spec qw//;
 use POSIX qw/strftime/;
+use IPC::Open3;
+use Memoize;
+memoize('revisions_eq');
+
+my ($SVN_PATH, $SVN, $SVN_LOG, $_use_lib);
+$_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB};
+libsvn_load();
+my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS};
 my $sha1 = qr/[a-f\d]{40}/;
 my $sha1_short = qr/[a-f\d]{4,40}/;
 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
-       $_find_copies_harder, $_l, $_version, $_upgrade, $_authors);
-my (@_branch_from, %tree_map, %users);
-my $_svn_co_url_revs;
+       $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
+       $_repack, $_repack_nr, $_repack_flags,
+       $_template, $_shared, $_no_default_regex, $_no_graft_copy,
+       $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
+       $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m);
+my (@_branch_from, %tree_map, %users, %rusers, %equiv);
+my ($_svn_co_url_revs, $_svn_pg_peg_revs);
+my @repo_path_split_cache;
 
 my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
                'branch|b=s' => \@_branch_from,
-               'authors-file|A=s' => \$_authors );
+               'branch-all-refs|B' => \$_branch_all_refs,
+               'authors-file|A=s' => \$_authors,
+               'repack:i' => \$_repack,
+               'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
+
+my ($_trunk, $_tags, $_branches);
+my %multi_opts = ( 'trunk|T=s' => \$_trunk,
+               'tags|t=s' => \$_tags,
+               'branches|b=s' => \$_branches );
+my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
+
+# yes, 'native' sets "\n".  Patches to fix this for non-*nix systems welcome:
+my %EOL = ( CR => "\015", LF => "\012", CRLF => "\015\012", native => "\012" );
+
 my %cmd = (
        fetch => [ \&fetch, "Download new revisions from SVN",
                        { 'revision|r=s' => \$_revision, %fc_opts } ],
-       init => [ \&init, "Initialize and fetch (import)", { } ],
+       init => [ \&init, "Initialize a repo for tracking" .
+                         " (requires URL argument)",
+                         \%init_opts ],
        commit => [ \&commit, "Commit git revisions to SVN",
                        {       'stdin|' => \$_stdin,
                                'edit|e' => \$_edit,
                                'rmdir' => \$_rmdir,
                                'find-copies-harder' => \$_find_copies_harder,
                                'l=i' => \$_l,
+                               'copy-similarity|C=i'=> \$_cp_similarity,
                                %fc_opts,
                        } ],
-       'show-ignore' => [ \&show_ignore, "Show svn:ignore listings", { } ],
+       'show-ignore' => [ \&show_ignore, "Show svn:ignore listings",
+                       { 'revision|r=i' => \$_revision } ],
        rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
                        { 'no-ignore-externals' => \$_no_ignore_ext,
+                         'copy-remote|remote=s' => \$_cp_remote,
                          'upgrade' => \$_upgrade } ],
+       'graft-branches' => [ \&graft_branches,
+                       'Detect merges/branches from already imported history',
+                       { 'merge-rx|m' => \@_opt_m,
+                         'no-default-regex' => \$_no_default_regex,
+                         'no-graft-copy' => \$_no_graft_copy } ],
+       'multi-init' => [ \&multi_init,
+                       'Initialize multiple trees (like git-svnimport)',
+                       { %multi_opts, %fc_opts } ],
+       'multi-fetch' => [ \&multi_fetch,
+                       'Fetch multiple trees (like git-svnimport)',
+                       \%fc_opts ],
+       'log' => [ \&show_log, 'Show commit logs',
+                       { 'limit=i' => \$_limit,
+                         'revision|r=s' => \$_revision,
+                         'verbose|v' => \$_verbose,
+                         'incremental' => \$_incremental,
+                         'oneline' => \$_oneline,
+                         'show-commit' => \$_show_commit,
+                         'authors-file|A=s' => \$_authors,
+                       } ],
 );
+
 my $cmd;
 for (my $i = 0; $i < @ARGV; $i++) {
        if (defined $cmd{$ARGV[$i]}) {
@@ -67,40 +121,21 @@ for (my $i = 0; $i < @ARGV; $i++) {
 
 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
 
-# convert GetOpt::Long specs for use by git-repo-config
-foreach my $o (keys %opts) {
-       my $v = $opts{$o};
-       my ($key) = ($o =~ /^([a-z\-]+)/);
-       $key =~ s/-//g;
-       my $arg = 'git-repo-config';
-       $arg .= ' --int' if ($o =~ /=i$/);
-       $arg .= ' --bool' if ($o !~ /=[sfi]$/);
-       if (ref $v eq 'ARRAY') {
-               chomp(my @tmp = `$arg --get-all svn.$key`);
-               @$v = @tmp if @tmp;
-       } else {
-               chomp(my $tmp = `$arg --get svn.$key`);
-               if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
-                       $$v = $tmp;
-               }
-       }
-}
-
-GetOptions(%opts, 'help|H|h' => \$_help,
-               'version|V' => \$_version,
-               'id|i=s' => \$GIT_SVN) or exit 1;
-
-$GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
-$GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
-$SVN_URL = undef;
-$REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
-$SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
+read_repo_config(\%opts);
+my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
+                               'version|V' => \$_version,
+                               'id|i=s' => \$GIT_SVN);
+exit 1 if (!$rv && $cmd ne 'log');
 
+set_default_vals();
 usage(0) if $_help;
 version() if $_version;
 usage(1) unless defined $cmd;
+init_vars();
 load_authors() if $_authors;
+load_all_refs() if $_branch_all_refs;
 svn_compat_check();
+migration_check() unless $cmd =~ /^(?:init|rebuild|multi-init)$/;
 $cmd{$cmd}->[0]->(@ARGV);
 exit 0;
 
@@ -119,7 +154,7 @@ Usage: $0 <command> [options] [arguments]\n
                print $fd '  ',pack('A13',$_),$cmd{$_}->[1],"\n";
                foreach (keys %{$cmd{$_}->[2]}) {
                        # prints out arguments as they should be passed:
-                       my $x = s#=s$## ? '<arg>' : s#=i$## ? '<num>' : '';
+                       my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
                        print $fd ' ' x 17, join(', ', map { length $_ > 1 ?
                                                        "--$_" : "-$_" }
                                                split /\|/,$_)," $x\n";
@@ -140,6 +175,9 @@ sub version {
 }
 
 sub rebuild {
+       if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
+               copy_remote_ref();
+       }
        $SVN_URL = shift or undef;
        my $newest_rev = 0;
        if ($_upgrade) {
@@ -160,18 +198,10 @@ sub rebuild {
                croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
                my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
                next if (!@commit); # skip merges
-               my $id = $commit[$#commit];
-               my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
-                                               \s([a-f\d\-]+)$/x);
-               if (!$rev || !$uuid || !$url) {
-                       # some of the original repositories I made had
-                       # indentifiers like this:
-                       ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
-                                                       \@([a-f\d\-]+)/x);
-                       if (!$rev || !$uuid) {
-                               croak "Unable to extract revision or UUID from ",
-                                       "$c, $id\n";
-                       }
+               my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
+               if (!$rev || !$uuid) {
+                       croak "Unable to extract revision or UUID from ",
+                               "$c, $commit[$#commit]\n";
                }
 
                # if we merged or otherwise started elsewhere, this is
@@ -179,7 +209,6 @@ sub rebuild {
                next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
                next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
 
-               print "r$rev = $c\n";
                unless (defined $latest) {
                        if (!$SVN_URL && !$url) {
                                croak "SVN repository location required: $url\n";
@@ -189,11 +218,13 @@ sub rebuild {
                        setup_git_svn();
                        $latest = $rev;
                }
-               assert_revision_eq_or_unknown($rev, $c);
-               sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
+               revdb_set($REVDB, $rev, $c);
+               print "r$rev = $c\n";
                $newest_rev = $rev if ($rev > $newest_rev);
        }
        close $rev_list or croak $?;
+
+       goto out if $_use_lib;
        if (!chdir $SVN_WC) {
                svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
                chdir $SVN_WC or croak $!;
@@ -206,11 +237,12 @@ sub rebuild {
                push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
                sys(@svn_up,"-r$newest_rev");
                $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
-               git_addremove();
-               exec('git-write-tree');
+               index_changes();
+               exec('git-write-tree') or croak $!;
        }
        waitpid $pid, 0;
-
+       croak $? if $?;
+out:
        if ($_upgrade) {
                print STDERR <<"";
 Keeping deprecated refs/head/$GIT_SVN-HEAD for now.  Please remove it
@@ -220,17 +252,31 @@ when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
 }
 
 sub init {
-       $SVN_URL = shift or croak "SVN repository location required\n";
+       $SVN_URL = shift or die "SVN repository location required " .
+                               "as a command-line argument\n";
+       $SVN_URL =~ s!/+$!!; # strip trailing slash
        unless (-d $GIT_DIR) {
-               sys('git-init-db');
+               my @init_db = ('git-init-db');
+               push @init_db, "--template=$_template" if defined $_template;
+               push @init_db, "--shared" if defined $_shared;
+               sys(@init_db);
        }
        setup_git_svn();
 }
 
 sub fetch {
-       my (@parents) = @_;
        check_upgrade_needed();
-       $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
+       $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
+       my $ret = $_use_lib ? fetch_lib(@_) : fetch_cmd(@_);
+       if ($ret->{commit} && quiet_run(qw(git-rev-parse --verify
+                                               refs/heads/master^0))) {
+               sys(qw(git-update-ref refs/heads/master),$ret->{commit});
+       }
+       return $ret;
+}
+
+sub fetch_cmd {
+       my (@parents) = @_;
        my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
        unless ($_revision) {
                $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
@@ -241,38 +287,128 @@ sub fetch {
        my $svn_log = svn_log_raw(@log_args);
 
        my $base = next_log_entry($svn_log) or croak "No base revision!\n";
-       my $last_commit = undef;
+       # don't need last_revision from grab_base_rev() because
+       # user could've specified a different revision to skip (they
+       # didn't want to import certain revisions into git for whatever
+       # reason, so trust $base->{revision} instead.
+       my (undef, $last_commit) = svn_grab_base_rev();
        unless (-d $SVN_WC) {
                svn_cmd_checkout($SVN_URL,$base->{revision},$SVN_WC);
                chdir $SVN_WC or croak $!;
                read_uuid();
                $last_commit = git_commit($base, @parents);
-               assert_svn_wc_clean($base->{revision}, $last_commit);
+               assert_tree($last_commit);
        } else {
                chdir $SVN_WC or croak $!;
                read_uuid();
-               $last_commit = file_to_s("$REV_DIR/$base->{revision}");
+               # looks like a user manually cp'd and svn switch'ed
+               unless ($last_commit) {
+                       sys(qw/svn revert -R ./);
+                       assert_svn_wc_clean($base->{revision});
+                       $last_commit = git_commit($base, @parents);
+                       assert_tree($last_commit);
+               }
        }
        my @svn_up = qw(svn up);
        push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
        my $last = $base;
        while (my $log_msg = next_log_entry($svn_log)) {
-               assert_svn_wc_clean($last->{revision}, $last_commit);
                if ($last->{revision} >= $log_msg->{revision}) {
                        croak "Out of order: last >= current: ",
                                "$last->{revision} >= $log_msg->{revision}\n";
                }
+               # Revert is needed for cases like:
+               # https://svn.musicpd.org/Jamming/trunk (r166:167), but
+               # I can't seem to reproduce something like that on a test...
+               sys(qw/svn revert -R ./);
+               assert_svn_wc_clean($last->{revision});
                sys(@svn_up,"-r$log_msg->{revision}");
                $last_commit = git_commit($log_msg, $last_commit, @parents);
                $last = $log_msg;
        }
-       assert_svn_wc_clean($last->{revision}, $last_commit);
-       unless (-e "$GIT_DIR/refs/heads/master") {
-               sys(qw(git-update-ref refs/heads/master),$last_commit);
-       }
+       close $svn_log->{fh};
+       $last->{commit} = $last_commit;
        return $last;
 }
 
+sub fetch_lib {
+       my (@parents) = @_;
+       $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
+       my $repo;
+       ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
+       $SVN_LOG ||= libsvn_connect($repo);
+       $SVN ||= libsvn_connect($repo);
+       my ($last_rev, $last_commit) = svn_grab_base_rev();
+       my ($base, $head) = libsvn_parse_revision($last_rev);
+       if ($base > $head) {
+               return { revision => $last_rev, commit => $last_commit }
+       }
+       my $index = set_index($GIT_SVN_INDEX);
+
+       # limit ourselves and also fork() since get_log won't release memory
+       # after processing a revision and SVN stuff seems to leak
+       my $inc = 1000;
+       my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
+       read_uuid();
+       if (defined $last_commit) {
+               unless (-e $GIT_SVN_INDEX) {
+                       sys(qw/git-read-tree/, $last_commit);
+               }
+               chomp (my $x = `git-write-tree`);
+               my ($y) = (`git-cat-file commit $last_commit`
+                                                       =~ /^tree ($sha1)/m);
+               if ($y ne $x) {
+                       unlink $GIT_SVN_INDEX or croak $!;
+                       sys(qw/git-read-tree/, $last_commit);
+               }
+               chomp ($x = `git-write-tree`);
+               if ($y ne $x) {
+                       print STDERR "trees ($last_commit) $y != $x\n",
+                                "Something is seriously wrong...\n";
+               }
+       }
+       while (1) {
+               # fork, because using SVN::Pool with get_log() still doesn't
+               # seem to help enough to keep memory usage down.
+               defined(my $pid = fork) or croak $!;
+               if (!$pid) {
+                       $SVN::Error::handler = \&libsvn_skip_unknown_revs;
+
+                       # Yes I'm perfectly aware that the fourth argument
+                       # below is the limit revisions number.  Unfortunately
+                       # performance sucks with it enabled, so it's much
+                       # faster to fetch revision ranges instead of relying
+                       # on the limiter.
+                       $SVN_LOG->get_log( '/'.$SVN_PATH, $min, $max, 0, 1, 1,
+                               sub {
+                                       my $log_msg;
+                                       if ($last_commit) {
+                                               $log_msg = libsvn_fetch(
+                                                       $last_commit, @_);
+                                               $last_commit = git_commit(
+                                                       $log_msg,
+                                                       $last_commit,
+                                                       @parents);
+                                       } else {
+                                               $log_msg = libsvn_new_tree(@_);
+                                               $last_commit = git_commit(
+                                                       $log_msg, @parents);
+                                       }
+                               });
+                       exit 0;
+               }
+               waitpid $pid, 0;
+               croak $? if $?;
+               ($last_rev, $last_commit) = svn_grab_base_rev();
+               last if ($max >= $head);
+               $min = $max + 1;
+               $max += $inc;
+               $max = $head if ($max > $head);
+       }
+       restore_index($index);
+       return { revision => $last_rev, commit => $last_commit };
+}
+
 sub commit {
        my (@commits) = @_;
        check_upgrade_needed();
@@ -297,37 +433,127 @@ sub commit {
                }
        }
        chomp @revs;
+       $_use_lib ? commit_lib(@revs) : commit_cmd(@revs);
+       print "Done committing ",scalar @revs," revisions to SVN\n";
+}
 
-       fetch();
-       chdir $SVN_WC or croak $!;
+sub commit_cmd {
+       my (@revs) = @_;
+
+       chdir $SVN_WC or croak "Unable to chdir $SVN_WC: $!\n";
        my $info = svn_info('.');
+       my $fetched = fetch();
+       if ($info->{Revision} != $fetched->{revision}) {
+               print STDERR "There are new revisions that were fetched ",
+                               "and need to be merged (or acknowledged) ",
+                               "before committing.\n";
+               exit 1;
+       }
+       $info = svn_info('.');
        read_uuid($info);
-       my $svn_current_rev =  $info->{'Last Changed Rev'};
+       my $last = $fetched;
        foreach my $c (@revs) {
-               my $mods = svn_checkout_tree($svn_current_rev, $c);
+               my $mods = svn_checkout_tree($last, $c);
                if (scalar @$mods == 0) {
                        print "Skipping, no changes detected\n";
                        next;
                }
-               $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
+               $last = svn_commit_tree($last, $c);
        }
-       print "Done committing ",scalar @revs," revisions to SVN\n";
+}
 
+sub commit_lib {
+       my (@revs) = @_;
+       my ($r_last, $cmt_last) = svn_grab_base_rev();
+       defined $r_last or die "Must have an existing revision to commit\n";
+       my $fetched = fetch();
+       if ($r_last != $fetched->{revision}) {
+               print STDERR "There are new revisions that were fetched ",
+                               "and need to be merged (or acknowledged) ",
+                               "before committing.\n",
+                               "last rev: $r_last\n",
+                               " current: $fetched->{revision}\n";
+               exit 1;
+       }
+       read_uuid();
+       my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
+       my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
+
+       foreach my $c (@revs) {
+               # fork for each commit because there's a memory leak I
+               # can't track down... (it's probably in the SVN code)
+               defined(my $pid = open my $fh, '-|') or croak $!;
+               if (!$pid) {
+                       if (defined $LC_ALL) {
+                               $ENV{LC_ALL} = $LC_ALL;
+                       } else {
+                               delete $ENV{LC_ALL};
+                       }
+                       my $log_msg = get_commit_message($c, $commit_msg);
+                       my $ed = SVN::Git::Editor->new(
+                                       {       r => $r_last,
+                                               ra => $SVN,
+                                               c => $c,
+                                               svn_path => $SVN_PATH
+                                       },
+                                       $SVN->get_commit_editor(
+                                               $log_msg->{msg},
+                                               sub {
+                                                       libsvn_commit_cb(
+                                                               @_, $c,
+                                                               $log_msg->{msg},
+                                                               $r_last,
+                                                               $cmt_last)
+                                               },
+                                               @lock)
+                                       );
+                       my $mods = libsvn_checkout_tree($cmt_last, $c, $ed);
+                       if (@$mods == 0) {
+                               print "No changes\nr$r_last = $cmt_last\n";
+                               $ed->abort_edit;
+                       } else {
+                               $ed->close_edit;
+                       }
+                       exit 0;
+               }
+               my ($r_new, $cmt_new, $no);
+               while (<$fh>) {
+                       print $_;
+                       chomp;
+                       if (/^r(\d+) = ($sha1)$/o) {
+                               ($r_new, $cmt_new) = ($1, $2);
+                       } elsif ($_ eq 'No changes') {
+                               $no = 1;
+                       }
+               }
+               close $fh or croak $?;
+               if (! defined $r_new && ! defined $cmt_new) {
+                       unless ($no) {
+                               die "Failed to parse revision information\n";
+                       }
+               } else {
+                       ($r_last, $cmt_last) = ($r_new, $cmt_new);
+               }
+       }
+       unlink $commit_msg;
 }
 
 sub show_ignore {
-       require File::Find or die $!;
-       my $exclude_file = "$GIT_DIR/info/exclude";
-       open my $fh, '<', $exclude_file or croak $!;
-       chomp(my @excludes = (<$fh>));
-       close $fh or croak $!;
+       $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
+       $_use_lib ? show_ignore_lib() : show_ignore_cmd();
+}
 
-       $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
+sub show_ignore_cmd {
+       require File::Find or die $!;
+       if (defined $_revision) {
+               die "-r/--revision option doesn't work unless the Perl SVN ",
+                       "libraries are used\n";
+       }
        chdir $SVN_WC or croak $!;
        my %ign;
        File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
                s#^\./##;
-               @{$ign{$_}} = safe_qx(qw(svn propget svn:ignore),$_);
+               @{$ign{$_}} = svn_propget_base('svn:ignore', $_);
                }}, no_chdir=>1},'.');
 
        print "\n# /\n";
@@ -339,14 +565,495 @@ sub show_ignore {
        }
 }
 
+sub show_ignore_lib {
+       my $repo;
+       ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
+       $SVN ||= libsvn_connect($repo);
+       my $r = defined $_revision ? $_revision : $SVN->get_latest_revnum;
+       libsvn_traverse_ignore(\*STDOUT, $SVN_PATH, $r);
+}
+
+sub graft_branches {
+       my $gr_file = "$GIT_DIR/info/grafts";
+       my ($grafts, $comments) = read_grafts($gr_file);
+       my $gr_sha1;
+
+       if (%$grafts) {
+               # temporarily disable our grafts file to make this idempotent
+               chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file));
+               rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
+       }
+
+       my $l_map = read_url_paths();
+       my @re = map { qr/$_/is } @_opt_m if @_opt_m;
+       unless ($_no_default_regex) {
+               push @re, (     qr/\b(?:merge|merging|merged)\s+(\S.+)/is,
+                               qr/\b(?:from|of)\s+(\S.+)/is );
+       }
+       foreach my $u (keys %$l_map) {
+               if (@re) {
+                       foreach my $p (keys %{$l_map->{$u}}) {
+                               graft_merge_msg($grafts,$l_map,$u,$p);
+                       }
+               }
+               unless ($_no_graft_copy) {
+                       if ($_use_lib) {
+                               graft_file_copy_lib($grafts,$l_map,$u);
+                       } else {
+                               graft_file_copy_cmd($grafts,$l_map,$u);
+                       }
+               }
+       }
+
+       write_grafts($grafts, $comments, $gr_file);
+       unlink "$gr_file~$gr_sha1" if $gr_sha1;
+}
+
+sub multi_init {
+       my $url = shift;
+       $_trunk ||= 'trunk';
+       $_trunk =~ s#/+$##;
+       $url =~ s#/+$## if $url;
+       if ($_trunk !~ m#^[a-z\+]+://#) {
+               $_trunk = '/' . $_trunk if ($_trunk !~ m#^/#);
+               unless ($url) {
+                       print STDERR "E: '$_trunk' is not a complete URL ",
+                               "and a separate URL is not specified\n";
+                       exit 1;
+               }
+               $_trunk = $url . $_trunk;
+       }
+       if ($GIT_SVN eq 'git-svn') {
+               print "GIT_SVN_ID set to 'trunk' for $_trunk\n";
+               $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
+       }
+       init_vars();
+       init($_trunk);
+       complete_url_ls_init($url, $_branches, '--branches/-b', '');
+       complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/');
+}
+
+sub multi_fetch {
+       # try to do trunk first, since branches/tags
+       # may be descended from it.
+       if (-e "$GIT_DIR/svn/trunk/info/url") {
+               fetch_child_id('trunk', @_);
+       }
+       rec_fetch('', "$GIT_DIR/svn", @_);
+}
+
+sub show_log {
+       my (@args) = @_;
+       my ($r_min, $r_max);
+       my $r_last = -1; # prevent dupes
+       rload_authors() if $_authors;
+       if (defined $TZ) {
+               $ENV{TZ} = $TZ;
+       } else {
+               delete $ENV{TZ};
+       }
+       if (defined $_revision) {
+               if ($_revision =~ /^(\d+):(\d+)$/) {
+                       ($r_min, $r_max) = ($1, $2);
+               } elsif ($_revision =~ /^\d+$/) {
+                       $r_min = $r_max = $_revision;
+               } else {
+                       print STDERR "-r$_revision is not supported, use ",
+                               "standard \'git log\' arguments instead\n";
+                       exit 1;
+               }
+       }
+
+       my $pid = open(my $log,'-|');
+       defined $pid or croak $!;
+       if (!$pid) {
+               exec(git_svn_log_cmd($r_min,$r_max), @args) or croak $!;
+       }
+       setup_pager();
+       my (@k, $c, $d);
+
+       while (<$log>) {
+               if (/^commit ($sha1_short)/o) {
+                       my $cmt = $1;
+                       if ($c && cmt_showable($c) && $c->{r} != $r_last) {
+                               $r_last = $c->{r};
+                               process_commit($c, $r_min, $r_max, \@k) or
+                                                               goto out;
+                       }
+                       $d = undef;
+                       $c = { c => $cmt };
+               } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
+                       get_author_info($c, $1, $2, $3);
+               } elsif (/^(?:tree|parent|committer) /) {
+                       # ignore
+               } elsif (/^:\d{6} \d{6} $sha1_short/o) {
+                       push @{$c->{raw}}, $_;
+               } elsif (/^diff /) {
+                       $d = 1;
+                       push @{$c->{diff}}, $_;
+               } elsif ($d) {
+                       push @{$c->{diff}}, $_;
+               } elsif (/^    (git-svn-id:.+)$/) {
+                       (undef, $c->{r}, undef) = extract_metadata($1);
+               } elsif (s/^    //) {
+                       push @{$c->{l}}, $_;
+               }
+       }
+       if ($c && defined $c->{r} && $c->{r} != $r_last) {
+               $r_last = $c->{r};
+               process_commit($c, $r_min, $r_max, \@k);
+       }
+       if (@k) {
+               my $swap = $r_max;
+               $r_max = $r_min;
+               $r_min = $swap;
+               process_commit($_, $r_min, $r_max) foreach reverse @k;
+       }
+out:
+       close $log;
+       print '-' x72,"\n" unless $_incremental || $_oneline;
+}
+
 ########################### utility functions #########################
 
+sub cmt_showable {
+       my ($c) = @_;
+       return 1 if defined $c->{r};
+       if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
+                               $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
+               my @msg = safe_qx(qw/git-cat-file commit/, $c->{c});
+               shift @msg while ($msg[0] ne "\n");
+               shift @msg;
+               @{$c->{l}} = grep !/^git-svn-id: /, @msg;
+
+               (undef, $c->{r}, undef) = extract_metadata(
+                               (grep(/^git-svn-id: /, @msg))[-1]);
+       }
+       return defined $c->{r};
+}
+
+sub git_svn_log_cmd {
+       my ($r_min, $r_max) = @_;
+       my @cmd = (qw/git-log --abbrev-commit --pretty=raw
+                       --default/, "refs/remotes/$GIT_SVN");
+       push @cmd, '--summary' if $_verbose;
+       return @cmd unless defined $r_max;
+       if ($r_max == $r_min) {
+               push @cmd, '--max-count=1';
+               if (my $c = revdb_get($REVDB, $r_max)) {
+                       push @cmd, $c;
+               }
+       } else {
+               my ($c_min, $c_max);
+               $c_max = revdb_get($REVDB, $r_max);
+               $c_min = revdb_get($REVDB, $r_min);
+               if ($c_min && $c_max) {
+                       if ($r_max > $r_max) {
+                               push @cmd, "$c_min..$c_max";
+                       } else {
+                               push @cmd, "$c_max..$c_min";
+                       }
+               } elsif ($r_max > $r_min) {
+                       push @cmd, $c_max;
+               } else {
+                       push @cmd, $c_min;
+               }
+       }
+       return @cmd;
+}
+
+sub fetch_child_id {
+       my $id = shift;
+       print "Fetching $id\n";
+       my $ref = "$GIT_DIR/refs/remotes/$id";
+       my $ca = file_to_s($ref) if (-r $ref);
+       defined(my $pid = fork) or croak $!;
+       if (!$pid) {
+               $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
+               init_vars();
+               fetch(@_);
+               exit 0;
+       }
+       waitpid $pid, 0;
+       croak $? if $?;
+       return unless $_repack || -r $ref;
+
+       my $cb = file_to_s($ref);
+
+       defined($pid = open my $fh, '-|') or croak $!;
+       my $url = file_to_s("$GIT_DIR/svn/$id/info/url");
+       $url = qr/\Q$url\E/;
+       if (!$pid) {
+               exec qw/git-rev-list --pretty=raw/,
+                               $ca ? "$ca..$cb" : $cb or croak $!;
+       }
+       while (<$fh>) {
+               if (/^    git-svn-id: $url\@\d+ [a-f0-9\-]+$/) {
+                       check_repack();
+               } elsif (/^    git-svn-id: \S+\@\d+ [a-f0-9\-]+$/) {
+                       last;
+               }
+       }
+       close $fh;
+}
+
+sub rec_fetch {
+       my ($pfx, $p, @args) = @_;
+       my @dir;
+       foreach (sort <$p/*>) {
+               if (-r "$_/info/url") {
+                       $pfx .= '/' if $pfx && $pfx !~ m!/$!;
+                       my $id = $pfx . basename $_;
+                       next if $id eq 'trunk';
+                       fetch_child_id($id, @args);
+               } elsif (-d $_) {
+                       push @dir, $_;
+               }
+       }
+       foreach (@dir) {
+               my $x = $_;
+               $x =~ s!^\Q$GIT_DIR\E/svn/!!;
+               rec_fetch($x, $_);
+       }
+}
+
+sub complete_url_ls_init {
+       my ($url, $var, $switch, $pfx) = @_;
+       unless ($var) {
+               print STDERR "W: $switch not specified\n";
+               return;
+       }
+       $var =~ s#/+$##;
+       if ($var !~ m#^[a-z\+]+://#) {
+               $var = '/' . $var if ($var !~ m#^/#);
+               unless ($url) {
+                       print STDERR "E: '$var' is not a complete URL ",
+                               "and a separate URL is not specified\n";
+                       exit 1;
+               }
+               $var = $url . $var;
+       }
+       chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
+                               : safe_qx(qw/svn ls --non-interactive/, $var));
+       my $old = $GIT_SVN;
+       defined(my $pid = fork) or croak $!;
+       if (!$pid) {
+               foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
+                       $u =~ s#/+$##;
+                       if ($u !~ m!\Q$var\E/(.+)$!) {
+                               print STDERR "W: Unrecognized URL: $u\n";
+                               die "This should never happen\n";
+                       }
+                       my $id = $pfx.$1;
+                       print "init $u => $id\n";
+                       $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
+                       init_vars();
+                       init($u);
+               }
+               exit 0;
+       }
+       waitpid $pid, 0;
+       croak $? if $?;
+}
+
+sub common_prefix {
+       my $paths = shift;
+       my %common;
+       foreach (@$paths) {
+               my @tmp = split m#/#, $_;
+               my $p = '';
+               while (my $x = shift @tmp) {
+                       $p .= "/$x";
+                       $common{$p} ||= 0;
+                       $common{$p}++;
+               }
+       }
+       foreach (sort {length $b <=> length $a} keys %common) {
+               if ($common{$_} == @$paths) {
+                       return $_;
+               }
+       }
+       return '';
+}
+
+# this isn't funky-filename safe, but good enough for now...
+sub graft_file_copy_cmd {
+       my ($grafts, $l_map, $u) = @_;
+       my $paths = $l_map->{$u};
+       my $pfx = common_prefix([keys %$paths]);
+       $SVN_URL ||= $u.$pfx;
+       my $pid = open my $fh, '-|';
+       defined $pid or croak $!;
+       unless ($pid) {
+               my @exec = qw/svn log -v/;
+               push @exec, "-r$_revision" if defined $_revision;
+               exec @exec, $u.$pfx or croak $!;
+       }
+       my ($r, $mp) = (undef, undef);
+       while (<$fh>) {
+               chomp;
+               if (/^\-{72}$/) {
+                       $mp = $r = undef;
+               } elsif (/^r(\d+) \| /) {
+                       $r = $1 unless defined $r;
+               } elsif (/^Changed paths:/) {
+                       $mp = 1;
+               } elsif ($mp && m#^   [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
+                       my ($p1, $p0, $r0) = ($1, $2, $3);
+                       my $c = find_graft_path_commit($paths, $p1, $r);
+                       next unless $c;
+                       find_graft_path_parents($grafts, $paths, $c, $p0, $r0);
+               }
+       }
+}
+
+sub graft_file_copy_lib {
+       my ($grafts, $l_map, $u) = @_;
+       my $tree_paths = $l_map->{$u};
+       my $pfx = common_prefix([keys %$tree_paths]);
+       my ($repo, $path) = repo_path_split($u.$pfx);
+       $SVN_LOG ||= libsvn_connect($repo);
+       $SVN ||= libsvn_connect($repo);
+
+       my ($base, $head) = libsvn_parse_revision();
+       my $inc = 1000;
+       my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
+       my $eh = $SVN::Error::handler;
+       $SVN::Error::handler = \&libsvn_skip_unknown_revs;
+       while (1) {
+               my $pool = SVN::Pool->new;
+               $SVN_LOG->get_log( "/$path", $min, $max, 0, 1, 1,
+                       sub {
+                               libsvn_graft_file_copies($grafts, $tree_paths,
+                                                       $path, @_);
+                       }, $pool);
+               $pool->clear;
+               last if ($max >= $head);
+               $min = $max + 1;
+               $max += $inc;
+               $max = $head if ($max > $head);
+       }
+       $SVN::Error::handler = $eh;
+}
+
+sub process_merge_msg_matches {
+       my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
+       my (@strong, @weak);
+       foreach (@matches) {
+               # merging with ourselves is not interesting
+               next if $_ eq $p;
+               if ($l_map->{$u}->{$_}) {
+                       push @strong, $_;
+               } else {
+                       push @weak, $_;
+               }
+       }
+       foreach my $w (@weak) {
+               last if @strong;
+               # no exact match, use branch name as regexp.
+               my $re = qr/\Q$w\E/i;
+               foreach (keys %{$l_map->{$u}}) {
+                       if (/$re/) {
+                               push @strong, $_;
+                               last;
+                       }
+               }
+               last if @strong;
+               $w = basename($w);
+               $re = qr/\Q$w\E/i;
+               foreach (keys %{$l_map->{$u}}) {
+                       if (/$re/) {
+                               push @strong, $_;
+                               last;
+                       }
+               }
+       }
+       my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
+                                       \s(?:[a-f\d\-]+)$/xsm);
+       unless (defined $rev) {
+               ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
+                                       \@(?:[a-f\d\-]+)/xsm);
+               return unless defined $rev;
+       }
+       foreach my $m (@strong) {
+               my ($r0, $s0) = find_rev_before($rev, $m);
+               $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
+       }
+}
+
+sub graft_merge_msg {
+       my ($grafts, $l_map, $u, $p, @re) = @_;
+
+       my $x = $l_map->{$u}->{$p};
+       my $rl = rev_list_raw($x);
+       while (my $c = next_rev_list_entry($rl)) {
+               foreach my $re (@re) {
+                       my (@br) = ($c->{m} =~ /$re/g);
+                       next unless @br;
+                       process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
+               }
+       }
+}
+
 sub read_uuid {
        return if $SVN_UUID;
-       my $info = shift || svn_info('.');
-       $SVN_UUID = $info->{'Repository UUID'} or
+       if ($_use_lib) {
+               my $pool = SVN::Pool->new;
+               $SVN_UUID = $SVN->get_uuid($pool);
+               $pool->clear;
+       } else {
+               my $info = shift || svn_info('.');
+               $SVN_UUID = $info->{'Repository UUID'} or
                                        croak "Repository UUID unreadable\n";
-       s_to_file($SVN_UUID,"$GIT_DIR/$GIT_SVN/info/uuid");
+       }
+}
+
+sub quiet_run {
+       my $pid = fork;
+       defined $pid or croak $!;
+       if (!$pid) {
+               open my $null, '>', '/dev/null' or croak $!;
+               open STDERR, '>&', $null or croak $!;
+               open STDOUT, '>&', $null or croak $!;
+               exec @_ or croak $!;
+       }
+       waitpid $pid, 0;
+       return $?;
+}
+
+sub repo_path_split {
+       my $full_url = shift;
+       $full_url =~ s#/+$##;
+
+       foreach (@repo_path_split_cache) {
+               if ($full_url =~ s#$_##) {
+                       my $u = $1;
+                       $full_url =~ s#^/+##;
+                       return ($u, $full_url);
+               }
+       }
+
+       my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
+       $path =~ s#^/+##;
+       my @paths = split(m#/+#, $path);
+
+       if ($_use_lib) {
+               while (1) {
+                       $SVN = libsvn_connect($url);
+                       last if (defined $SVN &&
+                               defined eval { $SVN->get_latest_revnum });
+                       my $n = shift @paths || last;
+                       $url .= "/$n";
+               }
+       } else {
+               while (quiet_run(qw/svn ls --non-interactive/, $url)) {
+                       my $n = shift @paths || last;
+                       $url .= "/$n";
+               }
+       }
+       push @repo_path_split_cache, qr/^(\Q$url\E)/;
+       $path = join('/',@paths);
+       return ($url, $path);
 }
 
 sub setup_git_svn {
@@ -354,24 +1061,21 @@ sub setup_git_svn {
        unless (-d $GIT_DIR) {
                croak "GIT_DIR=$GIT_DIR does not exist!\n";
        }
-       mkpath(["$GIT_DIR/$GIT_SVN"]);
-       mkpath(["$GIT_DIR/$GIT_SVN/info"]);
-       mkpath([$REV_DIR]);
-       s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
+       mkpath([$GIT_SVN_DIR]);
+       mkpath(["$GIT_SVN_DIR/info"]);
+       open my $fh, '>>',$REVDB or croak $!;
+       close $fh;
+       s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
 
-       open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
-       print $fd '.svn',"\n";
-       close $fd or croak $!;
 }
 
 sub assert_svn_wc_clean {
-       my ($svn_rev, $treeish) = @_;
+       return if $_use_lib;
+       my ($svn_rev) = @_;
        croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
-       croak "$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
        my $lcr = svn_info('.')->{'Last Changed Rev'};
        if ($svn_rev != $lcr) {
                print STDERR "Checking for copy-tree ... ";
-               # use
                my @diff = grep(/^Index: /,(safe_qx(qw(svn diff),
                                                "-r$lcr:$svn_rev")));
                if (@diff) {
@@ -387,10 +1091,9 @@ sub assert_svn_wc_clean {
                print STDERR $_ foreach @status;
                croak;
        }
-       assert_tree($treeish);
 }
 
-sub assert_tree {
+sub get_tree_from_treeish {
        my ($treeish) = @_;
        croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
        chomp(my $type = `git-cat-file -t $treeish`);
@@ -407,23 +1110,26 @@ sub assert_tree {
        } else {
                die "$treeish is a $type, expected tree, tag or commit\n";
        }
+       return $expected;
+}
+
+sub assert_tree {
+       return if $_use_lib;
+       my ($treeish) = @_;
+       my $expected = get_tree_from_treeish($treeish);
 
-       my $old_index = $ENV{GIT_INDEX_FILE};
        my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
        if (-e $tmpindex) {
                unlink $tmpindex or croak $!;
        }
-       $ENV{GIT_INDEX_FILE} = $tmpindex;
-       git_addremove();
+       my $old_index = set_index($tmpindex);
+       index_changes(1);
        chomp(my $tree = `git-write-tree`);
-       if ($old_index) {
-               $ENV{GIT_INDEX_FILE} = $old_index;
-       } else {
-               delete $ENV{GIT_INDEX_FILE};
-       }
+       restore_index($old_index);
        if ($tree ne $expected) {
                croak "Tree mismatch, Got: $tree, Expected: $expected\n";
        }
+       unlink $tmpindex;
 }
 
 sub parse_diff_tree {
@@ -463,7 +1169,7 @@ sub parse_diff_tree {
                        croak "Error parsing $_\n";
                }
        }
-       close $diff_fh or croak $!;
+       close $diff_fh or croak $?;
 
        return \@mods;
 }
@@ -557,24 +1263,31 @@ sub precommit_check {
        }
 }
 
-sub svn_checkout_tree {
-       my ($svn_rev, $treeish) = @_;
-       my $from = file_to_s("$REV_DIR/$svn_rev");
-       assert_svn_wc_clean($svn_rev,$from);
+
+sub get_diff {
+       my ($from, $treeish) = @_;
+       assert_tree($from);
        print "diff-tree $from $treeish\n";
        my $pid = open my $diff_fh, '-|';
        defined $pid or croak $!;
        if ($pid == 0) {
-               my @diff_tree = qw(git-diff-tree -z -r -C);
+               my @diff_tree = qw(git-diff-tree -z -r);
+               if ($_cp_similarity) {
+                       push @diff_tree, "-C$_cp_similarity";
+               } else {
+                       push @diff_tree, '-C';
+               }
                push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
                push @diff_tree, "-l$_l" if defined $_l;
                exec(@diff_tree, $from, $treeish) or croak $!;
        }
-       my $mods = parse_diff_tree($diff_fh);
-       unless (@$mods) {
-               # git can do empty commits, but SVN doesn't allow it...
-               return $mods;
-       }
+       return parse_diff_tree($diff_fh);
+}
+
+sub svn_checkout_tree {
+       my ($from, $treeish) = @_;
+       my $mods = get_diff($from->{commit}, $treeish);
+       return $mods unless (scalar @$mods);
        my ($rm, $add) = precommit_check($mods);
 
        my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
@@ -597,12 +1310,12 @@ sub svn_checkout_tree {
                } elsif ($m->{chg} eq 'T') {
                        sys(qw(svn rm --force),$m->{file_b});
                        apply_mod_line_blob($m);
-                       sys(qw(svn add --force), $m->{file_b});
+                       sys(qw(svn add), $m->{file_b});
                        svn_check_prop_executable($m);
                } elsif ($m->{chg} eq 'A') {
                        svn_ensure_parent_path( $m->{file_b} );
                        apply_mod_line_blob($m);
-                       sys(qw(svn add --force), $m->{file_b});
+                       sys(qw(svn add), $m->{file_b});
                        svn_check_prop_executable($m);
                } else {
                        croak "Invalid chg: $m->{chg}\n";
@@ -617,6 +1330,23 @@ sub svn_checkout_tree {
        return $mods;
 }
 
+sub libsvn_checkout_tree {
+       my ($from, $treeish, $ed) = @_;
+       my $mods = get_diff($from, $treeish);
+       return $mods unless (scalar @$mods);
+       my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
+       foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
+               my $f = $m->{chg};
+               if (defined $o{$f}) {
+                       $ed->$f($m);
+               } else {
+                       croak "Invalid change type: $f\n";
+               }
+       }
+       $ed->rmdirs if $_rmdir;
+       return $mods;
+}
+
 # svn ls doesn't work with respect to the current working tree, but what's
 # in the repository.  There's not even an option for it... *sigh*
 # (added files don't show up and removed files remain in the ls listing)
@@ -655,12 +1385,12 @@ sub handle_rmdir {
        }
 }
 
-sub svn_commit_tree {
-       my ($svn_rev, $commit) = @_;
-       my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
+sub get_commit_message {
+       my ($commit, $commit_msg) = (@_);
        my %log_msg = ( msg => '' );
        open my $msg, '>', $commit_msg or croak $!;
 
+       print "commit: $commit\n";
        chomp(my $type = `git-cat-file -t $commit`);
        if ($type eq 'commit') {
                my $pid = open my $msg_fh, '-|';
@@ -680,7 +1410,7 @@ sub svn_commit_tree {
                                print $msg $_ or croak $!;
                        }
                }
-               close $msg_fh or croak $!;
+               close $msg_fh or croak $?;
        }
        close $msg or croak $!;
 
@@ -694,43 +1424,93 @@ sub svn_commit_tree {
        { local $/; chomp($log_msg{msg} = <$msg>); }
        close $msg or croak $!;
 
-       my ($oneline) = ($log_msg{msg} =~ /([^\n\r]+)/);
+       return \%log_msg;
+}
+
+sub svn_commit_tree {
+       my ($last, $commit) = @_;
+       my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
+       my $log_msg = get_commit_message($commit, $commit_msg);
+       my ($oneline) = ($log_msg->{msg} =~ /([^\n\r]+)/);
        print "Committing $commit: $oneline\n";
 
+       if (defined $LC_ALL) {
+               $ENV{LC_ALL} = $LC_ALL;
+       } else {
+               delete $ENV{LC_ALL};
+       }
        my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
-       my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
+       $ENV{LC_ALL} = 'C';
        unlink $commit_msg;
-       defined $committed or croak
+       my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
+       if (!defined $committed) {
+               my $out = join("\n",@ci_output);
+               print STDERR "W: Trouble parsing \`svn commit' output:\n\n",
+                               $out, "\n\nAssuming English locale...";
+               ($committed) = ($out =~ /^Committed revision \d+\./sm);
+               defined $committed or die " FAILED!\n",
                        "Commit output failed to parse committed revision!\n",
-                       join("\n",@ci_output),"\n";
-       my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
+               print STDERR " OK\n";
+       }
 
        my @svn_up = qw(svn up);
        push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
-       if ($rev_committed == ($svn_rev + 1)) {
-               push @svn_up, "-r$rev_committed";
+       if ($_optimize_commits && ($committed == ($last->{revision} + 1))) {
+               push @svn_up, "-r$committed";
                sys(@svn_up);
                my $info = svn_info('.');
                my $date = $info->{'Last Changed Date'} or die "Missing date\n";
-               if ($info->{'Last Changed Rev'} != $rev_committed) {
-                       croak "$info->{'Last Changed Rev'} != $rev_committed\n"
+               if ($info->{'Last Changed Rev'} != $committed) {
+                       croak "$info->{'Last Changed Rev'} != $committed\n"
                }
                my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
                                        /(\d{4})\-(\d\d)\-(\d\d)\s
                                         (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
                                         or croak "Failed to parse date: $date\n";
-               $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
-               $log_msg{author} = $info->{'Last Changed Author'};
-               $log_msg{revision} = $rev_committed;
-               $log_msg{msg} .= "\n";
-               my $parent = file_to_s("$REV_DIR/$svn_rev");
-               git_commit(\%log_msg, $parent, $commit);
-               return $rev_committed;
+               $log_msg->{date} = "$tz $Y-$m-$d $H:$M:$S";
+               $log_msg->{author} = $info->{'Last Changed Author'};
+               $log_msg->{revision} = $committed;
+               $log_msg->{msg} .= "\n";
+               $log_msg->{parents} = [ $last->{commit} ];
+               $log_msg->{commit} = git_commit($log_msg, $commit);
+               return $log_msg;
        }
        # resync immediately
-       push @svn_up, "-r$svn_rev";
+       push @svn_up, "-r$last->{revision}";
        sys(@svn_up);
-       return fetch("$rev_committed=$commit")->{revision};
+       return fetch("$committed=$commit");
+}
+
+sub rev_list_raw {
+       my (@args) = @_;
+       my $pid = open my $fh, '-|';
+       defined $pid or croak $!;
+       if (!$pid) {
+               exec(qw/git-rev-list --pretty=raw/, @args) or croak $!;
+       }
+       return { fh => $fh, t => { } };
+}
+
+sub next_rev_list_entry {
+       my $rl = shift;
+       my $fh = $rl->{fh};
+       my $x = $rl->{t};
+       while (<$fh>) {
+               if (/^commit ($sha1)$/o) {
+                       if ($x->{c}) {
+                               $rl->{t} = { c => $1 };
+                               return $x;
+                       } else {
+                               $x->{c} = $1;
+                       }
+               } elsif (/^parent ($sha1)$/o) {
+                       $x->{p}->{$1} = 1;
+               } elsif (s/^    //) {
+                       $x->{m} ||= '';
+                       $x->{m} .= $_;
+               }
+       }
+       return ($x != $rl->{t}) ? $x : undef;
 }
 
 # read the entire log into a temporary file (which is removed ASAP)
@@ -745,7 +1525,7 @@ sub svn_log_raw {
                exec (qw(svn log), @log_args) or croak $!
        }
        waitpid $pid, 0;
-       croak if $?;
+       croak $? if $?;
        seek $log_fh, 0, 0 or croak $!;
        return { state => 'sep', fh => $log_fh };
 }
@@ -844,26 +1624,108 @@ sub svn_info {
                        push @{$ret->{-order}}, $1;
                }
        }
-       close $info_fh or croak $!;
+       close $info_fh or croak $?;
        return $ret;
 }
 
 sub sys { system(@_) == 0 or croak $? }
 
-sub git_addremove {
-       system( "git-diff-files --name-only -z ".
-                               " | git-update-index --remove -z --stdin && ".
-               "git-ls-files -z --others ".
-                       "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
-                               " | git-update-index --add -z --stdin"
-               ) == 0 or croak $?
+sub eol_cp {
+       my ($from, $to) = @_;
+       my $es = svn_propget_base('svn:eol-style', $to);
+       open my $rfd, '<', $from or croak $!;
+       binmode $rfd or croak $!;
+       open my $wfd, '>', $to or croak $!;
+       binmode $wfd or croak $!;
+       eol_cp_fd($rfd, $wfd, $es);
+       close $rfd or croak $!;
+       close $wfd or croak $!;
 }
 
-sub s_to_file {
-       my ($str, $file, $mode) = @_;
-       open my $fd,'>',$file or croak $!;
-       print $fd $str,"\n" or croak $!;
-       close $fd or croak $!;
+sub eol_cp_fd {
+       my ($rfd, $wfd, $es) = @_;
+       my $eol = defined $es ? $EOL{$es} : undef;
+       my $buf;
+       use bytes;
+       while (1) {
+               my ($r, $w, $t);
+               defined($r = sysread($rfd, $buf, 4096)) or croak $!;
+               return unless $r;
+               if ($eol) {
+                       if ($buf =~ /\015$/) {
+                               my $c;
+                               defined($r = sysread($rfd,$c,1)) or croak $!;
+                               $buf .= $c if $r > 0;
+                       }
+                       $buf =~ s/(?:\015\012|\015|\012)/$eol/gs;
+                       $r = length($buf);
+               }
+               for ($w = 0; $w < $r; $w += $t) {
+                       $t = syswrite($wfd, $buf, $r - $w, $w) or croak $!;
+               }
+       }
+       no bytes;
+}
+
+sub do_update_index {
+       my ($z_cmd, $cmd, $no_text_base) = @_;
+
+       my $z = open my $p, '-|';
+       defined $z or croak $!;
+       unless ($z) { exec @$z_cmd or croak $! }
+
+       my $pid = open my $ui, '|-';
+       defined $pid or croak $!;
+       unless ($pid) {
+               exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
+       }
+       local $/ = "\0";
+       while (my $x = <$p>) {
+               chomp $x;
+               if (!$no_text_base && lstat $x && ! -l _ &&
+                               svn_propget_base('svn:keywords', $x)) {
+                       my $mode = -x _ ? 0755 : 0644;
+                       my ($v,$d,$f) = File::Spec->splitpath($x);
+                       my $tb = File::Spec->catfile($d, '.svn', 'tmp',
+                                               'text-base',"$f.svn-base");
+                       $tb =~ s#^/##;
+                       unless (-f $tb) {
+                               $tb = File::Spec->catfile($d, '.svn',
+                                               'text-base',"$f.svn-base");
+                               $tb =~ s#^/##;
+                       }
+                       unlink $x or croak $!;
+                       eol_cp($tb, $x);
+                       chmod(($mode &~ umask), $x) or croak $!;
+               }
+               print $ui $x,"\0";
+       }
+       close $ui or croak $?;
+}
+
+sub index_changes {
+       return if $_use_lib;
+
+       if (!-f "$GIT_SVN_DIR/info/exclude") {
+               open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak $!;
+               print $fd '.svn',"\n";
+               close $fd or croak $!;
+       }
+       my $no_text_base = shift;
+       do_update_index([qw/git-diff-files --name-only -z/],
+                       'remove',
+                       $no_text_base);
+       do_update_index([qw/git-ls-files -z --others/,
+                               "--exclude-from=$GIT_SVN_DIR/info/exclude"],
+                       'add',
+                       $no_text_base);
+}
+
+sub s_to_file {
+       my ($str, $file, $mode) = @_;
+       open my $fd,'>',$file or croak $!;
+       print $fd $str,"\n" or croak $!;
+       close $fd or croak $!;
        chmod ($mode &~ umask, $file) if (defined $mode);
 }
 
@@ -878,10 +1740,9 @@ sub file_to_s {
 }
 
 sub assert_revision_unknown {
-       my $revno = shift;
-       if (-f "$REV_DIR/$revno") {
-               croak "$REV_DIR/$revno already exists! ",
-                               "Why are we refetching it?";
+       my $r = shift;
+       if (my $c = revdb_get($REVDB, $r)) {
+               croak "$r = $c already exists! Why are we refetching it?";
        }
 }
 
@@ -897,103 +1758,92 @@ sub trees_eq {
        return 1;
 }
 
-sub assert_revision_eq_or_unknown {
-       my ($revno, $commit) = @_;
-       if (-f "$REV_DIR/$revno") {
-               my $current = file_to_s("$REV_DIR/$revno");
-               if (($commit ne $current) && !trees_eq($commit, $current)) {
-                       croak "$REV_DIR/$revno already exists!\n",
-                               "current: $current\nexpected: $commit\n";
-               }
-               return;
-       }
-}
-
 sub git_commit {
        my ($log_msg, @parents) = @_;
        assert_revision_unknown($log_msg->{revision});
-       my $out_fh = IO::File->new_tmpfile or croak $!;
-
        map_tree_joins() if (@_branch_from && !%tree_map);
 
+       my (@tmp_parents, @exec_parents, %seen_parent);
+       if (my $lparents = $log_msg->{parents}) {
+               @tmp_parents = @$lparents
+       }
        # commit parents can be conditionally bound to a particular
        # svn revision via: "svn_revno=commit_sha1", filter them out here:
-       my @exec_parents;
        foreach my $p (@parents) {
                next unless defined $p;
                if ($p =~ /^(\d+)=($sha1_short)$/o) {
                        if ($1 == $log_msg->{revision}) {
-                               push @exec_parents, $2;
+                               push @tmp_parents, $2;
                        }
                } else {
-                       push @exec_parents, $p if $p =~ /$sha1_short/o;
+                       push @tmp_parents, $p if $p =~ /$sha1_short/o;
                }
        }
+       my $tree = $log_msg->{tree};
+       if (!defined $tree) {
+               my $index = set_index($GIT_SVN_INDEX);
+               index_changes();
+               chomp($tree = `git-write-tree`);
+               croak $? if $?;
+               restore_index($index);
+       }
+       if (exists $tree_map{$tree}) {
+               push @tmp_parents, @{$tree_map{$tree}};
+       }
+       foreach (@tmp_parents) {
+               next if $seen_parent{$_};
+               $seen_parent{$_} = 1;
+               push @exec_parents, $_;
+               # MAXPARENT is defined to 16 in commit-tree.c:
+               last if @exec_parents > 16;
+       }
 
-       my $pid = fork;
-       defined $pid or croak $!;
+       defined(my $pid = open my $out_fh, '-|') or croak $!;
        if ($pid == 0) {
-               $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
-               git_addremove();
-               chomp(my $tree = `git-write-tree`);
-               croak if $?;
-               if (exists $tree_map{$tree}) {
-                       my %seen_parent = map { $_ => 1 } @exec_parents;
-                       foreach (@{$tree_map{$tree}}) {
-                               # MAXPARENT is defined to 16 in commit-tree.c:
-                               if ($seen_parent{$_} || @exec_parents > 16) {
-                                       next;
-                               }
-                               push @exec_parents, $_;
-                               $seen_parent{$_} = 1;
-                       }
-               }
                my $msg_fh = IO::File->new_tmpfile or croak $!;
                print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
                                        "$SVN_URL\@$log_msg->{revision}",
                                        " $SVN_UUID\n" or croak $!;
                $msg_fh->flush == 0 or croak $!;
                seek $msg_fh, 0, 0 or croak $!;
-
                set_commit_env($log_msg);
-
                my @exec = ('git-commit-tree',$tree);
                push @exec, '-p', $_  foreach @exec_parents;
                open STDIN, '<&', $msg_fh or croak $!;
-               open STDOUT, '>&', $out_fh or croak $!;
                exec @exec or croak $!;
        }
-       waitpid($pid,0);
-       croak if $?;
-
-       $out_fh->flush == 0 or croak $!;
-       seek $out_fh, 0, 0 or croak $!;
        chomp(my $commit = do { local $/; <$out_fh> });
+       close $out_fh or croak $?;
        if ($commit !~ /^$sha1$/o) {
                croak "Failed to commit, invalid sha1: $commit\n";
        }
        my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
        if (my $primary_parent = shift @exec_parents) {
-               $pid = fork;
-               defined $pid or croak $!;
-               if (!$pid) {
-                       close STDERR;
-                       close STDOUT;
-                       exec 'git-rev-parse','--verify',
-                                               "refs/remotes/$GIT_SVN^0";
-               }
-               waitpid $pid, 0;
+               quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0");
                push @update_ref, $primary_parent unless $?;
        }
        sys(@update_ref);
-       sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
+       revdb_set($REVDB, $log_msg->{revision}, $commit);
+
+       # this output is read via pipe, do not change:
        print "r$log_msg->{revision} = $commit\n";
+       check_repack();
        return $commit;
 }
 
+sub check_repack {
+       if ($_repack && (--$_repack_nr == 0)) {
+               $_repack_nr = $_repack;
+               sys("git repack $_repack_flags");
+       }
+}
+
 sub set_commit_env {
        my ($log_msg) = @_;
        my $author = $log_msg->{author};
+       if (!defined $author || length $author == 0) {
+               $author = '(no author)';
+       }
        my ($name,$email) = defined $users{$author} ?  @{$users{$author}}
                                : ($author,"$author\@$SVN_UUID");
        $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
@@ -1036,7 +1886,7 @@ sub blob_to_file {
 
        if ($pid == 0) {
                open STDOUT, '>&', $blob_fh or croak $!;
-               exec('git-cat-file','blob',$blob);
+               exec('git-cat-file','blob',$blob) or croak $!;
        }
        waitpid $pid, 0;
        croak $? if $?;
@@ -1048,7 +1898,7 @@ sub safe_qx {
        my $pid = open my $child, '-|';
        defined $pid or croak $!;
        if ($pid == 0) {
-               exec(@_) or croak $?;
+               exec(@_) or croak $!;
        }
        my @ret = (<$child>);
        close $child or croak $?;
@@ -1066,6 +1916,9 @@ sub svn_compat_check {
        if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
                $_svn_co_url_revs = 1;
        }
+       if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
+               $_svn_pg_peg_revs = 1;
+       }
 
        # I really, really hope nobody hits this...
        unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) {
@@ -1090,12 +1943,17 @@ sub svn_cmd_checkout {
 }
 
 sub check_upgrade_needed {
+       if (!-r $REVDB) {
+               -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
+               open my $fh, '>>',$REVDB or croak $!;
+               close $fh;
+       }
        my $old = eval {
                my $pid = open my $child, '-|';
                defined $pid or croak $!;
                if ($pid == 0) {
                        close STDERR;
-                       exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
+                       exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
                }
                my @ret = (<$child>);
                close $child or croak $?;
@@ -1113,26 +1971,44 @@ sub check_upgrade_needed {
 # fills %tree_map with a reverse mapping of trees to commits.  Useful
 # for finding parents to commit on.
 sub map_tree_joins {
+       my %seen;
        foreach my $br (@_branch_from) {
                my $pid = open my $pipe, '-|';
                defined $pid or croak $!;
                if ($pid == 0) {
-                       exec(qw(git-rev-list --pretty=raw), $br) or croak $?;
+                       exec(qw(git-rev-list --topo-order --pretty=raw), $br)
+                                                               or croak $!;
                }
                while (<$pipe>) {
                        if (/^commit ($sha1)$/o) {
                                my $commit = $1;
+
+                               # if we've seen a commit,
+                               # we've seen its parents
+                               last if $seen{$commit};
                                my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
                                unless (defined $tree) {
                                        die "Failed to parse commit $commit\n";
                                }
                                push @{$tree_map{$tree}}, $commit;
+                               $seen{$commit} = 1;
                        }
                }
-               close $pipe or croak $?;
+               close $pipe; # we could be breaking the pipe early
        }
 }
 
+sub load_all_refs {
+       if (@_branch_from) {
+               print STDERR '--branch|-b parameters are ignored when ',
+                       "--branch-all-refs|-B is passed\n";
+       }
+
+       # don't worry about rev-list on non-commit objects/tags,
+       # it shouldn't blow up if a ref is a blob or tree...
+       chomp(@_branch_from = `git-rev-parse --symbolic --all`);
+}
+
 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
 sub load_authors {
        open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
@@ -1145,6 +2021,994 @@ sub load_authors {
        close $authors or croak $!;
 }
 
+sub rload_authors {
+       open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
+       while (<$authors>) {
+               chomp;
+               next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
+               my ($user, $name, $email) = ($1, $2, $3);
+               $rusers{"$name <$email>"} = $user;
+       }
+       close $authors or croak $!;
+}
+
+sub svn_propget_base {
+       my ($p, $f) = @_;
+       $f .= '@BASE' if $_svn_pg_peg_revs;
+       return safe_qx(qw/svn propget/, $p, $f);
+}
+
+sub git_svn_each {
+       my $sub = shift;
+       foreach (`git-rev-parse --symbolic --all`) {
+               next unless s#^refs/remotes/##;
+               chomp $_;
+               next unless -f "$GIT_DIR/svn/$_/info/url";
+               &$sub($_);
+       }
+}
+
+sub migrate_revdb {
+       git_svn_each(sub {
+               my $id = shift;
+               defined(my $pid = fork) or croak $!;
+               if (!$pid) {
+                       $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
+                       init_vars();
+                       exit 0 if -r $REVDB;
+                       print "Upgrading svn => git mapping...\n";
+                       -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
+                       open my $fh, '>>',$REVDB or croak $!;
+                       close $fh;
+                       rebuild();
+                       print "Done upgrading. You may now delete the ",
+                               "deprecated $GIT_SVN_DIR/revs directory\n";
+                       exit 0;
+               }
+               waitpid $pid, 0;
+               croak $? if $?;
+       });
+}
+
+sub migration_check {
+       migrate_revdb() unless (-e $REVDB);
+       return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
+       print "Upgrading repository...\n";
+       unless (-d "$GIT_DIR/svn") {
+               mkdir "$GIT_DIR/svn" or croak $!;
+       }
+       print "Data from a previous version of git-svn exists, but\n\t",
+                               "$GIT_SVN_DIR\n\t(required for this version ",
+                               "($VERSION) of git-svn) does not.\n";
+
+       foreach my $x (`git-rev-parse --symbolic --all`) {
+               next unless $x =~ s#^refs/remotes/##;
+               chomp $x;
+               next unless -f "$GIT_DIR/$x/info/url";
+               my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
+               next unless $u;
+               my $dn = dirname("$GIT_DIR/svn/$x");
+               mkpath([$dn]) unless -d $dn;
+               rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
+       }
+       migrate_revdb() if (-d $GIT_SVN_DIR && !-w $REVDB);
+       print "Done upgrading.\n";
+}
+
+sub find_rev_before {
+       my ($r, $id, $eq_ok) = @_;
+       my $f = "$GIT_DIR/svn/$id/.rev_db";
+       return (undef,undef) unless -r $f;
+       --$r unless $eq_ok;
+       while ($r > 0) {
+               if (my $c = revdb_get($f, $r)) {
+                       return ($r, $c);
+               }
+               --$r;
+       }
+       return (undef, undef);
+}
+
+sub init_vars {
+       $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
+       $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
+       $REVDB = "$GIT_SVN_DIR/.rev_db";
+       $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
+       $SVN_URL = undef;
+       $SVN_WC = "$GIT_SVN_DIR/tree";
+}
+
+# convert GetOpt::Long specs for use by git-repo-config
+sub read_repo_config {
+       return unless -d $GIT_DIR;
+       my $opts = shift;
+       foreach my $o (keys %$opts) {
+               my $v = $opts->{$o};
+               my ($key) = ($o =~ /^([a-z\-]+)/);
+               $key =~ s/-//g;
+               my $arg = 'git-repo-config';
+               $arg .= ' --int' if ($o =~ /[:=]i$/);
+               $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
+               if (ref $v eq 'ARRAY') {
+                       chomp(my @tmp = `$arg --get-all svn.$key`);
+                       @$v = @tmp if @tmp;
+               } else {
+                       chomp(my $tmp = `$arg --get svn.$key`);
+                       if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
+                               $$v = $tmp;
+                       }
+               }
+       }
+}
+
+sub set_default_vals {
+       if (defined $_repack) {
+               $_repack = 1000 if ($_repack <= 0);
+               $_repack_nr = $_repack;
+               $_repack_flags ||= '-d';
+       }
+}
+
+sub read_grafts {
+       my $gr_file = shift;
+       my ($grafts, $comments) = ({}, {});
+       if (open my $fh, '<', $gr_file) {
+               my @tmp;
+               while (<$fh>) {
+                       if (/^($sha1)\s+/) {
+                               my $c = $1;
+                               if (@tmp) {
+                                       @{$comments->{$c}} = @tmp;
+                                       @tmp = ();
+                               }
+                               foreach my $p (split /\s+/, $_) {
+                                       $grafts->{$c}->{$p} = 1;
+                               }
+                       } else {
+                               push @tmp, $_;
+                       }
+               }
+               close $fh or croak $!;
+               @{$comments->{'END'}} = @tmp if @tmp;
+       }
+       return ($grafts, $comments);
+}
+
+sub write_grafts {
+       my ($grafts, $comments, $gr_file) = @_;
+
+       open my $fh, '>', $gr_file or croak $!;
+       foreach my $c (sort keys %$grafts) {
+               if ($comments->{$c}) {
+                       print $fh $_ foreach @{$comments->{$c}};
+               }
+               my $p = $grafts->{$c};
+               delete $p->{$c}; # commits are not self-reproducing...
+               my $pid = open my $ch, '-|';
+               defined $pid or croak $!;
+               if (!$pid) {
+                       exec(qw/git-cat-file commit/, $c) or croak $!;
+               }
+               while (<$ch>) {
+                       if (/^parent ([a-f\d]{40})/) {
+                               $p->{$1} = 1;
+                       } else {
+                               last unless /^\S/i;
+                       }
+               }
+               close $ch; # breaking the pipe
+               print $fh $c, ' ', join(' ', sort keys %$p),"\n";
+       }
+       if ($comments->{'END'}) {
+               print $fh $_ foreach @{$comments->{'END'}};
+       }
+       close $fh or croak $!;
+}
+
+sub read_url_paths {
+       my $l_map = {};
+       git_svn_each(sub { my $x = shift;
+                       my $url = file_to_s("$GIT_DIR/svn/$x/info/url");
+                       my ($u, $p) = repo_path_split($url);
+                       $l_map->{$u}->{$p} = $x;
+                       });
+       return $l_map;
+}
+
+sub extract_metadata {
+       my $id = shift;
+       my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
+                                                       \s([a-f\d\-]+)$/x);
+       if (!$rev || !$uuid || !$url) {
+               # some of the original repositories I made had
+               # indentifiers like this:
+               ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
+       }
+       return ($url, $rev, $uuid);
+}
+
+sub tz_to_s_offset {
+       my ($tz) = @_;
+       $tz =~ s/(\d\d)$//;
+       return ($1 * 60) + ($tz * 3600);
+}
+
+sub setup_pager { # translated to Perl from pager.c
+       return unless (-t *STDOUT);
+       my $pager = $ENV{PAGER};
+       if (!defined $pager) {
+               $pager = 'less';
+       } elsif (length $pager == 0 || $pager eq 'cat') {
+               return;
+       }
+       pipe my $rfd, my $wfd or return;
+       defined(my $pid = fork) or croak $!;
+       if (!$pid) {
+               open STDOUT, '>&', $wfd or croak $!;
+               return;
+       }
+       open STDIN, '<&', $rfd or croak $!;
+       $ENV{LESS} ||= '-S';
+       exec $pager or croak "Can't run pager: $!\n";;
+}
+
+sub get_author_info {
+       my ($dest, $author, $t, $tz) = @_;
+       $author =~ s/(?:^\s*|\s*$)//g;
+       $dest->{a_raw} = $author;
+       my $_a;
+       if ($_authors) {
+               $_a = $rusers{$author} || undef;
+       }
+       if (!$_a) {
+               ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
+       }
+       $dest->{t} = $t;
+       $dest->{tz} = $tz;
+       $dest->{a} = $_a;
+       # Date::Parse isn't in the standard Perl distro :(
+       if ($tz =~ s/^\+//) {
+               $t += tz_to_s_offset($tz);
+       } elsif ($tz =~ s/^\-//) {
+               $t -= tz_to_s_offset($tz);
+       }
+       $dest->{t_utc} = $t;
+}
+
+sub process_commit {
+       my ($c, $r_min, $r_max, $defer) = @_;
+       if (defined $r_min && defined $r_max) {
+               if ($r_min == $c->{r} && $r_min == $r_max) {
+                       show_commit($c);
+                       return 0;
+               }
+               return 1 if $r_min == $r_max;
+               if ($r_min < $r_max) {
+                       # we need to reverse the print order
+                       return 0 if (defined $_limit && --$_limit < 0);
+                       push @$defer, $c;
+                       return 1;
+               }
+               if ($r_min != $r_max) {
+                       return 1 if ($r_min < $c->{r});
+                       return 1 if ($r_max > $c->{r});
+               }
+       }
+       return 0 if (defined $_limit && --$_limit < 0);
+       show_commit($c);
+       return 1;
+}
+
+sub show_commit {
+       my $c = shift;
+       if ($_oneline) {
+               my $x = "\n";
+               if (my $l = $c->{l}) {
+                       while ($l->[0] =~ /^\s*$/) { shift @$l }
+                       $x = $l->[0];
+               }
+               $_l_fmt ||= 'A' . length($c->{r});
+               print 'r',pack($_l_fmt, $c->{r}),' | ';
+               print "$c->{c} | " if $_show_commit;
+               print $x;
+       } else {
+               show_commit_normal($c);
+       }
+}
+
+sub show_commit_normal {
+       my ($c) = @_;
+       print '-' x72, "\nr$c->{r} | ";
+       print "$c->{c} | " if $_show_commit;
+       print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
+                                localtime($c->{t_utc})), ' | ';
+       my $nr_line = 0;
+
+       if (my $l = $c->{l}) {
+               while ($l->[$#$l] eq "\n" && $l->[($#$l - 1)] eq "\n") {
+                       pop @$l;
+               }
+               $nr_line = scalar @$l;
+               if (!$nr_line) {
+                       print "1 line\n\n\n";
+               } else {
+                       if ($nr_line == 1) {
+                               $nr_line = '1 line';
+                       } else {
+                               $nr_line .= ' lines';
+                       }
+                       print $nr_line, "\n\n";
+                       print $_ foreach @$l;
+               }
+       } else {
+               print "1 line\n\n";
+
+       }
+       foreach my $x (qw/raw diff/) {
+               if ($c->{$x}) {
+                       print "\n";
+                       print $_ foreach @{$c->{$x}}
+               }
+       }
+}
+
+sub libsvn_load {
+       return unless $_use_lib;
+       $_use_lib = eval {
+               require SVN::Core;
+               if ($SVN::Core::VERSION lt '1.2.1') {
+                       die "Need SVN::Core 1.2.1 or better ",
+                                       "(got $SVN::Core::VERSION) ",
+                                       "Falling back to command-line svn\n";
+               }
+               require SVN::Ra;
+               require SVN::Delta;
+               push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
+               my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
+                                       $SVN::Node::dir.$SVN::Node::unknown.
+                                       $SVN::Node::none.$SVN::Node::file.
+                                       $SVN::Node::dir.$SVN::Node::unknown;
+               1;
+       };
+}
+
+sub libsvn_connect {
+       my ($url) = @_;
+       my $auth = SVN::Core::auth_open([SVN::Client::get_simple_provider(),
+                         SVN::Client::get_ssl_server_trust_file_provider(),
+                         SVN::Client::get_username_provider()]);
+       my $s = eval { SVN::Ra->new(url => $url, auth => $auth) };
+       return $s;
+}
+
+sub libsvn_get_file {
+       my ($gui, $f, $rev) = @_;
+       my $p = $f;
+       return unless ($p =~ s#^\Q$SVN_PATH\E/?##);
+
+       my ($hash, $pid, $in, $out);
+       my $pool = SVN::Pool->new;
+       defined($pid = open3($in, $out, '>&STDERR',
+                               qw/git-hash-object -w --stdin/)) or croak $!;
+       my ($r, $props) = $SVN->get_file($f, $rev, $in, $pool);
+       $in->flush == 0 or croak $!;
+       close $in or croak $!;
+       $pool->clear;
+       chomp($hash = do { local $/; <$out> });
+       close $out or croak $!;
+       waitpid $pid, 0;
+       $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
+
+       my $mode = exists $props->{'svn:executable'} ? '100755' : '100644';
+       if (exists $props->{'svn:special'}) {
+               $mode = '120000';
+               my $link = `git-cat-file blob $hash`;
+               $link =~ s/^link // or die "svn:special file with contents: <",
+                                               $link, "> is not understood\n";
+               defined($pid = open3($in, $out, '>&STDERR',
+                               qw/git-hash-object -w --stdin/)) or croak $!;
+               print $in $link;
+               $in->flush == 0 or croak $!;
+               close $in or croak $!;
+               chomp($hash = do { local $/; <$out> });
+               close $out or croak $!;
+               waitpid $pid, 0;
+               $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
+       }
+       print $gui $mode,' ',$hash,"\t",$p,"\0" or croak $!;
+}
+
+sub libsvn_log_entry {
+       my ($rev, $author, $date, $msg, $parents) = @_;
+       my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
+                                        (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x)
+                               or die "Unable to parse date: $date\n";
+       if (defined $_authors && ! defined $users{$author}) {
+               die "Author: $author not defined in $_authors file\n";
+       }
+       return { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S",
+               author => $author, msg => $msg."\n", parents => $parents || [] }
+}
+
+sub process_rm {
+       my ($gui, $last_commit, $f) = @_;
+       $f =~ s#^\Q$SVN_PATH\E/?## or return;
+       # remove entire directories.
+       if (safe_qx('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
+               defined(my $pid = open my $ls, '-|') or croak $!;
+               if (!$pid) {
+                       exec(qw/git-ls-tree -r --name-only -z/,
+                               $last_commit,'--',$f) or croak $!;
+               }
+               local $/ = "\0";
+               while (<$ls>) {
+                       print $gui '0 ',0 x 40,"\t",$_ or croak $!;
+               }
+               close $ls or croak $?;
+       } else {
+               print $gui '0 ',0 x 40,"\t",$f,"\0" or croak $!;
+       }
+}
+
+sub libsvn_fetch {
+       my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
+       open my $gui, '| git-update-index -z --index-info' or croak $!;
+       my @amr;
+       foreach my $f (keys %$paths) {
+               my $m = $paths->{$f}->action();
+               $f =~ s#^/+##;
+               if ($m =~ /^[DR]$/) {
+                       process_rm($gui, $last_commit, $f);
+                       next if $m eq 'D';
+                       # 'R' can be file replacements, too, right?
+               }
+               my $pool = SVN::Pool->new;
+               my $t = $SVN->check_path($f, $rev, $pool);
+               if ($t == $SVN::Node::file) {
+                       if ($m =~ /^[AMR]$/) {
+                               push @amr, $f;
+                       } else {
+                               die "Unrecognized action: $m, ($f r$rev)\n";
+                       }
+               }
+               $pool->clear;
+       }
+       libsvn_get_file($gui, $_, $rev) foreach (@amr);
+       close $gui or croak $?;
+       return libsvn_log_entry($rev, $author, $date, $msg, [$last_commit]);
+}
+
+sub svn_grab_base_rev {
+       defined(my $pid = open my $fh, '-|') or croak $!;
+       if (!$pid) {
+               open my $null, '>', '/dev/null' or croak $!;
+               open STDERR, '>&', $null or croak $!;
+               exec qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
+                                                               or croak $!;
+       }
+       chomp(my $c = do { local $/; <$fh> });
+       close $fh;
+       if (defined $c && length $c) {
+               my ($url, $rev, $uuid) = extract_metadata((grep(/^git-svn-id: /,
+                       safe_qx(qw/git-cat-file commit/, $c)))[-1]);
+               return ($rev, $c);
+       }
+       return (undef, undef);
+}
+
+sub libsvn_parse_revision {
+       my $base = shift;
+       my $head = $SVN->get_latest_revnum();
+       if (!defined $_revision || $_revision eq 'BASE:HEAD') {
+               return ($base + 1, $head) if (defined $base);
+               return (0, $head);
+       }
+       return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
+       return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
+       if ($_revision =~ /^BASE:(\d+)$/) {
+               return ($base + 1, $1) if (defined $base);
+               return (0, $head);
+       }
+       return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
+       die "revision argument: $_revision not understood by git-svn\n",
+               "Try using the command-line svn client instead\n";
+}
+
+sub libsvn_traverse {
+       my ($gui, $pfx, $path, $rev) = @_;
+       my $cwd = "$pfx/$path";
+       my $pool = SVN::Pool->new;
+       $cwd =~ s#^/+##g;
+       my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
+       foreach my $d (keys %$dirent) {
+               my $t = $dirent->{$d}->kind;
+               if ($t == $SVN::Node::dir) {
+                       libsvn_traverse($gui, $cwd, $d, $rev);
+               } elsif ($t == $SVN::Node::file) {
+                       libsvn_get_file($gui, "$cwd/$d", $rev);
+               }
+       }
+       $pool->clear;
+}
+
+sub libsvn_traverse_ignore {
+       my ($fh, $path, $r) = @_;
+       $path =~ s#^/+##g;
+       my $pool = SVN::Pool->new;
+       my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
+       my $p = $path;
+       $p =~ s#^\Q$SVN_PATH\E/?##;
+       print $fh length $p ? "\n# $p\n" : "\n# /\n";
+       if (my $s = $props->{'svn:ignore'}) {
+               $s =~ s/[\r\n]+/\n/g;
+               chomp $s;
+               if (length $p == 0) {
+                       $s =~ s#\n#\n/$p#g;
+                       print $fh "/$s\n";
+               } else {
+                       $s =~ s#\n#\n/$p/#g;
+                       print $fh "/$p/$s\n";
+               }
+       }
+       foreach (sort keys %$dirent) {
+               next if $dirent->{$_}->kind != $SVN::Node::dir;
+               libsvn_traverse_ignore($fh, "$path/$_", $r);
+       }
+       $pool->clear;
+}
+
+sub revisions_eq {
+       my ($path, $r0, $r1) = @_;
+       return 1 if $r0 == $r1;
+       my $nr = 0;
+       if ($_use_lib) {
+               # should be OK to use Pool here (r1 - r0) should be small
+               my $pool = SVN::Pool->new;
+               $SVN->get_log("/$path", $r0, $r1, 0, 1, 1, sub {$nr++},$pool);
+               $pool->clear;
+       } else {
+               my ($url, undef) = repo_path_split($SVN_URL);
+               my $svn_log = svn_log_raw("$url/$path","-r$r0:$r1");
+               while (next_log_entry($svn_log)) { $nr++ }
+               close $svn_log->{fh};
+       }
+       return 0 if ($nr > 1);
+       return 1;
+}
+
+sub libsvn_find_parent_branch {
+       my ($paths, $rev, $author, $date, $msg) = @_;
+       my $svn_path = '/'.$SVN_PATH;
+
+       # look for a parent from another branch:
+       my $i = $paths->{$svn_path} or return;
+       my $branch_from = $i->copyfrom_path or return;
+       my $r = $i->copyfrom_rev;
+       print STDERR  "Found possible branch point: ",
+                               "$branch_from => $svn_path, $r\n";
+       $branch_from =~ s#^/##;
+       my $l_map = read_url_paths();
+       my $url = $SVN->{url};
+       defined $l_map->{$url} or return;
+       my $id = $l_map->{$url}->{$branch_from} or return;
+       my ($r0, $parent) = find_rev_before($r,$id,1);
+       return unless (defined $r0 && defined $parent);
+       if (revisions_eq($branch_from, $r0, $r)) {
+               unlink $GIT_SVN_INDEX;
+               print STDERR "Found branch parent: $parent\n";
+               sys(qw/git-read-tree/, $parent);
+               return libsvn_fetch($parent, $paths, $rev,
+                                       $author, $date, $msg);
+       }
+       print STDERR "Nope, branch point not imported or unknown\n";
+       return undef;
+}
+
+sub libsvn_new_tree {
+       if (my $log_entry = libsvn_find_parent_branch(@_)) {
+               return $log_entry;
+       }
+       my ($paths, $rev, $author, $date, $msg) = @_;
+       open my $gui, '| git-update-index -z --index-info' or croak $!;
+       my $pool = SVN::Pool->new;
+       libsvn_traverse($gui, '', $SVN_PATH, $rev, $pool);
+       $pool->clear;
+       close $gui or croak $?;
+       return libsvn_log_entry($rev, $author, $date, $msg);
+}
+
+sub find_graft_path_commit {
+       my ($tree_paths, $p1, $r1) = @_;
+       foreach my $x (keys %$tree_paths) {
+               next unless ($p1 =~ /^\Q$x\E/);
+               my $i = $tree_paths->{$x};
+               my ($r0, $parent) = find_rev_before($r1,$i,1);
+               return $parent if (defined $r0 && $r0 == $r1);
+               print STDERR "r$r1 of $i not imported\n";
+               next;
+       }
+       return undef;
+}
+
+sub find_graft_path_parents {
+       my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
+       foreach my $x (keys %$tree_paths) {
+               next unless ($p0 =~ /^\Q$x\E/);
+               my $i = $tree_paths->{$x};
+               my ($r, $parent) = find_rev_before($r0, $i, 1);
+               if (defined $r && defined $parent && revisions_eq($x,$r,$r0)) {
+                       $grafts->{$c}->{$parent} = 1;
+               }
+       }
+}
+
+sub libsvn_graft_file_copies {
+       my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
+       foreach (keys %$paths) {
+               my $i = $paths->{$_};
+               my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
+                                       $i->copyfrom_rev);
+               next unless (defined $p0 && defined $r0);
+
+               my $p1 = $_;
+               $p1 =~ s#^/##;
+               $p0 =~ s#^/##;
+               my $c = find_graft_path_commit($tree_paths, $p1, $rev);
+               next unless $c;
+               find_graft_path_parents($grafts, $tree_paths, $c, $p0, $r0);
+       }
+}
+
+sub set_index {
+       my $old = $ENV{GIT_INDEX_FILE};
+       $ENV{GIT_INDEX_FILE} = shift;
+       return $old;
+}
+
+sub restore_index {
+       my ($old) = @_;
+       if (defined $old) {
+               $ENV{GIT_INDEX_FILE} = $old;
+       } else {
+               delete $ENV{GIT_INDEX_FILE};
+       }
+}
+
+sub libsvn_commit_cb {
+       my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
+       if ($_optimize_commits && $rev == ($r_last + 1)) {
+               my $log = libsvn_log_entry($rev,$committer,$date,$msg);
+               $log->{tree} = get_tree_from_treeish($c);
+               my $cmt = git_commit($log, $cmt_last, $c);
+               my @diff = safe_qx('git-diff-tree', $cmt, $c);
+               if (@diff) {
+                       print STDERR "Trees differ: $cmt $c\n",
+                                       join('',@diff),"\n";
+                       exit 1;
+               }
+       } else {
+               fetch("$rev=$c");
+       }
+}
+
+sub libsvn_ls_fullurl {
+       my $fullurl = shift;
+       my ($repo, $path) = repo_path_split($fullurl);
+       $SVN ||= libsvn_connect($repo);
+       my @ret;
+       my $pool = SVN::Pool->new;
+       my ($dirent, undef, undef) = $SVN->get_dir($path,
+                                               $SVN->get_latest_revnum, $pool);
+       foreach my $d (keys %$dirent) {
+               if ($dirent->{$d}->kind == $SVN::Node::dir) {
+                       push @ret, "$d/"; # add '/' for compat with cli svn
+               }
+       }
+       $pool->clear;
+       return @ret;
+}
+
+
+sub libsvn_skip_unknown_revs {
+       my $err = shift;
+       my $errno = $err->apr_err();
+       # Maybe the branch we're tracking didn't
+       # exist when the repo started, so it's
+       # not an error if it doesn't, just continue
+       #
+       # Wonderfully consistent library, eh?
+       # 160013 - svn:// and file://
+       # 175002 - http(s)://
+       #   More codes may be discovered later...
+       if ($errno == 175002 || $errno == 160013) {
+               return;
+       }
+       croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
+};
+
+# Tie::File seems to be prone to offset errors if revisions get sparse,
+# it's not that fast, either.  Tie::File is also not in Perl 5.6.  So
+# one of my favorite modules is out :<  Next up would be one of the DBM
+# modules, but I'm not sure which is most portable...  So I'll just
+# go with something that's plain-text, but still capable of
+# being randomly accessed.  So here's my ultra-simple fixed-width
+# database.  All records are 40 characters + "\n", so it's easy to seek
+# to a revision: (41 * rev) is the byte offset.
+# A record of 40 0s denotes an empty revision.
+# And yes, it's still pretty fast (faster than Tie::File).
+sub revdb_set {
+       my ($file, $rev, $commit) = @_;
+       length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
+       open my $fh, '+<', $file or croak $!;
+       my $offset = $rev * 41;
+       # assume that append is the common case:
+       seek $fh, 0, 2 or croak $!;
+       my $pos = tell $fh;
+       if ($pos < $offset) {
+               print $fh (('0' x 40),"\n") x (($offset - $pos) / 41);
+       }
+       seek $fh, $offset, 0 or croak $!;
+       print $fh $commit,"\n";
+       close $fh or croak $!;
+}
+
+sub revdb_get {
+       my ($file, $rev) = @_;
+       my $ret;
+       my $offset = $rev * 41;
+       open my $fh, '<', $file or croak $!;
+       seek $fh, $offset, 0;
+       if (tell $fh == $offset) {
+               $ret = readline $fh;
+               if (defined $ret) {
+                       chomp $ret;
+                       $ret = undef if ($ret =~ /^0{40}$/);
+               }
+       }
+       close $fh or croak $!;
+       return $ret;
+}
+
+sub copy_remote_ref {
+       my $origin = $_cp_remote ? $_cp_remote : 'origin';
+       my $ref = "refs/remotes/$GIT_SVN";
+       if (safe_qx('git-ls-remote', $origin, $ref)) {
+               sys(qw/git fetch/, $origin, "$ref:$ref");
+       } else {
+               die "Unable to find remote reference: ",
+                               "refs/remotes/$GIT_SVN on $origin\n";
+       }
+}
+
+package SVN::Git::Editor;
+use vars qw/@ISA/;
+use strict;
+use warnings;
+use Carp qw/croak/;
+use IO::File;
+
+sub new {
+       my $class = shift;
+       my $git_svn = shift;
+       my $self = SVN::Delta::Editor->new(@_);
+       bless $self, $class;
+       foreach (qw/svn_path c r ra /) {
+               die "$_ required!\n" unless (defined $git_svn->{$_});
+               $self->{$_} = $git_svn->{$_};
+       }
+       $self->{pool} = SVN::Pool->new;
+       $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
+       $self->{rm} = { };
+       require Digest::MD5;
+       return $self;
+}
+
+sub split_path {
+       return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
+}
+
+sub repo_path {
+       (defined $_[1] && length $_[1]) ? "$_[0]->{svn_path}/$_[1]"
+                                       : $_[0]->{svn_path}
+}
+
+sub url_path {
+       my ($self, $path) = @_;
+       $self->{ra}->{url} . '/' . $self->repo_path($path);
+}
+
+sub rmdirs {
+       my ($self) = @_;
+       my $rm = $self->{rm};
+       delete $rm->{''}; # we never delete the url we're tracking
+       return unless %$rm;
+
+       foreach (keys %$rm) {
+               my @d = split m#/#, $_;
+               my $c = shift @d;
+               $rm->{$c} = 1;
+               while (@d) {
+                       $c .= '/' . shift @d;
+                       $rm->{$c} = 1;
+               }
+       }
+       delete $rm->{$self->{svn_path}};
+       delete $rm->{''}; # we never delete the url we're tracking
+       return unless %$rm;
+
+       defined(my $pid = open my $fh,'-|') or croak $!;
+       if (!$pid) {
+               exec qw/git-ls-tree --name-only -r -z/, $self->{c} or croak $!;
+       }
+       local $/ = "\0";
+       while (<$fh>) {
+               chomp;
+               $_ = $self->{svn_path} . '/' . $_;
+               my ($dn) = ($_ =~ m#^(.*?)/?(?:[^/]+)$#);
+               delete $rm->{$dn};
+               last unless %$rm;
+       }
+       my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
+       foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
+               $self->close_directory($bat->{$d}, $p);
+               my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
+               $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
+               delete $bat->{$d};
+       }
+}
+
+sub open_or_add_dir {
+       my ($self, $full_path, $baton) = @_;
+       my $p = SVN::Pool->new;
+       my $t = $self->{ra}->check_path($full_path, $self->{r}, $p);
+       $p->clear;
+       if ($t == $SVN::Node::none) {
+               return $self->add_directory($full_path, $baton,
+                                               undef, -1, $self->{pool});
+       } elsif ($t == $SVN::Node::dir) {
+               return $self->open_directory($full_path, $baton,
+                                               $self->{r}, $self->{pool});
+       }
+       print STDERR "$full_path already exists in repository at ",
+               "r$self->{r} and it is not a directory (",
+               ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
+       exit 1;
+}
+
+sub ensure_path {
+       my ($self, $path) = @_;
+       my $bat = $self->{bat};
+       $path = $self->repo_path($path);
+       return $bat->{''} unless (length $path);
+       my @p = split m#/+#, $path;
+       my $c = shift @p;
+       $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
+       while (@p) {
+               my $c0 = $c;
+               $c .= '/' . shift @p;
+               $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
+       }
+       return $bat->{$c};
+}
+
+sub A {
+       my ($self, $m) = @_;
+       my ($dir, $file) = split_path($m->{file_b});
+       my $pbat = $self->ensure_path($dir);
+       my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
+                                       undef, -1);
+       $self->chg_file($fbat, $m);
+       $self->close_file($fbat,undef,$self->{pool});
+}
+
+sub C {
+       my ($self, $m) = @_;
+       my ($dir, $file) = split_path($m->{file_b});
+       my $pbat = $self->ensure_path($dir);
+       my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
+                               $self->url_path($m->{file_a}), $self->{r});
+       $self->chg_file($fbat, $m);
+       $self->close_file($fbat,undef,$self->{pool});
+}
+
+sub delete_entry {
+       my ($self, $path, $pbat) = @_;
+       my $rpath = $self->repo_path($path);
+       my ($dir, $file) = split_path($rpath);
+       $self->{rm}->{$dir} = 1;
+       $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
+}
+
+sub R {
+       my ($self, $m) = @_;
+       my ($dir, $file) = split_path($m->{file_b});
+       my $pbat = $self->ensure_path($dir);
+       my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
+                               $self->url_path($m->{file_a}), $self->{r});
+       $self->chg_file($fbat, $m);
+       $self->close_file($fbat,undef,$self->{pool});
+
+       ($dir, $file) = split_path($m->{file_a});
+       $pbat = $self->ensure_path($dir);
+       $self->delete_entry($m->{file_a}, $pbat);
+}
+
+sub M {
+       my ($self, $m) = @_;
+       my ($dir, $file) = split_path($m->{file_b});
+       my $pbat = $self->ensure_path($dir);
+       my $fbat = $self->open_file($self->repo_path($m->{file_b}),
+                               $pbat,$self->{r},$self->{pool});
+       $self->chg_file($fbat, $m);
+       $self->close_file($fbat,undef,$self->{pool});
+}
+
+sub T { shift->M(@_) }
+
+sub change_file_prop {
+       my ($self, $fbat, $pname, $pval) = @_;
+       $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
+}
+
+sub chg_file {
+       my ($self, $fbat, $m) = @_;
+       if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
+               $self->change_file_prop($fbat,'svn:executable','*');
+       } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
+               $self->change_file_prop($fbat,'svn:executable',undef);
+       }
+       my $fh = IO::File->new_tmpfile or croak $!;
+       if ($m->{mode_b} =~ /^120/) {
+               print $fh 'link ' or croak $!;
+               $self->change_file_prop($fbat,'svn:special','*');
+       } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
+               $self->change_file_prop($fbat,'svn:special',undef);
+       }
+       defined(my $pid = fork) or croak $!;
+       if (!$pid) {
+               open STDOUT, '>&', $fh or croak $!;
+               exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
+       }
+       waitpid $pid, 0;
+       croak $? if $?;
+       $fh->flush == 0 or croak $!;
+       seek $fh, 0, 0 or croak $!;
+
+       my $md5 = Digest::MD5->new;
+       $md5->addfile($fh) or croak $!;
+       seek $fh, 0, 0 or croak $!;
+
+       my $exp = $md5->hexdigest;
+       my $atd = $self->apply_textdelta($fbat, undef, $self->{pool});
+       my $got = SVN::TxDelta::send_stream($fh, @$atd, $self->{pool});
+       die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
+
+       close $fh or croak $!;
+}
+
+sub D {
+       my ($self, $m) = @_;
+       my ($dir, $file) = split_path($m->{file_b});
+       my $pbat = $self->ensure_path($dir);
+       $self->delete_entry($m->{file_b}, $pbat);
+}
+
+sub close_edit {
+       my ($self) = @_;
+       my ($p,$bat) = ($self->{pool}, $self->{bat});
+       foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
+               $self->close_directory($bat->{$_}, $p);
+       }
+       $self->SUPER::close_edit($p);
+       $p->clear;
+}
+
+sub abort_edit {
+       my ($self) = @_;
+       $self->SUPER::abort_edit($self->{pool});
+       $self->{pool}->clear;
+}
+
 __END__
 
 Data structures:
@@ -1178,3 +3042,7 @@ diff-index line ($m hash)
        file_b => new/current file name of a file (any chg)
 }
 ;
+
+Notes:
+       I don't trust the each() function on unless I created %hash myself
+       because the internal iterator may not have started at base.
index e18fcaf..f7d3de4 100644 (file)
@@ -36,17 +36,22 @@ COMMANDS
 --------
 init::
        Creates an empty git repository with additional metadata
-       directories for git-svn.  The SVN_URL must be specified
-       at this point.
+       directories for git-svn.  The Subversion URL must be specified
+       as a command-line argument.
 
 fetch::
-       Fetch unfetched revisions from the SVN_URL we are tracking.
-       refs/heads/remotes/git-svn will be updated to the latest revision.
+       Fetch unfetched revisions from the Subversion URL we are
+       tracking.  refs/remotes/git-svn will be updated to the
+       latest revision.
 
-       Note: You should never attempt to modify the remotes/git-svn branch
-       outside of git-svn.  Instead, create a branch from remotes/git-svn
-       and work on that branch.  Use the 'commit' command (see below)
-       to write git commits back to remotes/git-svn.
+       Note: You should never attempt to modify the remotes/git-svn
+       branch outside of git-svn.  Instead, create a branch from
+       remotes/git-svn and work on that branch.  Use the 'commit'
+       command (see below) to write git commits back to
+       remotes/git-svn.
+
+       See 'Additional Fetch Arguments' if you are interested in
+       manually joining branches on commit.
 
 commit::
        Commit specified commit or tree objects to SVN.  This relies on
@@ -62,9 +67,9 @@ rebuild::
        tracked with git-svn.  Unfortunately, git-clone does not clone
        git-svn metadata and the svn working tree that git-svn uses for
        its operations.  This rebuilds the metadata so git-svn can
-       resume fetch operations.  SVN_URL may be optionally specified if
-       the directory/repository you're tracking has moved or changed
-       protocols.
+       resume fetch operations.  A Subversion URL may be optionally
+       specified at the command-line if the directory/repository you're
+       tracking has moved or changed protocols.
 
 show-ignore::
        Recursively finds and lists the svn:ignore property on
@@ -123,6 +128,24 @@ OPTIONS
        repo-config key: svn.l
        repo-config key: svn.findcopiesharder
 
+-A<filename>::
+--authors-file=<filename>::
+
+       Syntax is compatible with the files used by git-svnimport and
+       git-cvsimport:
+
+------------------------------------------------------------------------
+loginname = Joe User <user@example.com>
+------------------------------------------------------------------------
+
+       If this option is specified and git-svn encounters an SVN
+       committer name that does not exist in the authors-file, git-svn
+       will abort operation. The user will then have to add the
+       appropriate entry.  Re-running the previous git-svn command
+       after the authors-file is modified should continue operation.
+
+       repo-config key: svn.authors-file
+
 ADVANCED OPTIONS
 ----------------
 -b<refname>::
diff --git a/contrib/git-svn/t/lib-git-svn.sh b/contrib/git-svn/t/lib-git-svn.sh
new file mode 100644 (file)
index 0000000..2843258
--- /dev/null
@@ -0,0 +1,39 @@
+PATH=$PWD/../:$PATH
+if test -d ../../../t
+then
+    cd ../../../t
+else
+    echo "Must be run in contrib/git-svn/t" >&2
+    exit 1
+fi
+
+. ./test-lib.sh
+
+GIT_DIR=$PWD/.git
+GIT_SVN_DIR=$GIT_DIR/svn/git-svn
+SVN_TREE=$GIT_SVN_DIR/svn-tree
+
+svnadmin >/dev/null 2>&1
+if test $? != 1
+then
+    test_expect_success 'skipping contrib/git-svn test' :
+    test_done
+    exit
+fi
+
+svn >/dev/null 2>&1
+if test $? != 1
+then
+    test_expect_success 'skipping contrib/git-svn test' :
+    test_done
+    exit
+fi
+
+svnrepo=$PWD/svnrepo
+
+set -e
+
+svnadmin create $svnrepo
+svnrepo="file://$svnrepo/test-git-svn"
+
+
index 80ad357..443d518 100644 (file)
@@ -3,62 +3,27 @@
 # Copyright (c) 2006 Eric Wong
 #
 
-
-PATH=$PWD/../:$PATH
 test_description='git-svn tests'
-if test -d ../../../t
-then
-    cd ../../../t
-else
-    echo "Must be run in contrib/git-svn/t" >&2
-    exit 1
-fi
-
-. ./test-lib.sh
-
-GIT_DIR=$PWD/.git
-GIT_SVN_DIR=$GIT_DIR/git-svn
-SVN_TREE=$GIT_SVN_DIR/tree
-
-svnadmin >/dev/null 2>&1
-if test $? != 1
-then
-    test_expect_success 'skipping contrib/git-svn test' :
-    test_done
-    exit
-fi
-
-svn >/dev/null 2>&1
-if test $? != 1
-then
-    test_expect_success 'skipping contrib/git-svn test' :
-    test_done
-    exit
-fi
-
-svnrepo=$PWD/svnrepo
-
-set -e
-
-svnadmin create $svnrepo
-svnrepo="file://$svnrepo/test-git-svn"
+GIT_SVN_LC_ALL=$LC_ALL
+. ./lib-git-svn.sh
 
 mkdir import
-
 cd import
 
 echo foo > foo
-ln -s foo foo.link
+if test -z "$NO_SYMLINK"
+then
+       ln -s foo foo.link
+fi
 mkdir -p dir/a/b/c/d/e
 echo 'deep dir' > dir/a/b/c/d/e/file
 mkdir -p bar
 echo 'zzz' > bar/zzz
 echo '#!/bin/sh' > exec.sh
 chmod +x exec.sh
-svn import -m 'import for git-svn' . $svnrepo >/dev/null
+svn import -m 'import for git-svn' . "$svnrepo" >/dev/null
 
 cd ..
-
 rm -rf import
 
 test_expect_success \
@@ -69,9 +34,10 @@ test_expect_success \
     'import an SVN revision into git' \
     'git-svn fetch'
 
+test_expect_success "checkout from svn" "svn co $svnrepo $SVN_TREE"
 
 name='try a deep --rmdir with a commit'
-git checkout -b mybranch remotes/git-svn
+git checkout -f -b mybranch remotes/git-svn
 mv dir/a/b/c/d/e/file dir/file
 cp dir/file file
 git update-index --add --remove dir/a/b/c/d/e/file dir/file file
@@ -79,6 +45,7 @@ git commit -m "$name"
 
 test_expect_success "$name" \
     "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch &&
+     svn up $SVN_TREE &&
      test -d $SVN_TREE/dir && test ! -d $SVN_TREE/dir/a"
 
 
@@ -90,14 +57,14 @@ git update-index --remove dir/file
 git update-index --add dir/file/file
 git commit -m "$name"
 
-test_expect_code 1 "$name" \
+test_expect_failure "$name" \
     'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch' \
     || true
 
 
 name='detect node change from directory to file #1'
 rm -rf dir $GIT_DIR/index
-git checkout -b mybranch2 remotes/git-svn
+git checkout -f -b mybranch2 remotes/git-svn
 mv bar/zzz zzz
 rm -rf bar
 mv zzz bar
@@ -105,14 +72,14 @@ git update-index --remove -- bar/zzz
 git update-index --add -- bar
 git commit -m "$name"
 
-test_expect_code 1 "$name" \
+test_expect_failure "$name" \
     'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch2' \
     || true
 
 
 name='detect node change from file to directory #2'
 rm -f $GIT_DIR/index
-git checkout -b mybranch3 remotes/git-svn
+git checkout -f -b mybranch3 remotes/git-svn
 rm bar/zzz
 git-update-index --remove bar/zzz
 mkdir bar/zzz
@@ -120,14 +87,14 @@ echo yyy > bar/zzz/yyy
 git-update-index --add bar/zzz/yyy
 git commit -m "$name"
 
-test_expect_code 1 "$name" \
+test_expect_failure "$name" \
     'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch3' \
     || true
 
 
 name='detect node change from directory to file #2'
 rm -f $GIT_DIR/index
-git checkout -b mybranch4 remotes/git-svn
+git checkout -f -b mybranch4 remotes/git-svn
 rm -rf dir
 git update-index --remove -- dir/file
 touch dir
@@ -135,20 +102,21 @@ echo asdf > dir
 git update-index --add -- dir
 git commit -m "$name"
 
-test_expect_code 1 "$name" \
+test_expect_failure "$name" \
     'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch4' \
     || true
 
 
 name='remove executable bit from a file'
 rm -f $GIT_DIR/index
-git checkout -b mybranch5 remotes/git-svn
+git checkout -f -b mybranch5 remotes/git-svn
 chmod -x exec.sh
 git update-index exec.sh
 git commit -m "$name"
 
 test_expect_success "$name" \
     "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
+     svn up $SVN_TREE &&
      test ! -x $SVN_TREE/exec.sh"
 
 
@@ -159,49 +127,64 @@ git commit -m "$name"
 
 test_expect_success "$name" \
     "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
+     svn up $SVN_TREE &&
      test -x $SVN_TREE/exec.sh"
 
 
 
-name='executable file becomes a symlink to bar/zzz (file)'
-rm exec.sh
-ln -s bar/zzz exec.sh
-git update-index exec.sh
-git commit -m "$name"
-
-test_expect_success "$name" \
-    "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
-     test -L $SVN_TREE/exec.sh"
-
-
-
-name='new symlink is added to a file that was also just made executable'
-chmod +x bar/zzz
-ln -s bar/zzz exec-2.sh
-git update-index --add bar/zzz exec-2.sh
-git commit -m "$name"
-
-test_expect_success "$name" \
-    "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
-     test -x $SVN_TREE/bar/zzz &&
-     test -L $SVN_TREE/exec-2.sh"
-
-
-
-name='modify a symlink to become a file'
-git help > help || true
-rm exec-2.sh
-cp help exec-2.sh
-git update-index exec-2.sh
-git commit -m "$name"
-
-test_expect_success "$name" \
-    "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
-     test -f $SVN_TREE/exec-2.sh &&
-     test ! -L $SVN_TREE/exec-2.sh &&
-     diff -u help $SVN_TREE/exec-2.sh"
+if test -z "$NO_SYMLINK"
+then
+       name='executable file becomes a symlink to bar/zzz (file)'
+       rm exec.sh
+       ln -s bar/zzz exec.sh
+       git update-index exec.sh
+       git commit -m "$name"
+
+       test_expect_success "$name" \
+           "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
+            svn up $SVN_TREE &&
+            test -L $SVN_TREE/exec.sh"
+
+       name='new symlink is added to a file that was also just made executable'
+       chmod +x bar/zzz
+       ln -s bar/zzz exec-2.sh
+       git update-index --add bar/zzz exec-2.sh
+       git commit -m "$name"
+
+       test_expect_success "$name" \
+           "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
+            svn up $SVN_TREE &&
+            test -x $SVN_TREE/bar/zzz &&
+            test -L $SVN_TREE/exec-2.sh"
+
+       name='modify a symlink to become a file'
+       git help > help || true
+       rm exec-2.sh
+       cp help exec-2.sh
+       git update-index exec-2.sh
+       git commit -m "$name"
+
+       test_expect_success "$name" \
+           "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
+            svn up $SVN_TREE &&
+            test -f $SVN_TREE/exec-2.sh &&
+            test ! -L $SVN_TREE/exec-2.sh &&
+            diff -u help $SVN_TREE/exec-2.sh"
+fi
 
 
+if test -n "$GIT_SVN_LC_ALL" && echo $GIT_SVN_LC_ALL | grep -q '\.UTF-8$'
+then
+       name="commit with UTF-8 message: locale: $GIT_SVN_LC_ALL"
+       echo '# hello' >> exec-2.sh
+       git update-index exec-2.sh
+       git commit -m 'éï∏'
+       export LC_ALL="$GIT_SVN_LC_ALL"
+       test_expect_success "$name" "git-svn commit HEAD"
+       unset LC_ALL
+else
+       echo "UTF-8 locale not set, test skipped ($GIT_SVN_LC_ALL)"
+fi
 
 name='test fetch functionality (svn => git) with alternate GIT_SVN_ID'
 GIT_SVN_ID=alt
@@ -212,5 +195,28 @@ test_expect_success "$name" \
      git-rev-list --pretty=raw remotes/alt | grep ^tree | uniq > b &&
      diff -u a b"
 
+if test -n "$NO_SYMLINK"
+then
+       test_done
+       exit 0
+fi
+
+name='check imported tree checksums expected tree checksums'
+rm -f expected
+if test -n "$GIT_SVN_LC_ALL" && echo $GIT_SVN_LC_ALL | grep -q '\.UTF-8$'
+then
+       echo tree f735671b89a7eb30cab1d8597de35bd4271ab813 > expected
+fi
+cat >> expected <<\EOF
+tree 4b9af72bb861eaed053854ec502cf7df72618f0f
+tree 031b8d557afc6fea52894eaebb45bec52f1ba6d1
+tree 0b094cbff17168f24c302e297f55bfac65eb8bd3
+tree d667270a1f7b109f5eb3aaea21ede14b56bfdd6e
+tree 56a30b966619b863674f5978696f4a3594f2fca9
+tree d667270a1f7b109f5eb3aaea21ede14b56bfdd6e
+tree 8f51f74cf0163afc9ad68a4b1537288c4558b5a4
+EOF
+test_expect_success "$name" "diff -u a expected"
+
 test_done
 
diff --git a/contrib/git-svn/t/t0001-contrib-git-svn-props.sh b/contrib/git-svn/t/t0001-contrib-git-svn-props.sh
new file mode 100644 (file)
index 0000000..54e0ed7
--- /dev/null
@@ -0,0 +1,126 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Eric Wong
+#
+
+test_description='git-svn property tests'
+. ./lib-git-svn.sh
+
+mkdir import
+
+a_crlf=
+a_lf=
+a_cr=
+a_ne_crlf=
+a_ne_lf=
+a_ne_cr=
+a_empty=
+a_empty_lf=
+a_empty_cr=
+a_empty_crlf=
+
+cd import
+       cat >> kw.c <<\EOF
+/* Make it look like somebody copied a file from CVS into SVN: */
+/* $Id: kw.c,v 1.1.1.1 1994/03/06 00:00:00 eric Exp $ */
+EOF
+
+       printf "Hello\r\nWorld\r\n" > crlf
+       a_crlf=`git-hash-object -w crlf`
+       printf "Hello\rWorld\r" > cr
+       a_cr=`git-hash-object -w cr`
+       printf "Hello\nWorld\n" > lf
+       a_lf=`git-hash-object -w lf`
+
+       printf "Hello\r\nWorld" > ne_crlf
+       a_ne_crlf=`git-hash-object -w ne_crlf`
+       printf "Hello\nWorld" > ne_lf
+       a_ne_lf=`git-hash-object -w ne_lf`
+       printf "Hello\rWorld" > ne_cr
+       a_ne_cr=`git-hash-object -w ne_cr`
+
+       touch empty
+       a_empty=`git-hash-object -w empty`
+       printf "\n" > empty_lf
+       a_empty_lf=`git-hash-object -w empty_lf`
+       printf "\r" > empty_cr
+       a_empty_cr=`git-hash-object -w empty_cr`
+       printf "\r\n" > empty_crlf
+       a_empty_crlf=`git-hash-object -w empty_crlf`
+
+       svn import -m 'import for git-svn' . "$svnrepo" >/dev/null
+cd ..
+
+rm -rf import
+test_expect_success 'checkout working copy from svn' "svn co $svnrepo test_wc"
+test_expect_success 'setup some commits to svn' \
+       'cd test_wc &&
+               echo Greetings >> kw.c &&
+               svn commit -m "Not yet an Id" &&
+               svn up &&
+               echo Hello world >> kw.c &&
+               svn commit -m "Modified file, but still not yet an Id" &&
+               svn up &&
+               svn propset svn:keywords Id kw.c &&
+               svn commit -m "Propset Id" &&
+               svn up &&
+       cd ..'
+
+test_expect_success 'initialize git-svn' "git-svn init $svnrepo"
+test_expect_success 'fetch revisions from svn' 'git-svn fetch'
+
+name='test svn:keywords ignoring'
+test_expect_success "$name" \
+       'git checkout -b mybranch remotes/git-svn &&
+       echo Hi again >> kw.c &&
+       git commit -a -m "test keywoards ignoring" &&
+       git-svn commit remotes/git-svn..mybranch &&
+       git pull . remotes/git-svn'
+
+expect='/* $Id$ */'
+got="`sed -ne 2p kw.c`"
+test_expect_success 'raw $Id$ found in kw.c' "test '$expect' = '$got'"
+
+test_expect_success "propset CR on crlf files" \
+       'cd test_wc &&
+               svn propset svn:eol-style CR empty &&
+               svn propset svn:eol-style CR crlf &&
+               svn propset svn:eol-style CR ne_crlf &&
+               svn commit -m "propset CR on crlf files" &&
+               svn up &&
+        cd ..'
+
+test_expect_success 'fetch and pull latest from svn and checkout a new wc' \
+       "git-svn fetch &&
+        git pull . remotes/git-svn &&
+        svn co $svnrepo new_wc"
+
+for i in crlf ne_crlf lf ne_lf cr ne_cr empty_cr empty_lf empty empty_crlf
+do
+       test_expect_success "Comparing $i" "cmp $i new_wc/$i"
+done
+
+
+cd test_wc
+       printf '$Id$\rHello\rWorld\r' > cr
+       printf '$Id$\rHello\rWorld' > ne_cr
+       a_cr=`printf '$Id$\r\nHello\r\nWorld\r\n' | git-hash-object --stdin`
+       a_ne_cr=`printf '$Id$\r\nHello\r\nWorld' | git-hash-object --stdin`
+       test_expect_success 'Set CRLF on cr files' \
+       'svn propset svn:eol-style CRLF cr &&
+        svn propset svn:eol-style CRLF ne_cr &&
+        svn propset svn:keywords Id cr &&
+        svn propset svn:keywords Id ne_cr &&
+        svn commit -m "propset CRLF on cr files" &&
+        svn up'
+cd ..
+test_expect_success 'fetch and pull latest from svn' \
+       'git-svn fetch && git pull . remotes/git-svn'
+
+b_cr="`git-hash-object cr`"
+b_ne_cr="`git-hash-object ne_cr`"
+
+test_expect_success 'CRLF + $Id$' "test '$a_cr' = '$b_cr'"
+test_expect_success 'CRLF + $Id$ (no newline)' "test '$a_ne_cr' = '$b_ne_cr'"
+
+test_done
index 781badb..3b6bdce 100755 (executable)
@@ -425,7 +425,7 @@ class DiffWindow:
 class GitView:
        """ This is the main class
        """
-       version = "0.7"
+       version = "0.8"
 
        def __init__(self, with_diff=0):
                self.with_diff = with_diff
@@ -449,8 +449,32 @@ class GitView:
 
                self.accel_group = gtk.AccelGroup()
                self.window.add_accel_group(self.accel_group)
+               self.accel_group.connect_group(0xffc2, 0, gtk.ACCEL_LOCKED, self.refresh);
+               self.accel_group.connect_group(0xffc1, 0, gtk.ACCEL_LOCKED, self.maximize);
+               self.accel_group.connect_group(0xffc8, 0, gtk.ACCEL_LOCKED, self.fullscreen);
+               self.accel_group.connect_group(0xffc9, 0, gtk.ACCEL_LOCKED, self.unfullscreen);
 
-               self.construct()
+               self.window.add(self.construct())
+
+       def refresh(self, widget, event=None, *arguments, **keywords):
+               self.get_encoding()
+               self.get_bt_sha1()
+               Commit.children_sha1 = {}
+               self.set_branch(sys.argv[without_diff:])
+               self.window.show()
+               return True
+
+       def maximize(self, widget, event=None, *arguments, **keywords):
+               self.window.maximize()
+               return True
+
+       def fullscreen(self, widget, event=None, *arguments, **keywords):
+               self.window.fullscreen()
+               return True
+
+       def unfullscreen(self, widget, event=None, *arguments, **keywords):
+               self.window.unfullscreen()
+               return True
 
        def get_bt_sha1(self):
                """ Update the bt_sha1 dictionary with the
@@ -500,9 +524,9 @@ class GitView:
                menu_bar.show()
                vbox.pack_start(menu_bar, expand=False, fill=True)
                vbox.pack_start(paned, expand=True, fill=True)
-               self.window.add(vbox)
                paned.show()
                vbox.show()
+               return vbox
 
 
        def construct_top(self):
@@ -974,10 +998,15 @@ class GitView:
                try:
                        self.treeview.set_cursor(self.index[revid])
                except KeyError:
-                       print "Revision %s not present in the list" % revid
+                       dialog = gtk.MessageDialog(parent=None, flags=0,
+                                       type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_CLOSE,
+                                       message_format=None)
+                       dialog.set_markup("Revision <b>%s</b> not present in the list" % revid)
                        # revid == 0 is the parent of the first commit
                        if (revid != 0 ):
-                               print "Try running gitview without any options"
+                               dialog.format_secondary_text("Try running gitview without any options")
+                       dialog.run()
+                       dialog.destroy()
 
                self.treeview.grab_focus()
 
@@ -987,8 +1016,8 @@ class GitView:
                window.set_diff(commit_sha1, parent_sha1, encoding)
                self.treeview.grab_focus()
 
+without_diff = 0
 if __name__ == "__main__":
-       without_diff = 0
 
        if (len(sys.argv) > 1 ):
                if (sys.argv[1] == "--without-diff"):
index fcf759c..6924df2 100644 (file)
@@ -25,6 +25,15 @@ OPTIONS
 
        <args>
                All the valid option for git-rev-list(1)
+       Key Bindings:
+       F4:
+               To maximize the window
+       F5:
+               To reread references.
+       F11:
+               Full screen
+       F12:
+               Leave full screen
 
 EXAMPLES
 ------
@@ -33,6 +42,5 @@ EXAMPLES
          or drivers/scsi subdirectories
 
        gitview --since=2.weeks.ago
-         Show the changes during the last two weeks 
+         Show the changes during the last two weeks
 
-       
diff --git a/contrib/remotes2config.sh b/contrib/remotes2config.sh
new file mode 100644 (file)
index 0000000..25901e2
--- /dev/null
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+# Use this tool to rewrite your .git/remotes/ files into the config.
+
+. git-sh-setup
+
+if [ -d "$GIT_DIR"/remotes ]; then
+       echo "Rewriting $GIT_DIR/remotes" >&2
+       error=0
+       # rewrite into config
+       {
+               cd "$GIT_DIR"/remotes
+               ls | while read f; do
+                       name=$(echo -n "$f" | tr -c "A-Za-z0-9" ".")
+                       sed -n \
+                       -e "s/^URL: \(.*\)$/remote.$name.url \1 ./p" \
+                       -e "s/^Pull: \(.*\)$/remote.$name.fetch \1 ^$ /p" \
+                       -e "s/^Push: \(.*\)$/remote.$name.push \1 ^$ /p" \
+                       < "$f"
+               done
+               echo done
+       } | while read key value regex; do
+               case $key in
+               done)
+                       if [ $error = 0 ]; then
+                               mv "$GIT_DIR"/remotes "$GIT_DIR"/remotes.old
+                       fi ;;
+               *)
+                       echo "git-repo-config $key "$value" $regex"
+                       git-repo-config $key "$value" $regex || error=1 ;;
+               esac
+       done
+fi
+
+
index 12aacef..a67d6b4 100644 (file)
@@ -321,8 +321,10 @@ int main(int argc, char **argv)
 
        setup_git_directory();
 
-       if (argc != 2 || get_sha1(argv[1], sha1))
+       if (argc != 2)
                usage("git-convert-objects <sha1>");
+       if (get_sha1(argv[1], sha1))
+               die("Not a valid object name %s", argv[1]);
 
        entry = convert_entry(sha1);
        printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
index 776749e..2f03f99 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -267,12 +267,17 @@ static int upload(char *dir)
 static int execute(void)
 {
        static char line[1000];
-       int len;
+       int pktlen, len;
 
        alarm(init_timeout ? init_timeout : timeout);
-       len = packet_read_line(0, line, sizeof(line));
+       pktlen = packet_read_line(0, line, sizeof(line));
        alarm(0);
 
+       len = strlen(line);
+       if (pktlen != len)
+               loginfo("Extended attributes (%d bytes) exist <%.*s>",
+                       (int) pktlen - len,
+                       (int) pktlen - len, line + len + 1);
        if (len && line[len-1] == '\n')
                line[--len] = 0;
 
diff --git a/date.c b/date.c
index 034d722..66be23a 100644 (file)
--- a/date.c
+++ b/date.c
@@ -42,18 +42,24 @@ static const char *weekday_names[] = {
  * thing, which means that tz -0100 is passed in as the integer -100,
  * even though it means "sixty minutes off"
  */
-const char *show_date(unsigned long time, int tz)
+static struct tm *time_to_tm(unsigned long time, int tz)
 {
-       struct tm *tm;
        time_t t;
-       static char timebuf[200];
        int minutes;
 
        minutes = tz < 0 ? -tz : tz;
        minutes = (minutes / 100)*60 + (minutes % 100);
        minutes = tz < 0 ? -minutes : minutes;
        t = time + minutes * 60;
-       tm = gmtime(&t);
+       return gmtime(&t);
+}
+
+const char *show_date(unsigned long time, int tz)
+{
+       struct tm *tm;
+       static char timebuf[200];
+
+       tm = time_to_tm(time, tz);
        if (!tm)
                return NULL;
        sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
@@ -65,6 +71,21 @@ const char *show_date(unsigned long time, int tz)
        return timebuf;
 }
 
+const char *show_rfc2822_date(unsigned long time, int tz)
+{
+       struct tm *tm;
+       static char timebuf[200];
+
+       tm = time_to_tm(time, tz);
+       if (!tm)
+               return NULL;
+       sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
+               weekday_names[tm->tm_wday], tm->tm_mday,
+               month_names[tm->tm_mon], tm->tm_year + 1900,
+               tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
+       return timebuf;
+}
+
 /*
  * Check these. And note how it doesn't do the summer-time conversion.
  *
@@ -348,7 +369,7 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
 
        /* Four-digit year or a timezone? */
        if (n == 4) {
-               if (num <= 1200 && *offset == -1) {
+               if (num <= 1400 && *offset == -1) {
                        unsigned int minutes = num % 100;
                        unsigned int hours = num / 100;
                        *offset = hours*60 + minutes;
diff --git a/delta.h b/delta.h
index 9464f3e..7b3f86d 100644 (file)
--- a/delta.h
+++ b/delta.h
@@ -1,12 +1,75 @@
 #ifndef DELTA_H
 #define DELTA_H
 
-/* handling of delta buffers */
-extern void *diff_delta(void *from_buf, unsigned long from_size,
-                       void *to_buf, unsigned long to_size,
-                       unsigned long *delta_size, unsigned long max_size);
-extern void *patch_delta(void *src_buf, unsigned long src_size,
-                        void *delta_buf, unsigned long delta_size,
+/* opaque object for delta index */
+struct delta_index;
+
+/*
+ * create_delta_index: compute index data from given buffer
+ *
+ * This returns a pointer to a struct delta_index that should be passed to
+ * subsequent create_delta() calls, or to free_delta_index().  A NULL pointer
+ * is returned on failure.  The given buffer must not be freed nor altered
+ * before free_delta_index() is called.  The returned pointer must be freed
+ * using free_delta_index().
+ */
+extern struct delta_index *
+create_delta_index(const void *buf, unsigned long bufsize);
+
+/*
+ * free_delta_index: free the index created by create_delta_index()
+ *
+ * Given pointer must be what create_delta_index() returned, or NULL.
+ */
+extern void free_delta_index(struct delta_index *index);
+
+/*
+ * create_delta: create a delta from given index for the given buffer
+ *
+ * This function may be called multiple times with different buffers using
+ * the same delta_index pointer.  If max_delta_size is non-zero and the
+ * resulting delta is to be larger than max_delta_size then NULL is returned.
+ * On success, a non-NULL pointer to the buffer with the delta data is
+ * returned and *delta_size is updated with its size.  The returned buffer
+ * must be freed by the caller.
+ */
+extern void *
+create_delta(const struct delta_index *index,
+            const void *buf, unsigned long bufsize,
+            unsigned long *delta_size, unsigned long max_delta_size);
+
+/*
+ * diff_delta: create a delta from source buffer to target buffer
+ *
+ * If max_delta_size is non-zero and the resulting delta is to be larger
+ * than max_delta_size then NULL is returned.  On success, a non-NULL
+ * pointer to the buffer with the delta data is returned and *delta_size is
+ * updated with its size.  The returned buffer must be freed by the caller.
+ */
+static inline void *
+diff_delta(const void *src_buf, unsigned long src_bufsize,
+          const void *trg_buf, unsigned long trg_bufsize,
+          unsigned long *delta_size, unsigned long max_delta_size)
+{
+       struct delta_index *index = create_delta_index(src_buf, src_bufsize);
+       if (index) {
+               void *delta = create_delta(index, trg_buf, trg_bufsize,
+                                          delta_size, max_delta_size);
+               free_delta_index(index);
+               return delta;
+       }
+       return NULL;
+}
+
+/*
+ * patch_delta: recreate target buffer given source buffer and delta data
+ *
+ * On success, a non-NULL pointer to the target buffer is returned and
+ * *trg_bufsize is updated with its size.  On failure a NULL pointer is
+ * returned.  The returned buffer must be freed by the caller.
+ */
+extern void *patch_delta(const void *src_buf, unsigned long src_size,
+                        const void *delta_buf, unsigned long delta_size,
                         unsigned long *dst_size);
 
 /* the smallest possible delta size is 4 bytes */
@@ -14,7 +77,7 @@ extern void *patch_delta(void *src_buf, unsigned long src_size,
 
 /*
  * This must be called twice on the delta data buffer, first to get the
- * expected reference buffer size, and again to get the result buffer size.
+ * expected source buffer size, and again to get the target buffer size.
  */
 static inline unsigned long get_delta_hdr_size(const unsigned char **datap,
                                               const unsigned char *top)
index ff65742..8a9cd5d 100644 (file)
@@ -105,11 +105,11 @@ static void describe(char *arg, int last_one)
        static int initialized = 0;
        struct commit_name *n;
 
-       if (get_sha1(arg, sha1) < 0)
-               usage(describe_usage);
+       if (get_sha1(arg, sha1))
+               die("Not a valid object name %s", arg);
        cmit = lookup_commit_reference(sha1);
        if (!cmit)
-               usage(describe_usage);
+               die("%s is not a valid '%s' object", arg, commit_type);
 
        if (!initialized) {
                initialized = 1;
index 1188b31..25a798d 100644 (file)
 
 #include <stdlib.h>
 #include <string.h>
-#include <zlib.h>
 #include "delta.h"
 
 
-/* block size: min = 16, max = 64k, power of 2 */
-#define BLK_SIZE 16
-
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
+/* maximum hash entry list for the same hash bucket */
+#define HASH_LIMIT 64
+
+#define RABIN_SHIFT 23
+#define RABIN_WINDOW 16
+
+static const unsigned int T[256] = {
+       0x00000000, 0xab59b4d1, 0x56b369a2, 0xfdeadd73, 0x063f6795, 0xad66d344,
+       0x508c0e37, 0xfbd5bae6, 0x0c7ecf2a, 0xa7277bfb, 0x5acda688, 0xf1941259,
+       0x0a41a8bf, 0xa1181c6e, 0x5cf2c11d, 0xf7ab75cc, 0x18fd9e54, 0xb3a42a85,
+       0x4e4ef7f6, 0xe5174327, 0x1ec2f9c1, 0xb59b4d10, 0x48719063, 0xe32824b2,
+       0x1483517e, 0xbfdae5af, 0x423038dc, 0xe9698c0d, 0x12bc36eb, 0xb9e5823a,
+       0x440f5f49, 0xef56eb98, 0x31fb3ca8, 0x9aa28879, 0x6748550a, 0xcc11e1db,
+       0x37c45b3d, 0x9c9defec, 0x6177329f, 0xca2e864e, 0x3d85f382, 0x96dc4753,
+       0x6b369a20, 0xc06f2ef1, 0x3bba9417, 0x90e320c6, 0x6d09fdb5, 0xc6504964,
+       0x2906a2fc, 0x825f162d, 0x7fb5cb5e, 0xd4ec7f8f, 0x2f39c569, 0x846071b8,
+       0x798aaccb, 0xd2d3181a, 0x25786dd6, 0x8e21d907, 0x73cb0474, 0xd892b0a5,
+       0x23470a43, 0x881ebe92, 0x75f463e1, 0xdeadd730, 0x63f67950, 0xc8afcd81,
+       0x354510f2, 0x9e1ca423, 0x65c91ec5, 0xce90aa14, 0x337a7767, 0x9823c3b6,
+       0x6f88b67a, 0xc4d102ab, 0x393bdfd8, 0x92626b09, 0x69b7d1ef, 0xc2ee653e,
+       0x3f04b84d, 0x945d0c9c, 0x7b0be704, 0xd05253d5, 0x2db88ea6, 0x86e13a77,
+       0x7d348091, 0xd66d3440, 0x2b87e933, 0x80de5de2, 0x7775282e, 0xdc2c9cff,
+       0x21c6418c, 0x8a9ff55d, 0x714a4fbb, 0xda13fb6a, 0x27f92619, 0x8ca092c8,
+       0x520d45f8, 0xf954f129, 0x04be2c5a, 0xafe7988b, 0x5432226d, 0xff6b96bc,
+       0x02814bcf, 0xa9d8ff1e, 0x5e738ad2, 0xf52a3e03, 0x08c0e370, 0xa39957a1,
+       0x584ced47, 0xf3155996, 0x0eff84e5, 0xa5a63034, 0x4af0dbac, 0xe1a96f7d,
+       0x1c43b20e, 0xb71a06df, 0x4ccfbc39, 0xe79608e8, 0x1a7cd59b, 0xb125614a,
+       0x468e1486, 0xedd7a057, 0x103d7d24, 0xbb64c9f5, 0x40b17313, 0xebe8c7c2,
+       0x16021ab1, 0xbd5bae60, 0x6cb54671, 0xc7ecf2a0, 0x3a062fd3, 0x915f9b02,
+       0x6a8a21e4, 0xc1d39535, 0x3c394846, 0x9760fc97, 0x60cb895b, 0xcb923d8a,
+       0x3678e0f9, 0x9d215428, 0x66f4eece, 0xcdad5a1f, 0x3047876c, 0x9b1e33bd,
+       0x7448d825, 0xdf116cf4, 0x22fbb187, 0x89a20556, 0x7277bfb0, 0xd92e0b61,
+       0x24c4d612, 0x8f9d62c3, 0x7836170f, 0xd36fa3de, 0x2e857ead, 0x85dcca7c,
+       0x7e09709a, 0xd550c44b, 0x28ba1938, 0x83e3ade9, 0x5d4e7ad9, 0xf617ce08,
+       0x0bfd137b, 0xa0a4a7aa, 0x5b711d4c, 0xf028a99d, 0x0dc274ee, 0xa69bc03f,
+       0x5130b5f3, 0xfa690122, 0x0783dc51, 0xacda6880, 0x570fd266, 0xfc5666b7,
+       0x01bcbbc4, 0xaae50f15, 0x45b3e48d, 0xeeea505c, 0x13008d2f, 0xb85939fe,
+       0x438c8318, 0xe8d537c9, 0x153feaba, 0xbe665e6b, 0x49cd2ba7, 0xe2949f76,
+       0x1f7e4205, 0xb427f6d4, 0x4ff24c32, 0xe4abf8e3, 0x19412590, 0xb2189141,
+       0x0f433f21, 0xa41a8bf0, 0x59f05683, 0xf2a9e252, 0x097c58b4, 0xa225ec65,
+       0x5fcf3116, 0xf49685c7, 0x033df00b, 0xa86444da, 0x558e99a9, 0xfed72d78,
+       0x0502979e, 0xae5b234f, 0x53b1fe3c, 0xf8e84aed, 0x17bea175, 0xbce715a4,
+       0x410dc8d7, 0xea547c06, 0x1181c6e0, 0xbad87231, 0x4732af42, 0xec6b1b93,
+       0x1bc06e5f, 0xb099da8e, 0x4d7307fd, 0xe62ab32c, 0x1dff09ca, 0xb6a6bd1b,
+       0x4b4c6068, 0xe015d4b9, 0x3eb80389, 0x95e1b758, 0x680b6a2b, 0xc352defa,
+       0x3887641c, 0x93ded0cd, 0x6e340dbe, 0xc56db96f, 0x32c6cca3, 0x999f7872,
+       0x6475a501, 0xcf2c11d0, 0x34f9ab36, 0x9fa01fe7, 0x624ac294, 0xc9137645,
+       0x26459ddd, 0x8d1c290c, 0x70f6f47f, 0xdbaf40ae, 0x207afa48, 0x8b234e99,
+       0x76c993ea, 0xdd90273b, 0x2a3b52f7, 0x8162e626, 0x7c883b55, 0xd7d18f84,
+       0x2c043562, 0x875d81b3, 0x7ab75cc0, 0xd1eee811
+};
 
-#define GR_PRIME 0x9e370001
-#define HASH(v, shift) (((unsigned int)(v) * GR_PRIME) >> (shift))
+static const unsigned int U[256] = {
+       0x00000000, 0x7eb5200d, 0x5633f4cb, 0x2886d4c6, 0x073e5d47, 0x798b7d4a,
+       0x510da98c, 0x2fb88981, 0x0e7cba8e, 0x70c99a83, 0x584f4e45, 0x26fa6e48,
+       0x0942e7c9, 0x77f7c7c4, 0x5f711302, 0x21c4330f, 0x1cf9751c, 0x624c5511,
+       0x4aca81d7, 0x347fa1da, 0x1bc7285b, 0x65720856, 0x4df4dc90, 0x3341fc9d,
+       0x1285cf92, 0x6c30ef9f, 0x44b63b59, 0x3a031b54, 0x15bb92d5, 0x6b0eb2d8,
+       0x4388661e, 0x3d3d4613, 0x39f2ea38, 0x4747ca35, 0x6fc11ef3, 0x11743efe,
+       0x3eccb77f, 0x40799772, 0x68ff43b4, 0x164a63b9, 0x378e50b6, 0x493b70bb,
+       0x61bda47d, 0x1f088470, 0x30b00df1, 0x4e052dfc, 0x6683f93a, 0x1836d937,
+       0x250b9f24, 0x5bbebf29, 0x73386bef, 0x0d8d4be2, 0x2235c263, 0x5c80e26e,
+       0x740636a8, 0x0ab316a5, 0x2b7725aa, 0x55c205a7, 0x7d44d161, 0x03f1f16c,
+       0x2c4978ed, 0x52fc58e0, 0x7a7a8c26, 0x04cfac2b, 0x73e5d470, 0x0d50f47d,
+       0x25d620bb, 0x5b6300b6, 0x74db8937, 0x0a6ea93a, 0x22e87dfc, 0x5c5d5df1,
+       0x7d996efe, 0x032c4ef3, 0x2baa9a35, 0x551fba38, 0x7aa733b9, 0x041213b4,
+       0x2c94c772, 0x5221e77f, 0x6f1ca16c, 0x11a98161, 0x392f55a7, 0x479a75aa,
+       0x6822fc2b, 0x1697dc26, 0x3e1108e0, 0x40a428ed, 0x61601be2, 0x1fd53bef,
+       0x3753ef29, 0x49e6cf24, 0x665e46a5, 0x18eb66a8, 0x306db26e, 0x4ed89263,
+       0x4a173e48, 0x34a21e45, 0x1c24ca83, 0x6291ea8e, 0x4d29630f, 0x339c4302,
+       0x1b1a97c4, 0x65afb7c9, 0x446b84c6, 0x3adea4cb, 0x1258700d, 0x6ced5000,
+       0x4355d981, 0x3de0f98c, 0x15662d4a, 0x6bd30d47, 0x56ee4b54, 0x285b6b59,
+       0x00ddbf9f, 0x7e689f92, 0x51d01613, 0x2f65361e, 0x07e3e2d8, 0x7956c2d5,
+       0x5892f1da, 0x2627d1d7, 0x0ea10511, 0x7014251c, 0x5facac9d, 0x21198c90,
+       0x099f5856, 0x772a785b, 0x4c921c31, 0x32273c3c, 0x1aa1e8fa, 0x6414c8f7,
+       0x4bac4176, 0x3519617b, 0x1d9fb5bd, 0x632a95b0, 0x42eea6bf, 0x3c5b86b2,
+       0x14dd5274, 0x6a687279, 0x45d0fbf8, 0x3b65dbf5, 0x13e30f33, 0x6d562f3e,
+       0x506b692d, 0x2ede4920, 0x06589de6, 0x78edbdeb, 0x5755346a, 0x29e01467,
+       0x0166c0a1, 0x7fd3e0ac, 0x5e17d3a3, 0x20a2f3ae, 0x08242768, 0x76910765,
+       0x59298ee4, 0x279caee9, 0x0f1a7a2f, 0x71af5a22, 0x7560f609, 0x0bd5d604,
+       0x235302c2, 0x5de622cf, 0x725eab4e, 0x0ceb8b43, 0x246d5f85, 0x5ad87f88,
+       0x7b1c4c87, 0x05a96c8a, 0x2d2fb84c, 0x539a9841, 0x7c2211c0, 0x029731cd,
+       0x2a11e50b, 0x54a4c506, 0x69998315, 0x172ca318, 0x3faa77de, 0x411f57d3,
+       0x6ea7de52, 0x1012fe5f, 0x38942a99, 0x46210a94, 0x67e5399b, 0x19501996,
+       0x31d6cd50, 0x4f63ed5d, 0x60db64dc, 0x1e6e44d1, 0x36e89017, 0x485db01a,
+       0x3f77c841, 0x41c2e84c, 0x69443c8a, 0x17f11c87, 0x38499506, 0x46fcb50b,
+       0x6e7a61cd, 0x10cf41c0, 0x310b72cf, 0x4fbe52c2, 0x67388604, 0x198da609,
+       0x36352f88, 0x48800f85, 0x6006db43, 0x1eb3fb4e, 0x238ebd5d, 0x5d3b9d50,
+       0x75bd4996, 0x0b08699b, 0x24b0e01a, 0x5a05c017, 0x728314d1, 0x0c3634dc,
+       0x2df207d3, 0x534727de, 0x7bc1f318, 0x0574d315, 0x2acc5a94, 0x54797a99,
+       0x7cffae5f, 0x024a8e52, 0x06852279, 0x78300274, 0x50b6d6b2, 0x2e03f6bf,
+       0x01bb7f3e, 0x7f0e5f33, 0x57888bf5, 0x293dabf8, 0x08f998f7, 0x764cb8fa,
+       0x5eca6c3c, 0x207f4c31, 0x0fc7c5b0, 0x7172e5bd, 0x59f4317b, 0x27411176,
+       0x1a7c5765, 0x64c97768, 0x4c4fa3ae, 0x32fa83a3, 0x1d420a22, 0x63f72a2f,
+       0x4b71fee9, 0x35c4dee4, 0x1400edeb, 0x6ab5cde6, 0x42331920, 0x3c86392d,
+       0x133eb0ac, 0x6d8b90a1, 0x450d4467, 0x3bb8646a
+};
 
-struct index {
+struct index_entry {
        const unsigned char *ptr;
        unsigned int val;
-       struct index *next;
+       struct index_entry *next;
+};
+
+struct delta_index {
+       const void *src_buf;
+       unsigned long src_size;
+       unsigned int hash_mask;
+       struct index_entry *hash[0];
 };
 
-static struct index ** delta_index(const unsigned char *buf,
-                                  unsigned long bufsize,
-                                  unsigned long trg_bufsize,
-                                  unsigned int *hash_shift)
+struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
 {
-       unsigned int i, hsize, hshift, hlimit, entries, *hash_count;
-       const unsigned char *data;
-       struct index *entry, **hash;
+       unsigned int i, hsize, hmask, entries, prev_val, *hash_count;
+       const unsigned char *data, *buffer = buf;
+       struct delta_index *index;
+       struct index_entry *entry, **hash;
        void *mem;
+       unsigned long memsize;
+
+       if (!buf || !bufsize)
+               return NULL;
 
-       /* determine index hash size */
-       entries = bufsize  / BLK_SIZE;
+       /* Determine index hash size.  Note that indexing skips the
+          first byte to allow for optimizing the rabin polynomial
+          initialization in create_delta(). */
+       entries = (bufsize - 1)  / RABIN_WINDOW;
        hsize = entries / 4;
        for (i = 4; (1 << i) < hsize && i < 31; i++);
        hsize = 1 << i;
-       hshift = 32 - i;
-       *hash_shift = hshift;
+       hmask = hsize - 1;
 
        /* allocate lookup index */
-       mem = malloc(hsize * sizeof(*hash) + entries * sizeof(*entry));
+       memsize = sizeof(*index) +
+                 sizeof(*hash) * hsize +
+                 sizeof(*entry) * entries;
+       mem = malloc(memsize);
        if (!mem)
                return NULL;
+       index = mem;
+       mem = index + 1;
        hash = mem;
-       entry = mem + hsize * sizeof(*hash);
+       mem = hash + hsize;
+       entry = mem;
+
+       index->src_buf = buf;
+       index->src_size = bufsize;
+       index->hash_mask = hmask;
        memset(hash, 0, hsize * sizeof(*hash));
 
        /* allocate an array to count hash entries */
        hash_count = calloc(hsize, sizeof(*hash_count));
        if (!hash_count) {
-               free(hash);
+               free(index);
                return NULL;
        }
 
        /* then populate the index */
-       data = buf + entries * BLK_SIZE - BLK_SIZE;
-       while (data >= buf) {
-               unsigned int val = adler32(0, data, BLK_SIZE);
-               i = HASH(val, hshift);
-               entry->ptr = data;
-               entry->val = val;
-               entry->next = hash[i];
-               hash[i] = entry++;
-               hash_count[i]++;
-               data -= BLK_SIZE;
-       }
+       prev_val = ~0;
+       for (data = buffer + entries * RABIN_WINDOW - RABIN_WINDOW;
+            data >= buffer;
+            data -= RABIN_WINDOW) {
+               unsigned int val = 0;
+               for (i = 1; i <= RABIN_WINDOW; i++)
+                       val = ((val << 8) | data[i]) ^ T[val >> RABIN_SHIFT];
+               if (val == prev_val) {
+                       /* keep the lowest of consecutive identical blocks */
+                       entry[-1].ptr = data + RABIN_WINDOW;
+               } else {
+                       prev_val = val;
+                       i = val & hmask;
+                       entry->ptr = data + RABIN_WINDOW;
+                       entry->val = val;
+                       entry->next = hash[i];
+                       hash[i] = entry++;
+                       hash_count[i]++;
+               }
+       }
 
        /*
         * Determine a limit on the number of entries in the same hash
@@ -91,27 +209,18 @@ static struct index ** delta_index(const unsigned char *buf,
         * bucket that would bring us to O(m*n) computing costs (m and n
         * corresponding to reference and target buffer sizes).
         *
-        * The more the target buffer is large, the more it is important to
-        * have small entry lists for each hash buckets.  With such a limit
-        * the cost is bounded to something more like O(m+n).
-        */
-       hlimit = (1 << 26) / trg_bufsize;
-       if (hlimit < 4*BLK_SIZE)
-               hlimit = 4*BLK_SIZE;
-
-       /*
-        * Now make sure none of the hash buckets has more entries than
+        * Make sure none of the hash buckets has more entries than
         * we're willing to test.  Otherwise we cull the entry list
         * uniformly to still preserve a good repartition across
         * the reference buffer.
         */
        for (i = 0; i < hsize; i++) {
-               if (hash_count[i] < hlimit)
+               if (hash_count[i] < HASH_LIMIT)
                        continue;
                entry = hash[i];
                do {
-                       struct index *keep = entry;
-                       int skip = hash_count[i] / hlimit / 2;
+                       struct index_entry *keep = entry;
+                       int skip = hash_count[i] / HASH_LIMIT / 2;
                        do {
                                entry = entry->next;
                        } while(--skip && entry);
@@ -120,32 +229,31 @@ static struct index ** delta_index(const unsigned char *buf,
        }
        free(hash_count);
 
-       return hash;
+       return index;
 }
 
-/* provide the size of the copy opcode given the block offset and size */
-#define COPYOP_SIZE(o, s) \
-    (!!(o & 0xff) + !!(o & 0xff00) + !!(o & 0xff0000) + !!(o & 0xff000000) + \
-     !!(s & 0xff) + !!(s & 0xff00) + 1)
+void free_delta_index(struct delta_index *index)
+{
+       free(index);
+}
 
-/* the maximum size for any opcode */
-#define MAX_OP_SIZE COPYOP_SIZE(0xffffffff, 0xffffffff)
+/*
+ * The maximum size for any opcode sequence, including the initial header
+ * plus rabin window plus biggest copy.
+ */
+#define MAX_OP_SIZE    (5 + 5 + 1 + RABIN_WINDOW + 7)
 
-void *diff_delta(void *from_buf, unsigned long from_size,
-                void *to_buf, unsigned long to_size,
-                unsigned long *delta_size,
-                unsigned long max_size)
+void *
+create_delta(const struct delta_index *index,
+            const void *trg_buf, unsigned long trg_size,
+            unsigned long *delta_size, unsigned long max_size)
 {
-       unsigned int i, outpos, outsize, hash_shift;
+       unsigned int i, outpos, outsize, val;
        int inscnt;
        const unsigned char *ref_data, *ref_top, *data, *top;
        unsigned char *out;
-       struct index *entry, **hash;
 
-       if (!from_size || !to_size)
-               return NULL;
-       hash = delta_index(from_buf, from_size, to_size, &hash_shift);
-       if (!hash)
+       if (!trg_buf || !trg_size)
                return NULL;
 
        outpos = 0;
@@ -153,64 +261,66 @@ void *diff_delta(void *from_buf, unsigned long from_size,
        if (max_size && outsize >= max_size)
                outsize = max_size + MAX_OP_SIZE + 1;
        out = malloc(outsize);
-       if (!out) {
-               free(hash);
+       if (!out)
                return NULL;
-       }
-
-       ref_data = from_buf;
-       ref_top = from_buf + from_size;
-       data = to_buf;
-       top = to_buf + to_size;
 
        /* store reference buffer size */
-       out[outpos++] = from_size;
-       from_size >>= 7;
-       while (from_size) {
-               out[outpos - 1] |= 0x80;
-               out[outpos++] = from_size;
-               from_size >>= 7;
+       i = index->src_size;
+       while (i >= 0x80) {
+               out[outpos++] = i | 0x80;
+               i >>= 7;
        }
+       out[outpos++] = i;
 
        /* store target buffer size */
-       out[outpos++] = to_size;
-       to_size >>= 7;
-       while (to_size) {
-               out[outpos - 1] |= 0x80;
-               out[outpos++] = to_size;
-               to_size >>= 7;
+       i = trg_size;
+       while (i >= 0x80) {
+               out[outpos++] = i | 0x80;
+               i >>= 7;
        }
-
-       inscnt = 0;
+       out[outpos++] = i;
+
+       ref_data = index->src_buf;
+       ref_top = ref_data + index->src_size;
+       data = trg_buf;
+       top = trg_buf + trg_size;
+
+       outpos++;
+       val = 0;
+       for (i = 0; i < RABIN_WINDOW && data < top; i++, data++) {
+               out[outpos++] = *data;
+               val = ((val << 8) | *data) ^ T[val >> RABIN_SHIFT];
+       }
+       inscnt = i;
 
        while (data < top) {
                unsigned int moff = 0, msize = 0;
-               if (data + BLK_SIZE <= top) {
-                       unsigned int val = adler32(0, data, BLK_SIZE);
-                       i = HASH(val, hash_shift);
-                       for (entry = hash[i]; entry; entry = entry->next) {
-                               const unsigned char *ref = entry->ptr;
-                               const unsigned char *src = data;
-                               unsigned int ref_size = ref_top - ref;
-                               if (entry->val != val)
-                                       continue;
-                               if (ref_size > top - src)
-                                       ref_size = top - src;
-                               if (ref_size > 0x10000)
-                                       ref_size = 0x10000;
-                               if (ref_size <= msize)
-                                       break;
-                               while (ref_size-- && *src++ == *ref)
-                                       ref++;
-                               if (msize < ref - entry->ptr) {
-                                       /* this is our best match so far */
-                                       msize = ref - entry->ptr;
-                                       moff = entry->ptr - ref_data;
-                               }
+               struct index_entry *entry;
+               val ^= U[data[-RABIN_WINDOW]];
+               val = ((val << 8) | *data) ^ T[val >> RABIN_SHIFT];
+               i = val & index->hash_mask;
+               for (entry = index->hash[i]; entry; entry = entry->next) {
+                       const unsigned char *ref = entry->ptr;
+                       const unsigned char *src = data;
+                       unsigned int ref_size = ref_top - ref;
+                       if (entry->val != val)
+                               continue;
+                       if (ref_size > top - src)
+                               ref_size = top - src;
+                       if (ref_size > 0x10000)
+                               ref_size = 0x10000;
+                       if (ref_size <= msize)
+                               break;
+                       while (ref_size-- && *src++ == *ref)
+                               ref++;
+                       if (msize < ref - entry->ptr) {
+                               /* this is our best match so far */
+                               msize = ref - entry->ptr;
+                               moff = entry->ptr - ref_data;
                        }
                }
 
-               if (!msize || msize < COPYOP_SIZE(moff, msize)) {
+               if (msize < 4) {
                        if (!inscnt)
                                outpos++;
                        out[outpos++] = *data++;
@@ -222,6 +332,20 @@ void *diff_delta(void *from_buf, unsigned long from_size,
                } else {
                        unsigned char *op;
 
+                       if (msize >= RABIN_WINDOW) {
+                               const unsigned char *sk;
+                               sk = data + msize - RABIN_WINDOW;
+                               val = 0;
+                               for (i = 0; i < RABIN_WINDOW; i++)
+                                       val = ((val << 8) | *sk++) ^ T[val >> RABIN_SHIFT];
+                       } else {
+                               const unsigned char *sk = data + 1;
+                               for (i = 1; i < msize; i++) {
+                                       val ^= U[sk[-RABIN_WINDOW]];
+                                       val = ((val << 8) | *sk++) ^ T[val >> RABIN_SHIFT];
+                               }
+                       }
+
                        if (inscnt) {
                                while (moff && ref_data[moff-1] == data[-1]) {
                                        if (msize == 0x10000)
@@ -266,12 +390,10 @@ void *diff_delta(void *from_buf, unsigned long from_size,
                        if (max_size && outsize >= max_size)
                                outsize = max_size + MAX_OP_SIZE + 1;
                        if (max_size && outpos > max_size)
-                               out = NULL;
-                       else
-                               out = realloc(out, outsize);
+                               break;
+                       out = realloc(out, outsize);
                        if (!out) {
                                free(tmp);
-                               free(hash);
                                return NULL;
                        }
                }
@@ -280,7 +402,11 @@ void *diff_delta(void *from_buf, unsigned long from_size,
        if (inscnt)
                out[outpos - inscnt - 1] = inscnt;
 
-       free(hash);
+       if (max_size && outpos > max_size) {
+               free(out);
+               return NULL;
+       }
+
        *delta_size = outpos;
        return out;
 }
diff --git a/diff-files.c b/diff-files.c
deleted file mode 100644 (file)
index b9d193d..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * GIT - The information manager from hell
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#include "cache.h"
-#include "diff.h"
-#include "commit.h"
-#include "revision.h"
-
-static const char diff_files_usage[] =
-"git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]"
-COMMON_DIFF_OPTIONS_HELP;
-
-int main(int argc, const char **argv)
-{
-       struct rev_info rev;
-       int silent = 0;
-
-       git_config(git_diff_config);
-       init_revisions(&rev);
-       rev.abbrev = 0;
-
-       argc = setup_revisions(argc, argv, &rev, NULL);
-       while (1 < argc && argv[1][0] == '-') {
-               if (!strcmp(argv[1], "--base"))
-                       rev.max_count = 1;
-               else if (!strcmp(argv[1], "--ours"))
-                       rev.max_count = 2;
-               else if (!strcmp(argv[1], "--theirs"))
-                       rev.max_count = 3;
-               else if (!strcmp(argv[1], "-q"))
-                       silent = 1;
-               else
-                       usage(diff_files_usage);
-               argv++; argc--;
-       }
-       /*
-        * Make sure there are NO revision (i.e. pending object) parameter,
-        * rev.max_count is reasonable (0 <= n <= 3),
-        * there is no other revision filtering parameters.
-        */
-       if (rev.pending_objects ||
-           rev.min_age != -1 || rev.max_age != -1)
-               usage(diff_files_usage);
-       /*
-        * Backward compatibility wart - "diff-files -s" used to
-        * defeat the common diff option "-s" which asked for
-        * DIFF_FORMAT_NO_OUTPUT.
-        */
-       if (rev.diffopt.output_format == DIFF_FORMAT_NO_OUTPUT)
-               rev.diffopt.output_format = DIFF_FORMAT_RAW;
-       return run_diff_files(&rev, silent);
-}
diff --git a/diff-index.c b/diff-index.c
deleted file mode 100644 (file)
index 8694012..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-#include "cache.h"
-#include "diff.h"
-#include "commit.h"
-#include "revision.h"
-
-static const char diff_cache_usage[] =
-"git-diff-index [-m] [--cached] "
-"[<common diff options>] <tree-ish> [<path>...]"
-COMMON_DIFF_OPTIONS_HELP;
-
-int main(int argc, const char **argv)
-{
-       struct rev_info rev;
-       int match_missing = 0;
-       int cached = 0;
-       int i;
-
-       git_config(git_diff_config);
-       init_revisions(&rev);
-       rev.abbrev = 0;
-
-       argc = setup_revisions(argc, argv, &rev, NULL);
-       for (i = 1; i < argc; i++) {
-               const char *arg = argv[i];
-                       
-               if (!strcmp(arg, "--cached"))
-                       cached = 1;
-               else
-                       usage(diff_cache_usage);
-       }
-       /*
-        * Make sure there is one revision (i.e. pending object),
-        * and there is no revision filtering parameters.
-        */
-       if (!rev.pending_objects || rev.pending_objects->next ||
-           rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
-               usage(diff_cache_usage);
-       return run_diff_index(&rev, cached);
-}
diff --git a/diff-stages.c b/diff-stages.c
deleted file mode 100644 (file)
index dcd20e7..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (c) 2005 Junio C Hamano
- */
-
-#include "cache.h"
-#include "diff.h"
-
-static struct diff_options diff_options;
-
-static const char diff_stages_usage[] =
-"git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
-COMMON_DIFF_OPTIONS_HELP;
-
-static void diff_stages(int stage1, int stage2, const char **pathspec)
-{
-       int i = 0;
-       while (i < active_nr) {
-               struct cache_entry *ce, *stages[4] = { NULL, };
-               struct cache_entry *one, *two;
-               const char *name;
-               int len, skip;
-
-               ce = active_cache[i];
-               skip = !ce_path_match(ce, pathspec);
-               len = ce_namelen(ce);
-               name = ce->name;
-               for (;;) {
-                       int stage = ce_stage(ce);
-                       stages[stage] = ce;
-                       if (active_nr <= ++i)
-                               break;
-                       ce = active_cache[i];
-                       if (ce_namelen(ce) != len ||
-                           memcmp(name, ce->name, len))
-                               break;
-               }
-               one = stages[stage1];
-               two = stages[stage2];
-
-               if (skip || (!one && !two))
-                       continue;
-               if (!one)
-                       diff_addremove(&diff_options, '+', ntohl(two->ce_mode),
-                                      two->sha1, name, NULL);
-               else if (!two)
-                       diff_addremove(&diff_options, '-', ntohl(one->ce_mode),
-                                      one->sha1, name, NULL);
-               else if (memcmp(one->sha1, two->sha1, 20) ||
-                        (one->ce_mode != two->ce_mode) ||
-                        diff_options.find_copies_harder)
-                       diff_change(&diff_options,
-                                   ntohl(one->ce_mode), ntohl(two->ce_mode),
-                                   one->sha1, two->sha1, name, NULL);
-       }
-}
-
-int main(int ac, const char **av)
-{
-       int stage1, stage2;
-       const char *prefix = setup_git_directory();
-       const char **pathspec = NULL;
-
-       git_config(git_diff_config);
-       read_cache();
-       diff_setup(&diff_options);
-       while (1 < ac && av[1][0] == '-') {
-               const char *arg = av[1];
-               if (!strcmp(arg, "-r"))
-                       ; /* as usual */
-               else {
-                       int diff_opt_cnt;
-                       diff_opt_cnt = diff_opt_parse(&diff_options,
-                                                     av+1, ac-1);
-                       if (diff_opt_cnt < 0)
-                               usage(diff_stages_usage);
-                       else if (diff_opt_cnt) {
-                               av += diff_opt_cnt;
-                               ac -= diff_opt_cnt;
-                               continue;
-                       }
-                       else
-                               usage(diff_stages_usage);
-               }
-               ac--; av++;
-       }
-
-       if (ac < 3 ||
-           sscanf(av[1], "%d", &stage1) != 1 ||
-           ! (0 <= stage1 && stage1 <= 3) ||
-           sscanf(av[2], "%d", &stage2) != 1 ||
-           ! (0 <= stage2 && stage2 <= 3))
-               usage(diff_stages_usage);
-
-       av += 3; /* The rest from av[0] are for paths restriction. */
-       pathspec = get_pathspec(prefix, av);
-
-       if (diff_setup_done(&diff_options) < 0)
-               usage(diff_stages_usage);
-
-       diff_stages(stage1, stage2, pathspec);
-       diffcore_std(&diff_options);
-       diff_flush(&diff_options);
-       return 0;
-}
diff --git a/diff-tree.c b/diff-tree.c
deleted file mode 100644 (file)
index 7207867..0000000
+++ /dev/null
@@ -1,144 +0,0 @@
-#include "cache.h"
-#include "diff.h"
-#include "commit.h"
-#include "log-tree.h"
-
-static struct rev_info log_tree_opt;
-
-static int diff_tree_commit_sha1(const unsigned char *sha1)
-{
-       struct commit *commit = lookup_commit_reference(sha1);
-       if (!commit)
-               return -1;
-       return log_tree_commit(&log_tree_opt, commit);
-}
-
-static int diff_tree_stdin(char *line)
-{
-       int len = strlen(line);
-       unsigned char sha1[20];
-       struct commit *commit;
-
-       if (!len || line[len-1] != '\n')
-               return -1;
-       line[len-1] = 0;
-       if (get_sha1_hex(line, sha1))
-               return -1;
-       commit = lookup_commit(sha1);
-       if (!commit || parse_commit(commit))
-               return -1;
-       if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
-               /* Graft the fake parents locally to the commit */
-               int pos = 41;
-               struct commit_list **pptr, *parents;
-
-               /* Free the real parent list */
-               for (parents = commit->parents; parents; ) {
-                       struct commit_list *tmp = parents->next;
-                       free(parents);
-                       parents = tmp;
-               }
-               commit->parents = NULL;
-               pptr = &(commit->parents);
-               while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
-                       struct commit *parent = lookup_commit(sha1);
-                       if (parent) {
-                               pptr = &commit_list_insert(parent, pptr)->next;
-                       }
-                       pos += 41;
-               }
-       }
-       return log_tree_commit(&log_tree_opt, commit);
-}
-
-static const char diff_tree_usage[] =
-"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
-"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
-"  -r            diff recursively\n"
-"  --root        include the initial commit as diff against /dev/null\n"
-COMMON_DIFF_OPTIONS_HELP;
-
-int main(int argc, const char **argv)
-{
-       int nr_sha1;
-       char line[1000];
-       struct object *tree1, *tree2;
-       static struct rev_info *opt = &log_tree_opt;
-       struct object_list *list;
-       int read_stdin = 0;
-
-       git_config(git_diff_config);
-       nr_sha1 = 0;
-       init_revisions(opt);
-       opt->abbrev = 0;
-       opt->diff = 1;
-       argc = setup_revisions(argc, argv, opt, NULL);
-
-       while (--argc > 0) {
-               const char *arg = *++argv;
-
-               if (!strcmp(arg, "--stdin")) {
-                       read_stdin = 1;
-                       continue;
-               }
-               usage(diff_tree_usage);
-       }
-
-       /*
-        * NOTE! "setup_revisions()" will have inserted the revisions
-        * it parsed in reverse order. So if you do
-        *
-        *      git-diff-tree a b
-        *
-        * the commit list will be "b" -> "a" -> NULL, so we reverse
-        * the order of the objects if the first one is not marked
-        * UNINTERESTING.
-        */
-       nr_sha1 = 0;
-       list = opt->pending_objects;
-       if (list) {
-               nr_sha1++;
-               tree1 = list->item;
-               list = list->next;
-               if (list) {
-                       nr_sha1++;
-                       tree2 = tree1;
-                       tree1 = list->item;
-                       if (list->next)
-                               usage(diff_tree_usage);
-                       /* Switch them around if the second one was uninteresting.. */
-                       if (tree2->flags & UNINTERESTING) {
-                               struct object *tmp = tree2;
-                               tree2 = tree1;
-                               tree1 = tmp;
-                       }
-               }
-       }
-
-       switch (nr_sha1) {
-       case 0:
-               if (!read_stdin)
-                       usage(diff_tree_usage);
-               break;
-       case 1:
-               diff_tree_commit_sha1(tree1->sha1);
-               break;
-       case 2:
-               diff_tree_sha1(tree1->sha1,
-                              tree2->sha1,
-                              "", &opt->diffopt);
-               log_tree_diff_flush(opt);
-               break;
-       }
-
-       if (!read_stdin)
-               return 0;
-
-       if (opt->diffopt.detect_rename)
-               opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
-                                      DIFF_SETUP_USE_CACHE);
-       while (fgets(line, sizeof(line), stdin))
-               diff_tree_stdin(line);
-
-       return 0;
-}
diff --git a/diff.c b/diff.c
index 13b216f..9e9cfc8 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -8,6 +8,7 @@
 #include "quote.h"
 #include "diff.h"
 #include "diffcore.h"
+#include "delta.h"
 #include "xdiff-interface.h"
 
 static int use_size_cache;
@@ -231,11 +232,16 @@ static char *pprint_rename(const char *a, const char *b)
         * name-a => name-b
         */
        if (pfx_length + sfx_length) {
-               name = xmalloc(len_a + len_b - pfx_length - sfx_length + 7);
+               int a_midlen = len_a - pfx_length - sfx_length;
+               int b_midlen = len_b - pfx_length - sfx_length;
+               if (a_midlen < 0) a_midlen = 0;
+               if (b_midlen < 0) b_midlen = 0;
+
+               name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
                sprintf(name, "%.*s{%.*s => %.*s}%s",
                        pfx_length, a,
-                       len_a - pfx_length - sfx_length, a + pfx_length,
-                       len_b - pfx_length - sfx_length, b + pfx_length,
+                       a_midlen, a + pfx_length,
+                       b_midlen, b + pfx_length,
                        a + len_a - sfx_length);
        }
        else {
@@ -293,10 +299,10 @@ static void diffstat_consume(void *priv, char *line, unsigned long len)
 
 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
 static const char minuses[]= "----------------------------------------------------------------------";
+const char mime_boundary_leader[] = "------------";
 
 static void show_stats(struct diffstat_t* data)
 {
-       char *prefix = "";
        int i, len, add, del, total, adds = 0, dels = 0;
        int max, max_change = 0, max_len = 0;
        int total_files = data->nr;
@@ -318,6 +324,7 @@ static void show_stats(struct diffstat_t* data)
        }
 
        for (i = 0; i < data->nr; i++) {
+               char *prefix = "";
                char *name = data->files[i]->name;
                int added = data->files[i]->added;
                int deleted = data->files[i]->deleted;
@@ -391,6 +398,130 @@ static void show_stats(struct diffstat_t* data)
                        total_files, adds, dels);
 }
 
+struct checkdiff_t {
+       struct xdiff_emit_state xm;
+       const char *filename;
+       int lineno;
+};
+
+static void checkdiff_consume(void *priv, char *line, unsigned long len)
+{
+       struct checkdiff_t *data = priv;
+
+       if (line[0] == '+') {
+               int i, spaces = 0;
+
+               data->lineno++;
+
+               /* check space before tab */
+               for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
+                       if (line[i] == ' ')
+                               spaces++;
+               if (line[i - 1] == '\t' && spaces)
+                       printf("%s:%d: space before tab:%.*s\n",
+                               data->filename, data->lineno, (int)len, line);
+
+               /* check white space at line end */
+               if (line[len - 1] == '\n')
+                       len--;
+               if (isspace(line[len - 1]))
+                       printf("%s:%d: white space at end: %.*s\n",
+                               data->filename, data->lineno, (int)len, line);
+       } else if (line[0] == ' ')
+               data->lineno++;
+       else if (line[0] == '@') {
+               char *plus = strchr(line, '+');
+               if (plus)
+                       data->lineno = strtol(plus, NULL, 10);
+               else
+                       die("invalid diff");
+       }
+}
+
+static unsigned char *deflate_it(char *data,
+                                unsigned long size,
+                                unsigned long *result_size)
+{
+       int bound;
+       unsigned char *deflated;
+       z_stream stream;
+
+       memset(&stream, 0, sizeof(stream));
+       deflateInit(&stream, Z_BEST_COMPRESSION);
+       bound = deflateBound(&stream, size);
+       deflated = xmalloc(bound);
+       stream.next_out = deflated;
+       stream.avail_out = bound;
+
+       stream.next_in = (unsigned char *)data;
+       stream.avail_in = size;
+       while (deflate(&stream, Z_FINISH) == Z_OK)
+               ; /* nothing */
+       deflateEnd(&stream);
+       *result_size = stream.total_out;
+       return deflated;
+}
+
+static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
+{
+       void *cp;
+       void *delta;
+       void *deflated;
+       void *data;
+       unsigned long orig_size;
+       unsigned long delta_size;
+       unsigned long deflate_size;
+       unsigned long data_size;
+
+       printf("GIT binary patch\n");
+       /* We could do deflated delta, or we could do just deflated two,
+        * whichever is smaller.
+        */
+       delta = NULL;
+       deflated = deflate_it(two->ptr, two->size, &deflate_size);
+       if (one->size && two->size) {
+               delta = diff_delta(one->ptr, one->size,
+                                  two->ptr, two->size,
+                                  &delta_size, deflate_size);
+               if (delta) {
+                       void *to_free = delta;
+                       orig_size = delta_size;
+                       delta = deflate_it(delta, delta_size, &delta_size);
+                       free(to_free);
+               }
+       }
+
+       if (delta && delta_size < deflate_size) {
+               printf("delta %lu\n", orig_size);
+               free(deflated);
+               data = delta;
+               data_size = delta_size;
+       }
+       else {
+               printf("literal %lu\n", two->size);
+               free(delta);
+               data = deflated;
+               data_size = deflate_size;
+       }
+
+       /* emit data encoded in base85 */
+       cp = data;
+       while (data_size) {
+               int bytes = (52 < data_size) ? 52 : data_size;
+               char line[70];
+               data_size -= bytes;
+               if (bytes <= 26)
+                       line[0] = bytes + 'A' - 1;
+               else
+                       line[0] = bytes - 26 + 'a' - 1;
+               encode_85(line + 1, cp, bytes);
+               cp += bytes;
+               puts(line);
+       }
+       printf("\n");
+       free(data);
+}
+
 #define FIRST_FEW_BYTES 8000
 static int mmfile_is_binary(mmfile_t *mf)
 {
@@ -407,6 +538,7 @@ static void builtin_diff(const char *name_a,
                         struct diff_filespec *one,
                         struct diff_filespec *two,
                         const char *xfrm_msg,
+                        struct diff_options *o,
                         int complete_rewrite)
 {
        mmfile_t mf1, mf2;
@@ -451,8 +583,17 @@ static void builtin_diff(const char *name_a,
        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
                die("unable to read files to diff");
 
-       if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
-               printf("Binary files %s and %s differ\n", lbl[0], lbl[1]);
+       if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
+               /* Quite common confusing case */
+               if (mf1.size == mf2.size &&
+                   !memcmp(mf1.ptr, mf2.ptr, mf1.size))
+                       goto free_ab_and_return;
+               if (o->binary)
+                       emit_binary_diff(&mf1, &mf2);
+               else
+                       printf("Binary files %s and %s differ\n",
+                              lbl[0], lbl[1]);
+       }
        else {
                /* Crazy xdl interfaces.. */
                const char *diffopts = getenv("GIT_DIFF_OPTS");
@@ -463,7 +604,7 @@ static void builtin_diff(const char *name_a,
 
                ecbdata.label_path = lbl;
                xpp.flags = XDF_NEED_MINIMAL;
-               xecfg.ctxlen = 3;
+               xecfg.ctxlen = o->context;
                xecfg.flags = XDL_EMIT_FUNCNAMES;
                if (!diffopts)
                        ;
@@ -485,7 +626,8 @@ static void builtin_diff(const char *name_a,
 static void builtin_diffstat(const char *name_a, const char *name_b,
                             struct diff_filespec *one,
                             struct diff_filespec *two,
-                            struct diffstat_t *diffstat)
+                            struct diffstat_t *diffstat,
+                            int complete_rewrite)
 {
        mmfile_t mf1, mf2;
        struct diffstat_file *data;
@@ -496,7 +638,13 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
                data->is_unmerged = 1;
                return;
        }
-
+       if (complete_rewrite) {
+               diff_populate_filespec(one, 0);
+               diff_populate_filespec(two, 0);
+               data->deleted = count_lines(one->data, one->size);
+               data->added = count_lines(two->data, two->size);
+               return;
+       }
        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
                die("unable to read files to diff");
 
@@ -517,6 +665,41 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
        }
 }
 
+static void builtin_checkdiff(const char *name_a, const char *name_b,
+                            struct diff_filespec *one,
+                            struct diff_filespec *two)
+{
+       mmfile_t mf1, mf2;
+       struct checkdiff_t data;
+
+       if (!two)
+               return;
+
+       memset(&data, 0, sizeof(data));
+       data.xm.consume = checkdiff_consume;
+       data.filename = name_b ? name_b : name_a;
+       data.lineno = 0;
+
+       if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
+               die("unable to read files to diff");
+
+       if (mmfile_is_binary(&mf2))
+               return;
+       else {
+               /* Crazy xdl interfaces.. */
+               xpparam_t xpp;
+               xdemitconf_t xecfg;
+               xdemitcb_t ecb;
+
+               xpp.flags = XDF_NEED_MINIMAL;
+               xecfg.ctxlen = 0;
+               xecfg.flags = 0;
+               ecb.outf = xdiff_outf;
+               ecb.priv = &data;
+               xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
+       }
+}
+
 struct diff_filespec *alloc_filespec(const char *path)
 {
        int namelen = strlen(path);
@@ -921,6 +1104,7 @@ static void run_diff_cmd(const char *pgm,
                         struct diff_filespec *one,
                         struct diff_filespec *two,
                         const char *xfrm_msg,
+                        struct diff_options *o,
                         int complete_rewrite)
 {
        if (pgm) {
@@ -930,7 +1114,7 @@ static void run_diff_cmd(const char *pgm,
        }
        if (one && two)
                builtin_diff(name, other ? other : name,
-                            one, two, xfrm_msg, complete_rewrite);
+                            one, two, xfrm_msg, o, complete_rewrite);
        else
                printf("* Unmerged path %s\n", name);
 }
@@ -964,7 +1148,7 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
 
        if (DIFF_PAIR_UNMERGED(p)) {
                /* unmerged */
-               run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, 0);
+               run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
                return;
        }
 
@@ -1011,14 +1195,12 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
        }
 
        if (memcmp(one->sha1, two->sha1, 20)) {
-               char one_sha1[41];
                int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
-               memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
 
                len += snprintf(msg + len, sizeof(msg) - len,
                                "index %.*s..%.*s",
-                               abbrev, one_sha1, abbrev,
-                               sha1_to_hex(two->sha1));
+                               abbrev, sha1_to_hex(one->sha1),
+                               abbrev, sha1_to_hex(two->sha1));
                if (one->mode == two->mode)
                        len += snprintf(msg + len, sizeof(msg) - len,
                                        " %06o", one->mode);
@@ -1036,14 +1218,14 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
                 * needs to be split into deletion and creation.
                 */
                struct diff_filespec *null = alloc_filespec(two->path);
-               run_diff_cmd(NULL, name, other, one, null, xfrm_msg, 0);
+               run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
                free(null);
                null = alloc_filespec(one->path);
-               run_diff_cmd(NULL, name, other, null, two, xfrm_msg, 0);
+               run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
                free(null);
        }
        else
-               run_diff_cmd(pgm, name, other, one, two, xfrm_msg,
+               run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
                             complete_rewrite);
 
        free(name_munged);
@@ -1055,10 +1237,11 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
 {
        const char *name;
        const char *other;
+       int complete_rewrite = 0;
 
        if (DIFF_PAIR_UNMERGED(p)) {
                /* unmerged */
-               builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat);
+               builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, 0);
                return;
        }
 
@@ -1068,7 +1251,28 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
        diff_fill_sha1_info(p->one);
        diff_fill_sha1_info(p->two);
 
-       builtin_diffstat(name, other, p->one, p->two, diffstat);
+       if (p->status == DIFF_STATUS_MODIFIED && p->score)
+               complete_rewrite = 1;
+       builtin_diffstat(name, other, p->one, p->two, diffstat, complete_rewrite);
+}
+
+static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
+{
+       const char *name;
+       const char *other;
+
+       if (DIFF_PAIR_UNMERGED(p)) {
+               /* unmerged */
+               return;
+       }
+
+       name = p->one->path;
+       other = (strcmp(name, p->two->path) ? p->two->path : NULL);
+
+       diff_fill_sha1_info(p->one);
+       diff_fill_sha1_info(p->two);
+
+       builtin_checkdiff(name, other, p->one, p->two);
 }
 
 void diff_setup(struct diff_options *options)
@@ -1078,6 +1282,7 @@ void diff_setup(struct diff_options *options)
        options->line_termination = '\n';
        options->break_opt = -1;
        options->rename_limit = -1;
+       options->context = 3;
 
        options->change = diff_change;
        options->add_remove = diff_addremove;
@@ -1095,9 +1300,18 @@ int diff_setup_done(struct diff_options *options)
         * recursive bits for other formats here.
         */
        if ((options->output_format == DIFF_FORMAT_PATCH) ||
-           (options->output_format == DIFF_FORMAT_DIFFSTAT))
+           (options->output_format == DIFF_FORMAT_DIFFSTAT) ||
+           (options->output_format == DIFF_FORMAT_CHECKDIFF))
                options->recursive = 1;
 
+       /*
+        * These combinations do not make sense.
+        */
+       if (options->output_format == DIFF_FORMAT_RAW)
+               options->with_raw = 0;
+       if (options->output_format == DIFF_FORMAT_DIFFSTAT)
+               options->with_stat  = 0;
+
        if (options->detect_rename && options->rename_limit < 0)
                options->rename_limit = diff_rename_limit_default;
        if (options->setup & DIFF_SETUP_USE_CACHE) {
@@ -1118,17 +1332,70 @@ int diff_setup_done(struct diff_options *options)
        return 0;
 }
 
+int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
+{
+       char c, *eq;
+       int len;
+
+       if (*arg != '-')
+               return 0;
+       c = *++arg;
+       if (!c)
+               return 0;
+       if (c == arg_short) {
+               c = *++arg;
+               if (!c)
+                       return 1;
+               if (val && isdigit(c)) {
+                       char *end;
+                       int n = strtoul(arg, &end, 10);
+                       if (*end)
+                               return 0;
+                       *val = n;
+                       return 1;
+               }
+               return 0;
+       }
+       if (c != '-')
+               return 0;
+       arg++;
+       eq = strchr(arg, '=');
+       if (eq)
+               len = eq - arg;
+       else
+               len = strlen(arg);
+       if (!len || strncmp(arg, arg_long, len))
+               return 0;
+       if (eq) {
+               int n;
+               char *end;
+               if (!isdigit(*++eq))
+                       return 0;
+               n = strtoul(eq, &end, 10);
+               if (*end)
+                       return 0;
+               *val = n;
+       }
+       return 1;
+}
+
 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 {
        const char *arg = av[0];
        if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
                options->output_format = DIFF_FORMAT_PATCH;
+       else if (opt_arg(arg, 'U', "unified", &options->context))
+               options->output_format = DIFF_FORMAT_PATCH;
        else if (!strcmp(arg, "--patch-with-raw")) {
                options->output_format = DIFF_FORMAT_PATCH;
                options->with_raw = 1;
        }
        else if (!strcmp(arg, "--stat"))
                options->output_format = DIFF_FORMAT_DIFFSTAT;
+       else if (!strcmp(arg, "--check"))
+               options->output_format = DIFF_FORMAT_CHECKDIFF;
+       else if (!strcmp(arg, "--summary"))
+               options->summary = 1;
        else if (!strcmp(arg, "--patch-with-stat")) {
                options->output_format = DIFF_FORMAT_PATCH;
                options->with_stat = 1;
@@ -1139,6 +1406,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
                options->rename_limit = strtoul(arg+2, NULL, 10);
        else if (!strcmp(arg, "--full-index"))
                options->full_index = 1;
+       else if (!strcmp(arg, "--binary")) {
+               options->output_format = DIFF_FORMAT_PATCH;
+               options->full_index = options->binary = 1;
+       }
        else if (!strcmp(arg, "--name-only"))
                options->output_format = DIFF_FORMAT_NAME;
        else if (!strcmp(arg, "--name-status"))
@@ -1445,6 +1716,19 @@ static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
        run_diffstat(p, o, diffstat);
 }
 
+static void diff_flush_checkdiff(struct diff_filepair *p,
+               struct diff_options *o)
+{
+       if (diff_unmodified_pair(p))
+               return;
+
+       if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
+           (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
+               return; /* no tree diffs in patch format */
+
+       run_checkdiff(p, o);
+}
+
 int diff_queue_is_empty(void)
 {
        struct diff_queue_struct *q = &diff_queued_diff;
@@ -1575,6 +1859,9 @@ static void flush_one_pair(struct diff_filepair *p,
                case DIFF_FORMAT_DIFFSTAT:
                        diff_flush_stat(p, options, diffstat);
                        break;
+               case DIFF_FORMAT_CHECKDIFF:
+                       diff_flush_checkdiff(p, options);
+                       break;
                case DIFF_FORMAT_PATCH:
                        diff_flush_patch(p, options);
                        break;
@@ -1595,6 +1882,85 @@ static void flush_one_pair(struct diff_filepair *p,
        }
 }
 
+static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
+{
+       if (fs->mode)
+               printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
+       else
+               printf(" %s %s\n", newdelete, fs->path);
+}
+
+
+static void show_mode_change(struct diff_filepair *p, int show_name)
+{
+       if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
+               if (show_name)
+                       printf(" mode change %06o => %06o %s\n",
+                              p->one->mode, p->two->mode, p->two->path);
+               else
+                       printf(" mode change %06o => %06o\n",
+                              p->one->mode, p->two->mode);
+       }
+}
+
+static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
+{
+       const char *old, *new;
+
+       /* Find common prefix */
+       old = p->one->path;
+       new = p->two->path;
+       while (1) {
+               const char *slash_old, *slash_new;
+               slash_old = strchr(old, '/');
+               slash_new = strchr(new, '/');
+               if (!slash_old ||
+                   !slash_new ||
+                   slash_old - old != slash_new - new ||
+                   memcmp(old, new, slash_new - new))
+                       break;
+               old = slash_old + 1;
+               new = slash_new + 1;
+       }
+       /* p->one->path thru old is the common prefix, and old and new
+        * through the end of names are renames
+        */
+       if (old != p->one->path)
+               printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
+                      (int)(old - p->one->path), p->one->path,
+                      old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
+       else
+               printf(" %s %s => %s (%d%%)\n", renamecopy,
+                      p->one->path, p->two->path,
+                      (int)(0.5 + p->score * 100.0/MAX_SCORE));
+       show_mode_change(p, 0);
+}
+
+static void diff_summary(struct diff_filepair *p)
+{
+       switch(p->status) {
+       case DIFF_STATUS_DELETED:
+               show_file_mode_name("delete", p->one);
+               break;
+       case DIFF_STATUS_ADDED:
+               show_file_mode_name("create", p->two);
+               break;
+       case DIFF_STATUS_COPIED:
+               show_rename_copy("copy", p);
+               break;
+       case DIFF_STATUS_RENAMED:
+               show_rename_copy("rename", p);
+               break;
+       default:
+               if (p->score) {
+                       printf(" rewrite %s (%d%%)\n", p->two->path,
+                               (int)(0.5 + p->score * 100.0/MAX_SCORE));
+                       show_mode_change(p, 0);
+               } else  show_mode_change(p, 1);
+               break;
+       }
+}
+
 void diff_flush(struct diff_options *options)
 {
        struct diff_queue_struct *q = &diff_queued_diff;
@@ -1623,12 +1989,17 @@ void diff_flush(struct diff_options *options)
                show_stats(diffstat);
                free(diffstat);
                diffstat = NULL;
-               putchar(options->line_termination);
+               if (options->summary)
+                       for (i = 0; i < q->nr; i++)
+                               diff_summary(q->queue[i]);
+               if (options->stat_sep)
+                       fputs(options->stat_sep, stdout);
+               else
+                       putchar(options->line_termination);
        }
        for (i = 0; i < q->nr; i++) {
                struct diff_filepair *p = q->queue[i];
                flush_one_pair(p, diff_output_format, options, diffstat);
-               diff_free_filepair(p);
        }
 
        if (diffstat) {
@@ -1636,6 +2007,12 @@ void diff_flush(struct diff_options *options)
                free(diffstat);
        }
 
+       for (i = 0; i < q->nr; i++) {
+               if (diffstat && options->summary)
+                       diff_summary(q->queue[i]);
+               diff_free_filepair(q->queue[i]);
+       }
+
        free(q->queue);
        q->queue = NULL;
        q->nr = q->alloc = 0;
diff --git a/diff.h b/diff.h
index 7150b90..4fc597c 100644 (file)
--- a/diff.h
+++ b/diff.h
@@ -28,9 +28,12 @@ struct diff_options {
                 with_raw:1,
                 with_stat:1,
                 tree_in_recursive:1,
+                binary:1,
                 full_index:1,
                 silent_on_remove:1,
-                find_copies_harder:1;
+                find_copies_harder:1,
+                summary:1;
+       int context;
        int break_opt;
        int detect_rename;
        int line_termination;
@@ -41,6 +44,7 @@ struct diff_options {
        int rename_limit;
        int setup;
        int abbrev;
+       const char *stat_sep;
 
        int nr_paths;
        const char **paths;
@@ -49,6 +53,8 @@ struct diff_options {
        add_remove_fn_t add_remove;
 };
 
+extern const char mime_boundary_leader[];
+
 extern void diff_tree_setup_paths(const char **paths, struct diff_options *);
 extern void diff_tree_release_paths(struct diff_options *);
 extern int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
@@ -75,6 +81,8 @@ struct combine_diff_path {
 extern void show_combined_diff(struct combine_diff_path *elem, int num_parent,
                              int dense, struct rev_info *);
 
+extern void diff_tree_combined(const unsigned char *sha1, const unsigned char parent[][20], int num_parent, int dense, struct rev_info *rev);
+
 extern void diff_tree_combined_merge(const unsigned char *sha1, int, struct rev_info *);
 
 extern void diff_addremove(struct diff_options *,
@@ -148,6 +156,7 @@ extern int diff_queue_is_empty(void);
 #define DIFF_FORMAT_NAME       4
 #define DIFF_FORMAT_NAME_STATUS        5
 #define DIFF_FORMAT_DIFFSTAT   6
+#define DIFF_FORMAT_CHECKDIFF  7
 
 extern void diff_flush(struct diff_options*);
 
diff --git a/dir.c b/dir.c
new file mode 100644 (file)
index 0000000..d778ecd
--- /dev/null
+++ b/dir.c
@@ -0,0 +1,401 @@
+/*
+ * This handles recursive filename detection with exclude
+ * files, index knowledge etc..
+ *
+ * Copyright (C) Linus Torvalds, 2005-2006
+ *              Junio Hamano, 2005-2006
+ */
+#include <dirent.h>
+#include <fnmatch.h>
+
+#include "cache.h"
+#include "dir.h"
+
+int common_prefix(const char **pathspec)
+{
+       const char *path, *slash, *next;
+       int prefix;
+
+       if (!pathspec)
+               return 0;
+
+       path = *pathspec;
+       slash = strrchr(path, '/');
+       if (!slash)
+               return 0;
+
+       prefix = slash - path + 1;
+       while ((next = *++pathspec) != NULL) {
+               int len = strlen(next);
+               if (len >= prefix && !memcmp(path, next, len))
+                       continue;
+               for (;;) {
+                       if (!len)
+                               return 0;
+                       if (next[--len] != '/')
+                               continue;
+                       if (memcmp(path, next, len+1))
+                               continue;
+                       prefix = len + 1;
+                       break;
+               }
+       }
+       return prefix;
+}
+
+static int match_one(const char *match, const char *name, int namelen)
+{
+       int matchlen;
+
+       /* If the match was just the prefix, we matched */
+       matchlen = strlen(match);
+       if (!matchlen)
+               return 1;
+
+       /*
+        * If we don't match the matchstring exactly,
+        * we need to match by fnmatch
+        */
+       if (strncmp(match, name, matchlen))
+               return !fnmatch(match, name, 0);
+
+       /*
+        * If we did match the string exactly, we still
+        * need to make sure that it happened on a path
+        * component boundary (ie either the last character
+        * of the match was '/', or the next character of
+        * the name was '/' or the terminating NUL.
+        */
+       return  match[matchlen-1] == '/' ||
+               name[matchlen] == '/' ||
+               !name[matchlen];
+}
+
+int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
+{
+       int retval;
+       const char *match;
+
+       name += prefix;
+       namelen -= prefix;
+
+       for (retval = 0; (match = *pathspec++) != NULL; seen++) {
+               if (retval & *seen)
+                       continue;
+               match += prefix;
+               if (match_one(match, name, namelen)) {
+                       retval = 1;
+                       *seen = 1;
+               }
+       }
+       return retval;
+}
+
+void add_exclude(const char *string, const char *base,
+                int baselen, struct exclude_list *which)
+{
+       struct exclude *x = xmalloc(sizeof (*x));
+
+       x->pattern = string;
+       x->base = base;
+       x->baselen = baselen;
+       if (which->nr == which->alloc) {
+               which->alloc = alloc_nr(which->alloc);
+               which->excludes = realloc(which->excludes,
+                                         which->alloc * sizeof(x));
+       }
+       which->excludes[which->nr++] = x;
+}
+
+static int add_excludes_from_file_1(const char *fname,
+                                   const char *base,
+                                   int baselen,
+                                   struct exclude_list *which)
+{
+       int fd, i;
+       long size;
+       char *buf, *entry;
+
+       fd = open(fname, O_RDONLY);
+       if (fd < 0)
+               goto err;
+       size = lseek(fd, 0, SEEK_END);
+       if (size < 0)
+               goto err;
+       lseek(fd, 0, SEEK_SET);
+       if (size == 0) {
+               close(fd);
+               return 0;
+       }
+       buf = xmalloc(size+1);
+       if (read(fd, buf, size) != size)
+               goto err;
+       close(fd);
+
+       buf[size++] = '\n';
+       entry = buf;
+       for (i = 0; i < size; i++) {
+               if (buf[i] == '\n') {
+                       if (entry != buf + i && entry[0] != '#') {
+                               buf[i - (i && buf[i-1] == '\r')] = 0;
+                               add_exclude(entry, base, baselen, which);
+                       }
+                       entry = buf + i + 1;
+               }
+       }
+       return 0;
+
+ err:
+       if (0 <= fd)
+               close(fd);
+       return -1;
+}
+
+void add_excludes_from_file(struct dir_struct *dir, const char *fname)
+{
+       if (add_excludes_from_file_1(fname, "", 0,
+                                    &dir->exclude_list[EXC_FILE]) < 0)
+               die("cannot use %s as an exclude file", fname);
+}
+
+static int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
+{
+       char exclude_file[PATH_MAX];
+       struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
+       int current_nr = el->nr;
+
+       if (dir->exclude_per_dir) {
+               memcpy(exclude_file, base, baselen);
+               strcpy(exclude_file + baselen, dir->exclude_per_dir);
+               add_excludes_from_file_1(exclude_file, base, baselen, el);
+       }
+       return current_nr;
+}
+
+static void pop_exclude_per_directory(struct dir_struct *dir, int stk)
+{
+       struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
+
+       while (stk < el->nr)
+               free(el->excludes[--el->nr]);
+}
+
+/* Scan the list and let the last match determines the fate.
+ * Return 1 for exclude, 0 for include and -1 for undecided.
+ */
+static int excluded_1(const char *pathname,
+                     int pathlen,
+                     struct exclude_list *el)
+{
+       int i;
+
+       if (el->nr) {
+               for (i = el->nr - 1; 0 <= i; i--) {
+                       struct exclude *x = el->excludes[i];
+                       const char *exclude = x->pattern;
+                       int to_exclude = 1;
+
+                       if (*exclude == '!') {
+                               to_exclude = 0;
+                               exclude++;
+                       }
+
+                       if (!strchr(exclude, '/')) {
+                               /* match basename */
+                               const char *basename = strrchr(pathname, '/');
+                               basename = (basename) ? basename+1 : pathname;
+                               if (fnmatch(exclude, basename, 0) == 0)
+                                       return to_exclude;
+                       }
+                       else {
+                               /* match with FNM_PATHNAME:
+                                * exclude has base (baselen long) implicitly
+                                * in front of it.
+                                */
+                               int baselen = x->baselen;
+                               if (*exclude == '/')
+                                       exclude++;
+
+                               if (pathlen < baselen ||
+                                   (baselen && pathname[baselen-1] != '/') ||
+                                   strncmp(pathname, x->base, baselen))
+                                   continue;
+
+                               if (fnmatch(exclude, pathname+baselen,
+                                           FNM_PATHNAME) == 0)
+                                       return to_exclude;
+                       }
+               }
+       }
+       return -1; /* undecided */
+}
+
+int excluded(struct dir_struct *dir, const char *pathname)
+{
+       int pathlen = strlen(pathname);
+       int st;
+
+       for (st = EXC_CMDL; st <= EXC_FILE; st++) {
+               switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
+               case 0:
+                       return 0;
+               case 1:
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+static void add_name(struct dir_struct *dir, const char *pathname, int len)
+{
+       struct dir_entry *ent;
+
+       if (cache_name_pos(pathname, len) >= 0)
+               return;
+
+       if (dir->nr == dir->alloc) {
+               int alloc = alloc_nr(dir->alloc);
+               dir->alloc = alloc;
+               dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
+       }
+       ent = xmalloc(sizeof(*ent) + len + 1);
+       ent->len = len;
+       memcpy(ent->name, pathname, len);
+       ent->name[len] = 0;
+       dir->entries[dir->nr++] = ent;
+}
+
+static int dir_exists(const char *dirname, int len)
+{
+       int pos = cache_name_pos(dirname, len);
+       if (pos >= 0)
+               return 1;
+       pos = -pos-1;
+       if (pos >= active_nr) /* can't */
+               return 0;
+       return !strncmp(active_cache[pos]->name, dirname, len);
+}
+
+/*
+ * Read a directory tree. We currently ignore anything but
+ * directories, regular files and symlinks. That's because git
+ * doesn't handle them at all yet. Maybe that will change some
+ * day.
+ *
+ * Also, we ignore the name ".git" (even if it is not a directory).
+ * That likely will not change.
+ */
+static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen)
+{
+       DIR *fdir = opendir(path);
+       int contents = 0;
+
+       if (fdir) {
+               int exclude_stk;
+               struct dirent *de;
+               char fullname[MAXPATHLEN + 1];
+               memcpy(fullname, base, baselen);
+
+               exclude_stk = push_exclude_per_directory(dir, base, baselen);
+
+               while ((de = readdir(fdir)) != NULL) {
+                       int len;
+
+                       if ((de->d_name[0] == '.') &&
+                           (de->d_name[1] == 0 ||
+                            !strcmp(de->d_name + 1, ".") ||
+                            !strcmp(de->d_name + 1, "git")))
+                               continue;
+                       len = strlen(de->d_name);
+                       memcpy(fullname + baselen, de->d_name, len+1);
+                       if (excluded(dir, fullname) != dir->show_ignored) {
+                               if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
+                                       continue;
+                               }
+                       }
+
+                       switch (DTYPE(de)) {
+                       struct stat st;
+                       int subdir, rewind_base;
+                       default:
+                               continue;
+                       case DT_UNKNOWN:
+                               if (lstat(fullname, &st))
+                                       continue;
+                               if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
+                                       break;
+                               if (!S_ISDIR(st.st_mode))
+                                       continue;
+                               /* fallthrough */
+                       case DT_DIR:
+                               memcpy(fullname + baselen + len, "/", 2);
+                               len++;
+                               rewind_base = dir->nr;
+                               subdir = read_directory_recursive(dir, fullname, fullname,
+                                                       baselen + len);
+                               if (dir->show_other_directories &&
+                                   (subdir || !dir->hide_empty_directories) &&
+                                   !dir_exists(fullname, baselen + len)) {
+                                       // Rewind the read subdirectory
+                                       while (dir->nr > rewind_base)
+                                               free(dir->entries[--dir->nr]);
+                                       break;
+                               }
+                               contents += subdir;
+                               continue;
+                       case DT_REG:
+                       case DT_LNK:
+                               break;
+                       }
+                       add_name(dir, fullname, baselen + len);
+                       contents++;
+               }
+               closedir(fdir);
+
+               pop_exclude_per_directory(dir, exclude_stk);
+       }
+
+       return contents;
+}
+
+static int cmp_name(const void *p1, const void *p2)
+{
+       const struct dir_entry *e1 = *(const struct dir_entry **)p1;
+       const struct dir_entry *e2 = *(const struct dir_entry **)p2;
+
+       return cache_name_compare(e1->name, e1->len,
+                                 e2->name, e2->len);
+}
+
+int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
+{
+       /*
+        * Make sure to do the per-directory exclude for all the
+        * directories leading up to our base.
+        */
+       if (baselen) {
+               if (dir->exclude_per_dir) {
+                       char *p, *pp = xmalloc(baselen+1);
+                       memcpy(pp, base, baselen+1);
+                       p = pp;
+                       while (1) {
+                               char save = *p;
+                               *p = 0;
+                               push_exclude_per_directory(dir, pp, p-pp);
+                               *p++ = save;
+                               if (!save)
+                                       break;
+                               p = strchr(p, '/');
+                               if (p)
+                                       p++;
+                               else
+                                       p = pp + baselen;
+                       }
+                       free(pp);
+               }
+       }
+
+       read_directory_recursive(dir, path, base, baselen);
+       qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
+       return dir->nr;
+}
diff --git a/dir.h b/dir.h
new file mode 100644 (file)
index 0000000..56a1b7f
--- /dev/null
+++ b/dir.h
@@ -0,0 +1,51 @@
+#ifndef DIR_H
+#define DIR_H
+
+/*
+ * We maintain three exclude pattern lists:
+ * EXC_CMDL lists patterns explicitly given on the command line.
+ * EXC_DIRS lists patterns obtained from per-directory ignore files.
+ * EXC_FILE lists patterns from fallback ignore files.
+ */
+#define EXC_CMDL 0
+#define EXC_DIRS 1
+#define EXC_FILE 2
+
+
+struct dir_entry {
+       int len;
+       char name[FLEX_ARRAY]; /* more */
+};
+
+struct exclude_list {
+       int nr;
+       int alloc;
+       struct exclude {
+               const char *pattern;
+               const char *base;
+               int baselen;
+       } **excludes;
+};
+
+struct dir_struct {
+       int nr, alloc;
+       unsigned int show_ignored:1,
+                    show_other_directories:1,
+                    hide_empty_directories:1;
+       struct dir_entry **entries;
+
+       /* Exclude info */
+       const char *exclude_per_dir;
+       struct exclude_list exclude_list[3];
+};
+
+extern int common_prefix(const char **pathspec);
+extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
+
+extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen);
+extern int excluded(struct dir_struct *, const char *);
+extern void add_excludes_from_file(struct dir_struct *, const char *fname);
+extern void add_exclude(const char *string, const char *base,
+                       int baselen, struct exclude_list *which);
+
+#endif
index fbea263..1ccaf51 100644 (file)
@@ -21,10 +21,9 @@ static int dump_cache_tree(struct cache_tree *it,
        int i;
        int errs = 0;
 
-       if (!it)
-               return;
-       if (!ref)
-               die("internal error");
+       if (!it || !ref)
+               /* missing in either */
+               return 0;
 
        if (it->entry_count < 0) {
                dump_one(it, pfx, "");
index 6df6478..2e79eab 100644 (file)
@@ -13,7 +13,8 @@ char git_default_email[MAX_GITNAME];
 char git_default_name[MAX_GITNAME];
 int trust_executable_bit = 1;
 int assume_unchanged = 0;
-int only_use_symrefs = 0;
+int prefer_symlink_refs = 0;
+int log_all_ref_updates = 0;
 int warn_ambiguous_refs = 1;
 int repository_format_version = 0;
 char git_commit_encoding[MAX_ENCODING_LENGTH] = "utf-8";
index 44bb2f2..c1539d1 100644 (file)
@@ -21,7 +21,7 @@ const char *git_exec_path(void)
                return current_exec_path;
 
        env = getenv("GIT_EXEC_PATH");
-       if (env) {
+       if (env && *env) {
                return env;
        }
 
@@ -32,22 +32,25 @@ const char *git_exec_path(void)
 int execv_git_cmd(const char **argv)
 {
        char git_command[PATH_MAX + 1];
-       int len,  i;
+       int i;
        const char *paths[] = { current_exec_path,
                                getenv("GIT_EXEC_PATH"),
                                builtin_exec_path };
 
        for (i = 0; i < ARRAY_SIZE(paths); ++i) {
+               size_t len;
+               int rc;
                const char *exec_dir = paths[i];
                const char *tmp;
 
-               if (!exec_dir) continue;
+               if (!exec_dir || !*exec_dir) continue;
 
                if (*exec_dir != '/') {
                        if (!getcwd(git_command, sizeof(git_command))) {
                                fprintf(stderr, "git: cannot determine "
-                                       "current directory\n");
-                               exit(1);
+                                       "current directory: %s\n",
+                                       strerror(errno));
+                               break;
                        }
                        len = strlen(git_command);
 
@@ -57,17 +60,28 @@ int execv_git_cmd(const char **argv)
                                while (*exec_dir == '/')
                                        exec_dir++;
                        }
-                       snprintf(git_command + len, sizeof(git_command) - len,
-                                "/%s", exec_dir);
+
+                       rc = snprintf(git_command + len,
+                                     sizeof(git_command) - len, "/%s",
+                                     exec_dir);
+                       if (rc < 0 || rc >= sizeof(git_command) - len) {
+                               fprintf(stderr, "git: command name given "
+                                       "is too long.\n");
+                               break;
+                       }
                } else {
+                       if (strlen(exec_dir) + 1 > sizeof(git_command)) {
+                               fprintf(stderr, "git: command name given "
+                                       "is too long.\n");
+                               break;
+                       }
                        strcpy(git_command, exec_dir);
                }
 
                len = strlen(git_command);
-               len += snprintf(git_command + len, sizeof(git_command) - len,
-                               "/git-%s", argv[0]);
-
-               if (sizeof(git_command) <= len) {
+               rc = snprintf(git_command + len, sizeof(git_command) - len,
+                             "/git-%s", argv[0]);
+               if (rc < 0 || rc >= sizeof(git_command) - len) {
                        fprintf(stderr,
                                "git: command name given is too long.\n");
                        break;
index a3bcad0..8daa93d 100644 (file)
@@ -262,22 +262,58 @@ static void mark_recent_complete_commits(unsigned long cutoff)
 
 static void filter_refs(struct ref **refs, int nr_match, char **match)
 {
-       struct ref *prev, *current, *next;
-
-       for (prev = NULL, current = *refs; current; current = next) {
-               next = current->next;
-               if ((!memcmp(current->name, "refs/", 5) &&
-                    check_ref_format(current->name + 5)) ||
-                   (!fetch_all &&
-                    !path_match(current->name, nr_match, match))) {
-                       if (prev == NULL)
-                               *refs = next;
-                       else
-                               prev->next = next;
-                       free(current);
-               } else
-                       prev = current;
+       struct ref **return_refs;
+       struct ref *newlist = NULL;
+       struct ref **newtail = &newlist;
+       struct ref *ref, *next;
+       struct ref *fastarray[32];
+
+       if (nr_match && !fetch_all) {
+               if (ARRAY_SIZE(fastarray) < nr_match)
+                       return_refs = xcalloc(nr_match, sizeof(struct ref *));
+               else {
+                       return_refs = fastarray;
+                       memset(return_refs, 0, sizeof(struct ref *) * nr_match);
+               }
+       }
+       else
+               return_refs = NULL;
+
+       for (ref = *refs; ref; ref = next) {
+               next = ref->next;
+               if (!memcmp(ref->name, "refs/", 5) &&
+                   check_ref_format(ref->name + 5))
+                       ; /* trash */
+               else if (fetch_all) {
+                       *newtail = ref;
+                       ref->next = NULL;
+                       newtail = &ref->next;
+                       continue;
+               }
+               else {
+                       int order = path_match(ref->name, nr_match, match);
+                       if (order) {
+                               return_refs[order-1] = ref;
+                               continue; /* we will link it later */
+                       }
+               }
+               free(ref);
+       }
+
+       if (!fetch_all) {
+               int i;
+               for (i = 0; i < nr_match; i++) {
+                       ref = return_refs[i];
+                       if (ref) {
+                               *newtail = ref;
+                               ref->next = NULL;
+                               newtail = &ref->next;
+                       }
+               }
+               if (return_refs != fastarray)
+                       free(return_refs);
        }
+       *refs = newlist;
 }
 
 static int everything_local(struct ref **refs, int nr_match, char **match)
diff --git a/fetch.c b/fetch.c
index 73bde07..cf6c994 100644 (file)
--- a/fetch.c
+++ b/fetch.c
@@ -3,13 +3,13 @@
 #include "cache.h"
 #include "commit.h"
 #include "tree.h"
+#include "tree-walk.h"
 #include "tag.h"
 #include "blob.h"
 #include "refs.h"
 
 const char *write_ref = NULL;
-
-const unsigned char *current_ref = NULL;
+const char *write_ref_log_details = NULL;
 
 int get_tree = 0;
 int get_history = 0;
@@ -38,21 +38,33 @@ static int process(struct object *obj);
 
 static int process_tree(struct tree *tree)
 {
-       struct tree_entry_list *entry;
+       struct tree_desc desc;
+       struct name_entry entry;
 
        if (parse_tree(tree))
                return -1;
 
-       entry = tree->entries;
-       tree->entries = NULL;
-       while (entry) {
-               struct tree_entry_list *next = entry->next;
-               if (process(entry->item.any))
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+       while (tree_entry(&desc, &entry)) {
+               struct object *obj = NULL;
+
+               if (S_ISDIR(entry.mode)) {
+                       struct tree *tree = lookup_tree(entry.sha1);
+                       if (tree)
+                               obj = &tree->object;
+               }
+               else {
+                       struct blob *blob = lookup_blob(entry.sha1);
+                       if (blob)
+                               obj = &blob->object;
+               }
+               if (!obj || process(obj))
                        return -1;
-               free(entry->name);
-               free(entry);
-               entry = next;
        }
+       free(tree->buffer);
+       tree->buffer = NULL;
+       tree->size = 0;
        return 0;
 }
 
@@ -138,7 +150,8 @@ static int process(struct object *obj)
        if (has_sha1_file(obj->sha1)) {
                /* We already have it, so we should scan it now. */
                obj->flags |= TO_SCAN;
-       } else {
+       }
+       else {
                if (obj->flags & COMPLETE)
                        return 0;
                prefetch(obj->sha1);
@@ -204,35 +217,52 @@ static int mark_complete(const char *path, const unsigned char *sha1)
 
 int pull(char *target)
 {
+       struct ref_lock *lock = NULL;
        unsigned char sha1[20];
-       int fd = -1;
+       char *msg;
+       int ret;
 
        save_commit_buffer = 0;
        track_object_refs = 0;
-       if (write_ref && current_ref) {
-               fd = lock_ref_sha1(write_ref, current_ref);
-               if (fd < 0)
+       if (write_ref) {
+               lock = lock_ref_sha1(write_ref, NULL, 0);
+               if (!lock) {
+                       error("Can't lock ref %s", write_ref);
                        return -1;
+               }
        }
 
-       if (!get_recover) {
+       if (!get_recover)
                for_each_ref(mark_complete);
-       }
 
-       if (interpret_target(target, sha1))
-               return error("Could not interpret %s as something to pull",
-                            target);
-       if (process(lookup_unknown_object(sha1)))
+       if (interpret_target(target, sha1)) {
+               error("Could not interpret %s as something to pull", target);
+               if (lock)
+                       unlock_ref(lock);
+               return -1;
+       }
+       if (process(lookup_unknown_object(sha1))) {
+               if (lock)
+                       unlock_ref(lock);
                return -1;
-       if (loop())
+       }
+       if (loop()) {
+               if (lock)
+                       unlock_ref(lock);
                return -1;
-       
+       }
+
        if (write_ref) {
-               if (current_ref) {
-                       write_ref_sha1(write_ref, fd, sha1);
-               } else {
-                       write_ref_sha1_unlocked(write_ref, sha1);
+               if (write_ref_log_details) {
+                       msg = xmalloc(strlen(write_ref_log_details) + 12);
+                       sprintf(msg, "fetch from %s", write_ref_log_details);
                }
+               else
+                       msg = NULL;
+               ret = write_ref_sha1(lock, sha1, msg ? msg : "fetch (unknown)");
+               if (msg)
+                       free(msg);
+               return ret;
        }
        return 0;
 }
diff --git a/fetch.h b/fetch.h
index 9837a3d..841bb1a 100644 (file)
--- a/fetch.h
+++ b/fetch.h
@@ -25,8 +25,8 @@ extern int fetch_ref(char *ref, unsigned char *sha1);
 /* If set, the ref filename to write the target value to. */
 extern const char *write_ref;
 
-/* If set, the hash that the current value of write_ref must be. */
-extern const unsigned char *current_ref;
+/* If set additional text will appear in the ref log. */
+extern const char *write_ref_log_details;
 
 /* Set to fetch the target tree. */
 extern int get_tree;
index 98421aa..33ce366 100644 (file)
@@ -9,8 +9,10 @@
 #include "refs.h"
 #include "pack.h"
 #include "cache-tree.h"
+#include "tree-walk.h"
 
 #define REACHABLE 0x0001
+#define SEEN      0x0002
 
 static int show_root = 0;
 static int show_tags = 0;
@@ -115,15 +117,15 @@ static void check_connectivity(void)
 #define TREE_UNORDERED (-1)
 #define TREE_HAS_DUPS  (-2)
 
-static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
+static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
 {
-       int len1 = strlen(a->name);
-       int len2 = strlen(b->name);
+       int len1 = strlen(name1);
+       int len2 = strlen(name2);
        int len = len1 < len2 ? len1 : len2;
        unsigned char c1, c2;
        int cmp;
 
-       cmp = memcmp(a->name, b->name, len);
+       cmp = memcmp(name1, name2, len);
        if (cmp < 0)
                return 0;
        if (cmp > 0)
@@ -134,8 +136,8 @@ static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
         * Now we need to order the next one, but turn
         * a '\0' into a '/' for a directory entry.
         */
-       c1 = a->name[len];
-       c2 = b->name[len];
+       c1 = name1[len];
+       c2 = name2[len];
        if (!c1 && !c2)
                /*
                 * git-write-tree used to write out a nonsense tree that has
@@ -143,9 +145,9 @@ static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
                 * sure we do not have duplicate entries.
                 */
                return TREE_HAS_DUPS;
-       if (!c1 && a->directory)
+       if (!c1 && S_ISDIR(mode1))
                c1 = '/';
-       if (!c2 && b->directory)
+       if (!c2 && S_ISDIR(mode2))
                c2 = '/';
        return c1 < c2 ? 0 : TREE_UNORDERED;
 }
@@ -158,17 +160,32 @@ static int fsck_tree(struct tree *item)
        int has_bad_modes = 0;
        int has_dup_entries = 0;
        int not_properly_sorted = 0;
-       struct tree_entry_list *entry, *last;
+       struct tree_desc desc;
+       unsigned o_mode;
+       const char *o_name;
+       const unsigned char *o_sha1;
 
-       last = NULL;
-       for (entry = item->entries; entry; entry = entry->next) {
-               if (strchr(entry->name, '/'))
+       desc.buf = item->buffer;
+       desc.size = item->size;
+
+       o_mode = 0;
+       o_name = NULL;
+       o_sha1 = NULL;
+       while (desc.size) {
+               unsigned mode;
+               const char *name;
+               const unsigned char *sha1;
+
+               sha1 = tree_entry_extract(&desc, &name, &mode);
+
+               if (strchr(name, '/'))
                        has_full_path = 1;
-               has_zero_pad |= entry->zeropad;
+               has_zero_pad |= *(char *)desc.buf == '0';
+               update_tree_entry(&desc);
 
-               switch (entry->mode) {
+               switch (mode) {
                /*
-                * Standard modes.. 
+                * Standard modes..
                 */
                case S_IFREG | 0755:
                case S_IFREG | 0644:
@@ -187,8 +204,8 @@ static int fsck_tree(struct tree *item)
                        has_bad_modes = 1;
                }
 
-               if (last) {
-                       switch (verify_ordered(last, entry)) {
+               if (o_name) {
+                       switch (verify_ordered(o_mode, o_name, mode, name)) {
                        case TREE_UNORDERED:
                                not_properly_sorted = 1;
                                break;
@@ -198,17 +215,14 @@ static int fsck_tree(struct tree *item)
                        default:
                                break;
                        }
-                       free(last->name);
-                       free(last);
                }
 
-               last = entry;
+               o_mode = mode;
+               o_name = name;
+               o_sha1 = sha1;
        }
-       if (last) {
-               free(last->name);
-               free(last);
-       }
-       item->entries = NULL;
+       free(item->buffer);
+       item->buffer = NULL;
 
        retval = 0;
        if (has_full_path) {
@@ -278,6 +292,9 @@ static int fsck_sha1(unsigned char *sha1)
        struct object *obj = parse_object(sha1);
        if (!obj)
                return error("%s: object not found", sha1_to_hex(sha1));
+       if (obj->flags & SEEN)
+               return 0;
+       obj->flags |= SEEN;
        if (obj->type == blob_type)
                return 0;
        if (obj->type == tree_type)
@@ -446,6 +463,11 @@ static int fsck_cache_tree(struct cache_tree *it)
 
        if (0 <= it->entry_count) {
                struct object *obj = parse_object(it->sha1);
+               if (!obj) {
+                       error("%s: invalid sha1 pointer in cache-tree",
+                             sha1_to_hex(it->sha1));
+                       return 1;
+               }
                mark_reachable(obj, REACHABLE);
                obj->used = 1;
                if (obj->type != tree_type)
@@ -460,6 +482,7 @@ int main(int argc, char **argv)
 {
        int i, heads;
 
+       track_object_refs = 1;
        setup_git_directory();
 
        for (i = 1; i < argc; i++) {
index 6c59dbd..ec1eda2 100755 (executable)
@@ -37,7 +37,6 @@ show-branch
 status
 tag
 verify-tag
-whatchanged
 EOF
 while read cmd
 do
diff --git a/get-tar-commit-id.c b/get-tar-commit-id.c
deleted file mode 100644 (file)
index 4166290..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (C) 2005 Rene Scharfe
- */
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#define HEADERSIZE     1024
-
-int main(int argc, char **argv)
-{
-       char buffer[HEADERSIZE];
-       ssize_t n;
-
-       n = read(0, buffer, HEADERSIZE);
-       if (n < HEADERSIZE) {
-               fprintf(stderr, "read error\n");
-               return 3;
-       }
-       if (buffer[156] != 'g')
-               return 1;
-       if (memcmp(&buffer[512], "52 comment=", 11))
-               return 1;
-       n = write(1, &buffer[523], 41);
-       if (n < 41) {
-               fprintf(stderr, "write error\n");
-               return 2;
-       }
-       return 0;
-}
diff --git a/git-add.sh b/git-add.sh
deleted file mode 100755 (executable)
index d6a4bc7..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-#!/bin/sh
-
-USAGE='[-n] [-v] <file>...'
-SUBDIRECTORY_OK='Yes'
-. git-sh-setup
-
-show_only=
-verbose=
-while : ; do
-  case "$1" in
-    -n)
-       show_only=true
-       ;;
-    -v)
-       verbose=--verbose
-       ;;
-    --)
-       shift
-       break
-       ;;
-    -*)
-       usage
-       ;;
-    *)
-       break
-       ;;
-  esac
-  shift
-done
-
-# Check misspelled pathspec
-case "$#" in
-0)     ;;
-*)
-       git-ls-files --error-unmatch --others --cached -- "$@" >/dev/null || {
-               echo >&2 "Maybe you misspelled it?"
-               exit 1
-       }
-       ;;
-esac
-
-if test -f "$GIT_DIR/info/exclude"
-then
-       git-ls-files -z \
-       --exclude-from="$GIT_DIR/info/exclude" \
-       --others --exclude-per-directory=.gitignore -- "$@"
-else
-       git-ls-files -z \
-       --others --exclude-per-directory=.gitignore -- "$@"
-fi |
-case "$show_only" in
-true)
-       xargs -0 echo ;;
-*)
-       git-update-index --add $verbose -z --stdin ;;
-esac
index eab4aa8..4232e27 100755 (executable)
--- a/git-am.sh
+++ b/git-am.sh
@@ -14,6 +14,30 @@ stop_here () {
     exit 1
 }
 
+stop_here_user_resolve () {
+    if [ -n "$resolvemsg" ]; then
+           echo "$resolvemsg"
+           stop_here $1
+    fi
+    cmdline=$(basename $0)
+    if test '' != "$interactive"
+    then
+        cmdline="$cmdline -i"
+    fi
+    if test '' != "$threeway"
+    then
+        cmdline="$cmdline -3"
+    fi
+    if test '.dotest' != "$dotest"
+    then
+        cmdline="$cmdline -d=$dotest"
+    fi
+    echo "When you have resolved this problem run \"$cmdline --resolved\"."
+    echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
+
+    stop_here $1
+}
+
 go_next () {
        rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
                "$dotest/patch" "$dotest/info"
@@ -35,46 +59,12 @@ fall_back_3way () {
        GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
        git-write-tree >"$dotest/patch-merge-base+" &&
        # index has the base tree now.
-       (
-           cd "$dotest/patch-merge-tmp-dir" &&
-           GIT_INDEX_FILE="../patch-merge-tmp-index" \
-           GIT_OBJECT_DIRECTORY="$O_OBJECT" \
-           git-apply $binary --index <../patch
-        )
+       GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
+       git-apply $binary --cached <"$dotest/patch"
     then
        echo Using index info to reconstruct a base tree...
        mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
        mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
-    else
-       # Otherwise, try nearby trees that can be used to apply the
-       # patch.
-       (
-           N=10
-
-           # Hoping the patch is against our recent commits...
-           git-rev-list --max-count=$N HEAD
-
-           # or hoping the patch is against known tags...
-           git-ls-remote --tags .
-       ) |
-       while read base junk
-       do
-           # See if we have it as a tree...
-           git-cat-file tree "$base" >/dev/null 2>&1 || continue
-
-           rm -fr "$dotest"/patch-merge-* &&
-           mkdir "$dotest/patch-merge-tmp-dir" || break
-           (
-               cd "$dotest/patch-merge-tmp-dir" &&
-               GIT_INDEX_FILE=../patch-merge-tmp-index &&
-               GIT_OBJECT_DIRECTORY="$O_OBJECT" &&
-               export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY &&
-               git-read-tree "$base" &&
-               git-apply $binary --index &&
-               mv ../patch-merge-tmp-index ../patch-merge-index &&
-               echo "$base" >../patch-merge-base
-           ) <"$dotest/patch"  2>/dev/null && break
-       done
     fi
 
     test -f "$dotest/patch-merge-index" &&
@@ -101,7 +91,7 @@ fall_back_3way () {
 }
 
 prec=4
-dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws=
+dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws= resolvemsg=
 
 while case "$#" in 0) break;; esac
 do
@@ -137,6 +127,9 @@ do
        --whitespace=*)
        ws=$1; shift ;;
 
+       --resolvemsg=*)
+       resolvemsg=$(echo "$1" | sed -e "s/^--resolvemsg=//"); shift ;;
+
        --)
        shift; break ;;
        -*)
@@ -165,7 +158,7 @@ then
 else
        # Make sure we are not given --skip nor --resolved
        test ",$skip,$resolved," = ,,, ||
-               die "we are not resuming."
+               die "Resolve operation not in progress, we are not resuming."
 
        # Start afresh.
        mkdir -p "$dotest" || exit
@@ -374,7 +367,14 @@ do
                if test '' = "$changed"
                then
                        echo "No changes - did you forget update-index?"
-                       stop_here $this
+                       stop_here_user_resolve $this
+               fi
+               unmerged=$(git-ls-files -u)
+               if test -n "$unmerged"
+               then
+                       echo "You still have unmerged paths in your index"
+                       echo "did you forget update-index?"
+                       stop_here_user_resolve $this
                fi
                apply_status=0
                ;;
@@ -400,7 +400,7 @@ do
        if test $apply_status != 0
        then
                echo Patch failed at $msgnum.
-               stop_here $this
+               stop_here_user_resolve $this
        fi
 
        if test -x "$GIT_DIR"/hooks/pre-applypatch
@@ -413,7 +413,7 @@ do
        parent=$(git-rev-parse --verify HEAD) &&
        commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
        echo Committed: $commit &&
-       git-update-ref HEAD $commit $parent ||
+       git-update-ref -m "am: $SUBJECT" HEAD $commit $parent ||
        stop_here $this
 
        if test -x "$GIT_DIR"/hooks/post-applypatch
index 9df72a1..a6a7a48 100755 (executable)
@@ -10,9 +10,10 @@ use warnings;
 use strict;
 use Getopt::Long;
 use POSIX qw(strftime gmtime);
+use File::Basename qw(basename dirname);
 
 sub usage() {
-       print STDERR 'Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
+       print STDERR "Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
        -l, --long
                        Show long rev (Defaults off)
        -t, --time
@@ -23,7 +24,7 @@ sub usage() {
                        Use revs from revs-file instead of calling git-rev-list
        -h, --help
                        This message.
-';
+";
 
        exit(1);
 }
@@ -35,7 +36,7 @@ my $rc = GetOptions(  "long|l" => \$longrev,
                        "help|h" => \$help,
                        "rename|r" => \$rename,
                        "rev-file|S=s" => \$rev_file);
-if (!$rc or $help) {
+if (!$rc or $help or !@ARGV) {
        usage();
 }
 
@@ -208,6 +209,9 @@ sub find_parent_renames {
        while (my $change = <$patch>) {
                chomp $change;
                my $filename = <$patch>;
+               if (!defined $filename) {
+                       next;
+               }
                chomp $filename;
 
                if ($change =~ m/^[AMD]$/ ) {
index 12cab1e..e4b0947 100755 (executable)
@@ -204,7 +204,7 @@ echo Wrote tree $tree
 parent=$(git-rev-parse --verify HEAD) &&
 commit=$(git-commit-tree $tree -p $parent <"$final") || exit 1
 echo Committed: $commit
-git-update-ref HEAD $commit $parent || exit
+git-update-ref -m "applypatch: $SUBJECT" HEAD $commit $parent || exit
 
 if test -x "$GIT_DIR"/hooks/post-applypatch
 then
index 663a3a3..e0501ec 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-USAGE='[(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]]'
+USAGE='[-l] [(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r'
 LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
 If one argument, create a new branch <branchname> based off of current HEAD.
 If two arguments, create a new branch <branchname> based off of <start-point>.'
@@ -42,6 +42,7 @@ If you are sure you want to delete it, run 'git branch -D $branch_name'."
            esac
            ;;
        esac
+       rm -f "$GIT_DIR/logs/refs/heads/$branch_name"
        rm -f "$GIT_DIR/refs/heads/$branch_name"
        echo "Deleted branch $branch_name."
     done
@@ -55,6 +56,7 @@ ls_remote_branches () {
 }
 
 force=
+create_log=
 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
 do
        case "$1" in
@@ -69,6 +71,9 @@ do
        -f)
                force="$1"
                ;;
+       -l)
+               create_log="yes"
+               ;;
        --)
                shift
                break
@@ -82,8 +87,7 @@ done
 
 case "$#" in
 0)
-       git-rev-parse --symbolic --all |
-       sed -ne 's|^refs/heads/||p' |
+       git-rev-parse --symbolic --branches |
        sort |
        while read ref
        do
@@ -118,4 +122,9 @@ then
                die "cannot force-update the current branch."
        fi
 fi
-git update-ref "refs/heads/$branchname" $rev
+if test "$create_log" = 'yes'
+then
+       mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$branchname")
+       touch "$GIT_DIR/logs/refs/heads/$branchname"
+fi
+git update-ref -m "branch: Created from $head" "refs/heads/$branchname" $rev
index 463ed2e..564117f 100755 (executable)
@@ -5,10 +5,13 @@ SUBDIRECTORY_OK=Sometimes
 . git-sh-setup
 
 old=$(git-rev-parse HEAD)
+old_name=HEAD
 new=
+new_name=
 force=
 branch=
 newbranch=
+newbranch_log=
 merge=
 while [ "$#" != "0" ]; do
     arg="$1"
@@ -24,6 +27,9 @@ while [ "$#" != "0" ]; do
                git-check-ref-format "heads/$newbranch" ||
                        die "git checkout: we do not like '$newbranch' as a branch name."
                ;;
+       "-l")
+               newbranch_log=1
+               ;;
        "-f")
                force=1
                ;;
@@ -44,6 +50,7 @@ while [ "$#" != "0" ]; do
                                exit 1
                        fi
                        new="$rev"
+                       new_name="$arg^0"
                        if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
                                branch="$arg"
                        fi
@@ -51,9 +58,11 @@ while [ "$#" != "0" ]; do
                then
                        # checking out selected paths from a tree-ish.
                        new="$rev"
+                       new_name="$arg^{tree}"
                        branch=
                else
                        new=
+                       new_name=
                        branch=
                        set x "$arg" "$@"
                        shift
@@ -114,7 +123,7 @@ then
        cd "$cdup"
 fi
 
-[ -z "$new" ] && new=$old
+[ -z "$new" ] && new=$old && new_name="$old_name"
 
 # If we don't have an old branch that we're switching to,
 # and we don't have a new branch name for the target we
@@ -144,7 +153,7 @@ else
        work=`git write-tree` &&
        git read-tree --reset $new &&
        git checkout-index -f -u -q -a &&
-       git read-tree -m -u $old $new $work || exit
+       git read-tree -m -u --aggressive $old $new $work || exit
 
        if result=`git write-tree 2>/dev/null`
        then
@@ -187,9 +196,11 @@ fi
 #
 if [ "$?" -eq 0 ]; then
        if [ "$newbranch" ]; then
-               leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
-               mkdir -p "$GIT_DIR/$leading" &&
-               echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
+               if [ "$newbranch_log" ]; then
+                       mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
+                       touch "$GIT_DIR/logs/refs/heads/$newbranch"
+               fi
+               git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
                branch="$newbranch"
        fi
        [ "$branch" ] &&
index b200868..3834323 100755 (executable)
@@ -3,13 +3,15 @@
 # Copyright (c) 2005-2006 Pavel Roskin
 #
 
-USAGE="[-d] [-n] [-q] [-x | -X]"
+USAGE="[-d] [-n] [-q] [-x | -X] [--] <paths>..."
 LONG_USAGE='Clean untracked files from the working directory
        -d      remove directories as well
        -n      don'\''t remove anything, just show what would be done
        -q      be quiet, only report errors
        -x      remove ignored files as well
-       -X      remove only ignored files as well'
+       -X      remove only ignored files
+When optional <paths>... arguments are given, the paths
+affected are further limited to those that match them.'
 SUBDIRECTORY_OK=Yes
 . git-sh-setup
 
@@ -17,8 +19,8 @@ ignored=
 ignoredonly=
 cleandir=
 quiet=
-rmf="rm -f"
-rmrf="rm -rf"
+rmf="rm -f --"
+rmrf="rm -rf --"
 rm_refuse="echo Not removing"
 echo1="echo"
 
@@ -44,8 +46,15 @@ do
        -X)
                ignoredonly=1
                ;;
-       *)
+       --)
+               shift
+               break
+               ;;
+       -*)
                usage
+               ;;
+       *)
+               break
        esac
        shift
 done
@@ -64,7 +73,7 @@ if [ -z "$ignored" ]; then
        fi
 fi
 
-git-ls-files --others --directory $excl ${excl_info:+"$excl_info"} |
+git-ls-files --others --directory $excl ${excl_info:+"$excl_info"} -- "$@" |
 while read -r file; do
        if [ -d "$file" -a ! -L "$file" ]; then
                if [ -z "$cleandir" ]; then
index 0805168..6fa0daa 100755 (executable)
@@ -9,7 +9,7 @@
 unset CDPATH
 
 usage() {
-       echo >&2 "Usage: $0 [--use-separate-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [-n] <repo> [<dir>]"
+       echo >&2 "Usage: $0 [--template=<template_directory>] [--use-separate-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [-n] <repo> [<dir>]"
        exit 1
 }
 
@@ -29,7 +29,7 @@ http_fetch () {
 clone_dumb_http () {
        # $1 - remote, $2 - local
        cd "$2" &&
-       clone_tmp='.git/clone-tmp' &&
+       clone_tmp="$GIT_DIR/clone-tmp" &&
        mkdir -p "$clone_tmp" || exit 1
        http_fetch "$1/info/refs" "$clone_tmp/refs" || {
                echo >&2 "Cannot get remote repository information.
@@ -102,6 +102,7 @@ quiet=
 local=no
 use_local=no
 local_shared=no
+unset template
 no_checkout=
 upload_pack=
 bare=
@@ -120,6 +121,11 @@ while
        *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
         *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
           local_shared=yes; use_local=yes ;;
+       1,--template) usage ;;
+       *,--template)
+               shift; template="--template=$1" ;;
+       *,--template=*)
+         template="$1" ;;
        *,-q|*,--quiet) quiet=-q ;;
        *,--use-separate-remote)
                use_separate_remote=t ;;
@@ -199,17 +205,13 @@ dir="$2"
 [ -e "$dir" ] && echo "$dir already exists." && usage
 mkdir -p "$dir" &&
 D=$(cd "$dir" && pwd) &&
-trap 'err=$?; cd ..; rm -r "$D"; exit $err' exit
-case "$bare" in
-yes) GIT_DIR="$D" ;;
-*) GIT_DIR="$D/.git" ;;
-esac && export GIT_DIR && git-init-db || usage
+trap 'err=$?; cd ..; rm -r "$D"; exit $err' 0
 case "$bare" in
 yes)
        GIT_DIR="$D" ;;
 *)
        GIT_DIR="$D/.git" ;;
-esac
+esac && export GIT_DIR && git-init-db ${template+"$template"} || usage
 
 if test -n "$reference"
 then
@@ -261,11 +263,7 @@ yes,yes)
            ;;
        yes)
            mkdir -p "$GIT_DIR/objects/info"
-           {
-               test -f "$repo/objects/info/alternates" &&
-               cat "$repo/objects/info/alternates";
-               echo "$repo/objects"
-           } >"$GIT_DIR/objects/info/alternates"
+           echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
            ;;
        esac
        git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD"
@@ -389,11 +387,16 @@ Pull: refs/heads/$head_points_at:$origin_track" &&
                (cd "$GIT_DIR/$remote_top" && find . -type f -print) |
                while read dotslref
                do
-                       name=`expr "$dotslref" : './\(.*\)'` &&
-                       test "$use_separate_remote" = '' && {
-                               test "$head_points_at" = "$name" ||
-                               test "$origin" = "$name"
-                       } ||
+                       name=`expr "$dotslref" : './\(.*\)'`
+                       if test "z$head_points_at" = "z$name"
+                       then
+                               continue
+                       fi
+                       if test "$use_separate_remote" = '' &&
+                          test "z$origin" = "z$name"
+                       then
+                               continue
+                       fi
                        echo "Pull: refs/heads/${name}:$remote_top/${name}"
                done >>"$GIT_DIR/remotes/$origin" &&
                case "$use_separate_remote" in
@@ -411,5 +414,5 @@ Pull: refs/heads/$head_points_at:$origin_track" &&
 fi
 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
 
-trap - exit
+trap - 0
 
index 26cd7ca..6dd04fd 100755 (executable)
@@ -3,7 +3,7 @@
 # Copyright (c) 2005 Linus Torvalds
 # Copyright (c) 2006 Junio C Hamano
 
-USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>) [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
+USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-u] [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
 SUBDIRECTORY_OK=Yes
 . git-sh-setup
 
@@ -134,13 +134,17 @@ run_status () {
        report "Changed but not updated" \
            "use git-update-index to mark for commit"
 
+        option=""
+        if test -z "$untracked_files"; then
+            option="--directory --no-empty-directory"
+        fi
        if test -f "$GIT_DIR/info/exclude"
        then
-           git-ls-files -z --others --directory \
+           git-ls-files -z --others $option \
                --exclude-from="$GIT_DIR/info/exclude" \
                --exclude-per-directory=.gitignore
        else
-           git-ls-files -z --others --directory \
+           git-ls-files -z --others $option \
                --exclude-per-directory=.gitignore
        fi |
        perl -e '$/ = "\0";
@@ -203,6 +207,7 @@ verbose=
 signoff=
 force_author=
 only_include_assumed=
+untracked_files=
 while case "$#" in 0) break;; esac
 do
   case "$1" in
@@ -255,20 +260,41 @@ do
   -m|--m|--me|--mes|--mess|--messa|--messag|--message)
       case "$#" in 1) usage ;; esac
       shift
-      log_given=t$log_given
-      log_message="$1"
+      log_given=m$log_given
+      if test "$log_message" = ''
+      then
+          log_message="$1"
+      else
+          log_message="$log_message
+
+$1"
+      fi
       no_edit=t
       shift
       ;;
   -m*)
-      log_given=t$log_given
-      log_message=`expr "$1" : '-m\(.*\)'`
+      log_given=m$log_given
+      if test "$log_message" = ''
+      then
+          log_message=`expr "$1" : '-m\(.*\)'`
+      else
+          log_message="$log_message
+
+`expr "$1" : '-m\(.*\)'`"
+      fi
       no_edit=t
       shift
       ;;
   --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
-      log_given=t$log_given
-      log_message=`expr "$1" : '-[^=]*=\(.*\)'`
+      log_given=m$log_given
+      if test "$log_message" = ''
+      then
+          log_message=`expr "$1" : '-[^=]*=\(.*\)'`
+      else
+          log_message="$log_message
+
+`expr "$1" : '-[^=]*=\(.*\)'`"
+      fi
       no_edit=t
       shift
       ;;
@@ -340,6 +366,12 @@ do
       verbose=t
       shift
       ;;
+  -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|--untracked|\
+  --untracked-|--untracked-f|--untracked-fi|--untracked-fil|--untracked-file|\
+  --untracked-files)
+      untracked_files=t
+      shift
+      ;;
   --)
       shift
       break
@@ -367,7 +399,9 @@ esac
 
 case "$log_given" in
 tt*)
-  die "Only one of -c/-C/-F/-m can be used." ;;
+  die "Only one of -c/-C/-F can be used." ;;
+*tm*|*mt*)
+  die "Option -m cannot be combined with -c/-C/-F." ;;
 esac
 
 case "$#,$also,$only,$amend" in
@@ -615,6 +649,9 @@ fi
 if test -z "$no_edit"
 then
        {
+               echo ""
+               echo "# Please enter the commit message for your changes."
+               echo "# (Comment lines starting with '#' will not be included)"
                test -z "$only_include_assumed" || echo "$only_include_assumed"
                run_status
        } >>"$GIT_DIR"/COMMIT_EDITMSG
@@ -640,6 +677,8 @@ case "$no_edit" in
                exit 1
                ;;
        esac
+       git-var GIT_AUTHOR_IDENT > /dev/null  || die
+       git-var GIT_COMMITTER_IDENT > /dev/null  || die
        ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
        ;;
 esac
@@ -674,7 +713,8 @@ then
                rm -f "$TMP_INDEX"
        fi &&
        commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
-       git-update-ref HEAD $commit $current &&
+       rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
+       git-update-ref -m "commit: $rlogm" HEAD $commit $current &&
        rm -f -- "$GIT_DIR/MERGE_HEAD" &&
        if test -f "$NEXT_INDEX"
        then
diff --git a/git-count-objects.sh b/git-count-objects.sh
deleted file mode 100755 (executable)
index 40c58ef..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) 2005 Junio C Hamano
-#
-
-GIT_DIR=`git-rev-parse --git-dir` || exit $?
-
-dc </dev/null 2>/dev/null || {
-       # This is not a real DC at all -- it just knows how
-       # this script feeds DC and does the computation itself.
-       dc () {
-               while read a b
-               do
-                       case $a,$b in
-                       0,)     acc=0 ;;
-                       *,+)    acc=$(($acc + $a)) ;;
-                       p,)     echo "$acc" ;;
-                       esac
-               done
-       }
-}
-
-echo $(find "$GIT_DIR/objects"/?? -type f -print 2>/dev/null | wc -l) objects, \
-$({
-    echo 0
-    # "no-such" is to help Darwin folks by not using xargs -r.
-    find "$GIT_DIR/objects"/?? -type f -print 2>/dev/null |
-    xargs du -k "$GIT_DIR/objects/no-such" 2>/dev/null |
-    sed -e 's/[        ].*/ +/'
-    echo p
-} | dc) kilobytes
index 7b3a3d3..d1051d0 100755 (executable)
@@ -1,18 +1,24 @@
 #!/usr/bin/perl -w
 
+# Known limitations:
+# - cannot add or remove binary files
+# - does not propagate permissions
+# - tells "ready for commit" even when things could not be completed
+#   (eg addition of a binary file)
+
 use strict;
 use Getopt::Std;
 use File::Temp qw(tempdir);
 use Data::Dumper;
-use File::Basename qw(basename);
+use File::Basename qw(basename dirname);
 
 unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
     die "GIT_DIR is not defined or is unreadable";
 }
 
-our ($opt_h, $opt_p, $opt_v, $opt_c );
+our ($opt_h, $opt_p, $opt_v, $opt_c, $opt_f, $opt_m );
 
-getopts('hpvc');
+getopts('hpvcfm:');
 
 $opt_h && usage();
 
@@ -77,18 +83,29 @@ if ($parent) {
 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
 
 # grab the commit message
-`git-cat-file commit $commit | sed -e '1,/^\$/d' > .msg`;
+open(MSG, ">.msg") or die "Cannot open .msg for writing";
+print MSG $opt_m;
+close MSG;
+
+`git-cat-file commit $commit | sed -e '1,/^\$/d' >> .msg`;
 $? && die "Error extracting the commit message";
 
-my (@afiles, @dfiles, @mfiles);
+my (@afiles, @dfiles, @mfiles, @dirs);
 my @files = safe_pipe_capture('git-diff-tree', '-r', $parent, $commit);
-print @files;
+#print @files;
 $? && die "Error in git-diff-tree";
 foreach my $f (@files) {
     chomp $f;
     my @fields = split(m!\s+!, $f);
     if ($fields[4] eq 'A') {
-       push @afiles, $fields[5];
+        my $path = $fields[5];
+       push @afiles, $path;
+        # add any needed parent directories
+       $path = dirname $path;
+       while (!-d $path and ! grep { $_ eq $path } @dirs) {
+           unshift @dirs, $path;
+           $path = dirname $path;
+       }
     }
     if ($fields[4] eq 'M') {
        push @mfiles, $fields[5];
@@ -103,13 +120,21 @@ undef @files; # don't need it anymore
 
 # check that the files are clean and up to date according to cvs
 my $dirty;
+foreach my $d (@dirs) {
+    if (-e $d) {
+       $dirty = 1;
+       warn "$d exists and is not a directory!\n";
+    }
+}
 foreach my $f (@afiles) {
     # This should return only one value
     my @status = grep(m/^File/,  safe_pipe_capture('cvs', '-q', 'status' ,$f));
     if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
-    unless ($status[0] =~ m/Status: Unknown$/) {
+    if (-d dirname $f and $status[0] !~ m/Status: Unknown$/
+       and $status[0] !~ m/^File: no file /) {
        $dirty = 1;
-       warn "File $f is already known in your CVS checkout!\n";
+       warn "File $f is already known in your CVS checkout -- perhaps it has been added by another user. Or this may indicate that it exists on a different branch. If this is the case, use -f to force the merge.\n";
+       warn "Status was: $status[0]\n";
     }
 }
 foreach my $f (@mfiles, @dfiles) {
@@ -122,7 +147,11 @@ foreach my $f (@mfiles, @dfiles) {
     }
 }
 if ($dirty) {
-    die "Exiting: your CVS tree is not clean for this merge.";
+    if ($opt_f) {      warn "The tree is not clean -- forced merge\n";
+       $dirty = 0;
+    } else {
+       die "Exiting: your CVS tree is not clean for this merge.";
+    }
 }
 
 ###
@@ -131,6 +160,19 @@ if ($dirty) {
 ###
 
 
+print "Creating new directories\n";
+foreach my $d (@dirs) {
+    unless (mkdir $d) {
+        warn "Could not mkdir $d: $!";
+       $dirty = 1;
+    }
+    `cvs add $d`;
+    if ($?) {
+       $dirty = 1;
+       warn "Failed to cvs add directory $d -- you may need to do it manually";
+    }
+}
+
 print "'Patching' binary files\n";
 
 my @bfiles = grep(m/^Binary/, safe_pipe_capture('git-diff-tree', '-p', $parent, $commit));
@@ -143,7 +185,7 @@ foreach my $f (@bfiles) {
     my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
     chomp $blob;
     `git-cat-file blob $blob > $tmpdir/blob`;
-    if (system('cmp', '-q', $f, "$tmpdir/blob")) {
+    if (system('cmp', '-s', $f, "$tmpdir/blob")) {
        warn "Binary file $f in CVS does not match parent.\n";
        $dirty = 1;
        next;
@@ -215,7 +257,7 @@ if ($opt_c) {
 }
 sub usage {
        print STDERR <<END;
-Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [ parent ] commit
+Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
 END
        exit(1);
 }
old mode 100755 (executable)
new mode 100644 (file)
index c0ae00b..f3daa6c
@@ -23,13 +23,13 @@ use File::Basename qw(basename dirname);
 use Time::Local;
 use IO::Socket;
 use IO::Pipe;
-use POSIX qw(strftime dup2);
+use POSIX qw(strftime dup2 ENOENT);
 use IPC::Open2;
 
 $SIG{'PIPE'}="IGNORE";
 $ENV{'TZ'}="UTC";
 
-our($opt_h,$opt_o,$opt_v,$opt_k,$opt_u,$opt_d,$opt_p,$opt_C,$opt_z,$opt_i,$opt_P, $opt_s,$opt_m,$opt_M,$opt_A,$opt_S);
+our($opt_h,$opt_o,$opt_v,$opt_k,$opt_u,$opt_d,$opt_p,$opt_C,$opt_z,$opt_i,$opt_P, $opt_s,$opt_m,$opt_M,$opt_A,$opt_S,$opt_L);
 my (%conv_author_name, %conv_author_email);
 
 sub usage() {
@@ -85,7 +85,7 @@ sub write_author_info($) {
        close ($f);
 }
 
-getopts("hivmkuo:d:p:C:z:s:M:P:A:S:") or usage();
+getopts("hivmkuo:d:p:C:z:s:M:P:A:S:L:") or usage();
 usage if $opt_h;
 
 @ARGV <= 1 or usage();
@@ -315,15 +315,7 @@ sub _line {
                        chomp $cnt;
                        die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
                        $line="";
-                       $res=0;
-                       while($cnt) {
-                               my $buf;
-                               my $num = $self->{'socketi'}->read($buf,$cnt);
-                               die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
-                               print $fh $buf;
-                               $res += $num;
-                               $cnt -= $num;
-                       }
+                       $res = $self->_fetchfile($fh, $cnt);
                } elsif($line =~ s/^ //) {
                        print $fh $line;
                        $res += length($line);
@@ -335,14 +327,7 @@ sub _line {
                        chomp $cnt;
                        die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
                        $line="";
-                       while($cnt) {
-                               my $buf;
-                               my $num = $self->{'socketi'}->read($buf,$cnt);
-                               die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
-                               print $fh $buf;
-                               $res += $num;
-                               $cnt -= $num;
-                       }
+                       $res += $self->_fetchfile($fh, $cnt);
                } else {
                        chomp $line;
                        if($line eq "ok") {
@@ -350,7 +335,7 @@ sub _line {
                                return $res;
                        } elsif($line =~ s/^E //) {
                                # print STDERR "S: $line\n";
-                       } elsif($line =~ /^Remove-entry /i) {
+                       } elsif($line =~ /^(Remove-entry|Removed) /i) {
                                $line = $self->readline(); # filename
                                $line = $self->readline(); # OK
                                chomp $line;
@@ -384,6 +369,23 @@ sub file {
 
        return ($name, $res);
 }
+sub _fetchfile {
+       my ($self, $fh, $cnt) = @_;
+       my $res = 0;
+       my $bufsize = 1024 * 1024;
+       while($cnt) {
+           if ($bufsize > $cnt) {
+               $bufsize = $cnt;
+           }
+           my $buf;
+           my $num = $self->{'socketi'}->read($buf,$bufsize);
+           die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
+           print $fh $buf;
+           $res += $num;
+           $cnt -= $num;
+       }
+       return $res;
+}
 
 
 package main;
@@ -429,22 +431,25 @@ sub getwd() {
        return $pwd;
 }
 
+sub is_sha1 {
+       my $s = shift;
+       return $s =~ /^[a-f0-9]{40}$/;
+}
 
-sub get_headref($$) {
+sub get_headref ($$) {
     my $name    = shift;
     my $git_dir = shift; 
-    my $sha;
     
-    if (open(C,"$git_dir/refs/heads/$name")) {
-       chomp($sha = <C>);
-       close(C);
-       length($sha) == 40
-           or die "Cannot get head id for $name ($sha): $!\n";
+    my $f = "$git_dir/refs/heads/$name";
+    if(open(my $fh, $f)) {
+           chomp(my $r = <$fh>);
+           is_sha1($r) or die "Cannot get head id for $name ($r): $!";
+           return $r;
     }
-    return $sha;
+    die "unable to open $f: $!" unless $! == POSIX::ENOENT;
+    return undef;
 }
 
-
 -d $git_tree
        or mkdir($git_tree,0777)
        or die "Could not create $git_tree: $!";
@@ -460,10 +465,15 @@ $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
 $ENV{"GIT_DIR"} = $git_dir;
 my $orig_git_index;
 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
-my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
-                                   DIR => File::Spec->tmpdir());
-close ($git_ih);
-$ENV{GIT_INDEX_FILE} = $git_index;
+
+my %index; # holds filenames of one index per branch
+{   # init with an index for origin
+    my ($fh, $fn) = tempfile('gitXXXXXX', SUFFIX => '.idx',
+                            DIR => File::Spec->tmpdir());
+    close ($fh);
+    $index{$opt_o} = $fn;
+}
+$ENV{GIT_INDEX_FILE} = $index{$opt_o};
 unless(-d $git_dir) {
        system("git-init-db");
        die "Cannot init the GIT db at $git_tree: $?\n" if $?;
@@ -491,6 +501,13 @@ unless(-d $git_dir) {
        $tip_at_start = `git-rev-parse --verify HEAD`;
 
        # populate index
+       unless ($index{$last_branch}) {
+           my ($fh, $fn) = tempfile('gitXXXXXX', SUFFIX => '.idx',
+                                    DIR => File::Spec->tmpdir());
+           close ($fh);
+           $index{$last_branch} = $fn;
+       }
+       $ENV{GIT_INDEX_FILE} = $index{$last_branch};
        system('git-read-tree', $last_branch);
        die "read-tree failed: $?\n" if $?;
 
@@ -524,25 +541,39 @@ if ($opt_A) {
        write_author_info("$git_dir/cvs-authors");
 }
 
-my $pid = open(CVS,"-|");
-die "Cannot fork: $!\n" unless defined $pid;
-unless($pid) {
-       my @opt;
-       @opt = split(/,/,$opt_p) if defined $opt_p;
-       unshift @opt, '-z', $opt_z if defined $opt_z;
-       unshift @opt, '-q'         unless defined $opt_v;
-       unless (defined($opt_p) && $opt_p =~ m/--no-cvs-direct/) {
-               push @opt, '--cvs-direct';
+
+#
+# run cvsps into a file unless we are getting
+# it passed as a file via $opt_P
+#
+unless ($opt_P) {
+       print "Running cvsps...\n" if $opt_v;
+       my $pid = open(CVSPS,"-|");
+       die "Cannot fork: $!\n" unless defined $pid;
+       unless($pid) {
+               my @opt;
+               @opt = split(/,/,$opt_p) if defined $opt_p;
+               unshift @opt, '-z', $opt_z if defined $opt_z;
+               unshift @opt, '-q'         unless defined $opt_v;
+               unless (defined($opt_p) && $opt_p =~ m/--no-cvs-direct/) {
+                       push @opt, '--cvs-direct';
+               }
+               exec("cvsps","--norc",@opt,"-u","-A",'--root',$opt_d,$cvs_tree);
+               die "Could not start cvsps: $!\n";
        }
-       if ($opt_P) {
-           exec("cat", $opt_P);
-       } else {
-           exec("cvsps","--norc",@opt,"-u","-A",'--root',$opt_d,$cvs_tree);
-           die "Could not start cvsps: $!\n";
+       my ($cvspsfh, $cvspsfile) = tempfile('gitXXXXXX', SUFFIX => '.cvsps',
+                                            DIR => File::Spec->tmpdir());
+       while (<CVSPS>) {
+           print $cvspsfh $_;
        }
+       close CVSPS;
+       close $cvspsfh;
+       $opt_P = $cvspsfile;
 }
 
 
+open(CVS, "<$opt_P") or die $!;
+
 ## cvsps output:
 #---------------------
 #PatchSet 314
@@ -561,98 +592,70 @@ unless($pid) {
 
 my $state = 0;
 
-my($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
-my(@old,@new,@skipped);
-my $commit = sub {
-       my $pid;
-       while(@old) {
-               my @o2;
-               if(@old > 55) {
-                       @o2 = splice(@old,0,50);
-               } else {
-                       @o2 = @old;
-                       @old = ();
-               }
-               system("git-update-index","--force-remove","--",@o2);
-               die "Cannot remove files: $?\n" if $?;
-       }
-       while(@new) {
-               my @n2;
-               if(@new > 12) {
-                       @n2 = splice(@new,0,10);
-               } else {
-                       @n2 = @new;
-                       @new = ();
-               }
-               system("git-update-index","--add",
-                       (map { ('--cacheinfo', @$_) } @n2));
-               die "Cannot add files: $?\n" if $?;
-       }
+sub update_index (\@\@) {
+       my $old = shift;
+       my $new = shift;
+       open(my $fh, '|-', qw(git-update-index -z --index-info))
+               or die "unable to open git-update-index: $!";
+       print $fh
+               (map { "0 0000000000000000000000000000000000000000\t$_\0" }
+                       @$old),
+               (map { '100' . sprintf('%o', $_->[0]) . " $_->[1]\t$_->[2]\0" }
+                       @$new)
+               or die "unable to write to git-update-index: $!";
+       close $fh
+               or die "unable to write to git-update-index: $!";
+       $? and die "git-update-index reported error: $?";
+}
 
-       $pid = open(C,"-|");
-       die "Cannot fork: $!" unless defined $pid;
-       unless($pid) {
-               exec("git-write-tree");
-               die "Cannot exec git-write-tree: $!\n";
-       }
-       chomp(my $tree = <C>);
-       length($tree) == 40
-               or die "Cannot get tree id ($tree): $!\n";
-       close(C)
+sub write_tree () {
+       open(my $fh, '-|', qw(git-write-tree))
+               or die "unable to open git-write-tree: $!";
+       chomp(my $tree = <$fh>);
+       is_sha1($tree)
+               or die "Cannot get tree id ($tree): $!";
+       close($fh)
                or die "Error running git-write-tree: $?\n";
        print "Tree ID $tree\n" if $opt_v;
+       return $tree;
+}
 
-       my $parent = "";
-       if(open(C,"$git_dir/refs/heads/$last_branch")) {
-               chomp($parent = <C>);
-               close(C);
-               length($parent) == 40
-                       or die "Cannot get parent id ($parent): $!\n";
-               print "Parent ID $parent\n" if $opt_v;
-       }
-
-       my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
-       my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
-       $pid = fork();
-       die "Fork: $!\n" unless defined $pid;
-       unless($pid) {
-               $pr->writer();
-               $pw->reader();
-               open(OUT,">&STDOUT");
-               dup2($pw->fileno(),0);
-               dup2($pr->fileno(),1);
-               $pr->close();
-               $pw->close();
-
-               my @par = ();
-               @par = ("-p",$parent) if $parent;
-
-               # loose detection of merges
-               # based on the commit msg
-               foreach my $rx (@mergerx) {
-                       if ($logmsg =~ $rx) {
-                               my $mparent = $1;
-                               if ($mparent eq 'HEAD') { $mparent = $opt_o };
-                               if ( -e "$git_dir/refs/heads/$mparent") {
-                                       $mparent = get_headref($mparent, $git_dir);
-                                       push @par, '-p', $mparent;
-                                       print OUT "Merge parent branch: $mparent\n" if $opt_v;
-                               }
-                       }
+my($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
+my(@old,@new,@skipped,%ignorebranch);
+
+# commits that cvsps cannot place anywhere...
+$ignorebranch{'#CVSPS_NO_BRANCH'} = 1;
+
+sub commit {
+       update_index(@old, @new);
+       @old = @new = ();
+       my $tree = write_tree();
+       my $parent = get_headref($last_branch, $git_dir);
+       print "Parent ID " . ($parent ? $parent : "(empty)") . "\n" if $opt_v;
+
+       my @commit_args;
+       push @commit_args, ("-p", $parent) if $parent;
+
+       # loose detection of merges
+       # based on the commit msg
+       foreach my $rx (@mergerx) {
+               next unless $logmsg =~ $rx && $1;
+               my $mparent = $1 eq 'HEAD' ? $opt_o : $1;
+               if(my $sha1 = get_headref($mparent, $git_dir)) {
+                       push @commit_args, '-p', $mparent;
+                       print "Merge parent branch: $mparent\n" if $opt_v;
                }
-
-               exec("env",
-                       "GIT_AUTHOR_NAME=$author_name",
-                       "GIT_AUTHOR_EMAIL=$author_email",
-                       "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
-                       "GIT_COMMITTER_NAME=$author_name",
-                       "GIT_COMMITTER_EMAIL=$author_email",
-                       "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
-                       "git-commit-tree", $tree,@par);
-               die "Cannot exec git-commit-tree: $!\n";
        }
-       $pw->writer();
-       $pr->reader();
+
+       my $commit_date = strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date));
+       $ENV{GIT_AUTHOR_NAME} = $author_name;
+       $ENV{GIT_AUTHOR_EMAIL} = $author_email;
+       $ENV{GIT_AUTHOR_DATE} = $commit_date;
+       $ENV{GIT_COMMITTER_NAME} = $author_name;
+       $ENV{GIT_COMMITTER_EMAIL} = $author_email;
+       $ENV{GIT_COMMITTER_DATE} = $commit_date;
+       my $pid = open2(my $commit_read, my $commit_write,
+               'git-commit-tree', $tree, @commit_args);
 
        # compatibility with git2cvs
        substr($logmsg,32767) = "" if length($logmsg) > 32767;
@@ -661,18 +664,17 @@ my $commit = sub {
        if (@skipped) {
            $logmsg .= "\n\n\nSKIPPED:\n\t";
            $logmsg .= join("\n\t", @skipped) . "\n";
+           @skipped = ();
        }
 
-       print $pw "$logmsg\n"
+       print($commit_write "$logmsg\n") && close($commit_write)
                or die "Error writing to git-commit-tree: $!\n";
-       $pw->close();
 
-       print "Committed patch $patchset ($branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
-       chomp(my $cid = <$pr>);
-       length($cid) == 40
-               or die "Cannot get commit id ($cid): $!\n";
+       print "Committed patch $patchset ($branch $commit_date)\n" if $opt_v;
+       chomp(my $cid = <$commit_read>);
+       is_sha1($cid) or die "Cannot get commit id ($cid): $!\n";
        print "Commit ID $cid\n" if $opt_v;
-       $pr->close();
+       close($commit_read);
 
        waitpid($pid,0);
        die "Error running git-commit-tree: $?\n" if $?;
@@ -716,6 +718,7 @@ my $commit = sub {
        }
 };
 
+my $commitcount = 1;
 while(<CVS>) {
        chomp;
        if($state == 0 and /^-+$/) {
@@ -778,7 +781,16 @@ while(<CVS>) {
                        $state = 11;
                        next;
                }
+               if (exists $ignorebranch{$branch}) {
+                       print STDERR "Skipping $branch\n";
+                       $state = 11;
+                       next;
+               }
                if($ancestor) {
+                       if($ancestor eq $branch) {
+                               print STDERR "Branch $branch erroneously stems from itself -- changed ancestor to $opt_o\n";
+                               $ancestor = $opt_o;
+                       }
                        if(-f "$git_dir/refs/heads/$branch") {
                                print STDERR "Branch $branch already exists!\n";
                                $state=11;
@@ -786,6 +798,7 @@ while(<CVS>) {
                        }
                        unless(open(H,"$git_dir/refs/heads/$ancestor")) {
                                print STDERR "Branch $ancestor does not exist!\n";
+                               $ignorebranch{$branch} = 1;
                                $state=11;
                                next;
                        }
@@ -793,6 +806,7 @@ while(<CVS>) {
                        close(H);
                        unless(open(H,"> $git_dir/refs/heads/$branch")) {
                                print STDERR "Could not create branch $branch: $!\n";
+                               $ignorebranch{$branch} = 1;
                                $state=11;
                                next;
                        }
@@ -803,8 +817,17 @@ while(<CVS>) {
                }
                if(($ancestor || $branch) ne $last_branch) {
                        print "Switching from $last_branch to $branch\n" if $opt_v;
-                       system("git-read-tree", $branch);
-                       die "read-tree failed: $?\n" if $?;
+                       unless ($index{$branch}) {
+                           my ($fh, $fn) = tempfile('gitXXXXXX', SUFFIX => '.idx',
+                                                    DIR => File::Spec->tmpdir());
+                           close ($fh);
+                           $index{$branch} = $fn;
+                           $ENV{GIT_INDEX_FILE} = $index{$branch};
+                           system("git-read-tree", $branch);
+                           die "read-tree failed: $?\n" if $?;
+                       } else {
+                           $ENV{GIT_INDEX_FILE} = $index{$branch};
+                       }
                }
                $last_branch = $branch if $branch ne $last_branch;
                $state = 9;
@@ -849,7 +872,14 @@ while(<CVS>) {
        } elsif($state == 9 and /^\s*$/) {
                $state = 10;
        } elsif(($state == 9 or $state == 10) and /^-+$/) {
-               &$commit();
+               $commitcount++;
+               if ($opt_L && $commitcount > $opt_L) {
+                       last;
+               }
+               commit();
+               if (($commitcount & 1023) == 0) {
+                       system("git repack -a -d");
+               }
                $state = 1;
        } elsif($state == 11 and /^-+$/) {
                $state = 1;
@@ -859,9 +889,11 @@ while(<CVS>) {
                print "* UNKNOWN LINE * $_\n";
        }
 }
-&$commit() if $branch and $state != 11;
+commit() if $branch and $state != 11;
 
-unlink($git_index);
+foreach my $git_index (values %index) {
+    unlink($git_index);
+}
 
 if (defined $orig_git_index) {
        $ENV{GIT_INDEX_FILE} = $orig_git_index;
index 7d3f78e..5ccca4f 100755 (executable)
@@ -88,7 +88,7 @@ my $TEMP_DIR = tempdir( CLEANUP => 1 );
 $log->debug("Temporary directory is '$TEMP_DIR'");
 
 # if we are called with a pserver argument,
-# deal with the authentication cat before entereing the
+# deal with the authentication cat before entering the
 # main loop
 if (@ARGV && $ARGV[0] eq 'pserver') {
     my $line = <STDIN>; chomp $line;
@@ -117,7 +117,7 @@ while (<STDIN>)
 {
     chomp;
 
-    # Check to see if we've seen this method, and call appropiate function.
+    # Check to see if we've seen this method, and call appropriate function.
     if ( /^([\w-]+)(?:\s+(.*))?$/ and defined($methods->{$1}) )
     {
         # use the $methods hash to call the appropriate sub for this command
@@ -171,11 +171,11 @@ sub req_Root
        return 0;
     }
 
-    my @gitvars = `git-var -l`;
+    my @gitvars = `git-repo-config -l`;
     if ($?) {
-       print "E problems executing git-var on the server -- this is not a git repository or the PATH is not set correcly.\n";
+       print "E problems executing git-repo-config on the server -- this is not a git repository or the PATH is not set correctly.\n";
         print "E \n";
-        print "error 1 - problem executing git-var\n";
+        print "error 1 - problem executing git-repo-config\n";
        return 0;
     }
     foreach my $line ( @gitvars )
@@ -214,8 +214,7 @@ sub req_Globaloption
 {
     my ( $cmd, $data ) = @_;
     $log->debug("req_Globaloption : $data");
-
-    # TODO : is this data useful ???
+    $state->{globaloptions}{$data} = 1;
 }
 
 # Valid-responses request-list \n
@@ -224,7 +223,7 @@ sub req_Globaloption
 sub req_Validresponses
 {
     my ( $cmd, $data ) = @_;
-    $log->debug("req_Validrepsonses : $data");
+    $log->debug("req_Validresponses : $data");
 
     # TODO : re-enable this, currently it's not particularly useful
     #$state->{validresponses} = [ split /\s+/, $data ];
@@ -267,12 +266,32 @@ sub req_Directory
 
     $state->{localdir} = $data;
     $state->{repository} = $repository;
-    $state->{directory} = $repository;
-    $state->{directory} =~ s/^$state->{CVSROOT}\///;
-    $state->{module} = $1 if ($state->{directory} =~ s/^(.*?)(\/|$)//);
+    $state->{path} = $repository;
+    $state->{path} =~ s/^$state->{CVSROOT}\///;
+    $state->{module} = $1 if ($state->{path} =~ s/^(.*?)(\/|$)//);
+    $state->{path} .= "/" if ( $state->{path} =~ /\S/ );
+
+    $state->{directory} = $state->{localdir};
+    $state->{directory} = "" if ( $state->{directory} eq "." );
     $state->{directory} .= "/" if ( $state->{directory} =~ /\S/ );
 
-    $log->debug("req_Directory : localdir=$data repository=$repository directory=$state->{directory} module=$state->{module}");
+    if ( not defined($state->{prependdir}) and $state->{localdir} eq "." and $state->{path} =~ /\S/ )
+    {
+        $log->info("Setting prepend to '$state->{path}'");
+        $state->{prependdir} = $state->{path};
+        foreach my $entry ( keys %{$state->{entries}} )
+        {
+            $state->{entries}{$state->{prependdir} . $entry} = $state->{entries}{$entry};
+            delete $state->{entries}{$entry};
+        }
+    }
+
+    if ( defined ( $state->{prependdir} ) )
+    {
+        $log->debug("Prepending '$state->{prependdir}' to state|directory");
+        $state->{directory} = $state->{prependdir} . $state->{directory}
+    }
+    $log->debug("req_Directory : localdir=$data repository=$repository path=$state->{path} directory=$state->{directory} module=$state->{module}");
 }
 
 # Entry entry-line \n
@@ -290,7 +309,7 @@ sub req_Entry
 {
     my ( $cmd, $data ) = @_;
 
-    $log->debug("req_Entry : $data");
+    #$log->debug("req_Entry : $data");
 
     my @data = split(/\//, $data);
 
@@ -300,6 +319,22 @@ sub req_Entry
         options     => $data[4],
         tag_or_date => $data[5],
     };
+
+    $log->info("Received entry line '$data' => '" . $state->{directory} . $data[1] . "'");
+}
+
+# Questionable filename \n
+#     Response expected: no. Additional data: no. Tell the server to check
+#     whether filename should be ignored, and if not, next time the server
+#     sends responses, send (in a M response) `?' followed by the directory and
+#     filename. filename must not contain `/'; it needs to be a file in the
+#     directory named by the most recent Directory request.
+sub req_Questionable
+{
+    my ( $cmd, $data ) = @_;
+
+    $log->debug("req_Questionable : $data");
+    $state->{entries}{$state->{directory}.$data}{questionable} = 1;
 }
 
 # add \n
@@ -332,8 +367,7 @@ sub req_add
             next;
         }
 
-
-        my ( $filepart, $dirpart ) = filenamesplit($filename);
+        my ( $filepart, $dirpart ) = filenamesplit($filename, 1);
 
         print "E cvs add: scheduling file `$filename' for addition\n";
 
@@ -414,7 +448,7 @@ sub req_remove
         }
 
 
-        my ( $filepart, $dirpart ) = filenamesplit($filename);
+        my ( $filepart, $dirpart ) = filenamesplit($filename, 1);
 
         print "E cvs remove: scheduling `$filename' for removal\n";
 
@@ -502,22 +536,6 @@ sub req_Unchanged
     #$log->debug("req_Unchanged : $data");
 }
 
-# Questionable filename \n
-#     Response expected: no. Additional data: no.
-#     Tell the server to check whether filename should be ignored,
-#     and if not, next time the server sends responses, send (in
-#     a M response) `?' followed by the directory and filename.
-#     filename must not contain `/'; it needs to be a file in the
-#     directory named by the most recent Directory request.
-sub req_Questionable
-{
-    my ( $cmd, $data ) = @_;
-
-    $state->{entries}{$state->{directory}.$data}{questionable} = 1;
-
-    #$log->debug("req_Questionable : $data");
-}
-
 # Argument text \n
 #     Response expected: no. Save argument for use in a subsequent command.
 #     Arguments accumulate until an argument-using command is given, at which
@@ -733,7 +751,7 @@ sub req_update
     argsplit("update");
 
     #
-    # It may just be a client exploring the available heads/modukles
+    # It may just be a client exploring the available heads/modules
     # in that case, list them as top level directories and leave it
     # at that. Eclipse uses this technique to offer you a list of
     # projects (heads in this case) to checkout.
@@ -757,8 +775,7 @@ sub req_update
 
     $updater->update();
 
-    # if no files were specified, we need to work out what files we should be providing status on ...
-    argsfromdir($updater) if ( scalar ( @{$state->{args}} ) == 0 );
+    argsfromdir($updater);
 
     #$log->debug("update state : " . Dumper($state));
 
@@ -767,6 +784,8 @@ sub req_update
     {
         $filename = filecleanup($filename);
 
+        $log->debug("Processing file $filename");
+
         # if we have a -C we should pretend we never saw modified stuff
         if ( exists ( $state->{opt}{C} ) )
         {
@@ -821,13 +840,16 @@ sub req_update
 
         if ( $meta->{filehash} eq "deleted" )
         {
-            my ( $filepart, $dirpart ) = filenamesplit($filename);
+            my ( $filepart, $dirpart ) = filenamesplit($filename,1);
 
             $log->info("Removing '$filename' from working copy (no longer in the repo)");
 
             print "E cvs update: `$filename' is no longer in the repository\n";
-            print "Removed $dirpart\n";
-            print "$filepart\n";
+            # Don't want to actually _DO_ the update if -n specified
+            unless ( $state->{globaloptions}{-n} ) {
+               print "Removed $dirpart\n";
+               print "$filepart\n";
+           }
         }
         elsif ( not defined ( $state->{entries}{$filename}{modified_hash} )
                or $state->{entries}{$filename}{modified_hash} eq $oldmeta->{filehash} )
@@ -840,34 +862,42 @@ sub req_update
             print "MT newline\n";
             print "MT -updated\n";
 
-            my ( $filepart, $dirpart ) = filenamesplit($filename);
-            $dirpart =~ s/^$state->{directory}//;
-
-            if ( defined ( $wrev ) )
-            {
-                # instruct client we're sending a file to put in this path as a replacement
-                print "Update-existing $dirpart\n";
-                $log->debug("Updating existing file 'Update-existing $dirpart'");
-            } else {
-                # instruct client we're sending a file to put in this path as a new file
-                print "Created $dirpart\n";
-                $log->debug("Creating new file 'Created $dirpart'");
-            }
-            print $state->{CVSROOT} . "/$state->{module}/$filename\n";
-
-            # this is an "entries" line
-            $log->debug("/$filepart/1.$meta->{revision}///");
-            print "/$filepart/1.$meta->{revision}///\n";
-
-            # permissions
-            $log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
-            print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
-
-            # transmit file
-            transmitfile($meta->{filehash});
+            my ( $filepart, $dirpart ) = filenamesplit($filename,1);
+
+           # Don't want to actually _DO_ the update if -n specified
+           unless ( $state->{globaloptions}{-n} )
+           {
+               if ( defined ( $wrev ) )
+               {
+                   # instruct client we're sending a file to put in this path as a replacement
+                   print "Update-existing $dirpart\n";
+                   $log->debug("Updating existing file 'Update-existing $dirpart'");
+               } else {
+                   # instruct client we're sending a file to put in this path as a new file
+                   print "Clear-static-directory $dirpart\n";
+                   print $state->{CVSROOT} . "/$state->{module}/$dirpart\n";
+                   print "Clear-sticky $dirpart\n";
+                   print $state->{CVSROOT} . "/$state->{module}/$dirpart\n";
+
+                   $log->debug("Creating new file 'Created $dirpart'");
+                   print "Created $dirpart\n";
+               }
+               print $state->{CVSROOT} . "/$state->{module}/$filename\n";
+
+               # this is an "entries" line
+               $log->debug("/$filepart/1.$meta->{revision}///");
+               print "/$filepart/1.$meta->{revision}///\n";
+
+               # permissions
+               $log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
+               print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
+
+               # transmit file
+               transmitfile($meta->{filehash});
+           }
         } else {
             $log->info("Updating '$filename'");
-            my ( $filepart, $dirpart ) = filenamesplit($meta->{name});
+            my ( $filepart, $dirpart ) = filenamesplit($meta->{name},1);
 
             my $dir = tempdir( DIR => $TEMP_DIR, CLEANUP => 1 ) . "/";
 
@@ -892,19 +922,29 @@ sub req_update
                 $log->info("Merged successfully");
                 print "M M $filename\n";
                 $log->debug("Update-existing $dirpart");
-                print "Update-existing $dirpart\n";
-                $log->debug($state->{CVSROOT} . "/$state->{module}/$filename");
-                print $state->{CVSROOT} . "/$state->{module}/$filename\n";
-                $log->debug("/$filepart/1.$meta->{revision}///");
-                print "/$filepart/1.$meta->{revision}///\n";
+
+                # Don't want to actually _DO_ the update if -n specified
+                unless ( $state->{globaloptions}{-n} )
+                {
+                    print "Update-existing $dirpart\n";
+                    $log->debug($state->{CVSROOT} . "/$state->{module}/$filename");
+                    print $state->{CVSROOT} . "/$state->{module}/$filename\n";
+                    $log->debug("/$filepart/1.$meta->{revision}///");
+                    print "/$filepart/1.$meta->{revision}///\n";
+                }
             }
             elsif ( $return == 1 )
             {
                 $log->info("Merged with conflicts");
                 print "M C $filename\n";
-                print "Update-existing $dirpart\n";
-                print $state->{CVSROOT} . "/$state->{module}/$filename\n";
-                print "/$filepart/1.$meta->{revision}/+//\n";
+
+                # Don't want to actually _DO_ the update if -n specified
+                unless ( $state->{globaloptions}{-n} )
+                {
+                    print "Update-existing $dirpart\n";
+                    print $state->{CVSROOT} . "/$state->{module}/$filename\n";
+                    print "/$filepart/1.$meta->{revision}/+//\n";
+                }
             }
             else
             {
@@ -912,17 +952,21 @@ sub req_update
                 next;
             }
 
-            # permissions
-            $log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
-            print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
-
-            # transmit file, format is single integer on a line by itself (file
-            # size) followed by the file contents
-            # TODO : we should copy files in blocks
-            my $data = `cat $file_local`;
-            $log->debug("File size : " . length($data));
-            print length($data) . "\n";
-            print $data;
+            # Don't want to actually _DO_ the update if -n specified
+            unless ( $state->{globaloptions}{-n} )
+            {
+                # permissions
+                $log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
+                print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
+
+                # transmit file, format is single integer on a line by itself (file
+                # size) followed by the file contents
+                # TODO : we should copy files in blocks
+                my $data = `cat $file_local`;
+                $log->debug("File size : " . length($data));
+                print length($data) . "\n";
+                print $data;
+            }
 
             chdir "/";
         }
@@ -950,6 +994,7 @@ sub req_ci
 
     if ( -e $state->{CVSROOT} . "/index" )
     {
+        $log->warn("file 'index' already exists in the git repository");
         print "error 1 Index already exists in git repo\n";
         exit;
     }
@@ -957,6 +1002,7 @@ sub req_ci
     my $lockfile = "$state->{CVSROOT}/refs/heads/$state->{module}.lock";
     unless ( sysopen(LOCKFILE,$lockfile,O_EXCL|O_CREAT|O_WRONLY) )
     {
+        $log->warn("lockfile '$lockfile' already exists, please try again");
         print "error 1 Lock file '$lockfile' already exists, please try again\n";
         exit;
     }
@@ -988,6 +1034,7 @@ sub req_ci
     # foreach file specified on the commandline ...
     foreach my $filename ( @{$state->{args}} )
     {
+        my $committedfile = $filename;
         $filename = filecleanup($filename);
 
         next unless ( exists $state->{entries}{$filename}{modified_filename} or not $state->{entries}{$filename}{unchanged} );
@@ -1022,7 +1069,7 @@ sub req_ci
             exit;
         }
 
-        push @committedfiles, $filename;
+        push @committedfiles, $committedfile;
         $log->info("Committing $filename");
 
         system("mkdir","-p",$dirpart) unless ( -d $dirpart );
@@ -1105,7 +1152,7 @@ sub req_ci
 
         my $meta = $updater->getmeta($filename);
 
-        my ( $filepart, $dirpart ) = filenamesplit($filename);
+        my ( $filepart, $dirpart ) = filenamesplit($filename, 1);
 
         $log->debug("Checked-in $dirpart : $filename");
 
@@ -1141,7 +1188,7 @@ sub req_status
     $updater->update();
 
     # if no files were specified, we need to work out what files we should be providing status on ...
-    argsfromdir($updater) if ( scalar ( @{$state->{args}} ) == 0 );
+    argsfromdir($updater);
 
     # foreach file specified on the commandline ...
     foreach my $filename ( @{$state->{args}} )
@@ -1242,7 +1289,7 @@ sub req_diff
     $updater->update();
 
     # if no files were specified, we need to work out what files we should be providing status on ...
-    argsfromdir($updater) if ( scalar ( @{$state->{args}} ) == 0 );
+    argsfromdir($updater);
 
     # foreach file specified on the commandline ...
     foreach my $filename ( @{$state->{args}} )
@@ -1384,7 +1431,7 @@ sub req_log
     $updater->update();
 
     # if no files were specified, we need to work out what files we should be providing status on ...
-    argsfromdir($updater) if ( scalar ( @{$state->{args}} ) == 0 );
+    argsfromdir($updater);
 
     # foreach file specified on the commandline ...
     foreach my $filename ( @{$state->{args}} )
@@ -1460,7 +1507,7 @@ sub req_annotate
     $updater->update();
 
     # if no files were specified, we need to work out what files we should be providing annotate on ...
-    argsfromdir($updater) if ( scalar ( @{$state->{args}} ) == 0 );
+    argsfromdir($updater);
 
     # we'll need a temporary checkout dir
     my $tmpdir = tempdir ( DIR => $TEMP_DIR );
@@ -1655,13 +1702,36 @@ sub argsfromdir
 {
     my $updater = shift;
 
-    $state->{args} = [];
+    $state->{args} = [] if ( scalar(@{$state->{args}}) == 1 and $state->{args}[0] eq "." );
+
+    return if ( scalar ( @{$state->{args}} ) > 1 );
 
-    foreach my $file ( @{$updater->gethead} )
+    if ( scalar(@{$state->{args}}) == 1 )
     {
-        next if ( $file->{filehash} eq "deleted" and not defined ( $state->{entries}{$file->{name}} ) );
-        next unless ( $file->{name} =~ s/^$state->{directory}// );
-        push @{$state->{args}}, $file->{name};
+        my $arg = $state->{args}[0];
+        $arg .= $state->{prependdir} if ( defined ( $state->{prependdir} ) );
+
+        $log->info("Only one arg specified, checking for directory expansion on '$arg'");
+
+        foreach my $file ( @{$updater->gethead} )
+        {
+            next if ( $file->{filehash} eq "deleted" and not defined ( $state->{entries}{$file->{name}} ) );
+            next unless ( $file->{name} =~ /^$arg\// or $file->{name} eq $arg  );
+            push @{$state->{args}}, $file->{name};
+        }
+
+        shift @{$state->{args}} if ( scalar(@{$state->{args}}) > 1 );
+    } else {
+        $log->info("Only one arg specified, populating file list automatically");
+
+        $state->{args} = [];
+
+        foreach my $file ( @{$updater->gethead} )
+        {
+            next if ( $file->{filehash} eq "deleted" and not defined ( $state->{entries}{$file->{name}} ) );
+            next unless ( $file->{name} =~ s/^$state->{prependdir}// );
+            push @{$state->{args}}, $file->{name};
+        }
     }
 }
 
@@ -1731,16 +1801,22 @@ sub transmitfile
 }
 
 # This method takes a file name, and returns ( $dirpart, $filepart ) which
-# refers to the directory porition and the file portion of the filename
+# refers to the directory portion and the file portion of the filename
 # respectively
 sub filenamesplit
 {
     my $filename = shift;
+    my $fixforlocaldir = shift;
 
     my ( $filepart, $dirpart ) = ( $filename, "." );
     ( $filepart, $dirpart ) = ( $2, $1 ) if ( $filename =~ /(.*)\/(.*)/ );
     $dirpart .= "/";
 
+    if ( $fixforlocaldir )
+    {
+        $dirpart =~ s/^$state->{prependdir}//;
+    }
+
     return ( $filepart, $dirpart );
 }
 
@@ -1756,8 +1832,7 @@ sub filecleanup
     }
 
     $filename =~ s/^\.\///g;
-    $filename = $state->{directory} . $filename;
-
+    $filename = $state->{prependdir} . $filename;
     return $filename;
 }
 
@@ -1790,7 +1865,7 @@ Log::Log4perl
 =head2 new
 
 Creates a new log object, optionally you can specify a filename here to
-indicate the file to log to. If no log file is specified, you can specifiy one
+indicate the file to log to. If no log file is specified, you can specify one
 later with method setfile, or indicate you no longer want logging with method
 nofile.
 
@@ -2076,14 +2151,15 @@ sub update
     # TODO: log processing is memory bound
     # if we can parse into a 2nd file that is in reverse order
     # we can probably do something really efficient
-    my @git_log_params = ('--parents', '--topo-order');
+    my @git_log_params = ('--pretty', '--parents', '--topo-order');
 
     if (defined $lastcommit) {
         push @git_log_params, "$lastcommit..$self->{module}";
     } else {
         push @git_log_params, $self->{module};
     }
-    open(GITLOG, '-|', 'git-log', @git_log_params) or die "Cannot call git-log: $!";
+    # git-rev-list is the backend / plumbing version of git-log
+    open(GITLOG, '-|', 'git-rev-list', @git_log_params) or die "Cannot call git-rev-list: $!";
 
     my @commits;
 
@@ -2595,7 +2671,7 @@ sub in_array
 
 =head2 safe_pipe_capture
 
-an alterative to `command` that allows input to be passed as an array
+an alternative to `command` that allows input to be passed as an array
 to work around shell problems with weird characters in arguments
 
 =cut
diff --git a/git-diff.sh b/git-diff.sh
deleted file mode 100755 (executable)
index 0fe6770..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) 2005 Linus Torvalds
-# Copyright (c) 2005 Junio C Hamano
-
-USAGE='[ --diff-options ] <ent>{0,2} [<path>...]'
-SUBDIRECTORY_OK='Yes'
-. git-sh-setup
-
-rev=$(git-rev-parse --revs-only --no-flags --sq "$@") || exit
-flags=$(git-rev-parse --no-revs --flags --sq "$@")
-files=$(git-rev-parse --no-revs --no-flags --sq "$@")
-
-# I often say 'git diff --cached -p' and get scolded by git-diff-files, but
-# obviously I mean 'git diff --cached -p HEAD' in that case.
-case "$rev" in
-'')
-       case " $flags " in
-       *" '--cached' "*)
-               rev='HEAD '
-               ;;
-       esac
-esac
-
-# If we have -[123] --ours --theirs --base, don't do --cc by default.
-case " $flags " in
-*" '-"[123]"' "* | *" '--ours' "* | *" '--base' "* | *" '--theirs' "*)
-       cc_or_p=-p ;;
-*)
-       cc_or_p=--cc ;;
-esac
-
-# If we do not have --name-status, --name-only, -r, -c or --stat,
-# default to --cc.
-case " $flags " in
-*" '--name-status' "* | *" '--name-only' "* | *" '-r' "* | *" '-c' "* | \
-*" '--stat' "*)
-       ;;
-*)
-       flags="$flags'$cc_or_p' " ;;
-esac
-
-# If we do not have -B, -C, -r, nor -p, default to -M.
-case " $flags " in
-*" '-"[BCMrp]* | *" '--find-copies-harder' "*)
-       ;; # something like -M50.
-*)
-       flags="$flags'-M' " ;;
-esac
-
-case "$rev" in
-?*' '?*' '?*)
-       usage
-       ;;
-?*' '^?*)
-       begin=$(expr "$rev" : '.*^.\([0-9a-f]*\).*') &&
-       end=$(expr "$rev" : '.\([0-9a-f]*\). .*') || exit
-       cmd="git-diff-tree $flags $begin $end -- $files"
-       ;;
-?*' '?*)
-       cmd="git-diff-tree $flags $rev -- $files"
-       ;;
-?*' ')
-       cmd="git-diff-index $flags $rev -- $files"
-       ;;
-'')
-       cmd="git-diff-files $flags -- $files"
-       ;;
-*)
-       usage
-       ;;
-esac
-
-eval "$cmd"
index 83143f8..48818f8 100755 (executable)
@@ -166,7 +166,10 @@ fast_forward_local () {
            mb=$(git-merge-base "$local" "$2") &&
            case "$2,$mb" in
            $local,*)
-               echo >&2 "* $1: same as $3"
+               if test -n "$verbose"
+               then
+                       echo >&2 "* $1: same as $3"
+               fi
                ;;
            *,$local)
                echo >&2 "* $1: fast forward to $3"
@@ -211,12 +214,12 @@ esac
 reflist=$(get_remote_refs_for_fetch "$@")
 if test "$tags"
 then
-       taglist=$(IFS=" " &&
+       taglist=`IFS="  " &&
                  git-ls-remote $upload_pack --tags "$remote" |
                  while read sha1 name
                  do
                        case "$name" in
-                       (*^*) continue ;;
+                       *^*) continue ;;
                        esac
                        if git-check-ref-format "$name"
                        then
@@ -224,7 +227,7 @@ then
                        else
                            echo >&2 "warning: tag ${name} ignored"
                        fi
-                 done)
+                 done`
        if test "$#" -gt 1
        then
                # remote URL plus explicit refspecs; we need to merge them.
@@ -270,14 +273,22 @@ fetch_main () {
          if [ -n "$GIT_SSL_NO_VERIFY" ]; then
              curl_extra_args="-k"
          fi
-         remote_name_quoted=$(perl -e '
+         max_depth=5
+         depth=0
+         head="ref: $remote_name"
+         while (expr "z$head" : "zref:" && expr $depth \< $max_depth) >/dev/null
+         do
+           remote_name_quoted=$(perl -e '
              my $u = $ARGV[0];
+              $u =~ s/^ref:\s*//;
              $u =~ s{([^-a-zA-Z0-9/.])}{sprintf"%%%02x",ord($1)}eg;
              print "$u";
-         ' "$remote_name")
-         head=$(curl -nsfL $curl_extra_args "$remote/$remote_name_quoted") &&
+         ' "$head")
+           head=$(curl -nsfL $curl_extra_args "$remote/$remote_name_quoted")
+           depth=$( expr \( $depth + 1 \) )
+         done
          expr "z$head" : "z$_x40\$" >/dev/null ||
-                 die "Failed to fetch $remote_name from $remote"
+             die "Failed to fetch $remote_name from $remote"
          echo >&2 Fetching "$remote_name from $remote" using http
          git-http-fetch -v -a "$head" "$remote/" || exit
          ;;
diff --git a/git-format-patch.sh b/git-format-patch.sh
deleted file mode 100755 (executable)
index c7133bc..0000000
+++ /dev/null
@@ -1,345 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) 2005 Junio C Hamano
-#
-
-USAGE='[-n | -k] [-o <dir> | --stdout] [--signoff] [--check] [--diff-options] [--attach] <his> [<mine>]'
-LONG_USAGE='Prepare each commit with its patch since <mine> head forked from
-<his> head, one file per patch formatted to resemble UNIX mailbox
-format, for e-mail submission or use with git-am.
-
-Each output file is numbered sequentially from 1, and uses the
-first line of the commit message (massaged for pathname safety)
-as the filename.
-
-When -o is specified, output files are created in <dir>; otherwise
-they are created in the current working directory.  This option
-is ignored if --stdout is specified.
-
-When -n is specified, instead of "[PATCH] Subject", the first
-line is formatted as "[PATCH N/M] Subject", unless you have only
-one patch.
-
-When --attach is specified, patches are attached, not inlined.'
-
-. git-sh-setup
-
-# Force diff to run in C locale.
-LANG=C LC_ALL=C
-export LANG LC_ALL
-
-diff_opts=
-LF='
-'
-
-outdir=./
-while case "$#" in 0) break;; esac
-do
-    case "$1" in
-    -c|--c|--ch|--che|--chec|--check)
-    check=t ;;
-    -a|--a|--au|--aut|--auth|--autho|--author|\
-    -d|--d|--da|--dat|--date|\
-    -m|--m|--mb|--mbo|--mbox) # now noop
-    ;;
-    --at|--att|--atta|--attac|--attach)
-    attach=t ;;
-    -k|--k|--ke|--kee|--keep|--keep-|--keep-s|--keep-su|--keep-sub|\
-    --keep-subj|--keep-subje|--keep-subjec|--keep-subject)
-    keep_subject=t ;;
-    -n|--n|--nu|--num|--numb|--numbe|--number|--numbere|--numbered)
-    numbered=t ;;
-    -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
-    signoff=t ;;
-    --st|--std|--stdo|--stdou|--stdout)
-    stdout=t ;;
-    -o=*|--o=*|--ou=*|--out=*|--outp=*|--outpu=*|--output=*|--output-=*|\
-    --output-d=*|--output-di=*|--output-dir=*|--output-dire=*|\
-    --output-direc=*|--output-direct=*|--output-directo=*|\
-    --output-director=*|--output-directory=*)
-    outdir=`expr "$1" : '-[^=]*=\(.*\)'` ;;
-    -o|--o|--ou|--out|--outp|--outpu|--output|--output-|--output-d|\
-    --output-di|--output-dir|--output-dire|--output-direc|--output-direct|\
-    --output-directo|--output-director|--output-directory)
-    case "$#" in 1) usage ;; esac; shift
-    outdir="$1" ;;
-    -h|--h|--he|--hel|--help)
-        usage
-       ;;
-    -*' '* | -*"$LF"* | -*'    '*)
-       # Ignore diff option that has whitespace for now.
-       ;;
-    -*)        diff_opts="$diff_opts$1 " ;;
-    *) break ;;
-    esac
-    shift
-done
-
-case "$keep_subject$numbered" in
-tt)
-       die '--keep-subject and --numbered are incompatible.' ;;
-esac
-
-tmp=.tmp-series$$
-trap 'rm -f $tmp-*' 0 1 2 3 15
-
-series=$tmp-series
-commsg=$tmp-commsg
-filelist=$tmp-files
-
-# Backward compatible argument parsing hack.
-#
-# Historically, we supported:
-# 1. "rev1"            is equivalent to "rev1..HEAD"
-# 2. "rev1..rev2"
-# 3. "rev1" "rev2      is equivalent to "rev1..rev2"
-#
-# We want to take a sequence of "rev1..rev2" in general.
-# Also, "rev1.." should mean "rev1..HEAD"; git-diff users are
-# familiar with that syntax.
-
-case "$#,$1$2" in
-1,?*..?*)
-       # single "rev1..rev2"
-       ;;
-1,?*..)
-       # single "rev1.." should mean "rev1..HEAD"
-       set x "$1"HEAD
-       shift
-       ;;
-1,*)
-       # single rev1
-       set x "$1..HEAD"
-       shift
-       ;;
-2,?*..?*)
-       # not traditional "rev1" "rev2"
-       ;;
-2,*)
-       set x "$1..$2"
-       shift
-       ;;
-esac
-
-# Now we have what we want in $@
-for revpair
-do
-       case "$revpair" in
-       ?*..?*)
-               rev1=`expr "z$revpair" : 'z\(.*\)\.\.'`
-               rev2=`expr "z$revpair" : 'z.*\.\.\(.*\)'`
-               ;;
-       *)
-               rev1="$revpair^"
-               rev2="$revpair"
-               ;;
-       esac
-       git-rev-parse --verify "$rev1^0" >/dev/null 2>&1 ||
-               die "Not a valid rev $rev1 ($revpair)"
-       git-rev-parse --verify "$rev2^0" >/dev/null 2>&1 ||
-               die "Not a valid rev $rev2 ($revpair)"
-       git-cherry -v "$rev1" "$rev2" |
-       while read sign rev comment
-       do
-               case "$sign" in
-               '-')
-                       echo >&2 "Merged already: $comment"
-                       ;;
-               *)
-                       echo $rev
-                       ;;
-               esac
-       done
-done >$series
-
-me=`git-var GIT_AUTHOR_IDENT | sed -e 's/>.*/>/'`
-headers=`git-repo-config --get format.headers`
-case "$attach" in
-"") ;;
-*)
-       mimemagic="050802040500080604070107"
-esac
-
-case "$outdir" in
-*/) ;;
-*) outdir="$outdir/" ;;
-esac
-test -d "$outdir" || mkdir -p "$outdir" || exit
-
-titleScript='
-       /./d
-       /^$/n
-       s/^\[PATCH[^]]*\] *//
-       s/[^-a-z.A-Z_0-9]/-/g
-        s/\.\.\.*/\./g
-       s/\.*$//
-       s/--*/-/g
-       s/^-//
-       s/-$//
-       s/$/./
-       p
-       q
-'
-
-process_one () {
-       perl -w -e '
-my ($keep_subject, $num, $signoff, $headers, $mimemagic, $commsg) = @ARGV;
-my ($signoff_pattern, $done_header, $done_subject, $done_separator, $signoff_seen,
-    $last_was_signoff);
-
-if ($signoff) {
-       $signoff = "Signed-off-by: " . `git-var GIT_COMMITTER_IDENT`;
-       $signoff =~ s/>.*/>/;
-       $signoff_pattern = quotemeta($signoff);
-}
-
-my @weekday_names = qw(Sun Mon Tue Wed Thu Fri Sat);
-my @month_names = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
-
-sub show_date {
-    my ($time, $tz) = @_;
-    my $minutes = abs($tz);
-    $minutes = int($minutes / 100) * 60 + ($minutes % 100);
-    if ($tz < 0) {
-        $minutes = -$minutes;
-    }
-    my $t = $time + $minutes * 60;
-    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($t);
-    return sprintf("%s %s %d %02d:%02d:%02d %d %+05d",
-                  $weekday_names[$wday],
-                  $month_names[$mon],
-                  $mday, $hour, $min, $sec,
-                  $year+1900, $tz);
-}
-
-print "From nobody Mon Sep 17 00:00:00 2001\n";
-open FH, "git stripspace <$commsg |" or die "open $commsg pipe";
-while (<FH>) {
-    unless ($done_header) {
-       if (/^$/) {
-           $done_header = 1;
-       }
-       elsif (/^author (.*>) (.*)$/) {
-           my ($author_ident, $author_date) = ($1, $2);
-           my ($utc, $off) = ($author_date =~ /^(\d+) ([-+]?\d+)$/);
-           $author_date = show_date($utc, $off);
-
-           print "From: $author_ident\n";
-           print "Date: $author_date\n";
-       }
-       next;
-    }
-    unless ($done_subject) {
-       unless ($keep_subject) {
-           s/^\[PATCH[^]]*\]\s*//;
-           s/^/[PATCH$num] /;
-       }
-       if ($headers) {
-           print "$headers\n";
-       }
-        print "Subject: $_";
-       if ($mimemagic) {
-           print "MIME-Version: 1.0\n";
-           print "Content-Type: multipart/mixed;\n";
-           print " boundary=\"------------$mimemagic\"\n";
-           print "\n";
-           print "This is a multi-part message in MIME format.\n";
-           print "--------------$mimemagic\n";
-           print "Content-Type: text/plain; charset=UTF-8; format=fixed\n";
-           print "Content-Transfer-Encoding: 8bit\n";
-       }
-       $done_subject = 1;
-       next;
-    }
-    unless ($done_separator) {
-        print "\n";
-        $done_separator = 1;
-        next if (/^$/);
-    }
-
-    $last_was_signoff = 0;
-    if (/Signed-off-by:/i) {
-        if ($signoff ne "" && /Signed-off-by:\s*$signoff_pattern$/i) {
-           $signoff_seen = 1;
-       }
-    }
-    print $_;
-}
-if (!$signoff_seen && $signoff ne "") {
-    if (!$last_was_signoff) {
-        print "\n";
-    }
-    print "$signoff\n";
-}
-print "\n---\n\n";
-close FH or die "close $commsg pipe";
-' "$keep_subject" "$num" "$signoff" "$headers" "$mimemagic" $commsg
-
-       git-diff-tree -p $diff_opts "$commit" | git-apply --stat --summary
-       echo
-       case "$mimemagic" in
-       '');;
-       *)
-               echo "--------------$mimemagic"
-               echo "Content-Type: text/x-patch;"
-               echo " name=\"$commit.diff\""
-               echo "Content-Transfer-Encoding: 8bit"
-               echo "Content-Disposition: inline;"
-               echo " filename=\"$commit.diff\""
-               echo
-       esac
-       git-diff-tree -p $diff_opts "$commit"
-       case "$mimemagic" in
-       '')
-               echo "-- "
-               echo "@@GIT_VERSION@@"
-               ;;
-       *)
-               echo
-               echo "--------------$mimemagic--"
-               echo
-               ;;
-       esac
-       echo
-}
-
-total=`wc -l <$series | tr -dc "[0-9]"`
-case "$total,$numbered" in
-1,*)
-       numfmt='' ;;
-*,t)
-       numfmt=`echo "$total" | wc -c`
-       numfmt=$(($numfmt-1))
-       numfmt=" %0${numfmt}d/$total"
-esac
-
-i=1
-while read commit
-do
-    git-cat-file commit "$commit" | git-stripspace >$commsg
-    title=`sed -ne "$titleScript" <$commsg`
-    case "$numbered" in
-    '') num= ;;
-    *)
-        num=`printf "$numfmt" $i` ;;
-    esac
-
-    file=`printf '%04d-%stxt' $i "$title"`
-    if test '' = "$stdout"
-    then
-           echo "$file"
-           process_one >"$outdir$file"
-           if test t = "$check"
-           then
-               # This is slightly modified from Andrew Morton's Perfect Patch.
-               # Lines you introduce should not have trailing whitespace.
-               # Also check for an indentation that has SP before a TAB.
-               grep -n '^+\([  ]*      .*\|.*[         ]\)$' "$outdir$file"
-               :
-           fi
-    else
-           echo >&2 "$file"
-           process_one
-    fi
-    i=`expr "$i" + 1`
-done <$series
diff --git a/git-grep.sh b/git-grep.sh
deleted file mode 100755 (executable)
index ad4f2fe..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) Linus Torvalds, 2005
-#
-
-USAGE='[<option>...] [-e] <pattern> [<path>...]'
-SUBDIRECTORY_OK='Yes'
-. git-sh-setup
-
-got_pattern () {
-       if [ -z "$no_more_patterns" ]
-       then
-               pattern="$1" no_more_patterns=yes
-       else
-               die "git-grep: do not specify more than one pattern"
-       fi
-}
-
-no_more_patterns=
-pattern=
-flags=()
-git_flags=()
-while : ; do
-       case "$1" in
-       -o|--cached|--deleted|--others|--killed|\
-       --ignored|--modified|--exclude=*|\
-       --exclude-from=*|\--exclude-per-directory=*)
-               git_flags=("${git_flags[@]}" "$1")
-               ;;
-       -e)
-               got_pattern "$2"
-               shift
-               ;;
-       -A|-B|-C|-D|-d|-f|-m)
-               flags=("${flags[@]}" "$1" "$2")
-               shift
-               ;;
-       --)
-               # The rest are git-ls-files paths
-               shift
-               break
-               ;;
-       -*)
-               flags=("${flags[@]}" "$1")
-               ;;
-       *)
-               if [ -z "$no_more_patterns" ]
-               then
-                       got_pattern "$1"
-                       shift
-               fi
-               [ "$1" = -- ] && shift
-               break
-               ;;
-       esac
-       shift
-done
-[ "$pattern" ] || {
-       usage
-}
-git-ls-files -z "${git_flags[@]}" -- "$@" |
-       xargs -0 grep "${flags[@]}" -e "$pattern" --
index b6882a9..2fdcaf7 100755 (executable)
@@ -58,11 +58,19 @@ http://* | https://* )
        ;;
 
 rsync://* )
-       mkdir $tmpdir
+       mkdir $tmpdir &&
+       rsync -rlq "$peek_repo/HEAD" $tmpdir &&
        rsync -rq "$peek_repo/refs" $tmpdir || {
                echo "failed    slurping"
                exit
        }
+       head=$(cat "$tmpdir/HEAD") &&
+       case "$head" in
+       ref:' '*)
+               head=$(expr "z$head" : 'zref: \(.*\)') &&
+               head=$(cat "$tmpdir/$head") || exit
+       esac &&
+       echo "$head     HEAD"
        (cd $tmpdir && find refs -type f) |
        while read path
        do
index b834e79..af1f25b 100755 (executable)
@@ -55,8 +55,7 @@ finish () {
 
        case "$no_summary" in
        '')
-               git-diff-tree -p -M "$head" "$1" |
-               git-apply --stat --summary
+               git-diff-tree -p --stat --summary -M "$head" "$1"
                ;;
        esac
 }
diff --git a/git-p4import.py b/git-p4import.py
new file mode 100644 (file)
index 0000000..908941d
--- /dev/null
@@ -0,0 +1,361 @@
+#!/usr/bin/python
+#
+# This tool is copyright (c) 2006, Sean Estabrooks.
+# It is released under the Gnu Public License, version 2.
+#
+# Import Perforce branches into Git repositories.
+# Checking out the files is done by calling the standard p4
+# client which you must have properly configured yourself
+#
+
+import marshal
+import os
+import sys
+import time
+import getopt
+
+from signal import signal, \
+   SIGPIPE, SIGINT, SIG_DFL, \
+   default_int_handler
+
+signal(SIGPIPE, SIG_DFL)
+s = signal(SIGINT, SIG_DFL)
+if s != default_int_handler:
+   signal(SIGINT, s)
+
+def die(msg, *args):
+    for a in args:
+        msg = "%s %s" % (msg, a)
+    print "git-p4import fatal error:", msg
+    sys.exit(1)
+
+def usage():
+    print "USAGE: git-p4import [-q|-v]  [--authors=<file>]  [-t <timezone>]  [//p4repo/path <branch>]"
+    sys.exit(1)
+
+verbosity = 1
+logfile = "/dev/null"
+ignore_warnings = False
+stitch = 0
+tagall = True
+
+def report(level, msg, *args):
+    global verbosity
+    global logfile
+    for a in args:
+        msg = "%s %s" % (msg, a)
+    fd = open(logfile, "a")
+    fd.writelines(msg)
+    fd.close()
+    if level <= verbosity:
+        print msg
+
+class p4_command:
+    def __init__(self, _repopath):
+        try:
+            global logfile
+            self.userlist = {}
+            if _repopath[-1] == '/':
+                self.repopath = _repopath[:-1]
+            else:
+                self.repopath = _repopath
+            if self.repopath[-4:] != "/...":
+                self.repopath= "%s/..." % self.repopath
+            f=os.popen('p4 -V 2>>%s'%logfile, 'rb')
+            a = f.readlines()
+            if f.close():
+                raise
+        except:
+                die("Could not find the \"p4\" command")
+
+    def p4(self, cmd, *args):
+        global logfile
+        cmd = "%s %s" % (cmd, ' '.join(args))
+        report(2, "P4:", cmd)
+        f=os.popen('p4 -G %s 2>>%s' % (cmd,logfile), 'rb')
+        list = []
+        while 1:
+           try:
+                list.append(marshal.load(f))
+           except EOFError:
+                break
+        self.ret = f.close()
+        return list
+
+    def sync(self, id, force=False, trick=False, test=False):
+        if force:
+            ret = self.p4("sync -f %s@%s"%(self.repopath, id))[0]
+        elif trick:
+            ret = self.p4("sync -k %s@%s"%(self.repopath, id))[0]
+        elif test:
+            ret = self.p4("sync -n %s@%s"%(self.repopath, id))[0]
+        else:
+            ret = self.p4("sync    %s@%s"%(self.repopath, id))[0]
+        if ret['code'] == "error":
+             data = ret['data'].upper()
+             if data.find('VIEW') > 0:
+                 die("Perforce reports %s is not in client view"% self.repopath)
+             elif data.find('UP-TO-DATE') < 0:
+                 die("Could not sync files from perforce", self.repopath)
+
+    def changes(self, since=0):
+        try:
+            list = []
+            for rec in self.p4("changes %s@%s,#head" % (self.repopath, since+1)):
+                list.append(rec['change'])
+            list.reverse()
+            return list
+        except:
+            return []
+
+    def authors(self, filename):
+        f=open(filename)
+        for l in f.readlines():
+            self.userlist[l[:l.find('=')].rstrip()] = \
+                    (l[l.find('=')+1:l.find('<')].rstrip(),l[l.find('<')+1:l.find('>')])
+        f.close()
+        for f,e in self.userlist.items():
+                report(2, f, ":", e[0], "  <", e[1], ">")
+
+    def _get_user(self, id):
+        if not self.userlist.has_key(id):
+            try:
+                user = self.p4("users", id)[0]
+                self.userlist[id] = (user['FullName'], user['Email'])
+            except:
+                self.userlist[id] = (id, "")
+        return self.userlist[id]
+
+    def _format_date(self, ticks):
+        symbol='+'
+        name = time.tzname[0]
+        offset = time.timezone
+        if ticks[8]:
+            name = time.tzname[1]
+            offset = time.altzone
+        if offset < 0:
+            offset *= -1
+            symbol = '-'
+        localo = "%s%02d%02d %s" % (symbol, offset / 3600, offset % 3600, name)
+        tickso = time.strftime("%a %b %d %H:%M:%S %Y", ticks)
+        return "%s %s" % (tickso, localo)
+
+    def where(self):
+        try:
+            return self.p4("where %s" % self.repopath)[-1]['path']
+        except:
+            return ""
+
+    def describe(self, num):
+        desc = self.p4("describe -s", num)[0]
+        self.msg = desc['desc']
+        self.author, self.email = self._get_user(desc['user'])
+        self.date = self._format_date(time.localtime(long(desc['time'])))
+        return self
+
+class git_command:
+    def __init__(self):
+        try:
+            self.version = self.git("--version")[0][12:].rstrip()
+        except:
+            die("Could not find the \"git\" command")
+        try:
+            self.gitdir = self.get_single("rev-parse --git-dir")
+            report(2, "gdir:", self.gitdir)
+        except:
+            die("Not a git repository... did you forget to \"git init-db\" ?")
+        try:
+            self.cdup = self.get_single("rev-parse --show-cdup")
+            if self.cdup != "":
+                os.chdir(self.cdup)
+            self.topdir = os.getcwd()
+            report(2, "topdir:", self.topdir)
+        except:
+            die("Could not find top git directory")
+
+    def git(self, cmd):
+        global logfile
+        report(2, "GIT:", cmd)
+        f=os.popen('git %s 2>>%s' % (cmd,logfile), 'rb')
+        r=f.readlines()
+        self.ret = f.close()
+        return r
+
+    def get_single(self, cmd):
+        return self.git(cmd)[0].rstrip()
+
+    def current_branch(self):
+        try:
+            testit = self.git("rev-parse --verify HEAD")[0]
+            return self.git("symbolic-ref HEAD")[0][11:].rstrip()
+        except:
+            return None
+
+    def get_config(self, variable):
+        try:
+            return self.git("repo-config --get %s" % variable)[0].rstrip()
+        except:
+            return None
+
+    def set_config(self, variable, value):
+        try:
+            self.git("repo-config %s %s"%(variable, value) )
+        except:
+            die("Could not set %s to " % variable, value)
+
+    def make_tag(self, name, head):
+        self.git("tag -f %s %s"%(name,head))
+
+    def top_change(self, branch):
+        try:
+            a=self.get_single("name-rev --tags refs/heads/%s" % branch)
+            loc = a.find(' tags/') + 6
+            if a[loc:loc+3] != "p4/":
+                raise
+            return int(a[loc+3:][:-2])
+        except:
+            return 0
+
+    def update_index(self):
+        self.git("ls-files -m -d -o -z | git update-index --add --remove -z --stdin")
+
+    def checkout(self, branch):
+        self.git("checkout %s" % branch)
+
+    def repoint_head(self, branch):
+        self.git("symbolic-ref HEAD refs/heads/%s" % branch)
+
+    def remove_files(self):
+        self.git("ls-files | xargs rm")
+
+    def clean_directories(self):
+        self.git("clean -d")
+
+    def fresh_branch(self, branch):
+        report(1, "Creating new branch", branch)
+        self.git("ls-files | xargs rm")
+        os.remove(".git/index")
+        self.repoint_head(branch)
+        self.git("clean -d")
+
+    def basedir(self):
+        return self.topdir
+
+    def commit(self, author, email, date, msg, id):
+        self.update_index()
+        fd=open(".msg", "w")
+        fd.writelines(msg)
+        fd.close()
+        try:
+                current = self.get_single("rev-parse --verify HEAD")
+                head = "-p HEAD"
+        except:
+                current = ""
+                head = ""
+        tree = self.get_single("write-tree")
+        for r,l in [('DATE',date),('NAME',author),('EMAIL',email)]:
+            os.environ['GIT_AUTHOR_%s'%r] = l
+            os.environ['GIT_COMMITTER_%s'%r] = l
+        commit = self.get_single("commit-tree %s %s < .msg" % (tree,head))
+        os.remove(".msg")
+        self.make_tag("p4/%s"%id, commit)
+        self.git("update-ref HEAD %s %s" % (commit, current) )
+
+try:
+    opts, args = getopt.getopt(sys.argv[1:], "qhvt:",
+            ["authors=","help","stitch=","timezone=","log=","ignore","notags"])
+except getopt.GetoptError:
+    usage()
+
+for o, a in opts:
+    if o == "-q":
+        verbosity = 0
+    if o == "-v":
+        verbosity += 1
+    if o in ("--log"):
+        logfile = a
+    if o in ("--notags"):
+        tagall = False
+    if o in ("-h", "--help"):
+        usage()
+    if o in ("--ignore"):
+        ignore_warnings = True
+
+git = git_command()
+branch=git.current_branch()
+
+for o, a in opts:
+    if o in ("-t", "--timezone"):
+        git.set_config("perforce.timezone", a)
+    if o in ("--stitch"):
+        git.set_config("perforce.%s.path" % branch, a)
+        stitch = 1
+
+if len(args) == 2:
+    branch = args[1]
+    git.checkout(branch)
+    if branch == git.current_branch():
+        die("Branch %s already exists!" % branch)
+    report(1, "Setting perforce to ", args[0])
+    git.set_config("perforce.%s.path" % branch, args[0])
+elif len(args) != 0:
+    die("You must specify the perforce //depot/path and git branch")
+
+p4path = git.get_config("perforce.%s.path" % branch)
+if p4path == None:
+    die("Do not know Perforce //depot/path for git branch", branch)
+
+p4 = p4_command(p4path)
+
+for o, a in opts:
+    if o in ("-a", "--authors"):
+        p4.authors(a)
+
+localdir = git.basedir()
+if p4.where()[:len(localdir)] != localdir:
+    report(1, "**WARNING** Appears p4 client is misconfigured")
+    report(1, "   for sync from %s to %s" % (p4.repopath, localdir))
+    if ignore_warnings != True:
+        die("Reconfigure or use \"--ignore\" on command line")
+
+if stitch == 0:
+    top = git.top_change(branch)
+else:
+    top = 0
+changes = p4.changes(top)
+count = len(changes)
+if count == 0:
+    report(1, "Already up to date...")
+    sys.exit(0)
+
+ptz = git.get_config("perforce.timezone")
+if ptz:
+    report(1, "Setting timezone to", ptz)
+    os.environ['TZ'] = ptz
+    time.tzset()
+
+if stitch == 1:
+    git.remove_files()
+    git.clean_directories()
+    p4.sync(changes[0], force=True)
+elif top == 0 and branch != git.current_branch():
+    p4.sync(changes[0], test=True)
+    report(1, "Creating new initial commit");
+    git.fresh_branch(branch)
+    p4.sync(changes[0], force=True)
+else:
+    p4.sync(changes[0], trick=True)
+
+report(1, "processing %s changes from p4 (%s) to git (%s)" % (count, p4.repopath, branch))
+for id in changes:
+    report(1, "Importing changeset", id)
+    change = p4.describe(id)
+    p4.sync(id)
+    if tagall :
+            git.commit(change.author, change.email, change.date, change.msg, id)
+    else:
+            git.commit(change.author, change.email, change.date, change.msg, "import")
+    if stitch == 1:
+        git.clean_directories()
+        stitch = 0
+
index c9b899e..187f088 100755 (executable)
@@ -10,7 +10,10 @@ get_data_source () {
                # Not so fast.  This could be the partial URL shorthand...
                token=$(expr "z$1" : 'z\([^/]*\)/')
                remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
-               if test -f "$GIT_DIR/branches/$token"
+               if test "$(git-repo-config --get "remote.$token.url")"
+               then
+                       echo config-partial
+               elif test -f "$GIT_DIR/branches/$token"
                then
                        echo branches-partial
                else
@@ -18,7 +21,10 @@ get_data_source () {
                fi
                ;;
        *)
-               if test -f "$GIT_DIR/remotes/$1"
+               if test "$(git-repo-config --get "remote.$1.url")"
+               then
+                       echo config
+               elif test -f "$GIT_DIR/remotes/$1"
                then
                        echo remotes
                elif test -f "$GIT_DIR/branches/$1"
@@ -35,6 +41,15 @@ get_remote_url () {
        case "$data_source" in
        '')
                echo "$1" ;;
+       config-partial)
+               token=$(expr "z$1" : 'z\([^/]*\)/')
+               remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
+               url=$(git-repo-config --get "remote.$token.url")
+               echo "$url/$remainder"
+               ;;
+       config)
+               git-repo-config --get "remote.$1.url"
+               ;;
        remotes)
                sed -ne '/^URL: */{
                        s///p
@@ -56,8 +71,10 @@ get_remote_url () {
 get_remote_default_refs_for_push () {
        data_source=$(get_data_source "$1")
        case "$data_source" in
-       '' | branches | branches-partial)
+       '' | config-partial | branches | branches-partial)
                ;; # no default push mapping, just send matching refs.
+       config)
+               git-repo-config --get-all "remote.$1.push" ;;
        remotes)
                sed -ne '/^Push: */{
                        s///p
@@ -111,8 +128,11 @@ canon_refs_list_for_fetch () {
 get_remote_default_refs_for_fetch () {
        data_source=$(get_data_source "$1")
        case "$data_source" in
-       '' | branches-partial)
+       '' | config-partial | branches-partial)
                echo "HEAD:" ;;
+       config)
+               canon_refs_list_for_fetch \
+                       $(git-repo-config --get-all "remote.$1.fetch") ;;
        branches)
                remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
                case "$remote_branch" in '') remote_branch=master ;; esac
diff --git a/git-quiltimport.sh b/git-quiltimport.sh
new file mode 100755 (executable)
index 0000000..12d9d0c
--- /dev/null
@@ -0,0 +1,118 @@
+#!/bin/sh
+USAGE='--dry-run --author <author> --patches </path/to/quilt/patch/directory>'
+SUBDIRECTORY_ON=Yes
+. git-sh-setup
+
+dry_run=""
+quilt_author=""
+while case "$#" in 0) break;; esac
+do
+       case "$1" in
+       --au=*|--aut=*|--auth=*|--autho=*|--author=*)
+               quilt_author=$(expr "$1" : '-[^=]*\(.*\)')
+               shift
+               ;;
+
+       --au|--aut|--auth|--autho|--author)
+               case "$#" in 1) usage ;; esac
+               shift
+               quilt_author="$1"
+               shift
+               ;;
+
+       --dry-run)
+               shift
+               dry_run=1
+               ;;
+
+       --pa=*|--pat=*|--patc=*|--patch=*|--patche=*|--patches=*)
+               QUILT_PATCHES=$(expr "$1" : '-[^=]*\(.*\)')
+               shift
+               ;;
+
+       --pa|--pat|--patc|--patch|--patche|--patches)
+               case "$#" in 1) usage ;; esac
+               shift
+               QUILT_PATCHES="$1"
+               shift
+               ;;
+
+       *)
+               break
+               ;;
+       esac
+done
+
+# Quilt Author
+if [ -n "$quilt_author" ] ; then
+       quilt_author_name=$(expr "z$quilt_author" : 'z\(.*[^ ]\) *<.*') &&
+       quilt_author_email=$(expr "z$quilt_author" : '.*<\([^>]*\)') &&
+       test '' != "$quilt_author_name" &&
+       test '' != "$quilt_author_email" ||
+       die "malformatted --author parameter"
+fi
+
+# Quilt patch directory
+: ${QUILT_PATCHES:=patches}
+if ! [ -d "$QUILT_PATCHES" ] ; then
+       echo "The \"$QUILT_PATCHES\" directory does not exist."
+       exit 1
+fi
+
+# Temporay directories
+tmp_dir=.dotest
+tmp_msg="$tmp_dir/msg"
+tmp_patch="$tmp_dir/patch"
+tmp_info="$tmp_dir/info"
+
+
+# Find the intial commit
+commit=$(git-rev-parse HEAD)
+
+mkdir $tmp_dir || exit 2
+for patch_name in $(cat "$QUILT_PATCHES/series" | grep -v '^#'); do
+       echo $patch_name
+       (cat $QUILT_PATCHES/$patch_name | git-mailinfo "$tmp_msg" "$tmp_patch" > "$tmp_info") || exit 3
+
+       # Parse the author information
+       export GIT_AUTHOR_NAME=$(sed -ne 's/Author: //p' "$tmp_info")
+       export GIT_AUTHOR_EMAIL=$(sed -ne 's/Email: //p' "$tmp_info")
+       while test -z "$GIT_AUTHOR_EMAIL" && test -z "$GIT_AUTHOR_NAME" ; do
+               if [ -n "$quilt_author" ] ; then
+                       GIT_AUTHOR_NAME="$quilt_author_name";
+                       GIT_AUTHOR_EMAIL="$quilt_author_email";
+               elif [ -n "$dry_run" ]; then
+                       echo "No author found in $patch_name" >&2;
+                       GIT_AUTHOR_NAME="dry-run-not-found";
+                       GIT_AUTHOR_EMAIL="dry-run-not-found";
+               else
+                       echo "No author found in $patch_name" >&2;
+                       echo "---"
+                       cat $tmp_msg
+                       echo -n "Author: ";
+                       read patch_author
+
+                       echo "$patch_author"
+
+                       patch_author_name=$(expr "z$patch_author" : 'z\(.*[^ ]\) *<.*') &&
+                       patch_author_email=$(expr "z$patch_author" : '.*<\([^>]*\)') &&
+                       test '' != "$patch_author_name" &&
+                       test '' != "$patch_author_email" &&
+                       GIT_AUTHOR_NAME="$patch_author_name" &&
+                       GIT_AUTHOR_EMAIL="$patch_author_email"
+               fi
+       done
+       export GIT_AUTHOR_DATE=$(sed -ne 's/Date: //p' "$tmp_info")
+       export SUBJECT=$(sed -ne 's/Subject: //p' "$tmp_info")
+       if [ -z "$SUBJECT" ] ; then
+               SUBJECT=$(echo $patch_name | sed -e 's/.patch$//')
+       fi
+
+       if [ -z "$dry_run" ] ; then
+               git-apply --index -C1 "$tmp_patch" &&
+               tree=$(git-write-tree) &&
+               commit=$((echo "$SUBJECT"; echo; cat "$tmp_msg") | git-commit-tree $tree -p $commit) &&
+               git-update-ref HEAD $commit || exit 4
+       fi
+done
+rm -rf $tmp_dir || exit 5
index f7b2b94..e6b57b8 100755 (executable)
@@ -4,37 +4,61 @@
 #
 
 USAGE='[--onto <newbase>] <upstream> [<branch>]'
-LONG_USAGE='git-rebase applies to <upstream> (or optionally to <newbase>) commits
-from <branch> that do not appear in <upstream>. When <branch> is not
-specified it defaults to the current branch (HEAD).
-
-When git-rebase is complete, <branch> will be updated to point to the
-newly created line of commit objects, so the previous line will not be
-accessible unless there are other references to it already.
-
-Assuming the following history:
-
-          A---B---C topic
-         /
-    D---E---F---G master
-
-The result of the following command:
-
-    git-rebase --onto master~1 master topic
-
-  would be:
-
-              A'\''--B'\''--C'\'' topic
-             /
-    D---E---F---G master
+LONG_USAGE='git-rebase replaces <branch> with a new branch of the
+same name.  When the --onto option is provided the new branch starts
+out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
+It then attempts to create a new commit for each commit from the original
+<branch> that does not exist in the <upstream> branch.
+
+It is possible that a merge failure will prevent this process from being
+completely automatic.  You will have to resolve any such merge failure
+and run git rebase --continue.  Another option is to bypass the commit
+that caused the merge failure with git rebase --skip.  To restore the
+original <branch> and remove the .dotest working files, use the command
+git rebase --abort instead.
+
+Note that if <branch> is not specified on the command line, the
+currently checked out branch is used.  You must be in the top
+directory of your project to start (or continue) a rebase.
+
+Example:       git-rebase master~1 topic
+
+        A---B---C topic                   A'\''--B'\''--C'\'' topic
+       /                   -->           /
+  D---E---F---G master          D---E---F---G master
 '
-
 . git-sh-setup
 
+RESOLVEMSG="
+When you have resolved this problem run \"git rebase --continue\".
+If you would prefer to skip this patch, instead run \"git rebase --skip\".
+To restore the original branch and stop rebasing run \"git rebase --abort\".
+"
 unset newbase
 while case "$#" in 0) break ;; esac
 do
        case "$1" in
+       --continue)
+               diff=$(git-diff-files)
+               case "$diff" in
+               ?*)     echo "You must edit all merge conflicts and then"
+                       echo "mark them as resolved using git update-index"
+                       exit 1
+                       ;;
+               esac
+               git am --resolved --3way --resolvemsg="$RESOLVEMSG"
+               exit
+               ;;
+       --skip)
+               git am -3 --skip --resolvemsg="$RESOLVEMSG"
+               exit
+               ;;
+       --abort)
+               [ -d .dotest ] || die "No rebase in progress?"
+               git reset --hard ORIG_HEAD
+               rm -r .dotest
+               exit
+               ;;
        --onto)
                test 2 -le "$#" || usage
                newbase="$2"
@@ -128,5 +152,6 @@ then
        exit 0
 fi
 
-git-format-patch -k --stdout --full-index "$upstream" ORIG_HEAD |
-git am --binary -3 -k
+git-format-patch -k --stdout --full-index "$upstream"..ORIG_HEAD |
+git am --binary -3 -k --resolvemsg="$RESOLVEMSG"
+
index e0c9f32..4fb3f26 100755 (executable)
@@ -48,15 +48,15 @@ name=$(git-rev-list --objects --all $rev_list 2>&1 |
        exit 1
 if [ -z "$name" ]; then
        echo Nothing new to pack.
-       exit 0
-fi
-echo "Pack pack-$name created."
+else
+       echo "Pack pack-$name created."
 
-mkdir -p "$PACKDIR" || exit
+       mkdir -p "$PACKDIR" || exit
 
-mv .tmp-pack-$name.pack "$PACKDIR/pack-$name.pack" &&
-mv .tmp-pack-$name.idx  "$PACKDIR/pack-$name.idx" ||
-exit
+       mv .tmp-pack-$name.pack "$PACKDIR/pack-$name.pack" &&
+       mv .tmp-pack-$name.idx  "$PACKDIR/pack-$name.idx" ||
+       exit
+fi
 
 if test "$remove_redundant" = t
 then
index 2c48bfb..4319e35 100755 (executable)
@@ -30,4 +30,4 @@ echo "  $url"
 echo
 
 git log  $baserev..$headrev | git-shortlog ;
-git diff $baserev..$headrev | git-apply --stat --summary
+git diff --stat --summary $baserev..$headrev
index 6cb073c..296f3b7 100755 (executable)
@@ -6,6 +6,7 @@ USAGE='[--mixed | --soft | --hard]  [<commit-ish>]'
 tmp=${GIT_DIR}/reset.$$
 trap 'rm -f $tmp-*' 0 1 2 3 15
 
+update=
 reset_type=--mixed
 case "$1" in
 --mixed | --soft | --hard)
@@ -23,24 +24,7 @@ rev=$(git-rev-parse --verify $rev^0) || exit
 # behind before a hard reset, so that we can remove them.
 if test "$reset_type" = "--hard"
 then
-       {
-               git-ls-files --stage -z
-               git-rev-parse --verify HEAD 2>/dev/null &&
-               git-ls-tree -r -z HEAD
-       } | perl -e '
-           use strict;
-           my %seen;
-           $/ = "\0";
-           while (<>) {
-               chomp;
-               my ($info, $path) = split(/\t/, $_);
-               next if ($info =~ / tree /);
-               if (!$seen{$path}) {
-                       $seen{$path} = 1;
-                       print "$path\0";
-               }
-           }
-       ' >$tmp-exists
+       update=-u
 fi
 
 # Soft reset does not touch the index file nor the working tree
@@ -54,7 +38,7 @@ then
                die "Cannot do a soft reset in the middle of a merge."
        fi
 else
-       git-read-tree --reset "$rev" || exit
+       git-read-tree --reset $update "$rev" || exit
 fi
 
 # Any resets update HEAD to the head being switched to.
@@ -64,37 +48,11 @@ then
 else
        rm -f "$GIT_DIR/ORIG_HEAD"
 fi
-git-update-ref HEAD "$rev"
+git-update-ref -m "reset $reset_type $@" HEAD "$rev"
 
 case "$reset_type" in
 --hard )
-       # Hard reset matches the working tree to that of the tree
-       # being switched to.
-       git-checkout-index -f -u -q -a
-       git-ls-files --cached -z |
-       perl -e '
-               use strict;
-               my (%keep, $fh);
-               $/ = "\0";
-               while (<STDIN>) {
-                       chomp;
-                       $keep{$_} = 1;
-               }
-               open $fh, "<", $ARGV[0]
-                       or die "cannot open $ARGV[0]";
-               while (<$fh>) {
-                       chomp;
-                       if (! exists $keep{$_}) {
-                               # it is ok if this fails -- it may already
-                               # have been culled by checkout-index.
-                               unlink $_;
-                               while (s|/[^/]*$||) {
-                                       rmdir($_) or last;
-                               }
-                       }
-               }
-       ' $tmp-exists
-       ;;
+       ;; # Nothing else to do
 --soft )
        ;; # Nothing else to do
 --mixed )
index c19d3a6..de8b5f0 100755 (executable)
@@ -137,7 +137,7 @@ esac >.msg
 # $prev and $commit on top of us (when cherry-picking or replaying).
 
 echo >&2 "First trying simple merge strategy to $me."
-git-read-tree -m -u $base $head $next &&
+git-read-tree -m -u --aggressive $base $head $next &&
 result=$(git-write-tree 2>/dev/null) || {
     echo >&2 "Simple $me fails; trying Automatic $me."
     git-merge-index -o git-merge-one-file -a || {
diff --git a/git-rm.sh b/git-rm.sh
deleted file mode 100755 (executable)
index fda4541..0000000
--- a/git-rm.sh
+++ /dev/null
@@ -1,70 +0,0 @@
-#!/bin/sh
-
-USAGE='[-f] [-n] [-v] [--] <file>...'
-SUBDIRECTORY_OK='Yes'
-. git-sh-setup
-
-remove_files=
-show_only=
-verbose=
-while : ; do
-  case "$1" in
-    -f)
-       remove_files=true
-       ;;
-    -n)
-       show_only=true
-       ;;
-    -v)
-       verbose=--verbose
-       ;;
-    --)
-       shift; break
-       ;;
-    -*)
-       usage
-       ;;
-    *)
-       break
-       ;;
-  esac
-  shift
-done
-
-# This is typo-proofing. If some paths match and some do not, we want
-# to do nothing.
-case "$#" in
-0)     ;;
-*)
-       git-ls-files --error-unmatch -- "$@" >/dev/null || {
-               echo >&2 "Maybe you misspelled it?"
-               exit 1
-       }
-       ;;
-esac
-
-if test -f "$GIT_DIR/info/exclude"
-then
-       git-ls-files -z \
-       --exclude-from="$GIT_DIR/info/exclude" \
-       --exclude-per-directory=.gitignore -- "$@"
-else
-       git-ls-files -z \
-       --exclude-per-directory=.gitignore -- "$@"
-fi |
-case "$show_only,$remove_files" in
-true,*)
-       xargs -0 echo
-       ;;
-*,true)
-       xargs -0 sh -c "
-               while [ \$# -gt 0 ]; do
-                       file=\$1; shift
-                       rm -- \"\$file\" && git-update-index --remove $verbose \"\$file\"
-               done
-       " inline
-       ;;
-*)
-       git-update-index --force-remove $verbose -z --stdin
-       ;;
-esac
index ecfa347..7b1cca7 100755 (executable)
@@ -21,7 +21,6 @@ use warnings;
 use Term::ReadLine;
 use Getopt::Long;
 use Data::Dumper;
-use Net::SMTP;
 
 # most mail servers generate the Date: header, but not all...
 $ENV{LC_ALL} = 'C';
@@ -37,10 +36,12 @@ sub cleanup_compose_files();
 my $compose_filename = ".msg.$$";
 
 # Variables we fill in automatically, or via prompting:
-my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose,$time);
+my (@to,@cc,@initial_cc,@bcclist,
+       $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
 
 # Behavior modification variables
-my ($chain_reply_to, $smtp_server, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0);
+my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
+my $smtp_server;
 
 # Example reply to:
 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
@@ -55,6 +56,7 @@ my $rc = GetOptions("from=s" => \$from,
                    "subject=s" => \$initial_subject,
                    "to=s" => \@to,
                    "cc=s" => \@initial_cc,
+                   "bcc=s" => \@bcclist,
                    "chain-reply-to!" => \$chain_reply_to,
                    "smtp-server=s" => \$smtp_server,
                    "compose" => \$compose,
@@ -89,6 +91,41 @@ sub gitvar_ident {
 my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
 my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
 
+my %aliases;
+chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
+chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
+my %parse_alias = (
+       # multiline formats can be supported in the future
+       mutt => sub { my $fh = shift; while (<$fh>) {
+               if (/^alias\s+(\S+)\s+(.*)$/) {
+                       my ($alias, $addr) = ($1, $2);
+                       $addr =~ s/#.*$//; # mutt allows # comments
+                        # commas delimit multiple addresses
+                       $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
+               }}},
+       mailrc => sub { my $fh = shift; while (<$fh>) {
+               if (/^alias\s+(\S+)\s+(.*)$/) {
+                       # spaces delimit multiple addresses
+                       $aliases{$1} = [ split(/\s+/, $2) ];
+               }}},
+       pine => sub { my $fh = shift; while (<$fh>) {
+               if (/^(\S+)\s+(.*)$/) {
+                       $aliases{$1} = [ split(/\s*,\s*/, $2) ];
+               }}},
+       gnus => sub { my $fh = shift; while (<$fh>) {
+               if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
+                       $aliases{$1} = [ $2 ];
+               }}}
+);
+
+if (@alias_files && defined $parse_alias{$aliasfiletype}) {
+       foreach my $file (@alias_files) {
+               open my $fh, '<', $file or die "opening $file: $!\n";
+               $parse_alias{$aliasfiletype}->($fh);
+               close $fh;
+       }
+}
+
 my $prompting = 0;
 if (!defined $from) {
        $from = $author || $committer;
@@ -112,6 +149,20 @@ if (!@to) {
        $prompting++;
 }
 
+sub expand_aliases {
+       my @cur = @_;
+       my @last;
+       do {
+               @last = @cur;
+               @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
+       } while (join(',',@cur) ne join(',',@last));
+       return @cur;
+}
+
+@to = expand_aliases(@to);
+@initial_cc = expand_aliases(@initial_cc);
+@bcclist = expand_aliases(@bcclist);
+
 if (!defined $initial_subject && $compose) {
        do {
                $_ = $term->readline("What subject should the emails start with? ",
@@ -131,8 +182,14 @@ if (!defined $initial_reply_to && $prompting) {
        $initial_reply_to =~ s/(^\s+|\s+$)//g;
 }
 
-if (!defined $smtp_server) {
-       $smtp_server = "localhost";
+if (!$smtp_server) {
+       foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
+               if (-x $_) {
+                       $smtp_server = $_;
+                       last;
+               }
+       }
+       $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
 }
 
 if ($compose) {
@@ -214,6 +271,9 @@ Options:
    --cc           Specify an initial "Cc:" list for the entire series
                   of emails.
 
+   --bcc          Specify a list of email addresses that should be Bcc:
+                 on all the emails.
+
    --compose      Use \$EDITOR to edit an introductory message for the
                   patch series.
 
@@ -248,16 +308,23 @@ EOT
 }
 
 # Variables we set as part of the loop over files
-our ($message_id, $cc, %mail, $subject, $reply_to, $message);
+our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
 
 sub extract_valid_address {
        my $address = shift;
+       my $local_part_regexp = '[^<>"\s@]+';
+       my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
+
+       # check for a local address:
+       return $address if ($address =~ /^($local_part_regexp)$/);
+
        if ($have_email_valid) {
-               return Email::Valid->address($address);
+               return scalar Email::Valid->address($address);
        } else {
                # less robust/correct than the monster regexp in Email::Valid,
                # but still does a 99% job, and one less dependency
-               return ($address =~ /([^\"<>\s]+@[^<>\s]+)/);
+               $address =~ /($local_part_regexp\@$domain_regexp)/;
+               return $1;
        }
 }
 
@@ -289,8 +356,15 @@ sub send_message
 {
        my @recipients = unique_email_list(@to);
        my $to = join (",\n\t", @recipients);
-       @recipients = unique_email_list(@recipients,@cc);
+       @recipients = unique_email_list(@recipients,@cc,@bcclist);
        my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime($time++));
+       my $gitversion = '@@GIT_VERSION@@';
+       if ($gitversion =~ m/..GIT_VERSION../) {
+           $gitversion = `git --version`;
+           chomp $gitversion;
+           # keep only what's after the last space
+           $gitversion =~ s/^.* //;
+       }
 
        my $header = "From: $from
 To: $to
@@ -299,34 +373,55 @@ Subject: $subject
 Reply-To: $from
 Date: $date
 Message-Id: $message_id
-X-Mailer: git-send-email @@GIT_VERSION@@
+X-Mailer: git-send-email $gitversion
 ";
-       $header .= "In-Reply-To: $reply_to\n" if $reply_to;
+       if ($reply_to) {
 
-       $smtp ||= Net::SMTP->new( $smtp_server );
-       $smtp->mail( $from ) or die $smtp->message;
-       $smtp->to( @recipients ) or die $smtp->message;
-       $smtp->data or die $smtp->message;
-       $smtp->datasend("$header\n$message") or die $smtp->message;
-       $smtp->dataend() or die $smtp->message;
-       $smtp->ok or die "Failed to send $subject\n".$smtp->message;
+               $header .= "In-Reply-To: $reply_to\n";
+               $header .= "References: $references\n";
+       }
 
+       if ($smtp_server =~ m#^/#) {
+               my $pid = open my $sm, '|-';
+               defined $pid or die $!;
+               if (!$pid) {
+                       exec($smtp_server,'-i',
+                            map { extract_valid_address($_) }
+                            @recipients) or die $!;
+               }
+               print $sm "$header\n$message";
+               close $sm or die $?;
+       } else {
+               require Net::SMTP;
+               $smtp ||= Net::SMTP->new( $smtp_server );
+               $smtp->mail( $from ) or die $smtp->message;
+               $smtp->to( @recipients ) or die $smtp->message;
+               $smtp->data or die $smtp->message;
+               $smtp->datasend("$header\n$message") or die $smtp->message;
+               $smtp->dataend() or die $smtp->message;
+               $smtp->ok or die "Failed to send $subject\n".$smtp->message;
+       }
        if ($quiet) {
                printf "Sent %s\n", $subject;
        } else {
-               print "OK. Log says:
-Date: $date
-Server: $smtp_server Port: 25
-From: $from
-Subject: $subject
-Cc: $cc
-To: $to
-
-Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
+               print "OK. Log says:\nDate: $date\n";
+               if ($smtp) {
+                       print "Server: $smtp_server\n";
+               } else {
+                       print "Sendmail: $smtp_server\n";
+               }
+               print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
+               if ($smtp) {
+                       print "Result: ", $smtp->code, ' ',
+                               ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
+               } else {
+                       print "Result: OK\n";
+               }
        }
 }
 
 $reply_to = $initial_reply_to;
+$references = $initial_reply_to || '';
 make_message_id();
 $subject = $initial_subject;
 
@@ -403,6 +498,11 @@ foreach my $t (@files) {
        # set up for the next message
        if ($chain_reply_to || length($reply_to) == 0) {
                $reply_to = $message_id;
+               if (length $references > 0) {
+                       $references .= " $message_id";
+               } else {
+                       $references = "$message_id";
+               }
        }
        make_message_id();
 }
@@ -423,9 +523,14 @@ sub unique_email_list(@) {
        my @emails;
 
        foreach my $entry (@_) {
-               my $clean = extract_valid_address($entry);
-               next if $seen{$clean}++;
-               push @emails, $entry;
+               if (my $clean = extract_valid_address($entry)) {
+                       $seen{$clean} ||= 0;
+                       next if $seen{$clean}++;
+                       push @emails, $entry;
+               } else {
+                       print STDERR "W: unable to extract a valid address",
+                                       " from: $entry\n";
+               }
        }
        return @emails;
 }
index 61f559f..38ac732 100755 (executable)
@@ -63,10 +63,17 @@ my $svn_dir = $ARGV[1];
 
 our @mergerx = ();
 if ($opt_m) {
-       @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
+       my $branch_esc = quotemeta ($branch_name);
+       my $trunk_esc  = quotemeta ($trunk_name);
+       @mergerx =
+       (
+               qr!\b(?:merg(?:ed?|ing))\b.*?\b((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
+               qr!\b(?:from|of)\W+((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
+               qr!\b(?:from|of)\W+(?:the )?([\w\.\-]+)[-\s]branch\b!i
+       );
 }
 if ($opt_M) {
-       push (@mergerx, qr/$opt_M/);
+       unshift (@mergerx, qr/$opt_M/);
 }
 
 # Absolutize filename now, since we will have chdir'ed by the time we
index dc6aa95..a0afa25 100755 (executable)
@@ -25,14 +25,12 @@ do
        force=1
        ;;
     -l)
-        cd "$GIT_DIR/refs" &&
        case "$#" in
        1)
-               find tags -type f -print ;;
-       *)
-               shift
-               find tags -type f -print | grep "$@" ;;
+               set x . ;;
        esac
+       shift
+       git rev-parse --symbolic --tags | sort | grep "$@"
        exit $?
        ;;
     -m)
diff --git a/git-whatchanged.sh b/git-whatchanged.sh
deleted file mode 100755 (executable)
index 1fb9feb..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/bin/sh
-
-USAGE='[-p] [--max-count=<n>] [<since>..<limit>] [--pretty=<format>] [-m] [git-diff-tree options] [git-rev-list options]'
-SUBDIRECTORY_OK='Yes'
-. git-sh-setup
-
-diff_tree_flags=$(git-rev-parse --sq --no-revs --flags "$@") || exit
-case "$0" in
-*whatchanged)
-       count=
-       test -z "$diff_tree_flags" &&
-               diff_tree_flags=$(git-repo-config --get whatchanged.difftree)
-       diff_tree_default_flags='-c -M --abbrev' ;;
-*show)
-       count=-n1
-       test -z "$diff_tree_flags" &&
-               diff_tree_flags=$(git-repo-config --get show.difftree)
-       diff_tree_default_flags='--cc --always' ;;
-esac
-test -z "$diff_tree_flags" &&
-       diff_tree_flags="$diff_tree_default_flags"
-
-rev_list_args=$(git-rev-parse --sq --default HEAD --revs-only "$@") &&
-diff_tree_args=$(git-rev-parse --sq --no-revs --no-flags "$@") &&
-
-eval "git-rev-list $count $rev_list_args" |
-eval "git-diff-tree --stdin --pretty -r $diff_tree_flags $diff_tree_args" |
-LESS="$LESS -S" ${PAGER:-less}
diff --git a/git.c b/git.c
index aa2b814..329ebec 100644 (file)
--- a/git.c
+++ b/git.c
@@ -8,9 +8,9 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
-#include <sys/ioctl.h>
 #include "git-compat-util.h"
 #include "exec_cmd.h"
+#include "cache.h"
 
 #include "builtin.h"
 
@@ -33,6 +33,113 @@ static void prepend_to_path(const char *dir, int len)
        setenv("PATH", path, 1);
 }
 
+static const char *alias_command;
+static char *alias_string = NULL;
+
+static int git_alias_config(const char *var, const char *value)
+{
+       if (!strncmp(var, "alias.", 6) && !strcmp(var + 6, alias_command)) {
+               alias_string = strdup(value);
+       }
+       return 0;
+}
+
+static int split_cmdline(char *cmdline, const char ***argv)
+{
+       int src, dst, count = 0, size = 16;
+       char quoted = 0;
+
+       *argv = malloc(sizeof(char*) * size);
+
+       /* split alias_string */
+       (*argv)[count++] = cmdline;
+       for (src = dst = 0; cmdline[src];) {
+               char c = cmdline[src];
+               if (!quoted && isspace(c)) {
+                       cmdline[dst++] = 0;
+                       while (cmdline[++src]
+                                       && isspace(cmdline[src]))
+                               ; /* skip */
+                       if (count >= size) {
+                               size += 16;
+                               *argv = realloc(*argv, sizeof(char*) * size);
+                       }
+                       (*argv)[count++] = cmdline + dst;
+               } else if(!quoted && (c == '\'' || c == '"')) {
+                       quoted = c;
+                       src++;
+               } else if (c == quoted) {
+                       quoted = 0;
+                       src++;
+               } else {
+                       if (c == '\\' && quoted != '\'') {
+                               src++;
+                               c = cmdline[src];
+                               if (!c) {
+                                       free(*argv);
+                                       *argv = NULL;
+                                       return error("cmdline ends with \\");
+                               }
+                       }
+                       cmdline[dst++] = c;
+                       src++;
+               }
+       }
+
+       cmdline[dst] = 0;
+
+       if (quoted) {
+               free(*argv);
+               *argv = NULL;
+               return error("unclosed quote");
+       }
+
+       return count;
+}
+
+static int handle_alias(int *argcp, const char ***argv)
+{
+       int nongit = 0, ret = 0;
+       const char *subdir;
+
+       subdir = setup_git_directory_gently(&nongit);
+       if (!nongit) {
+               int count;
+               const char** new_argv;
+
+               alias_command = (*argv)[0];
+               git_config(git_alias_config);
+               if (alias_string) {
+
+                       count = split_cmdline(alias_string, &new_argv);
+
+                       if (count < 1)
+                               die("empty alias for %s", alias_command);
+
+                       if (!strcmp(alias_command, new_argv[0]))
+                               die("recursive alias: %s", alias_command);
+
+                       /* insert after command name */
+                       if (*argcp > 1) {
+                               new_argv = realloc(new_argv, sizeof(char*) *
+                                                  (count + *argcp));
+                               memcpy(new_argv + count, *argv + 1,
+                                      sizeof(char*) * *argcp);
+                       }
+
+                       *argv = new_argv;
+                       *argcp += count - 1;
+
+                       ret = 1;
+               }
+       }
+
+       if (subdir)
+               chdir(subdir);
+
+       return ret;
+}
+
 const char git_version_string[] = GIT_VERSION;
 
 static void handle_internal_command(int argc, const char **argv, char **envp)
@@ -47,6 +154,31 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
                { "log", cmd_log },
                { "whatchanged", cmd_whatchanged },
                { "show", cmd_show },
+               { "push", cmd_push },
+               { "format-patch", cmd_format_patch },
+               { "count-objects", cmd_count_objects },
+               { "diff", cmd_diff },
+               { "grep", cmd_grep },
+               { "rm", cmd_rm },
+               { "add", cmd_add },
+               { "rev-list", cmd_rev_list },
+               { "init-db", cmd_init_db },
+               { "get-tar-commit-id", cmd_get_tar_commit_id },
+               { "upload-tar", cmd_upload_tar },
+               { "check-ref-format", cmd_check_ref_format },
+               { "ls-files", cmd_ls_files },
+               { "ls-tree", cmd_ls_tree },
+               { "tar-tree", cmd_tar_tree },
+               { "read-tree", cmd_read_tree },
+               { "commit-tree", cmd_commit_tree },
+               { "apply", cmd_apply },
+               { "show-branch", cmd_show_branch },
+               { "diff-files", cmd_diff_files },
+               { "diff-index", cmd_diff_index },
+               { "diff-stages", cmd_diff_stages },
+               { "diff-tree", cmd_diff_tree },
+               { "cat-file", cmd_cat_file },
+               { "rev-parse", cmd_rev_parse }
        };
        int i;
 
@@ -70,6 +202,7 @@ int main(int argc, const char **argv, char **envp)
        char *slash = strrchr(cmd, '/');
        char git_command[PATH_MAX + 1];
        const char *exec_path = NULL;
+       int done_alias = 0;
 
        /*
         * Take the basename of argv[0] as the command
@@ -154,11 +287,21 @@ int main(int argc, const char **argv, char **envp)
        exec_path = git_exec_path();
        prepend_to_path(exec_path, strlen(exec_path));
 
-       /* See if it's an internal command */
-       handle_internal_command(argc, argv, envp);
+       while (1) {
+               /* See if it's an internal command */
+               handle_internal_command(argc, argv, envp);
 
-       /* .. then try the external ones */
-       execv_git_cmd(argv);
+               /* .. then try the external ones */
+               execv_git_cmd(argv);
+
+               /* It could be an alias -- this works around the insanity
+                * of overriding "git log" with "git show" by having
+                * alias.log = show
+                */
+               if (done_alias || !handle_alias(&argc, &argv))
+                       break;
+               done_alias = 1;
+       }
 
        if (errno == ENOENT)
                cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
index 96dfc1d..8ccd256 100644 (file)
@@ -74,12 +74,12 @@ Git revision tree visualiser ('gitk')
 %setup -q
 
 %build
-make %{_smp_mflags} CFLAGS="$RPM_OPT_FLAGS" WITH_OWN_SUBPROCESS_PY=YesPlease WITH_SEND_EMAIL=1 \
+make %{_smp_mflags} CFLAGS="$RPM_OPT_FLAGS" WITH_OWN_SUBPROCESS_PY=YesPlease \
      prefix=%{_prefix} all %{!?_without_docs: doc}
 
 %install
 rm -rf $RPM_BUILD_ROOT
-make %{_smp_mflags} DESTDIR=$RPM_BUILD_ROOT WITH_OWN_SUBPROCESS_PY=YesPlease WITH_SEND_EMAIL=1 \
+make %{_smp_mflags} DESTDIR=$RPM_BUILD_ROOT WITH_OWN_SUBPROCESS_PY=YesPlease \
      prefix=%{_prefix} mandir=%{_mandir} \
      install %{!?_without_docs: install-doc}
 
diff --git a/gitk b/gitk
index 5362b76..ba4644f 100755 (executable)
--- a/gitk
+++ b/gitk
@@ -16,83 +16,112 @@ proc gitdir {} {
     }
 }
 
-proc start_rev_list {rlargs} {
+proc start_rev_list {view} {
     global startmsecs nextupdate ncmupdate
     global commfd leftover tclencoding datemode
+    global viewargs viewfiles commitidx
 
     set startmsecs [clock clicks -milliseconds]
     set nextupdate [expr {$startmsecs + 100}]
     set ncmupdate 1
-    initlayout
+    set commitidx($view) 0
+    set args $viewargs($view)
+    if {$viewfiles($view) ne {}} {
+       set args [concat $args "--" $viewfiles($view)]
+    }
     set order "--topo-order"
     if {$datemode} {
        set order "--date-order"
     }
     if {[catch {
-       set commfd [open [concat | git-rev-list --header $order \
-                             --parents --boundary --default HEAD $rlargs] r]
+       set fd [open [concat | git rev-list --header $order \
+                         --parents --boundary --default HEAD $args] r]
     } err]} {
-       puts stderr "Error executing git-rev-list: $err"
+       puts stderr "Error executing git rev-list: $err"
        exit 1
     }
-    set leftover {}
-    fconfigure $commfd -blocking 0 -translation lf
+    set commfd($view) $fd
+    set leftover($view) {}
+    fconfigure $fd -blocking 0 -translation lf
     if {$tclencoding != {}} {
-       fconfigure $commfd -encoding $tclencoding
+       fconfigure $fd -encoding $tclencoding
+    }
+    fileevent $fd readable [list getcommitlines $fd $view]
+    nowbusy $view
+}
+
+proc stop_rev_list {} {
+    global commfd curview
+
+    if {![info exists commfd($curview)]} return
+    set fd $commfd($curview)
+    catch {
+       set pid [pid $fd]
+       exec kill $pid
     }
-    fileevent $commfd readable [list getcommitlines $commfd]
-    . config -cursor watch
-    settextcursor watch
+    catch {close $fd}
+    unset commfd($curview)
 }
 
-proc getcommits {rargs} {
-    global phase canv mainfont
+proc getcommits {} {
+    global phase canv mainfont curview
 
     set phase getcommits
-    start_rev_list $rargs
-    $canv delete all
-    $canv create text 3 3 -anchor nw -text "Reading commits..." \
-       -font $mainfont -tags textitems
+    initlayout
+    start_rev_list $curview
+    show_status "Reading commits..."
 }
 
-proc getcommitlines {commfd}  {
+proc getcommitlines {fd view}  {
     global commitlisted nextupdate
-    global leftover
+    global leftover commfd
     global displayorder commitidx commitrow commitdata
-    global parentlist childlist children
+    global parentlist childlist children curview hlview
+    global vparentlist vchildlist vdisporder vcmitlisted
 
-    set stuff [read $commfd]
+    set stuff [read $fd]
     if {$stuff == {}} {
-       if {![eof $commfd]} return
+       if {![eof $fd]} return
+       global viewname
+       unset commfd($view)
+       notbusy $view
        # set it blocking so we wait for the process to terminate
-       fconfigure $commfd -blocking 1
-       if {![catch {close $commfd} err]} {
-           after idle finishcommits
-           return
+       fconfigure $fd -blocking 1
+       if {[catch {close $fd} err]} {
+           set fv {}
+           if {$view != $curview} {
+               set fv " for the \"$viewname($view)\" view"
+           }
+           if {[string range $err 0 4] == "usage"} {
+               set err "Gitk: error reading commits$fv:\
+                       bad arguments to git rev-list."
+               if {$viewname($view) eq "Command line"} {
+                   append err \
+                       "  (Note: arguments to gitk are passed to git rev-list\
+                        to allow selection of commits to be displayed.)"
+               }
+           } else {
+               set err "Error reading commits$fv: $err"
+           }
+           error_popup $err
        }
-       if {[string range $err 0 4] == "usage"} {
-           set err \
-               "Gitk: error reading commits: bad arguments to git-rev-list.\
-               (Note: arguments to gitk are passed to git-rev-list\
-               to allow selection of commits to be displayed.)"
-       } else {
-           set err "Error reading commits: $err"
+       if {$view == $curview} {
+           after idle finishcommits
        }
-       error_popup $err
-       exit 1
+       return
     }
     set start 0
     set gotsome 0
     while 1 {
        set i [string first "\0" $stuff $start]
        if {$i < 0} {
-           append leftover [string range $stuff $start end]
+           append leftover($view) [string range $stuff $start end]
            break
        }
        if {$start == 0} {
-           set cmit $leftover
+           set cmit $leftover($view)
            append cmit [string range $stuff 0 [expr {$i - 1}]]
-           set leftover {}
+           set leftover($view) {}
        } else {
            set cmit [string range $stuff $start [expr {$i - 1}]]
        }
@@ -119,47 +148,58 @@ proc getcommitlines {commfd}  {
            if {[string length $shortcmit] > 80} {
                set shortcmit "[string range $shortcmit 0 80]..."
            }
-           error_popup "Can't parse git-rev-list output: {$shortcmit}"
+           error_popup "Can't parse git rev-list output: {$shortcmit}"
            exit 1
        }
        set id [lindex $ids 0]
        if {$listed} {
            set olds [lrange $ids 1 end]
-           if {[llength $olds] > 1} {
-               set olds [lsort -unique $olds]
-           }
+           set i 0
            foreach p $olds {
-               lappend children($p) $id
+               if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
+                   lappend children($view,$p) $id
+               }
+               incr i
            }
        } else {
            set olds {}
        }
-       lappend parentlist $olds
-       if {[info exists children($id)]} {
-           lappend childlist $children($id)
-       } else {
-           lappend childlist {}
+       if {![info exists children($view,$id)]} {
+           set children($view,$id) {}
        }
        set commitdata($id) [string range $cmit [expr {$j + 1}] end]
-       set commitrow($id) $commitidx
-       incr commitidx
-       lappend displayorder $id
-       lappend commitlisted $listed
+       set commitrow($view,$id) $commitidx($view)
+       incr commitidx($view)
+       if {$view == $curview} {
+           lappend parentlist $olds
+           lappend childlist $children($view,$id)
+           lappend displayorder $id
+           lappend commitlisted $listed
+       } else {
+           lappend vparentlist($view) $olds
+           lappend vchildlist($view) $children($view,$id)
+           lappend vdisporder($view) $id
+           lappend vcmitlisted($view) $listed
+       }
        set gotsome 1
     }
     if {$gotsome} {
-       layoutmore
+       if {$view == $curview} {
+           layoutmore
+       } elseif {[info exists hlview] && $view == $hlview} {
+           vhighlightmore
+       }
     }
     if {[clock clicks -milliseconds] >= $nextupdate} {
-       doupdate 1
+       doupdate
     }
 }
 
-proc doupdate {reading} {
+proc doupdate {} {
     global commfd nextupdate numcommits ncmupdate
 
-    if {$reading} {
-       fileevent $commfd readable {}
+    foreach v [array names commfd] {
+       fileevent $commfd($v) readable {}
     }
     update
     set nextupdate [expr {[clock clicks -milliseconds] + 100}]
@@ -170,28 +210,37 @@ proc doupdate {reading} {
     } else {
        set ncmupdate [expr {$numcommits + 100}]
     }
-    if {$reading} {
-       fileevent $commfd readable [list getcommitlines $commfd]
+    foreach v [array names commfd] {
+       set fd $commfd($v)
+       fileevent $fd readable [list getcommitlines $fd $v]
     }
 }
 
 proc readcommit {id} {
-    if {[catch {set contents [exec git-cat-file commit $id]}]} return
+    if {[catch {set contents [exec git cat-file commit $id]}]} return
     parsecommit $id $contents 0
 }
 
-proc updatecommits {rargs} {
-    stopfindproc
-    foreach v {colormap selectedline matchinglines treediffs
-       mergefilelist currentid rowtextx commitrow
-       rowidlist rowoffsets idrowranges idrangedrawn iddrawn
-       linesegends crossings cornercrossings} {
-       global $v
-       catch {unset $v}
+proc updatecommits {} {
+    global viewdata curview phase displayorder
+    global children commitrow selectedline thickerline
+
+    if {$phase ne {}} {
+       stop_rev_list
+       set phase {}
     }
-    allcanvs delete all
+    set n $curview
+    foreach id $displayorder {
+       catch {unset children($n,$id)}
+       catch {unset commitrow($n,$id)}
+    }
+    set curview -1
+    catch {unset selectedline}
+    catch {unset thickerline}
+    catch {unset viewdata($n)}
+    discardallcommits
     readrefs
-    getcommits $rargs
+    showview $n
 }
 
 proc parsecommit {id contents listed} {
@@ -230,8 +279,8 @@ proc parsecommit {id contents listed} {
        set headline $comment
     }
     if {!$listed} {
-       # git-rev-list indents the comment by 4 spaces;
-       # if we got this via git-cat-file, add the indentation
+       # git rev-list indents the comment by 4 spaces;
+       # if we got this via git cat-file, add the indentation
        set newcomment {}
        foreach line [split $comment "\n"] {
            append newcomment "    "
@@ -274,10 +323,16 @@ proc readrefs {} {
            match id path]} {
            continue
        }
+       if {[regexp {^remotes/.*/HEAD$} $path match]} {
+           continue
+       }
        if {![regexp {^(tags|heads)/(.*)$} $path match type name]} {
            set type others
            set name $path
        }
+       if {[regexp {^remotes/} $path match]} {
+           set type heads
+       }
        if {$type == "tags"} {
            set tagids($name) $id
            lappend idtags($id) $name
@@ -285,14 +340,14 @@ proc readrefs {} {
            set type {}
            set tag {}
            catch {
-               set commit [exec git-rev-parse "$id^0"]
+               set commit [exec git rev-parse "$id^0"]
                if {"$commit" != "$id"} {
                    set tagids($name) $commit
                    lappend idtags($commit) $name
                }
            }           
            catch {
-               set tagcontents($name) [exec git-cat-file tag "$id"]
+               set tagcontents($name) [exec git cat-file tag "$id"]
            }
        } elseif { $type == "heads" } {
            set headids($name) $id
@@ -305,31 +360,38 @@ proc readrefs {} {
     close $refd
 }
 
+proc show_error {w top msg} {
+    message $w.m -text $msg -justify center -aspect 400
+    pack $w.m -side top -fill x -padx 20 -pady 20
+    button $w.ok -text OK -command "destroy $top"
+    pack $w.ok -side bottom -fill x
+    bind $top <Visibility> "grab $top; focus $top"
+    bind $top <Key-Return> "destroy $top"
+    tkwait window $top
+}
+
 proc error_popup msg {
     set w .error
     toplevel $w
     wm transient $w .
-    message $w.m -text $msg -justify center -aspect 400
-    pack $w.m -side top -fill x -padx 20 -pady 20
-    button $w.ok -text OK -command "destroy $w"
-    pack $w.ok -side bottom -fill x
-    bind $w <Visibility> "grab $w; focus $w"
-    bind $w <Key-Return> "destroy $w"
-    tkwait window $w
+    show_error $w $w $msg
 }
 
-proc makewindow {rargs} {
-    global canv canv2 canv3 linespc charspc ctext cflist textfont mainfont uifont
+proc makewindow {} {
+    global canv canv2 canv3 linespc charspc ctext cflist
+    global textfont mainfont uifont
     global findtype findtypemenu findloc findstring fstring geometry
     global entries sha1entry sha1string sha1but
     global maincursor textcursor curtextcursor
-    global rowctxmenu mergemax
+    global rowctxmenu mergemax wrapcomment
+    global highlight_files gdttype
+    global searchstring sstring
 
     menu .bar
     .bar add cascade -label "File" -menu .bar.file
     .bar configure -font $uifont
     menu .bar.file
-    .bar.file add command -label "Update" -command [list updatecommits $rargs]
+    .bar.file add command -label "Update" -command updatecommits
     .bar.file add command -label "Reread references" -command rereadrefs
     .bar.file add command -label "Quit" -command doquit
     .bar.file configure -font $uifont
@@ -337,6 +399,17 @@ proc makewindow {rargs} {
     .bar add cascade -label "Edit" -menu .bar.edit
     .bar.edit add command -label "Preferences" -command doprefs
     .bar.edit configure -font $uifont
+
+    menu .bar.view -font $uifont
+    .bar add cascade -label "View" -menu .bar.view
+    .bar.view add command -label "New view..." -command {newview 0}
+    .bar.view add command -label "Edit view..." -command editview \
+       -state disabled
+    .bar.view add command -label "Delete view" -command delview -state disabled
+    .bar.view add separator
+    .bar.view add radiobutton -label "All files" -command {showview 0} \
+       -variable selectedview -value 0
+    
     menu .bar.help
     .bar add cascade -label "Help" -menu .bar.help
     .bar.help add command -label "About gitk" -command about
@@ -362,6 +435,8 @@ proc makewindow {rargs} {
     }
     frame .ctop.top
     frame .ctop.top.bar
+    frame .ctop.top.lbar
+    pack .ctop.top.lbar -side bottom -fill x
     pack .ctop.top.bar -side bottom -fill x
     set cscroll .ctop.top.csb
     scrollbar $cscroll -command {allcanvs yview} -highlightthickness 0
@@ -423,36 +498,80 @@ proc makewindow {rargs} {
     set findstring {}
     set fstring .ctop.top.bar.findstring
     lappend entries $fstring
-    entry $fstring -width 30 -font $textfont -textvariable findstring -font $textfont
+    entry $fstring -width 30 -font $textfont -textvariable findstring
+    trace add variable findstring write find_change
     pack $fstring -side left -expand 1 -fill x
     set findtype Exact
     set findtypemenu [tk_optionMenu .ctop.top.bar.findtype \
                          findtype Exact IgnCase Regexp]
+    trace add variable findtype write find_change
     .ctop.top.bar.findtype configure -font $uifont
     .ctop.top.bar.findtype.menu configure -font $uifont
     set findloc "All fields"
     tk_optionMenu .ctop.top.bar.findloc findloc "All fields" Headline \
-       Comments Author Committer Files Pickaxe
+       Comments Author Committer
+    trace add variable findloc write find_change
     .ctop.top.bar.findloc configure -font $uifont
     .ctop.top.bar.findloc.menu configure -font $uifont
-
     pack .ctop.top.bar.findloc -side right
     pack .ctop.top.bar.findtype -side right
-    # for making sure type==Exact whenever loc==Pickaxe
-    trace add variable findloc write findlocchange
+
+    label .ctop.top.lbar.flabel -text "Highlight:  Commits " \
+       -font $uifont
+    pack .ctop.top.lbar.flabel -side left -fill y
+    set gdttype "touching paths:"
+    set gm [tk_optionMenu .ctop.top.lbar.gdttype gdttype "touching paths:" \
+               "adding/removing string:"]
+    trace add variable gdttype write hfiles_change
+    $gm conf -font $uifont
+    .ctop.top.lbar.gdttype conf -font $uifont
+    pack .ctop.top.lbar.gdttype -side left -fill y
+    entry .ctop.top.lbar.fent -width 25 -font $textfont \
+       -textvariable highlight_files
+    trace add variable highlight_files write hfiles_change
+    lappend entries .ctop.top.lbar.fent
+    pack .ctop.top.lbar.fent -side left -fill x -expand 1
+    label .ctop.top.lbar.vlabel -text " OR in view" -font $uifont
+    pack .ctop.top.lbar.vlabel -side left -fill y
+    global viewhlmenu selectedhlview
+    set viewhlmenu [tk_optionMenu .ctop.top.lbar.vhl selectedhlview None]
+    $viewhlmenu entryconf 0 -command delvhighlight
+    $viewhlmenu conf -font $uifont
+    .ctop.top.lbar.vhl conf -font $uifont
+    pack .ctop.top.lbar.vhl -side left -fill y
+    label .ctop.top.lbar.rlabel -text " OR " -font $uifont
+    pack .ctop.top.lbar.rlabel -side left -fill y
+    global highlight_related
+    set m [tk_optionMenu .ctop.top.lbar.relm highlight_related None \
+              "Descendent" "Not descendent" "Ancestor" "Not ancestor"]
+    $m conf -font $uifont
+    .ctop.top.lbar.relm conf -font $uifont
+    trace add variable highlight_related write vrel_change
+    pack .ctop.top.lbar.relm -side left -fill y
 
     panedwindow .ctop.cdet -orient horizontal
     .ctop add .ctop.cdet
     frame .ctop.cdet.left
+    frame .ctop.cdet.left.bot
+    pack .ctop.cdet.left.bot -side bottom -fill x
+    button .ctop.cdet.left.bot.search -text "Search" -command dosearch \
+       -font $uifont
+    pack .ctop.cdet.left.bot.search -side left -padx 5
+    set sstring .ctop.cdet.left.bot.sstring
+    entry $sstring -width 20 -font $textfont -textvariable searchstring
+    lappend entries $sstring
+    trace add variable searchstring write incrsearch
+    pack $sstring -side left -expand 1 -fill x
     set ctext .ctop.cdet.left.ctext
     text $ctext -bg white -state disabled -font $textfont \
        -width $geometry(ctextw) -height $geometry(ctexth) \
-       -yscrollcommand ".ctop.cdet.left.sb set" -wrap none
+       -yscrollcommand scrolltext -wrap none
     scrollbar .ctop.cdet.left.sb -command "$ctext yview"
     pack .ctop.cdet.left.sb -side right -fill y
     pack $ctext -side left -fill both -expand 1
     .ctop.cdet add .ctop.cdet.left
 
+    $ctext tag conf comment -wrap $wrapcomment
     $ctext tag conf filesep -font [concat $textfont bold] -back "#aaaaaa"
     $ctext tag conf hunksep -fore blue
     $ctext tag conf d0 -fore red
@@ -480,12 +599,26 @@ proc makewindow {rargs} {
     $ctext tag conf found -back yellow
 
     frame .ctop.cdet.right
+    frame .ctop.cdet.right.mode
+    radiobutton .ctop.cdet.right.mode.patch -text "Patch" \
+       -command reselectline -variable cmitmode -value "patch"
+    radiobutton .ctop.cdet.right.mode.tree -text "Tree" \
+       -command reselectline -variable cmitmode -value "tree"
+    grid .ctop.cdet.right.mode.patch .ctop.cdet.right.mode.tree -sticky ew
+    pack .ctop.cdet.right.mode -side top -fill x
     set cflist .ctop.cdet.right.cfiles
-    listbox $cflist -bg white -selectmode extended -width $geometry(cflistw) \
-       -yscrollcommand ".ctop.cdet.right.sb set" -font $mainfont
+    set indent [font measure $mainfont "nn"]
+    text $cflist -width $geometry(cflistw) -background white -font $mainfont \
+       -tabs [list $indent [expr {2 * $indent}]] \
+       -yscrollcommand ".ctop.cdet.right.sb set" \
+       -cursor [. cget -cursor] \
+       -spacing1 1 -spacing3 1
     scrollbar .ctop.cdet.right.sb -command "$cflist yview"
     pack .ctop.cdet.right.sb -side right -fill y
     pack $cflist -side left -fill both -expand 1
+    $cflist tag configure highlight \
+       -background [$cflist cget -selectbackground]
+    $cflist tag configure bold -font [concat $mainfont bold]
     .ctop.cdet add .ctop.cdet.right
     bind .ctop.cdet <Configure> {resizecdetpanes %W %w}
 
@@ -501,6 +634,8 @@ proc makewindow {rargs} {
     bindkey <End> sellastline
     bind . <Key-Up> "selnextline -1"
     bind . <Key-Down> "selnextline 1"
+    bind . <Shift-Key-Up> "next_highlight -1"
+    bind . <Shift-Key-Down> "next_highlight 1"
     bindkey <Key-Right> "goforw"
     bindkey <Key-Left> "goback"
     bind . <Key-Prior> "selnextpage -1"
@@ -532,17 +667,20 @@ proc makewindow {rargs} {
     bind . <Control-q> doquit
     bind . <Control-f> dofind
     bind . <Control-g> {findnext 0}
-    bind . <Control-r> findprev
+    bind . <Control-r> dosearchback
+    bind . <Control-s> dosearch
     bind . <Control-equal> {incrfont 1}
     bind . <Control-KP_Add> {incrfont 1}
     bind . <Control-minus> {incrfont -1}
     bind . <Control-KP_Subtract> {incrfont -1}
-    bind $cflist <<ListboxSelect>> listboxsel
     bind . <Destroy> {savestuff %W}
     bind . <Button-1> "click %W"
     bind $fstring <Key-Return> dofind
     bind $sha1entry <Key-Return> gotocommit
     bind $sha1entry <<PasteSelection>> clearsha1
+    bind $cflist <1> {sel_flist %W %x %y; break}
+    bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
+    bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
 
     set maincursor [. cget -cursor]
     set textcursor [$ctext cget -cursor]
@@ -575,6 +713,7 @@ proc canvscan {op w x y} {
 proc scrollcanv {cscroll f0 f1} {
     $cscroll set $f0 $f1
     drawfrac $f0 $f1
+    flushhighlights
 }
 
 # when we make a key binding for the toplevel, make sure
@@ -605,7 +744,9 @@ proc click {w} {
 proc savestuff {w} {
     global canv canv2 canv3 ctext cflist mainfont textfont uifont
     global stuffsaved findmergefiles maxgraphpct
-    global maxwidth
+    global maxwidth showneartags
+    global viewname viewfiles viewargs viewperm nextviewnum
+    global cmitmode wrapcomment
 
     if {$stuffsaved} return
     if {![winfo viewable .]} return
@@ -617,6 +758,9 @@ proc savestuff {w} {
        puts $f [list set findmergefiles $findmergefiles]
        puts $f [list set maxgraphpct $maxgraphpct]
        puts $f [list set maxwidth $maxwidth]
+       puts $f [list set cmitmode $cmitmode]
+       puts $f [list set wrapcomment $wrapcomment]
+       puts $f [list set showneartags $showneartags]
        puts $f "set geometry(width) [winfo width .ctop]"
        puts $f "set geometry(height) [winfo height .ctop]"
        puts $f "set geometry(canv1) [expr {[winfo width $canv]-2}]"
@@ -629,6 +773,13 @@ proc savestuff {w} {
        set wid [expr {([winfo width $cflist] - 11) \
                           / [font measure [$cflist cget -font] "0"]}]
        puts $f "set geometry(cflistw) $wid"
+       puts -nonewline $f "set permviews {"
+       for {set v 0} {$v < $nextviewnum} {incr v} {
+           if {$viewperm($v)} {
+               puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v)]}"
+           }
+       }
+       puts $f "}"
        close $f
        file rename -force "~/.gitk-new" "~/.gitk"
     }
@@ -707,69 +858,1305 @@ proc about {} {
        raise $w
        return
     }
-    toplevel $w
-    wm title $w "About gitk"
-    message $w.m -text {
-Gitk - a commit viewer for git
+    toplevel $w
+    wm title $w "About gitk"
+    message $w.m -text {
+Gitk - a commit viewer for git
+
+Copyright © 2005-2006 Paul Mackerras
+
+Use and redistribute under the terms of the GNU General Public License} \
+           -justify center -aspect 400
+    pack $w.m -side top -fill x -padx 20 -pady 20
+    button $w.ok -text Close -command "destroy $w"
+    pack $w.ok -side bottom
+}
+
+proc keys {} {
+    set w .keys
+    if {[winfo exists $w]} {
+       raise $w
+       return
+    }
+    toplevel $w
+    wm title $w "Gitk key bindings"
+    message $w.m -text {
+Gitk key bindings:
+
+<Ctrl-Q>               Quit
+<Home>         Move to first commit
+<End>          Move to last commit
+<Up>, p, i     Move up one commit
+<Down>, n, k   Move down one commit
+<Left>, z, j   Go back in history list
+<Right>, x, l  Go forward in history list
+<PageUp>       Move up one page in commit list
+<PageDown>     Move down one page in commit list
+<Ctrl-Home>    Scroll to top of commit list
+<Ctrl-End>     Scroll to bottom of commit list
+<Ctrl-Up>      Scroll commit list up one line
+<Ctrl-Down>    Scroll commit list down one line
+<Ctrl-PageUp>  Scroll commit list up one page
+<Ctrl-PageDown>        Scroll commit list down one page
+<Shift-Up>     Move to previous highlighted line
+<Shift-Down>   Move to next highlighted line
+<Delete>, b    Scroll diff view up one page
+<Backspace>    Scroll diff view up one page
+<Space>                Scroll diff view down one page
+u              Scroll diff view up 18 lines
+d              Scroll diff view down 18 lines
+<Ctrl-F>               Find
+<Ctrl-G>               Move to next find hit
+<Return>       Move to next find hit
+/              Move to next find hit, or redo find
+?              Move to previous find hit
+f              Scroll diff view to next file
+<Ctrl-S>               Search for next hit in diff view
+<Ctrl-R>               Search for previous hit in diff view
+<Ctrl-KP+>     Increase font size
+<Ctrl-plus>    Increase font size
+<Ctrl-KP->     Decrease font size
+<Ctrl-minus>   Decrease font size
+} \
+           -justify left -bg white -border 2 -relief sunken
+    pack $w.m -side top -fill both
+    button $w.ok -text Close -command "destroy $w"
+    pack $w.ok -side bottom
+}
+
+# Procedures for manipulating the file list window at the
+# bottom right of the overall window.
+
+proc treeview {w l openlevs} {
+    global treecontents treediropen treeheight treeparent treeindex
+
+    set ix 0
+    set treeindex() 0
+    set lev 0
+    set prefix {}
+    set prefixend -1
+    set prefendstack {}
+    set htstack {}
+    set ht 0
+    set treecontents() {}
+    $w conf -state normal
+    foreach f $l {
+       while {[string range $f 0 $prefixend] ne $prefix} {
+           if {$lev <= $openlevs} {
+               $w mark set e:$treeindex($prefix) "end -1c"
+               $w mark gravity e:$treeindex($prefix) left
+           }
+           set treeheight($prefix) $ht
+           incr ht [lindex $htstack end]
+           set htstack [lreplace $htstack end end]
+           set prefixend [lindex $prefendstack end]
+           set prefendstack [lreplace $prefendstack end end]
+           set prefix [string range $prefix 0 $prefixend]
+           incr lev -1
+       }
+       set tail [string range $f [expr {$prefixend+1}] end]
+       while {[set slash [string first "/" $tail]] >= 0} {
+           lappend htstack $ht
+           set ht 0
+           lappend prefendstack $prefixend
+           incr prefixend [expr {$slash + 1}]
+           set d [string range $tail 0 $slash]
+           lappend treecontents($prefix) $d
+           set oldprefix $prefix
+           append prefix $d
+           set treecontents($prefix) {}
+           set treeindex($prefix) [incr ix]
+           set treeparent($prefix) $oldprefix
+           set tail [string range $tail [expr {$slash+1}] end]
+           if {$lev <= $openlevs} {
+               set ht 1
+               set treediropen($prefix) [expr {$lev < $openlevs}]
+               set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
+               $w mark set d:$ix "end -1c"
+               $w mark gravity d:$ix left
+               set str "\n"
+               for {set i 0} {$i < $lev} {incr i} {append str "\t"}
+               $w insert end $str
+               $w image create end -align center -image $bm -padx 1 \
+                   -name a:$ix
+               $w insert end $d [highlight_tag $prefix]
+               $w mark set s:$ix "end -1c"
+               $w mark gravity s:$ix left
+           }
+           incr lev
+       }
+       if {$tail ne {}} {
+           if {$lev <= $openlevs} {
+               incr ht
+               set str "\n"
+               for {set i 0} {$i < $lev} {incr i} {append str "\t"}
+               $w insert end $str
+               $w insert end $tail [highlight_tag $f]
+           }
+           lappend treecontents($prefix) $tail
+       }
+    }
+    while {$htstack ne {}} {
+       set treeheight($prefix) $ht
+       incr ht [lindex $htstack end]
+       set htstack [lreplace $htstack end end]
+    }
+    $w conf -state disabled
+}
+
+proc linetoelt {l} {
+    global treeheight treecontents
+
+    set y 2
+    set prefix {}
+    while {1} {
+       foreach e $treecontents($prefix) {
+           if {$y == $l} {
+               return "$prefix$e"
+           }
+           set n 1
+           if {[string index $e end] eq "/"} {
+               set n $treeheight($prefix$e)
+               if {$y + $n > $l} {
+                   append prefix $e
+                   incr y
+                   break
+               }
+           }
+           incr y $n
+       }
+    }
+}
+
+proc highlight_tree {y prefix} {
+    global treeheight treecontents cflist
+
+    foreach e $treecontents($prefix) {
+       set path $prefix$e
+       if {[highlight_tag $path] ne {}} {
+           $cflist tag add bold $y.0 "$y.0 lineend"
+       }
+       incr y
+       if {[string index $e end] eq "/" && $treeheight($path) > 1} {
+           set y [highlight_tree $y $path]
+       }
+    }
+    return $y
+}
+
+proc treeclosedir {w dir} {
+    global treediropen treeheight treeparent treeindex
+
+    set ix $treeindex($dir)
+    $w conf -state normal
+    $w delete s:$ix e:$ix
+    set treediropen($dir) 0
+    $w image configure a:$ix -image tri-rt
+    $w conf -state disabled
+    set n [expr {1 - $treeheight($dir)}]
+    while {$dir ne {}} {
+       incr treeheight($dir) $n
+       set dir $treeparent($dir)
+    }
+}
+
+proc treeopendir {w dir} {
+    global treediropen treeheight treeparent treecontents treeindex
+
+    set ix $treeindex($dir)
+    $w conf -state normal
+    $w image configure a:$ix -image tri-dn
+    $w mark set e:$ix s:$ix
+    $w mark gravity e:$ix right
+    set lev 0
+    set str "\n"
+    set n [llength $treecontents($dir)]
+    for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
+       incr lev
+       append str "\t"
+       incr treeheight($x) $n
+    }
+    foreach e $treecontents($dir) {
+       set de $dir$e
+       if {[string index $e end] eq "/"} {
+           set iy $treeindex($de)
+           $w mark set d:$iy e:$ix
+           $w mark gravity d:$iy left
+           $w insert e:$ix $str
+           set treediropen($de) 0
+           $w image create e:$ix -align center -image tri-rt -padx 1 \
+               -name a:$iy
+           $w insert e:$ix $e [highlight_tag $de]
+           $w mark set s:$iy e:$ix
+           $w mark gravity s:$iy left
+           set treeheight($de) 1
+       } else {
+           $w insert e:$ix $str
+           $w insert e:$ix $e [highlight_tag $de]
+       }
+    }
+    $w mark gravity e:$ix left
+    $w conf -state disabled
+    set treediropen($dir) 1
+    set top [lindex [split [$w index @0,0] .] 0]
+    set ht [$w cget -height]
+    set l [lindex [split [$w index s:$ix] .] 0]
+    if {$l < $top} {
+       $w yview $l.0
+    } elseif {$l + $n + 1 > $top + $ht} {
+       set top [expr {$l + $n + 2 - $ht}]
+       if {$l < $top} {
+           set top $l
+       }
+       $w yview $top.0
+    }
+}
+
+proc treeclick {w x y} {
+    global treediropen cmitmode ctext cflist cflist_top
+
+    if {$cmitmode ne "tree"} return
+    if {![info exists cflist_top]} return
+    set l [lindex [split [$w index "@$x,$y"] "."] 0]
+    $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
+    $cflist tag add highlight $l.0 "$l.0 lineend"
+    set cflist_top $l
+    if {$l == 1} {
+       $ctext yview 1.0
+       return
+    }
+    set e [linetoelt $l]
+    if {[string index $e end] ne "/"} {
+       showfile $e
+    } elseif {$treediropen($e)} {
+       treeclosedir $w $e
+    } else {
+       treeopendir $w $e
+    }
+}
+
+proc setfilelist {id} {
+    global treefilelist cflist
+
+    treeview $cflist $treefilelist($id) 0
+}
+
+image create bitmap tri-rt -background black -foreground blue -data {
+    #define tri-rt_width 13
+    #define tri-rt_height 13
+    static unsigned char tri-rt_bits[] = {
+       0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
+       0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
+       0x00, 0x00};
+} -maskdata {
+    #define tri-rt-mask_width 13
+    #define tri-rt-mask_height 13
+    static unsigned char tri-rt-mask_bits[] = {
+       0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
+       0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
+       0x08, 0x00};
+}
+image create bitmap tri-dn -background black -foreground blue -data {
+    #define tri-dn_width 13
+    #define tri-dn_height 13
+    static unsigned char tri-dn_bits[] = {
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
+       0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00};
+} -maskdata {
+    #define tri-dn-mask_width 13
+    #define tri-dn-mask_height 13
+    static unsigned char tri-dn-mask_bits[] = {
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
+       0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00};
+}
+
+proc init_flist {first} {
+    global cflist cflist_top selectedline difffilestart
+
+    $cflist conf -state normal
+    $cflist delete 0.0 end
+    if {$first ne {}} {
+       $cflist insert end $first
+       set cflist_top 1
+       $cflist tag add highlight 1.0 "1.0 lineend"
+    } else {
+       catch {unset cflist_top}
+    }
+    $cflist conf -state disabled
+    set difffilestart {}
+}
+
+proc highlight_tag {f} {
+    global highlight_paths
+
+    foreach p $highlight_paths {
+       if {[string match $p $f]} {
+           return "bold"
+       }
+    }
+    return {}
+}
+
+proc highlight_filelist {} {
+    global cmitmode cflist
+
+    $cflist conf -state normal
+    if {$cmitmode ne "tree"} {
+       set end [lindex [split [$cflist index end] .] 0]
+       for {set l 2} {$l < $end} {incr l} {
+           set line [$cflist get $l.0 "$l.0 lineend"]
+           if {[highlight_tag $line] ne {}} {
+               $cflist tag add bold $l.0 "$l.0 lineend"
+           }
+       }
+    } else {
+       highlight_tree 2 {}
+    }
+    $cflist conf -state disabled
+}
+
+proc unhighlight_filelist {} {
+    global cflist
+
+    $cflist conf -state normal
+    $cflist tag remove bold 1.0 end
+    $cflist conf -state disabled
+}
+
+proc add_flist {fl} {
+    global cflist
+
+    $cflist conf -state normal
+    foreach f $fl {
+       $cflist insert end "\n"
+       $cflist insert end $f [highlight_tag $f]
+    }
+    $cflist conf -state disabled
+}
+
+proc sel_flist {w x y} {
+    global ctext difffilestart cflist cflist_top cmitmode
+
+    if {$cmitmode eq "tree"} return
+    if {![info exists cflist_top]} return
+    set l [lindex [split [$w index "@$x,$y"] "."] 0]
+    $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
+    $cflist tag add highlight $l.0 "$l.0 lineend"
+    set cflist_top $l
+    if {$l == 1} {
+       $ctext yview 1.0
+    } else {
+       catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
+    }
+}
+
+# Functions for adding and removing shell-type quoting
+
+proc shellquote {str} {
+    if {![string match "*\['\"\\ \t]*" $str]} {
+       return $str
+    }
+    if {![string match "*\['\"\\]*" $str]} {
+       return "\"$str\""
+    }
+    if {![string match "*'*" $str]} {
+       return "'$str'"
+    }
+    return "\"[string map {\" \\\" \\ \\\\} $str]\""
+}
+
+proc shellarglist {l} {
+    set str {}
+    foreach a $l {
+       if {$str ne {}} {
+           append str " "
+       }
+       append str [shellquote $a]
+    }
+    return $str
+}
+
+proc shelldequote {str} {
+    set ret {}
+    set used -1
+    while {1} {
+       incr used
+       if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
+           append ret [string range $str $used end]
+           set used [string length $str]
+           break
+       }
+       set first [lindex $first 0]
+       set ch [string index $str $first]
+       if {$first > $used} {
+           append ret [string range $str $used [expr {$first - 1}]]
+           set used $first
+       }
+       if {$ch eq " " || $ch eq "\t"} break
+       incr used
+       if {$ch eq "'"} {
+           set first [string first "'" $str $used]
+           if {$first < 0} {
+               error "unmatched single-quote"
+           }
+           append ret [string range $str $used [expr {$first - 1}]]
+           set used $first
+           continue
+       }
+       if {$ch eq "\\"} {
+           if {$used >= [string length $str]} {
+               error "trailing backslash"
+           }
+           append ret [string index $str $used]
+           continue
+       }
+       # here ch == "\""
+       while {1} {
+           if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
+               error "unmatched double-quote"
+           }
+           set first [lindex $first 0]
+           set ch [string index $str $first]
+           if {$first > $used} {
+               append ret [string range $str $used [expr {$first - 1}]]
+               set used $first
+           }
+           if {$ch eq "\""} break
+           incr used
+           append ret [string index $str $used]
+           incr used
+       }
+    }
+    return [list $used $ret]
+}
+
+proc shellsplit {str} {
+    set l {}
+    while {1} {
+       set str [string trimleft $str]
+       if {$str eq {}} break
+       set dq [shelldequote $str]
+       set n [lindex $dq 0]
+       set word [lindex $dq 1]
+       set str [string range $str $n end]
+       lappend l $word
+    }
+    return $l
+}
+
+# Code to implement multiple views
+
+proc newview {ishighlight} {
+    global nextviewnum newviewname newviewperm uifont newishighlight
+    global newviewargs revtreeargs
+
+    set newishighlight $ishighlight
+    set top .gitkview
+    if {[winfo exists $top]} {
+       raise $top
+       return
+    }
+    set newviewname($nextviewnum) "View $nextviewnum"
+    set newviewperm($nextviewnum) 0
+    set newviewargs($nextviewnum) [shellarglist $revtreeargs]
+    vieweditor $top $nextviewnum "Gitk view definition" 
+}
+
+proc editview {} {
+    global curview
+    global viewname viewperm newviewname newviewperm
+    global viewargs newviewargs
+
+    set top .gitkvedit-$curview
+    if {[winfo exists $top]} {
+       raise $top
+       return
+    }
+    set newviewname($curview) $viewname($curview)
+    set newviewperm($curview) $viewperm($curview)
+    set newviewargs($curview) [shellarglist $viewargs($curview)]
+    vieweditor $top $curview "Gitk: edit view $viewname($curview)"
+}
+
+proc vieweditor {top n title} {
+    global newviewname newviewperm viewfiles
+    global uifont
+
+    toplevel $top
+    wm title $top $title
+    label $top.nl -text "Name" -font $uifont
+    entry $top.name -width 20 -textvariable newviewname($n)
+    grid $top.nl $top.name -sticky w -pady 5
+    checkbutton $top.perm -text "Remember this view" -variable newviewperm($n)
+    grid $top.perm - -pady 5 -sticky w
+    message $top.al -aspect 1000 -font $uifont \
+       -text "Commits to include (arguments to git rev-list):"
+    grid $top.al - -sticky w -pady 5
+    entry $top.args -width 50 -textvariable newviewargs($n) \
+       -background white
+    grid $top.args - -sticky ew -padx 5
+    message $top.l -aspect 1000 -font $uifont \
+       -text "Enter files and directories to include, one per line:"
+    grid $top.l - -sticky w
+    text $top.t -width 40 -height 10 -background white
+    if {[info exists viewfiles($n)]} {
+       foreach f $viewfiles($n) {
+           $top.t insert end $f
+           $top.t insert end "\n"
+       }
+       $top.t delete {end - 1c} end
+       $top.t mark set insert 0.0
+    }
+    grid $top.t - -sticky ew -padx 5
+    frame $top.buts
+    button $top.buts.ok -text "OK" -command [list newviewok $top $n]
+    button $top.buts.can -text "Cancel" -command [list destroy $top]
+    grid $top.buts.ok $top.buts.can
+    grid columnconfigure $top.buts 0 -weight 1 -uniform a
+    grid columnconfigure $top.buts 1 -weight 1 -uniform a
+    grid $top.buts - -pady 10 -sticky ew
+    focus $top.t
+}
+
+proc doviewmenu {m first cmd op argv} {
+    set nmenu [$m index end]
+    for {set i $first} {$i <= $nmenu} {incr i} {
+       if {[$m entrycget $i -command] eq $cmd} {
+           eval $m $op $i $argv
+           break
+       }
+    }
+}
+
+proc allviewmenus {n op args} {
+    global viewhlmenu
+
+    doviewmenu .bar.view 7 [list showview $n] $op $args
+    doviewmenu $viewhlmenu 1 [list addvhighlight $n] $op $args
+}
+
+proc newviewok {top n} {
+    global nextviewnum newviewperm newviewname newishighlight
+    global viewname viewfiles viewperm selectedview curview
+    global viewargs newviewargs viewhlmenu
+
+    if {[catch {
+       set newargs [shellsplit $newviewargs($n)]
+    } err]} {
+       error_popup "Error in commit selection arguments: $err"
+       wm raise $top
+       focus $top
+       return
+    }
+    set files {}
+    foreach f [split [$top.t get 0.0 end] "\n"] {
+       set ft [string trim $f]
+       if {$ft ne {}} {
+           lappend files $ft
+       }
+    }
+    if {![info exists viewfiles($n)]} {
+       # creating a new view
+       incr nextviewnum
+       set viewname($n) $newviewname($n)
+       set viewperm($n) $newviewperm($n)
+       set viewfiles($n) $files
+       set viewargs($n) $newargs
+       addviewmenu $n
+       if {!$newishighlight} {
+           after idle showview $n
+       } else {
+           after idle addvhighlight $n
+       }
+    } else {
+       # editing an existing view
+       set viewperm($n) $newviewperm($n)
+       if {$newviewname($n) ne $viewname($n)} {
+           set viewname($n) $newviewname($n)
+           doviewmenu .bar.view 7 [list showview $n] \
+               entryconf [list -label $viewname($n)]
+           doviewmenu $viewhlmenu 1 [list addvhighlight $n] \
+               entryconf [list -label $viewname($n) -value $viewname($n)]
+       }
+       if {$files ne $viewfiles($n) || $newargs ne $viewargs($n)} {
+           set viewfiles($n) $files
+           set viewargs($n) $newargs
+           if {$curview == $n} {
+               after idle updatecommits
+           }
+       }
+    }
+    catch {destroy $top}
+}
+
+proc delview {} {
+    global curview viewdata viewperm hlview selectedhlview
+
+    if {$curview == 0} return
+    if {[info exists hlview] && $hlview == $curview} {
+       set selectedhlview None
+       unset hlview
+    }
+    allviewmenus $curview delete
+    set viewdata($curview) {}
+    set viewperm($curview) 0
+    showview 0
+}
+
+proc addviewmenu {n} {
+    global viewname viewhlmenu
+
+    .bar.view add radiobutton -label $viewname($n) \
+       -command [list showview $n] -variable selectedview -value $n
+    $viewhlmenu add radiobutton -label $viewname($n) \
+       -command [list addvhighlight $n] -variable selectedhlview
+}
+
+proc flatten {var} {
+    global $var
+
+    set ret {}
+    foreach i [array names $var] {
+       lappend ret $i [set $var\($i\)]
+    }
+    return $ret
+}
+
+proc unflatten {var l} {
+    global $var
+
+    catch {unset $var}
+    foreach {i v} $l {
+       set $var\($i\) $v
+    }
+}
+
+proc showview {n} {
+    global curview viewdata viewfiles
+    global displayorder parentlist childlist rowidlist rowoffsets
+    global colormap rowtextx commitrow nextcolor canvxmax
+    global numcommits rowrangelist commitlisted idrowranges
+    global selectedline currentid canv canvy0
+    global matchinglines treediffs
+    global pending_select phase
+    global commitidx rowlaidout rowoptim linesegends
+    global commfd nextupdate
+    global selectedview
+    global vparentlist vchildlist vdisporder vcmitlisted
+    global hlview selectedhlview
+
+    if {$n == $curview} return
+    set selid {}
+    if {[info exists selectedline]} {
+       set selid $currentid
+       set y [yc $selectedline]
+       set ymax [lindex [$canv cget -scrollregion] 3]
+       set span [$canv yview]
+       set ytop [expr {[lindex $span 0] * $ymax}]
+       set ybot [expr {[lindex $span 1] * $ymax}]
+       if {$ytop < $y && $y < $ybot} {
+           set yscreen [expr {$y - $ytop}]
+       } else {
+           set yscreen [expr {($ybot - $ytop) / 2}]
+       }
+    }
+    unselectline
+    normalline
+    stopfindproc
+    if {$curview >= 0} {
+       set vparentlist($curview) $parentlist
+       set vchildlist($curview) $childlist
+       set vdisporder($curview) $displayorder
+       set vcmitlisted($curview) $commitlisted
+       if {$phase ne {}} {
+           set viewdata($curview) \
+               [list $phase $rowidlist $rowoffsets $rowrangelist \
+                    [flatten idrowranges] [flatten idinlist] \
+                    $rowlaidout $rowoptim $numcommits $linesegends]
+       } elseif {![info exists viewdata($curview)]
+                 || [lindex $viewdata($curview) 0] ne {}} {
+           set viewdata($curview) \
+               [list {} $rowidlist $rowoffsets $rowrangelist]
+       }
+    }
+    catch {unset matchinglines}
+    catch {unset treediffs}
+    clear_display
+    if {[info exists hlview] && $hlview == $n} {
+       unset hlview
+       set selectedhlview None
+    }
+
+    set curview $n
+    set selectedview $n
+    .bar.view entryconf 2 -state [expr {$n == 0? "disabled": "normal"}]
+    .bar.view entryconf 3 -state [expr {$n == 0? "disabled": "normal"}]
+
+    if {![info exists viewdata($n)]} {
+       set pending_select $selid
+       getcommits
+       return
+    }
+
+    set v $viewdata($n)
+    set phase [lindex $v 0]
+    set displayorder $vdisporder($n)
+    set parentlist $vparentlist($n)
+    set childlist $vchildlist($n)
+    set commitlisted $vcmitlisted($n)
+    set rowidlist [lindex $v 1]
+    set rowoffsets [lindex $v 2]
+    set rowrangelist [lindex $v 3]
+    if {$phase eq {}} {
+       set numcommits [llength $displayorder]
+       catch {unset idrowranges}
+    } else {
+       unflatten idrowranges [lindex $v 4]
+       unflatten idinlist [lindex $v 5]
+       set rowlaidout [lindex $v 6]
+       set rowoptim [lindex $v 7]
+       set numcommits [lindex $v 8]
+       set linesegends [lindex $v 9]
+    }
+
+    catch {unset colormap}
+    catch {unset rowtextx}
+    set nextcolor 0
+    set canvxmax [$canv cget -width]
+    set curview $n
+    set row 0
+    setcanvscroll
+    set yf 0
+    set row 0
+    if {$selid ne {} && [info exists commitrow($n,$selid)]} {
+       set row $commitrow($n,$selid)
+       # try to get the selected row in the same position on the screen
+       set ymax [lindex [$canv cget -scrollregion] 3]
+       set ytop [expr {[yc $row] - $yscreen}]
+       if {$ytop < 0} {
+           set ytop 0
+       }
+       set yf [expr {$ytop * 1.0 / $ymax}]
+    }
+    allcanvs yview moveto $yf
+    drawvisible
+    selectline $row 0
+    if {$phase ne {}} {
+       if {$phase eq "getcommits"} {
+           show_status "Reading commits..."
+       }
+       if {[info exists commfd($n)]} {
+           layoutmore
+       } else {
+           finishcommits
+       }
+    } elseif {$numcommits == 0} {
+       show_status "No commits selected"
+    }
+}
+
+# Stuff relating to the highlighting facility
+
+proc ishighlighted {row} {
+    global vhighlights fhighlights nhighlights rhighlights
+
+    if {[info exists nhighlights($row)] && $nhighlights($row) > 0} {
+       return $nhighlights($row)
+    }
+    if {[info exists vhighlights($row)] && $vhighlights($row) > 0} {
+       return $vhighlights($row)
+    }
+    if {[info exists fhighlights($row)] && $fhighlights($row) > 0} {
+       return $fhighlights($row)
+    }
+    if {[info exists rhighlights($row)] && $rhighlights($row) > 0} {
+       return $rhighlights($row)
+    }
+    return 0
+}
+
+proc bolden {row font} {
+    global canv linehtag selectedline boldrows
+
+    lappend boldrows $row
+    $canv itemconf $linehtag($row) -font $font
+    if {[info exists selectedline] && $row == $selectedline} {
+       $canv delete secsel
+       set t [eval $canv create rect [$canv bbox $linehtag($row)] \
+                  -outline {{}} -tags secsel \
+                  -fill [$canv cget -selectbackground]]
+       $canv lower $t
+    }
+}
+
+proc bolden_name {row font} {
+    global canv2 linentag selectedline boldnamerows
+
+    lappend boldnamerows $row
+    $canv2 itemconf $linentag($row) -font $font
+    if {[info exists selectedline] && $row == $selectedline} {
+       $canv2 delete secsel
+       set t [eval $canv2 create rect [$canv2 bbox $linentag($row)] \
+                  -outline {{}} -tags secsel \
+                  -fill [$canv2 cget -selectbackground]]
+       $canv2 lower $t
+    }
+}
+
+proc unbolden {} {
+    global mainfont boldrows
+
+    set stillbold {}
+    foreach row $boldrows {
+       if {![ishighlighted $row]} {
+           bolden $row $mainfont
+       } else {
+           lappend stillbold $row
+       }
+    }
+    set boldrows $stillbold
+}
+
+proc addvhighlight {n} {
+    global hlview curview viewdata vhl_done vhighlights commitidx
+
+    if {[info exists hlview]} {
+       delvhighlight
+    }
+    set hlview $n
+    if {$n != $curview && ![info exists viewdata($n)]} {
+       set viewdata($n) [list getcommits {{}} {{}} {} {} {} 0 0 0 {}]
+       set vparentlist($n) {}
+       set vchildlist($n) {}
+       set vdisporder($n) {}
+       set vcmitlisted($n) {}
+       start_rev_list $n
+    }
+    set vhl_done $commitidx($hlview)
+    if {$vhl_done > 0} {
+       drawvisible
+    }
+}
+
+proc delvhighlight {} {
+    global hlview vhighlights
+
+    if {![info exists hlview]} return
+    unset hlview
+    catch {unset vhighlights}
+    unbolden
+}
+
+proc vhighlightmore {} {
+    global hlview vhl_done commitidx vhighlights
+    global displayorder vdisporder curview mainfont
+
+    set font [concat $mainfont bold]
+    set max $commitidx($hlview)
+    if {$hlview == $curview} {
+       set disp $displayorder
+    } else {
+       set disp $vdisporder($hlview)
+    }
+    set vr [visiblerows]
+    set r0 [lindex $vr 0]
+    set r1 [lindex $vr 1]
+    for {set i $vhl_done} {$i < $max} {incr i} {
+       set id [lindex $disp $i]
+       if {[info exists commitrow($curview,$id)]} {
+           set row $commitrow($curview,$id)
+           if {$r0 <= $row && $row <= $r1} {
+               if {![highlighted $row]} {
+                   bolden $row $font
+               }
+               set vhighlights($row) 1
+           }
+       }
+    }
+    set vhl_done $max
+}
+
+proc askvhighlight {row id} {
+    global hlview vhighlights commitrow iddrawn mainfont
+
+    if {[info exists commitrow($hlview,$id)]} {
+       if {[info exists iddrawn($id)] && ![ishighlighted $row]} {
+           bolden $row [concat $mainfont bold]
+       }
+       set vhighlights($row) 1
+    } else {
+       set vhighlights($row) 0
+    }
+}
+
+proc hfiles_change {name ix op} {
+    global highlight_files filehighlight fhighlights fh_serial
+    global mainfont highlight_paths
+
+    if {[info exists filehighlight]} {
+       # delete previous highlights
+       catch {close $filehighlight}
+       unset filehighlight
+       catch {unset fhighlights}
+       unbolden
+       unhighlight_filelist
+    }
+    set highlight_paths {}
+    after cancel do_file_hl $fh_serial
+    incr fh_serial
+    if {$highlight_files ne {}} {
+       after 300 do_file_hl $fh_serial
+    }
+}
+
+proc makepatterns {l} {
+    set ret {}
+    foreach e $l {
+       set ee [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} $e]
+       if {[string index $ee end] eq "/"} {
+           lappend ret "$ee*"
+       } else {
+           lappend ret $ee
+           lappend ret "$ee/*"
+       }
+    }
+    return $ret
+}
+
+proc do_file_hl {serial} {
+    global highlight_files filehighlight highlight_paths gdttype fhl_list
+
+    if {$gdttype eq "touching paths:"} {
+       if {[catch {set paths [shellsplit $highlight_files]}]} return
+       set highlight_paths [makepatterns $paths]
+       highlight_filelist
+       set gdtargs [concat -- $paths]
+    } else {
+       set gdtargs [list "-S$highlight_files"]
+    }
+    set cmd [concat | git-diff-tree -r -s --stdin $gdtargs]
+    set filehighlight [open $cmd r+]
+    fconfigure $filehighlight -blocking 0
+    fileevent $filehighlight readable readfhighlight
+    set fhl_list {}
+    drawvisible
+    flushhighlights
+}
+
+proc flushhighlights {} {
+    global filehighlight fhl_list
+
+    if {[info exists filehighlight]} {
+       lappend fhl_list {}
+       puts $filehighlight ""
+       flush $filehighlight
+    }
+}
+
+proc askfilehighlight {row id} {
+    global filehighlight fhighlights fhl_list
+
+    lappend fhl_list $id
+    set fhighlights($row) -1
+    puts $filehighlight $id
+}
+
+proc readfhighlight {} {
+    global filehighlight fhighlights commitrow curview mainfont iddrawn
+    global fhl_list
+
+    while {[gets $filehighlight line] >= 0} {
+       set line [string trim $line]
+       set i [lsearch -exact $fhl_list $line]
+       if {$i < 0} continue
+       for {set j 0} {$j < $i} {incr j} {
+           set id [lindex $fhl_list $j]
+           if {[info exists commitrow($curview,$id)]} {
+               set fhighlights($commitrow($curview,$id)) 0
+           }
+       }
+       set fhl_list [lrange $fhl_list [expr {$i+1}] end]
+       if {$line eq {}} continue
+       if {![info exists commitrow($curview,$line)]} continue
+       set row $commitrow($curview,$line)
+       if {[info exists iddrawn($line)] && ![ishighlighted $row]} {
+           bolden $row [concat $mainfont bold]
+       }
+       set fhighlights($row) 1
+    }
+    if {[eof $filehighlight]} {
+       # strange...
+       puts "oops, git-diff-tree died"
+       catch {close $filehighlight}
+       unset filehighlight
+    }
+    next_hlcont
+}
+
+proc find_change {name ix op} {
+    global nhighlights mainfont boldnamerows
+    global findstring findpattern findtype
+
+    # delete previous highlights, if any
+    foreach row $boldnamerows {
+       bolden_name $row $mainfont
+    }
+    set boldnamerows {}
+    catch {unset nhighlights}
+    unbolden
+    if {$findtype ne "Regexp"} {
+       set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
+                  $findstring]
+       set findpattern "*$e*"
+    }
+    drawvisible
+}
+
+proc askfindhighlight {row id} {
+    global nhighlights commitinfo iddrawn mainfont
+    global findstring findtype findloc findpattern
+
+    if {![info exists commitinfo($id)]} {
+       getcommit $id
+    }
+    set info $commitinfo($id)
+    set isbold 0
+    set fldtypes {Headline Author Date Committer CDate Comments}
+    foreach f $info ty $fldtypes {
+       if {$findloc ne "All fields" && $findloc ne $ty} {
+           continue
+       }
+       if {$findtype eq "Regexp"} {
+           set doesmatch [regexp $findstring $f]
+       } elseif {$findtype eq "IgnCase"} {
+           set doesmatch [string match -nocase $findpattern $f]
+       } else {
+           set doesmatch [string match $findpattern $f]
+       }
+       if {$doesmatch} {
+           if {$ty eq "Author"} {
+               set isbold 2
+           } else {
+               set isbold 1
+           }
+       }
+    }
+    if {[info exists iddrawn($id)]} {
+       if {$isbold && ![ishighlighted $row]} {
+           bolden $row [concat $mainfont bold]
+       }
+       if {$isbold >= 2} {
+           bolden_name $row [concat $mainfont bold]
+       }
+    }
+    set nhighlights($row) $isbold
+}
+
+proc vrel_change {name ix op} {
+    global highlight_related
+
+    rhighlight_none
+    if {$highlight_related ne "None"} {
+       after idle drawvisible
+    }
+}
+
+# prepare for testing whether commits are descendents or ancestors of a
+proc rhighlight_sel {a} {
+    global descendent desc_todo ancestor anc_todo
+    global highlight_related rhighlights
+
+    catch {unset descendent}
+    set desc_todo [list $a]
+    catch {unset ancestor}
+    set anc_todo [list $a]
+    if {$highlight_related ne "None"} {
+       rhighlight_none
+       after idle drawvisible
+    }
+}
+
+proc rhighlight_none {} {
+    global rhighlights
+
+    catch {unset rhighlights}
+    unbolden
+}
+
+proc is_descendent {a} {
+    global curview children commitrow descendent desc_todo
+
+    set v $curview
+    set la $commitrow($v,$a)
+    set todo $desc_todo
+    set leftover {}
+    set done 0
+    for {set i 0} {$i < [llength $todo]} {incr i} {
+       set do [lindex $todo $i]
+       if {$commitrow($v,$do) < $la} {
+           lappend leftover $do
+           continue
+       }
+       foreach nk $children($v,$do) {
+           if {![info exists descendent($nk)]} {
+               set descendent($nk) 1
+               lappend todo $nk
+               if {$nk eq $a} {
+                   set done 1
+               }
+           }
+       }
+       if {$done} {
+           set desc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
+           return
+       }
+    }
+    set descendent($a) 0
+    set desc_todo $leftover
+}
+
+proc is_ancestor {a} {
+    global curview parentlist commitrow ancestor anc_todo
+
+    set v $curview
+    set la $commitrow($v,$a)
+    set todo $anc_todo
+    set leftover {}
+    set done 0
+    for {set i 0} {$i < [llength $todo]} {incr i} {
+       set do [lindex $todo $i]
+       if {![info exists commitrow($v,$do)] || $commitrow($v,$do) > $la} {
+           lappend leftover $do
+           continue
+       }
+       foreach np [lindex $parentlist $commitrow($v,$do)] {
+           if {![info exists ancestor($np)]} {
+               set ancestor($np) 1
+               lappend todo $np
+               if {$np eq $a} {
+                   set done 1
+               }
+           }
+       }
+       if {$done} {
+           set anc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
+           return
+       }
+    }
+    set ancestor($a) 0
+    set anc_todo $leftover
+}
+
+proc askrelhighlight {row id} {
+    global descendent highlight_related iddrawn mainfont rhighlights
+    global selectedline ancestor
+
+    if {![info exists selectedline]} return
+    set isbold 0
+    if {$highlight_related eq "Descendent" ||
+       $highlight_related eq "Not descendent"} {
+       if {![info exists descendent($id)]} {
+           is_descendent $id
+       }
+       if {$descendent($id) == ($highlight_related eq "Descendent")} {
+           set isbold 1
+       }
+    } elseif {$highlight_related eq "Ancestor" ||
+             $highlight_related eq "Not ancestor"} {
+       if {![info exists ancestor($id)]} {
+           is_ancestor $id
+       }
+       if {$ancestor($id) == ($highlight_related eq "Ancestor")} {
+           set isbold 1
+       }
+    }
+    if {[info exists iddrawn($id)]} {
+       if {$isbold && ![ishighlighted $row]} {
+           bolden $row [concat $mainfont bold]
+       }
+    }
+    set rhighlights($row) $isbold
+}
+
+proc next_hlcont {} {
+    global fhl_row fhl_dirn displayorder numcommits
+    global vhighlights fhighlights nhighlights rhighlights
+    global hlview filehighlight findstring highlight_related
+
+    if {![info exists fhl_dirn] || $fhl_dirn == 0} return
+    set row $fhl_row
+    while {1} {
+       if {$row < 0 || $row >= $numcommits} {
+           bell
+           set fhl_dirn 0
+           return
+       }
+       set id [lindex $displayorder $row]
+       if {[info exists hlview]} {
+           if {![info exists vhighlights($row)]} {
+               askvhighlight $row $id
+           }
+           if {$vhighlights($row) > 0} break
+       }
+       if {$findstring ne {}} {
+           if {![info exists nhighlights($row)]} {
+               askfindhighlight $row $id
+           }
+           if {$nhighlights($row) > 0} break
+       }
+       if {$highlight_related ne "None"} {
+           if {![info exists rhighlights($row)]} {
+               askrelhighlight $row $id
+           }
+           if {$rhighlights($row) > 0} break
+       }
+       if {[info exists filehighlight]} {
+           if {![info exists fhighlights($row)]} {
+               # ask for a few more while we're at it...
+               set r $row
+               for {set n 0} {$n < 100} {incr n} {
+                   if {![info exists fhighlights($r)]} {
+                       askfilehighlight $r [lindex $displayorder $r]
+                   }
+                   incr r $fhl_dirn
+                   if {$r < 0 || $r >= $numcommits} break
+               }
+               flushhighlights
+           }
+           if {$fhighlights($row) < 0} {
+               set fhl_row $row
+               return
+           }
+           if {$fhighlights($row) > 0} break
+       }
+       incr row $fhl_dirn
+    }
+    set fhl_dirn 0
+    selectline $row 1
+}
 
-Copyright © 2005-2006 Paul Mackerras
+proc next_highlight {dirn} {
+    global selectedline fhl_row fhl_dirn
+    global hlview filehighlight findstring highlight_related
 
-Use and redistribute under the terms of the GNU General Public License} \
-           -justify center -aspect 400
-    pack $w.m -side top -fill x -padx 20 -pady 20
-    button $w.ok -text Close -command "destroy $w"
-    pack $w.ok -side bottom
+    if {![info exists selectedline]} return
+    if {!([info exists hlview] || $findstring ne {} ||
+         $highlight_related ne "None" || [info exists filehighlight])} return
+    set fhl_row [expr {$selectedline + $dirn}]
+    set fhl_dirn $dirn
+    next_hlcont
 }
 
-proc keys {} {
-    set w .keys
-    if {[winfo exists $w]} {
-       raise $w
-       return
-    }
-    toplevel $w
-    wm title $w "Gitk key bindings"
-    message $w.m -text {
-Gitk key bindings:
+proc cancel_next_highlight {} {
+    global fhl_dirn
 
-<Ctrl-Q>               Quit
-<Home>         Move to first commit
-<End>          Move to last commit
-<Up>, p, i     Move up one commit
-<Down>, n, k   Move down one commit
-<Left>, z, j   Go back in history list
-<Right>, x, l  Go forward in history list
-<PageUp>       Move up one page in commit list
-<PageDown>     Move down one page in commit list
-<Ctrl-Home>    Scroll to top of commit list
-<Ctrl-End>     Scroll to bottom of commit list
-<Ctrl-Up>      Scroll commit list up one line
-<Ctrl-Down>    Scroll commit list down one line
-<Ctrl-PageUp>  Scroll commit list up one page
-<Ctrl-PageDown>        Scroll commit list down one page
-<Delete>, b    Scroll diff view up one page
-<Backspace>    Scroll diff view up one page
-<Space>                Scroll diff view down one page
-u              Scroll diff view up 18 lines
-d              Scroll diff view down 18 lines
-<Ctrl-F>               Find
-<Ctrl-G>               Move to next find hit
-<Ctrl-R>               Move to previous find hit
-<Return>       Move to next find hit
-/              Move to next find hit, or redo find
-?              Move to previous find hit
-f              Scroll diff view to next file
-<Ctrl-KP+>     Increase font size
-<Ctrl-plus>    Increase font size
-<Ctrl-KP->     Decrease font size
-<Ctrl-minus>   Decrease font size
-} \
-           -justify left -bg white -border 2 -relief sunken
-    pack $w.m -side top -fill both
-    button $w.ok -text Close -command "destroy $w"
-    pack $w.ok -side bottom
+    set fhl_dirn 0
 }
 
+# Graph layout functions
+
 proc shortids {ids} {
     set res {}
     foreach id $ids {
@@ -805,20 +2192,21 @@ proc ntimes {n o} {
 }
 
 proc usedinrange {id l1 l2} {
-    global children commitrow
+    global children commitrow childlist curview
 
-    if {[info exists commitrow($id)]} {
-       set r $commitrow($id)
+    if {[info exists commitrow($curview,$id)]} {
+       set r $commitrow($curview,$id)
        if {$l1 <= $r && $r <= $l2} {
            return [expr {$r - $l1 + 1}]
        }
+       set kids [lindex $childlist $r]
+    } else {
+       set kids $children($curview,$id)
     }
-    foreach c $children($id) {
-       if {[info exists commitrow($c)]} {
-           set r $commitrow($c)
-           if {$l1 <= $r && $r <= $l2} {
-               return [expr {$r - $l1 + 1}]
-           }
+    foreach c $kids {
+       set r $commitrow($curview,$c)
+       if {$l1 <= $r && $r <= $l2} {
+           return [expr {$r - $l1 + 1}]
        }
     }
     return 0
@@ -886,18 +2274,19 @@ proc makeuparrow {oid x y z} {
 proc initlayout {} {
     global rowidlist rowoffsets displayorder commitlisted
     global rowlaidout rowoptim
-    global idinlist rowchk
-    global commitidx numcommits canvxmax canv
+    global idinlist rowchk rowrangelist idrowranges
+    global numcommits canvxmax canv
     global nextcolor
     global parentlist childlist children
+    global colormap rowtextx
+    global linesegends
 
-    set commitidx 0
     set numcommits 0
     set displayorder {}
     set commitlisted {}
     set parentlist {}
     set childlist {}
-    catch {unset children}
+    set rowrangelist {}
     set nextcolor 0
     set rowidlist {{}}
     set rowoffsets {{}}
@@ -906,6 +2295,10 @@ proc initlayout {} {
     set rowlaidout 0
     set rowoptim 0
     set canvxmax [$canv cget -width]
+    catch {unset colormap}
+    catch {unset rowtextx}
+    catch {unset idrowranges}
+    set linesegends {}
 }
 
 proc setcanvscroll {} {
@@ -938,13 +2331,12 @@ proc visiblerows {} {
 
 proc layoutmore {} {
     global rowlaidout rowoptim commitidx numcommits optim_delay
-    global uparrowlen
+    global uparrowlen curview
 
     set row $rowlaidout
-    set rowlaidout [layoutrows $row $commitidx 0]
+    set rowlaidout [layoutrows $row $commitidx($curview) 0]
     set orow [expr {$rowlaidout - $uparrowlen - 1}]
     if {$orow > $rowoptim} {
-       checkcrossings $rowoptim $orow
        optimize_rows $rowoptim 0 $orow
        set rowoptim $orow
     }
@@ -955,8 +2347,8 @@ proc layoutmore {} {
 }
 
 proc showstuff {canshow} {
-    global numcommits
-    global linesegends idrowranges idrangedrawn
+    global numcommits commitrow pending_select selectedline
+    global linesegends idrowranges idrangedrawn curview
 
     if {$numcommits == 0} {
        global phase
@@ -969,17 +2361,16 @@ proc showstuff {canshow} {
     set rows [visiblerows]
     set r0 [lindex $rows 0]
     set r1 [lindex $rows 1]
+    set selrow -1
     for {set r $row} {$r < $canshow} {incr r} {
-       if {[info exists linesegends($r)]} {
-           foreach id $linesegends($r) {
-               set i -1
-               foreach {s e} $idrowranges($id) {
-                   incr i
-                   if {$e ne {} && $e < $numcommits && $s <= $r1 && $e >= $r0
-                       && ![info exists idrangedrawn($id,$i)]} {
-                       drawlineseg $id $i
-                       set idrangedrawn($id,$i) 1
-                   }
+       foreach id [lindex $linesegends [expr {$r+1}]] {
+           set i -1
+           foreach {s e} [rowranges $id] {
+               incr i
+               if {$e ne {} && $e < $numcommits && $s <= $r1 && $e >= $r0
+                   && ![info exists idrangedrawn($id,$i)]} {
+                   drawlineseg $id $i
+                   set idrangedrawn($id,$i) 1
                }
            }
        }
@@ -991,6 +2382,14 @@ proc showstuff {canshow} {
        drawcmitrow $row
        incr row
     }
+    if {[info exists pending_select] &&
+       [info exists commitrow($curview,$pending_select)] &&
+       $commitrow($curview,$pending_select) < $numcommits} {
+       selectline $commitrow($curview,$pending_select) 1
+    }
+    if {![info exists selectedline] && ![info exists pending_select]} {
+       selectline 0 1
+    }
 }
 
 proc layoutrows {row endrow last} {
@@ -998,8 +2397,8 @@ proc layoutrows {row endrow last} {
     global uparrowlen downarrowlen maxwidth mingaplen
     global childlist parentlist
     global idrowranges linesegends
-    global commitidx
-    global idinlist rowchk
+    global commitidx curview
+    global idinlist rowchk rowrangelist
 
     set idlist [lindex $rowidlist $row]
     set offs [lindex $rowoffsets $row]
@@ -1014,10 +2413,12 @@ proc layoutrows {row endrow last} {
                lappend oldolds $p
            }
        }
+       set lse {}
        set nev [expr {[llength $idlist] + [llength $newolds]
                       + [llength $oldolds] - $maxwidth + 1}]
        if {$nev > 0} {
-           if {!$last && $row + $uparrowlen + $mingaplen >= $commitidx} break
+           if {!$last &&
+               $row + $uparrowlen + $mingaplen >= $commitidx($curview)} break
            for {set x [llength $idlist]} {[incr x -1] >= 0} {} {
                set i [lindex $idlist $x]
                if {![info exists rowchk($i)] || $row >= $rowchk($i)} {
@@ -1029,7 +2430,7 @@ proc layoutrows {row endrow last} {
                        set offs [incrange $offs $x 1]
                        set idinlist($i) 0
                        set rm1 [expr {$row - 1}]
-                       lappend linesegends($rm1) $i
+                       lappend lse $i
                        lappend idrowranges($i) $rm1
                        if {[incr nev -1] <= 0} break
                        continue
@@ -1040,6 +2441,7 @@ proc layoutrows {row endrow last} {
            lset rowidlist $row $idlist
            lset rowoffsets $row $offs
        }
+       lappend linesegends $lse
        set col [lsearch -exact $idlist $id]
        if {$col < 0} {
            set col [llength $idlist]
@@ -1058,9 +2460,13 @@ proc layoutrows {row endrow last} {
        } else {
            unset idinlist($id)
        }
+       set ranges {}
        if {[info exists idrowranges($id)]} {
-           lappend idrowranges($id) $row
+           set ranges $idrowranges($id)
+           lappend ranges $row
+           unset idrowranges($id)
        }
+       lappend rowrangelist $ranges
        incr row
        set offs [ntimes [llength $idlist] 0]
        set l [llength $newolds]
@@ -1101,29 +2507,28 @@ proc layoutrows {row endrow last} {
 proc addextraid {id row} {
     global displayorder commitrow commitinfo
     global commitidx commitlisted
-    global parentlist childlist children
+    global parentlist childlist children curview
 
-    incr commitidx
+    incr commitidx($curview)
     lappend displayorder $id
     lappend commitlisted 0
     lappend parentlist {}
-    set commitrow($id) $row
+    set commitrow($curview,$id) $row
     readcommit $id
     if {![info exists commitinfo($id)]} {
        set commitinfo($id) {"No commit information available"}
     }
-    if {[info exists children($id)]} {
-       lappend childlist $children($id)
-    } else {
-       lappend childlist {}
+    if {![info exists children($curview,$id)]} {
+       set children($curview,$id) {}
     }
+    lappend childlist $children($curview,$id)
 }
 
 proc layouttail {} {
-    global rowidlist rowoffsets idinlist commitidx
-    global idrowranges
+    global rowidlist rowoffsets idinlist commitidx curview
+    global idrowranges rowrangelist
 
-    set row $commitidx
+    set row $commitidx($curview)
     set idlist [lindex $rowidlist $row]
     while {$idlist ne {}} {
        set col [expr {[llength $idlist] - 1}]
@@ -1131,6 +2536,8 @@ proc layouttail {} {
        addextraid $id $row
        unset idinlist($id)
        lappend idrowranges($id) $row
+       lappend rowrangelist $idrowranges($id)
+       unset idrowranges($id)
        incr row
        set offs [ntimes $col 0]
        set idlist [lreplace $idlist $col $col]
@@ -1144,6 +2551,8 @@ proc layouttail {} {
        lset rowoffsets $row 0
        makeuparrow $id 0 $row 0
        lappend idrowranges($id) $row
+       lappend rowrangelist $idrowranges($id)
+       unset idrowranges($id)
        incr row
        lappend rowidlist {}
        lappend rowoffsets {}
@@ -1160,7 +2569,7 @@ proc insert_pad {row col npad} {
 }
 
 proc optimize_rows {row col endrow} {
-    global rowidlist rowoffsets idrowranges linesegends displayorder
+    global rowidlist rowoffsets idrowranges displayorder
 
     for {} {$row < $endrow} {incr row} {
        set idlist [lindex $rowidlist $row]
@@ -1179,8 +2588,8 @@ proc optimize_rows {row col endrow} {
            set z0 [lindex $rowoffsets $y0 $x0]
            if {$z0 eq {}} {
                set id [lindex $idlist $col]
-               if {[info exists idrowranges($id)] &&
-                   $y0 > [lindex $idrowranges($id) 0]} {
+               set ranges [rowranges $id]
+               if {$ranges ne {} && $y0 > [lindex $ranges 0]} {
                    set isarrow 1
                }
            }
@@ -1238,8 +2647,8 @@ proc optimize_rows {row col endrow} {
                if {$o eq {}} {
                    # check if this is the link to the first child
                    set id [lindex $idlist $col]
-                   if {[info exists idrowranges($id)] &&
-                       $row == [lindex $idrowranges($id) 0]} {
+                   set ranges [rowranges $id]
+                   if {$ranges ne {} && $row == [lindex $ranges 0]} {
                        # it is, work out offset to child
                        set y0 [expr {$row - 1}]
                        set id [lindex $displayorder $y0]
@@ -1293,13 +2702,36 @@ proc linewidth {id} {
     return $wid
 }
 
+proc rowranges {id} {
+    global phase idrowranges commitrow rowlaidout rowrangelist curview
+
+    set ranges {}
+    if {$phase eq {} ||
+       ([info exists commitrow($curview,$id)]
+        && $commitrow($curview,$id) < $rowlaidout)} {
+       set ranges [lindex $rowrangelist $commitrow($curview,$id)]
+    } elseif {[info exists idrowranges($id)]} {
+       set ranges $idrowranges($id)
+    }
+    return $ranges
+}
+
 proc drawlineseg {id i} {
-    global rowoffsets rowidlist idrowranges
+    global rowoffsets rowidlist
     global displayorder
     global canv colormap linespc
+    global numcommits commitrow curview
 
-    set startrow [lindex $idrowranges($id) [expr {2 * $i}]]
-    set row [lindex $idrowranges($id) [expr {2 * $i + 1}]]
+    set ranges [rowranges $id]
+    set downarrow 1
+    if {[info exists commitrow($curview,$id)]
+       && $commitrow($curview,$id) < $numcommits} {
+       set downarrow [expr {$i < [llength $ranges] / 2 - 1}]
+    } else {
+       set downarrow 1
+    }
+    set startrow [lindex $ranges [expr {2 * $i}]]
+    set row [lindex $ranges [expr {2 * $i + 1}]]
     if {$startrow == $row} return
     assigncolor $id
     set coords {}
@@ -1343,8 +2775,7 @@ proc drawlineseg {id i} {
        }
     }
     if {[llength $coords] < 4} return
-    set last [expr {[llength $idrowranges($id)] / 2 - 1}]
-    if {$i < $last} {
+    if {$downarrow} {
        # This line has an arrow at the lower end: check if the arrow is
        # on a diagonal segment, and if so, work around the Tk 8.4
        # refusal to draw arrows on diagonal lines.
@@ -1364,7 +2795,7 @@ proc drawlineseg {id i} {
            }
        }
     }
-    set arrow [expr {2 * ($i > 0) + ($i < $last)}]
+    set arrow [expr {2 * ($i > 0) + $downarrow}]
     set arrow [lindex {none first last both} $arrow]
     set t [$canv create line $coords -width [linewidth $id] \
               -fill $colormap($id) -tags lines.$id -arrow $arrow]
@@ -1373,7 +2804,7 @@ proc drawlineseg {id i} {
 }
 
 proc drawparentlinks {id row col olds} {
-    global rowidlist canv colormap idrowranges
+    global rowidlist canv colormap
 
     set row2 [expr {$row + 1}]
     set x [xc $row $col]
@@ -1392,9 +2823,9 @@ proc drawparentlinks {id row col olds} {
        if {$x2 > $rmx} {
            set rmx $x2
        }
-       if {[info exists idrowranges($p)] &&
-           $row2 == [lindex $idrowranges($p) 0] &&
-           $row2 < [lindex $idrowranges($p) 1]} {
+       set ranges [rowranges $p]
+       if {$ranges ne {} && $row2 == [lindex $ranges 0]
+           && $row2 < [lindex $ranges 1]} {
            # drawlineseg will do this one for us
            continue
        }
@@ -1417,19 +2848,19 @@ proc drawparentlinks {id row col olds} {
 
 proc drawlines {id} {
     global colormap canv
-    global idrowranges idrangedrawn
-    global childlist iddrawn commitrow rowidlist
+    global idrangedrawn
+    global children iddrawn commitrow rowidlist curview
 
     $canv delete lines.$id
-    set nr [expr {[llength $idrowranges($id)] / 2}]
+    set nr [expr {[llength [rowranges $id]] / 2}]
     for {set i 0} {$i < $nr} {incr i} {
        if {[info exists idrangedrawn($id,$i)]} {
            drawlineseg $id $i
        }
     }
-    foreach child [lindex $childlist $commitrow($id)] {
+    foreach child $children($curview,$id) {
        if {[info exists iddrawn($child)]} {
-           set row $commitrow($child)
+           set row $commitrow($curview,$child)
            set col [lsearch -exact [lindex $rowidlist $row] $child]
            if {$col >= 0} {
                drawparentlinks $child $row $col [list $id]
@@ -1443,7 +2874,7 @@ proc drawcmittext {id row col rmx} {
     global commitlisted commitinfo rowidlist
     global rowtextx idpos idtags idheads idotherrefs
     global linehtag linentag linedtag
-    global mainfont namefont canvxmax
+    global mainfont canvxmax boldrows boldnamerows
 
     set ofill [expr {[lindex $commitlisted $row]? "blue": "white"}]
     set x [xc $row $col]
@@ -1468,11 +2899,22 @@ proc drawcmittext {id row col rmx} {
     set name [lindex $commitinfo($id) 1]
     set date [lindex $commitinfo($id) 2]
     set date [formatdate $date]
+    set font $mainfont
+    set nfont $mainfont
+    set isbold [ishighlighted $row]
+    if {$isbold > 0} {
+       lappend boldrows $row
+       lappend font bold
+       if {$isbold > 1} {
+           lappend boldnamerows $row
+           lappend nfont bold
+       }
+    }
     set linehtag($row) [$canv create text $xt $y -anchor w \
-                           -text $headline -font $mainfont ]
+                           -text $headline -font $font]
     $canv bind $linehtag($row) <Button-3> "rowmenu %X %Y $id"
     set linentag($row) [$canv2 create text 3 $y -anchor w \
-                           -text $name -font $namefont]
+                           -text $name -font $nfont]
     set linedtag($row) [$canv3 create text 3 $y -anchor w \
                            -text $date -font $mainfont]
     set xr [expr {$xt + [font measure $mainfont $headline]}]
@@ -1484,14 +2926,17 @@ proc drawcmittext {id row col rmx} {
 
 proc drawcmitrow {row} {
     global displayorder rowidlist
-    global idrowranges idrangedrawn iddrawn
+    global idrangedrawn iddrawn
     global commitinfo parentlist numcommits
+    global filehighlight fhighlights findstring nhighlights
+    global hlview vhighlights
+    global highlight_related rhighlights
 
     if {$row >= $numcommits} return
     foreach id [lindex $rowidlist $row] {
-       if {![info exists idrowranges($id)]} continue
+       if {$id eq {}} continue
        set i -1
-       foreach {s e} $idrowranges($id) {
+       foreach {s e} [rowranges $id] {
            incr i
            if {$row < $s} continue
            if {$e eq {}} break
@@ -1506,6 +2951,18 @@ proc drawcmitrow {row} {
     }
 
     set id [lindex $displayorder $row]
+    if {[info exists hlview] && ![info exists vhighlights($row)]} {
+       askvhighlight $row $id
+    }
+    if {[info exists filehighlight] && ![info exists fhighlights($row)]} {
+       askfilehighlight $row $id
+    }
+    if {$findstring ne {} && ![info exists nhighlights($row)]} {
+       askfindhighlight $row $id
+    }
+    if {$highlight_related ne "None" && ![info exists rhighlights($row)]} {
+       askrelhighlight $row $id
+    }
     if {[info exists iddrawn($id)]} return
     set col [lsearch -exact [lindex $rowidlist $row] $id]
     if {$col < 0} {
@@ -1554,68 +3011,101 @@ proc drawvisible {} {
 
 proc clear_display {} {
     global iddrawn idrangedrawn
+    global vhighlights fhighlights nhighlights rhighlights
 
     allcanvs delete all
     catch {unset iddrawn}
     catch {unset idrangedrawn}
+    catch {unset vhighlights}
+    catch {unset fhighlights}
+    catch {unset nhighlights}
+    catch {unset rhighlights}
+}
+
+proc findcrossings {id} {
+    global rowidlist parentlist numcommits rowoffsets displayorder
+
+    set cross {}
+    set ccross {}
+    foreach {s e} [rowranges $id] {
+       if {$e >= $numcommits} {
+           set e [expr {$numcommits - 1}]
+       }
+       if {$e <= $s} continue
+       set x [lsearch -exact [lindex $rowidlist $e] $id]
+       if {$x < 0} {
+           puts "findcrossings: oops, no [shortids $id] in row $e"
+           continue
+       }
+       for {set row $e} {[incr row -1] >= $s} {} {
+           set olds [lindex $parentlist $row]
+           set kid [lindex $displayorder $row]
+           set kidx [lsearch -exact [lindex $rowidlist $row] $kid]
+           if {$kidx < 0} continue
+           set nextrow [lindex $rowidlist [expr {$row + 1}]]
+           foreach p $olds {
+               set px [lsearch -exact $nextrow $p]
+               if {$px < 0} continue
+               if {($kidx < $x && $x < $px) || ($px < $x && $x < $kidx)} {
+                   if {[lsearch -exact $ccross $p] >= 0} continue
+                   if {$x == $px + ($kidx < $px? -1: 1)} {
+                       lappend ccross $p
+                   } elseif {[lsearch -exact $cross $p] < 0} {
+                       lappend cross $p
+                   }
+               }
+           }
+           set inc [lindex $rowoffsets $row $x]
+           if {$inc eq {}} break
+           incr x $inc
+       }
+    }
+    return [concat $ccross {{}} $cross]
 }
 
 proc assigncolor {id} {
     global colormap colors nextcolor
-    global commitrow parentlist children childlist
-    global cornercrossings crossings
+    global commitrow parentlist children children curview
 
     if {[info exists colormap($id)]} return
     set ncolors [llength $colors]
-    if {[info exists commitrow($id)]} {
-       set kids [lindex $childlist $commitrow($id)]
-    } elseif {[info exists children($id)]} {
-       set kids $children($id)
+    if {[info exists children($curview,$id)]} {
+       set kids $children($curview,$id)
     } else {
        set kids {}
     }
     if {[llength $kids] == 1} {
        set child [lindex $kids 0]
        if {[info exists colormap($child)]
-           && [llength [lindex $parentlist $commitrow($child)]] == 1} {
+           && [llength [lindex $parentlist $commitrow($curview,$child)]] == 1} {
            set colormap($id) $colormap($child)
            return
        }
     }
     set badcolors {}
-    if {[info exists cornercrossings($id)]} {
-       foreach x $cornercrossings($id) {
-           if {[info exists colormap($x)]
-               && [lsearch -exact $badcolors $colormap($x)] < 0} {
-               lappend badcolors $colormap($x)
-           }
+    set origbad {}
+    foreach x [findcrossings $id] {
+       if {$x eq {}} {
+           # delimiter between corner crossings and other crossings
+           if {[llength $badcolors] >= $ncolors - 1} break
+           set origbad $badcolors
        }
-       if {[llength $badcolors] >= $ncolors} {
-           set badcolors {}
+       if {[info exists colormap($x)]
+           && [lsearch -exact $badcolors $colormap($x)] < 0} {
+           lappend badcolors $colormap($x)
        }
     }
-    set origbad $badcolors
-    if {[llength $badcolors] < $ncolors - 1} {
-       if {[info exists crossings($id)]} {
-           foreach x $crossings($id) {
-               if {[info exists colormap($x)]
-                   && [lsearch -exact $badcolors $colormap($x)] < 0} {
-                   lappend badcolors $colormap($x)
-               }
-           }
-           if {[llength $badcolors] >= $ncolors} {
-               set badcolors $origbad
-           }
-       }
-       set origbad $badcolors
+    if {[llength $badcolors] >= $ncolors} {
+       set badcolors $origbad
     }
+    set origbad $badcolors
     if {[llength $badcolors] < $ncolors - 1} {
        foreach child $kids {
            if {[info exists colormap($child)]
                && [lsearch -exact $badcolors $colormap($child)] < 0} {
                lappend badcolors $colormap($child)
            }
-           foreach p [lindex $parentlist $commitrow($child)] {
+           foreach p [lindex $parentlist $commitrow($curview,$child)] {
                if {[info exists colormap($p)]
                    && [lsearch -exact $badcolors $colormap($p)] < 0} {
                    lappend badcolors $colormap($p)
@@ -1648,7 +3138,7 @@ proc bindline {t id} {
 proc drawtags {id x xt y1} {
     global idtags idheads idotherrefs
     global linespc lthickness
-    global canv mainfont commitrow rowtextx
+    global canv mainfont commitrow rowtextx curview
 
     set marks {}
     set ntags 0
@@ -1691,7 +3181,7 @@ proc drawtags {id x xt y1} {
                       $xr $yt $xr $yb $xl $yb $x [expr {$yb - $delta}] \
                       -width 1 -outline black -fill yellow -tags tag.$id]
            $canv bind $t <1> [list showtag $tag 1]
-           set rowtextx($commitrow($id)) [expr {$xr + $linespc}]
+           set rowtextx($commitrow($curview,$id)) [expr {$xr + $linespc}]
        } else {
            # draw a head or other ref
            if {[incr nheads -1] >= 0} {
@@ -1702,6 +3192,14 @@ proc drawtags {id x xt y1} {
            set xl [expr {$xl - $delta/2}]
            $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
                -width 1 -outline black -fill $col -tags tag.$id
+           if {[regexp {^(remotes/.*/|remotes/)} $tag match remoteprefix]} {
+               set rwid [font measure $mainfont $remoteprefix]
+               set xi [expr {$x + 1}]
+               set yti [expr {$yt + 1}]
+               set xri [expr {$x + $rwid}]
+               $canv create polygon $xi $yti $xri $yti $xri $yb $xi $yb \
+                       -width 0 -fill "#ffddaa" -tags tag.$id
+           }
        }
        set t [$canv create text $xl $y1 -anchor w -text $tag \
                   -font $mainfont -tags tag.$id]
@@ -1712,55 +3210,6 @@ proc drawtags {id x xt y1} {
     return $xt
 }
 
-proc checkcrossings {row endrow} {
-    global displayorder parentlist rowidlist
-
-    for {} {$row < $endrow} {incr row} {
-       set id [lindex $displayorder $row]
-       set i [lsearch -exact [lindex $rowidlist $row] $id]
-       if {$i < 0} continue
-       set idlist [lindex $rowidlist [expr {$row+1}]]
-       foreach p [lindex $parentlist $row] {
-           set j [lsearch -exact $idlist $p]
-           if {$j > 0} {
-               if {$j < $i - 1} {
-                   notecrossings $row $p $j $i [expr {$j+1}]
-               } elseif {$j > $i + 1} {
-                   notecrossings $row $p $i $j [expr {$j-1}]
-               }
-           }
-       }
-    }
-}
-
-proc notecrossings {row id lo hi corner} {
-    global rowidlist crossings cornercrossings
-
-    for {set i $lo} {[incr i] < $hi} {} {
-       set p [lindex [lindex $rowidlist $row] $i]
-       if {$p == {}} continue
-       if {$i == $corner} {
-           if {![info exists cornercrossings($id)]
-               || [lsearch -exact $cornercrossings($id) $p] < 0} {
-               lappend cornercrossings($id) $p
-           }
-           if {![info exists cornercrossings($p)]
-               || [lsearch -exact $cornercrossings($p) $id] < 0} {
-               lappend cornercrossings($p) $id
-           }
-       } else {
-           if {![info exists crossings($id)]
-               || [lsearch -exact $crossings($id) $p] < 0} {
-               lappend crossings($id) $p
-           }
-           if {![info exists crossings($p)]
-               || [lsearch -exact $crossings($p) $id] < 0} {
-               lappend crossings($p) $id
-           }
-       }
-    }
-}
-
 proc xcoord {i level ln} {
     global canvx0 xspc1 xspc2
 
@@ -1773,23 +3222,25 @@ proc xcoord {i level ln} {
     return $x
 }
 
+proc show_status {msg} {
+    global canv mainfont
+
+    clear_display
+    $canv create text 3 3 -anchor nw -text $msg -font $mainfont -tags textitems
+}
+
 proc finishcommits {} {
-    global commitidx phase
+    global commitidx phase curview
     global canv mainfont ctext maincursor textcursor
-    global findinprogress
+    global findinprogress pending_select
 
-    if {$commitidx > 0} {
+    if {$commitidx($curview) > 0} {
        drawrest
     } else {
-       $canv delete all
-       $canv create text 3 3 -anchor nw -text "No commits selected" \
-           -font $mainfont -tags textitems
-    }
-    if {![info exists findinprogress]} {
-       . config -cursor $maincursor
-       settextcursor $textcursor
+       show_status "No commits selected"
     }
     set phase {}
+    catch {unset pending_select}
 }
 
 # Don't change the text pane cursor if it is currently the hand cursor,
@@ -1803,17 +3254,41 @@ proc settextcursor {c} {
     set curtextcursor $c
 }
 
+proc nowbusy {what} {
+    global isbusy
+
+    if {[array names isbusy] eq {}} {
+       . config -cursor watch
+       settextcursor watch
+    }
+    set isbusy($what) 1
+}
+
+proc notbusy {what} {
+    global isbusy maincursor textcursor
+
+    catch {unset isbusy($what)}
+    if {[array names isbusy] eq {}} {
+       . config -cursor $maincursor
+       settextcursor $textcursor
+    }
+}
+
 proc drawrest {} {
     global numcommits
     global startmsecs
     global canvy0 numcommits linespc
-    global rowlaidout commitidx
+    global rowlaidout commitidx curview
+    global pending_select
 
     set row $rowlaidout
-    layoutrows $rowlaidout $commitidx 1
+    layoutrows $rowlaidout $commitidx($curview) 1
     layouttail
-    optimize_rows $row 0 $commitidx
-    showstuff $commitidx
+    optimize_rows $row 0 $commitidx($curview)
+    showstuff $commitidx($curview)
+    if {[info exists pending_select]} {
+       selectline 0 1
+    }
 
     set drawmsecs [expr {[clock clicks -milliseconds] - $startmsecs}]
     #puts "overall $drawmsecs ms for $numcommits commits"
@@ -1842,18 +3317,15 @@ proc findmatches {f} {
 proc dofind {} {
     global findtype findloc findstring markedmatches commitinfo
     global numcommits displayorder linehtag linentag linedtag
-    global mainfont namefont canv canv2 canv3 selectedline
+    global mainfont canv canv2 canv3 selectedline
     global matchinglines foundstring foundstrlen matchstring
     global commitdata
 
     stopfindproc
     unmarkmatches
+    cancel_next_highlight
     focus .
     set matchinglines {}
-    if {$findloc == "Pickaxe"} {
-       findpatches
-       return
-    }
     if {$findtype == "IgnCase"} {
        set foundstring [string tolower $findstring]
     } else {
@@ -1863,17 +3335,13 @@ proc dofind {} {
     if {$foundstrlen == 0} return
     regsub -all {[*?\[\\]} $foundstring {\\&} matchstring
     set matchstring "*$matchstring*"
-    if {$findloc == "Files"} {
-       findfiles
-       return
-    }
     if {![info exists selectedline]} {
        set oldsel -1
     } else {
        set oldsel $selectedline
     }
     set didsel 0
-    set fldtypes {Headline Author Date Committer CDate Comment}
+    set fldtypes {Headline Author Date Committer CDate Comments}
     set l -1
     foreach id $displayorder {
        set d $commitdata($id)
@@ -1903,7 +3371,7 @@ proc dofind {} {
                markmatches $canv $l $f $linehtag($l) $matches $mainfont
            } elseif {$ty == "Author"} {
                drawcmitrow $l
-               markmatches $canv2 $l $f $linentag($l) $matches $namefont
+               markmatches $canv2 $l $f $linentag($l) $matches $mainfont
            } elseif {$ty == "Date"} {
                drawcmitrow $l
                markmatches $canv3 $l $f $linedtag($l) $matches $mainfont
@@ -1976,281 +3444,21 @@ proc findprev {} {
     }
 }
 
-proc findlocchange {name ix op} {
-    global findloc findtype findtypemenu
-    if {$findloc == "Pickaxe"} {
-       set findtype Exact
-       set state disabled
-    } else {
-       set state normal
-    }
-    $findtypemenu entryconf 1 -state $state
-    $findtypemenu entryconf 2 -state $state
-}
-
-proc stopfindproc {{done 0}} {
-    global findprocpid findprocfile findids
-    global ctext findoldcursor phase maincursor textcursor
-    global findinprogress
-
-    catch {unset findids}
-    if {[info exists findprocpid]} {
-       if {!$done} {
-           catch {exec kill $findprocpid}
-       }
-       catch {close $findprocfile}
-       unset findprocpid
-    }
-    if {[info exists findinprogress]} {
-       unset findinprogress
-       if {$phase != "incrdraw"} {
-           . config -cursor $maincursor
-           settextcursor $textcursor
-       }
-    }
-}
-
-proc findpatches {} {
-    global findstring selectedline numcommits
-    global findprocpid findprocfile
-    global finddidsel ctext displayorder findinprogress
-    global findinsertpos
-
-    if {$numcommits == 0} return
-
-    # make a list of all the ids to search, starting at the one
-    # after the selected line (if any)
-    if {[info exists selectedline]} {
-       set l $selectedline
-    } else {
-       set l -1
-    }
-    set inputids {}
-    for {set i 0} {$i < $numcommits} {incr i} {
-       if {[incr l] >= $numcommits} {
-           set l 0
-       }
-       append inputids [lindex $displayorder $l] "\n"
-    }
-
-    if {[catch {
-       set f [open [list | git-diff-tree --stdin -s -r -S$findstring \
-                        << $inputids] r]
-    } err]} {
-       error_popup "Error starting search process: $err"
-       return
-    }
-
-    set findinsertpos end
-    set findprocfile $f
-    set findprocpid [pid $f]
-    fconfigure $f -blocking 0
-    fileevent $f readable readfindproc
-    set finddidsel 0
-    . config -cursor watch
-    settextcursor watch
-    set findinprogress 1
-}
-
-proc readfindproc {} {
-    global findprocfile finddidsel
-    global commitrow matchinglines findinsertpos
-
-    set n [gets $findprocfile line]
-    if {$n < 0} {
-       if {[eof $findprocfile]} {
-           stopfindproc 1
-           if {!$finddidsel} {
-               bell
-           }
-       }
-       return
-    }
-    if {![regexp {^[0-9a-f]{40}} $line id]} {
-       error_popup "Can't parse git-diff-tree output: $line"
-       stopfindproc
-       return
-    }
-    if {![info exists commitrow($id)]} {
-       puts stderr "spurious id: $id"
-       return
-    }
-    set l $commitrow($id)
-    insertmatch $l $id
-}
-
-proc insertmatch {l id} {
-    global matchinglines findinsertpos finddidsel
-
-    if {$findinsertpos == "end"} {
-       if {$matchinglines != {} && $l < [lindex $matchinglines 0]} {
-           set matchinglines [linsert $matchinglines 0 $l]
-           set findinsertpos 1
-       } else {
-           lappend matchinglines $l
-       }
-    } else {
-       set matchinglines [linsert $matchinglines $findinsertpos $l]
-       incr findinsertpos
-    }
-    markheadline $l $id
-    if {!$finddidsel} {
-       findselectline $l
-       set finddidsel 1
-    }
-}
-
-proc findfiles {} {
-    global selectedline numcommits displayorder ctext
-    global ffileline finddidsel parentlist
-    global findinprogress findstartline findinsertpos
-    global treediffs fdiffid fdiffsneeded fdiffpos
-    global findmergefiles
-
-    if {$numcommits == 0} return
-
-    if {[info exists selectedline]} {
-       set l [expr {$selectedline + 1}]
-    } else {
-       set l 0
-    }
-    set ffileline $l
-    set findstartline $l
-    set diffsneeded {}
-    set fdiffsneeded {}
-    while 1 {
-       set id [lindex $displayorder $l]
-       if {$findmergefiles || [llength [lindex $parentlist $l]] == 1} {
-           if {![info exists treediffs($id)]} {
-               append diffsneeded "$id\n"
-               lappend fdiffsneeded $id
-           }
-       }
-       if {[incr l] >= $numcommits} {
-           set l 0
-       }
-       if {$l == $findstartline} break
-    }
-
-    # start off a git-diff-tree process if needed
-    if {$diffsneeded ne {}} {
-       if {[catch {
-           set df [open [list | git-diff-tree -r --stdin << $diffsneeded] r]
-       } err ]} {
-           error_popup "Error starting search process: $err"
-           return
-       }
-       catch {unset fdiffid}
-       set fdiffpos 0
-       fconfigure $df -blocking 0
-       fileevent $df readable [list readfilediffs $df]
-    }
-
-    set finddidsel 0
-    set findinsertpos end
-    set id [lindex $displayorder $l]
-    . config -cursor watch
-    settextcursor watch
-    set findinprogress 1
-    findcont
-    update
-}
-
-proc readfilediffs {df} {
-    global findid fdiffid fdiffs
-
-    set n [gets $df line]
-    if {$n < 0} {
-       if {[eof $df]} {
-           donefilediff
-           if {[catch {close $df} err]} {
-               stopfindproc
-               bell
-               error_popup "Error in git-diff-tree: $err"
-           } elseif {[info exists findid]} {
-               set id $findid
-               stopfindproc
-               bell
-               error_popup "Couldn't find diffs for $id"
-           }
-       }
-       return
-    }
-    if {[regexp {^([0-9a-f]{40})$} $line match id]} {
-       # start of a new string of diffs
-       donefilediff
-       set fdiffid $id
-       set fdiffs {}
-    } elseif {[string match ":*" $line]} {
-       lappend fdiffs [lindex $line 5]
-    }
-}
-
-proc donefilediff {} {
-    global fdiffid fdiffs treediffs findid
-    global fdiffsneeded fdiffpos
-
-    if {[info exists fdiffid]} {
-       while {[lindex $fdiffsneeded $fdiffpos] ne $fdiffid
-              && $fdiffpos < [llength $fdiffsneeded]} {
-           # git-diff-tree doesn't output anything for a commit
-           # which doesn't change anything
-           set nullid [lindex $fdiffsneeded $fdiffpos]
-           set treediffs($nullid) {}
-           if {[info exists findid] && $nullid eq $findid} {
-               unset findid
-               findcont
-           }
-           incr fdiffpos
-       }
-       incr fdiffpos
-
-       if {![info exists treediffs($fdiffid)]} {
-           set treediffs($fdiffid) $fdiffs
-       }
-       if {[info exists findid] && $fdiffid eq $findid} {
-           unset findid
-           findcont
-       }
-    }
-}
-
-proc findcont {} {
-    global findid treediffs parentlist
-    global ffileline findstartline finddidsel
-    global displayorder numcommits matchinglines findinprogress
-    global findmergefiles
+proc stopfindproc {{done 0}} {
+    global findprocpid findprocfile findids
+    global ctext findoldcursor phase maincursor textcursor
+    global findinprogress
 
-    set l $ffileline
-    while {1} {
-       set id [lindex $displayorder $l]
-       if {$findmergefiles || [llength [lindex $parentlist $l]] == 1} {
-           if {![info exists treediffs($id)]} {
-               set findid $id
-               set ffileline $l
-               return
-           }
-           set doesmatch 0
-           foreach f $treediffs($id) {
-               set x [findmatches $f]
-               if {$x != {}} {
-                   set doesmatch 1
-                   break
-               }
-           }
-           if {$doesmatch} {
-               insertmatch $l $id
-           }
-       }
-       if {[incr l] >= $numcommits} {
-           set l 0
+    catch {unset findids}
+    if {[info exists findprocpid]} {
+       if {!$done} {
+           catch {exec kill $findprocpid}
        }
-       if {$l == $findstartline} break
-    }
-    stopfindproc
-    if {!$finddidsel} {
-       bell
+       catch {close $findprocfile}
+       unset findprocpid
     }
+    catch {unset findinprogress}
+    notbusy find
 }
 
 # mark a commit as matching by putting a yellow background
@@ -2310,31 +3518,34 @@ proc selcanvline {w x y} {
 
 proc commit_descriptor {p} {
     global commitinfo
+    if {![info exists commitinfo($p)]} {
+       getcommit $p
+    }
     set l "..."
-    if {[info exists commitinfo($p)]} {
+    if {[llength $commitinfo($p)] > 1} {
        set l [lindex $commitinfo($p) 0]
     }
-    return "$p ($l)"
+    return "$p ($l)\n"
 }
 
 # append some text to the ctext widget, and make any SHA1 ID
 # that we know about be a clickable link.
-proc appendwithlinks {text} {
-    global ctext commitrow linknum
+proc appendwithlinks {text tags} {
+    global ctext commitrow linknum curview
 
     set start [$ctext index "end - 1c"]
-    $ctext insert end $text
-    $ctext insert end "\n"
+    $ctext insert end $text $tags
     set links [regexp -indices -all -inline {[0-9a-f]{40}} $text]
     foreach l $links {
        set s [lindex $l 0]
        set e [lindex $l 1]
        set linkid [string range $text $s $e]
-       if {![info exists commitrow($linkid)]} continue
+       if {![info exists commitrow($curview,$linkid)]} continue
        incr e
        $ctext tag add link "$start + $s c" "$start + $e c"
        $ctext tag add link$linknum "$start + $s c" "$start + $e c"
-       $ctext tag bind link$linknum <1> [list selectline $commitrow($linkid) 1]
+       $ctext tag bind link$linknum <1> \
+           [list selectline $commitrow($curview,$linkid) 1]
        incr linknum
     }
     $ctext tag conf link -foreground blue -underline 1
@@ -2358,16 +3569,77 @@ proc viewnextline {dir} {
     allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
 }
 
+# add a list of tag or branch names at position pos
+# returns the number of names inserted
+proc appendrefs {pos l var} {
+    global ctext commitrow linknum curview idtags $var
+
+    if {[catch {$ctext index $pos}]} {
+       return 0
+    }
+    set tags {}
+    foreach id $l {
+       foreach tag [set $var\($id\)] {
+           lappend tags [concat $tag $id]
+       }
+    }
+    set tags [lsort -index 1 $tags]
+    set sep {}
+    foreach tag $tags {
+       set name [lindex $tag 0]
+       set id [lindex $tag 1]
+       set lk link$linknum
+       incr linknum
+       $ctext insert $pos $sep
+       $ctext insert $pos $name $lk
+       $ctext tag conf $lk -foreground blue
+       if {[info exists commitrow($curview,$id)]} {
+           $ctext tag bind $lk <1> \
+               [list selectline $commitrow($curview,$id) 1]
+           $ctext tag conf $lk -underline 1
+           $ctext tag bind $lk <Enter> { %W configure -cursor hand2 }
+           $ctext tag bind $lk <Leave> { %W configure -cursor $curtextcursor }
+       }
+       set sep ", "
+    }
+    return [llength $tags]
+}
+
+# called when we have finished computing the nearby tags
+proc dispneartags {} {
+    global selectedline currentid ctext anc_tags desc_tags showneartags
+    global desc_heads
+
+    if {![info exists selectedline] || !$showneartags} return
+    set id $currentid
+    $ctext conf -state normal
+    if {[info exists desc_heads($id)]} {
+       if {[appendrefs branch $desc_heads($id) idheads] > 1} {
+           $ctext insert "branch -2c" "es"
+       }
+    }
+    if {[info exists anc_tags($id)]} {
+       appendrefs follows $anc_tags($id) idtags
+    }
+    if {[info exists desc_tags($id)]} {
+       appendrefs precedes $desc_tags($id) idtags
+    }
+    $ctext conf -state disabled
+}
+
 proc selectline {l isnew} {
     global canv canv2 canv3 ctext commitinfo selectedline
     global displayorder linehtag linentag linedtag
     global canvy0 linespc parentlist childlist
-    global cflist currentid sha1entry
+    global currentid sha1entry
     global commentend idtags linknum
-    global mergemax numcommits
+    global mergemax numcommits pending_select
+    global cmitmode desc_tags anc_tags showneartags allcommits desc_heads
 
+    catch {unset pending_select}
     $canv delete hover
     normalline
+    cancel_next_highlight
     if {$l < 0 || $l >= $numcommits} return
     set y [expr {$canvy0 + $l * $linespc}]
     set ymax [lindex [$canv cget -scrollregion] 3]
@@ -2431,12 +3703,11 @@ proc selectline {l isnew} {
     $sha1entry insert 0 $id
     $sha1entry selection from 0
     $sha1entry selection to end
+    rhighlight_sel $id
 
     $ctext conf -state normal
-    $ctext delete 0.0 end
+    clear_ctext
     set linknum 0
-    $ctext mark set fmark.0 0.0
-    $ctext mark gravity fmark.0 left
     set info $commitinfo($id)
     set date [formatdate [lindex $info 2]]
     $ctext insert end "Author: [lindex $info 1]  $date\n"
@@ -2450,7 +3721,7 @@ proc selectline {l isnew} {
        $ctext insert end "\n"
     }
  
-    set comment {}
+    set headers {}
     set olds [lindex $parentlist $l]
     if {[llength $olds] > 1} {
        set np 0
@@ -2461,32 +3732,60 @@ proc selectline {l isnew} {
                set tag m$np
            }
            $ctext insert end "Parent: " $tag
-           appendwithlinks [commit_descriptor $p]
+           appendwithlinks [commit_descriptor $p] {}
            incr np
        }
     } else {
        foreach p $olds {
-           append comment "Parent: [commit_descriptor $p]\n"
+           append headers "Parent: [commit_descriptor $p]"
        }
     }
 
     foreach c [lindex $childlist $l] {
-       append comment "Child:  [commit_descriptor $c]\n"
+       append headers "Child:  [commit_descriptor $c]"
     }
-    append comment "\n"
-    append comment [lindex $info 5]
 
     # make anything that looks like a SHA1 ID be a clickable link
-    appendwithlinks $comment
+    appendwithlinks $headers {}
+    if {$showneartags} {
+       if {![info exists allcommits]} {
+           getallcommits
+       }
+       $ctext insert end "Branch: "
+       $ctext mark set branch "end -1c"
+       $ctext mark gravity branch left
+       if {[info exists desc_heads($id)]} {
+           if {[appendrefs branch $desc_heads($id) idheads] > 1} {
+               # turn "Branch" into "Branches"
+               $ctext insert "branch -2c" "es"
+           }
+       }
+       $ctext insert end "\nFollows: "
+       $ctext mark set follows "end -1c"
+       $ctext mark gravity follows left
+       if {[info exists anc_tags($id)]} {
+           appendrefs follows $anc_tags($id) idtags
+       }
+       $ctext insert end "\nPrecedes: "
+       $ctext mark set precedes "end -1c"
+       $ctext mark gravity precedes left
+       if {[info exists desc_tags($id)]} {
+           appendrefs precedes $desc_tags($id) idtags
+       }
+       $ctext insert end "\n"
+    }
+    $ctext insert end "\n"
+    appendwithlinks [lindex $info 5] {comment}
 
     $ctext tag delete Comments
     $ctext tag remove found 1.0 end
     $ctext conf -state disabled
     set commentend [$ctext index "end - 1c"]
 
-    $cflist delete 0 end
-    $cflist insert end "Comments"
-    if {[llength $olds] <= 1} {
+    init_flist "Comments"
+    if {$cmitmode eq "tree"} {
+       gettree $id
+    } elseif {[llength $olds] <= 1} {
        startdiff $id
     } else {
        mergediff $id $l
@@ -2521,6 +3820,7 @@ proc selnextpage {dir} {
        set lpp 1
     }
     allcanvs yview scroll [expr {$dir * $lpp}] units
+    drawvisible
     if {![info exists selectedline]} return
     set l [expr {$selectedline + $dir * $lpp}]
     if {$l < 0} {
@@ -2533,24 +3833,36 @@ proc selnextpage {dir} {
 }
 
 proc unselectline {} {
-    global selectedline
+    global selectedline currentid
 
     catch {unset selectedline}
+    catch {unset currentid}
     allcanvs delete secsel
+    rhighlight_none
+    cancel_next_highlight
+}
+
+proc reselectline {} {
+    global selectedline
+
+    if {[info exists selectedline]} {
+       selectline $selectedline 0
+    }
 }
 
 proc addtohistory {cmd} {
-    global history historyindex
+    global history historyindex curview
 
+    set elt [list $curview $cmd]
     if {$historyindex > 0
-       && [lindex $history [expr {$historyindex - 1}]] == $cmd} {
+       && [lindex $history [expr {$historyindex - 1}]] == $elt} {
        return
     }
 
     if {$historyindex < [llength $history]} {
-       set history [lreplace $history $historyindex end $cmd]
+       set history [lreplace $history $historyindex end $elt]
     } else {
-       lappend history $cmd
+       lappend history $elt
     }
     incr historyindex
     if {$historyindex > 1} {
@@ -2561,13 +3873,23 @@ proc addtohistory {cmd} {
     .ctop.top.bar.rightbut conf -state disabled
 }
 
+proc godo {elt} {
+    global curview
+
+    set view [lindex $elt 0]
+    set cmd [lindex $elt 1]
+    if {$curview != $view} {
+       showview $view
+    }
+    eval $cmd
+}
+
 proc goback {} {
     global history historyindex
 
     if {$historyindex > 1} {
        incr historyindex -1
-       set cmd [lindex $history [expr {$historyindex - 1}]]
-       eval $cmd
+       godo [lindex $history [expr {$historyindex - 1}]]
        .ctop.top.bar.rightbut conf -state normal
     }
     if {$historyindex <= 1} {
@@ -2581,7 +3903,7 @@ proc goforw {} {
     if {$historyindex < [llength $history]} {
        set cmd [lindex $history $historyindex]
        incr historyindex
-       eval $cmd
+       godo $cmd
        .ctop.top.bar.leftbut conf -state normal
     }
     if {$historyindex >= [llength $history]} {
@@ -2589,17 +3911,104 @@ proc goforw {} {
     }
 }
 
+proc gettree {id} {
+    global treefilelist treeidlist diffids diffmergeid treepending
+
+    set diffids $id
+    catch {unset diffmergeid}
+    if {![info exists treefilelist($id)]} {
+       if {![info exists treepending]} {
+           if {[catch {set gtf [open [concat | git ls-tree -r $id] r]}]} {
+               return
+           }
+           set treepending $id
+           set treefilelist($id) {}
+           set treeidlist($id) {}
+           fconfigure $gtf -blocking 0
+           fileevent $gtf readable [list gettreeline $gtf $id]
+       }
+    } else {
+       setfilelist $id
+    }
+}
+
+proc gettreeline {gtf id} {
+    global treefilelist treeidlist treepending cmitmode diffids
+
+    while {[gets $gtf line] >= 0} {
+       if {[lindex $line 1] ne "blob"} continue
+       set sha1 [lindex $line 2]
+       set fname [lindex $line 3]
+       lappend treefilelist($id) $fname
+       lappend treeidlist($id) $sha1
+    }
+    if {![eof $gtf]} return
+    close $gtf
+    unset treepending
+    if {$cmitmode ne "tree"} {
+       if {![info exists diffmergeid]} {
+           gettreediffs $diffids
+       }
+    } elseif {$id ne $diffids} {
+       gettree $diffids
+    } else {
+       setfilelist $id
+    }
+}
+
+proc showfile {f} {
+    global treefilelist treeidlist diffids
+    global ctext commentend
+
+    set i [lsearch -exact $treefilelist($diffids) $f]
+    if {$i < 0} {
+       puts "oops, $f not in list for id $diffids"
+       return
+    }
+    set blob [lindex $treeidlist($diffids) $i]
+    if {[catch {set bf [open [concat | git cat-file blob $blob] r]} err]} {
+       puts "oops, error reading blob $blob: $err"
+       return
+    }
+    fconfigure $bf -blocking 0
+    fileevent $bf readable [list getblobline $bf $diffids]
+    $ctext config -state normal
+    clear_ctext $commentend
+    $ctext insert end "\n"
+    $ctext insert end "$f\n" filesep
+    $ctext config -state disabled
+    $ctext yview $commentend
+}
+
+proc getblobline {bf id} {
+    global diffids cmitmode ctext
+
+    if {$id ne $diffids || $cmitmode ne "tree"} {
+       catch {close $bf}
+       return
+    }
+    $ctext config -state normal
+    while {[gets $bf line] >= 0} {
+       $ctext insert end "$line\n"
+    }
+    if {[eof $bf]} {
+       # delete last newline
+       $ctext delete "end - 2c" "end - 1c"
+       close $bf
+    }
+    $ctext config -state disabled
+}
+
 proc mergediff {id l} {
     global diffmergeid diffopts mdifffd
-    global difffilestart diffids
+    global diffids
     global parentlist
 
     set diffmergeid $id
     set diffids $id
-    catch {unset difffilestart}
     # this doesn't seem to actually affect anything...
     set env(GIT_DIFF_OPTS) $diffopts
-    set cmd [concat | git-diff-tree --no-commit-id --cc $id]
+    set cmd [concat | git diff-tree --no-commit-id --cc $id]
     if {[catch {set mdf [open $cmd r]} err]} {
        error_popup "Error getting merge diffs: $err"
        return
@@ -2631,11 +4040,8 @@ proc getmergediffline {mdf id np} {
        # start of a new file
        $ctext insert end "\n"
        set here [$ctext index "end - 1c"]
-       set i [$cflist index end]
-       $ctext mark set fmark.$i $here
-       $ctext mark gravity fmark.$i left
-       set difffilestart([expr {$i-1}]) $here
-       $cflist insert end $fname
+       lappend difffilestart $here
+       add_flist [list $fname]
        set l [expr {(78 - [string length $fname]) / 2}]
        set pad [string range "----------------------------------------" 1 $l]
        $ctext insert end "$pad $fname $pad\n" filesep
@@ -2705,9 +4111,7 @@ proc startdiff {ids} {
 
 proc addtocflist {ids} {
     global treediffs cflist
-    foreach f $treediffs($ids) {
-       $cflist insert end $f
-    }
+    add_flist $treediffs($ids)
     getblobdiffs $ids
 }
 
@@ -2716,7 +4120,7 @@ proc gettreediffs {ids} {
     set treepending $ids
     set treediff {}
     if {[catch \
-        {set gdtf [open [concat | git-diff-tree --no-commit-id -r $ids] r]} \
+        {set gdtf [open [concat | git diff-tree --no-commit-id -r $ids] r]} \
        ]} return
     fconfigure $gdtf -blocking 0
     fileevent $gdtf readable [list gettreediffline $gdtf $ids]
@@ -2724,6 +4128,7 @@ proc gettreediffs {ids} {
 
 proc gettreediffline {gdtf ids} {
     global treediff treediffs treepending diffids diffmergeid
+    global cmitmode
 
     set n [gets $gdtf line]
     if {$n < 0} {
@@ -2731,7 +4136,9 @@ proc gettreediffline {gdtf ids} {
        close $gdtf
        set treediffs($ids) $treediff
        unset treepending
-       if {$ids != $diffids} {
+       if {$cmitmode eq "tree"} {
+           gettree $diffids
+       } elseif {$ids != $diffids} {
            if {![info exists diffmergeid]} {
                gettreediffs $diffids
            }
@@ -2746,10 +4153,10 @@ proc gettreediffline {gdtf ids} {
 
 proc getblobdiffs {ids} {
     global diffopts blobdifffd diffids env curdifftag curtagstart
-    global difffilestart nextupdate diffinhdr treediffs
+    global nextupdate diffinhdr treediffs
 
     set env(GIT_DIFF_OPTS) $diffopts
-    set cmd [concat | git-diff-tree --no-commit-id -r -p -C $ids]
+    set cmd [concat | git diff-tree --no-commit-id -r -p -C $ids]
     if {[catch {set bdf [open $cmd r]} err]} {
        puts "error getting diffs: $err"
        return
@@ -2759,11 +4166,23 @@ proc getblobdiffs {ids} {
     set blobdifffd($ids) $bdf
     set curdifftag Comments
     set curtagstart 0.0
-    catch {unset difffilestart}
     fileevent $bdf readable [list getblobdiffline $bdf $diffids]
     set nextupdate [expr {[clock clicks -milliseconds] + 100}]
 }
 
+proc setinlist {var i val} {
+    global $var
+
+    while {[llength [set $var]] < $i} {
+       lappend $var {}
+    }
+    if {[llength [set $var]] == $i} {
+       lappend $var $val
+    } else {
+       lset $var $i $val
+    }
+}
+
 proc getblobdiffline {bdf ids} {
     global diffids blobdifffd ctext curdifftag curtagstart
     global diffnexthead diffnextnote difffilestart
@@ -2787,23 +4206,17 @@ proc getblobdiffline {bdf ids} {
        # start of a new file
        $ctext insert end "\n"
        $ctext tag add $curdifftag $curtagstart end
-       set curtagstart [$ctext index "end - 1c"]
-       set header $newname
        set here [$ctext index "end - 1c"]
-       set i [lsearch -exact $treediffs($diffids) $fname]
+       set curtagstart $here
+       set header $newname
+       set i [lsearch -exact $treediffs($ids) $fname]
        if {$i >= 0} {
-           set difffilestart($i) $here
-           incr i
-           $ctext mark set fmark.$i $here
-           $ctext mark gravity fmark.$i left
+           setinlist difffilestart $i $here
        }
-       if {$newname != $fname} {
-           set i [lsearch -exact $treediffs($diffids) $newname]
+       if {$newname ne $fname} {
+           set i [lsearch -exact $treediffs($ids) $newname]
            if {$i >= 0} {
-               set difffilestart($i) $here
-               incr i
-               $ctext mark set fmark.$i $here
-               $ctext mark gravity fmark.$i left
+               setinlist difffilestart $i $here
            }
        }
        set curdifftag "f:$fname"
@@ -2853,26 +4266,143 @@ proc getblobdiffline {bdf ids} {
 proc nextfile {} {
     global difffilestart ctext
     set here [$ctext index @0,0]
-    for {set i 0} {[info exists difffilestart($i)]} {incr i} {
-       if {[$ctext compare $difffilestart($i) > $here]} {
-           if {![info exists pos]
-               || [$ctext compare $difffilestart($i) < $pos]} {
-               set pos $difffilestart($i)
-           }
+    foreach loc $difffilestart {
+       if {[$ctext compare $loc > $here]} {
+           $ctext yview $loc
+       }
+    }
+}
+
+proc clear_ctext {{first 1.0}} {
+    global ctext smarktop smarkbot
+
+    set l [lindex [split $first .] 0]
+    if {![info exists smarktop] || [$ctext compare $first < $smarktop.0]} {
+       set smarktop $l
+    }
+    if {![info exists smarkbot] || [$ctext compare $first < $smarkbot.0]} {
+       set smarkbot $l
+    }
+    $ctext delete $first end
+}
+
+proc incrsearch {name ix op} {
+    global ctext searchstring searchdirn
+
+    $ctext tag remove found 1.0 end
+    if {[catch {$ctext index anchor}]} {
+       # no anchor set, use start of selection, or of visible area
+       set sel [$ctext tag ranges sel]
+       if {$sel ne {}} {
+           $ctext mark set anchor [lindex $sel 0]
+       } elseif {$searchdirn eq "-forwards"} {
+           $ctext mark set anchor @0,0
+       } else {
+           $ctext mark set anchor @0,[winfo height $ctext]
        }
     }
-    if {[info exists pos]} {
-       $ctext yview $pos
+    if {$searchstring ne {}} {
+       set here [$ctext search $searchdirn -- $searchstring anchor]
+       if {$here ne {}} {
+           $ctext see $here
+       }
+       searchmarkvisible 1
     }
 }
 
-proc listboxsel {} {
-    global ctext cflist currentid
-    if {![info exists currentid]} return
-    set sel [lsort [$cflist curselection]]
-    if {$sel eq {}} return
-    set first [lindex $sel 0]
-    catch {$ctext yview fmark.$first}
+proc dosearch {} {
+    global sstring ctext searchstring searchdirn
+
+    focus $sstring
+    $sstring icursor end
+    set searchdirn -forwards
+    if {$searchstring ne {}} {
+       set sel [$ctext tag ranges sel]
+       if {$sel ne {}} {
+           set start "[lindex $sel 0] + 1c"
+       } elseif {[catch {set start [$ctext index anchor]}]} {
+           set start "@0,0"
+       }
+       set match [$ctext search -count mlen -- $searchstring $start]
+       $ctext tag remove sel 1.0 end
+       if {$match eq {}} {
+           bell
+           return
+       }
+       $ctext see $match
+       set mend "$match + $mlen c"
+       $ctext tag add sel $match $mend
+       $ctext mark unset anchor
+    }
+}
+
+proc dosearchback {} {
+    global sstring ctext searchstring searchdirn
+
+    focus $sstring
+    $sstring icursor end
+    set searchdirn -backwards
+    if {$searchstring ne {}} {
+       set sel [$ctext tag ranges sel]
+       if {$sel ne {}} {
+           set start [lindex $sel 0]
+       } elseif {[catch {set start [$ctext index anchor]}]} {
+           set start @0,[winfo height $ctext]
+       }
+       set match [$ctext search -backwards -count ml -- $searchstring $start]
+       $ctext tag remove sel 1.0 end
+       if {$match eq {}} {
+           bell
+           return
+       }
+       $ctext see $match
+       set mend "$match + $ml c"
+       $ctext tag add sel $match $mend
+       $ctext mark unset anchor
+    }
+}
+
+proc searchmark {first last} {
+    global ctext searchstring
+
+    set mend $first.0
+    while {1} {
+       set match [$ctext search -count mlen -- $searchstring $mend $last.end]
+       if {$match eq {}} break
+       set mend "$match + $mlen c"
+       $ctext tag add found $match $mend
+    }
+}
+
+proc searchmarkvisible {doall} {
+    global ctext smarktop smarkbot
+
+    set topline [lindex [split [$ctext index @0,0] .] 0]
+    set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
+    if {$doall || $botline < $smarktop || $topline > $smarkbot} {
+       # no overlap with previous
+       searchmark $topline $botline
+       set smarktop $topline
+       set smarkbot $botline
+    } else {
+       if {$topline < $smarktop} {
+           searchmark $topline [expr {$smarktop-1}]
+           set smarktop $topline
+       }
+       if {$botline > $smarkbot} {
+           searchmark [expr {$smarkbot+1}] $botline
+           set smarkbot $botline
+       }
+    }
+}
+
+proc scrolltext {f0 f1} {
+    global searchstring
+
+    .ctop.cdet.left.sb set $f0 $f1
+    if {$searchstring ne {}} {
+       searchmarkvisible 0
+    }
 }
 
 proc setcoords {} {
@@ -2905,11 +4435,10 @@ proc redisplay {} {
 }
 
 proc incrfont {inc} {
-    global mainfont namefont textfont ctext canv phase
+    global mainfont textfont ctext canv phase
     global stopped entries
     unmarkmatches
     set mainfont [lreplace $mainfont 1 1 [expr {[lindex $mainfont 1] + $inc}]]
-    set namefont [lreplace $namefont 1 1 [expr {[lindex $namefont 1] + $inc}]]
     set textfont [lreplace $textfont 1 1 [expr {[lindex $textfont 1] + $inc}]]
     setcoords
     $ctext conf -font $textfont
@@ -2917,7 +4446,7 @@ proc incrfont {inc} {
     foreach e $entries {
        $e conf -font $mainfont
     }
-    if {$phase == "getcommits"} {
+    if {$phase eq "getcommits"} {
        $canv itemconf textitems -font $mainfont
     }
     redisplay
@@ -2948,7 +4477,7 @@ proc sha1change {n1 n2 op} {
 
 proc gotocommit {} {
     global sha1string currentid commitrow tagids headids
-    global displayorder numcommits
+    global displayorder numcommits curview
 
     if {$sha1string == {}
        || ([info exists currentid] && $sha1string == $currentid)} return
@@ -2974,8 +4503,8 @@ proc gotocommit {} {
            }
        }
     }
-    if {[info exists commitrow($id)]} {
-       selectline $commitrow($id) 1
+    if {[info exists commitrow($curview,$id)]} {
+       selectline $commitrow($curview,$id) 1
        return
     }
     if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
@@ -3050,12 +4579,13 @@ proc linehover {} {
 }
 
 proc clickisonarrow {id y} {
-    global lthickness idrowranges
+    global lthickness
 
+    set ranges [rowranges $id]
     set thresh [expr {2 * $lthickness + 6}]
-    set n [expr {[llength $idrowranges($id)] - 1}]
+    set n [expr {[llength $ranges] - 1}]
     for {set i 1} {$i < $n} {incr i} {
-       set row [lindex $idrowranges($id) $i]
+       set row [lindex $ranges $i]
        if {abs([yc $row] - $y) < $thresh} {
            return $i
        }
@@ -3064,11 +4594,11 @@ proc clickisonarrow {id y} {
 }
 
 proc arrowjump {id n y} {
-    global idrowranges canv
+    global canv
 
     # 1 <-> 2, 3 <-> 4, etc...
     set n [expr {(($n - 1) ^ 1) + 1}]
-    set row [lindex $idrowranges($id) $n]
+    set row [lindex [rowranges $id] $n]
     set yt [yc $row]
     set ymax [lindex [$canv cget -scrollregion] 3]
     if {$ymax eq {} || $ymax <= 0} return
@@ -3082,7 +4612,7 @@ proc arrowjump {id n y} {
 }
 
 proc lineclick {x y id isnew} {
-    global ctext commitinfo childlist commitrow cflist canv thickerline
+    global ctext commitinfo children canv thickerline curview
 
     if {![info exists commitinfo($id)] && ![getcommit $id]} return
     unmarkmatches
@@ -3109,7 +4639,7 @@ proc lineclick {x y id isnew} {
     }
     # fill the details pane with info about this line
     $ctext conf -state normal
-    $ctext delete 0.0 end
+    clear_ctext
     $ctext tag conf link -foreground blue -underline 1
     $ctext tag bind link <Enter> { %W configure -cursor hand2 }
     $ctext tag bind link <Leave> { %W configure -cursor $curtextcursor }
@@ -3121,7 +4651,7 @@ proc lineclick {x y id isnew} {
     $ctext insert end "\tAuthor:\t[lindex $info 1]\n"
     set date [formatdate [lindex $info 2]]
     $ctext insert end "\tDate:\t$date\n"
-    set kids [lindex $childlist $commitrow($id)]
+    set kids $children($curview,$id)
     if {$kids ne {}} {
        $ctext insert end "\nChildren:"
        set i 0
@@ -3139,8 +4669,7 @@ proc lineclick {x y id isnew} {
        }
     }
     $ctext conf -state disabled
-
-    $cflist delete 0 end
+    init_flist {}
 }
 
 proc normalline {} {
@@ -3153,9 +4682,9 @@ proc normalline {} {
 }
 
 proc selbyid {id} {
-    global commitrow
-    if {[info exists commitrow($id)]} {
-       selectline $commitrow($id) 1
+    global commitrow curview
+    if {[info exists commitrow($curview,$id)]} {
+       selectline $commitrow($curview,$id) 1
     }
 }
 
@@ -3168,9 +4697,10 @@ proc mstime {} {
 }
 
 proc rowmenu {x y id} {
-    global rowctxmenu commitrow selectedline rowmenuid
+    global rowctxmenu commitrow selectedline rowmenuid curview
 
-    if {![info exists selectedline] || $commitrow($id) eq $selectedline} {
+    if {![info exists selectedline]
+       || $commitrow($curview,$id) eq $selectedline} {
        set state disabled
     } else {
        set state normal
@@ -3198,15 +4728,12 @@ proc diffvssel {dirn} {
 }
 
 proc doseldiff {oldid newid} {
-    global ctext cflist
+    global ctext
     global commitinfo
 
     $ctext conf -state normal
-    $ctext delete 0.0 end
-    $ctext mark set fmark.0 0.0
-    $ctext mark gravity fmark.0 left
-    $cflist delete 0 end
-    $cflist insert end "Top"
+    clear_ctext
+    init_flist "Top"
     $ctext insert end "From "
     $ctext tag conf link -foreground blue -underline 1
     $ctext tag bind link <Enter> { %W configure -cursor hand2 }
@@ -3298,7 +4825,7 @@ proc mkpatchgo {} {
     set oldid [$patchtop.fromsha1 get]
     set newid [$patchtop.tosha1 get]
     set fname [$patchtop.fname get]
-    if {[catch {exec git-diff-tree -p $oldid $newid >$fname &} err]} {
+    if {[catch {exec git diff-tree -p $oldid $newid >$fname &} err]} {
        error_popup "Error creating patch: $err"
     }
     catch {destroy $patchtop}
@@ -3373,14 +4900,22 @@ proc domktag {} {
 }
 
 proc redrawtags {id} {
-    global canv linehtag commitrow idpos selectedline
+    global canv linehtag commitrow idpos selectedline curview
+    global mainfont
 
-    if {![info exists commitrow($id)]} return
-    drawcmitrow $commitrow($id)
+    if {![info exists commitrow($curview,$id)]} return
+    drawcmitrow $commitrow($curview,$id)
     $canv delete tag.$id
     set xt [eval drawtags $id $idpos($id)]
-    $canv coords $linehtag($commitrow($id)) $xt [lindex $idpos($id) 2]
-    if {[info exists selectedline] && $selectedline == $commitrow($id)} {
+    $canv coords $linehtag($commitrow($curview,$id)) $xt [lindex $idpos($id) 2]
+    set text [$canv itemcget $linehtag($commitrow($curview,$id)) -text]
+    set xr [expr {$xt + [font measure $mainfont $text]}]
+    if {$xr > $canvxmax} {
+       set canvxmax $xr
+       setcanvscroll
+    }
+    if {[info exists selectedline]
+       && $selectedline == $commitrow($curview,$id)} {
        selectline $selectedline 0
     }
 }
@@ -3452,22 +4987,192 @@ proc wrcomcan {} {
     unset wrcomtop
 }
 
-proc listrefs {id} {
-    global idtags idheads idotherrefs
+# Stuff for finding nearby tags
+proc getallcommits {} {
+    global allcstart allcommits allcfd
 
-    set x {}
-    if {[info exists idtags($id)]} {
-       set x $idtags($id)
+    set fd [open [concat | git rev-list --all --topo-order --parents] r]
+    set allcfd $fd
+    fconfigure $fd -blocking 0
+    set allcommits "reading"
+    nowbusy allcommits
+    restartgetall $fd
+}
+
+proc discardallcommits {} {
+    global allparents allchildren allcommits allcfd
+    global desc_tags anc_tags alldtags tagisdesc allids desc_heads
+
+    if {![info exists allcommits]} return
+    if {$allcommits eq "reading"} {
+       catch {close $allcfd}
     }
-    set y {}
-    if {[info exists idheads($id)]} {
-       set y $idheads($id)
+    foreach v {allcommits allchildren allparents allids desc_tags anc_tags
+               alldtags tagisdesc desc_heads} {
+       catch {unset $v}
     }
-    set z {}
-    if {[info exists idotherrefs($id)]} {
-       set z $idotherrefs($id)
+}
+
+proc restartgetall {fd} {
+    global allcstart
+
+    fileevent $fd readable [list getallclines $fd]
+    set allcstart [clock clicks -milliseconds]
+}
+
+proc combine_dtags {l1 l2} {
+    global tagisdesc notfirstd
+
+    set res [lsort -unique [concat $l1 $l2]]
+    for {set i 0} {$i < [llength $res]} {incr i} {
+       set x [lindex $res $i]
+       for {set j [expr {$i+1}]} {$j < [llength $res]} {} {
+           set y [lindex $res $j]
+           if {[info exists tagisdesc($x,$y)]} {
+               if {$tagisdesc($x,$y) > 0} {
+                   # x is a descendent of y, exclude x
+                   set res [lreplace $res $i $i]
+                   incr i -1
+                   break
+               } else {
+                   # y is a descendent of x, exclude y
+                   set res [lreplace $res $j $j]
+               }
+           } else {
+               # no relation, keep going
+               incr j
+           }
+       }
     }
-    return [list $x $y $z]
+    return $res
+}
+
+proc combine_atags {l1 l2} {
+    global tagisdesc
+
+    set res [lsort -unique [concat $l1 $l2]]
+    for {set i 0} {$i < [llength $res]} {incr i} {
+       set x [lindex $res $i]
+       for {set j [expr {$i+1}]} {$j < [llength $res]} {} {
+           set y [lindex $res $j]
+           if {[info exists tagisdesc($x,$y)]} {
+               if {$tagisdesc($x,$y) < 0} {
+                   # x is an ancestor of y, exclude x
+                   set res [lreplace $res $i $i]
+                   incr i -1
+                   break
+               } else {
+                   # y is an ancestor of x, exclude y
+                   set res [lreplace $res $j $j]
+               }
+           } else {
+               # no relation, keep going
+               incr j
+           }
+       }
+    }
+    return $res
+}
+
+proc getallclines {fd} {
+    global allparents allchildren allcommits allcstart
+    global desc_tags anc_tags idtags alldtags tagisdesc allids
+    global desc_heads idheads
+
+    while {[gets $fd line] >= 0} {
+       set id [lindex $line 0]
+       lappend allids $id
+       set olds [lrange $line 1 end]
+       set allparents($id) $olds
+       if {![info exists allchildren($id)]} {
+           set allchildren($id) {}
+       }
+       foreach p $olds {
+           lappend allchildren($p) $id
+       }
+       # compute nearest tagged descendents as we go
+       # also compute descendent heads
+       set dtags {}
+       set dheads {}
+       foreach child $allchildren($id) {
+           if {[info exists idtags($child)]} {
+               set ctags [list $child]
+           } else {
+               set ctags $desc_tags($child)
+           }
+           if {$dtags eq {}} {
+               set dtags $ctags
+           } elseif {$ctags ne $dtags} {
+               set dtags [combine_dtags $dtags $ctags]
+           }
+           set cheads $desc_heads($child)
+           if {$dheads eq {}} {
+               set dheads $cheads
+           } elseif {$cheads ne $dheads} {
+               set dheads [lsort -unique [concat $dheads $cheads]]
+           }
+       }
+       set desc_tags($id) $dtags
+       if {[info exists idtags($id)]} {
+           set adt $dtags
+           foreach tag $dtags {
+               set adt [concat $adt $alldtags($tag)]
+           }
+           set adt [lsort -unique $adt]
+           set alldtags($id) $adt
+           foreach tag $adt {
+               set tagisdesc($id,$tag) -1
+               set tagisdesc($tag,$id) 1
+           }
+       }
+       if {[info exists idheads($id)]} {
+           lappend dheads $id
+       }
+       set desc_heads($id) $dheads
+       if {[clock clicks -milliseconds] - $allcstart >= 50} {
+           fileevent $fd readable {}
+           after idle restartgetall $fd
+           return
+       }
+    }
+    if {[eof $fd]} {
+       after idle restartatags [llength $allids]
+       if {[catch {close $fd} err]} {
+           error_popup "Error reading full commit graph: $err.\n\
+                        Results may be incomplete."
+       }
+    }
+}
+
+# walk backward through the tree and compute nearest tagged ancestors
+proc restartatags {i} {
+    global allids allparents idtags anc_tags t0
+
+    set t0 [clock clicks -milliseconds]
+    while {[incr i -1] >= 0} {
+       set id [lindex $allids $i]
+       set atags {}
+       foreach p $allparents($id) {
+           if {[info exists idtags($p)]} {
+               set ptags [list $p]
+           } else {
+               set ptags $anc_tags($p)
+           }
+           if {$atags eq {}} {
+               set atags $ptags
+           } elseif {$ptags ne $atags} {
+               set atags [combine_atags $atags $ptags]
+           }
+       }
+       set anc_tags($id) $atags
+       if {[clock clicks -milliseconds] - $t0 >= 50} {
+           after idle restartatags $i
+           return
+       }
+    }
+    set allcommits "done"
+    notbusy allcommits
+    dispneartags
 }
 
 proc rereadrefs {} {
@@ -3491,23 +5196,41 @@ proc rereadrefs {} {
     }
 }
 
+proc listrefs {id} {
+    global idtags idheads idotherrefs
+
+    set x {}
+    if {[info exists idtags($id)]} {
+       set x $idtags($id)
+    }
+    set y {}
+    if {[info exists idheads($id)]} {
+       set y $idheads($id)
+    }
+    set z {}
+    if {[info exists idotherrefs($id)]} {
+       set z $idotherrefs($id)
+    }
+    return [list $x $y $z]
+}
+
 proc showtag {tag isnew} {
-    global ctext cflist tagcontents tagids linknum
+    global ctext tagcontents tagids linknum
 
     if {$isnew} {
        addtohistory [list showtag $tag 0]
     }
     $ctext conf -state normal
-    $ctext delete 0.0 end
+    clear_ctext
     set linknum 0
     if {[info exists tagcontents($tag)]} {
        set text $tagcontents($tag)
     } else {
        set text "Tag: $tag\nId:  $tagids($tag)"
     }
-    appendwithlinks $text
+    appendwithlinks $text {}
     $ctext conf -state disabled
-    $cflist delete 0 end
+    init_flist {}
 }
 
 proc doquit {} {
@@ -3517,8 +5240,8 @@ proc doquit {} {
 }
 
 proc doprefs {} {
-    global maxwidth maxgraphpct diffopts findmergefiles
-    global oldprefs prefstop
+    global maxwidth maxgraphpct diffopts
+    global oldprefs prefstop showneartags
 
     set top .gitkprefs
     set prefstop $top
@@ -3526,7 +5249,7 @@ proc doprefs {} {
        raise $top
        return
     }
-    foreach v {maxwidth maxgraphpct diffopts findmergefiles} {
+    foreach v {maxwidth maxgraphpct diffopts showneartags} {
        set oldprefs($v) [set $v]
     }
     toplevel $top
@@ -3542,16 +5265,17 @@ proc doprefs {} {
        -font optionfont
     spinbox $top.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
     grid x $top.maxpctl $top.maxpct -sticky w
-    checkbutton $top.findm -variable findmergefiles
-    label $top.findml -text "Include merges for \"Find\" in \"Files\"" \
-       -font optionfont
-    grid $top.findm $top.findml - -sticky w
     label $top.ddisp -text "Diff display options"
     grid $top.ddisp - -sticky w -pady 10
     label $top.diffoptl -text "Options for diff program" \
        -font optionfont
     entry $top.diffopt -width 20 -textvariable diffopts
     grid x $top.diffoptl $top.diffopt -sticky w
+    frame $top.ntag
+    label $top.ntag.l -text "Display nearby tags" -font optionfont
+    checkbutton $top.ntag.b -variable showneartags
+    pack $top.ntag.b $top.ntag.l -side left
+    grid x $top.ntag -sticky w
     frame $top.buts
     button $top.buts.ok -text "OK" -command prefsok
     button $top.buts.can -text "Cancel" -command prefscan
@@ -3562,10 +5286,10 @@ proc doprefs {} {
 }
 
 proc prefscan {} {
-    global maxwidth maxgraphpct diffopts findmergefiles
-    global oldprefs prefstop
+    global maxwidth maxgraphpct diffopts
+    global oldprefs prefstop showneartags
 
-    foreach v {maxwidth maxgraphpct diffopts findmergefiles} {
+    foreach v {maxwidth maxgraphpct diffopts showneartags} {
        set $v $oldprefs($v)
     }
     catch {destroy $prefstop}
@@ -3574,13 +5298,15 @@ proc prefscan {} {
 
 proc prefsok {} {
     global maxwidth maxgraphpct
-    global oldprefs prefstop
+    global oldprefs prefstop showneartags
 
     catch {destroy $prefstop}
     unset prefstop
     if {$maxwidth != $oldprefs(maxwidth)
        || $maxgraphpct != $oldprefs(maxgraphpct)} {
        redisplay
+    } elseif {$showneartags != $oldprefs(showneartags)} {
+       reselectline
     }
 }
 
@@ -3864,11 +5590,11 @@ proc tcl_encoding {enc} {
 # defaults...
 set datemode 0
 set diffopts "-U 5 -p"
-set wrcomcmd "git-diff-tree --stdin -p --pretty"
+set wrcomcmd "git diff-tree --stdin -p --pretty"
 
 set gitencoding {}
 catch {
-    set gitencoding [exec git-repo-config --get i18n.commitencoding]
+    set gitencoding [exec git repo-config --get i18n.commitencoding]
 }
 if {$gitencoding == ""} {
     set gitencoding "utf-8"
@@ -3889,13 +5615,14 @@ set fastdate 0
 set uparrowlen 7
 set downarrowlen 7
 set mingaplen 30
+set cmitmode "patch"
+set wrapcomment "none"
+set showneartags 1
 
 set colors {green red blue magenta darkgrey brown orange}
 
 catch {source ~/.gitk}
 
-set namefont $mainfont
-
 font create optionfont -family sans-serif -size -12
 
 set revtreeargs {}
@@ -3912,19 +5639,83 @@ foreach arg $argv {
 # check that we can find a .git directory somewhere...
 set gitdir [gitdir]
 if {![file isdirectory $gitdir]} {
-    error_popup "Cannot find the git directory \"$gitdir\"."
+    show_error {} . "Cannot find the git directory \"$gitdir\"."
     exit 1
 }
 
+set cmdline_files {}
+set i [lsearch -exact $revtreeargs "--"]
+if {$i >= 0} {
+    set cmdline_files [lrange $revtreeargs [expr {$i + 1}] end]
+    set revtreeargs [lrange $revtreeargs 0 [expr {$i - 1}]]
+} elseif {$revtreeargs ne {}} {
+    if {[catch {
+       set f [eval exec git rev-parse --no-revs --no-flags $revtreeargs]
+       set cmdline_files [split $f "\n"]
+       set n [llength $cmdline_files]
+       set revtreeargs [lrange $revtreeargs 0 end-$n]
+    } err]} {
+       # unfortunately we get both stdout and stderr in $err,
+       # so look for "fatal:".
+       set i [string first "fatal:" $err]
+       if {$i > 0} {
+           set err [string range $err [expr {$i + 6}] end]
+       }
+       show_error {} . "Bad arguments to gitk:\n$err"
+       exit 1
+    }
+}
+
 set history {}
 set historyindex 0
+set fh_serial 0
+set nhl_names {}
+set highlight_paths {}
+set searchdirn -forwards
+set boldrows {}
+set boldnamerows {}
 
 set optim_delay 16
 
+set nextviewnum 1
+set curview 0
+set selectedview 0
+set selectedhlview None
+set viewfiles(0) {}
+set viewperm(0) 0
+set viewargs(0) {}
+
+set cmdlineok 0
 set stopped 0
 set stuffsaved 0
 set patchnum 0
 setcoords
-makewindow $revtreeargs
+makewindow
 readrefs
-getcommits $revtreeargs
+
+if {$cmdline_files ne {} || $revtreeargs ne {}} {
+    # create a view for the files/dirs specified on the command line
+    set curview 1
+    set selectedview 1
+    set nextviewnum 2
+    set viewname(1) "Command line"
+    set viewfiles(1) $cmdline_files
+    set viewargs(1) $revtreeargs
+    set viewperm(1) 0
+    addviewmenu 1
+    .bar.view entryconf 2 -state normal
+    .bar.view entryconf 3 -state normal
+}
+
+if {[info exists permviews]} {
+    foreach v $permviews {
+       set n $nextviewnum
+       incr nextviewnum
+       set viewname($n) [lindex $v 0]
+       set viewfiles($n) [lindex $v 1]
+       set viewargs($n) [lindex $v 2]
+       set viewperm($n) 1
+       addviewmenu $n
+    }
+}
+getcommits
diff --git a/gitweb/README b/gitweb/README
new file mode 100644 (file)
index 0000000..8d67276
--- /dev/null
@@ -0,0 +1,9 @@
+GIT web Interface
+
+The one working on:
+  http://www.kernel.org/git/
+
+From the git version 1.4.0 gitweb is bundled with git.
+
+Any comment/question/concern to:
+  Kay Sievers <kay.sievers@vrfy.org>
diff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi
new file mode 100755 (executable)
index 0000000..5eabe06
--- /dev/null
@@ -0,0 +1,2536 @@
+#!/usr/bin/perl
+
+# gitweb - simple web interface to track changes in git repositories
+#
+# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
+# (C) 2005, Christian Gierke
+#
+# This program is licensed under the GPLv2
+
+use strict;
+use warnings;
+use CGI qw(:standard :escapeHTML -nosticky);
+use CGI::Util qw(unescape);
+use CGI::Carp qw(fatalsToBrowser);
+use Encode;
+use Fcntl ':mode';
+binmode STDOUT, ':utf8';
+
+my $cgi = new CGI;
+my $version =          "267";
+my $my_url =           $cgi->url();
+my $my_uri =           $cgi->url(-absolute => 1);
+my $rss_link =         "";
+
+# absolute fs-path which will be prepended to the project path
+#my $projectroot =     "/pub/scm";
+my $projectroot =      "/home/kay/public_html/pub/scm";
+
+# location of the git-core binaries
+my $gitbin =           "/usr/bin";
+
+# location for temporary files needed for diffs
+my $git_temp =         "/tmp/gitweb";
+
+# target of the home link on top of all pages
+my $home_link =                $my_uri;
+
+# html text to include at home page
+my $home_text =                "indextext.html";
+
+# source of projects list
+#my $projects_list =   $projectroot;
+my $projects_list =    "index/index.aux";
+
+# input validation and dispatch
+my $action = $cgi->param('a');
+if (defined $action) {
+       if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
+               undef $action;
+               die_error(undef, "Invalid action parameter.");
+       }
+       if ($action eq "git-logo.png") {
+               git_logo();
+               exit;
+       } elsif ($action eq "opml") {
+               git_opml();
+               exit;
+       }
+}
+
+my $order = $cgi->param('o');
+if (defined $order) {
+       if ($order =~ m/[^0-9a-zA-Z_]/) {
+               undef $order;
+               die_error(undef, "Invalid order parameter.");
+       }
+}
+
+my $project = $cgi->param('p');
+if (defined $project) {
+       $project = validate_input($project);
+       if (!defined($project)) {
+               die_error(undef, "Invalid project parameter.");
+       }
+       if (!(-d "$projectroot/$project")) {
+               undef $project;
+               die_error(undef, "No such directory.");
+       }
+       if (!(-e "$projectroot/$project/HEAD")) {
+               undef $project;
+               die_error(undef, "No such project.");
+       }
+       $rss_link = "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
+                   "$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>";
+       $ENV{'GIT_DIR'} = "$projectroot/$project";
+} else {
+       git_project_list();
+       exit;
+}
+
+my $file_name = $cgi->param('f');
+if (defined $file_name) {
+       $file_name = validate_input($file_name);
+       if (!defined($file_name)) {
+               die_error(undef, "Invalid file parameter.");
+       }
+}
+
+my $hash = $cgi->param('h');
+if (defined $hash) {
+       $hash = validate_input($hash);
+       if (!defined($hash)) {
+               die_error(undef, "Invalid hash parameter.");
+       }
+}
+
+my $hash_parent = $cgi->param('hp');
+if (defined $hash_parent) {
+       $hash_parent = validate_input($hash_parent);
+       if (!defined($hash_parent)) {
+               die_error(undef, "Invalid hash parent parameter.");
+       }
+}
+
+my $hash_base = $cgi->param('hb');
+if (defined $hash_base) {
+       $hash_base = validate_input($hash_base);
+       if (!defined($hash_base)) {
+               die_error(undef, "Invalid hash base parameter.");
+       }
+}
+
+my $page = $cgi->param('pg');
+if (defined $page) {
+       if ($page =~ m/[^0-9]$/) {
+               undef $page;
+               die_error(undef, "Invalid page parameter.");
+       }
+}
+
+my $searchtext = $cgi->param('s');
+if (defined $searchtext) {
+       if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
+               undef $searchtext;
+               die_error(undef, "Invalid search parameter.");
+       }
+       $searchtext = quotemeta $searchtext;
+}
+
+sub validate_input {
+       my $input = shift;
+
+       if ($input =~ m/^[0-9a-fA-F]{40}$/) {
+               return $input;
+       }
+       if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
+               return undef;
+       }
+       if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
+               return undef;
+       }
+       return $input;
+}
+
+if (!defined $action || $action eq "summary") {
+       git_summary();
+       exit;
+} elsif ($action eq "heads") {
+       git_heads();
+       exit;
+} elsif ($action eq "tags") {
+       git_tags();
+       exit;
+} elsif ($action eq "blob") {
+       git_blob();
+       exit;
+} elsif ($action eq "blob_plain") {
+       git_blob_plain();
+       exit;
+} elsif ($action eq "tree") {
+       git_tree();
+       exit;
+} elsif ($action eq "rss") {
+       git_rss();
+       exit;
+} elsif ($action eq "commit") {
+       git_commit();
+       exit;
+} elsif ($action eq "log") {
+       git_log();
+       exit;
+} elsif ($action eq "blobdiff") {
+       git_blobdiff();
+       exit;
+} elsif ($action eq "blobdiff_plain") {
+       git_blobdiff_plain();
+       exit;
+} elsif ($action eq "commitdiff") {
+       git_commitdiff();
+       exit;
+} elsif ($action eq "commitdiff_plain") {
+       git_commitdiff_plain();
+       exit;
+} elsif ($action eq "history") {
+       git_history();
+       exit;
+} elsif ($action eq "search") {
+       git_search();
+       exit;
+} elsif ($action eq "shortlog") {
+       git_shortlog();
+       exit;
+} elsif ($action eq "tag") {
+       git_tag();
+       exit;
+} elsif ($action eq "blame") {
+       git_blame();
+       exit;
+} else {
+       undef $action;
+       die_error(undef, "Unknown action.");
+       exit;
+}
+
+# quote unsafe chars, but keep the slash, even when it's not
+# correct, but quoted slashes look too horrible in bookmarks
+sub esc_param {
+       my $str = shift;
+       $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
+       $str =~ s/\+/%2B/g;
+       $str =~ s/ /\+/g;
+       return $str;
+}
+
+# replace invalid utf8 character with SUBSTITUTION sequence
+sub esc_html {
+       my $str = shift;
+       $str = decode("utf8", $str, Encode::FB_DEFAULT);
+       $str = escapeHTML($str);
+       return $str;
+}
+
+# git may return quoted and escaped filenames
+sub unquote {
+       my $str = shift;
+       if ($str =~ m/^"(.*)"$/) {
+               $str = $1;
+               $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
+       }
+       return $str;
+}
+
+sub git_header_html {
+       my $status = shift || "200 OK";
+       my $expires = shift;
+
+       my $title = "git";
+       if (defined $project) {
+               $title .= " - $project";
+               if (defined $action) {
+                       $title .= "/$action";
+               }
+       }
+       print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status, -expires => $expires);
+       print <<EOF;
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
+<!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
+<head>
+<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
+<meta name="robots" content="index, nofollow"/>
+<title>$title</title>
+$rss_link
+<style type="text/css">
+body {
+       font-family: sans-serif; font-size: 12px; border:solid #d9d8d1; border-width:1px;
+       margin:10px; background-color:#ffffff; color:#000000;
+}
+a { color:#0000cc; }
+a:hover, a:visited, a:active { color:#880000; }
+div.page_header { height:25px; padding:8px; font-size:18px; font-weight:bold; background-color:#d9d8d1; }
+div.page_header a:visited, a.header { color:#0000cc; }
+div.page_header a:hover { color:#880000; }
+div.page_nav { padding:8px; }
+div.page_nav a:visited { color:#0000cc; }
+div.page_path { padding:8px; border:solid #d9d8d1; border-width:0px 0px 1px}
+div.page_footer { height:17px; padding:4px 8px; background-color: #d9d8d1; }
+div.page_footer_text { float:left; color:#555555; font-style:italic; }
+div.page_body { padding:8px; }
+div.title, a.title {
+       display:block; padding:6px 8px;
+       font-weight:bold; background-color:#edece6; text-decoration:none; color:#000000;
+}
+a.title:hover { background-color: #d9d8d1; }
+div.title_text { padding:6px 0px; border: solid #d9d8d1; border-width:0px 0px 1px; }
+div.log_body { padding:8px 8px 8px 150px; }
+span.age { position:relative; float:left; width:142px; font-style:italic; }
+div.log_link {
+       padding:0px 8px;
+       font-size:10px; font-family:sans-serif; font-style:normal;
+       position:relative; float:left; width:136px;
+}
+div.list_head { padding:6px 8px 4px; border:solid #d9d8d1; border-width:1px 0px 0px; font-style:italic; }
+a.list { text-decoration:none; color:#000000; }
+a.list:hover { text-decoration:underline; color:#880000; }
+a.text { text-decoration:none; color:#0000cc; }
+a.text:visited { text-decoration:none; color:#880000; }
+a.text:hover { text-decoration:underline; color:#880000; }
+table { padding:8px 4px; }
+th { padding:2px 5px; font-size:12px; text-align:left; }
+tr.light:hover { background-color:#edece6; }
+tr.dark { background-color:#f6f6f0; }
+tr.dark:hover { background-color:#edece6; }
+td { padding:2px 5px; font-size:12px; vertical-align:top; }
+td.link { padding:2px 5px; font-family:sans-serif; font-size:10px; }
+div.pre { font-family:monospace; font-size:12px; white-space:pre; }
+div.diff_info { font-family:monospace; color:#000099; background-color:#edece6; font-style:italic; }
+div.index_include { border:solid #d9d8d1; border-width:0px 0px 1px; padding:12px 8px; }
+div.search { margin:4px 8px; position:absolute; top:56px; right:12px }
+a.linenr { color:#999999; text-decoration:none }
+a.rss_logo {
+       float:right; padding:3px 0px; width:35px; line-height:10px;
+       border:1px solid; border-color:#fcc7a5 #7d3302 #3e1a01 #ff954e;
+       color:#ffffff; background-color:#ff6600;
+       font-weight:bold; font-family:sans-serif; font-size:10px;
+       text-align:center; text-decoration:none;
+}
+a.rss_logo:hover { background-color:#ee5500; }
+span.tag {
+       padding:0px 4px; font-size:10px; font-weight:normal;
+       background-color:#ffffaa; border:1px solid; border-color:#ffffcc #ffee00 #ffee00 #ffffcc;
+}
+</style>
+</head>
+<body>
+EOF
+       print "<div class=\"page_header\">\n" .
+             "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
+             "<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
+             "</a>\n";
+       print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
+       if (defined $project) {
+               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
+               if (defined $action) {
+                       print " / $action";
+               }
+               print "\n";
+               if (!defined $searchtext) {
+                       $searchtext = "";
+               }
+               my $search_hash;
+               if (defined $hash) {
+                       $search_hash = $hash;
+               } else {
+                       $search_hash  = "HEAD";
+               }
+               $cgi->param("a", "search");
+               $cgi->param("h", $search_hash);
+               print $cgi->startform(-method => "get", -action => $my_uri) .
+                     "<div class=\"search\">\n" .
+                     $cgi->hidden(-name => "p") . "\n" .
+                     $cgi->hidden(-name => "a") . "\n" .
+                     $cgi->hidden(-name => "h") . "\n" .
+                     $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
+                     "</div>" .
+                     $cgi->end_form() . "\n";
+       }
+       print "</div>\n";
+}
+
+sub git_footer_html {
+       print "<div class=\"page_footer\">\n";
+       if (defined $project) {
+               my $descr = git_read_description($project);
+               if (defined $descr) {
+                       print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
+               }
+               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
+       } else {
+               print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
+       }
+       print "</div>\n" .
+             "</body>\n" .
+             "</html>";
+}
+
+sub die_error {
+       my $status = shift || "403 Forbidden";
+       my $error = shift || "Malformed query, file missing or permission denied"; 
+
+       git_header_html($status);
+       print "<div class=\"page_body\">\n" .
+             "<br/><br/>\n" .
+             "$status - $error\n" .
+             "<br/>\n" .
+             "</div>\n";
+       git_footer_html();
+       exit;
+}
+
+sub git_get_type {
+       my $hash = shift;
+
+       open my $fd, "-|", "$gitbin/git-cat-file -t $hash" or return;
+       my $type = <$fd>;
+       close $fd or return;
+       chomp $type;
+       return $type;
+}
+
+sub git_read_head {
+       my $project = shift;
+       my $oENV = $ENV{'GIT_DIR'};
+       my $retval = undef;
+       $ENV{'GIT_DIR'} = "$projectroot/$project";
+       if (open my $fd, "-|", "$gitbin/git-rev-parse", "--verify", "HEAD") {
+               my $head = <$fd>;
+               close $fd;
+               if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
+                       $retval = $1;
+               }
+       }
+       if (defined $oENV) {
+               $ENV{'GIT_DIR'} = $oENV;
+       }
+       return $retval;
+}
+
+sub git_read_hash {
+       my $path = shift;
+
+       open my $fd, "$projectroot/$path" or return undef;
+       my $head = <$fd>;
+       close $fd;
+       chomp $head;
+       if ($head =~ m/^[0-9a-fA-F]{40}$/) {
+               return $head;
+       }
+}
+
+sub git_read_description {
+       my $path = shift;
+
+       open my $fd, "$projectroot/$path/description" or return undef;
+       my $descr = <$fd>;
+       close $fd;
+       chomp $descr;
+       return $descr;
+}
+
+sub git_read_tag {
+       my $tag_id = shift;
+       my %tag;
+       my @comment;
+
+       open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" or return;
+       $tag{'id'} = $tag_id;
+       while (my $line = <$fd>) {
+               chomp $line;
+               if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
+                       $tag{'object'} = $1;
+               } elsif ($line =~ m/^type (.+)$/) {
+                       $tag{'type'} = $1;
+               } elsif ($line =~ m/^tag (.+)$/) {
+                       $tag{'name'} = $1;
+               } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
+                       $tag{'author'} = $1;
+                       $tag{'epoch'} = $2;
+                       $tag{'tz'} = $3;
+               } elsif ($line =~ m/--BEGIN/) {
+                       push @comment, $line;
+                       last;
+               } elsif ($line eq "") {
+                       last;
+               }
+       }
+       push @comment, <$fd>;
+       $tag{'comment'} = \@comment;
+       close $fd or return;
+       if (!defined $tag{'name'}) {
+               return
+       };
+       return %tag
+}
+
+sub age_string {
+       my $age = shift;
+       my $age_str;
+
+       if ($age > 60*60*24*365*2) {
+               $age_str = (int $age/60/60/24/365);
+               $age_str .= " years ago";
+       } elsif ($age > 60*60*24*(365/12)*2) {
+               $age_str = int $age/60/60/24/(365/12);
+               $age_str .= " months ago";
+       } elsif ($age > 60*60*24*7*2) {
+               $age_str = int $age/60/60/24/7;
+               $age_str .= " weeks ago";
+       } elsif ($age > 60*60*24*2) {
+               $age_str = int $age/60/60/24;
+               $age_str .= " days ago";
+       } elsif ($age > 60*60*2) {
+               $age_str = int $age/60/60;
+               $age_str .= " hours ago";
+       } elsif ($age > 60*2) {
+               $age_str = int $age/60;
+               $age_str .= " min ago";
+       } elsif ($age > 2) {
+               $age_str = int $age;
+               $age_str .= " sec ago";
+       } else {
+               $age_str .= " right now";
+       }
+       return $age_str;
+}
+
+sub git_read_commit {
+       my $commit_id = shift;
+       my $commit_text = shift;
+
+       my @commit_lines;
+       my %co;
+
+       if (defined $commit_text) {
+               @commit_lines = @$commit_text;
+       } else {
+               $/ = "\0";
+               open my $fd, "-|", "$gitbin/git-rev-list --header --parents --max-count=1 $commit_id" or return;
+               @commit_lines = split '\n', <$fd>;
+               close $fd or return;
+               $/ = "\n";
+               pop @commit_lines;
+       }
+       my $header = shift @commit_lines;
+       if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
+               return;
+       }
+       ($co{'id'}, my @parents) = split ' ', $header;
+       $co{'parents'} = \@parents;
+       $co{'parent'} = $parents[0];
+       while (my $line = shift @commit_lines) {
+               last if $line eq "\n";
+               if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
+                       $co{'tree'} = $1;
+               } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
+                       $co{'author'} = $1;
+                       $co{'author_epoch'} = $2;
+                       $co{'author_tz'} = $3;
+                       if ($co{'author'} =~ m/^([^<]+) </) {
+                               $co{'author_name'} = $1;
+                       } else {
+                               $co{'author_name'} = $co{'author'};
+                       }
+               } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
+                       $co{'committer'} = $1;
+                       $co{'committer_epoch'} = $2;
+                       $co{'committer_tz'} = $3;
+                       $co{'committer_name'} = $co{'committer'};
+                       $co{'committer_name'} =~ s/ <.*//;
+               }
+       }
+       if (!defined $co{'tree'}) {
+               return;
+       };
+
+       foreach my $title (@commit_lines) {
+               $title =~ s/^    //;
+               if ($title ne "") {
+                       $co{'title'} = chop_str($title, 80, 5);
+                       # remove leading stuff of merges to make the interesting part visible
+                       if (length($title) > 50) {
+                               $title =~ s/^Automatic //;
+                               $title =~ s/^merge (of|with) /Merge ... /i;
+                               if (length($title) > 50) {
+                                       $title =~ s/(http|rsync):\/\///;
+                               }
+                               if (length($title) > 50) {
+                                       $title =~ s/(master|www|rsync)\.//;
+                               }
+                               if (length($title) > 50) {
+                                       $title =~ s/kernel.org:?//;
+                               }
+                               if (length($title) > 50) {
+                                       $title =~ s/\/pub\/scm//;
+                               }
+                       }
+                       $co{'title_short'} = chop_str($title, 50, 5);
+                       last;
+               }
+       }
+       # remove added spaces
+       foreach my $line (@commit_lines) {
+               $line =~ s/^    //;
+       }
+       $co{'comment'} = \@commit_lines;
+
+       my $age = time - $co{'committer_epoch'};
+       $co{'age'} = $age;
+       $co{'age_string'} = age_string($age);
+       my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
+       if ($age > 60*60*24*7*2) {
+               $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
+               $co{'age_string_age'} = $co{'age_string'};
+       } else {
+               $co{'age_string_date'} = $co{'age_string'};
+               $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
+       }
+       return %co;
+}
+
+sub git_diff_print {
+       my $from = shift;
+       my $from_name = shift;
+       my $to = shift;
+       my $to_name = shift;
+       my $format = shift || "html";
+
+       my $from_tmp = "/dev/null";
+       my $to_tmp = "/dev/null";
+       my $pid = $$;
+
+       # create tmp from-file
+       if (defined $from) {
+               $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
+               open my $fd2, "> $from_tmp";
+               open my $fd, "-|", "$gitbin/git-cat-file blob $from";
+               my @file = <$fd>;
+               print $fd2 @file;
+               close $fd2;
+               close $fd;
+       }
+
+       # create tmp to-file
+       if (defined $to) {
+               $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
+               open my $fd2, "> $to_tmp";
+               open my $fd, "-|", "$gitbin/git-cat-file blob $to";
+               my @file = <$fd>;
+               print $fd2 @file;
+               close $fd2;
+               close $fd;
+       }
+
+       open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
+       if ($format eq "plain") {
+               undef $/;
+               print <$fd>;
+               $/ = "\n";
+       } else {
+               while (my $line = <$fd>) {
+                       chomp($line);
+                       my $char = substr($line, 0, 1);
+                       my $color = "";
+                       if ($char eq '+') {
+                               $color = " style=\"color:#008800;\"";
+                       } elsif ($char eq "-") {
+                               $color = " style=\"color:#cc0000;\"";
+                       } elsif ($char eq "@") {
+                               $color = " style=\"color:#990099;\"";
+                       } elsif ($char eq "\\") {
+                               # skip errors
+                               next;
+                       }
+                       while ((my $pos = index($line, "\t")) != -1) {
+                               if (my $count = (8 - (($pos-1) % 8))) {
+                                       my $spaces = ' ' x $count;
+                                       $line =~ s/\t/$spaces/;
+                               }
+                       }
+                       print "<div class=\"pre\"$color>" . esc_html($line) . "</div>\n";
+               }
+       }
+       close $fd;
+
+       if (defined $from) {
+               unlink($from_tmp);
+       }
+       if (defined $to) {
+               unlink($to_tmp);
+       }
+}
+
+sub mode_str {
+       my $mode = oct shift;
+
+       if (S_ISDIR($mode & S_IFMT)) {
+               return 'drwxr-xr-x';
+       } elsif (S_ISLNK($mode)) {
+               return 'lrwxrwxrwx';
+       } elsif (S_ISREG($mode)) {
+               # git cares only about the executable bit
+               if ($mode & S_IXUSR) {
+                       return '-rwxr-xr-x';
+               } else {
+                       return '-rw-r--r--';
+               };
+       } else {
+               return '----------';
+       }
+}
+
+sub chop_str {
+       my $str = shift;
+       my $len = shift;
+       my $add_len = shift || 10;
+
+       # allow only $len chars, but don't cut a word if it would fit in $add_len
+       # if it doesn't fit, cut it if it's still longer than the dots we would add
+       $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
+       my $body = $1;
+       my $tail = $2;
+       if (length($tail) > 4) {
+               $tail = " ...";
+       }
+       return "$body$tail";
+}
+
+sub file_type {
+       my $mode = oct shift;
+
+       if (S_ISDIR($mode & S_IFMT)) {
+               return "directory";
+       } elsif (S_ISLNK($mode)) {
+               return "symlink";
+       } elsif (S_ISREG($mode)) {
+               return "file";
+       } else {
+               return "unknown";
+       }
+}
+
+sub format_log_line_html {
+       my $line = shift;
+
+       $line = esc_html($line);
+       $line =~ s/ /&nbsp;/g;
+       if ($line =~ m/([0-9a-fA-F]{40})/) {
+               my $hash_text = $1;
+               if (git_get_type($hash_text) eq "commit") {
+                       my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
+                       $line =~ s/$hash_text/$link/;
+               }
+       }
+       return $line;
+}
+
+sub date_str {
+       my $epoch = shift;
+       my $tz = shift || "-0000";
+
+       my %date;
+       my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
+       my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
+       my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
+       $date{'hour'} = $hour;
+       $date{'minute'} = $min;
+       $date{'mday'} = $mday;
+       $date{'day'} = $days[$wday];
+       $date{'month'} = $months[$mon];
+       $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
+       $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
+
+       $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
+       my $local = $epoch + ((int $1 + ($2/60)) * 3600);
+       ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
+       $date{'hour_local'} = $hour;
+       $date{'minute_local'} = $min;
+       $date{'tz_local'} = $tz;
+       return %date;
+}
+
+# git-logo (cached in browser for one day)
+sub git_logo {
+       binmode STDOUT, ':raw';
+       print $cgi->header(-type => 'image/png', -expires => '+1d');
+       # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
+       print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
+               "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
+               "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
+               "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
+               "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
+               "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
+               "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
+               "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
+               "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
+               "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
+               "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
+               "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
+               "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
+}
+
+sub get_file_owner {
+       my $path = shift;
+
+       my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
+       my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
+       if (!defined $gcos) {
+               return undef;
+       }
+       my $owner = $gcos;
+       $owner =~ s/[,;].*$//;
+       return decode("utf8", $owner, Encode::FB_DEFAULT);
+}
+
+sub git_read_projects {
+       my @list;
+
+       if (-d $projects_list) {
+               # search in directory
+               my $dir = $projects_list;
+               opendir my $dh, $dir or return undef;
+               while (my $dir = readdir($dh)) {
+                       if (-e "$projectroot/$dir/HEAD") {
+                               my $pr = {
+                                       path => $dir,
+                               };
+                               push @list, $pr
+                       }
+               }
+               closedir($dh);
+       } elsif (-f $projects_list) {
+               # read from file(url-encoded):
+               # 'git%2Fgit.git Linus+Torvalds'
+               # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
+               # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
+               open my $fd , $projects_list or return undef;
+               while (my $line = <$fd>) {
+                       chomp $line;
+                       my ($path, $owner) = split ' ', $line;
+                       $path = unescape($path);
+                       $owner = unescape($owner);
+                       if (!defined $path) {
+                               next;
+                       }
+                       if (-e "$projectroot/$path/HEAD") {
+                               my $pr = {
+                                       path => $path,
+                                       owner => decode("utf8", $owner, Encode::FB_DEFAULT),
+                               };
+                               push @list, $pr
+                       }
+               }
+               close $fd;
+       }
+       @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
+       return @list;
+}
+
+sub git_get_project_config {
+       my $key = shift;
+
+       return unless ($key);
+       $key =~ s/^gitweb\.//;
+       return if ($key =~ m/\W/);
+
+       my $val = qx(git-repo-config --get gitweb.$key);
+       return ($val);
+}
+
+sub git_get_project_config_bool {
+       my $val = git_get_project_config (@_);
+       if ($val and $val =~ m/true|yes|on/) {
+               return (1);
+       }
+       return; # implicit false
+}
+
+sub git_project_list {
+       my @list = git_read_projects();
+       my @projects;
+       if (!@list) {
+               die_error(undef, "No project found.");
+       }
+       foreach my $pr (@list) {
+               my $head = git_read_head($pr->{'path'});
+               if (!defined $head) {
+                       next;
+               }
+               $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
+               my %co = git_read_commit($head);
+               if (!%co) {
+                       next;
+               }
+               $pr->{'commit'} = \%co;
+               if (!defined $pr->{'descr'}) {
+                       my $descr = git_read_description($pr->{'path'}) || "";
+                       $pr->{'descr'} = chop_str($descr, 25, 5);
+               }
+               if (!defined $pr->{'owner'}) {
+                       $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
+               }
+               push @projects, $pr;
+       }
+       git_header_html();
+       if (-f $home_text) {
+               print "<div class=\"index_include\">\n";
+               open (my $fd, $home_text);
+               print <$fd>;
+               close $fd;
+               print "</div>\n";
+       }
+       print "<table cellspacing=\"0\">\n" .
+             "<tr>\n";
+       if (!defined($order) || (defined($order) && ($order eq "project"))) {
+               @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
+               print "<th>Project</th>\n";
+       } else {
+               print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
+       }
+       if (defined($order) && ($order eq "descr")) {
+               @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
+               print "<th>Description</th>\n";
+       } else {
+               print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
+       }
+       if (defined($order) && ($order eq "owner")) {
+               @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
+               print "<th>Owner</th>\n";
+       } else {
+               print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
+       }
+       if (defined($order) && ($order eq "age")) {
+               @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
+               print "<th>Last Change</th>\n";
+       } else {
+               print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
+       }
+       print "<th></th>\n" .
+             "</tr>\n";
+       my $alternate = 0;
+       foreach my $pr (@projects) {
+               if ($alternate) {
+                       print "<tr class=\"dark\">\n";
+               } else {
+                       print "<tr class=\"light\">\n";
+               }
+               $alternate ^= 1;
+               print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"), -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
+                     "<td>$pr->{'descr'}</td>\n" .
+                     "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
+               my $colored_age;
+               if ($pr->{'commit'}{'age'} < 60*60*2) {
+                       $colored_age = "<span style =\"color: #009900;\"><b><i>$pr->{'commit'}{'age_string'}</i></b></span>";
+               } elsif ($pr->{'commit'}{'age'} < 60*60*24*2) {
+                       $colored_age = "<span style =\"color: #009900;\"><i>$pr->{'commit'}{'age_string'}</i></span>";
+               } else {
+                       $colored_age = "<i>$pr->{'commit'}{'age_string'}</i>";
+               }
+               print "<td>$colored_age</td>\n" .
+                     "<td class=\"link\">" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
+                     "</td>\n" .
+                     "</tr>\n";
+       }
+       print "</table>\n";
+       git_footer_html();
+}
+
+sub read_info_ref {
+       my $type = shift || "";
+       my %refs;
+       # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
+       # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
+       open my $fd, "$projectroot/$project/info/refs" or return;
+       while (my $line = <$fd>) {
+               chomp($line);
+               if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
+                       if (defined $refs{$1}) {
+                               $refs{$1} .= " / $2";
+                       } else {
+                               $refs{$1} = $2;
+                       }
+               }
+       }
+       close $fd or return;
+       return \%refs;
+}
+
+sub git_read_refs {
+       my $ref_dir = shift;
+       my @reflist;
+
+       my @refs;
+       opendir my $dh, "$projectroot/$project/$ref_dir";
+       while (my $dir = readdir($dh)) {
+               if ($dir =~ m/^\./) {
+                       next;
+               }
+               if (-d "$projectroot/$project/$ref_dir/$dir") {
+                       opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
+                       my @subdirs = grep !m/^\./, readdir $dh2;
+                       closedir($dh2);
+                       foreach my $subdir (@subdirs) {
+                               push @refs, "$dir/$subdir"
+                       }
+                       next;
+               }
+               push @refs, $dir;
+       }
+       closedir($dh);
+       foreach my $ref_file (@refs) {
+               my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
+               my $type = git_get_type($ref_id) || next;
+               my %ref_item;
+               my %co;
+               $ref_item{'type'} = $type;
+               $ref_item{'id'} = $ref_id;
+               $ref_item{'epoch'} = 0;
+               $ref_item{'age'} = "unknown";
+               if ($type eq "tag") {
+                       my %tag = git_read_tag($ref_id);
+                       $ref_item{'comment'} = $tag{'comment'};
+                       if ($tag{'type'} eq "commit") {
+                               %co = git_read_commit($tag{'object'});
+                               $ref_item{'epoch'} = $co{'committer_epoch'};
+                               $ref_item{'age'} = $co{'age_string'};
+                       } elsif (defined($tag{'epoch'})) {
+                               my $age = time - $tag{'epoch'};
+                               $ref_item{'epoch'} = $tag{'epoch'};
+                               $ref_item{'age'} = age_string($age);
+                       }
+                       $ref_item{'reftype'} = $tag{'type'};
+                       $ref_item{'name'} = $tag{'name'};
+                       $ref_item{'refid'} = $tag{'object'};
+               } elsif ($type eq "commit"){
+                       %co = git_read_commit($ref_id);
+                       $ref_item{'reftype'} = "commit";
+                       $ref_item{'name'} = $ref_file;
+                       $ref_item{'title'} = $co{'title'};
+                       $ref_item{'refid'} = $ref_id;
+                       $ref_item{'epoch'} = $co{'committer_epoch'};
+                       $ref_item{'age'} = $co{'age_string'};
+               }
+
+               push @reflist, \%ref_item;
+       }
+       # sort tags by age
+       @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
+       return \@reflist;
+}
+
+sub git_summary {
+       my $descr = git_read_description($project) || "none";
+       my $head = git_read_head($project);
+       my %co = git_read_commit($head);
+       my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
+
+       my $owner;
+       if (-f $projects_list) {
+               open (my $fd , $projects_list);
+               while (my $line = <$fd>) {
+                       chomp $line;
+                       my ($pr, $ow) = split ' ', $line;
+                       $pr = unescape($pr);
+                       $ow = unescape($ow);
+                       if ($pr eq $project) {
+                               $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
+                               last;
+                       }
+               }
+               close $fd;
+       }
+       if (!defined $owner) {
+               $owner = get_file_owner("$projectroot/$project");
+       }
+
+       my $refs = read_info_ref();
+       git_header_html();
+       print "<div class=\"page_nav\">\n" .
+             "summary".
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree")}, "tree") .
+             "<br/><br/>\n" .
+             "</div>\n";
+       print "<div class=\"title\">&nbsp;</div>\n";
+       print "<table cellspacing=\"0\">\n" .
+             "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
+             "<tr><td>owner</td><td>$owner</td></tr>\n" .
+             "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
+             "</table>\n";
+       open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_head($project) or die_error(undef, "Open failed.");
+       my (@revlist) = map { chomp; $_ } <$fd>;
+       close $fd;
+       print "<div>\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog"), -class => "title"}, "shortlog") .
+             "</div>\n";
+       my $i = 16;
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       foreach my $commit (@revlist) {
+               my %co = git_read_commit($commit);
+               my %ad = date_str($co{'author_epoch'});
+               if ($alternate) {
+                       print "<tr class=\"dark\">\n";
+               } else {
+                       print "<tr class=\"light\">\n";
+               }
+               $alternate ^= 1;
+               if ($i-- > 0) {
+                       my $ref = "";
+                       if (defined $refs->{$commit}) {
+                               $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
+                       }
+                       print "<td><i>$co{'age_string'}</i></td>\n" .
+                             "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
+                             "<td>";
+                       if (length($co{'title_short'}) < length($co{'title'})) {
+                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
+                                     "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
+                       } else {
+                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
+                                     "<b>" . esc_html($co{'title'}) . "$ref</b>");
+                       }
+                       print "</td>\n" .
+                             "<td class=\"link\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
+                             "</td>\n" .
+                             "</tr>";
+               } else {
+                       print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "...") . "</td>\n" .
+                       "</tr>";
+                       last;
+               }
+       }
+       print "</table\n>";
+
+       my $taglist = git_read_refs("refs/tags");
+       if (defined @$taglist) {
+               print "<div>\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags"), -class => "title"}, "tags") .
+                     "</div>\n";
+               my $i = 16;
+               print "<table cellspacing=\"0\">\n";
+               my $alternate = 0;
+               foreach my $entry (@$taglist) {
+                       my %tag = %$entry;
+                       my $comment_lines = $tag{'comment'};
+                       my $comment = shift @$comment_lines;
+                       if (defined($comment)) {
+                               $comment = chop_str($comment, 30, 5);
+                       }
+                       if ($alternate) {
+                               print "<tr class=\"dark\">\n";
+                       } else {
+                               print "<tr class=\"light\">\n";
+                       }
+                       $alternate ^= 1;
+                       if ($i-- > 0) {
+                               print "<td><i>$tag{'age'}</i></td>\n" .
+                                     "<td>" .
+                                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
+                                     "<b>" . esc_html($tag{'name'}) . "</b>") .
+                                     "</td>\n" .
+                                     "<td>";
+                               if (defined($comment)) {
+                                     print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
+                               }
+                               print "</td>\n" .
+                                     "<td class=\"link\">";
+                               if ($tag{'type'} eq "tag") {
+                                     print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
+                               }
+                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
+                               if ($tag{'reftype'} eq "commit") {
+                                     print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
+                                           " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
+                               }
+                               print "</td>\n" .
+                                     "</tr>";
+                       } else {
+                               print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "...") . "</td>\n" .
+                               "</tr>";
+                               last;
+                       }
+               }
+               print "</table\n>";
+       }
+
+       my $headlist = git_read_refs("refs/heads");
+       if (defined @$headlist) {
+               print "<div>\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads"), -class => "title"}, "heads") .
+                     "</div>\n";
+               my $i = 16;
+               print "<table cellspacing=\"0\">\n";
+               my $alternate = 0;
+               foreach my $entry (@$headlist) {
+                       my %tag = %$entry;
+                       if ($alternate) {
+                               print "<tr class=\"dark\">\n";
+                       } else {
+                               print "<tr class=\"light\">\n";
+                       }
+                       $alternate ^= 1;
+                       if ($i-- > 0) {
+                               print "<td><i>$tag{'age'}</i></td>\n" .
+                                     "<td>" .
+                                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"},
+                                     "<b>" . esc_html($tag{'name'}) . "</b>") .
+                                     "</td>\n" .
+                                     "<td class=\"link\">" .
+                                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
+                                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
+                                     "</td>\n" .
+                                     "</tr>";
+                       } else {
+                               print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "...") . "</td>\n" .
+                               "</tr>";
+                               last;
+                       }
+               }
+               print "</table\n>";
+       }
+       git_footer_html();
+}
+
+sub git_tag {
+       my $head = git_read_head($project);
+       git_header_html();
+       print "<div class=\"page_nav\">\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
+             "<br/>\n" .
+             "</div>\n";
+       my %tag = git_read_tag($hash);
+       print "<div>\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($tag{'name'})) . "\n" .
+             "</div>\n";
+       print "<div class=\"title_text\">\n" .
+             "<table cellspacing=\"0\">\n" .
+             "<tr>\n" .
+             "<td>object</td>\n" .
+             "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
+             "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
+             "</tr>\n";
+       if (defined($tag{'author'})) {
+               my %ad = date_str($tag{'epoch'}, $tag{'tz'});
+               print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
+               print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
+       }
+       print "</table>\n\n" .
+             "</div>\n";
+       print "<div class=\"page_body\">";
+       my $comment = $tag{'comment'};
+       foreach my $line (@$comment) {
+               print esc_html($line) . "<br/>\n";
+       }
+       print "</div>\n";
+       git_footer_html();
+}
+
+sub git_blame {
+       my $fd;
+       die_error('403 Permission denied', "Permission denied.") if (!git_get_project_config_bool ('blame'));
+       die_error('404 Not Found', "What file will it be, master?") if (!$file_name);
+       $hash_base ||= git_read_head($project);
+       die_error(undef, "Reading commit failed.") unless ($hash_base);
+       my %co = git_read_commit($hash_base)
+               or die_error(undef, "Reading commit failed.");
+       if (!defined $hash) {
+               $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
+                       or die_error(undef, "Error lookup file.");
+       }
+       open ($fd, "-|", "$gitbin/git-annotate", '-l', '-t', '-r', $file_name, $hash_base)
+               or die_error(undef, "Open failed.");
+       git_header_html();
+       print "<div class=\"page_nav\">\n" .
+               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
+               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
+               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
+               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
+               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
+       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
+               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head") . "<br/>\n";
+       print "</div>\n".
+               "<div>" .
+               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
+               "</div>\n";
+       print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
+       print "<div class=\"page_body\">\n";
+       print <<HTML;
+<table style="border-collapse: collapse;">
+  <tr>
+    <th>Commit</th>
+    <th>Age</th>
+    <th>Author</th>
+    <th>Line</th>
+    <th>Data</th>
+  </tr>
+HTML
+       my @line_class = (qw(light dark));
+       my $line_class_len = scalar (@line_class);
+       my $line_class_num = $#line_class;
+       while (my $line = <$fd>) {
+               my $long_rev;
+               my $short_rev;
+               my $author;
+               my $time;
+               my $lineno;
+               my $data;
+               my $age;
+               my $age_str;
+               my $age_style;
+
+               chomp $line;
+               $line_class_num = ($line_class_num + 1) % $line_class_len;
+
+               if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
+                       $long_rev = $1;
+                       $author   = $2;
+                       $time     = $3;
+                       $lineno   = $4;
+                       $data     = $5;
+               } else {
+                       print qq(  <tr><td colspan="5" style="color: red; background-color: yellow;">Unable to parse: $line</td></tr>\n);
+                       next;
+               }
+               $short_rev  = substr ($long_rev, 0, 8);
+               $age        = time () - $time;
+               $age_str    = age_string ($age);
+               $age_str    =~ s/ /&nbsp;/g;
+               $age_style  = 'font-style: italic;';
+               $age_style .= ' color: #009900; background: transparent;' if ($age < 60*60*24*2);
+               $age_style .= ' font-weight: bold;' if ($age < 60*60*2);
+               $author     = esc_html ($author);
+               $author     =~ s/ /&nbsp;/g;
+               # escape tabs
+               while ((my $pos = index($data, "\t")) != -1) {
+                       if (my $count = (8 - ($pos % 8))) {
+                               my $spaces = ' ' x $count;
+                               $data =~ s/\t/$spaces/;
+                       }
+               }
+               $data = esc_html ($data);
+               $data =~ s/ /&nbsp;/g;
+
+               print <<HTML;
+  <tr class="$line_class[$line_class_num]">
+    <td style="font-family: monospace;"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
+    <td style="$age_style">$age_str</td>
+    <td>$author</td>
+    <td style="text-align: right;"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
+    <td style="font-family: monospace;">$data</td>
+  </tr>
+HTML
+       } # while (my $line = <$fd>)
+       print "</table>\n\n";
+       close $fd or print "Reading blob failed.\n";
+       print "</div>";
+       git_footer_html();
+}
+
+sub git_tags {
+       my $head = git_read_head($project);
+       git_header_html();
+       print "<div class=\"page_nav\">\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
+             "<br/>\n" .
+             "</div>\n";
+       my $taglist = git_read_refs("refs/tags");
+       print "<div>\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
+             "</div>\n";
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       if (defined @$taglist) {
+               foreach my $entry (@$taglist) {
+                       my %tag = %$entry;
+                       my $comment_lines = $tag{'comment'};
+                       my $comment = shift @$comment_lines;
+                       if (defined($comment)) {
+                               $comment = chop_str($comment, 30, 5);
+                       }
+                       if ($alternate) {
+                               print "<tr class=\"dark\">\n";
+                       } else {
+                               print "<tr class=\"light\">\n";
+                       }
+                       $alternate ^= 1;
+                       print "<td><i>$tag{'age'}</i></td>\n" .
+                             "<td>" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
+                             "<b>" . esc_html($tag{'name'}) . "</b>") .
+                             "</td>\n" .
+                             "<td>";
+                       if (defined($comment)) {
+                             print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
+                       }
+                       print "</td>\n" .
+                             "<td class=\"link\">";
+                       if ($tag{'type'} eq "tag") {
+                             print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
+                       }
+                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
+                       if ($tag{'reftype'} eq "commit") {
+                             print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
+                                   " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
+                       }
+                       print "</td>\n" .
+                             "</tr>";
+               }
+       }
+       print "</table\n>";
+       git_footer_html();
+}
+
+sub git_heads {
+       my $head = git_read_head($project);
+       git_header_html();
+       print "<div class=\"page_nav\">\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
+             "<br/>\n" .
+             "</div>\n";
+       my $taglist = git_read_refs("refs/heads");
+       print "<div>\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
+             "</div>\n";
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       if (defined @$taglist) {
+               foreach my $entry (@$taglist) {
+                       my %tag = %$entry;
+                       if ($alternate) {
+                               print "<tr class=\"dark\">\n";
+                       } else {
+                               print "<tr class=\"light\">\n";
+                       }
+                       $alternate ^= 1;
+                       print "<td><i>$tag{'age'}</i></td>\n" .
+                             "<td>" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
+                             "</td>\n" .
+                             "<td class=\"link\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
+                             "</td>\n" .
+                             "</tr>";
+               }
+       }
+       print "</table\n>";
+       git_footer_html();
+}
+
+sub git_get_hash_by_path {
+       my $base = shift;
+       my $path = shift || return undef;
+
+       my $tree = $base;
+       my @parts = split '/', $path;
+       while (my $part = shift @parts) {
+               open my $fd, "-|", "$gitbin/git-ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
+               my (@entries) = map { chomp; $_ } <$fd>;
+               close $fd or return undef;
+               foreach my $line (@entries) {
+                       #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
+                       $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
+                       my $t_mode = $1;
+                       my $t_type = $2;
+                       my $t_hash = $3;
+                       my $t_name = validate_input(unquote($4));
+                       if ($t_name eq $part) {
+                               if (!(@parts)) {
+                                       return $t_hash;
+                               }
+                               if ($t_type eq "tree") {
+                                       $tree = $t_hash;
+                               }
+                               last;
+                       }
+               }
+       }
+}
+
+sub git_blob {
+       if (!defined $hash && defined $file_name) {
+               my $base = $hash_base || git_read_head($project);
+               $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
+       }
+       my $have_blame = git_get_project_config_bool ('blame');
+       open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
+       git_header_html();
+       if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
+               print "<div class=\"page_nav\">\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
+               if (defined $file_name) {
+                       if ($have_blame) {
+                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") .  " | ";
+                       }
+                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
+                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "<br/>\n";
+               } else {
+                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "<br/>\n";
+               }
+               print "</div>\n".
+                      "<div>" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
+                     "</div>\n";
+       } else {
+               print "<div class=\"page_nav\">\n" .
+                     "<br/><br/></div>\n" .
+                     "<div class=\"title\">$hash</div>\n";
+       }
+       if (defined $file_name) {
+               print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
+       }
+       print "<div class=\"page_body\">\n";
+       my $nr;
+       while (my $line = <$fd>) {
+               chomp $line;
+               $nr++;
+               while ((my $pos = index($line, "\t")) != -1) {
+                       if (my $count = (8 - ($pos % 8))) {
+                               my $spaces = ' ' x $count;
+                               $line =~ s/\t/$spaces/;
+                       }
+               }
+               printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
+       }
+       close $fd or print "Reading blob failed.\n";
+       print "</div>";
+       git_footer_html();
+}
+
+sub git_blob_plain {
+       my $save_as = "$hash.txt";
+       if (defined $file_name) {
+               $save_as = $file_name;
+       }
+       print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"$save_as\"");
+       open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
+       undef $/;
+       print <$fd>;
+       $/ = "\n";
+       close $fd;
+}
+
+sub git_tree {
+       if (!defined $hash) {
+               $hash = git_read_head($project);
+               if (defined $file_name) {
+                       my $base = $hash_base || $hash;
+                       $hash = git_get_hash_by_path($base, $file_name, "tree");
+               }
+               if (!defined $hash_base) {
+                       $hash_base = $hash;
+               }
+       }
+       $/ = "\0";
+       open my $fd, "-|", "$gitbin/git-ls-tree -z $hash" or die_error(undef, "Open git-ls-tree failed.");
+       chomp (my (@entries) = <$fd>);
+       close $fd or die_error(undef, "Reading tree failed.");
+       $/ = "\n";
+
+       my $refs = read_info_ref();
+       my $ref = "";
+       if (defined $refs->{$hash_base}) {
+               $ref = " <span class=\"tag\">" . esc_html($refs->{$hash_base}) . "</span>";
+       }
+       git_header_html();
+       my $base_key = "";
+       my $base = "";
+       if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
+               $base_key = ";hb=$hash_base";
+               print "<div class=\"page_nav\">\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash_base")}, "shortlog") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash_base")}, "log") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
+                     " | tree" .
+                     "<br/><br/>\n" .
+                     "</div>\n";
+               print "<div>\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
+                     "</div>\n";
+       } else {
+               print "<div class=\"page_nav\">\n";
+               print "<br/><br/></div>\n";
+               print "<div class=\"title\">$hash</div>\n";
+       }
+       if (defined $file_name) {
+               $base = esc_html("$file_name/");
+               print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
+       } else {
+               print "<div class=\"page_path\"><b>/</b></div>\n";
+       }
+       print "<div class=\"page_body\">\n";
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       foreach my $line (@entries) {
+               #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
+               $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
+               my $t_mode = $1;
+               my $t_type = $2;
+               my $t_hash = $3;
+               my $t_name = validate_input($4);
+               if ($alternate) {
+                       print "<tr class=\"dark\">\n";
+               } else {
+                       print "<tr class=\"light\">\n";
+               }
+               $alternate ^= 1;
+               print "<td style=\"font-family:monospace\">" . mode_str($t_mode) . "</td>\n";
+               if ($t_type eq "blob") {
+                       print "<td class=\"list\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name"), -class => "list"}, esc_html($t_name)) .
+                             "</td>\n" .
+                             "<td class=\"link\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
+#                            " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") .
+                             "</td>\n";
+               } elsif ($t_type eq "tree") {
+                       print "<td class=\"list\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
+                             "</td>\n" .
+                             "<td class=\"link\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
+                             "</td>\n";
+               }
+               print "</tr>\n";
+       }
+       print "</table>\n" .
+             "</div>";
+       git_footer_html();
+}
+
+sub git_rss {
+       # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
+       open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_head($project) or die_error(undef, "Open failed.");
+       my (@revlist) = map { chomp; $_ } <$fd>;
+       close $fd or die_error(undef, "Reading rev-list failed.");
+       print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
+       print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
+             "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
+       print "<channel>\n";
+       print "<title>$project</title>\n".
+             "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
+             "<description>$project log</description>\n".
+             "<language>en</language>\n";
+
+       for (my $i = 0; $i <= $#revlist; $i++) {
+               my $commit = $revlist[$i];
+               my %co = git_read_commit($commit);
+               # we read 150, we always show 30 and the ones more recent than 48 hours
+               if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
+                       last;
+               }
+               my %cd = date_str($co{'committer_epoch'});
+               open $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $co{'id'}" or next;
+               my @difftree = map { chomp; $_ } <$fd>;
+               close $fd or next;
+               print "<item>\n" .
+                     "<title>" .
+                     sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
+                     "</title>\n" .
+                     "<author>" . esc_html($co{'author'}) . "</author>\n" .
+                     "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
+                     "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
+                     "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
+                     "<description>" . esc_html($co{'title'}) . "</description>\n" .
+                     "<content:encoded>" .
+                     "<![CDATA[\n";
+               my $comment = $co{'comment'};
+               foreach my $line (@$comment) {
+                       $line = decode("utf8", $line, Encode::FB_DEFAULT);
+                       print "$line<br/>\n";
+               }
+               print "<br/>\n";
+               foreach my $line (@difftree) {
+                       if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
+                               next;
+                       }
+                       my $file = validate_input(unquote($7));
+                       $file = decode("utf8", $file, Encode::FB_DEFAULT);
+                       print "$file<br/>\n";
+               }
+               print "]]>\n" .
+                     "</content:encoded>\n" .
+                     "</item>\n";
+       }
+       print "</channel></rss>";
+}
+
+sub git_opml {
+       my @list = git_read_projects();
+
+       print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
+       print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
+             "<opml version=\"1.0\">\n".
+             "<head>".
+             "  <title>Git OPML Export</title>\n".
+             "</head>\n".
+             "<body>\n".
+             "<outline text=\"git RSS feeds\">\n";
+
+       foreach my $pr (@list) {
+               my %proj = %$pr;
+               my $head = git_read_head($proj{'path'});
+               if (!defined $head) {
+                       next;
+               }
+               $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
+               my %co = git_read_commit($head);
+               if (!%co) {
+                       next;
+               }
+
+               my $path = esc_html(chop_str($proj{'path'}, 25, 5));
+               my $rss =  "$my_url?p=$proj{'path'};a=rss";
+               my $html =  "$my_url?p=$proj{'path'};a=summary";
+               print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
+       }
+       print "</outline>\n".
+             "</body>\n".
+             "</opml>\n";
+}
+
+sub git_log {
+       my $head = git_read_head($project);
+       if (!defined $hash) {
+               $hash = $head;
+       }
+       if (!defined $page) {
+               $page = 0;
+       }
+       my $refs = read_info_ref();
+       git_header_html();
+       print "<div class=\"page_nav\">\n";
+       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
+             " | log" .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
+
+       my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
+       open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
+       my (@revlist) = map { chomp; $_ } <$fd>;
+       close $fd;
+
+       if ($hash ne $head || $page) {
+               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "HEAD");
+       } else {
+               print "HEAD";
+       }
+       if ($page > 0) {
+               print " &sdot; " .
+               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
+       } else {
+               print " &sdot; prev";
+       }
+       if ($#revlist >= (100 * ($page+1)-1)) {
+               print " &sdot; " .
+               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
+       } else {
+               print " &sdot; next";
+       }
+       print "<br/>\n" .
+             "</div>\n";
+       if (!@revlist) {
+               print "<div>\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
+                     "</div>\n";
+               my %co = git_read_commit($hash);
+               print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
+       }
+       for (my $i = ($page * 100); $i <= $#revlist; $i++) {
+               my $commit = $revlist[$i];
+               my $ref = "";
+               if (defined $refs->{$commit}) {
+                       $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
+               }
+               my %co = git_read_commit($commit);
+               next if !%co;
+               my %ad = date_str($co{'author_epoch'});
+               print "<div>\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "title"},
+                     "<span class=\"age\">$co{'age_string'}</span>" . esc_html($co{'title'}) . $ref) . "\n";
+               print "</div>\n";
+               print "<div class=\"title_text\">\n" .
+                     "<div class=\"log_link\">\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
+                     "<br/>\n" .
+                     "</div>\n" .
+                     "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
+                     "</div>\n" .
+                     "<div class=\"log_body\">\n";
+               my $comment = $co{'comment'};
+               my $empty = 0;
+               foreach my $line (@$comment) {
+                       if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
+                               next;
+                       }
+                       if ($line eq "") {
+                               if ($empty) {
+                                       next;
+                               }
+                               $empty = 1;
+                       } else {
+                               $empty = 0;
+                       }
+                       print format_log_line_html($line) . "<br/>\n";
+               }
+               if (!$empty) {
+                       print "<br/>\n";
+               }
+               print "</div>\n";
+       }
+       git_footer_html();
+}
+
+sub git_commit {
+       my %co = git_read_commit($hash);
+       if (!%co) {
+               die_error(undef, "Unknown commit object.");
+       }
+       my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
+       my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
+
+       my @difftree;
+       my $root = "";
+       my $parent = $co{'parent'};
+       if (!defined $parent) {
+               $root = " --root";
+               $parent = "";
+       }
+       open my $fd, "-|", "$gitbin/git-diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
+       @difftree = map { chomp; $_ } <$fd>;
+       close $fd or die_error(undef, "Reading diff-tree failed.");
+
+       # non-textual hash id's can be cached
+       my $expires;
+       if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
+               $expires = "+1d";
+       }
+       my $refs = read_info_ref();
+       my $ref = "";
+       if (defined $refs->{$co{'id'}}) {
+               $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
+       }
+       git_header_html(undef, $expires);
+       print "<div class=\"page_nav\">\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
+             " | commit";
+       if (defined $co{'parent'}) {
+               print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff");
+       }
+       print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "\n" .
+             "<br/><br/></div>\n";
+       if (defined $co{'parent'}) {
+               print "<div>\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
+                     "</div>\n";
+       } else {
+               print "<div>\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
+                     "</div>\n";
+       }
+       print "<div class=\"title_text\">\n" .
+             "<table cellspacing=\"0\">\n";
+       print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
+             "<tr>" .
+             "<td></td><td> $ad{'rfc2822'}";
+       if ($ad{'hour_local'} < 6) {
+               printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
+       } else {
+               printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
+       }
+       print "</td>" .
+             "</tr>\n";
+       print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
+       print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
+       print "<tr><td>commit</td><td style=\"font-family:monospace\">$co{'id'}</td></tr>\n";
+       print "<tr>" .
+             "<td>tree</td>" .
+             "<td style=\"font-family:monospace\">" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
+             "</td>" .
+             "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
+             "</td>" .
+             "</tr>\n";
+       my $parents  = $co{'parents'};
+       foreach my $par (@$parents) {
+               print "<tr>" .
+                     "<td>parent</td>" .
+                     "<td style=\"font-family:monospace\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
+                     "<td class=\"link\">" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
+                     "</td>" .
+                     "</tr>\n";
+       }
+       print "</table>". 
+             "</div>\n";
+       print "<div class=\"page_body\">\n";
+       my $comment = $co{'comment'};
+       my $empty = 0;
+       my $signed = 0;
+       foreach my $line (@$comment) {
+               # print only one empty line
+               if ($line eq "") {
+                       if ($empty || $signed) {
+                               next;
+                       }
+                       $empty = 1;
+               } else {
+                       $empty = 0;
+               }
+               if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
+                       $signed = 1;
+                       print "<span style=\"color: #888888\">" . esc_html($line) . "</span><br/>\n";
+               } else {
+                       $signed = 0;
+                       print format_log_line_html($line) . "<br/>\n";
+               }
+       }
+       print "</div>\n";
+       print "<div class=\"list_head\">\n";
+       if ($#difftree > 10) {
+               print(($#difftree + 1) . " files changed:\n");
+       }
+       print "</div>\n";
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       foreach my $line (@difftree) {
+               # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
+               # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
+               if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
+                       next;
+               }
+               my $from_mode = $1;
+               my $to_mode = $2;
+               my $from_id = $3;
+               my $to_id = $4;
+               my $status = $5;
+               my $similarity = $6;
+               my $file = validate_input(unquote($7));
+               if ($alternate) {
+                       print "<tr class=\"dark\">\n";
+               } else {
+                       print "<tr class=\"light\">\n";
+               }
+               $alternate ^= 1;
+               if ($status eq "A") {
+                       my $mode_chng = "";
+                       if (S_ISREG(oct $to_mode)) {
+                               $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
+                       }
+                       print "<td>" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
+                             "<td><span style=\"color: #008000;\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
+                             "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
+               } elsif ($status eq "D") {
+                       print "<td>" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
+                             "<td><span style=\"color: #c00000;\">[deleted " . file_type($from_mode). "]</span></td>\n" .
+                             "<td class=\"link\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") .
+                             "</td>\n"
+               } elsif ($status eq "M" || $status eq "T") {
+                       my $mode_chnge = "";
+                       if ($from_mode != $to_mode) {
+                               $mode_chnge = " <span style=\"color: #777777;\">[changed";
+                               if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
+                                       $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
+                               }
+                               if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
+                                       if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
+                                               $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
+                                       } elsif (S_ISREG($to_mode)) {
+                                               $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
+                                       }
+                               }
+                               $mode_chnge .= "]</span>\n";
+                       }
+                       print "<td>";
+                       if ($to_id ne $from_id) {
+                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
+                       } else {
+                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
+                       }
+                       print "</td>\n" .
+                             "<td>$mode_chnge</td>\n" .
+                             "<td class=\"link\">";
+                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
+                       if ($to_id ne $from_id) {
+                               print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
+                       }
+                       print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") . "\n";
+                       print "</td>\n";
+               } elsif ($status eq "R") {
+                       my ($from_file, $to_file) = split "\t", $file;
+                       my $mode_chng = "";
+                       if ($from_mode != $to_mode) {
+                               $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
+                       }
+                       print "<td>" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"), -class => "list"}, esc_html($to_file)) . "</td>\n" .
+                             "<td><span style=\"color: #777777;\">[moved from " .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$from_file"), -class => "list"}, esc_html($from_file)) .
+                             " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
+                             "<td class=\"link\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
+                       if ($to_id ne $from_id) {
+                               print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
+                       }
+                       print "</td>\n";
+               }
+               print "</tr>\n";
+       }
+       print "</table>\n";
+       git_footer_html();
+}
+
+sub git_blobdiff {
+       mkdir($git_temp, 0700);
+       git_header_html();
+       if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
+               print "<div class=\"page_nav\">\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") .
+                     "<br/>\n";
+               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain") .
+                     "</div>\n";
+               print "<div>\n" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" .
+                     "</div>\n";
+       } else {
+               print "<div class=\"page_nav\">\n" .
+                     "<br/><br/></div>\n" .
+                     "<div class=\"title\">$hash vs $hash_parent</div>\n";
+       }
+       if (defined $file_name) {
+               print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
+       }
+       print "<div class=\"page_body\">\n" .
+             "<div class=\"diff_info\">blob:" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
+             " -> blob:" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
+             "</div>\n";
+       git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
+       print "</div>";
+       git_footer_html();
+}
+
+sub git_blobdiff_plain {
+       mkdir($git_temp, 0700);
+       print $cgi->header(-type => "text/plain", -charset => 'utf-8');
+       git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
+}
+
+sub git_commitdiff {
+       mkdir($git_temp, 0700);
+       my %co = git_read_commit($hash);
+       if (!%co) {
+               die_error(undef, "Unknown commit object.");
+       }
+       if (!defined $hash_parent) {
+               $hash_parent = $co{'parent'};
+       }
+       open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
+       my (@difftree) = map { chomp; $_ } <$fd>;
+       close $fd or die_error(undef, "Reading diff-tree failed.");
+
+       # non-textual hash id's can be cached
+       my $expires;
+       if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
+               $expires = "+1d";
+       }
+       my $refs = read_info_ref();
+       my $ref = "";
+       if (defined $refs->{$co{'id'}}) {
+               $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
+       }
+       git_header_html(undef, $expires);
+       print "<div class=\"page_nav\">\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
+             " | commitdiff" .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "<br/>\n";
+       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain") . "\n" .
+             "</div>\n";
+       print "<div>\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
+             "</div>\n";
+       print "<div class=\"page_body\">\n";
+       my $comment = $co{'comment'};
+       my $empty = 0;
+       my $signed = 0;
+       my @log = @$comment;
+       # remove first and empty lines after that
+       shift @log;
+       while (defined $log[0] && $log[0] eq "") {
+               shift @log;
+       }
+       foreach my $line (@log) {
+               if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
+                       next;
+               }
+               if ($line eq "") {
+                       if ($empty) {
+                               next;
+                       }
+                       $empty = 1;
+               } else {
+                       $empty = 0;
+               }
+               print format_log_line_html($line) . "<br/>\n";
+       }
+       print "<br/>\n";
+       foreach my $line (@difftree) {
+               # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
+               # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
+               $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
+               my $from_mode = $1;
+               my $to_mode = $2;
+               my $from_id = $3;
+               my $to_id = $4;
+               my $status = $5;
+               my $file = validate_input(unquote($6));
+               if ($status eq "A") {
+                       print "<div class=\"diff_info\">" .  file_type($to_mode) . ":" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
+                             "</div>\n";
+                       git_diff_print(undef, "/dev/null", $to_id, "b/$file");
+               } elsif ($status eq "D") {
+                       print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
+                             "</div>\n";
+                       git_diff_print($from_id, "a/$file", undef, "/dev/null");
+               } elsif ($status eq "M") {
+                       if ($from_id ne $to_id) {
+                               print "<div class=\"diff_info\">" .
+                                     file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
+                                     " -> " .
+                                     file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
+                               print "</div>\n";
+                               git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
+                       }
+               }
+       }
+       print "<br/>\n" .
+             "</div>";
+       git_footer_html();
+}
+
+sub git_commitdiff_plain {
+       mkdir($git_temp, 0700);
+       open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
+       my (@difftree) = map { chomp; $_ } <$fd>;
+       close $fd or die_error(undef, "Reading diff-tree failed.");
+
+       # try to figure out the next tag after this commit
+       my $tagname;
+       my $refs = read_info_ref("tags");
+       open $fd, "-|", "$gitbin/git-rev-list HEAD";
+       chomp (my (@commits) = <$fd>);
+       close $fd;
+       foreach my $commit (@commits) {
+               if (defined $refs->{$commit}) {
+                       $tagname = $refs->{$commit}
+               }
+               if ($commit eq $hash) {
+                       last;
+               }
+       }
+
+       print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
+       my %co = git_read_commit($hash);
+       my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
+       my $comment = $co{'comment'};
+       print "From: $co{'author'}\n" .
+             "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
+             "Subject: $co{'title'}\n";
+       if (defined $tagname) {
+             print "X-Git-Tag: $tagname\n";
+       }
+       print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
+             "\n";
+
+       foreach my $line (@$comment) {;
+               print "$line\n";
+       }
+       print "---\n\n";
+
+       foreach my $line (@difftree) {
+               $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
+               my $from_id = $3;
+               my $to_id = $4;
+               my $status = $5;
+               my $file = $6;
+               if ($status eq "A") {
+                       git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
+               } elsif ($status eq "D") {
+                       git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
+               } elsif ($status eq "M") {
+                       git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
+               }
+       }
+}
+
+sub git_history {
+       if (!defined $hash) {
+               $hash = git_read_head($project);
+       }
+       my %co = git_read_commit($hash);
+       if (!%co) {
+               die_error(undef, "Unknown commit object.");
+       }
+       my $refs = read_info_ref();
+       git_header_html();
+       print "<div class=\"page_nav\">\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
+             "<br/><br/>\n" .
+             "</div>\n";
+       print "<div>\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
+             "</div>\n";
+       print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b><br/></div>\n";
+
+       open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -- \'$file_name\'";
+       my $commit;
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       while (my $line = <$fd>) {
+               if ($line =~ m/^([0-9a-fA-F]{40})/){
+                       $commit = $1;
+                       next;
+               }
+               if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/ && (defined $commit)) {
+                       my %co = git_read_commit($commit);
+                       if (!%co) {
+                               next;
+                       }
+                       my $ref = "";
+                       if (defined $refs->{$commit}) {
+                               $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
+                       }
+                       if ($alternate) {
+                               print "<tr class=\"dark\">\n";
+                       } else {
+                               print "<tr class=\"light\">\n";
+                       }
+                       $alternate ^= 1;
+                       print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
+                             "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
+                             "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
+                             esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
+                             "<td class=\"link\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=$commit;f=$file_name")}, "blob");
+                       my $blob = git_get_hash_by_path($hash, $file_name);
+                       my $blob_parent = git_get_hash_by_path($commit, $file_name);
+                       if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
+                               print " | " .
+                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
+                               "diff to current");
+                       }
+                       print "</td>\n" .
+                             "</tr>\n";
+                       undef $commit;
+               }
+       }
+       print "</table>\n";
+       close $fd;
+       git_footer_html();
+}
+
+sub git_search {
+       if (!defined $searchtext) {
+               die_error("", "Text field empty.");
+       }
+       if (!defined $hash) {
+               $hash = git_read_head($project);
+       }
+       my %co = git_read_commit($hash);
+       if (!%co) {
+               die_error(undef, "Unknown commit object.");
+       }
+       # pickaxe may take all resources of your box and run for several minutes
+       # with every query - so decide by yourself how public you make this feature :)
+       my $commit_search = 1;
+       my $author_search = 0;
+       my $committer_search = 0;
+       my $pickaxe_search = 0;
+       if ($searchtext =~ s/^author\\://i) {
+               $author_search = 1;
+       } elsif ($searchtext =~ s/^committer\\://i) {
+               $committer_search = 1;
+       } elsif ($searchtext =~ s/^pickaxe\\://i) {
+               $commit_search = 0;
+               $pickaxe_search = 1;
+       }
+       git_header_html();
+       print "<div class=\"page_nav\">\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary;h=$hash")}, "summary") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
+             "<br/><br/>\n" .
+             "</div>\n";
+
+       print "<div>\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
+             "</div>\n";
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       if ($commit_search) {
+               $/ = "\0";
+               open my $fd, "-|", "$gitbin/git-rev-list --header --parents $hash" or next;
+               while (my $commit_text = <$fd>) {
+                       if (!grep m/$searchtext/i, $commit_text) {
+                               next;
+                       }
+                       if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
+                               next;
+                       }
+                       if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
+                               next;
+                       }
+                       my @commit_lines = split "\n", $commit_text;
+                       my %co = git_read_commit(undef, \@commit_lines);
+                       if (!%co) {
+                               next;
+                       }
+                       if ($alternate) {
+                               print "<tr class=\"dark\">\n";
+                       } else {
+                               print "<tr class=\"light\">\n";
+                       }
+                       $alternate ^= 1;
+                       print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
+                             "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
+                             "<td>" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" . esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
+                       my $comment = $co{'comment'};
+                       foreach my $line (@$comment) {
+                               if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
+                                       my $lead = esc_html($1) || "";
+                                       $lead = chop_str($lead, 30, 10);
+                                       my $match = esc_html($2) || "";
+                                       my $trail = esc_html($3) || "";
+                                       $trail = chop_str($trail, 30, 10);
+                                       my $text = "$lead<span style=\"color:#e00000\">$match</span>$trail";
+                                       print chop_str($text, 80, 5) . "<br/>\n";
+                               }
+                       }
+                       print "</td>\n" .
+                             "<td class=\"link\">" .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
+                       print "</td>\n" .
+                             "</tr>\n";
+               }
+               close $fd;
+       }
+
+       if ($pickaxe_search) {
+               $/ = "\n";
+               open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S\'$searchtext\'";
+               undef %co;
+               my @files;
+               while (my $line = <$fd>) {
+                       if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
+                               my %set;
+                               $set{'file'} = $6;
+                               $set{'from_id'} = $3;
+                               $set{'to_id'} = $4;
+                               $set{'id'} = $set{'to_id'};
+                               if ($set{'id'} =~ m/0{40}/) {
+                                       $set{'id'} = $set{'from_id'};
+                               }
+                               if ($set{'id'} =~ m/0{40}/) {
+                                       next;
+                               }
+                               push @files, \%set;
+                       } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
+                               if (%co) {
+                                       if ($alternate) {
+                                               print "<tr class=\"dark\">\n";
+                                       } else {
+                                               print "<tr class=\"light\">\n";
+                                       }
+                                       $alternate ^= 1;
+                                       print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
+                                             "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
+                                             "<td>" .
+                                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
+                                             esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
+                                       while (my $setref = shift @files) {
+                                               my %set = %$setref;
+                                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
+                                                     "<span style=\"color:#e00000\">" . esc_html($set{'file'}) . "</span>") .
+                                                     "<br/>\n";
+                                       }
+                                       print "</td>\n" .
+                                             "<td class=\"link\">" .
+                                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
+                                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
+                                       print "</td>\n" .
+                                             "</tr>\n";
+                               }
+                               %co = git_read_commit($1);
+                       }
+               }
+               close $fd;
+       }
+       print "</table>\n";
+       git_footer_html();
+}
+
+sub git_shortlog {
+       my $head = git_read_head($project);
+       if (!defined $hash) {
+               $hash = $head;
+       }
+       if (!defined $page) {
+               $page = 0;
+       }
+       my $refs = read_info_ref();
+       git_header_html();
+       print "<div class=\"page_nav\">\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
+             " | shortlog" .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
+             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
+
+       my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
+       open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
+       my (@revlist) = map { chomp; $_ } <$fd>;
+       close $fd;
+
+       if ($hash ne $head || $page) {
+               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "HEAD");
+       } else {
+               print "HEAD";
+       }
+       if ($page > 0) {
+               print " &sdot; " .
+               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
+       } else {
+               print " &sdot; prev";
+       }
+       if ($#revlist >= (100 * ($page+1)-1)) {
+               print " &sdot; " .
+               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
+       } else {
+               print " &sdot; next";
+       }
+       print "<br/>\n" .
+             "</div>\n";
+       print "<div>\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
+             "</div>\n";
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       for (my $i = ($page * 100); $i <= $#revlist; $i++) {
+               my $commit = $revlist[$i];
+               my $ref = "";
+               if (defined $refs->{$commit}) {
+                       $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
+               }
+               my %co = git_read_commit($commit);
+               my %ad = date_str($co{'author_epoch'});
+               if ($alternate) {
+                       print "<tr class=\"dark\">\n";
+               } else {
+                       print "<tr class=\"light\">\n";
+               }
+               $alternate ^= 1;
+               print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
+                     "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
+                     "<td>";
+               if (length($co{'title_short'}) < length($co{'title'})) {
+                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
+                             "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
+               } else {
+                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
+                             "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
+               }
+               print "</td>\n" .
+                     "<td class=\"link\">" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
+                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
+                     "</td>\n" .
+                     "</tr>";
+       }
+       if ($#revlist >= (100 * ($page+1)-1)) {
+               print "<tr>\n" .
+                     "<td>" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -title => "Alt-n"}, "next") .
+                     "</td>\n" .
+                     "</tr>\n";
+       }
+       print "</table\n>";
+       git_footer_html();
+}
diff --git a/gitweb/test/Märchen b/gitweb/test/Märchen
new file mode 100644 (file)
index 0000000..8f7a1d3
--- /dev/null
@@ -0,0 +1,2 @@
+Märchen
+Märchen
diff --git a/gitweb/test/file with spaces b/gitweb/test/file with spaces
new file mode 100644 (file)
index 0000000..f108543
--- /dev/null
@@ -0,0 +1,4 @@
+This
+filename
+contains
+spaces.
diff --git a/gitweb/test/file+plus+sign b/gitweb/test/file+plus+sign
new file mode 100644 (file)
index 0000000..fd05278
--- /dev/null
@@ -0,0 +1,6 @@
+This
+filename
+contains
++
+plus
+chars.
index 861644b..da1a7f5 100644 (file)
@@ -399,6 +399,7 @@ void prefetch(unsigned char *sha1)
        snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
        snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
                 "%s.temp", filename);
+       newreq->slot = NULL;
        newreq->next = NULL;
 
        if (object_queue_head == NULL) {
@@ -583,10 +584,8 @@ static void process_alternates_response(void *callback_data)
                        // skip 'objects' at end
                        if (okay) {
                                target = xmalloc(serverlen + posn - i - 6);
-                               strncpy(target, base, serverlen);
-                               strncpy(target + serverlen, data + i,
-                                       posn - i - 7);
-                               target[serverlen + posn - i - 7] = '\0';
+                               safe_strncpy(target, base, serverlen);
+                               safe_strncpy(target + serverlen, data + i, posn - i - 6);
                                if (get_verbosely)
                                        fprintf(stderr,
                                                "Also look at %s\n", target);
@@ -727,8 +726,8 @@ xml_cdata(void *userData, const XML_Char *s, int len)
        struct xml_ctx *ctx = (struct xml_ctx *)userData;
        if (ctx->cdata)
                free(ctx->cdata);
-       ctx->cdata = xcalloc(len+1, 1);
-       strncpy(ctx->cdata, s, len);
+       ctx->cdata = xmalloc(len + 1);
+       safe_strncpy(ctx->cdata, s, len + 1);
 }
 
 static int remote_ls(struct alt_base *repo, const char *path, int flags,
@@ -1223,6 +1222,7 @@ int main(int argc, char **argv)
        int rc = 0;
 
        setup_git_directory();
+       git_config(git_default_config);
 
        while (arg < argc && argv[arg][0] == '-') {
                if (argv[arg][1] == 't') {
@@ -1249,6 +1249,7 @@ int main(int argc, char **argv)
        }
        commit_id = argv[arg];
        url = argv[arg + 1];
+       write_ref_log_details = url;
 
        http_init();
 
@@ -1269,10 +1270,10 @@ int main(int argc, char **argv)
        if (pull(commit_id))
                rc = 1;
 
-       curl_slist_free_all(no_pragma_header);
-
        http_cleanup();
 
+       curl_slist_free_all(no_pragma_header);
+
        if (corrupt_object_found) {
                fprintf(stderr,
 "Some loose object were found to be corrupt, but they might be just\n"
index b4327d9..2d9441e 100644 (file)
@@ -186,6 +186,7 @@ static void process_response(void *callback_data)
        finish_request(request);
 }
 
+#ifdef USE_CURL_MULTI
 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
                               void *data)
 {
@@ -349,6 +350,41 @@ static void start_fetch_loose(struct transfer_request *request)
        }
 }
 
+static void start_mkcol(struct transfer_request *request)
+{
+       char *hex = sha1_to_hex(request->obj->sha1);
+       struct active_request_slot *slot;
+       char *posn;
+
+       request->url = xmalloc(strlen(remote->url) + 13);
+       strcpy(request->url, remote->url);
+       posn = request->url + strlen(remote->url);
+       strcpy(posn, "objects/");
+       posn += 8;
+       memcpy(posn, hex, 2);
+       posn += 2;
+       strcpy(posn, "/");
+
+       slot = get_active_slot();
+       slot->callback_func = process_response;
+       slot->callback_data = request;
+       curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
+       curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
+       curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
+       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
+       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
+
+       if (start_active_slot(slot)) {
+               request->slot = slot;
+               request->state = RUN_MKCOL;
+       } else {
+               request->state = ABORTED;
+               free(request->url);
+               request->url = NULL;
+       }
+}
+#endif
+
 static void start_fetch_packed(struct transfer_request *request)
 {
        char *url;
@@ -438,40 +474,6 @@ static void start_fetch_packed(struct transfer_request *request)
        }
 }
 
-static void start_mkcol(struct transfer_request *request)
-{
-       char *hex = sha1_to_hex(request->obj->sha1);
-       struct active_request_slot *slot;
-       char *posn;
-
-       request->url = xmalloc(strlen(remote->url) + 13);
-       strcpy(request->url, remote->url);
-       posn = request->url + strlen(remote->url);
-       strcpy(posn, "objects/");
-       posn += 8;
-       memcpy(posn, hex, 2);
-       posn += 2;
-       strcpy(posn, "/");
-
-       slot = get_active_slot();
-       slot->callback_func = process_response;
-       slot->callback_data = request;
-       curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
-       curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
-       curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
-
-       if (start_active_slot(slot)) {
-               request->slot = slot;
-               request->state = RUN_MKCOL;
-       } else {
-               request->state = ABORTED;
-               free(request->url);
-               request->url = NULL;
-       }
-}
-
 static void start_put(struct transfer_request *request)
 {
        char *hex = sha1_to_hex(request->obj->sha1);
@@ -788,6 +790,7 @@ static void finish_request(struct transfer_request *request)
        }
 }
 
+#ifdef USE_CURL_MULTI
 void fill_active_slots(void)
 {
        struct transfer_request *request = request_queue_head;
@@ -821,6 +824,7 @@ void fill_active_slots(void)
                slot = slot->next;
        }
 }
+#endif
 
 static void get_remote_object_list(unsigned char parent);
 
@@ -851,8 +855,10 @@ static void add_fetch_request(struct object *obj)
        request->next = request_queue_head;
        request_queue_head = request;
 
+#ifdef USE_CURL_MULTI
        fill_active_slots();
        step_active_slots();
+#endif
 }
 
 static int add_send_request(struct object *obj, struct remote_lock *lock)
@@ -889,8 +895,10 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)
        request->next = request_queue_head;
        request_queue_head = request;
 
+#ifdef USE_CURL_MULTI
        fill_active_slots();
        step_active_slots();
+#endif
 
        return 1;
 }
@@ -1261,8 +1269,8 @@ xml_cdata(void *userData, const XML_Char *s, int len)
        struct xml_ctx *ctx = (struct xml_ctx *)userData;
        if (ctx->cdata)
                free(ctx->cdata);
-       ctx->cdata = xcalloc(len+1, 1);
-       strncpy(ctx->cdata, s, len);
+       ctx->cdata = xmalloc(len + 1);
+       safe_strncpy(ctx->cdata, s, len + 1);
 }
 
 static struct remote_lock *lock_remote(char *path, long timeout)
@@ -1464,7 +1472,7 @@ static void process_ls_object(struct remote_ls_ctx *ls)
                return;
        path += 8;
        obj_hex = xmalloc(strlen(path));
-       strncpy(obj_hex, path, 2);
+       safe_strncpy(obj_hex, path, 3);
        strcpy(obj_hex + 2, path + 3);
        one_remote_object(obj_hex);
        free(obj_hex);
@@ -1704,6 +1712,7 @@ static struct object_list **process_blob(struct blob *blob,
                return p;
 
        obj->flags |= SEEN;
+       name = strdup(name);
        return add_object(obj, p, path, name);
 }
 
@@ -1713,7 +1722,8 @@ static struct object_list **process_tree(struct tree *tree,
                                         const char *name)
 {
        struct object *obj = &tree->object;
-       struct tree_entry_list *entry;
+       struct tree_desc desc;
+       struct name_entry entry;
        struct name_path me;
 
        obj->flags |= LOCAL;
@@ -1724,21 +1734,23 @@ static struct object_list **process_tree(struct tree *tree,
                die("bad tree object %s", sha1_to_hex(obj->sha1));
 
        obj->flags |= SEEN;
+       name = strdup(name);
        p = add_object(obj, p, NULL, name);
        me.up = path;
        me.elem = name;
        me.elem_len = strlen(name);
-       entry = tree->entries;
-       tree->entries = NULL;
-       while (entry) {
-               struct tree_entry_list *next = entry->next;
-               if (entry->directory)
-                       p = process_tree(entry->item.tree, p, &me, entry->name);
+
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+
+       while (tree_entry(&desc, &entry)) {
+               if (S_ISDIR(entry.mode))
+                       p = process_tree(lookup_tree(entry.sha1), p, &me, name);
                else
-                       p = process_blob(entry->item.blob, p, &me, entry->name);
-               free(entry);
-               entry = next;
+                       p = process_blob(lookup_blob(entry.sha1), p, &me, name);
        }
+       free(tree->buffer);
+       tree->buffer = NULL;
        return p;
 }
 
@@ -2148,8 +2160,8 @@ static void fetch_symref(char *path, char **symref, unsigned char *sha1)
 
        /* If it's a symref, set the refname; otherwise try for a sha1 */
        if (!strncmp((char *)buffer.buffer, "ref: ", 5)) {
-               *symref = xcalloc(buffer.posn - 5, 1);
-               strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 6);
+               *symref = xmalloc(buffer.posn - 5);
+               safe_strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 5);
        } else {
                get_sha1_hex(buffer.buffer, sha1);
        }
@@ -2519,7 +2531,9 @@ int main(int argc, char **argv)
                if (objects_to_send)
                        fprintf(stderr, "    sending %d objects\n",
                                objects_to_send);
+#ifdef USE_CURL_MULTI
                fill_active_slots();
+#endif
                finish_all_active_slots();
 
                /* Update the remote branch if all went well */
diff --git a/http.c b/http.c
index 0cb42a8..08769cc 100644 (file)
--- a/http.c
+++ b/http.c
@@ -25,7 +25,6 @@ long curl_low_speed_limit = -1;
 long curl_low_speed_time = -1;
 
 struct curl_slist *pragma_header;
-struct curl_slist *no_range_header;
 
 struct active_request_slot *active_queue_head = NULL;
 
@@ -208,7 +207,6 @@ void http_init(void)
        curl_global_init(CURL_GLOBAL_ALL);
 
        pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
-       no_range_header = curl_slist_append(no_range_header, "Range:");
 
 #ifdef USE_CURL_MULTI
        {
@@ -288,7 +286,8 @@ void http_cleanup(void)
        curl_multi_cleanup(curlm);
 #endif
        curl_global_cleanup();
-       
+
+       curl_slist_free_all(pragma_header);
 }
 
 struct active_request_slot *get_active_slot(void)
@@ -344,9 +343,14 @@ struct active_request_slot *get_active_slot(void)
        slot->finished = NULL;
        slot->callback_data = NULL;
        slot->callback_func = NULL;
+       curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
-       curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
        curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
+       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
+       curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
+       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
+       curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
+       curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 
        return slot;
 }
@@ -435,11 +439,15 @@ void release_active_slot(struct active_request_slot *slot)
 {
        closedown_active_slot(slot);
        if (slot->curl) {
+#ifdef USE_CURL_MULTI
                curl_multi_remove_handle(curlm, slot->curl);
+#endif
                curl_easy_cleanup(slot->curl);
                slot->curl = NULL;
        }
+#ifdef USE_CURL_MULTI
        fill_active_slots();
+#endif
 }
 
 static void finish_active_slot(struct active_request_slot *slot)
diff --git a/ident.c b/ident.c
index 7c81fe8..7b44cbd 100644 (file)
--- a/ident.c
+++ b/ident.c
@@ -71,10 +71,9 @@ int setup_ident(void)
                len = strlen(git_default_email);
                git_default_email[len++] = '.';
                if (he && (domainname = strchr(he->h_name, '.')))
-                       strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
+                       safe_strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
                else
-                       strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
-               git_default_email[sizeof(git_default_email) - 1] = 0;
+                       safe_strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
        }
        /* And set the default date */
        datestamp(git_default_date, sizeof(git_default_date));
index 52e2400..285ad29 100644 (file)
@@ -924,6 +924,7 @@ imap_open_store( imap_server_conf_t *srvc )
        struct hostent *he;
        struct sockaddr_in addr;
        int s, a[2], preauth;
+       pid_t pid;
 
        ctx = xcalloc( sizeof(*ctx), 1 );
 
@@ -941,7 +942,10 @@ imap_open_store( imap_server_conf_t *srvc )
                        exit( 1 );
                }
 
-               if (fork() == 0) {
+               pid = fork();
+               if (pid < 0)
+                       _exit( 127 );
+               if (!pid) {
                        if (dup2( a[0], 0 ) == -1 || dup2( a[0], 1 ) == -1)
                                _exit( 127 );
                        close( a[0] );
diff --git a/index.c b/index.c
deleted file mode 100644 (file)
index f92b960..0000000
--- a/index.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright (c) 2005, Junio C Hamano
- */
-#include <signal.h>
-#include "cache.h"
-
-static struct cache_file *cache_file_list;
-
-static void remove_lock_file(void)
-{
-       while (cache_file_list) {
-               if (cache_file_list->lockfile[0])
-                       unlink(cache_file_list->lockfile);
-               cache_file_list = cache_file_list->next;
-       }
-}
-
-static void remove_lock_file_on_signal(int signo)
-{
-       remove_lock_file();
-       signal(SIGINT, SIG_DFL);
-       raise(signo);
-}
-
-int hold_index_file_for_update(struct cache_file *cf, const char *path)
-{
-       int fd;
-       sprintf(cf->lockfile, "%s.lock", path);
-       fd = open(cf->lockfile, O_RDWR | O_CREAT | O_EXCL, 0666);
-       if (fd >=0 && !cf->next) {
-               cf->next = cache_file_list;
-               cache_file_list = cf;
-               signal(SIGINT, remove_lock_file_on_signal);
-               atexit(remove_lock_file);
-       }
-       return fd;
-}
-
-int commit_index_file(struct cache_file *cf)
-{
-       char indexfile[PATH_MAX];
-       int i;
-       strcpy(indexfile, cf->lockfile);
-       i = strlen(indexfile) - 5; /* .lock */
-       indexfile[i] = 0;
-       i = rename(cf->lockfile, indexfile);
-       cf->lockfile[0] = 0;
-       return i;
-}
-
-void rollback_index_file(struct cache_file *cf)
-{
-       if (cf->lockfile[0])
-               unlink(cf->lockfile);
-       cf->lockfile[0] = 0;
-}
-
diff --git a/init-db.c b/init-db.c
deleted file mode 100644 (file)
index ff29496..0000000
--- a/init-db.c
+++ /dev/null
@@ -1,291 +0,0 @@
-/*
- * GIT - The information manager from hell
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#include "cache.h"
-
-#ifndef DEFAULT_GIT_TEMPLATE_DIR
-#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
-#endif
-
-static void safe_create_dir(const char *dir, int share)
-{
-       if (mkdir(dir, 0777) < 0) {
-               if (errno != EEXIST) {
-                       perror(dir);
-                       exit(1);
-               }
-       }
-       else if (share && adjust_shared_perm(dir))
-               die("Could not make %s writable by group\n", dir);
-}
-
-static int copy_file(const char *dst, const char *src, int mode)
-{
-       int fdi, fdo, status;
-
-       mode = (mode & 0111) ? 0777 : 0666;
-       if ((fdi = open(src, O_RDONLY)) < 0)
-               return fdi;
-       if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
-               close(fdi);
-               return fdo;
-       }
-       status = copy_fd(fdi, fdo);
-       close(fdo);
-
-       if (!status && adjust_shared_perm(dst))
-               return -1;
-
-       return status;
-}
-
-static void copy_templates_1(char *path, int baselen,
-                            char *template, int template_baselen,
-                            DIR *dir)
-{
-       struct dirent *de;
-
-       /* Note: if ".git/hooks" file exists in the repository being
-        * re-initialized, /etc/core-git/templates/hooks/update would
-        * cause git-init-db to fail here.  I think this is sane but
-        * it means that the set of templates we ship by default, along
-        * with the way the namespace under .git/ is organized, should
-        * be really carefully chosen.
-        */
-       safe_create_dir(path, 1);
-       while ((de = readdir(dir)) != NULL) {
-               struct stat st_git, st_template;
-               int namelen;
-               int exists = 0;
-
-               if (de->d_name[0] == '.')
-                       continue;
-               namelen = strlen(de->d_name);
-               if ((PATH_MAX <= baselen + namelen) ||
-                   (PATH_MAX <= template_baselen + namelen))
-                       die("insanely long template name %s", de->d_name);
-               memcpy(path + baselen, de->d_name, namelen+1);
-               memcpy(template + template_baselen, de->d_name, namelen+1);
-               if (lstat(path, &st_git)) {
-                       if (errno != ENOENT)
-                               die("cannot stat %s", path);
-               }
-               else
-                       exists = 1;
-
-               if (lstat(template, &st_template))
-                       die("cannot stat template %s", template);
-
-               if (S_ISDIR(st_template.st_mode)) {
-                       DIR *subdir = opendir(template);
-                       int baselen_sub = baselen + namelen;
-                       int template_baselen_sub = template_baselen + namelen;
-                       if (!subdir)
-                               die("cannot opendir %s", template);
-                       path[baselen_sub++] =
-                               template[template_baselen_sub++] = '/';
-                       path[baselen_sub] =
-                               template[template_baselen_sub] = 0;
-                       copy_templates_1(path, baselen_sub,
-                                        template, template_baselen_sub,
-                                        subdir);
-                       closedir(subdir);
-               }
-               else if (exists)
-                       continue;
-               else if (S_ISLNK(st_template.st_mode)) {
-                       char lnk[256];
-                       int len;
-                       len = readlink(template, lnk, sizeof(lnk));
-                       if (len < 0)
-                               die("cannot readlink %s", template);
-                       if (sizeof(lnk) <= len)
-                               die("insanely long symlink %s", template);
-                       lnk[len] = 0;
-                       if (symlink(lnk, path))
-                               die("cannot symlink %s %s", lnk, path);
-               }
-               else if (S_ISREG(st_template.st_mode)) {
-                       if (copy_file(path, template, st_template.st_mode))
-                               die("cannot copy %s to %s", template, path);
-               }
-               else
-                       error("ignoring template %s", template);
-       }
-}
-
-static void copy_templates(const char *git_dir, int len, char *template_dir)
-{
-       char path[PATH_MAX];
-       char template_path[PATH_MAX];
-       int template_len;
-       DIR *dir;
-
-       if (!template_dir)
-               template_dir = DEFAULT_GIT_TEMPLATE_DIR;
-       strcpy(template_path, template_dir);
-       template_len = strlen(template_path);
-       if (template_path[template_len-1] != '/') {
-               template_path[template_len++] = '/';
-               template_path[template_len] = 0;
-       }
-       dir = opendir(template_path);
-       if (!dir) {
-               fprintf(stderr, "warning: templates not found %s\n",
-                       template_dir);
-               return;
-       }
-
-       /* Make sure that template is from the correct vintage */
-       strcpy(template_path + template_len, "config");
-       repository_format_version = 0;
-       git_config_from_file(check_repository_format_version,
-                            template_path);
-       template_path[template_len] = 0;
-
-       if (repository_format_version &&
-           repository_format_version != GIT_REPO_VERSION) {
-               fprintf(stderr, "warning: not copying templates of "
-                       "a wrong format version %d from '%s'\n",
-                       repository_format_version,
-                       template_dir);
-               closedir(dir);
-               return;
-       }
-
-       memcpy(path, git_dir, len);
-       path[len] = 0;
-       copy_templates_1(path, len,
-                        template_path, template_len,
-                        dir);
-       closedir(dir);
-}
-
-static void create_default_files(const char *git_dir, char *template_path)
-{
-       unsigned len = strlen(git_dir);
-       static char path[PATH_MAX];
-       unsigned char sha1[20];
-       struct stat st1;
-       char repo_version_string[10];
-
-       if (len > sizeof(path)-50)
-               die("insane git directory %s", git_dir);
-       memcpy(path, git_dir, len);
-
-       if (len && path[len-1] != '/')
-               path[len++] = '/';
-
-       /*
-        * Create .git/refs/{heads,tags}
-        */
-       strcpy(path + len, "refs");
-       safe_create_dir(path, 1);
-       strcpy(path + len, "refs/heads");
-       safe_create_dir(path, 1);
-       strcpy(path + len, "refs/tags");
-       safe_create_dir(path, 1);
-
-       /* First copy the templates -- we might have the default
-        * config file there, in which case we would want to read
-        * from it after installing.
-        */
-       path[len] = 0;
-       copy_templates(path, len, template_path);
-
-       git_config(git_default_config);
-
-       /*
-        * Create the default symlink from ".git/HEAD" to the "master"
-        * branch, if it does not exist yet.
-        */
-       strcpy(path + len, "HEAD");
-       if (read_ref(path, sha1) < 0) {
-               if (create_symref(path, "refs/heads/master") < 0)
-                       exit(1);
-       }
-
-       /* This forces creation of new config file */
-       sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
-       git_config_set("core.repositoryformatversion", repo_version_string);
-
-       path[len] = 0;
-       strcpy(path + len, "config");
-
-       /* Check filemode trustability */
-       if (!lstat(path, &st1)) {
-               struct stat st2;
-               int filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
-                               !lstat(path, &st2) &&
-                               st1.st_mode != st2.st_mode);
-               git_config_set("core.filemode",
-                              filemode ? "true" : "false");
-       }
-}
-
-static const char init_db_usage[] =
-"git-init-db [--template=<template-directory>] [--shared]";
-
-/*
- * If you want to, you can share the DB area with any number of branches.
- * That has advantages: you can save space by sharing all the SHA1 objects.
- * On the other hand, it might just make lookup slower and messier. You
- * be the judge.  The default case is to have one DB per managed directory.
- */
-int main(int argc, char **argv)
-{
-       const char *git_dir;
-       const char *sha1_dir;
-       char *path, *template_dir = NULL;
-       int len, i;
-
-       for (i = 1; i < argc; i++, argv++) {
-               char *arg = argv[1];
-               if (!strncmp(arg, "--template=", 11))
-                       template_dir = arg+11;
-               else if (!strcmp(arg, "--shared"))
-                       shared_repository = 1;
-               else
-                       die(init_db_usage);
-       }
-
-       /*
-        * Set up the default .git directory contents
-        */
-       git_dir = getenv(GIT_DIR_ENVIRONMENT);
-       if (!git_dir) {
-               git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
-               fprintf(stderr, "defaulting to local storage area\n");
-       }
-       safe_create_dir(git_dir, 0);
-
-       /* Check to see if the repository version is right.
-        * Note that a newly created repository does not have
-        * config file, so this will not fail.  What we are catching
-        * is an attempt to reinitialize new repository with an old tool.
-        */
-       check_repository_format();
-
-       create_default_files(git_dir, template_dir);
-
-       /*
-        * And set up the object store.
-        */
-       sha1_dir = get_object_directory();
-       len = strlen(sha1_dir);
-       path = xmalloc(len + 40);
-       memcpy(path, sha1_dir, len);
-
-       safe_create_dir(sha1_dir, 1);
-       strcpy(path+len, "/pack");
-       safe_create_dir(path, 1);
-       strcpy(path+len, "/info");
-       safe_create_dir(path, 1);
-
-       if (shared_repository)
-               git_config_set("core.sharedRepository", "true");
-
-       return 0;
-}
index fa9e697..ffa4887 100644 (file)
@@ -208,6 +208,7 @@ int main(int argc, char **argv)
        int arg = 1;
 
        setup_git_directory();
+       git_config(git_default_config);
 
        while (arg < argc && argv[arg][0] == '-') {
                if (argv[arg][1] == 't')
@@ -239,6 +240,7 @@ int main(int argc, char **argv)
                usage(local_pull_usage);
        commit_id = argv[arg];
        path = argv[arg + 1];
+       write_ref_log_details = path;
 
        if (pull(commit_id))
                return 1;
diff --git a/lockfile.c b/lockfile.c
new file mode 100644 (file)
index 0000000..2346e0e
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2005, Junio C Hamano
+ */
+#include <signal.h>
+#include "cache.h"
+
+static struct lock_file *lock_file_list;
+
+static void remove_lock_file(void)
+{
+       while (lock_file_list) {
+               if (lock_file_list->filename[0])
+                       unlink(lock_file_list->filename);
+               lock_file_list = lock_file_list->next;
+       }
+}
+
+static void remove_lock_file_on_signal(int signo)
+{
+       remove_lock_file();
+       signal(SIGINT, SIG_DFL);
+       raise(signo);
+}
+
+int hold_lock_file_for_update(struct lock_file *lk, const char *path)
+{
+       int fd;
+       sprintf(lk->filename, "%s.lock", path);
+       fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
+       if (0 <= fd) {
+               if (!lk->next) {
+                       lk->next = lock_file_list;
+                       lock_file_list = lk;
+                       signal(SIGINT, remove_lock_file_on_signal);
+                       atexit(remove_lock_file);
+               }
+               if (adjust_shared_perm(lk->filename))
+                       return error("cannot fix permission bits on %s",
+                                    lk->filename);
+       }
+       return fd;
+}
+
+int commit_lock_file(struct lock_file *lk)
+{
+       char result_file[PATH_MAX];
+       int i;
+       strcpy(result_file, lk->filename);
+       i = strlen(result_file) - 5; /* .lock */
+       result_file[i] = 0;
+       i = rename(lk->filename, result_file);
+       lk->filename[0] = 0;
+       return i;
+}
+
+void rollback_lock_file(struct lock_file *lk)
+{
+       if (lk->filename[0])
+               unlink(lk->filename);
+       lk->filename[0] = 0;
+}
+
index 9634c46..ebb49f2 100644 (file)
@@ -3,6 +3,46 @@
 #include "commit.h"
 #include "log-tree.h"
 
+static void show_parents(struct commit *commit, int abbrev)
+{
+       struct commit_list *p;
+       for (p = commit->parents; p ; p = p->next) {
+               struct commit *parent = p->item;
+               printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
+       }
+}
+
+static int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
+{
+       int signoff_len = strlen(signoff);
+       static const char signed_off_by[] = "Signed-off-by: ";
+       char *cp = buf;
+
+       /* Do we have enough space to add it? */
+       if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 2)
+               return at;
+
+       /* First see if we already have the sign-off by the signer */
+       while (1) {
+               cp = strstr(cp, signed_off_by);
+               if (!cp)
+                       break;
+               cp += strlen(signed_off_by);
+               if ((cp + signoff_len < buf + at) &&
+                   !strncmp(cp, signoff, signoff_len) &&
+                   isspace(cp[signoff_len]))
+                       return at; /* we already have him */
+       }
+
+       strcpy(buf + at, signed_off_by);
+       at += strlen(signed_off_by);
+       strcpy(buf + at, signoff);
+       at += signoff_len;
+       buf[at++] = '\n';
+       buf[at] = 0;
+       return at;
+}
+
 void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
 {
        static char this_header[16384];
@@ -11,10 +51,14 @@ void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
        const char *extra;
        int len;
+       const char *subject = NULL, *extra_headers = opt->extra_headers;
 
        opt->loginfo = NULL;
        if (!opt->verbose_header) {
-               puts(sha1_to_hex(commit->object.sha1));
+               fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
+               if (opt->parents)
+                       show_parents(commit, abbrev_commit);
+               putchar('\n');
                return;
        }
 
@@ -37,17 +81,73 @@ void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
        /*
         * Print header line of header..
         */
-       printf("%s%s",
-               opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
-               diff_unique_abbrev(commit->object.sha1, abbrev_commit));
-       if (parent) 
-               printf(" (from %s)", diff_unique_abbrev(parent->object.sha1, abbrev_commit));
-       putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
+
+       if (opt->commit_format == CMIT_FMT_EMAIL) {
+               char *sha1 = sha1_to_hex(commit->object.sha1);
+               if (opt->total > 0) {
+                       static char buffer[64];
+                       snprintf(buffer, sizeof(buffer),
+                                       "Subject: [PATCH %d/%d] ",
+                                       opt->nr, opt->total);
+                       subject = buffer;
+               } else if (opt->total == 0)
+                       subject = "Subject: [PATCH] ";
+               else
+                       subject = "Subject: ";
+
+               printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
+               if (opt->mime_boundary) {
+                       static char subject_buffer[1024];
+                       static char buffer[1024];
+                       snprintf(subject_buffer, sizeof(subject_buffer) - 1,
+                                "%s"
+                                "MIME-Version: 1.0\n"
+                                "Content-Type: multipart/mixed;\n"
+                                " boundary=\"%s%s\"\n"
+                                "\n"
+                                "This is a multi-part message in MIME "
+                                "format.\n"
+                                "--%s%s\n"
+                                "Content-Type: text/plain; "
+                                "charset=UTF-8; format=fixed\n"
+                                "Content-Transfer-Encoding: 8bit\n\n",
+                                extra_headers ? extra_headers : "",
+                                mime_boundary_leader, opt->mime_boundary,
+                                mime_boundary_leader, opt->mime_boundary);
+                       extra_headers = subject_buffer;
+
+                       snprintf(buffer, sizeof(buffer) - 1,
+                                "--%s%s\n"
+                                "Content-Type: text/x-patch;\n"
+                                " name=\"%s.diff\"\n"
+                                "Content-Transfer-Encoding: 8bit\n"
+                                "Content-Disposition: inline;\n"
+                                " filename=\"%s.diff\"\n\n",
+                                mime_boundary_leader, opt->mime_boundary,
+                                sha1, sha1);
+                       opt->diffopt.stat_sep = buffer;
+               }
+       } else {
+               printf("%s%s",
+                      opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
+                      diff_unique_abbrev(commit->object.sha1, abbrev_commit));
+               if (opt->parents)
+                       show_parents(commit, abbrev_commit);
+               if (parent)
+                       printf(" (from %s)",
+                              diff_unique_abbrev(parent->object.sha1,
+                                                 abbrev_commit));
+               putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
+       }
 
        /*
         * And then the pretty-printed message itself
         */
-       len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev);
+       len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev, subject, extra_headers);
+
+       if (opt->add_signoff)
+               len = append_signoff(this_header, sizeof(this_header), len,
+                                    opt->add_signoff);
        printf("%s%s%s", this_header, extra, sep);
 }
 
@@ -152,15 +252,18 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
 int log_tree_commit(struct rev_info *opt, struct commit *commit)
 {
        struct log_info log;
+       int shown;
 
        log.commit = commit;
        log.parent = NULL;
        opt->loginfo = &log;
 
-       if (!log_tree_diff(opt, commit, &log) && opt->loginfo && opt->always_show_header) {
+       shown = log_tree_diff(opt, commit, &log);
+       if (!shown && opt->loginfo && opt->always_show_header) {
                log.parent = NULL;
                show_log(opt, opt->loginfo, "");
+               shown = 1;
        }
        opt->loginfo = NULL;
-       return 0;
+       return shown;
 }
diff --git a/ls-files.c b/ls-files.c
deleted file mode 100644 (file)
index 4a4af1c..0000000
+++ /dev/null
@@ -1,823 +0,0 @@
-/*
- * This merges the file listing in the directory cache index
- * with the actual working directory list, and shows different
- * combinations of the two.
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#include <dirent.h>
-#include <fnmatch.h>
-
-#include "cache.h"
-#include "quote.h"
-
-static int abbrev = 0;
-static int show_deleted = 0;
-static int show_cached = 0;
-static int show_others = 0;
-static int show_ignored = 0;
-static int show_stage = 0;
-static int show_unmerged = 0;
-static int show_modified = 0;
-static int show_killed = 0;
-static int show_other_directories = 0;
-static int hide_empty_directories = 0;
-static int show_valid_bit = 0;
-static int line_terminator = '\n';
-
-static int prefix_len = 0, prefix_offset = 0;
-static const char *prefix = NULL;
-static const char **pathspec = NULL;
-static int error_unmatch = 0;
-static char *ps_matched = NULL;
-
-static const char *tag_cached = "";
-static const char *tag_unmerged = "";
-static const char *tag_removed = "";
-static const char *tag_other = "";
-static const char *tag_killed = "";
-static const char *tag_modified = "";
-
-static const char *exclude_per_dir = NULL;
-
-/* We maintain three exclude pattern lists:
- * EXC_CMDL lists patterns explicitly given on the command line.
- * EXC_DIRS lists patterns obtained from per-directory ignore files.
- * EXC_FILE lists patterns from fallback ignore files.
- */
-#define EXC_CMDL 0
-#define EXC_DIRS 1
-#define EXC_FILE 2
-static struct exclude_list {
-       int nr;
-       int alloc;
-       struct exclude {
-               const char *pattern;
-               const char *base;
-               int baselen;
-       } **excludes;
-} exclude_list[3];
-
-static void add_exclude(const char *string, const char *base,
-                       int baselen, struct exclude_list *which)
-{
-       struct exclude *x = xmalloc(sizeof (*x));
-
-       x->pattern = string;
-       x->base = base;
-       x->baselen = baselen;
-       if (which->nr == which->alloc) {
-               which->alloc = alloc_nr(which->alloc);
-               which->excludes = realloc(which->excludes,
-                                         which->alloc * sizeof(x));
-       }
-       which->excludes[which->nr++] = x;
-}
-
-static int add_excludes_from_file_1(const char *fname,
-                                   const char *base,
-                                   int baselen,
-                                   struct exclude_list *which)
-{
-       int fd, i;
-       long size;
-       char *buf, *entry;
-
-       fd = open(fname, O_RDONLY);
-       if (fd < 0)
-               goto err;
-       size = lseek(fd, 0, SEEK_END);
-       if (size < 0)
-               goto err;
-       lseek(fd, 0, SEEK_SET);
-       if (size == 0) {
-               close(fd);
-               return 0;
-       }
-       buf = xmalloc(size+1);
-       if (read(fd, buf, size) != size)
-               goto err;
-       close(fd);
-
-       buf[size++] = '\n';
-       entry = buf;
-       for (i = 0; i < size; i++) {
-               if (buf[i] == '\n') {
-                       if (entry != buf + i && entry[0] != '#') {
-                               buf[i - (i && buf[i-1] == '\r')] = 0;
-                               add_exclude(entry, base, baselen, which);
-                       }
-                       entry = buf + i + 1;
-               }
-       }
-       return 0;
-
- err:
-       if (0 <= fd)
-               close(fd);
-       return -1;
-}
-
-static void add_excludes_from_file(const char *fname)
-{
-       if (add_excludes_from_file_1(fname, "", 0,
-                                    &exclude_list[EXC_FILE]) < 0)
-               die("cannot use %s as an exclude file", fname);
-}
-
-static int push_exclude_per_directory(const char *base, int baselen)
-{
-       char exclude_file[PATH_MAX];
-       struct exclude_list *el = &exclude_list[EXC_DIRS];
-       int current_nr = el->nr;
-
-       if (exclude_per_dir) {
-               memcpy(exclude_file, base, baselen);
-               strcpy(exclude_file + baselen, exclude_per_dir);
-               add_excludes_from_file_1(exclude_file, base, baselen, el);
-       }
-       return current_nr;
-}
-
-static void pop_exclude_per_directory(int stk)
-{
-       struct exclude_list *el = &exclude_list[EXC_DIRS];
-
-       while (stk < el->nr)
-               free(el->excludes[--el->nr]);
-}
-
-/* Scan the list and let the last match determines the fate.
- * Return 1 for exclude, 0 for include and -1 for undecided.
- */
-static int excluded_1(const char *pathname,
-                     int pathlen,
-                     struct exclude_list *el)
-{
-       int i;
-
-       if (el->nr) {
-               for (i = el->nr - 1; 0 <= i; i--) {
-                       struct exclude *x = el->excludes[i];
-                       const char *exclude = x->pattern;
-                       int to_exclude = 1;
-
-                       if (*exclude == '!') {
-                               to_exclude = 0;
-                               exclude++;
-                       }
-
-                       if (!strchr(exclude, '/')) {
-                               /* match basename */
-                               const char *basename = strrchr(pathname, '/');
-                               basename = (basename) ? basename+1 : pathname;
-                               if (fnmatch(exclude, basename, 0) == 0)
-                                       return to_exclude;
-                       }
-                       else {
-                               /* match with FNM_PATHNAME:
-                                * exclude has base (baselen long) implicitly
-                                * in front of it.
-                                */
-                               int baselen = x->baselen;
-                               if (*exclude == '/')
-                                       exclude++;
-
-                               if (pathlen < baselen ||
-                                   (baselen && pathname[baselen-1] != '/') ||
-                                   strncmp(pathname, x->base, baselen))
-                                   continue;
-
-                               if (fnmatch(exclude, pathname+baselen,
-                                           FNM_PATHNAME) == 0)
-                                       return to_exclude;
-                       }
-               }
-       }
-       return -1; /* undecided */
-}
-
-static int excluded(const char *pathname)
-{
-       int pathlen = strlen(pathname);
-       int st;
-
-       for (st = EXC_CMDL; st <= EXC_FILE; st++) {
-               switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
-               case 0:
-                       return 0;
-               case 1:
-                       return 1;
-               }
-       }
-       return 0;
-}
-
-struct nond_on_fs {
-       int len;
-       char name[FLEX_ARRAY]; /* more */
-};
-
-static struct nond_on_fs **dir;
-static int nr_dir;
-static int dir_alloc;
-
-static void add_name(const char *pathname, int len)
-{
-       struct nond_on_fs *ent;
-
-       if (cache_name_pos(pathname, len) >= 0)
-               return;
-
-       if (nr_dir == dir_alloc) {
-               dir_alloc = alloc_nr(dir_alloc);
-               dir = xrealloc(dir, dir_alloc*sizeof(ent));
-       }
-       ent = xmalloc(sizeof(*ent) + len + 1);
-       ent->len = len;
-       memcpy(ent->name, pathname, len);
-       ent->name[len] = 0;
-       dir[nr_dir++] = ent;
-}
-
-static int dir_exists(const char *dirname, int len)
-{
-       int pos = cache_name_pos(dirname, len);
-       if (pos >= 0)
-               return 1;
-       pos = -pos-1;
-       if (pos >= active_nr) /* can't */
-               return 0;
-       return !strncmp(active_cache[pos]->name, dirname, len);
-}
-
-/*
- * Read a directory tree. We currently ignore anything but
- * directories, regular files and symlinks. That's because git
- * doesn't handle them at all yet. Maybe that will change some
- * day.
- *
- * Also, we ignore the name ".git" (even if it is not a directory).
- * That likely will not change.
- */
-static int read_directory(const char *path, const char *base, int baselen)
-{
-       DIR *fdir = opendir(path);
-       int contents = 0;
-
-       if (fdir) {
-               int exclude_stk;
-               struct dirent *de;
-               char fullname[MAXPATHLEN + 1];
-               memcpy(fullname, base, baselen);
-
-               exclude_stk = push_exclude_per_directory(base, baselen);
-
-               while ((de = readdir(fdir)) != NULL) {
-                       int len;
-
-                       if ((de->d_name[0] == '.') &&
-                           (de->d_name[1] == 0 ||
-                            !strcmp(de->d_name + 1, ".") ||
-                            !strcmp(de->d_name + 1, "git")))
-                               continue;
-                       len = strlen(de->d_name);
-                       memcpy(fullname + baselen, de->d_name, len+1);
-                       if (excluded(fullname) != show_ignored) {
-                               if (!show_ignored || DTYPE(de) != DT_DIR) {
-                                       continue;
-                               }
-                       }
-
-                       switch (DTYPE(de)) {
-                       struct stat st;
-                       int subdir, rewind_base;
-                       default:
-                               continue;
-                       case DT_UNKNOWN:
-                               if (lstat(fullname, &st))
-                                       continue;
-                               if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
-                                       break;
-                               if (!S_ISDIR(st.st_mode))
-                                       continue;
-                               /* fallthrough */
-                       case DT_DIR:
-                               memcpy(fullname + baselen + len, "/", 2);
-                               len++;
-                               rewind_base = nr_dir;
-                               subdir = read_directory(fullname, fullname,
-                                                       baselen + len);
-                               if (show_other_directories &&
-                                   (subdir || !hide_empty_directories) &&
-                                   !dir_exists(fullname, baselen + len)) {
-                                       // Rewind the read subdirectory
-                                       while (nr_dir > rewind_base)
-                                               free(dir[--nr_dir]);
-                                       break;
-                               }
-                               contents += subdir;
-                               continue;
-                       case DT_REG:
-                       case DT_LNK:
-                               break;
-                       }
-                       add_name(fullname, baselen + len);
-                       contents++;
-               }
-               closedir(fdir);
-
-               pop_exclude_per_directory(exclude_stk);
-       }
-
-       return contents;
-}
-
-static int cmp_name(const void *p1, const void *p2)
-{
-       const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
-       const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
-
-       return cache_name_compare(e1->name, e1->len,
-                                 e2->name, e2->len);
-}
-
-/*
- * Match a pathspec against a filename. The first "len" characters
- * are the common prefix
- */
-static int match(const char **spec, char *ps_matched,
-                const char *filename, int len)
-{
-       const char *m;
-
-       while ((m = *spec++) != NULL) {
-               int matchlen = strlen(m + len);
-
-               if (!matchlen)
-                       goto matched;
-               if (!strncmp(m + len, filename + len, matchlen)) {
-                       if (m[len + matchlen - 1] == '/')
-                               goto matched;
-                       switch (filename[len + matchlen]) {
-                       case '/': case '\0':
-                               goto matched;
-                       }
-               }
-               if (!fnmatch(m + len, filename + len, 0))
-                       goto matched;
-               if (ps_matched)
-                       ps_matched++;
-               continue;
-       matched:
-               if (ps_matched)
-                       *ps_matched = 1;
-               return 1;
-       }
-       return 0;
-}
-
-static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
-{
-       int len = prefix_len;
-       int offset = prefix_offset;
-
-       if (len >= ent->len)
-               die("git-ls-files: internal error - directory entry not superset of prefix");
-
-       if (pathspec && !match(pathspec, ps_matched, ent->name, len))
-               return;
-
-       fputs(tag, stdout);
-       write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
-       putchar(line_terminator);
-}
-
-static void show_other_files(void)
-{
-       int i;
-       for (i = 0; i < nr_dir; i++) {
-               /* We should not have a matching entry, but we
-                * may have an unmerged entry for this path.
-                */
-               struct nond_on_fs *ent = dir[i];
-               int pos = cache_name_pos(ent->name, ent->len);
-               struct cache_entry *ce;
-               if (0 <= pos)
-                       die("bug in show-other-files");
-               pos = -pos - 1;
-               if (pos < active_nr) { 
-                       ce = active_cache[pos];
-                       if (ce_namelen(ce) == ent->len &&
-                           !memcmp(ce->name, ent->name, ent->len))
-                               continue; /* Yup, this one exists unmerged */
-               }
-               show_dir_entry(tag_other, ent);
-       }
-}
-
-static void show_killed_files(void)
-{
-       int i;
-       for (i = 0; i < nr_dir; i++) {
-               struct nond_on_fs *ent = dir[i];
-               char *cp, *sp;
-               int pos, len, killed = 0;
-
-               for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
-                       sp = strchr(cp, '/');
-                       if (!sp) {
-                               /* If ent->name is prefix of an entry in the
-                                * cache, it will be killed.
-                                */
-                               pos = cache_name_pos(ent->name, ent->len);
-                               if (0 <= pos)
-                                       die("bug in show-killed-files");
-                               pos = -pos - 1;
-                               while (pos < active_nr &&
-                                      ce_stage(active_cache[pos]))
-                                       pos++; /* skip unmerged */
-                               if (active_nr <= pos)
-                                       break;
-                               /* pos points at a name immediately after
-                                * ent->name in the cache.  Does it expect
-                                * ent->name to be a directory?
-                                */
-                               len = ce_namelen(active_cache[pos]);
-                               if ((ent->len < len) &&
-                                   !strncmp(active_cache[pos]->name,
-                                            ent->name, ent->len) &&
-                                   active_cache[pos]->name[ent->len] == '/')
-                                       killed = 1;
-                               break;
-                       }
-                       if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
-                               /* If any of the leading directories in
-                                * ent->name is registered in the cache,
-                                * ent->name will be killed.
-                                */
-                               killed = 1;
-                               break;
-                       }
-               }
-               if (killed)
-                       show_dir_entry(tag_killed, dir[i]);
-       }
-}
-
-static void show_ce_entry(const char *tag, struct cache_entry *ce)
-{
-       int len = prefix_len;
-       int offset = prefix_offset;
-
-       if (len >= ce_namelen(ce))
-               die("git-ls-files: internal error - cache entry not superset of prefix");
-
-       if (pathspec && !match(pathspec, ps_matched, ce->name, len))
-               return;
-
-       if (tag && *tag && show_valid_bit &&
-           (ce->ce_flags & htons(CE_VALID))) {
-               static char alttag[4];
-               memcpy(alttag, tag, 3);
-               if (isalpha(tag[0]))
-                       alttag[0] = tolower(tag[0]);
-               else if (tag[0] == '?')
-                       alttag[0] = '!';
-               else {
-                       alttag[0] = 'v';
-                       alttag[1] = tag[0];
-                       alttag[2] = ' ';
-                       alttag[3] = 0;
-               }
-               tag = alttag;
-       }
-
-       if (!show_stage) {
-               fputs(tag, stdout);
-               write_name_quoted("", 0, ce->name + offset,
-                                 line_terminator, stdout);
-               putchar(line_terminator);
-       }
-       else {
-               printf("%s%06o %s %d\t",
-                      tag,
-                      ntohl(ce->ce_mode),
-                      abbrev ? find_unique_abbrev(ce->sha1,abbrev)
-                               : sha1_to_hex(ce->sha1),
-                      ce_stage(ce));
-               write_name_quoted("", 0, ce->name + offset,
-                                 line_terminator, stdout);
-               putchar(line_terminator);
-       }
-}
-
-static void show_files(void)
-{
-       int i;
-
-       /* For cached/deleted files we don't need to even do the readdir */
-       if (show_others || show_killed) {
-               const char *path = ".", *base = "";
-               int baselen = prefix_len;
-
-               if (baselen) {
-                       path = base = prefix;
-                       if (exclude_per_dir) {
-                               char *p, *pp = xmalloc(baselen+1);
-                               memcpy(pp, prefix, baselen+1);
-                               p = pp;
-                               while (1) {
-                                       char save = *p;
-                                       *p = 0;
-                                       push_exclude_per_directory(pp, p-pp);
-                                       *p++ = save;
-                                       if (!save)
-                                               break;
-                                       p = strchr(p, '/');
-                                       if (p)
-                                               p++;
-                                       else
-                                               p = pp + baselen;
-                               }
-                               free(pp);
-                       }
-               }
-               read_directory(path, base, baselen);
-               qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
-               if (show_others)
-                       show_other_files();
-               if (show_killed)
-                       show_killed_files();
-       }
-       if (show_cached | show_stage) {
-               for (i = 0; i < active_nr; i++) {
-                       struct cache_entry *ce = active_cache[i];
-                       if (excluded(ce->name) != show_ignored)
-                               continue;
-                       if (show_unmerged && !ce_stage(ce))
-                               continue;
-                       show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
-               }
-       }
-       if (show_deleted | show_modified) {
-               for (i = 0; i < active_nr; i++) {
-                       struct cache_entry *ce = active_cache[i];
-                       struct stat st;
-                       int err;
-                       if (excluded(ce->name) != show_ignored)
-                               continue;
-                       err = lstat(ce->name, &st);
-                       if (show_deleted && err)
-                               show_ce_entry(tag_removed, ce);
-                       if (show_modified && ce_modified(ce, &st, 0))
-                               show_ce_entry(tag_modified, ce);
-               }
-       }
-}
-
-/*
- * Prune the index to only contain stuff starting with "prefix"
- */
-static void prune_cache(void)
-{
-       int pos = cache_name_pos(prefix, prefix_len);
-       unsigned int first, last;
-
-       if (pos < 0)
-               pos = -pos-1;
-       active_cache += pos;
-       active_nr -= pos;
-       first = 0;
-       last = active_nr;
-       while (last > first) {
-               int next = (last + first) >> 1;
-               struct cache_entry *ce = active_cache[next];
-               if (!strncmp(ce->name, prefix, prefix_len)) {
-                       first = next+1;
-                       continue;
-               }
-               last = next;
-       }
-       active_nr = last;
-}
-
-static void verify_pathspec(void)
-{
-       const char **p, *n, *prev;
-       char *real_prefix;
-       unsigned long max;
-
-       prev = NULL;
-       max = PATH_MAX;
-       for (p = pathspec; (n = *p) != NULL; p++) {
-               int i, len = 0;
-               for (i = 0; i < max; i++) {
-                       char c = n[i];
-                       if (prev && prev[i] != c)
-                               break;
-                       if (!c || c == '*' || c == '?')
-                               break;
-                       if (c == '/')
-                               len = i+1;
-               }
-               prev = n;
-               if (len < max) {
-                       max = len;
-                       if (!max)
-                               break;
-               }
-       }
-
-       if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
-               die("git-ls-files: cannot generate relative filenames containing '..'");
-
-       real_prefix = NULL;
-       prefix_len = max;
-       if (max) {
-               real_prefix = xmalloc(max + 1);
-               memcpy(real_prefix, prev, max);
-               real_prefix[max] = 0;
-       }
-       prefix = real_prefix;
-}
-
-static const char ls_files_usage[] =
-       "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
-       "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
-       "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
-       "[--] [<file>]*";
-
-int main(int argc, const char **argv)
-{
-       int i;
-       int exc_given = 0;
-
-       prefix = setup_git_directory();
-       if (prefix)
-               prefix_offset = strlen(prefix);
-       git_config(git_default_config);
-
-       for (i = 1; i < argc; i++) {
-               const char *arg = argv[i];
-
-               if (!strcmp(arg, "--")) {
-                       i++;
-                       break;
-               }
-               if (!strcmp(arg, "-z")) {
-                       line_terminator = 0;
-                       continue;
-               }
-               if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
-                       tag_cached = "H ";
-                       tag_unmerged = "M ";
-                       tag_removed = "R ";
-                       tag_modified = "C ";
-                       tag_other = "? ";
-                       tag_killed = "K ";
-                       if (arg[1] == 'v')
-                               show_valid_bit = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
-                       show_cached = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
-                       show_deleted = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
-                       show_modified = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
-                       show_others = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
-                       show_ignored = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
-                       show_stage = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
-                       show_killed = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--directory")) {
-                       show_other_directories = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--no-empty-directory")) {
-                       hide_empty_directories = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
-                       /* There's no point in showing unmerged unless
-                        * you also show the stage information.
-                        */
-                       show_stage = 1;
-                       show_unmerged = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-x") && i+1 < argc) {
-                       exc_given = 1;
-                       add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
-                       continue;
-               }
-               if (!strncmp(arg, "--exclude=", 10)) {
-                       exc_given = 1;
-                       add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
-                       continue;
-               }
-               if (!strcmp(arg, "-X") && i+1 < argc) {
-                       exc_given = 1;
-                       add_excludes_from_file(argv[++i]);
-                       continue;
-               }
-               if (!strncmp(arg, "--exclude-from=", 15)) {
-                       exc_given = 1;
-                       add_excludes_from_file(arg+15);
-                       continue;
-               }
-               if (!strncmp(arg, "--exclude-per-directory=", 24)) {
-                       exc_given = 1;
-                       exclude_per_dir = arg + 24;
-                       continue;
-               }
-               if (!strcmp(arg, "--full-name")) {
-                       prefix_offset = 0;
-                       continue;
-               }
-               if (!strcmp(arg, "--error-unmatch")) {
-                       error_unmatch = 1;
-                       continue;
-               }
-               if (!strncmp(arg, "--abbrev=", 9)) {
-                       abbrev = strtoul(arg+9, NULL, 10);
-                       if (abbrev && abbrev < MINIMUM_ABBREV)
-                               abbrev = MINIMUM_ABBREV;
-                       else if (abbrev > 40)
-                               abbrev = 40;
-                       continue;
-               }
-               if (!strcmp(arg, "--abbrev")) {
-                       abbrev = DEFAULT_ABBREV;
-                       continue;
-               }
-               if (*arg == '-')
-                       usage(ls_files_usage);
-               break;
-       }
-
-       pathspec = get_pathspec(prefix, argv + i);
-
-       /* Verify that the pathspec matches the prefix */
-       if (pathspec)
-               verify_pathspec();
-
-       /* Treat unmatching pathspec elements as errors */
-       if (pathspec && error_unmatch) {
-               int num;
-               for (num = 0; pathspec[num]; num++)
-                       ;
-               ps_matched = xcalloc(1, num);
-       }
-
-       if (show_ignored && !exc_given) {
-               fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
-                       argv[0]);
-               exit(1);
-       }
-
-       /* With no flags, we default to showing the cached files */
-       if (!(show_stage | show_deleted | show_others | show_unmerged |
-             show_killed | show_modified))
-               show_cached = 1;
-
-       read_cache();
-       if (prefix)
-               prune_cache();
-       show_files();
-
-       if (ps_matched) {
-               /* We need to make sure all pathspec matched otherwise
-                * it is an error.
-                */
-               int num, errors = 0;
-               for (num = 0; pathspec[num]; num++) {
-                       if (ps_matched[num])
-                               continue;
-                       error("pathspec '%s' did not match any.",
-                             pathspec[num] + prefix_offset);
-                       errors++;
-               }
-               return errors ? 1 : 0;
-       }
-
-       return 0;
-}
diff --git a/ls-tree.c b/ls-tree.c
deleted file mode 100644 (file)
index e4ef200..0000000
--- a/ls-tree.c
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * GIT - The information manager from hell
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#include "cache.h"
-#include "blob.h"
-#include "tree.h"
-#include "quote.h"
-
-static int line_termination = '\n';
-#define LS_RECURSIVE 1
-#define LS_TREE_ONLY 2
-#define LS_SHOW_TREES 4
-#define LS_NAME_ONLY 8
-static int abbrev = 0;
-static int ls_options = 0;
-const char **pathspec;
-static int chomp_prefix = 0;
-static const char *prefix;
-
-static const char ls_tree_usage[] =
-       "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
-
-static int show_recursive(const char *base, int baselen, const char *pathname)
-{
-       const char **s;
-
-       if (ls_options & LS_RECURSIVE)
-               return 1;
-
-       s = pathspec;
-       if (!s)
-               return 0;
-
-       for (;;) {
-               const char *spec = *s++;
-               int len, speclen;
-
-               if (!spec)
-                       return 0;
-               if (strncmp(base, spec, baselen))
-                       continue;
-               len = strlen(pathname);
-               spec += baselen;
-               speclen = strlen(spec);
-               if (speclen <= len)
-                       continue;
-               if (memcmp(pathname, spec, len))
-                       continue;
-               return 1;
-       }
-}
-
-static int show_tree(unsigned char *sha1, const char *base, int baselen,
-                    const char *pathname, unsigned mode, int stage)
-{
-       int retval = 0;
-       const char *type = blob_type;
-
-       if (S_ISDIR(mode)) {
-               if (show_recursive(base, baselen, pathname)) {
-                       retval = READ_TREE_RECURSIVE;
-                       if (!(ls_options & LS_SHOW_TREES))
-                               return retval;
-               }
-               type = tree_type;
-       }
-       else if (ls_options & LS_TREE_ONLY)
-               return 0;
-
-       if (chomp_prefix &&
-           (baselen < chomp_prefix || memcmp(prefix, base, chomp_prefix)))
-               return 0;
-
-       if (!(ls_options & LS_NAME_ONLY))
-               printf("%06o %s %s\t", mode, type,
-                               abbrev ? find_unique_abbrev(sha1,abbrev)
-                                       : sha1_to_hex(sha1));
-       write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
-                         pathname,
-                         line_termination, stdout);
-       putchar(line_termination);
-       return retval;
-}
-
-int main(int argc, const char **argv)
-{
-       unsigned char sha1[20];
-       struct tree *tree;
-
-       prefix = setup_git_directory();
-       git_config(git_default_config);
-       if (prefix && *prefix)
-               chomp_prefix = strlen(prefix);
-       while (1 < argc && argv[1][0] == '-') {
-               switch (argv[1][1]) {
-               case 'z':
-                       line_termination = 0;
-                       break;
-               case 'r':
-                       ls_options |= LS_RECURSIVE;
-                       break;
-               case 'd':
-                       ls_options |= LS_TREE_ONLY;
-                       break;
-               case 't':
-                       ls_options |= LS_SHOW_TREES;
-                       break;
-               case '-':
-                       if (!strcmp(argv[1]+2, "name-only") ||
-                           !strcmp(argv[1]+2, "name-status")) {
-                               ls_options |= LS_NAME_ONLY;
-                               break;
-                       }
-                       if (!strcmp(argv[1]+2, "full-name")) {
-                               chomp_prefix = 0;
-                               break;
-                       }
-                       if (!strncmp(argv[1]+2, "abbrev=",7)) {
-                               abbrev = strtoul(argv[1]+9, NULL, 10);
-                               if (abbrev && abbrev < MINIMUM_ABBREV)
-                                       abbrev = MINIMUM_ABBREV;
-                               else if (abbrev > 40)
-                                       abbrev = 40;
-                               break;
-                       }
-                       if (!strcmp(argv[1]+2, "abbrev")) {
-                               abbrev = DEFAULT_ABBREV;
-                               break;
-                       }
-                       /* otherwise fallthru */
-               default:
-                       usage(ls_tree_usage);
-               }
-               argc--; argv++;
-       }
-       /* -d -r should imply -t, but -d by itself should not have to. */
-       if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
-           ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
-               ls_options |= LS_SHOW_TREES;
-
-       if (argc < 2)
-               usage(ls_tree_usage);
-       if (get_sha1(argv[1], sha1) < 0)
-               usage(ls_tree_usage);
-
-       pathspec = get_pathspec(prefix, argv + 2);
-       tree = parse_tree_indirect(sha1);
-       if (!tree)
-               die("not a tree object");
-       read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
-
-       return 0;
-}
index b276519..d9b74f3 100644 (file)
@@ -72,11 +72,14 @@ static int bogus_from(char *line)
        return 1;
 }
 
-static int handle_from(char *line)
+static int handle_from(char *in_line)
 {
-       char *at = strchr(line, '@');
+       char line[1000];
+       char *at;
        char *dst;
 
+       strcpy(line, in_line);
+       at = strchr(line, '@');
        if (!at)
                return bogus_from(line);
 
@@ -237,38 +240,57 @@ static int eatspace(char *line)
 #define SEEN_FROM 01
 #define SEEN_DATE 02
 #define SEEN_SUBJECT 04
+#define SEEN_BOGUS_UNIX_FROM 010
+#define SEEN_PREFIX  020
 
-/* First lines of body can have From:, Date:, and Subject: */
-static int handle_inbody_header(int *seen, char *line)
+/* First lines of body can have From:, Date:, and Subject: or empty */
+static void handle_inbody_header(int *seen, char *line)
 {
+       if (*seen & SEEN_PREFIX)
+               return;
+       if (isspace(*line)) {
+               char *cp;
+               for (cp = line + 1; *cp; cp++) {
+                       if (!isspace(*cp))
+                               break;
+               }
+               if (!*cp)
+                       return;
+       }
+       if (!memcmp(">From", line, 5) && isspace(line[5])) {
+               if (!(*seen & SEEN_BOGUS_UNIX_FROM)) {
+                       *seen |= SEEN_BOGUS_UNIX_FROM;
+                       return;
+               }
+       }
        if (!memcmp("From:", line, 5) && isspace(line[5])) {
                if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
                        *seen |= SEEN_FROM;
-                       return 1;
+                       return;
                }
        }
        if (!memcmp("Date:", line, 5) && isspace(line[5])) {
                if (!(*seen & SEEN_DATE)) {
                        handle_date(line+6);
                        *seen |= SEEN_DATE;
-                       return 1;
+                       return;
                }
        }
        if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
                if (!(*seen & SEEN_SUBJECT)) {
                        handle_subject(line+9);
                        *seen |= SEEN_SUBJECT;
-                       return 1;
+                       return;
                }
        }
        if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
                if (!(*seen & SEEN_SUBJECT)) {
                        handle_subject(line);
                        *seen |= SEEN_SUBJECT;
-                       return 1;
+                       return;
                }
        }
-       return 0;
+       *seen |= SEEN_PREFIX;
 }
 
 static char *cleanup_subject(char *subject)
@@ -303,6 +325,7 @@ static char *cleanup_subject(char *subject)
                        }       
                        break;
                }
+               eatspace(subject);
                return subject;
        }
 }                      
@@ -324,6 +347,7 @@ static void cleanup_space(char *buf)
        }
 }
 
+static void decode_header_bq(char *it);
 typedef int (*header_fn_t)(char *);
 struct header_def {
        const char *name;
@@ -331,7 +355,7 @@ struct header_def {
        int namelen;
 };
 
-static void check_header(char *line, int len, struct header_def *header)
+static void check_header(char *line, struct header_def *header)
 {
        int i;
 
@@ -343,13 +367,17 @@ static void check_header(char *line, int len, struct header_def *header)
                int len = header[i].namelen;
                if (!strncasecmp(line, header[i].name, len) &&
                    line[len] == ':' && isspace(line[len + 1])) {
+                       /* Unwrap inline B and Q encoding, and optionally
+                        * normalize the meta information to utf8.
+                        */
+                       decode_header_bq(line + len + 2);
                        header[i].func(line + len + 2);
                        break;
                }
        }
 }
 
-static void check_subheader_line(char *line, int len)
+static void check_subheader_line(char *line)
 {
        static struct header_def header[] = {
                { "Content-Type", handle_subcontent_type },
@@ -357,9 +385,9 @@ static void check_subheader_line(char *line, int len)
                  handle_content_transfer_encoding },
                { NULL },
        };
-       check_header(line, len, header);
+       check_header(line, header);
 }
-static void check_header_line(char *line, int len)
+static void check_header_line(char *line)
 {
        static struct header_def header[] = {
                { "From", handle_from },
@@ -370,7 +398,30 @@ static void check_header_line(char *line, int len)
                  handle_content_transfer_encoding },
                { NULL },
        };
-       check_header(line, len, header);
+       check_header(line, header);
+}
+
+static int is_rfc2822_header(char *line)
+{
+       /*
+        * The section that defines the loosest possible
+        * field name is "3.6.8 Optional fields".
+        *
+        * optional-field = field-name ":" unstructured CRLF
+        * field-name = 1*ftext
+        * ftext = %d33-57 / %59-126
+        */
+       int ch;
+       char *cp = line;
+       while ((ch = *cp++)) {
+               if (ch == ':')
+                       return cp != line;
+               if ((33 <= ch && ch <= 57) ||
+                   (59 <= ch && ch <= 126))
+                       continue;
+               break;
+       }
+       return 0;
 }
 
 static int read_one_header_line(char *line, int sz, FILE *in)
@@ -379,18 +430,23 @@ static int read_one_header_line(char *line, int sz, FILE *in)
        while (ofs < sz) {
                int peek, len;
                if (fgets(line + ofs, sz - ofs, in) == NULL)
-                       return ofs;
+                       break;
                len = eatspace(line + ofs);
-               if (len == 0)
-                       return ofs;
-               peek = fgetc(in); ungetc(peek, in);
-               if (peek == ' ' || peek == '\t') {
-                       /* Yuck, 2822 header "folding" */
-                       ofs += len;
-                       continue;
+               if ((len == 0) || !is_rfc2822_header(line)) {
+                       /* Re-add the newline */
+                       line[ofs + len] = '\n';
+                       line[ofs + len + 1] = '\0';
+                       break;
                }
-               return ofs + len;
+               ofs += len;
+               /* Yuck, 2822 header "folding" */
+               peek = fgetc(in); ungetc(peek, in);
+               if (peek != ' ' && peek != '\t')
+                       break;
        }
+       /* Count mbox From headers as headers */
+       if (!ofs && !memcmp(line, "From ", 5))
+               ofs = 1;
        return ofs;
 }
 
@@ -585,25 +641,13 @@ static void decode_transfer_encoding(char *line)
 static void handle_info(void)
 {
        char *sub;
-       static int done_info = 0;
-
-       if (done_info)
-               return;
 
-       done_info = 1;
        sub = cleanup_subject(subject);
        cleanup_space(name);
        cleanup_space(date);
        cleanup_space(email);
        cleanup_space(sub);
 
-       /* Unwrap inline B and Q encoding, and optionally
-        * normalize the meta information to utf8.
-        */
-       decode_header_bq(name);
-       decode_header_bq(date);
-       decode_header_bq(email);
-       decode_header_bq(sub);
        printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
               name, email, sub, date);
 }
@@ -611,7 +655,7 @@ static void handle_info(void)
 /* We are inside message body and have read line[] already.
  * Spit out the commit log.
  */
-static int handle_commit_msg(void)
+static int handle_commit_msg(int *seen)
 {
        if (!cmitmsg)
                return 0;
@@ -635,6 +679,11 @@ static int handle_commit_msg(void)
                decode_transfer_encoding(line);
                if (metainfo_charset)
                        convert_to_utf8(line, charset);
+
+               handle_inbody_header(seen, line);
+               if (!(*seen & SEEN_PREFIX))
+                       continue;
+
                fputs(line, cmitmsg);
        } while (fgets(line, sizeof(line), stdin) != NULL);
        fclose(cmitmsg);
@@ -666,26 +715,16 @@ static void handle_patch(void)
  * that the first part to contain commit message and a patch, and
  * handle other parts as pure patches.
  */
-static int handle_multipart_one_part(void)
+static int handle_multipart_one_part(int *seen)
 {
-       int seen = 0;
        int n = 0;
-       int len;
 
        while (fgets(line, sizeof(line), stdin) != NULL) {
        again:
-               len = eatspace(line);
                n++;
-               if (!len)
-                       continue;
                if (is_multipart_boundary(line))
                        break;
-               if (0 <= seen && handle_inbody_header(&seen, line))
-                       continue;
-               seen = -1; /* no more inbody headers */
-               line[len] = '\n';
-               handle_info();
-               if (handle_commit_msg())
+               if (handle_commit_msg(seen))
                        goto again;
                handle_patch();
                break;
@@ -697,6 +736,7 @@ static int handle_multipart_one_part(void)
 
 static void handle_multipart_body(void)
 {
+       int seen = 0;
        int part_num = 0;
 
        /* Skip up to the first boundary */
@@ -709,16 +749,16 @@ static void handle_multipart_body(void)
                return;
        /* We are on boundary line.  Start slurping the subhead. */
        while (1) {
-               int len = read_one_header_line(line, sizeof(line), stdin);
-               if (!len) {
-                       if (handle_multipart_one_part() < 0)
+               int hdr = read_one_header_line(line, sizeof(line), stdin);
+               if (!hdr) {
+                       if (handle_multipart_one_part(&seen) < 0)
                                return;
                        /* Reset per part headers */
                        transfer_encoding = TE_DONTCARE;
                        charset[0] = 0;
                }
                else
-                       check_subheader_line(line, len);
+                       check_subheader_line(line);
        }
        fclose(patchfile);
        if (!patch_lines) {
@@ -732,19 +772,8 @@ static void handle_body(void)
 {
        int seen = 0;
 
-       while (fgets(line, sizeof(line), stdin) != NULL) {
-               int len = eatspace(line);
-               if (!len)
-                       continue;
-               if (0 <= seen && handle_inbody_header(&seen, line))
-                       continue;
-               seen = -1; /* no more inbody headers */
-               line[len] = '\n';
-               handle_info();
-               handle_commit_msg();
-               handle_patch();
-               break;
-       }
+       handle_commit_msg(&seen);
+       handle_patch();
        fclose(patchfile);
        if (!patch_lines) {
                fprintf(stderr, "No patch found\n");
@@ -787,15 +816,16 @@ int main(int argc, char **argv)
                exit(1);
        }
        while (1) {
-               int len = read_one_header_line(line, sizeof(line), stdin);
-               if (!len) {
+               int hdr = read_one_header_line(line, sizeof(line), stdin);
+               if (!hdr) {
                        if (multipart_boundary[0])
                                handle_multipart_body();
                        else
                                handle_body();
+                       handle_info();
                        break;
                }
-               check_header_line(line, len);
+               check_header_line(line);
        }
        return 0;
 }
index c529e2d..70a569c 100644 (file)
@@ -162,7 +162,7 @@ int main(int argc, const char **argv)
 
        while (*argp) {
                const char *file = *argp++;
-               FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "rt");
+               FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
                int file_done = 0;
 
                if ( !f )
index 07f5ab4..4856ca0 100644 (file)
@@ -82,8 +82,9 @@ static struct commit *interesting(struct commit_list *list)
  * commit B.
  *
  *
- * Another pathological example how this thing can fail to mark an ancestor
- * of a merge base as UNINTERESTING without the postprocessing phase.
+ * Another pathological example how this thing used to fail to mark an
+ * ancestor of a merge base as UNINTERESTING before we introduced the
+ * postprocessing phase (mark_reachable_commits).
  *
  *               2
  *               H
@@ -118,7 +119,9 @@ static struct commit *interesting(struct commit_list *list)
  *      D7                     2 3 7 7 3 2 1 2
  *      E7                     2 3 7 7 7 2 1 2
  *
- * and we end up showing E as an interesting merge base.
+ * and we ended up showing E as an interesting merge base.
+ * The postprocessing phase re-injects C and continues traversal
+ * to contaminate D and E.
  */
 
 static int show_all = 0;
@@ -247,10 +250,12 @@ int main(int argc, char **argv)
                        usage(merge_base_usage);
                argc--; argv++;
        }
-       if (argc != 3 ||
-           get_sha1(argv[1], rev1key) ||
-           get_sha1(argv[2], rev2key))
+       if (argc != 3)
                usage(merge_base_usage);
+       if (get_sha1(argv[1], rev1key))
+               die("Not a valid object name %s", argv[1]);
+       if (get_sha1(argv[2], rev2key))
+               die("Not a valid object name %s", argv[2]);
        rev1 = lookup_commit_reference(rev1key);
        rev2 = lookup_commit_reference(rev2key);
        if (!rev1 || !rev2)
index 50528d5..9dcaab7 100644 (file)
@@ -24,16 +24,14 @@ static const char *sha1_to_hex_zero(const unsigned char *sha1)
 
 static void resolve(const char *base, struct name_entry *branch1, struct name_entry *result)
 {
-       char branch1_sha1[50];
-
        /* If it's already branch1, don't bother showing it */
        if (!branch1)
                return;
-       memcpy(branch1_sha1, sha1_to_hex_zero(branch1->sha1), 41);
 
        printf("0 %06o->%06o %s->%s %s%s\n",
                branch1->mode, result->mode,
-               branch1_sha1, sha1_to_hex_zero(result->sha1),
+               sha1_to_hex_zero(branch1->sha1),
+               sha1_to_hex_zero(result->sha1),
                base, result->path);
 }
 
@@ -151,7 +149,7 @@ static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
        unsigned char sha1[20];
        void *buf;
 
-       if (get_sha1(rev, sha1) < 0)
+       if (get_sha1(rev, sha1))
                die("unknown rev %s", rev);
        buf = fill_tree_descriptor(desc, sha1);
        if (!buf)
diff --git a/mktag.c b/mktag.c
index 2328878..f0fe528 100644 (file)
--- a/mktag.c
+++ b/mktag.c
@@ -45,42 +45,46 @@ static int verify_tag(char *buffer, unsigned long size)
        unsigned char sha1[20];
        const char *object, *type_line, *tag_line, *tagger_line;
 
-       if (size < 64 || size > MAXSIZE-1)
-               return -1;
+       if (size < 64)
+               return error("wanna fool me ? you obviously got the size wrong !\n");
+
        buffer[size] = 0;
 
        /* Verify object line */
        object = buffer;
        if (memcmp(object, "object ", 7))
-               return -1;
+               return error("char%d: does not start with \"object \"\n", 0);
+
        if (get_sha1_hex(object + 7, sha1))
-               return -1;
+               return error("char%d: could not get SHA1 hash\n", 7);
 
        /* Verify type line */
        type_line = object + 48;
        if (memcmp(type_line - 1, "\ntype ", 6))
-               return -1;
+               return error("char%d: could not find \"\\ntype \"\n", 47);
 
        /* Verify tag-line */
        tag_line = strchr(type_line, '\n');
        if (!tag_line)
-               return -1;
+               return error("char%td: could not find next \"\\n\"\n", type_line - buffer);
        tag_line++;
        if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
-               return -1;
+               return error("char%td: no \"tag \" found\n", tag_line - buffer);
 
        /* Get the actual type */
        typelen = tag_line - type_line - strlen("type \n");
        if (typelen >= sizeof(type))
-               return -1;
+               return error("char%td: type too long\n", type_line+5 - buffer);
+
        memcpy(type, type_line+5, typelen);
        type[typelen] = 0;
 
        /* Verify that the object matches */
        if (get_sha1_hex(object + 7, sha1))
-               return -1;
+               return error("char%d: could not get SHA1 hash but this is really odd since i got it before !\n", 7);
+
        if (verify_object(sha1, type))
-               return -1;
+               return error("char%d: could not verify object %s\n", 7, sha1);
 
        /* Verify the tag-name: we don't allow control characters or spaces in it */
        tag_line += 4;
@@ -90,14 +94,14 @@ static int verify_tag(char *buffer, unsigned long size)
                        break;
                if (c > ' ')
                        continue;
-               return -1;
+               return error("char%td: could not verify tag name\n", tag_line - buffer);
        }
 
        /* Verify the tagger line */
        tagger_line = tag_line;
 
        if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n'))
-               return -1;
+               return error("char%td: could not find \"tagger\"\n", tagger_line - buffer);
 
        /* The actual stuff afterwards we don't care about.. */
        return 0;
@@ -105,8 +109,8 @@ static int verify_tag(char *buffer, unsigned long size)
 
 int main(int argc, char **argv)
 {
-       unsigned long size;
-       char buffer[MAXSIZE];
+       unsigned long size = 4096;
+       char *buffer = malloc(size);
        unsigned char result_sha1[20];
 
        if (argc != 1)
@@ -114,13 +118,9 @@ int main(int argc, char **argv)
 
        setup_git_directory();
 
-       // Read the signature
-       size = 0;
-       for (;;) {
-               int ret = xread(0, buffer + size, MAXSIZE - size);
-               if (ret <= 0)
-                       break;
-               size += ret;
+       if (read_pipe(0, &buffer, &size)) {
+               free(buffer);
+               die("could not read from stdin");
        }
 
        // Verify it for some basic sanity: it needs to start with "object <sha1>\ntype\ntagger "
@@ -129,6 +129,9 @@ int main(int argc, char **argv)
 
        if (write_sha1_file(buffer, size, tag_type, result_sha1) < 0)
                die("unable to write tag file");
+
+       free(buffer);
+
        printf("%s\n", sha1_to_hex(result_sha1));
        return 0;
 }
index 4d46e0d..9adc874 100644 (file)
--- a/object.c
+++ b/object.c
@@ -9,7 +9,7 @@ struct object **objs;
 static int nr_objs;
 int obj_allocs;
 
-int track_object_refs = 1;
+int track_object_refs = 0;
 
 static int hashtable_index(const unsigned char *sha1)
 {
@@ -200,8 +200,11 @@ struct object *parse_object(const unsigned char *sha1)
                        obj = &blob->object;
                } else if (!strcmp(type, tree_type)) {
                        struct tree *tree = lookup_tree(sha1);
-                       parse_tree_buffer(tree, buffer, size);
                        obj = &tree->object;
+                       if (!tree->object.parsed) {
+                               parse_tree_buffer(tree, buffer, size);
+                               buffer = NULL;
+                       }
                } else if (!strcmp(type, commit_type)) {
                        struct commit *commit = lookup_commit(sha1);
                        parse_commit_buffer(commit, buffer, size);
index 84ed90d..e575879 100644 (file)
@@ -29,12 +29,12 @@ static int verify_packfile(struct packed_git *p)
        pack_base = p->pack_base;
        SHA1_Update(&ctx, pack_base, pack_size - 20);
        SHA1_Final(sha1, &ctx);
-       if (memcmp(sha1, index_base + index_size - 40, 20))
-               return error("Packfile %s SHA1 mismatch with idx",
-                            p->pack_name);
        if (memcmp(sha1, pack_base + pack_size - 20, 20))
                return error("Packfile %s SHA1 mismatch with itself",
                             p->pack_name);
+       if (memcmp(sha1, index_base + index_size - 40, 20))
+               return error("Packfile %s SHA1 mismatch with idx",
+                            p->pack_name);
 
        /* Make sure everything reachable from idx is valid.  Since we
         * have verified that nr_objects matches between idx and pack,
index c0acc46..179560f 100644 (file)
@@ -156,7 +156,7 @@ static void prepare_pack_revindex(struct pack_revindex *rix)
 
        rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
        for (i = 0; i < num_ent; i++) {
-               long hl = *((long *)(index + 24 * i));
+               unsigned int hl = *((unsigned int *)(index + 24 * i));
                rix->revindex[i] = ntohl(hl);
        }
        /* This knows the pack format -- the 20-byte trailer
@@ -463,48 +463,21 @@ static void rehash_objects(void)
        }
 }
 
-struct name_path {
-       struct name_path *up;
-       const char *elem;
-       int len;
-};
-
-#define DIRBITS 12
-
-static unsigned name_hash(struct name_path *path, const char *name)
+static unsigned name_hash(const char *name)
 {
-       struct name_path *p = path;
-       const char *n = name + strlen(name);
-       unsigned hash = 0, name_hash = 0, name_done = 0;
-
-       if (n != name && n[-1] == '\n')
-               n--;
-       while (name <= --n) {
-               unsigned char c = *n;
-               if (c == '/' && !name_done) {
-                       name_hash = hash;
-                       name_done = 1;
-                       hash = 0;
-               }
-               hash = hash * 11 + c;
-       }
-       if (!name_done) {
-               name_hash = hash;
-               hash = 0;
-       }
-       for (p = path; p; p = p->up) {
-               hash = hash * 11 + '/';
-               n = p->elem + p->len;
-               while (p->elem <= --n) {
-                       unsigned char c = *n;
-                       hash = hash * 11 + c;
-               }
-       }
+       unsigned char c;
+       unsigned hash = 0;
+
        /*
-        * Make sure "Makefile" and "t/Makefile" are hashed separately
-        * but close enough.
+        * This effectively just creates a sortable number from the
+        * last sixteen non-whitespace characters. Last characters
+        * count "most", so things that end in ".c" sort together.
         */
-       hash = (name_hash<<DIRBITS) | (hash & ((1U<<DIRBITS )-1));
+       while ((c = *name++) != 0) {
+               if (isspace(c))
+                       continue;
+               hash = (hash >> 2) + (c << 24);
+       }
        return hash;
 }
 
@@ -686,48 +659,39 @@ static int name_cmp_len(const char *name)
 }
 
 static void add_pbase_object(struct tree_desc *tree,
-                            struct name_path *up,
                             const char *name,
-                            int cmplen)
+                            int cmplen,
+                            const char *fullname)
 {
-       while (tree->size) {
-               const unsigned char *sha1;
-               const char *entry_name;
-               int entry_len;
-               unsigned mode;
+       struct name_entry entry;
+
+       while (tree_entry(tree,&entry)) {
                unsigned long size;
                char type[20];
 
-               sha1 = tree_entry_extract(tree, &entry_name, &mode);
-               update_tree_entry(tree);
-               entry_len = strlen(entry_name);
-               if (entry_len != cmplen ||
-                   memcmp(entry_name, name, cmplen) ||
-                   !has_sha1_file(sha1) ||
-                   sha1_object_info(sha1, type, &size))
+               if (entry.pathlen != cmplen ||
+                   memcmp(entry.path, name, cmplen) ||
+                   !has_sha1_file(entry.sha1) ||
+                   sha1_object_info(entry.sha1, type, &size))
                        continue;
                if (name[cmplen] != '/') {
-                       unsigned hash = name_hash(up, name);
-                       add_object_entry(sha1, hash, 1);
+                       unsigned hash = name_hash(fullname);
+                       add_object_entry(entry.sha1, hash, 1);
                        return;
                }
                if (!strcmp(type, tree_type)) {
                        struct tree_desc sub;
-                       struct name_path me;
                        struct pbase_tree_cache *tree;
                        const char *down = name+cmplen+1;
                        int downlen = name_cmp_len(down);
 
-                       tree = pbase_tree_get(sha1);
+                       tree = pbase_tree_get(entry.sha1);
                        if (!tree)
                                return;
                        sub.buf = tree->tree_data;
                        sub.size = tree->tree_size;
 
-                       me.up = up;
-                       me.elem = entry_name;
-                       me.len = entry_len;
-                       add_pbase_object(&sub, &me, down, downlen);
+                       add_pbase_object(&sub, down, downlen, fullname);
                        pbase_tree_put(tree);
                }
        }
@@ -783,14 +747,14 @@ static void add_preferred_base_object(char *name, unsigned hash)
 
        for (it = pbase_tree; it; it = it->next) {
                if (cmplen == 0) {
-                       hash = name_hash(NULL, "");
+                       hash = name_hash("");
                        add_object_entry(it->pcache.sha1, hash, 1);
                }
                else {
                        struct tree_desc tree;
                        tree.buf = it->pcache.tree_data;
                        tree.size = it->pcache.tree_size;
-                       add_pbase_object(&tree, NULL, name, cmplen);
+                       add_pbase_object(&tree, name, cmplen, name);
                }
        }
 }
@@ -994,6 +958,7 @@ static int type_size_sort(const struct object_entry *a, const struct object_entr
 struct unpacked {
        struct object_entry *entry;
        void *data;
+       struct delta_index *index;
 };
 
 /*
@@ -1004,64 +969,59 @@ struct unpacked {
  * more importantly, the bigger file is likely the more recent
  * one.
  */
-static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
+static int try_delta(struct unpacked *trg, struct unpacked *src,
+                    struct delta_index *src_index, unsigned max_depth)
 {
-       struct object_entry *cur_entry = cur->entry;
-       struct object_entry *old_entry = old->entry;
-       unsigned long size, oldsize, delta_size, sizediff;
-       long max_size;
+       struct object_entry *trg_entry = trg->entry;
+       struct object_entry *src_entry = src->entry;
+       unsigned long size, src_size, delta_size, sizediff, max_size;
        void *delta_buf;
 
        /* Don't bother doing diffs between different types */
-       if (cur_entry->type != old_entry->type)
+       if (trg_entry->type != src_entry->type)
                return -1;
 
        /* We do not compute delta to *create* objects we are not
         * going to pack.
         */
-       if (cur_entry->preferred_base)
+       if (trg_entry->preferred_base)
                return -1;
 
-       /* If the current object is at pack edge, take the depth the
+       /*
+        * If the current object is at pack edge, take the depth the
         * objects that depend on the current object into account --
         * otherwise they would become too deep.
         */
-       if (cur_entry->delta_child) {
-               if (max_depth <= cur_entry->delta_limit)
+       if (trg_entry->delta_child) {
+               if (max_depth <= trg_entry->delta_limit)
                        return 0;
-               max_depth -= cur_entry->delta_limit;
+               max_depth -= trg_entry->delta_limit;
        }
-
-       size = cur_entry->size;
-       oldsize = old_entry->size;
-       sizediff = oldsize > size ? oldsize - size : size - oldsize;
-
-       if (size < 50)
-               return -1;
-       if (old_entry->depth >= max_depth)
+       if (src_entry->depth >= max_depth)
                return 0;
 
-       /*
-        * NOTE!
-        *
-        * We always delta from the bigger to the smaller, since that's
-        * more space-efficient (deletes don't have to say _what_ they
-        * delete).
-        */
-       max_size = size / 2 - 20;
-       if (cur_entry->delta)
-               max_size = cur_entry->delta_size-1;
+       /* Now some size filtering heuristics. */
+       size = trg_entry->size;
+       max_size = size/2 - 20;
+       max_size = max_size * (max_depth - src_entry->depth) / max_depth;
+       if (max_size == 0)
+               return 0;
+       if (trg_entry->delta && trg_entry->delta_size <= max_size)
+               max_size = trg_entry->delta_size-1;
+       src_size = src_entry->size;
+       sizediff = src_size < size ? size - src_size : 0;
        if (sizediff >= max_size)
                return 0;
-       delta_buf = diff_delta(old->data, oldsize,
-                              cur->data, size, &delta_size, max_size);
+
+       delta_buf = create_delta(src_index, trg->data, size, &delta_size, max_size);
        if (!delta_buf)
                return 0;
-       cur_entry->delta = old_entry;
-       cur_entry->delta_size = delta_size;
-       cur_entry->depth = old_entry->depth + 1;
+
+       trg_entry->delta = src_entry;
+       trg_entry->delta_size = delta_size;
+       trg_entry->depth = src_entry->depth + 1;
        free(delta_buf);
-       return 0;
+       return 1;
 }
 
 static void progress_interval(int signum)
@@ -1109,11 +1069,16 @@ static void find_deltas(struct object_entry **list, int window, int depth)
                         */
                        continue;
 
+               if (entry->size < 50)
+                       continue;
+               free_delta_index(n->index);
+               n->index = NULL;
                free(n->data);
                n->entry = entry;
                n->data = read_sha1_file(entry->sha1, type, &size);
                if (size != entry->size)
-                       die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
+                       die("object %s inconsistent object length (%lu vs %lu)",
+                           sha1_to_hex(entry->sha1), size, entry->size);
 
                j = window;
                while (--j > 0) {
@@ -1124,18 +1089,20 @@ static void find_deltas(struct object_entry **list, int window, int depth)
                        m = array + other_idx;
                        if (!m->entry)
                                break;
-                       if (try_delta(n, m, depth) < 0)
+                       if (try_delta(n, m, m->index, depth) < 0)
                                break;
                }
-#if 0
                /* if we made n a delta, and if n is already at max
                 * depth, leaving it in the window is pointless.  we
                 * should evict it first.
-                * ... in theory only; somehow this makes things worse.
                 */
                if (entry->delta && depth <= entry->depth)
                        continue;
-#endif
+
+               n->index = create_delta_index(n->data, size);
+               if (!n->index)
+                       die("out of memory");
+
                idx++;
                if (idx >= window)
                        idx = 0;
@@ -1144,8 +1111,10 @@ static void find_deltas(struct object_entry **list, int window, int depth)
        if (progress)
                fputc('\n', stderr);
 
-       for (i = 0; i < window; ++i)
+       for (i = 0; i < window; ++i) {
+               free_delta_index(array[i].index);
                free(array[i].data);
+       }
        free(array);
 }
 
@@ -1239,6 +1208,7 @@ int main(int argc, char **argv)
 
        setup_git_directory();
 
+       progress = isatty(2);
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
 
@@ -1269,6 +1239,10 @@ int main(int argc, char **argv)
                                        usage(pack_usage);
                                continue;
                        }
+                       if (!strcmp("--progress", arg)) {
+                               progress = 1;
+                               continue;
+                       }
                        if (!strcmp("-q", arg)) {
                                progress = 0;
                                continue;
@@ -1323,7 +1297,7 @@ int main(int argc, char **argv)
                }
                if (get_sha1_hex(line, sha1))
                        die("expected sha1, got garbage:\n %s", line);
-               hash = name_hash(NULL, line+41);
+               hash = name_hash(line+41);
                add_preferred_base_object(line+41, hash);
                add_object_entry(sha1, hash, 0);
        }
index d95f0d9..8f318ed 100644 (file)
@@ -13,8 +13,8 @@
 #include <string.h>
 #include "delta.h"
 
-void *patch_delta(void *src_buf, unsigned long src_size,
-                 void *delta_buf, unsigned long delta_size,
+void *patch_delta(const void *src_buf, unsigned long src_size,
+                 const void *delta_buf, unsigned long delta_size,
                  unsigned long *dst_size)
 {
        const unsigned char *data, *top;
diff --git a/path.c b/path.c
index 334b2bd..194e0b5 100644 (file)
--- a/path.c
+++ b/path.c
@@ -83,14 +83,19 @@ int git_mkstemp(char *path, size_t len, const char *template)
 }
 
 
-char *safe_strncpy(char *dest, const char *src, size_t n)
+size_t safe_strncpy(char *dest, const char *src, size_t size)
 {
-       strncpy(dest, src, n);
-       dest[n - 1] = '\0';
+       size_t ret = strlen(src);
 
-       return dest;
+       if (size) {
+               size_t len = (ret >= size) ? size - 1 : ret;
+               memcpy(dest, src, len);
+               dest[len] = '\0';
+       }
+       return ret;
 }
 
+
 int validate_symref(const char *path)
 {
        struct stat st;
@@ -250,3 +255,26 @@ char *enter_repo(char *path, int strict)
 
        return NULL;
 }
+
+int adjust_shared_perm(const char *path)
+{
+       struct stat st;
+       int mode;
+
+       if (!shared_repository)
+               return 0;
+       if (lstat(path, &st) < 0)
+               return -1;
+       mode = st.st_mode;
+       if (mode & S_IRUSR)
+               mode |= S_IRGRP;
+       if (mode & S_IWUSR)
+               mode |= S_IWGRP;
+       if (mode & S_IXUSR)
+               mode |= S_IXGRP;
+       if (S_ISDIR(mode))
+               mode |= S_ISGID;
+       if (chmod(path, mode) < 0)
+               return -2;
+       return 0;
+}
index 1f71d12..c499c51 100644 (file)
@@ -347,6 +347,70 @@ int ce_path_match(const struct cache_entry *ce, const char **pathspec)
 }
 
 /*
+ * We fundamentally don't like some paths: we don't want
+ * dot or dot-dot anywhere, and for obvious reasons don't
+ * want to recurse into ".git" either.
+ *
+ * Also, we don't want double slashes or slashes at the
+ * end that can make pathnames ambiguous.
+ */
+static int verify_dotfile(const char *rest)
+{
+       /*
+        * The first character was '.', but that
+        * has already been discarded, we now test
+        * the rest.
+        */
+       switch (*rest) {
+       /* "." is not allowed */
+       case '\0': case '/':
+               return 0;
+
+       /*
+        * ".git" followed by  NUL or slash is bad. This
+        * shares the path end test with the ".." case.
+        */
+       case 'g':
+               if (rest[1] != 'i')
+                       break;
+               if (rest[2] != 't')
+                       break;
+               rest += 2;
+       /* fallthrough */
+       case '.':
+               if (rest[1] == '\0' || rest[1] == '/')
+                       return 0;
+       }
+       return 1;
+}
+
+int verify_path(const char *path)
+{
+       char c;
+
+       goto inside;
+       for (;;) {
+               if (!c)
+                       return 1;
+               if (c == '/') {
+inside:
+                       c = *path++;
+                       switch (c) {
+                       default:
+                               continue;
+                       case '/': case '\0':
+                               break;
+                       case '.':
+                               if (verify_dotfile(path))
+                                       continue;
+                       }
+                       return 0;
+               }
+               c = *path++;
+       }
+}
+
+/*
  * Do we have another file that has the beginning components being a
  * proper superset of the name we're trying to add?
  */
@@ -487,6 +551,8 @@ int add_cache_entry(struct cache_entry *ce, int option)
 
        if (!ok_to_add)
                return -1;
+       if (!verify_path(ce->name))
+               return -1;
 
        if (!skip_df_check &&
            check_file_directory_conflict(ce, pos, ok_to_replace)) {
@@ -511,6 +577,123 @@ int add_cache_entry(struct cache_entry *ce, int option)
        return 0;
 }
 
+/* Three functions to allow overloaded pointer return; see linux/err.h */
+static inline void *ERR_PTR(long error)
+{
+       return (void *) error;
+}
+
+static inline long PTR_ERR(const void *ptr)
+{
+       return (long) ptr;
+}
+
+static inline long IS_ERR(const void *ptr)
+{
+       return (unsigned long)ptr > (unsigned long)-1000L;
+}
+
+/*
+ * "refresh" does not calculate a new sha1 file or bring the
+ * cache up-to-date for mode/content changes. But what it
+ * _does_ do is to "re-match" the stat information of a file
+ * with the cache, so that you can refresh the cache for a
+ * file that hasn't been changed but where the stat entry is
+ * out of date.
+ *
+ * For example, you'd want to do this after doing a "git-read-tree",
+ * to link up the stat cache details with the proper files.
+ */
+static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
+{
+       struct stat st;
+       struct cache_entry *updated;
+       int changed, size;
+
+       if (lstat(ce->name, &st) < 0)
+               return ERR_PTR(-errno);
+
+       changed = ce_match_stat(ce, &st, really);
+       if (!changed) {
+               if (really && assume_unchanged &&
+                   !(ce->ce_flags & htons(CE_VALID)))
+                       ; /* mark this one VALID again */
+               else
+                       return NULL;
+       }
+
+       if (ce_modified(ce, &st, really))
+               return ERR_PTR(-EINVAL);
+
+       size = ce_size(ce);
+       updated = xmalloc(size);
+       memcpy(updated, ce, size);
+       fill_stat_cache_info(updated, &st);
+
+       /* In this case, if really is not set, we should leave
+        * CE_VALID bit alone.  Otherwise, paths marked with
+        * --no-assume-unchanged (i.e. things to be edited) will
+        * reacquire CE_VALID bit automatically, which is not
+        * really what we want.
+        */
+       if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
+               updated->ce_flags &= ~htons(CE_VALID);
+
+       return updated;
+}
+
+int refresh_cache(unsigned int flags)
+{
+       int i;
+       int has_errors = 0;
+       int really = (flags & REFRESH_REALLY) != 0;
+       int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
+       int quiet = (flags & REFRESH_QUIET) != 0;
+       int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
+
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce, *new;
+               ce = active_cache[i];
+               if (ce_stage(ce)) {
+                       while ((i < active_nr) &&
+                              ! strcmp(active_cache[i]->name, ce->name))
+                               i++;
+                       i--;
+                       if (allow_unmerged)
+                               continue;
+                       printf("%s: needs merge\n", ce->name);
+                       has_errors = 1;
+                       continue;
+               }
+
+               new = refresh_entry(ce, really);
+               if (!new)
+                       continue;
+               if (IS_ERR(new)) {
+                       if (not_new && PTR_ERR(new) == -ENOENT)
+                               continue;
+                       if (really && PTR_ERR(new) == -EINVAL) {
+                               /* If we are doing --really-refresh that
+                                * means the index is not valid anymore.
+                                */
+                               ce->ce_flags &= ~htons(CE_VALID);
+                               active_cache_changed = 1;
+                       }
+                       if (quiet)
+                               continue;
+                       printf("%s: needs update\n", ce->name);
+                       has_errors = 1;
+                       continue;
+               }
+               active_cache_changed = 1;
+               /* You can NOT just free active_cache[i] here, since it
+                * might not be necessarily malloc()ed but can also come
+                * from mmap(). */
+               active_cache[i] = new;
+       }
+       return has_errors;
+}
+
 static int verify_hdr(struct cache_header *hdr, unsigned long size)
 {
        SHA_CTX c;
@@ -583,7 +766,7 @@ int read_cache(void)
 
        active_nr = ntohl(hdr->hdr_entries);
        active_alloc = alloc_nr(active_nr);
-       active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
+       active_cache = xcalloc(active_alloc, sizeof(struct cache_entry *));
 
        offset = sizeof(*hdr);
        for (i = 0; i < active_nr; i++) {
@@ -643,7 +826,7 @@ static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
 }
 
 static int write_index_ext_header(SHA_CTX *context, int fd,
-                                 unsigned long ext, unsigned long sz)
+                                 unsigned int ext, unsigned int sz)
 {
        ext = htonl(ext);
        sz = htonl(sz);
diff --git a/read-tree.c b/read-tree.c
deleted file mode 100644 (file)
index fb1d682..0000000
+++ /dev/null
@@ -1,949 +0,0 @@
-/*
- * GIT - The information manager from hell
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#define DBRT_DEBUG 1
-
-#include "cache.h"
-
-#include "object.h"
-#include "tree.h"
-#include "cache-tree.h"
-#include <sys/time.h>
-#include <signal.h>
-
-static int merge = 0;
-static int update = 0;
-static int index_only = 0;
-static int nontrivial_merge = 0;
-static int trivial_merges_only = 0;
-static int aggressive = 0;
-static int verbose_update = 0;
-static volatile int progress_update = 0;
-static const char *prefix = NULL;
-
-static int head_idx = -1;
-static int merge_size = 0;
-
-static struct object_list *trees = NULL;
-
-static struct cache_entry df_conflict_entry = { 
-};
-
-static struct tree_entry_list df_conflict_list = {
-       .name = NULL,
-       .next = &df_conflict_list
-};
-
-typedef int (*merge_fn_t)(struct cache_entry **src);
-
-static int entcmp(char *name1, int dir1, char *name2, int dir2)
-{
-       int len1 = strlen(name1);
-       int len2 = strlen(name2);
-       int len = len1 < len2 ? len1 : len2;
-       int ret = memcmp(name1, name2, len);
-       unsigned char c1, c2;
-       if (ret)
-               return ret;
-       c1 = name1[len];
-       c2 = name2[len];
-       if (!c1 && dir1)
-               c1 = '/';
-       if (!c2 && dir2)
-               c2 = '/';
-       ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
-       if (c1 && c2 && !ret)
-               ret = len1 - len2;
-       return ret;
-}
-
-static int unpack_trees_rec(struct tree_entry_list **posns, int len,
-                           const char *base, merge_fn_t fn, int *indpos)
-{
-       int baselen = strlen(base);
-       int src_size = len + 1;
-       do {
-               int i;
-               char *first;
-               int firstdir = 0;
-               int pathlen;
-               unsigned ce_size;
-               struct tree_entry_list **subposns;
-               struct cache_entry **src;
-               int any_files = 0;
-               int any_dirs = 0;
-               char *cache_name;
-               int ce_stage;
-
-               /* Find the first name in the input. */
-
-               first = NULL;
-               cache_name = NULL;
-
-               /* Check the cache */
-               if (merge && *indpos < active_nr) {
-                       /* This is a bit tricky: */
-                       /* If the index has a subdirectory (with
-                        * contents) as the first name, it'll get a
-                        * filename like "foo/bar". But that's after
-                        * "foo", so the entry in trees will get
-                        * handled first, at which point we'll go into
-                        * "foo", and deal with "bar" from the index,
-                        * because the base will be "foo/". The only
-                        * way we can actually have "foo/bar" first of
-                        * all the things is if the trees don't
-                        * contain "foo" at all, in which case we'll
-                        * handle "foo/bar" without going into the
-                        * directory, but that's fine (and will return
-                        * an error anyway, with the added unknown
-                        * file case.
-                        */
-
-                       cache_name = active_cache[*indpos]->name;
-                       if (strlen(cache_name) > baselen &&
-                           !memcmp(cache_name, base, baselen)) {
-                               cache_name += baselen;
-                               first = cache_name;
-                       } else {
-                               cache_name = NULL;
-                       }
-               }
-
-#if DBRT_DEBUG > 1
-               if (first)
-                       printf("index %s\n", first);
-#endif
-               for (i = 0; i < len; i++) {
-                       if (!posns[i] || posns[i] == &df_conflict_list)
-                               continue;
-#if DBRT_DEBUG > 1
-                       printf("%d %s\n", i + 1, posns[i]->name);
-#endif
-                       if (!first || entcmp(first, firstdir,
-                                            posns[i]->name, 
-                                            posns[i]->directory) > 0) {
-                               first = posns[i]->name;
-                               firstdir = posns[i]->directory;
-                       }
-               }
-               /* No name means we're done */
-               if (!first)
-                       return 0;
-
-               pathlen = strlen(first);
-               ce_size = cache_entry_size(baselen + pathlen);
-
-               src = xcalloc(src_size, sizeof(struct cache_entry *));
-
-               subposns = xcalloc(len, sizeof(struct tree_list_entry *));
-
-               if (cache_name && !strcmp(cache_name, first)) {
-                       any_files = 1;
-                       src[0] = active_cache[*indpos];
-                       remove_cache_entry_at(*indpos);
-               }
-
-               for (i = 0; i < len; i++) {
-                       struct cache_entry *ce;
-
-                       if (!posns[i] ||
-                           (posns[i] != &df_conflict_list &&
-                            strcmp(first, posns[i]->name))) {
-                               continue;
-                       }
-
-                       if (posns[i] == &df_conflict_list) {
-                               src[i + merge] = &df_conflict_entry;
-                               continue;
-                       }
-
-                       if (posns[i]->directory) {
-                               any_dirs = 1;
-                               parse_tree(posns[i]->item.tree);
-                               subposns[i] = posns[i]->item.tree->entries;
-                               posns[i] = posns[i]->next;
-                               src[i + merge] = &df_conflict_entry;
-                               continue;
-                       }
-
-                       if (!merge)
-                               ce_stage = 0;
-                       else if (i + 1 < head_idx)
-                               ce_stage = 1;
-                       else if (i + 1 > head_idx)
-                               ce_stage = 3;
-                       else
-                               ce_stage = 2;
-
-                       ce = xcalloc(1, ce_size);
-                       ce->ce_mode = create_ce_mode(posns[i]->mode);
-                       ce->ce_flags = create_ce_flags(baselen + pathlen,
-                                                      ce_stage);
-                       memcpy(ce->name, base, baselen);
-                       memcpy(ce->name + baselen, first, pathlen + 1);
-
-                       any_files = 1;
-
-                       memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
-                       src[i + merge] = ce;
-                       subposns[i] = &df_conflict_list;
-                       posns[i] = posns[i]->next;
-               }
-               if (any_files) {
-                       if (merge) {
-                               int ret;
-
-#if DBRT_DEBUG > 1
-                               printf("%s:\n", first);
-                               for (i = 0; i < src_size; i++) {
-                                       printf(" %d ", i);
-                                       if (src[i])
-                                               printf("%s\n", sha1_to_hex(src[i]->sha1));
-                                       else
-                                               printf("\n");
-                               }
-#endif
-                               ret = fn(src);
-                               
-#if DBRT_DEBUG > 1
-                               printf("Added %d entries\n", ret);
-#endif
-                               *indpos += ret;
-                       } else {
-                               for (i = 0; i < src_size; i++) {
-                                       if (src[i]) {
-                                               add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
-                                       }
-                               }
-                       }
-               }
-               if (any_dirs) {
-                       char *newbase = xmalloc(baselen + 2 + pathlen);
-                       memcpy(newbase, base, baselen);
-                       memcpy(newbase + baselen, first, pathlen);
-                       newbase[baselen + pathlen] = '/';
-                       newbase[baselen + pathlen + 1] = '\0';
-                       if (unpack_trees_rec(subposns, len, newbase, fn,
-                                            indpos))
-                               return -1;
-                       free(newbase);
-               }
-               free(subposns);
-               free(src);
-       } while (1);
-}
-
-static void reject_merge(struct cache_entry *ce)
-{
-       die("Entry '%s' would be overwritten by merge. Cannot merge.", 
-           ce->name);
-}
-
-/* Unlink the last component and attempt to remove leading
- * directories, in case this unlink is the removal of the
- * last entry in the directory -- empty directories are removed.
- */
-static void unlink_entry(char *name)
-{
-       char *cp, *prev;
-
-       if (unlink(name))
-               return;
-       prev = NULL;
-       while (1) {
-               int status;
-               cp = strrchr(name, '/');
-               if (prev)
-                       *prev = '/';
-               if (!cp)
-                       break;
-
-               *cp = 0;
-               status = rmdir(name);
-               if (status) {
-                       *cp = '/';
-                       break;
-               }
-               prev = cp;
-       }
-}
-
-static void progress_interval(int signum)
-{
-       progress_update = 1;
-}
-
-static void setup_progress_signal(void)
-{
-       struct sigaction sa;
-       struct itimerval v;
-
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = progress_interval;
-       sigemptyset(&sa.sa_mask);
-       sa.sa_flags = SA_RESTART;
-       sigaction(SIGALRM, &sa, NULL);
-
-       v.it_interval.tv_sec = 1;
-       v.it_interval.tv_usec = 0;
-       v.it_value = v.it_interval;
-       setitimer(ITIMER_REAL, &v, NULL);
-}
-
-static void check_updates(struct cache_entry **src, int nr)
-{
-       static struct checkout state = {
-               .base_dir = "",
-               .force = 1,
-               .quiet = 1,
-               .refresh_cache = 1,
-       };
-       unsigned short mask = htons(CE_UPDATE);
-       unsigned last_percent = 200, cnt = 0, total = 0;
-
-       if (update && verbose_update) {
-               for (total = cnt = 0; cnt < nr; cnt++) {
-                       struct cache_entry *ce = src[cnt];
-                       if (!ce->ce_mode || ce->ce_flags & mask)
-                               total++;
-               }
-
-               /* Don't bother doing this for very small updates */
-               if (total < 250)
-                       total = 0;
-
-               if (total) {
-                       fprintf(stderr, "Checking files out...\n");
-                       setup_progress_signal();
-                       progress_update = 1;
-               }
-               cnt = 0;
-       }
-
-       while (nr--) {
-               struct cache_entry *ce = *src++;
-
-               if (total) {
-                       if (!ce->ce_mode || ce->ce_flags & mask) {
-                               unsigned percent;
-                               cnt++;
-                               percent = (cnt * 100) / total;
-                               if (percent != last_percent ||
-                                   progress_update) {
-                                       fprintf(stderr, "%4u%% (%u/%u) done\r",
-                                               percent, cnt, total);
-                                       last_percent = percent;
-                               }
-                       }
-               }
-               if (!ce->ce_mode) {
-                       if (update)
-                               unlink_entry(ce->name);
-                       continue;
-               }
-               if (ce->ce_flags & mask) {
-                       ce->ce_flags &= ~mask;
-                       if (update)
-                               checkout_entry(ce, &state, NULL);
-               }
-       }
-       if (total) {
-               signal(SIGALRM, SIG_IGN);
-               fputc('\n', stderr);
-       }
-}
-
-static int unpack_trees(merge_fn_t fn)
-{
-       int indpos = 0;
-       unsigned len = object_list_length(trees);
-       struct tree_entry_list **posns;
-       int i;
-       struct object_list *posn = trees;
-       merge_size = len;
-
-       if (len) {
-               posns = xmalloc(len * sizeof(struct tree_entry_list *));
-               for (i = 0; i < len; i++) {
-                       posns[i] = ((struct tree *) posn->item)->entries;
-                       posn = posn->next;
-               }
-               if (unpack_trees_rec(posns, len, prefix ? prefix : "",
-                                    fn, &indpos))
-                       return -1;
-       }
-
-       if (trivial_merges_only && nontrivial_merge)
-               die("Merge requires file-level merging");
-
-       check_updates(active_cache, active_nr);
-       return 0;
-}
-
-static int list_tree(unsigned char *sha1)
-{
-       struct tree *tree = parse_tree_indirect(sha1);
-       if (!tree)
-               return -1;
-       object_list_append(&tree->object, &trees);
-       return 0;
-}
-
-static int same(struct cache_entry *a, struct cache_entry *b)
-{
-       if (!!a != !!b)
-               return 0;
-       if (!a && !b)
-               return 1;
-       return a->ce_mode == b->ce_mode && 
-               !memcmp(a->sha1, b->sha1, 20);
-}
-
-
-/*
- * When a CE gets turned into an unmerged entry, we
- * want it to be up-to-date
- */
-static void verify_uptodate(struct cache_entry *ce)
-{
-       struct stat st;
-
-       if (index_only)
-               return;
-
-       if (!lstat(ce->name, &st)) {
-               unsigned changed = ce_match_stat(ce, &st, 1);
-               if (!changed)
-                       return;
-               errno = 0;
-       }
-       if (errno == ENOENT)
-               return;
-       die("Entry '%s' not uptodate. Cannot merge.", ce->name);
-}
-
-static void invalidate_ce_path(struct cache_entry *ce)
-{
-       if (ce)
-               cache_tree_invalidate_path(active_cache_tree, ce->name);
-}
-
-static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
-{
-       merge->ce_flags |= htons(CE_UPDATE);
-       if (old) {
-               /*
-                * See if we can re-use the old CE directly?
-                * That way we get the uptodate stat info.
-                *
-                * This also removes the UPDATE flag on
-                * a match.
-                */
-               if (same(old, merge)) {
-                       *merge = *old;
-               } else {
-                       verify_uptodate(old);
-                       invalidate_ce_path(old);
-               }
-       }
-       merge->ce_flags &= ~htons(CE_STAGEMASK);
-       add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
-       return 1;
-}
-
-static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
-{
-       if (old)
-               verify_uptodate(old);
-       ce->ce_mode = 0;
-       add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
-       invalidate_ce_path(ce);
-       return 1;
-}
-
-static int keep_entry(struct cache_entry *ce)
-{
-       add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
-       return 1;
-}
-
-#if DBRT_DEBUG
-static void show_stage_entry(FILE *o,
-                            const char *label, const struct cache_entry *ce)
-{
-       if (!ce)
-               fprintf(o, "%s (missing)\n", label);
-       else
-               fprintf(o, "%s%06o %s %d\t%s\n",
-                       label,
-                       ntohl(ce->ce_mode),
-                       sha1_to_hex(ce->sha1),
-                       ce_stage(ce),
-                       ce->name);
-}
-#endif
-
-static int threeway_merge(struct cache_entry **stages)
-{
-       struct cache_entry *index;
-       struct cache_entry *head; 
-       struct cache_entry *remote = stages[head_idx + 1];
-       int count;
-       int head_match = 0;
-       int remote_match = 0;
-
-       int df_conflict_head = 0;
-       int df_conflict_remote = 0;
-
-       int any_anc_missing = 0;
-       int no_anc_exists = 1;
-       int i;
-
-       for (i = 1; i < head_idx; i++) {
-               if (!stages[i])
-                       any_anc_missing = 1;
-               else
-                       no_anc_exists = 0;
-       }
-
-       index = stages[0];
-       head = stages[head_idx];
-
-       if (head == &df_conflict_entry) {
-               df_conflict_head = 1;
-               head = NULL;
-       }
-
-       if (remote == &df_conflict_entry) {
-               df_conflict_remote = 1;
-               remote = NULL;
-       }
-
-       /* First, if there's a #16 situation, note that to prevent #13
-        * and #14. 
-        */
-       if (!same(remote, head)) {
-               for (i = 1; i < head_idx; i++) {
-                       if (same(stages[i], head)) {
-                               head_match = i;
-                       }
-                       if (same(stages[i], remote)) {
-                               remote_match = i;
-                       }
-               }
-       }
-
-       /* We start with cases where the index is allowed to match
-        * something other than the head: #14(ALT) and #2ALT, where it
-        * is permitted to match the result instead.
-        */
-       /* #14, #14ALT, #2ALT */
-       if (remote && !df_conflict_head && head_match && !remote_match) {
-               if (index && !same(index, remote) && !same(index, head))
-                       reject_merge(index);
-               return merged_entry(remote, index);
-       }
-       /*
-        * If we have an entry in the index cache, then we want to
-        * make sure that it matches head.
-        */
-       if (index && !same(index, head)) {
-               reject_merge(index);
-       }
-
-       if (head) {
-               /* #5ALT, #15 */
-               if (same(head, remote))
-                       return merged_entry(head, index);
-               /* #13, #3ALT */
-               if (!df_conflict_remote && remote_match && !head_match)
-                       return merged_entry(head, index);
-       }
-
-       /* #1 */
-       if (!head && !remote && any_anc_missing)
-               return 0;
-
-       /* Under the new "aggressive" rule, we resolve mostly trivial
-        * cases that we historically had git-merge-one-file resolve.
-        */
-       if (aggressive) {
-               int head_deleted = !head && !df_conflict_head;
-               int remote_deleted = !remote && !df_conflict_remote;
-               /*
-                * Deleted in both.
-                * Deleted in one and unchanged in the other.
-                */
-               if ((head_deleted && remote_deleted) ||
-                   (head_deleted && remote && remote_match) ||
-                   (remote_deleted && head && head_match)) {
-                       if (index)
-                               return deleted_entry(index, index);
-                       return 0;
-               }
-               /*
-                * Added in both, identically.
-                */
-               if (no_anc_exists && head && remote && same(head, remote))
-                       return merged_entry(head, index);
-
-       }
-
-       /* Below are "no merge" cases, which require that the index be
-        * up-to-date to avoid the files getting overwritten with
-        * conflict resolution files. 
-        */
-       if (index) {
-               verify_uptodate(index);
-       }
-
-       nontrivial_merge = 1;
-
-       /* #2, #3, #4, #6, #7, #9, #11. */
-       count = 0;
-       if (!head_match || !remote_match) {
-               for (i = 1; i < head_idx; i++) {
-                       if (stages[i]) {
-                               keep_entry(stages[i]);
-                               count++;
-                               break;
-                       }
-               }
-       }
-#if DBRT_DEBUG
-       else {
-               fprintf(stderr, "read-tree: warning #16 detected\n");
-               show_stage_entry(stderr, "head   ", stages[head_match]);
-               show_stage_entry(stderr, "remote ", stages[remote_match]);
-       }
-#endif
-       if (head) { count += keep_entry(head); }
-       if (remote) { count += keep_entry(remote); }
-       return count;
-}
-
-/*
- * Two-way merge.
- *
- * The rule is to "carry forward" what is in the index without losing
- * information across a "fast forward", favoring a successful merge
- * over a merge failure when it makes sense.  For details of the
- * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
- *
- */
-static int twoway_merge(struct cache_entry **src)
-{
-       struct cache_entry *current = src[0];
-       struct cache_entry *oldtree = src[1], *newtree = src[2];
-
-       if (merge_size != 2)
-               return error("Cannot do a twoway merge of %d trees",
-                            merge_size);
-
-       if (current) {
-               if ((!oldtree && !newtree) || /* 4 and 5 */
-                   (!oldtree && newtree &&
-                    same(current, newtree)) || /* 6 and 7 */
-                   (oldtree && newtree &&
-                    same(oldtree, newtree)) || /* 14 and 15 */
-                   (oldtree && newtree &&
-                    !same(oldtree, newtree) && /* 18 and 19*/
-                    same(current, newtree))) {
-                       return keep_entry(current);
-               }
-               else if (oldtree && !newtree && same(current, oldtree)) {
-                       /* 10 or 11 */
-                       return deleted_entry(oldtree, current);
-               }
-               else if (oldtree && newtree &&
-                        same(current, oldtree) && !same(current, newtree)) {
-                       /* 20 or 21 */
-                       return merged_entry(newtree, current);
-               }
-               else {
-                       /* all other failures */
-                       if (oldtree)
-                               reject_merge(oldtree);
-                       if (current)
-                               reject_merge(current);
-                       if (newtree)
-                               reject_merge(newtree);
-                       return -1;
-               }
-       }
-       else if (newtree)
-               return merged_entry(newtree, current);
-       else
-               return deleted_entry(oldtree, current);
-}
-
-/*
- * Bind merge.
- *
- * Keep the index entries at stage0, collapse stage1 but make sure
- * stage0 does not have anything there.
- */
-static int bind_merge(struct cache_entry **src)
-{
-       struct cache_entry *old = src[0];
-       struct cache_entry *a = src[1];
-
-       if (merge_size != 1)
-               return error("Cannot do a bind merge of %d trees\n",
-                            merge_size);
-       if (a && old)
-               die("Entry '%s' overlaps.  Cannot bind.", a->name);
-       if (!a)
-               return keep_entry(old);
-       else
-               return merged_entry(a, NULL);
-}
-
-/*
- * One-way merge.
- *
- * The rule is:
- * - take the stat information from stage0, take the data from stage1
- */
-static int oneway_merge(struct cache_entry **src)
-{
-       struct cache_entry *old = src[0];
-       struct cache_entry *a = src[1];
-
-       if (merge_size != 1)
-               return error("Cannot do a oneway merge of %d trees",
-                            merge_size);
-
-       if (!a) {
-               invalidate_ce_path(old);
-               return 0;
-       }
-       if (old && same(old, a)) {
-               return keep_entry(old);
-       }
-       return merged_entry(a, NULL);
-}
-
-static int read_cache_unmerged(void)
-{
-       int i, deleted;
-       struct cache_entry **dst;
-
-       read_cache();
-       dst = active_cache;
-       deleted = 0;
-       for (i = 0; i < active_nr; i++) {
-               struct cache_entry *ce = active_cache[i];
-               if (ce_stage(ce)) {
-                       deleted++;
-                       invalidate_ce_path(ce);
-                       continue;
-               }
-               if (deleted)
-                       *dst = ce;
-               dst++;
-       }
-       active_nr -= deleted;
-       return deleted;
-}
-
-static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
-{
-       struct tree_entry_list *ent;
-       int cnt;
-       
-       memcpy(it->sha1, tree->object.sha1, 20);
-       for (cnt = 0, ent = tree->entries; ent; ent = ent->next) {
-               if (!ent->directory)
-                       cnt++;
-               else {
-                       struct cache_tree_sub *sub;
-                       struct tree *subtree = (struct tree *)ent->item.tree;
-                       if (!subtree->object.parsed)
-                               parse_tree(subtree);
-                       sub = cache_tree_sub(it, ent->name);
-                       sub->cache_tree = cache_tree();
-                       prime_cache_tree_rec(sub->cache_tree, subtree);
-                       cnt += sub->cache_tree->entry_count;
-               }
-       }
-       it->entry_count = cnt;
-}
-
-static void prime_cache_tree(void)
-{
-       struct tree *tree = (struct tree *)trees->item;
-       if (!tree)
-               return;
-       active_cache_tree = cache_tree();
-       prime_cache_tree_rec(active_cache_tree, tree);
-
-}
-
-static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
-
-static struct cache_file cache_file;
-
-int main(int argc, char **argv)
-{
-       int i, newfd, reset, stage = 0;
-       unsigned char sha1[20];
-       merge_fn_t fn = NULL;
-
-       setup_git_directory();
-       git_config(git_default_config);
-
-       newfd = hold_index_file_for_update(&cache_file, get_index_file());
-       if (newfd < 0)
-               die("unable to create new cachefile");
-
-       git_config(git_default_config);
-
-       merge = 0;
-       reset = 0;
-       for (i = 1; i < argc; i++) {
-               const char *arg = argv[i];
-
-               /* "-u" means "update", meaning that a merge will update
-                * the working tree.
-                */
-               if (!strcmp(arg, "-u")) {
-                       update = 1;
-                       continue;
-               }
-
-               if (!strcmp(arg, "-v")) {
-                       verbose_update = 1;
-                       continue;
-               }
-
-               /* "-i" means "index only", meaning that a merge will
-                * not even look at the working tree.
-                */
-               if (!strcmp(arg, "-i")) {
-                       index_only = 1;
-                       continue;
-               }
-
-               /* "--prefix=<subdirectory>/" means keep the current index
-                *  entries and put the entries from the tree under the
-                * given subdirectory.
-                */
-               if (!strncmp(arg, "--prefix=", 9)) {
-                       if (stage || merge || prefix)
-                               usage(read_tree_usage);
-                       prefix = arg + 9;
-                       merge = 1;
-                       stage = 1;
-                       if (read_cache_unmerged())
-                               die("you need to resolve your current index first");
-                       continue;
-               }
-
-               /* This differs from "-m" in that we'll silently ignore unmerged entries */
-               if (!strcmp(arg, "--reset")) {
-                       if (stage || merge || prefix)
-                               usage(read_tree_usage);
-                       reset = 1;
-                       merge = 1;
-                       stage = 1;
-                       read_cache_unmerged();
-                       continue;
-               }
-
-               if (!strcmp(arg, "--trivial")) {
-                       trivial_merges_only = 1;
-                       continue;
-               }
-
-               if (!strcmp(arg, "--aggressive")) {
-                       aggressive = 1;
-                       continue;
-               }
-
-               /* "-m" stands for "merge", meaning we start in stage 1 */
-               if (!strcmp(arg, "-m")) {
-                       if (stage || merge || prefix)
-                               usage(read_tree_usage);
-                       if (read_cache_unmerged())
-                               die("you need to resolve your current index first");
-                       stage = 1;
-                       merge = 1;
-                       continue;
-               }
-
-               /* using -u and -i at the same time makes no sense */
-               if (1 < index_only + update)
-                       usage(read_tree_usage);
-
-               if (get_sha1(arg, sha1) < 0)
-                       usage(read_tree_usage);
-               if (list_tree(sha1) < 0)
-                       die("failed to unpack tree object %s", arg);
-               stage++;
-       }
-       if ((update||index_only) && !merge)
-               usage(read_tree_usage);
-
-       if (prefix) {
-               int pfxlen = strlen(prefix);
-               int pos;
-               if (prefix[pfxlen-1] != '/')
-                       die("prefix must end with /");
-               if (stage != 2)
-                       die("binding merge takes only one tree");
-               pos = cache_name_pos(prefix, pfxlen);
-               if (0 <= pos)
-                       die("corrupt index file");
-               pos = -pos-1;
-               if (pos < active_nr &&
-                   !strncmp(active_cache[pos]->name, prefix, pfxlen))
-                       die("subdirectory '%s' already exists.", prefix);
-               pos = cache_name_pos(prefix, pfxlen-1);
-               if (0 <= pos)
-                       die("file '%.*s' already exists.", pfxlen-1, prefix);
-       }
-
-       if (merge) {
-               if (stage < 2)
-                       die("just how do you expect me to merge %d trees?", stage-1);
-               switch (stage - 1) {
-               case 1:
-                       fn = prefix ? bind_merge : oneway_merge;
-                       break;
-               case 2:
-                       fn = twoway_merge;
-                       break;
-               case 3:
-               default:
-                       fn = threeway_merge;
-                       cache_tree_free(&active_cache_tree);
-                       break;
-               }
-
-               if (stage - 1 >= 3)
-                       head_idx = stage - 2;
-               else
-                       head_idx = 1;
-       }
-
-       unpack_trees(fn);
-
-       /*
-        * When reading only one tree (either the most basic form,
-        * "-m ent" or "--reset ent" form), we can obtain a fully
-        * valid cache-tree because the index must match exactly
-        * what came from the tree.
-        */
-       if (trees->item && !prefix && (!merge || (stage == 2))) {
-               cache_tree_free(&active_cache_tree);
-               prime_cache_tree();
-       }
-
-       if (write_cache(newfd, active_cache, active_nr) ||
-           commit_index_file(&cache_file))
-               die("unable to write new index file");
-       return 0;
-}
diff --git a/refs.c b/refs.c
index 03398cc..713ca46 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -76,8 +76,8 @@ int create_symref(const char *git_HEAD, const char *refs_heads_master)
        char ref[1000];
        int fd, len, written;
 
-#ifdef USE_SYMLINK_HEAD
-       if (!only_use_symrefs) {
+#ifndef NO_SYMLINK_HEAD
+       if (prefer_symlink_refs) {
                unlink(git_HEAD);
                if (!symlink(refs_heads_master, git_HEAD))
                        return 0;
@@ -104,6 +104,11 @@ int create_symref(const char *git_HEAD, const char *refs_heads_master)
                error("Unable to create %s", git_HEAD);
                return -3;
        }
+       if (adjust_shared_perm(git_HEAD)) {
+               unlink(lockpath);
+               error("Unable to fix permissions on %s", lockpath);
+               return -4;
+       }
        return 0;
 }
 
@@ -114,7 +119,7 @@ int read_ref(const char *filename, unsigned char *sha1)
        return -1;
 }
 
-static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
+static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim)
 {
        int retval = 0;
        DIR *dir = opendir(git_path("%s", base));
@@ -142,11 +147,13 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
                        namelen = strlen(de->d_name);
                        if (namelen > 255)
                                continue;
+                       if (namelen>5 && !strcmp(de->d_name+namelen-5,".lock"))
+                               continue;
                        memcpy(path + baselen, de->d_name, namelen+1);
                        if (stat(git_path("%s", path), &st) < 0)
                                continue;
                        if (S_ISDIR(st.st_mode)) {
-                               retval = do_for_each_ref(path, fn);
+                               retval = do_for_each_ref(path, fn, trim);
                                if (retval)
                                        break;
                                continue;
@@ -160,7 +167,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
                                      "commit object!", path);
                                continue;
                        }
-                       retval = fn(path, sha1);
+                       retval = fn(path + trim, sha1);
                        if (retval)
                                break;
                }
@@ -180,125 +187,29 @@ int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
 
 int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
 {
-       return do_for_each_ref("refs", fn);
-}
-
-static char *ref_file_name(const char *ref)
-{
-       char *base = get_refs_directory();
-       int baselen = strlen(base);
-       int reflen = strlen(ref);
-       char *ret = xmalloc(baselen + 2 + reflen);
-       sprintf(ret, "%s/%s", base, ref);
-       return ret;
-}
-
-static char *ref_lock_file_name(const char *ref)
-{
-       char *base = get_refs_directory();
-       int baselen = strlen(base);
-       int reflen = strlen(ref);
-       char *ret = xmalloc(baselen + 7 + reflen);
-       sprintf(ret, "%s/%s.lock", base, ref);
-       return ret;
-}
-
-int get_ref_sha1(const char *ref, unsigned char *sha1)
-{
-       const char *filename;
-
-       if (check_ref_format(ref))
-               return -1;
-       filename = git_path("refs/%s", ref);
-       return read_ref(filename, sha1);
+       return do_for_each_ref("refs", fn, 0);
 }
 
-static int lock_ref_file(const char *filename, const char *lock_filename,
-                        const unsigned char *old_sha1)
+int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1))
 {
-       int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
-       unsigned char current_sha1[20];
-       int retval;
-       if (fd < 0) {
-               return error("Couldn't open lock file for %s: %s",
-                            filename, strerror(errno));
-       }
-       retval = read_ref(filename, current_sha1);
-       if (old_sha1) {
-               if (retval) {
-                       close(fd);
-                       unlink(lock_filename);
-                       return error("Could not read the current value of %s",
-                                    filename);
-               }
-               if (memcmp(current_sha1, old_sha1, 20)) {
-                       close(fd);
-                       unlink(lock_filename);
-                       error("The current value of %s is %s",
-                             filename, sha1_to_hex(current_sha1));
-                       return error("Expected %s",
-                                    sha1_to_hex(old_sha1));
-               }
-       } else {
-               if (!retval) {
-                       close(fd);
-                       unlink(lock_filename);
-                       return error("Unexpectedly found a value of %s for %s",
-                                    sha1_to_hex(current_sha1), filename);
-               }
-       }
-       return fd;
+       return do_for_each_ref("refs/tags", fn, 10);
 }
 
-int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
+int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1))
 {
-       char *filename;
-       char *lock_filename;
-       int retval;
-       if (check_ref_format(ref))
-               return -1;
-       filename = ref_file_name(ref);
-       lock_filename = ref_lock_file_name(ref);
-       retval = lock_ref_file(filename, lock_filename, old_sha1);
-       free(filename);
-       free(lock_filename);
-       return retval;
+       return do_for_each_ref("refs/heads", fn, 11);
 }
 
-static int write_ref_file(const char *filename,
-                         const char *lock_filename, int fd,
-                         const unsigned char *sha1)
+int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
 {
-       char *hex = sha1_to_hex(sha1);
-       char term = '\n';
-       if (write(fd, hex, 40) < 40 ||
-           write(fd, &term, 1) < 1) {
-               error("Couldn't write %s", filename);
-               close(fd);
-               return -1;
-       }
-       close(fd);
-       rename(lock_filename, filename);
-       return 0;
+       return do_for_each_ref("refs/remotes", fn, 13);
 }
 
-int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
+int get_ref_sha1(const char *ref, unsigned char *sha1)
 {
-       char *filename;
-       char *lock_filename;
-       int retval;
-       if (fd < 0)
-               return -1;
        if (check_ref_format(ref))
                return -1;
-       filename = ref_file_name(ref);
-       lock_filename = ref_lock_file_name(ref);
-       if (safe_create_leading_directories(filename))
-               die("unable to create leading directory for %s", filename);
-       retval = write_ref_file(filename, lock_filename, fd, sha1);
-       free(filename);
-       free(lock_filename);
-       return retval;
+       return read_ref(git_path("refs/%s", ref), sha1);
 }
 
 /*
@@ -353,25 +264,257 @@ int check_ref_format(const char *ref)
        }
 }
 
-int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
+static struct ref_lock *verify_lock(struct ref_lock *lock,
+       const unsigned char *old_sha1, int mustexist)
+{
+       char buf[40];
+       int nr, fd = open(lock->ref_file, O_RDONLY);
+       if (fd < 0 && (mustexist || errno != ENOENT)) {
+               error("Can't verify ref %s", lock->ref_file);
+               unlock_ref(lock);
+               return NULL;
+       }
+       nr = read(fd, buf, 40);
+       close(fd);
+       if (nr != 40 || get_sha1_hex(buf, lock->old_sha1) < 0) {
+               error("Can't verify ref %s", lock->ref_file);
+               unlock_ref(lock);
+               return NULL;
+       }
+       if (memcmp(lock->old_sha1, old_sha1, 20)) {
+               error("Ref %s is at %s but expected %s", lock->ref_file,
+                       sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
+               unlock_ref(lock);
+               return NULL;
+       }
+       return lock;
+}
+
+static struct ref_lock *lock_ref_sha1_basic(const char *path,
+       int plen,
+       const unsigned char *old_sha1, int mustexist)
+{
+       struct ref_lock *lock;
+       struct stat st;
+
+       lock = xcalloc(1, sizeof(struct ref_lock));
+       lock->lock_fd = -1;
+
+       plen = strlen(path) - plen;
+       path = resolve_ref(path, lock->old_sha1, mustexist);
+       if (!path) {
+               unlock_ref(lock);
+               return NULL;
+       }
+       lock->lk = xcalloc(1, sizeof(struct lock_file));
+
+       lock->ref_file = strdup(path);
+       lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen));
+       lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;
+
+       if (safe_create_leading_directories(lock->ref_file))
+               die("unable to create directory for %s", lock->ref_file);
+       lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file);
+       if (lock->lock_fd < 0) {
+               error("Couldn't open lock file %s: %s",
+                     lock->lk->filename, strerror(errno));
+               unlock_ref(lock);
+               return NULL;
+       }
+
+       return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
+}
+
+struct ref_lock *lock_ref_sha1(const char *ref,
+       const unsigned char *old_sha1, int mustexist)
 {
-       char *filename;
-       char *lock_filename;
-       int fd;
-       int retval;
        if (check_ref_format(ref))
+               return NULL;
+       return lock_ref_sha1_basic(git_path("refs/%s", ref),
+               5 + strlen(ref), old_sha1, mustexist);
+}
+
+struct ref_lock *lock_any_ref_for_update(const char *ref,
+       const unsigned char *old_sha1, int mustexist)
+{
+       return lock_ref_sha1_basic(git_path("%s", ref),
+               strlen(ref), old_sha1, mustexist);
+}
+
+void unlock_ref(struct ref_lock *lock)
+{
+       if (lock->lock_fd >= 0) {
+               close(lock->lock_fd);
+               /* Do not free lock->lk -- atexit() still looks at them */
+               if (lock->lk)
+                       rollback_lock_file(lock->lk);
+       }
+       if (lock->ref_file)
+               free(lock->ref_file);
+       if (lock->log_file)
+               free(lock->log_file);
+       free(lock);
+}
+
+static int log_ref_write(struct ref_lock *lock,
+       const unsigned char *sha1, const char *msg)
+{
+       int logfd, written, oflags = O_APPEND | O_WRONLY;
+       unsigned maxlen, len;
+       char *logrec;
+       const char *comitter;
+
+       if (log_all_ref_updates) {
+               if (safe_create_leading_directories(lock->log_file) < 0)
+                       return error("unable to create directory for %s",
+                               lock->log_file);
+               oflags |= O_CREAT;
+       }
+
+       logfd = open(lock->log_file, oflags, 0666);
+       if (logfd < 0) {
+               if (!log_all_ref_updates && errno == ENOENT)
+                       return 0;
+               return error("Unable to append to %s: %s",
+                       lock->log_file, strerror(errno));
+       }
+
+       setup_ident();
+       comitter = git_committer_info(1);
+       if (msg) {
+               maxlen = strlen(comitter) + strlen(msg) + 2*40 + 5;
+               logrec = xmalloc(maxlen);
+               len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
+                       sha1_to_hex(lock->old_sha1),
+                       sha1_to_hex(sha1),
+                       comitter,
+                       msg);
+       }
+       else {
+               maxlen = strlen(comitter) + 2*40 + 4;
+               logrec = xmalloc(maxlen);
+               len = snprintf(logrec, maxlen, "%s %s %s\n",
+                       sha1_to_hex(lock->old_sha1),
+                       sha1_to_hex(sha1),
+                       comitter);
+       }
+       written = len <= maxlen ? write(logfd, logrec, len) : -1;
+       free(logrec);
+       close(logfd);
+       if (written != len)
+               return error("Unable to append to %s", lock->log_file);
+       return 0;
+}
+
+int write_ref_sha1(struct ref_lock *lock,
+       const unsigned char *sha1, const char *logmsg)
+{
+       static char term = '\n';
+
+       if (!lock)
                return -1;
-       filename = ref_file_name(ref);
-       lock_filename = ref_lock_file_name(ref);
-       if (safe_create_leading_directories(filename))
-               die("unable to create leading directory for %s", filename);
-       fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
-       if (fd < 0) {
-               error("Writing %s", lock_filename);
-               perror("Open");
+       if (!lock->force_write && !memcmp(lock->old_sha1, sha1, 20)) {
+               unlock_ref(lock);
+               return 0;
        }
-       retval = write_ref_file(filename, lock_filename, fd, sha1);
-       free(filename);
-       free(lock_filename);
-       return retval;
+       if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
+           write(lock->lock_fd, &term, 1) != 1
+               || close(lock->lock_fd) < 0) {
+               error("Couldn't write %s", lock->lk->filename);
+               unlock_ref(lock);
+               return -1;
+       }
+       if (log_ref_write(lock, sha1, logmsg) < 0) {
+               unlock_ref(lock);
+               return -1;
+       }
+       if (commit_lock_file(lock->lk)) {
+               error("Couldn't set %s", lock->ref_file);
+               unlock_ref(lock);
+               return -1;
+       }
+       lock->lock_fd = -1;
+       unlock_ref(lock);
+       return 0;
+}
+
+int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
+{
+       const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
+       char *tz_c;
+       int logfd, tz;
+       struct stat st;
+       unsigned long date;
+       unsigned char logged_sha1[20];
+
+       logfile = git_path("logs/%s", ref);
+       logfd = open(logfile, O_RDONLY, 0);
+       if (logfd < 0)
+               die("Unable to read log %s: %s", logfile, strerror(errno));
+       fstat(logfd, &st);
+       if (!st.st_size)
+               die("Log %s is empty.", logfile);
+       logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
+       close(logfd);
+
+       lastrec = NULL;
+       rec = logend = logdata + st.st_size;
+       while (logdata < rec) {
+               if (logdata < rec && *(rec-1) == '\n')
+                       rec--;
+               lastgt = NULL;
+               while (logdata < rec && *(rec-1) != '\n') {
+                       rec--;
+                       if (*rec == '>')
+                               lastgt = rec;
+               }
+               if (!lastgt)
+                       die("Log %s is corrupt.", logfile);
+               date = strtoul(lastgt + 1, &tz_c, 10);
+               if (date <= at_time) {
+                       if (lastrec) {
+                               if (get_sha1_hex(lastrec, logged_sha1))
+                                       die("Log %s is corrupt.", logfile);
+                               if (get_sha1_hex(rec + 41, sha1))
+                                       die("Log %s is corrupt.", logfile);
+                               if (memcmp(logged_sha1, sha1, 20)) {
+                                       tz = strtoul(tz_c, NULL, 10);
+                                       fprintf(stderr,
+                                               "warning: Log %s has gap after %s.\n",
+                                               logfile, show_rfc2822_date(date, tz));
+                               }
+                       }
+                       else if (date == at_time) {
+                               if (get_sha1_hex(rec + 41, sha1))
+                                       die("Log %s is corrupt.", logfile);
+                       }
+                       else {
+                               if (get_sha1_hex(rec + 41, logged_sha1))
+                                       die("Log %s is corrupt.", logfile);
+                               if (memcmp(logged_sha1, sha1, 20)) {
+                                       tz = strtoul(tz_c, NULL, 10);
+                                       fprintf(stderr,
+                                               "warning: Log %s unexpectedly ended on %s.\n",
+                                               logfile, show_rfc2822_date(date, tz));
+                               }
+                       }
+                       munmap((void*)logdata, st.st_size);
+                       return 0;
+               }
+               lastrec = rec;
+       }
+
+       rec = logdata;
+       while (rec < logend && *rec != '>' && *rec != '\n')
+               rec++;
+       if (rec == logend || *rec == '\n')
+               die("Log %s is corrupt.", logfile);
+       date = strtoul(rec + 1, &tz_c, 10);
+       tz = strtoul(tz_c, NULL, 10);
+       if (get_sha1_hex(logdata, sha1))
+               die("Log %s is corrupt.", logfile);
+       munmap((void*)logdata, st.st_size);
+       fprintf(stderr, "warning: Log %s only goes back to %s.\n",
+               logfile, show_rfc2822_date(date, tz));
+       return 0;
 }
diff --git a/refs.h b/refs.h
index 2625596..553155c 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -1,26 +1,42 @@
 #ifndef REFS_H
 #define REFS_H
 
+struct ref_lock {
+       char *ref_file;
+       char *log_file;
+       struct lock_file *lk;
+       unsigned char old_sha1[20];
+       int lock_fd;
+       int force_write;
+};
+
 /*
  * Calls the specified function for each ref file until it returns nonzero,
  * and returns the value
  */
 extern int head_ref(int (*fn)(const char *path, const unsigned char *sha1));
 extern int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1));
+extern int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1));
+extern int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1));
+extern int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1));
 
 /** Reads the refs file specified into sha1 **/
 extern int get_ref_sha1(const char *ref, unsigned char *sha1);
 
-/** Locks ref and returns the fd to give to write_ref_sha1() if the ref
- * has the given value currently; otherwise, returns -1.
- **/
-extern int lock_ref_sha1(const char *ref, const unsigned char *old_sha1);
+/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/
+extern struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1, int mustexist);
+
+/** Locks any ref (for 'HEAD' type refs). */
+extern struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int mustexist);
+
+/** Release any lock taken but not written. **/
+extern void unlock_ref(struct ref_lock *lock);
 
-/** Writes sha1 into the refs file specified, locked with the given fd. **/
-extern int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1);
+/** Writes sha1 into the ref specified by the lock. **/
+extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
 
-/** Writes sha1 into the refs file specified. **/
-extern int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1);
+/** Reads log for the value of ref during at_time. **/
+extern int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1);
 
 /** Returns 0 if target has the right format for a ref. **/
 extern int check_ref_format(const char *target);
index c5ebb76..08fc4cc 100644 (file)
@@ -2,57 +2,83 @@
 #include <regex.h>
 
 static const char git_config_set_usage[] =
-"git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
+"git-repo-config [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
 
 static char* key = NULL;
-static char* value = NULL;
+static regex_t* key_regexp = NULL;
 static regex_t* regexp = NULL;
+static int show_keys = 0;
+static int use_key_regexp = 0;
 static int do_all = 0;
 static int do_not_match = 0;
 static int seen = 0;
 static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
 
+static int show_all_config(const char *key_, const char *value_)
+{
+       if (value_)
+               printf("%s=%s\n", key_, value_);
+       else
+               printf("%s\n", key_);
+       return 0;
+}
+
 static int show_config(const char* key_, const char* value_)
 {
+       char value[256];
+       const char *vptr = value;
+       int dup_error = 0;
+
        if (value_ == NULL)
                value_ = "";
 
-       if (!strcmp(key_, key) &&
-                       (regexp == NULL ||
+       if (!use_key_regexp && strcmp(key_, key))
+               return 0;
+       if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
+               return 0;
+       if (regexp != NULL &&
                         (do_not_match ^
-                         !regexec(regexp, value_, 0, NULL, 0)))) {
-               if (do_all) {
-                       printf("%s\n", value_);
-                       return 0;
-               }
-               if (seen > 0) {
-                       fprintf(stderr, "More than one value: %s\n", value);
-                       free(value);
-               }
+                         regexec(regexp, value_, 0, NULL, 0)))
+               return 0;
 
-               if (type == T_INT) {
-                       value = malloc(256);
-                       sprintf(value, "%d", git_config_int(key_, value_));
-               } else if (type == T_BOOL) {
-                       value = malloc(256);
-                       sprintf(value, "%s", git_config_bool(key_, value_)
-                                            ? "true" : "false");
-               } else {
-                       value = strdup(value_);
-               }
-               seen++;
+       if (show_keys)
+               printf("%s ", key_);
+       if (seen && !do_all)
+               dup_error = 1;
+       if (type == T_INT)
+               sprintf(value, "%d", git_config_int(key_, value_));
+       else if (type == T_BOOL)
+               vptr = git_config_bool(key_, value_) ? "true" : "false";
+       else
+               vptr = value_;
+       seen++;
+       if (dup_error) {
+               error("More than one value for the key %s: %s",
+                               key_, vptr);
        }
+       else
+               printf("%s\n", vptr);
+
        return 0;
 }
 
 static int get_value(const char* key_, const char* regex_)
 {
-       int i;
-
-       key = malloc(strlen(key_)+1);
-       for (i = 0; key_[i]; i++)
-               key[i] = tolower(key_[i]);
-       key[i] = 0;
+       char *tl;
+
+       key = strdup(key_);
+       for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
+               *tl = tolower(*tl);
+       for (tl=key; *tl && *tl != '.'; ++tl)
+               *tl = tolower(*tl);
+
+       if (use_key_regexp) {
+               key_regexp = (regex_t*)malloc(sizeof(regex_t));
+               if (regcomp(key_regexp, key, REG_EXTENDED)) {
+                       fprintf(stderr, "Invalid key pattern: %s\n", key_);
+                       return -1;
+               }
+       }
 
        if (regex_) {
                if (regex_[0] == '!') {
@@ -67,11 +93,7 @@ static int get_value(const char* key_, const char* regex_)
                }
        }
 
-       i = git_config(show_config);
-       if (value) {
-               printf("%s\n", value);
-               free(value);
-       }
+       git_config(show_config);
        free(key);
        if (regexp) {
                regfree(regexp);
@@ -79,20 +101,23 @@ static int get_value(const char* key_, const char* regex_)
        }
 
        if (do_all)
-               return 0;
+               return !seen;
 
-       return seen == 1 ? 0 : 1;
+       return (seen == 1) ? 0 : 1;
 }
 
 int main(int argc, const char **argv)
 {
-       setup_git_directory();
+       int nongit = 0;
+       setup_git_directory_gently(&nongit);
 
        while (1 < argc) {
                if (!strcmp(argv[1], "--int"))
                        type = T_INT;
                else if (!strcmp(argv[1], "--bool"))
                        type = T_BOOL;
+               else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
+                       return git_config(show_all_config);
                else
                        break;
                argc--;
@@ -112,6 +137,11 @@ int main(int argc, const char **argv)
                else if (!strcmp(argv[1], "--get-all")) {
                        do_all = 1;
                        return get_value(argv[2], NULL);
+               } else if (!strcmp(argv[1], "--get-regexp")) {
+                       show_keys = 1;
+                       use_key_regexp = 1;
+                       do_all = 1;
+                       return get_value(argv[2], NULL);
                } else
 
                        return git_config_set(argv[1], argv[2]);
@@ -125,6 +155,11 @@ int main(int argc, const char **argv)
                else if (!strcmp(argv[1], "--get-all")) {
                        do_all = 1;
                        return get_value(argv[2], argv[3]);
+               } else if (!strcmp(argv[1], "--get-regexp")) {
+                       show_keys = 1;
+                       use_key_regexp = 1;
+                       do_all = 1;
+                       return get_value(argv[2], argv[3]);
                } else if (!strcmp(argv[1], "--replace-all"))
 
                        return git_config_set_multivar(argv[2], argv[3], NULL, 1);
diff --git a/rev-list.c b/rev-list.c
deleted file mode 100644 (file)
index 8b0ec38..0000000
+++ /dev/null
@@ -1,357 +0,0 @@
-#include "cache.h"
-#include "refs.h"
-#include "tag.h"
-#include "commit.h"
-#include "tree.h"
-#include "blob.h"
-#include "tree-walk.h"
-#include "diff.h"
-#include "revision.h"
-
-/* bits #0-15 in revision.h */
-
-#define COUNTED                (1u<<16)
-
-static const char rev_list_usage[] =
-"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
-"  limiting output:\n"
-"    --max-count=nr\n"
-"    --max-age=epoch\n"
-"    --min-age=epoch\n"
-"    --sparse\n"
-"    --no-merges\n"
-"    --remove-empty\n"
-"    --all\n"
-"  ordering output:\n"
-"    --topo-order\n"
-"    --date-order\n"
-"  formatting output:\n"
-"    --parents\n"
-"    --objects | --objects-edge\n"
-"    --unpacked\n"
-"    --header | --pretty\n"
-"    --abbrev=nr | --no-abbrev\n"
-"    --abbrev-commit\n"
-"  special purpose:\n"
-"    --bisect"
-;
-
-struct rev_info revs;
-
-static int bisect_list = 0;
-static int show_timestamp = 0;
-static int hdr_termination = 0;
-static const char *header_prefix;
-
-static void show_commit(struct commit *commit)
-{
-       if (show_timestamp)
-               printf("%lu ", commit->date);
-       if (header_prefix)
-               fputs(header_prefix, stdout);
-       if (commit->object.flags & BOUNDARY)
-               putchar('-');
-       if (revs.abbrev_commit && revs.abbrev)
-               fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
-                     stdout);
-       else
-               fputs(sha1_to_hex(commit->object.sha1), stdout);
-       if (revs.parents) {
-               struct commit_list *parents = commit->parents;
-               while (parents) {
-                       struct object *o = &(parents->item->object);
-                       parents = parents->next;
-                       if (o->flags & TMP_MARK)
-                               continue;
-                       printf(" %s", sha1_to_hex(o->sha1));
-                       o->flags |= TMP_MARK;
-               }
-               /* TMP_MARK is a general purpose flag that can
-                * be used locally, but the user should clean
-                * things up after it is done with them.
-                */
-               for (parents = commit->parents;
-                    parents;
-                    parents = parents->next)
-                       parents->item->object.flags &= ~TMP_MARK;
-       }
-       if (revs.commit_format == CMIT_FMT_ONELINE)
-               putchar(' ');
-       else
-               putchar('\n');
-
-       if (revs.verbose_header) {
-               static char pretty_header[16384];
-               pretty_print_commit(revs.commit_format, commit, ~0,
-                                   pretty_header, sizeof(pretty_header),
-                                   revs.abbrev);
-               printf("%s%c", pretty_header, hdr_termination);
-       }
-       fflush(stdout);
-}
-
-static struct object_list **process_blob(struct blob *blob,
-                                        struct object_list **p,
-                                        struct name_path *path,
-                                        const char *name)
-{
-       struct object *obj = &blob->object;
-
-       if (!revs.blob_objects)
-               return p;
-       if (obj->flags & (UNINTERESTING | SEEN))
-               return p;
-       obj->flags |= SEEN;
-       return add_object(obj, p, path, name);
-}
-
-static struct object_list **process_tree(struct tree *tree,
-                                        struct object_list **p,
-                                        struct name_path *path,
-                                        const char *name)
-{
-       struct object *obj = &tree->object;
-       struct tree_entry_list *entry;
-       struct name_path me;
-
-       if (!revs.tree_objects)
-               return p;
-       if (obj->flags & (UNINTERESTING | SEEN))
-               return p;
-       if (parse_tree(tree) < 0)
-               die("bad tree object %s", sha1_to_hex(obj->sha1));
-       obj->flags |= SEEN;
-       p = add_object(obj, p, path, name);
-       me.up = path;
-       me.elem = name;
-       me.elem_len = strlen(name);
-       entry = tree->entries;
-       tree->entries = NULL;
-       while (entry) {
-               struct tree_entry_list *next = entry->next;
-               if (entry->directory)
-                       p = process_tree(entry->item.tree, p, &me, entry->name);
-               else
-                       p = process_blob(entry->item.blob, p, &me, entry->name);
-               free(entry);
-               entry = next;
-       }
-       return p;
-}
-
-static void show_commit_list(struct rev_info *revs)
-{
-       struct commit *commit;
-       struct object_list *objects = NULL, **p = &objects, *pending;
-
-       while ((commit = get_revision(revs)) != NULL) {
-               p = process_tree(commit->tree, p, NULL, "");
-               show_commit(commit);
-       }
-       for (pending = revs->pending_objects; pending; pending = pending->next) {
-               struct object *obj = pending->item;
-               const char *name = pending->name;
-               if (obj->flags & (UNINTERESTING | SEEN))
-                       continue;
-               if (obj->type == tag_type) {
-                       obj->flags |= SEEN;
-                       p = add_object(obj, p, NULL, name);
-                       continue;
-               }
-               if (obj->type == tree_type) {
-                       p = process_tree((struct tree *)obj, p, NULL, name);
-                       continue;
-               }
-               if (obj->type == blob_type) {
-                       p = process_blob((struct blob *)obj, p, NULL, name);
-                       continue;
-               }
-               die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
-       }
-       while (objects) {
-               /* An object with name "foo\n0000000..." can be used to
-                * confuse downstream git-pack-objects very badly.
-                */
-               const char *ep = strchr(objects->name, '\n');
-               if (ep) {
-                       printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
-                              (int) (ep - objects->name),
-                              objects->name);
-               }
-               else
-                       printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
-               objects = objects->next;
-       }
-}
-
-/*
- * This is a truly stupid algorithm, but it's only
- * used for bisection, and we just don't care enough.
- *
- * We care just barely enough to avoid recursing for
- * non-merge entries.
- */
-static int count_distance(struct commit_list *entry)
-{
-       int nr = 0;
-
-       while (entry) {
-               struct commit *commit = entry->item;
-               struct commit_list *p;
-
-               if (commit->object.flags & (UNINTERESTING | COUNTED))
-                       break;
-               if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
-                       nr++;
-               commit->object.flags |= COUNTED;
-               p = commit->parents;
-               entry = p;
-               if (p) {
-                       p = p->next;
-                       while (p) {
-                               nr += count_distance(p);
-                               p = p->next;
-                       }
-               }
-       }
-
-       return nr;
-}
-
-static void clear_distance(struct commit_list *list)
-{
-       while (list) {
-               struct commit *commit = list->item;
-               commit->object.flags &= ~COUNTED;
-               list = list->next;
-       }
-}
-
-static struct commit_list *find_bisection(struct commit_list *list)
-{
-       int nr, closest;
-       struct commit_list *p, *best;
-
-       nr = 0;
-       p = list;
-       while (p) {
-               if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
-                       nr++;
-               p = p->next;
-       }
-       closest = 0;
-       best = list;
-
-       for (p = list; p; p = p->next) {
-               int distance;
-
-               if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
-                       continue;
-
-               distance = count_distance(p);
-               clear_distance(list);
-               if (nr - distance < distance)
-                       distance = nr - distance;
-               if (distance > closest) {
-                       best = p;
-                       closest = distance;
-               }
-       }
-       if (best)
-               best->next = NULL;
-       return best;
-}
-
-static void mark_edge_parents_uninteresting(struct commit *commit)
-{
-       struct commit_list *parents;
-
-       for (parents = commit->parents; parents; parents = parents->next) {
-               struct commit *parent = parents->item;
-               if (!(parent->object.flags & UNINTERESTING))
-                       continue;
-               mark_tree_uninteresting(parent->tree);
-               if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
-                       parent->object.flags |= SHOWN;
-                       printf("-%s\n", sha1_to_hex(parent->object.sha1));
-               }
-       }
-}
-
-static void mark_edges_uninteresting(struct commit_list *list)
-{
-       for ( ; list; list = list->next) {
-               struct commit *commit = list->item;
-
-               if (commit->object.flags & UNINTERESTING) {
-                       mark_tree_uninteresting(commit->tree);
-                       continue;
-               }
-               mark_edge_parents_uninteresting(commit);
-       }
-}
-
-int main(int argc, const char **argv)
-{
-       struct commit_list *list;
-       int i;
-
-       init_revisions(&revs);
-       revs.abbrev = 0;
-       revs.commit_format = CMIT_FMT_UNSPECIFIED;
-       argc = setup_revisions(argc, argv, &revs, NULL);
-
-       for (i = 1 ; i < argc; i++) {
-               const char *arg = argv[i];
-
-               if (!strcmp(arg, "--header")) {
-                       revs.verbose_header = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--timestamp")) {
-                       show_timestamp = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--bisect")) {
-                       bisect_list = 1;
-                       continue;
-               }
-               usage(rev_list_usage);
-
-       }
-       if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
-               /* The command line has a --pretty  */
-               hdr_termination = '\n';
-               if (revs.commit_format == CMIT_FMT_ONELINE)
-                       header_prefix = "";
-               else
-                       header_prefix = "commit ";
-       }
-       else if (revs.verbose_header)
-               /* Only --header was specified */
-               revs.commit_format = CMIT_FMT_RAW;
-
-       list = revs.commits;
-
-       if ((!list &&
-            (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
-             !revs.pending_objects)) ||
-           revs.diff)
-               usage(rev_list_usage);
-
-       save_commit_buffer = revs.verbose_header;
-       track_object_refs = 0;
-       if (bisect_list)
-               revs.limited = 1;
-
-       prepare_revision_walk(&revs);
-       if (revs.tree_objects)
-               mark_edges_uninteresting(revs.commits);
-
-       if (bisect_list)
-               revs.commits = find_bisection(revs.commits);
-
-       show_commit_list(&revs);
-
-       return 0;
-}
diff --git a/rev-parse.c b/rev-parse.c
deleted file mode 100644 (file)
index 62e16af..0000000
+++ /dev/null
@@ -1,349 +0,0 @@
-/*
- * rev-parse.c
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#include "cache.h"
-#include "commit.h"
-#include "refs.h"
-#include "quote.h"
-
-#define DO_REVS                1
-#define DO_NOREV       2
-#define DO_FLAGS       4
-#define DO_NONFLAGS    8
-static int filter = ~0;
-
-static char *def = NULL;
-
-#define NORMAL 0
-#define REVERSED 1
-static int show_type = NORMAL;
-static int symbolic = 0;
-static int abbrev = 0;
-static int output_sq = 0;
-
-static int revs_count = 0;
-
-/*
- * Some arguments are relevant "revision" arguments,
- * others are about output format or other details.
- * This sorts it all out.
- */
-static int is_rev_argument(const char *arg)
-{
-       static const char *rev_args[] = {
-               "--all",
-               "--bisect",
-               "--dense",
-               "--header",
-               "--max-age=",
-               "--max-count=",
-               "--min-age=",
-               "--no-merges",
-               "--objects",
-               "--objects-edge",
-               "--parents",
-               "--pretty",
-               "--sparse",
-               "--topo-order",
-               "--date-order",
-               "--unpacked",
-               NULL
-       };
-       const char **p = rev_args;
-
-       /* accept -<digit>, like traditional "head" */
-       if ((*arg == '-') && isdigit(arg[1]))
-               return 1;
-
-       for (;;) {
-               const char *str = *p++;
-               int len;
-               if (!str)
-                       return 0;
-               len = strlen(str);
-               if (!strcmp(arg, str) ||
-                   (str[len-1] == '=' && !strncmp(arg, str, len)))
-                       return 1;
-       }
-}
-
-/* Output argument as a string, either SQ or normal */
-static void show(const char *arg)
-{
-       if (output_sq) {
-               int sq = '\'', ch;
-
-               putchar(sq);
-               while ((ch = *arg++)) {
-                       if (ch == sq)
-                               fputs("'\\'", stdout);
-                       putchar(ch);
-               }
-               putchar(sq);
-               putchar(' ');
-       }
-       else
-               puts(arg);
-}
-
-/* Output a revision, only if filter allows it */
-static void show_rev(int type, const unsigned char *sha1, const char *name)
-{
-       if (!(filter & DO_REVS))
-               return;
-       def = NULL;
-       revs_count++;
-
-       if (type != show_type)
-               putchar('^');
-       if (symbolic && name)
-               show(name);
-       else if (abbrev)
-               show(find_unique_abbrev(sha1, abbrev));
-       else
-               show(sha1_to_hex(sha1));
-}
-
-/* Output a flag, only if filter allows it. */
-static int show_flag(char *arg)
-{
-       if (!(filter & DO_FLAGS))
-               return 0;
-       if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
-               show(arg);
-               return 1;
-       }
-       return 0;
-}
-
-static void show_default(void)
-{
-       char *s = def;
-
-       if (s) {
-               unsigned char sha1[20];
-
-               def = NULL;
-               if (!get_sha1(s, sha1)) {
-                       show_rev(NORMAL, sha1, s);
-                       return;
-               }
-       }
-}
-
-static int show_reference(const char *refname, const unsigned char *sha1)
-{
-       show_rev(NORMAL, sha1, refname);
-       return 0;
-}
-
-static void show_datestring(const char *flag, const char *datestr)
-{
-       static char buffer[100];
-
-       /* date handling requires both flags and revs */
-       if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
-               return;
-       snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
-       show(buffer);
-}
-
-static int show_file(const char *arg)
-{
-       show_default();
-       if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
-               show(arg);
-               return 1;
-       }
-       return 0;
-}
-
-int main(int argc, char **argv)
-{
-       int i, as_is = 0, verify = 0;
-       unsigned char sha1[20];
-       const char *prefix = setup_git_directory();
-       
-       git_config(git_default_config);
-
-       for (i = 1; i < argc; i++) {
-               char *arg = argv[i];
-               char *dotdot;
-
-               if (as_is) {
-                       if (show_file(arg) && as_is < 2)
-                               verify_filename(prefix, arg);
-                       continue;
-               }
-               if (!strcmp(arg,"-n")) {
-                       if (++i >= argc)
-                               die("-n requires an argument");
-                       if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
-                               show(arg);
-                               show(argv[i]);
-                       }
-                       continue;
-               }
-               if (!strncmp(arg,"-n",2)) {
-                       if ((filter & DO_FLAGS) && (filter & DO_REVS))
-                               show(arg);
-                       continue;
-               }
-
-               if (*arg == '-') {
-                       if (!strcmp(arg, "--")) {
-                               as_is = 2;
-                               /* Pass on the "--" if we show anything but files.. */
-                               if (filter & (DO_FLAGS | DO_REVS))
-                                       show_file(arg);
-                               continue;
-                       }
-                       if (!strcmp(arg, "--default")) {
-                               def = argv[i+1];
-                               i++;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--revs-only")) {
-                               filter &= ~DO_NOREV;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--no-revs")) {
-                               filter &= ~DO_REVS;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--flags")) {
-                               filter &= ~DO_NONFLAGS;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--no-flags")) {
-                               filter &= ~DO_FLAGS;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--verify")) {
-                               filter &= ~(DO_FLAGS|DO_NOREV);
-                               verify = 1;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--short") ||
-                           !strncmp(arg, "--short=", 8)) {
-                               filter &= ~(DO_FLAGS|DO_NOREV);
-                               verify = 1;
-                               abbrev = DEFAULT_ABBREV;
-                               if (arg[7] == '=')
-                                       abbrev = strtoul(arg + 8, NULL, 10);
-                               if (abbrev < MINIMUM_ABBREV)
-                                       abbrev = MINIMUM_ABBREV;
-                               else if (40 <= abbrev)
-                                       abbrev = 40;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--sq")) {
-                               output_sq = 1;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--not")) {
-                               show_type ^= REVERSED;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--symbolic")) {
-                               symbolic = 1;
-                               continue;
-                       }
-                       if (!strcmp(arg, "--all")) {
-                               for_each_ref(show_reference);
-                               continue;
-                       }
-                       if (!strcmp(arg, "--show-prefix")) {
-                               if (prefix)
-                                       puts(prefix);
-                               continue;
-                       }
-                       if (!strcmp(arg, "--show-cdup")) {
-                               const char *pfx = prefix;
-                               while (pfx) {
-                                       pfx = strchr(pfx, '/');
-                                       if (pfx) {
-                                               pfx++;
-                                               printf("../");
-                                       }
-                               }
-                               putchar('\n');
-                               continue;
-                       }
-                       if (!strcmp(arg, "--git-dir")) {
-                               const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
-                               static char cwd[PATH_MAX];
-                               if (gitdir) {
-                                       puts(gitdir);
-                                       continue;
-                               }
-                               if (!prefix) {
-                                       puts(".git");
-                                       continue;
-                               }
-                               if (!getcwd(cwd, PATH_MAX))
-                                       die("unable to get current working directory");
-                               printf("%s/.git\n", cwd);
-                               continue;
-                       }
-                       if (!strncmp(arg, "--since=", 8)) {
-                               show_datestring("--max-age=", arg+8);
-                               continue;
-                       }
-                       if (!strncmp(arg, "--after=", 8)) {
-                               show_datestring("--max-age=", arg+8);
-                               continue;
-                       }
-                       if (!strncmp(arg, "--before=", 9)) {
-                               show_datestring("--min-age=", arg+9);
-                               continue;
-                       }
-                       if (!strncmp(arg, "--until=", 8)) {
-                               show_datestring("--min-age=", arg+8);
-                               continue;
-                       }
-                       if (show_flag(arg) && verify)
-                               die("Needed a single revision");
-                       continue;
-               }
-
-               /* Not a flag argument */
-               dotdot = strstr(arg, "..");
-               if (dotdot) {
-                       unsigned char end[20];
-                       char *next = dotdot + 2;
-                       char *this = arg;
-                       *dotdot = 0;
-                       if (!*next)
-                               next = "HEAD";
-                       if (dotdot == arg)
-                               this = "HEAD";
-                       if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
-                               show_rev(NORMAL, end, next);
-                               show_rev(REVERSED, sha1, this);
-                               continue;
-                       }
-                       *dotdot = '.';
-               }
-               if (!get_sha1(arg, sha1)) {
-                       show_rev(NORMAL, sha1, arg);
-                       continue;
-               }
-               if (*arg == '^' && !get_sha1(arg+1, sha1)) {
-                       show_rev(REVERSED, sha1, arg+1);
-                       continue;
-               }
-               as_is = 1;
-               if (!show_file(arg))
-                       continue;
-               if (verify)
-                       die("Needed a single revision");
-               verify_filename(prefix, arg);
-       }
-       show_default();
-       if (verify && revs_count != 1)
-               die("Needed a single revision");
-       return 0;
-}
index f2a9f25..75c648c 100644 (file)
@@ -53,8 +53,9 @@ static void mark_blob_uninteresting(struct blob *blob)
 
 void mark_tree_uninteresting(struct tree *tree)
 {
+       struct tree_desc desc;
+       struct name_entry entry;
        struct object *obj = &tree->object;
-       struct tree_entry_list *entry;
 
        if (obj->flags & UNINTERESTING)
                return;
@@ -63,17 +64,22 @@ void mark_tree_uninteresting(struct tree *tree)
                return;
        if (parse_tree(tree) < 0)
                die("bad tree %s", sha1_to_hex(obj->sha1));
-       entry = tree->entries;
-       tree->entries = NULL;
-       while (entry) {
-               struct tree_entry_list *next = entry->next;
-               if (entry->directory)
-                       mark_tree_uninteresting(entry->item.tree);
+
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+       while (tree_entry(&desc, &entry)) {
+               if (S_ISDIR(entry.mode))
+                       mark_tree_uninteresting(lookup_tree(entry.sha1));
                else
-                       mark_blob_uninteresting(entry->item.blob);
-               free(entry);
-               entry = next;
+                       mark_blob_uninteresting(lookup_blob(entry.sha1));
        }
+
+       /*
+        * We don't care about the tree any more
+        * after it has been marked uninteresting.
+        */
+       free(tree->buffer);
+       tree->buffer = NULL;
 }
 
 void mark_parents_uninteresting(struct commit *commit)
@@ -297,7 +303,7 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
                parse_commit(p);
                switch (rev_compare_tree(revs, p->tree, commit->tree)) {
                case REV_TREE_SAME:
-                       if (p->object.flags & UNINTERESTING) {
+                       if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
                                /* Even if a merge with an uninteresting
                                 * side branch brought the entire change
                                 * we are interested in, we do not want
@@ -477,12 +483,43 @@ static void handle_all(struct rev_info *revs, unsigned flags)
        for_each_ref(handle_one_ref);
 }
 
+static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
+{
+       unsigned char sha1[20];
+       struct object *it;
+       struct commit *commit;
+       struct commit_list *parents;
+
+       if (*arg == '^') {
+               flags ^= UNINTERESTING;
+               arg++;
+       }
+       if (get_sha1(arg, sha1))
+               return 0;
+       while (1) {
+               it = get_reference(revs, arg, sha1, 0);
+               if (strcmp(it->type, tag_type))
+                       break;
+               memcpy(sha1, ((struct tag*)it)->tagged->sha1, 20);
+       }
+       if (strcmp(it->type, commit_type))
+               return 0;
+       commit = (struct commit *)it;
+       for (parents = commit->parents; parents; parents = parents->next) {
+               it = &parents->item->object;
+               it->flags |= flags;
+               add_pending_object(revs, it, arg);
+       }
+       return 1;
+}
+
 void init_revisions(struct rev_info *revs)
 {
        memset(revs, 0, sizeof(*revs));
 
        revs->abbrev = DEFAULT_ABBREV;
        revs->ignore_merges = 1;
+       revs->simplify_history = 1;
        revs->pruning.recursive = 1;
        revs->pruning.add_remove = file_add_remove;
        revs->pruning.change = file_change;
@@ -544,7 +581,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
                                revs->max_count = atoi(arg + 12);
                                continue;
                        }
-                       /* accept -<digit>, like traditilnal "head" */
+                       /* accept -<digit>, like traditional "head" */
                        if ((*arg == '-') && isdigit(arg[1])) {
                                revs->max_count = atoi(arg + 1);
                                continue;
@@ -664,6 +701,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
                        }
                        if (!strcmp(arg, "-c")) {
                                revs->diff = 1;
+                               revs->dense_combined_merges = 0;
                                revs->combine_merges = 1;
                                continue;
                        }
@@ -702,6 +740,14 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
                                revs->abbrev = DEFAULT_ABBREV;
                                continue;
                        }
+                       if (!strncmp(arg, "--abbrev=", 9)) {
+                               revs->abbrev = strtoul(arg + 9, NULL, 10);
+                               if (revs->abbrev < MINIMUM_ABBREV)
+                                       revs->abbrev = MINIMUM_ABBREV;
+                               else if (revs->abbrev > 40)
+                                       revs->abbrev = 40;
+                               continue;
+                       }
                        if (!strcmp(arg, "--abbrev-commit")) {
                                revs->abbrev_commit = 1;
                                continue;
@@ -711,6 +757,10 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
                                revs->full_diff = 1;
                                continue;
                        }
+                       if (!strcmp(arg, "--full-history")) {
+                               revs->simplify_history = 0;
+                               continue;
+                       }
                        opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
                        if (opts > 0) {
                                revs->diff = 1;
@@ -740,37 +790,56 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
                                include = get_reference(revs, next, sha1, flags);
                                if (!exclude || !include)
                                        die("Invalid revision range %s..%s", arg, next);
+
+                               if (!seen_dashdash) {
+                                       *dotdot = '.';
+                                       verify_non_filename(revs->prefix, arg);
+                               }
                                add_pending_object(revs, exclude, this);
                                add_pending_object(revs, include, next);
                                continue;
                        }
                        *dotdot = '.';
                }
+               dotdot = strstr(arg, "^@");
+               if (dotdot && !dotdot[2]) {
+                       *dotdot = 0;
+                       if (add_parents_only(revs, arg, flags))
+                               continue;
+                       *dotdot = '^';
+               }
                local_flags = 0;
                if (*arg == '^') {
                        local_flags = UNINTERESTING;
                        arg++;
                }
-               if (get_sha1(arg, sha1) < 0) {
+               if (get_sha1(arg, sha1)) {
                        int j;
 
                        if (seen_dashdash || local_flags)
                                die("bad revision '%s'", arg);
 
-                       /* If we didn't have a "--", all filenames must exist */
+                       /* If we didn't have a "--":
+                        * (1) all filenames must exist;
+                        * (2) all rev-args must not be interpretable
+                        *     as a valid filename.
+                        * but the latter we have checked in the main loop.
+                        */
                        for (j = i; j < argc; j++)
                                verify_filename(revs->prefix, argv[j]);
 
                        revs->prune_data = get_pathspec(revs->prefix, argv + i);
                        break;
                }
+               if (!seen_dashdash)
+                       verify_non_filename(revs->prefix, arg);
                object = get_reference(revs, arg, sha1, flags ^ local_flags);
                add_pending_object(revs, object, arg);
        }
        if (def && !revs->pending_objects) {
                unsigned char sha1[20];
                struct object *object;
-               if (get_sha1(def, sha1) < 0)
+               if (get_sha1(def, sha1))
                        die("bad default revision '%s'", def);
                object = get_reference(revs, def, sha1, 0);
                add_pending_object(revs, object, def);
index 48d7b4c..4020e25 100644 (file)
@@ -30,6 +30,7 @@ struct rev_info {
                        no_merges:1,
                        no_walk:1,
                        remove_empty_trees:1,
+                       simplify_history:1,
                        lifo:1,
                        topo_order:1,
                        tag_objects:1,
@@ -58,6 +59,10 @@ struct rev_info {
        unsigned int    abbrev;
        enum cmit_fmt   commit_format;
        struct log_info *loginfo;
+       int             nr, total;
+       const char      *mime_boundary;
+       const char      *add_signoff;
+       const char      *extra_headers;
 
        /* special limits */
        int max_count;
diff --git a/rsh.c b/rsh.c
index d665269..07166ad 100644 (file)
--- a/rsh.c
+++ b/rsh.c
@@ -48,6 +48,7 @@ int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
        int sizen;
        int of;
        int i;
+       pid_t pid;
 
        if (!strcmp(url, "-")) {
                *fd_in = 0;
@@ -91,7 +92,10 @@ int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
        if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
                return error("Couldn't create socket");
 
-       if (!fork()) {
+       pid = fork();
+       if (pid < 0)
+               return error("Couldn't fork");
+       if (!pid) {
                const char *ssh, *ssh_basename;
                ssh = getenv("GIT_SSH");
                if (!ssh) ssh = "ssh";
diff --git a/setup.c b/setup.c
index cce9bb8..fe7f884 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -80,11 +80,31 @@ void verify_filename(const char *prefix, const char *arg)
        if (!lstat(name, &st))
                return;
        if (errno == ENOENT)
-               die("ambiguous argument '%s': unknown revision or filename\n"
-                   "Use '--' to separate filenames from revisions", arg);
+               die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
+                   "Use '--' to separate paths from revisions", arg);
        die("'%s': %s", arg, strerror(errno));
 }
 
+/*
+ * Opposite of the above: the command line did not have -- marker
+ * and we parsed the arg as a refname.  It should not be interpretable
+ * as a filename.
+ */
+void verify_non_filename(const char *prefix, const char *arg)
+{
+       const char *name;
+       struct stat st;
+
+       if (*arg == '-')
+               return; /* flag */
+       name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
+       if (!lstat(name, &st))
+               die("ambiguous argument '%s': both revision and filename\n"
+                   "Use '--' to separate filenames from revisions", arg);
+       if (errno != ENOENT)
+               die("'%s': %s", arg, strerror(errno));
+}
+
 const char **get_pathspec(const char *prefix, const char **pathspec)
 {
        const char *entry = *pathspec;
index f2d33af..b4ff233 100644 (file)
@@ -50,29 +50,6 @@ int get_sha1_hex(const char *hex, unsigned char *sha1)
        return 0;
 }
 
-int adjust_shared_perm(const char *path)
-{
-       struct stat st;
-       int mode;
-
-       if (!shared_repository)
-               return 0;
-       if (lstat(path, &st) < 0)
-               return -1;
-       mode = st.st_mode;
-       if (mode & S_IRUSR)
-               mode |= S_IRGRP;
-       if (mode & S_IWUSR)
-               mode |= S_IWGRP;
-       if (mode & S_IXUSR)
-               mode |= S_IXGRP;
-       if (S_ISDIR(mode))
-               mode |= S_ISGID;
-       if (chmod(path, mode) < 0)
-               return -2;
-       return 0;
-}
-
 int safe_create_leading_directories(char *path)
 {
        char *pos = path;
@@ -108,9 +85,10 @@ int safe_create_leading_directories(char *path)
 
 char * sha1_to_hex(const unsigned char *sha1)
 {
-       static char buffer[50];
+       static int bufno;
+       static char hexbuffer[4][50];
        static const char hex[] = "0123456789abcdef";
-       char *buf = buffer;
+       char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
        int i;
 
        for (i = 0; i < 20; i++) {
@@ -216,6 +194,8 @@ char *sha1_pack_index_name(const unsigned char *sha1)
 struct alternate_object_database *alt_odb_list;
 static struct alternate_object_database **alt_odb_tail;
 
+static void read_info_alternates(const char * alternates, int depth);
+
 /*
  * Prepare alternate object database registry.
  *
@@ -231,14 +211,85 @@ static struct alternate_object_database **alt_odb_tail;
  * SHA1, an extra slash for the first level indirection, and the
  * terminating NUL.
  */
-static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
-                                const char *relative_base)
+static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
 {
-       const char *cp, *last;
-       struct alternate_object_database *ent;
+       struct stat st;
        const char *objdir = get_object_directory();
+       struct alternate_object_database *ent;
+       struct alternate_object_database *alt;
+       /* 43 = 40-byte + 2 '/' + terminating NUL */
+       int pfxlen = len;
+       int entlen = pfxlen + 43;
        int base_len = -1;
 
+       if (*entry != '/' && relative_base) {
+               /* Relative alt-odb */
+               if (base_len < 0)
+                       base_len = strlen(relative_base) + 1;
+               entlen += base_len;
+               pfxlen += base_len;
+       }
+       ent = xmalloc(sizeof(*ent) + entlen);
+
+       if (*entry != '/' && relative_base) {
+               memcpy(ent->base, relative_base, base_len - 1);
+               ent->base[base_len - 1] = '/';
+               memcpy(ent->base + base_len, entry, len);
+       }
+       else
+               memcpy(ent->base, entry, pfxlen);
+
+       ent->name = ent->base + pfxlen + 1;
+       ent->base[pfxlen + 3] = '/';
+       ent->base[pfxlen] = ent->base[entlen-1] = 0;
+
+       /* Detect cases where alternate disappeared */
+       if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
+               error("object directory %s does not exist; "
+                     "check .git/objects/info/alternates.",
+                     ent->base);
+               free(ent);
+               return -1;
+       }
+
+       /* Prevent the common mistake of listing the same
+        * thing twice, or object directory itself.
+        */
+       for (alt = alt_odb_list; alt; alt = alt->next) {
+               if (!memcmp(ent->base, alt->base, pfxlen)) {
+                       free(ent);
+                       return -1;
+               }
+       }
+       if (!memcmp(ent->base, objdir, pfxlen)) {
+               free(ent);
+               return -1;
+       }
+
+       /* add the alternate entry */
+       *alt_odb_tail = ent;
+       alt_odb_tail = &(ent->next);
+       ent->next = NULL;
+
+       /* recursively add alternates */
+       read_info_alternates(ent->base, depth + 1);
+
+       ent->base[pfxlen] = '/';
+
+       return 0;
+}
+
+static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
+                                const char *relative_base, int depth)
+{
+       const char *cp, *last;
+
+       if (depth > 5) {
+               error("%s: ignoring alternate object stores, nesting too deep.",
+                               relative_base);
+               return;
+       }
+
        last = alt;
        while (last < ep) {
                cp = last;
@@ -248,60 +299,15 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
                        last = cp + 1;
                        continue;
                }
-               for ( ; cp < ep && *cp != sep; cp++)
-                       ;
+               while (cp < ep && *cp != sep)
+                       cp++;
                if (last != cp) {
-                       struct stat st;
-                       struct alternate_object_database *alt;
-                       /* 43 = 40-byte + 2 '/' + terminating NUL */
-                       int pfxlen = cp - last;
-                       int entlen = pfxlen + 43;
-
-                       if (*last != '/' && relative_base) {
-                               /* Relative alt-odb */
-                               if (base_len < 0)
-                                       base_len = strlen(relative_base) + 1;
-                               entlen += base_len;
-                               pfxlen += base_len;
-                       }
-                       ent = xmalloc(sizeof(*ent) + entlen);
-
-                       if (*last != '/' && relative_base) {
-                               memcpy(ent->base, relative_base, base_len - 1);
-                               ent->base[base_len - 1] = '/';
-                               memcpy(ent->base + base_len,
-                                      last, cp - last);
-                       }
-                       else
-                               memcpy(ent->base, last, pfxlen);
-
-                       ent->name = ent->base + pfxlen + 1;
-                       ent->base[pfxlen + 3] = '/';
-                       ent->base[pfxlen] = ent->base[entlen-1] = 0;
-
-                       /* Detect cases where alternate disappeared */
-                       if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
-                               error("object directory %s does not exist; "
-                                     "check .git/objects/info/alternates.",
-                                     ent->base);
-                               goto bad;
-                       }
-                       ent->base[pfxlen] = '/';
-
-                       /* Prevent the common mistake of listing the same
-                        * thing twice, or object directory itself.
-                        */
-                       for (alt = alt_odb_list; alt; alt = alt->next)
-                               if (!memcmp(ent->base, alt->base, pfxlen))
-                                       goto bad;
-                       if (!memcmp(ent->base, objdir, pfxlen)) {
-                       bad:
-                               free(ent);
-                       }
-                       else {
-                               *alt_odb_tail = ent;
-                               alt_odb_tail = &(ent->next);
-                               ent->next = NULL;
+                       if ((*last != '/') && depth) {
+                               error("%s: ignoring relative alternate object store %s",
+                                               relative_base, last);
+                       } else {
+                               link_alt_odb_entry(last, cp - last,
+                                               relative_base, depth);
                        }
                }
                while (cp < ep && *cp == sep)
@@ -310,23 +316,14 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
        }
 }
 
-void prepare_alt_odb(void)
+static void read_info_alternates(const char * relative_base, int depth)
 {
-       char path[PATH_MAX];
        char *map;
-       int fd;
        struct stat st;
-       char *alt;
-
-       alt = getenv(ALTERNATE_DB_ENVIRONMENT);
-       if (!alt) alt = "";
-
-       if (alt_odb_tail)
-               return;
-       alt_odb_tail = &alt_odb_list;
-       link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL);
+       char path[PATH_MAX];
+       int fd;
 
-       sprintf(path, "%s/info/alternates", get_object_directory());
+       sprintf(path, "%s/info/alternates", relative_base);
        fd = open(path, O_RDONLY);
        if (fd < 0)
                return;
@@ -339,11 +336,26 @@ void prepare_alt_odb(void)
        if (map == MAP_FAILED)
                return;
 
-       link_alt_odb_entries(map, map + st.st_size, '\n',
-                            get_object_directory());
+       link_alt_odb_entries(map, map + st.st_size, '\n', relative_base, depth);
+
        munmap(map, st.st_size);
 }
 
+void prepare_alt_odb(void)
+{
+       char *alt;
+
+       alt = getenv(ALTERNATE_DB_ENVIRONMENT);
+       if (!alt) alt = "";
+
+       if (alt_odb_tail)
+               return;
+       alt_odb_tail = &alt_odb_list;
+       link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL, 0);
+
+       read_info_alternates(get_object_directory(), 0);
+}
+
 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
 {
        char *name = sha1_file_name(sha1);
@@ -582,6 +594,12 @@ static void prepare_packed_git_one(char *objdir, int local)
 
                /* we have .idx.  Is it a file we can map? */
                strcpy(path + len, de->d_name);
+               for (p = packed_git; p; p = p->next) {
+                       if (!memcmp(path, p->pack_name, len + namelen - 4))
+                               break;
+               }
+               if (p)
+                       continue;
                p = add_packed_git(path, len + namelen, local);
                if (!p)
                        continue;
@@ -591,12 +609,12 @@ static void prepare_packed_git_one(char *objdir, int local)
        closedir(dir);
 }
 
+static int prepare_packed_git_run_once = 0;
 void prepare_packed_git(void)
 {
-       static int run_once = 0;
        struct alternate_object_database *alt;
 
-       if (run_once)
+       if (prepare_packed_git_run_once)
                return;
        prepare_packed_git_one(get_object_directory(), 1);
        prepare_alt_odb();
@@ -605,7 +623,13 @@ void prepare_packed_git(void)
                prepare_packed_git_one(alt->base, 0);
                alt->name[-1] = '/';
        }
-       run_once = 1;
+       prepare_packed_git_run_once = 1;
+}
+
+static void reprepare_packed_git(void)
+{
+       prepare_packed_git_run_once = 0;
+       prepare_packed_git();
 }
 
 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
@@ -1126,7 +1150,7 @@ int find_pack_entry_one(const unsigned char *sha1,
                int mi = (lo + hi) / 2;
                int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
                if (!cmp) {
-                       e->offset = ntohl(*((int*)(index + 24 * mi)));
+                       e->offset = ntohl(*((unsigned int *)(index + 24 * mi)));
                        memcpy(e->sha1, sha1, 20);
                        e->p = p;
                        return 1;
@@ -1177,9 +1201,12 @@ int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep
        if (!map) {
                struct pack_entry e;
 
-               if (!find_pack_entry(sha1, &e))
-                       return error("unable to find %s", sha1_to_hex(sha1));
-               return packed_object_info(&e, type, sizep);
+               if (find_pack_entry(sha1, &e))
+                       return packed_object_info(&e, type, sizep);
+               reprepare_packed_git();
+               if (find_pack_entry(sha1, &e))
+                       return packed_object_info(&e, type, sizep);
+               return error("unable to find %s", sha1_to_hex(sha1));
        }
        if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
                status = error("unable to unpack %s header",
@@ -1221,6 +1248,9 @@ void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size
                munmap(map, mapsize);
                return buf;
        }
+       reprepare_packed_git();
+       if (find_pack_entry(sha1, &e))
+               return read_packed_sha1(sha1, type, size);
        return NULL;
 }
 
@@ -1364,6 +1394,25 @@ int move_temp_to_file(const char *tmpfile, char *filename)
        return 0;
 }
 
+static int write_buffer(int fd, const void *buf, size_t len)
+{
+       while (len) {
+               ssize_t size;
+
+               size = write(fd, buf, len);
+               if (!size)
+                       return error("file write: disk full");
+               if (size < 0) {
+                       if (errno == EINTR || errno == EAGAIN)
+                               continue;
+                       return error("file write error (%s)", strerror(errno));
+               }
+               len -= size;
+               buf += size;
+       }
+       return 0;
+}
+
 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
 {
        int size;
@@ -1430,8 +1479,8 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
        deflateEnd(&stream);
        size = stream.total_out;
 
-       if (write(fd, compressed, size) != size)
-               die("unable to write file");
+       if (write_buffer(fd, compressed, size) < 0)
+               die("unable to write sha1 file");
        fchmod(fd, 0444);
        close(fd);
        free(compressed);
@@ -1439,73 +1488,70 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
        return move_temp_to_file(tmpfile, filename);
 }
 
-int write_sha1_to_fd(int fd, const unsigned char *sha1)
+/*
+ * We need to unpack and recompress the object for writing
+ * it out to a different file.
+ */
+static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
 {
-       ssize_t size;
-       unsigned long objsize;
-       int posn = 0;
-       void *map = map_sha1_file_internal(sha1, &objsize);
-       void *buf = map;
-       void *temp_obj = NULL;
+       size_t size;
        z_stream stream;
+       unsigned char *unpacked;
+       unsigned long len;
+       char type[20];
+       char hdr[50];
+       int hdrlen;
+       void *buf;
 
-       if (!buf) {
-               unsigned char *unpacked;
-               unsigned long len;
-               char type[20];
-               char hdr[50];
-               int hdrlen;
-               // need to unpack and recompress it by itself
-               unpacked = read_packed_sha1(sha1, type, &len);
+       // need to unpack and recompress it by itself
+       unpacked = read_packed_sha1(sha1, type, &len);
 
-               hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
+       hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
 
-               /* Set it up */
-               memset(&stream, 0, sizeof(stream));
-               deflateInit(&stream, Z_BEST_COMPRESSION);
-               size = deflateBound(&stream, len + hdrlen);
-               temp_obj = buf = xmalloc(size);
-
-               /* Compress it */
-               stream.next_out = buf;
-               stream.avail_out = size;
-               
-               /* First header.. */
-               stream.next_in = (void *)hdr;
-               stream.avail_in = hdrlen;
-               while (deflate(&stream, 0) == Z_OK)
-                       /* nothing */;
+       /* Set it up */
+       memset(&stream, 0, sizeof(stream));
+       deflateInit(&stream, Z_BEST_COMPRESSION);
+       size = deflateBound(&stream, len + hdrlen);
+       buf = xmalloc(size);
 
-               /* Then the data itself.. */
-               stream.next_in = unpacked;
-               stream.avail_in = len;
-               while (deflate(&stream, Z_FINISH) == Z_OK)
-                       /* nothing */;
-               deflateEnd(&stream);
-               free(unpacked);
-               
-               objsize = stream.total_out;
-       }
+       /* Compress it */
+       stream.next_out = buf;
+       stream.avail_out = size;
 
-       do {
-               size = write(fd, buf + posn, objsize - posn);
-               if (size <= 0) {
-                       if (!size) {
-                               fprintf(stderr, "write closed\n");
-                       } else {
-                               perror("write ");
-                       }
-                       return -1;
-               }
-               posn += size;
-       } while (posn < objsize);
+       /* First header.. */
+       stream.next_in = (void *)hdr;
+       stream.avail_in = hdrlen;
+       while (deflate(&stream, 0) == Z_OK)
+               /* nothing */;
+
+       /* Then the data itself.. */
+       stream.next_in = unpacked;
+       stream.avail_in = len;
+       while (deflate(&stream, Z_FINISH) == Z_OK)
+               /* nothing */;
+       deflateEnd(&stream);
+       free(unpacked);
 
-       if (map)
-               munmap(map, objsize);
-       if (temp_obj)
-               free(temp_obj);
+       *objsize = stream.total_out;
+       return buf;
+}
 
-       return 0;
+int write_sha1_to_fd(int fd, const unsigned char *sha1)
+{
+       int retval;
+       unsigned long objsize;
+       void *buf = map_sha1_file_internal(sha1, &objsize);
+
+       if (buf) {
+               retval = write_buffer(fd, buf, objsize);
+               munmap(buf, objsize);
+               return retval;
+       }
+
+       buf = repack_object(sha1, &objsize);
+       retval = write_buffer(fd, buf, objsize);
+       free(buf);
+       return retval;
 }
 
 int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
@@ -1544,7 +1590,8 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
                                SHA1_Update(&c, discard, sizeof(discard) -
                                            stream.avail_out);
                        } while (stream.avail_in && ret == Z_OK);
-                       write(local, buffer, *bufposn - stream.avail_in);
+                       if (write_buffer(local, buffer, *bufposn - stream.avail_in) < 0)
+                               die("unable to write sha1 file");
                        memmove(buffer, buffer + *bufposn - stream.avail_in,
                                stream.avail_in);
                        *bufposn = stream.avail_in;
@@ -1610,16 +1657,24 @@ int has_sha1_file(const unsigned char *sha1)
        return find_sha1_file(sha1, &st) ? 1 : 0;
 }
 
-int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
+/*
+ * reads from fd as long as possible into a supplied buffer of size bytes.
+ * If neccessary the buffer's size is increased using realloc()
+ *
+ * returns 0 if anything went fine and -1 otherwise
+ *
+ * NOTE: both buf and size may change, but even when -1 is returned
+ * you still have to free() it yourself.
+ */
+int read_pipe(int fd, char** return_buf, unsigned long* return_size)
 {
-       unsigned long size = 4096;
-       char *buf = malloc(size);
-       int iret, ret;
+       char* buf = *return_buf;
+       unsigned long size = *return_size;
+       int iret;
        unsigned long off = 0;
-       unsigned char hdr[50];
-       int hdrlen;
+
        do {
-               iret = read(fd, buf + off, size - off);
+               iret = xread(fd, buf + off, size - off);
                if (iret > 0) {
                        off += iret;
                        if (off == size) {
@@ -1628,16 +1683,34 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
                        }
                }
        } while (iret > 0);
-       if (iret < 0) {
+
+       *return_buf = buf;
+       *return_size = off;
+
+       if (iret < 0)
+               return -1;
+       return 0;
+}
+
+int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
+{
+       unsigned long size = 4096;
+       char *buf = malloc(size);
+       int ret;
+       unsigned char hdr[50];
+       int hdrlen;
+
+       if (read_pipe(fd, &buf, &size)) {
                free(buf);
                return -1;
        }
+
        if (!type)
                type = blob_type;
        if (write_object)
-               ret = write_sha1_file(buf, off, type, sha1);
+               ret = write_sha1_file(buf, size, type, sha1);
        else {
-               write_sha1_file_prepare(buf, off, type, sha1, hdr, &hdrlen);
+               write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
                ret = 0;
        }
        free(buf);
index 345935b..8fe9b7a 100644 (file)
@@ -4,6 +4,7 @@
 #include "tree.h"
 #include "blob.h"
 #include "tree-walk.h"
+#include "refs.h"
 
 static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
 {
@@ -245,36 +246,60 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
                "refs/remotes/%.*s/HEAD",
                NULL
        };
-       const char **p;
-       const char *warning = "warning: refname '%.*s' is ambiguous.\n";
-       char *pathname;
-       int already_found = 0;
+       static const char *warning = "warning: refname '%.*s' is ambiguous.\n";
+       const char **p, *pathname;
+       char *real_path = NULL;
+       int refs_found = 0, am;
+       unsigned long at_time = (unsigned long)-1;
        unsigned char *this_result;
        unsigned char sha1_from_ref[20];
 
        if (len == 40 && !get_sha1_hex(str, sha1))
                return 0;
 
+       /* At a given period of time? "@{2 hours ago}" */
+       for (am = 1; am < len - 1; am++) {
+               if (str[am] == '@' && str[am+1] == '{' && str[len-1] == '}') {
+                       int date_len = len - am - 3;
+                       char *date_spec = xmalloc(date_len + 1);
+                       safe_strncpy(date_spec, str + am + 2, date_len + 1);
+                       at_time = approxidate(date_spec);
+                       free(date_spec);
+                       len = am;
+                       break;
+               }
+       }
+
        /* Accept only unambiguous ref paths. */
        if (ambiguous_path(str, len))
                return -1;
 
        for (p = fmt; *p; p++) {
-               this_result = already_found ? sha1_from_ref : sha1;
-               pathname = git_path(*p, len, str);
-               if (!read_ref(pathname, this_result)) {
-                       if (warn_ambiguous_refs) {
-                               if (already_found)
-                                       fprintf(stderr, warning, len, str);
-                               already_found++;
-                       }
-                       else
-                               return 0;
+               this_result = refs_found ? sha1_from_ref : sha1;
+               pathname = resolve_ref(git_path(*p, len, str), this_result, 1);
+               if (pathname) {
+                       if (!refs_found++)
+                               real_path = strdup(pathname);
+                       if (!warn_ambiguous_refs)
+                               break;
                }
        }
-       if (already_found)
-               return 0;
-       return -1;
+
+       if (!refs_found)
+               return -1;
+
+       if (warn_ambiguous_refs && refs_found > 1)
+               fprintf(stderr, warning, len, str);
+
+       if (at_time != (unsigned long)-1) {
+               read_ref_at(
+                       real_path + strlen(git_path(".")) - 1,
+                       at_time,
+                       sha1);
+       }
+
+       free(real_path);
+       return 0;
 }
 
 static int get_sha1_1(const char *name, int len, unsigned char *sha1);
@@ -456,19 +481,65 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
  */
 int get_sha1(const char *name, unsigned char *sha1)
 {
-       int ret;
+       int ret, bracket_depth;
        unsigned unused;
+       int namelen = strlen(name);
+       const char *cp;
 
        prepare_alt_odb();
-       ret = get_sha1_1(name, strlen(name), sha1);
-       if (ret < 0) {
-               const char *cp = strchr(name, ':');
-               if (cp) {
-                       unsigned char tree_sha1[20];
-                       if (!get_sha1_1(name, cp-name, tree_sha1))
-                               return get_tree_entry(tree_sha1, cp+1, sha1,
-                                                     &unused);
+       ret = get_sha1_1(name, namelen, sha1);
+       if (!ret)
+               return ret;
+       /* sha1:path --> object name of path in ent sha1
+        * :path -> object name of path in index
+        * :[0-3]:path -> object name of path in index at stage
+        */
+       if (name[0] == ':') {
+               int stage = 0;
+               struct cache_entry *ce;
+               int pos;
+               if (namelen < 3 ||
+                   name[2] != ':' ||
+                   name[1] < '0' || '3' < name[1])
+                       cp = name + 1;
+               else {
+                       stage = name[1] - '0';
+                       cp = name + 3;
                }
+               namelen = namelen - (cp - name);
+               if (!active_cache)
+                       read_cache();
+               if (active_nr < 0)
+                       return -1;
+               pos = cache_name_pos(cp, namelen);
+               if (pos < 0)
+                       pos = -pos - 1;
+               while (pos < active_nr) {
+                       ce = active_cache[pos];
+                       if (ce_namelen(ce) != namelen ||
+                           memcmp(ce->name, cp, namelen))
+                               break;
+                       if (ce_stage(ce) == stage) {
+                               memcpy(sha1, ce->sha1, 20);
+                               return 0;
+                       }
+                       pos++;
+               }
+               return -1;
+       }
+       for (cp = name, bracket_depth = 0; *cp; cp++) {
+               if (*cp == '{')
+                       bracket_depth++;
+               else if (bracket_depth && *cp == '}')
+                       bracket_depth--;
+               else if (!bracket_depth && *cp == ':')
+                       break;
+       }
+       if (*cp == ':') {
+               unsigned char tree_sha1[20];
+               if (!get_sha1_1(name, cp-name, tree_sha1))
+                       return get_tree_entry(tree_sha1, cp+1, sha1,
+                                             &unused);
        }
        return ret;
 }
diff --git a/show-branch.c b/show-branch.c
deleted file mode 100644 (file)
index 24efb65..0000000
+++ /dev/null
@@ -1,761 +0,0 @@
-#include <stdlib.h>
-#include <fnmatch.h>
-#include "cache.h"
-#include "commit.h"
-#include "refs.h"
-
-static const char show_branch_usage[] =
-"git-show-branch [--current] [--all] [--heads] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...]";
-
-static int default_num = 0;
-static int default_alloc = 0;
-static char **default_arg = NULL;
-
-#define UNINTERESTING  01
-
-#define REV_SHIFT       2
-#define MAX_REVS       29 /* should not exceed bits_per_int - REV_SHIFT */
-
-static struct commit *interesting(struct commit_list *list)
-{
-       while (list) {
-               struct commit *commit = list->item;
-               list = list->next;
-               if (commit->object.flags & UNINTERESTING)
-                       continue;
-               return commit;
-       }
-       return NULL;
-}
-
-static struct commit *pop_one_commit(struct commit_list **list_p)
-{
-       struct commit *commit;
-       struct commit_list *list;
-       list = *list_p;
-       commit = list->item;
-       *list_p = list->next;
-       free(list);
-       return commit;
-}
-
-struct commit_name {
-       const char *head_name; /* which head's ancestor? */
-       int generation; /* how many parents away from head_name */
-};
-
-/* Name the commit as nth generation ancestor of head_name;
- * we count only the first-parent relationship for naming purposes.
- */
-static void name_commit(struct commit *commit, const char *head_name, int nth)
-{
-       struct commit_name *name;
-       if (!commit->object.util)
-               commit->object.util = xmalloc(sizeof(struct commit_name));
-       name = commit->object.util;
-       name->head_name = head_name;
-       name->generation = nth;
-}
-
-/* Parent is the first parent of the commit.  We may name it
- * as (n+1)th generation ancestor of the same head_name as
- * commit is nth generation ancestor of, if that generation
- * number is better than the name it already has.
- */
-static void name_parent(struct commit *commit, struct commit *parent)
-{
-       struct commit_name *commit_name = commit->object.util;
-       struct commit_name *parent_name = parent->object.util;
-       if (!commit_name)
-               return;
-       if (!parent_name ||
-           commit_name->generation + 1 < parent_name->generation)
-               name_commit(parent, commit_name->head_name,
-                           commit_name->generation + 1);
-}
-
-static int name_first_parent_chain(struct commit *c)
-{
-       int i = 0;
-       while (c) {
-               struct commit *p;
-               if (!c->object.util)
-                       break;
-               if (!c->parents)
-                       break;
-               p = c->parents->item;
-               if (!p->object.util) {
-                       name_parent(c, p);
-                       i++;
-               }
-               c = p;
-       }
-       return i;
-}
-
-static void name_commits(struct commit_list *list,
-                        struct commit **rev,
-                        char **ref_name,
-                        int num_rev)
-{
-       struct commit_list *cl;
-       struct commit *c;
-       int i;
-
-       /* First give names to the given heads */
-       for (cl = list; cl; cl = cl->next) {
-               c = cl->item;
-               if (c->object.util)
-                       continue;
-               for (i = 0; i < num_rev; i++) {
-                       if (rev[i] == c) {
-                               name_commit(c, ref_name[i], 0);
-                               break;
-                       }
-               }
-       }
-
-       /* Then commits on the first parent ancestry chain */
-       do {
-               i = 0;
-               for (cl = list; cl; cl = cl->next) {
-                       i += name_first_parent_chain(cl->item);
-               }
-       } while (i);
-
-       /* Finally, any unnamed commits */
-       do {
-               i = 0;
-               for (cl = list; cl; cl = cl->next) {
-                       struct commit_list *parents;
-                       struct commit_name *n;
-                       int nth;
-                       c = cl->item;
-                       if (!c->object.util)
-                               continue;
-                       n = c->object.util;
-                       parents = c->parents;
-                       nth = 0;
-                       while (parents) {
-                               struct commit *p = parents->item;
-                               char newname[1000], *en;
-                               parents = parents->next;
-                               nth++;
-                               if (p->object.util)
-                                       continue;
-                               en = newname;
-                               switch (n->generation) {
-                               case 0:
-                                       en += sprintf(en, "%s", n->head_name);
-                                       break;
-                               case 1:
-                                       en += sprintf(en, "%s^", n->head_name);
-                                       break;
-                               default:
-                                       en += sprintf(en, "%s~%d",
-                                               n->head_name, n->generation);
-                                       break;
-                               }
-                               if (nth == 1)
-                                       en += sprintf(en, "^");
-                               else
-                                       en += sprintf(en, "^%d", nth);
-                               name_commit(p, strdup(newname), 0);
-                               i++;
-                               name_first_parent_chain(p);
-                       }
-               }
-       } while (i);
-}
-
-static int mark_seen(struct commit *commit, struct commit_list **seen_p)
-{
-       if (!commit->object.flags) {
-               insert_by_date(commit, seen_p);
-               return 1;
-       }
-       return 0;
-}
-
-static void join_revs(struct commit_list **list_p,
-                     struct commit_list **seen_p,
-                     int num_rev, int extra)
-{
-       int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
-       int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
-
-       while (*list_p) {
-               struct commit_list *parents;
-               int still_interesting = !!interesting(*list_p);
-               struct commit *commit = pop_one_commit(list_p);
-               int flags = commit->object.flags & all_mask;
-
-               if (!still_interesting && extra <= 0)
-                       break;
-
-               mark_seen(commit, seen_p);
-               if ((flags & all_revs) == all_revs)
-                       flags |= UNINTERESTING;
-               parents = commit->parents;
-
-               while (parents) {
-                       struct commit *p = parents->item;
-                       int this_flag = p->object.flags;
-                       parents = parents->next;
-                       if ((this_flag & flags) == flags)
-                               continue;
-                       if (!p->object.parsed)
-                               parse_commit(p);
-                       if (mark_seen(p, seen_p) && !still_interesting)
-                               extra--;
-                       p->object.flags |= flags;
-                       insert_by_date(p, list_p);
-               }
-       }
-
-       /*
-        * Postprocess to complete well-poisoning.
-        *
-        * At this point we have all the commits we have seen in
-        * seen_p list (which happens to be sorted chronologically but
-        * it does not really matter).  Mark anything that can be
-        * reached from uninteresting commits not interesting.
-        */
-       for (;;) {
-               int changed = 0;
-               struct commit_list *s;
-               for (s = *seen_p; s; s = s->next) {
-                       struct commit *c = s->item;
-                       struct commit_list *parents;
-
-                       if (((c->object.flags & all_revs) != all_revs) &&
-                           !(c->object.flags & UNINTERESTING))
-                               continue;
-
-                       /* The current commit is either a merge base or
-                        * already uninteresting one.  Mark its parents
-                        * as uninteresting commits _only_ if they are
-                        * already parsed.  No reason to find new ones
-                        * here.
-                        */
-                       parents = c->parents;
-                       while (parents) {
-                               struct commit *p = parents->item;
-                               parents = parents->next;
-                               if (!(p->object.flags & UNINTERESTING)) {
-                                       p->object.flags |= UNINTERESTING;
-                                       changed = 1;
-                               }
-                       }
-               }
-               if (!changed)
-                       break;
-       }
-}
-
-static void show_one_commit(struct commit *commit, int no_name)
-{
-       char pretty[256], *cp;
-       struct commit_name *name = commit->object.util;
-       if (commit->object.parsed)
-               pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
-                                   pretty, sizeof(pretty), 0);
-       else
-               strcpy(pretty, "(unavailable)");
-       if (!strncmp(pretty, "[PATCH] ", 8))
-               cp = pretty + 8;
-       else
-               cp = pretty;
-
-       if (!no_name) {
-               if (name && name->head_name) {
-                       printf("[%s", name->head_name);
-                       if (name->generation) {
-                               if (name->generation == 1)
-                                       printf("^");
-                               else
-                                       printf("~%d", name->generation);
-                       }
-                       printf("] ");
-               }
-               else
-                       printf("[%s] ",
-                              find_unique_abbrev(commit->object.sha1, 7));
-       }
-       puts(cp);
-}
-
-static char *ref_name[MAX_REVS + 1];
-static int ref_name_cnt;
-
-static const char *find_digit_prefix(const char *s, int *v)
-{
-       const char *p;
-       int ver;
-       char ch;
-
-       for (p = s, ver = 0;
-            '0' <= (ch = *p) && ch <= '9';
-            p++)
-               ver = ver * 10 + ch - '0';
-       *v = ver;
-       return p;
-}
-
-
-static int version_cmp(const char *a, const char *b)
-{
-       while (1) {
-               int va, vb;
-
-               a = find_digit_prefix(a, &va);
-               b = find_digit_prefix(b, &vb);
-               if (va != vb)
-                       return va - vb;
-
-               while (1) {
-                       int ca = *a;
-                       int cb = *b;
-                       if ('0' <= ca && ca <= '9')
-                               ca = 0;
-                       if ('0' <= cb && cb <= '9')
-                               cb = 0;
-                       if (ca != cb)
-                               return ca - cb;
-                       if (!ca)
-                               break;
-                       a++;
-                       b++;
-               }
-               if (!*a && !*b)
-                       return 0;
-       }
-}
-
-static int compare_ref_name(const void *a_, const void *b_)
-{
-       const char * const*a = a_, * const*b = b_;
-       return version_cmp(*a, *b);
-}
-
-static void sort_ref_range(int bottom, int top)
-{
-       qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
-             compare_ref_name);
-}
-
-static int append_ref(const char *refname, const unsigned char *sha1)
-{
-       struct commit *commit = lookup_commit_reference_gently(sha1, 1);
-       int i;
-
-       if (!commit)
-               return 0;
-       /* Avoid adding the same thing twice */
-       for (i = 0; i < ref_name_cnt; i++)
-               if (!strcmp(refname, ref_name[i]))
-                       return 0;
-
-       if (MAX_REVS <= ref_name_cnt) {
-               fprintf(stderr, "warning: ignoring %s; "
-                       "cannot handle more than %d refs\n",
-                       refname, MAX_REVS);
-               return 0;
-       }
-       ref_name[ref_name_cnt++] = strdup(refname);
-       ref_name[ref_name_cnt] = NULL;
-       return 0;
-}
-
-static int append_head_ref(const char *refname, const unsigned char *sha1)
-{
-       unsigned char tmp[20];
-       int ofs = 11;
-       if (strncmp(refname, "refs/heads/", ofs))
-               return 0;
-       /* If both heads/foo and tags/foo exists, get_sha1 would
-        * get confused.
-        */
-       if (get_sha1(refname + ofs, tmp) || memcmp(tmp, sha1, 20))
-               ofs = 5;
-       return append_ref(refname + ofs, sha1);
-}
-
-static int append_tag_ref(const char *refname, const unsigned char *sha1)
-{
-       if (strncmp(refname, "refs/tags/", 10))
-               return 0;
-       return append_ref(refname + 5, sha1);
-}
-
-static const char *match_ref_pattern = NULL;
-static int match_ref_slash = 0;
-static int count_slash(const char *s)
-{
-       int cnt = 0;
-       while (*s)
-               if (*s++ == '/')
-                       cnt++;
-       return cnt;
-}
-
-static int append_matching_ref(const char *refname, const unsigned char *sha1)
-{
-       /* we want to allow pattern hold/<asterisk> to show all
-        * branches under refs/heads/hold/, and v0.99.9? to show
-        * refs/tags/v0.99.9a and friends.
-        */
-       const char *tail;
-       int slash = count_slash(refname);
-       for (tail = refname; *tail && match_ref_slash < slash; )
-               if (*tail++ == '/')
-                       slash--;
-       if (!*tail)
-               return 0;
-       if (fnmatch(match_ref_pattern, tail, 0))
-               return 0;
-       if (!strncmp("refs/heads/", refname, 11))
-               return append_head_ref(refname, sha1);
-       if (!strncmp("refs/tags/", refname, 10))
-               return append_tag_ref(refname, sha1);
-       return append_ref(refname, sha1);
-}
-
-static void snarf_refs(int head, int tag)
-{
-       if (head) {
-               int orig_cnt = ref_name_cnt;
-               for_each_ref(append_head_ref);
-               sort_ref_range(orig_cnt, ref_name_cnt);
-       }
-       if (tag) {
-               int orig_cnt = ref_name_cnt;
-               for_each_ref(append_tag_ref);
-               sort_ref_range(orig_cnt, ref_name_cnt);
-       }
-}
-
-static int rev_is_head(char *head_path, int headlen, char *name,
-                      unsigned char *head_sha1, unsigned char *sha1)
-{
-       int namelen;
-       if ((!head_path[0]) ||
-           (head_sha1 && sha1 && memcmp(head_sha1, sha1, 20)))
-               return 0;
-       namelen = strlen(name);
-       if ((headlen < namelen) ||
-           memcmp(head_path + headlen - namelen, name, namelen))
-               return 0;
-       if (headlen == namelen ||
-           head_path[headlen - namelen - 1] == '/')
-               return 1;
-       return 0;
-}
-
-static int show_merge_base(struct commit_list *seen, int num_rev)
-{
-       int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
-       int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
-       int exit_status = 1;
-
-       while (seen) {
-               struct commit *commit = pop_one_commit(&seen);
-               int flags = commit->object.flags & all_mask;
-               if (!(flags & UNINTERESTING) &&
-                   ((flags & all_revs) == all_revs)) {
-                       puts(sha1_to_hex(commit->object.sha1));
-                       exit_status = 0;
-                       commit->object.flags |= UNINTERESTING;
-               }
-       }
-       return exit_status;
-}
-
-static int show_independent(struct commit **rev,
-                           int num_rev,
-                           char **ref_name,
-                           unsigned int *rev_mask)
-{
-       int i;
-
-       for (i = 0; i < num_rev; i++) {
-               struct commit *commit = rev[i];
-               unsigned int flag = rev_mask[i];
-
-               if (commit->object.flags == flag)
-                       puts(sha1_to_hex(commit->object.sha1));
-               commit->object.flags |= UNINTERESTING;
-       }
-       return 0;
-}
-
-static void append_one_rev(const char *av)
-{
-       unsigned char revkey[20];
-       if (!get_sha1(av, revkey)) {
-               append_ref(av, revkey);
-               return;
-       }
-       if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
-               /* glob style match */
-               int saved_matches = ref_name_cnt;
-               match_ref_pattern = av;
-               match_ref_slash = count_slash(av);
-               for_each_ref(append_matching_ref);
-               if (saved_matches == ref_name_cnt &&
-                   ref_name_cnt < MAX_REVS)
-                       error("no matching refs with %s", av);
-               if (saved_matches + 1 < ref_name_cnt)
-                       sort_ref_range(saved_matches, ref_name_cnt);
-               return;
-       }
-       die("bad sha1 reference %s", av);
-}
-
-static int git_show_branch_config(const char *var, const char *value)
-{
-       if (!strcmp(var, "showbranch.default")) {
-               if (default_alloc <= default_num + 1) {
-                       default_alloc = default_alloc * 3 / 2 + 20;
-                       default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc);
-               }
-               default_arg[default_num++] = strdup(value);
-               default_arg[default_num] = NULL;
-               return 0;
-       }
-
-       return git_default_config(var, value);
-}
-
-int main(int ac, char **av)
-{
-       struct commit *rev[MAX_REVS], *commit;
-       struct commit_list *list = NULL, *seen = NULL;
-       unsigned int rev_mask[MAX_REVS];
-       int num_rev, i, extra = 0;
-       int all_heads = 0, all_tags = 0;
-       int all_mask, all_revs;
-       int lifo = 1;
-       char head_path[128];
-       const char *head_path_p;
-       int head_path_len;
-       unsigned char head_sha1[20];
-       int merge_base = 0;
-       int independent = 0;
-       int no_name = 0;
-       int sha1_name = 0;
-       int shown_merge_point = 0;
-       int with_current_branch = 0;
-       int head_at = -1;
-       int topics = 0;
-
-       setup_git_directory();
-       git_config(git_show_branch_config);
-
-       /* If nothing is specified, try the default first */
-       if (ac == 1 && default_num) {
-               ac = default_num + 1;
-               av = default_arg - 1; /* ick; we would not address av[0] */
-       }
-
-       while (1 < ac && av[1][0] == '-') {
-               char *arg = av[1];
-               if (!strcmp(arg, "--")) {
-                       ac--; av++;
-                       break;
-               }
-               else if (!strcmp(arg, "--all"))
-                       all_heads = all_tags = 1;
-               else if (!strcmp(arg, "--heads"))
-                       all_heads = 1;
-               else if (!strcmp(arg, "--tags"))
-                       all_tags = 1;
-               else if (!strcmp(arg, "--more"))
-                       extra = 1;
-               else if (!strcmp(arg, "--list"))
-                       extra = -1;
-               else if (!strcmp(arg, "--no-name"))
-                       no_name = 1;
-               else if (!strcmp(arg, "--current"))
-                       with_current_branch = 1;
-               else if (!strcmp(arg, "--sha1-name"))
-                       sha1_name = 1;
-               else if (!strncmp(arg, "--more=", 7))
-                       extra = atoi(arg + 7);
-               else if (!strcmp(arg, "--merge-base"))
-                       merge_base = 1;
-               else if (!strcmp(arg, "--independent"))
-                       independent = 1;
-               else if (!strcmp(arg, "--topo-order"))
-                       lifo = 1;
-               else if (!strcmp(arg, "--topics"))
-                       topics = 1;
-               else if (!strcmp(arg, "--date-order"))
-                       lifo = 0;
-               else
-                       usage(show_branch_usage);
-               ac--; av++;
-       }
-       ac--; av++;
-
-       /* Only one of these is allowed */
-       if (1 < independent + merge_base + (extra != 0))
-               usage(show_branch_usage);
-
-       /* If nothing is specified, show all branches by default */
-       if (ac + all_heads + all_tags == 0)
-               all_heads = 1;
-
-       if (all_heads + all_tags)
-               snarf_refs(all_heads, all_tags);
-       while (0 < ac) {
-               append_one_rev(*av);
-               ac--; av++;
-       }
-
-       head_path_p = resolve_ref(git_path("HEAD"), head_sha1, 1);
-       if (head_path_p) {
-               head_path_len = strlen(head_path_p);
-               memcpy(head_path, head_path_p, head_path_len + 1);
-       }
-       else {
-               head_path_len = 0;
-               head_path[0] = 0;
-       }
-
-       if (with_current_branch && head_path_p) {
-               int has_head = 0;
-               for (i = 0; !has_head && i < ref_name_cnt; i++) {
-                       /* We are only interested in adding the branch
-                        * HEAD points at.
-                        */
-                       if (rev_is_head(head_path,
-                                       head_path_len,
-                                       ref_name[i],
-                                       head_sha1, NULL))
-                               has_head++;
-               }
-               if (!has_head) {
-                       int pfxlen = strlen(git_path("refs/heads/"));
-                       append_one_rev(head_path + pfxlen);
-               }
-       }
-
-       if (!ref_name_cnt) {
-               fprintf(stderr, "No revs to be shown.\n");
-               exit(0);
-       }
-
-       for (num_rev = 0; ref_name[num_rev]; num_rev++) {
-               unsigned char revkey[20];
-               unsigned int flag = 1u << (num_rev + REV_SHIFT);
-
-               if (MAX_REVS <= num_rev)
-                       die("cannot handle more than %d revs.", MAX_REVS);
-               if (get_sha1(ref_name[num_rev], revkey))
-                       die("'%s' is not a valid ref.", ref_name[num_rev]);
-               commit = lookup_commit_reference(revkey);
-               if (!commit)
-                       die("cannot find commit %s (%s)",
-                           ref_name[num_rev], revkey);
-               parse_commit(commit);
-               mark_seen(commit, &seen);
-
-               /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
-                * and so on.  REV_SHIFT bits from bit 0 are used for
-                * internal bookkeeping.
-                */
-               commit->object.flags |= flag;
-               if (commit->object.flags == flag)
-                       insert_by_date(commit, &list);
-               rev[num_rev] = commit;
-       }
-       for (i = 0; i < num_rev; i++)
-               rev_mask[i] = rev[i]->object.flags;
-
-       if (0 <= extra)
-               join_revs(&list, &seen, num_rev, extra);
-
-       if (merge_base)
-               return show_merge_base(seen, num_rev);
-
-       if (independent)
-               return show_independent(rev, num_rev, ref_name, rev_mask);
-
-       /* Show list; --more=-1 means list-only */
-       if (1 < num_rev || extra < 0) {
-               for (i = 0; i < num_rev; i++) {
-                       int j;
-                       int is_head = rev_is_head(head_path,
-                                                 head_path_len,
-                                                 ref_name[i],
-                                                 head_sha1,
-                                                 rev[i]->object.sha1);
-                       if (extra < 0)
-                               printf("%c [%s] ",
-                                      is_head ? '*' : ' ', ref_name[i]);
-                       else {
-                               for (j = 0; j < i; j++)
-                                       putchar(' ');
-                               printf("%c [%s] ",
-                                      is_head ? '*' : '!', ref_name[i]);
-                       }
-                       /* header lines never need name */
-                       show_one_commit(rev[i], 1);
-                       if (is_head)
-                               head_at = i;
-               }
-               if (0 <= extra) {
-                       for (i = 0; i < num_rev; i++)
-                               putchar('-');
-                       putchar('\n');
-               }
-       }
-       if (extra < 0)
-               exit(0);
-
-       /* Sort topologically */
-       sort_in_topological_order(&seen, lifo);
-
-       /* Give names to commits */
-       if (!sha1_name && !no_name)
-               name_commits(seen, rev, ref_name, num_rev);
-
-       all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
-       all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
-
-       while (seen) {
-               struct commit *commit = pop_one_commit(&seen);
-               int this_flag = commit->object.flags;
-               int is_merge_point = ((this_flag & all_revs) == all_revs);
-
-               shown_merge_point |= is_merge_point;
-
-               if (1 < num_rev) {
-                       int is_merge = !!(commit->parents && commit->parents->next);
-                       if (topics &&
-                           !is_merge_point &&
-                           (this_flag & (1u << REV_SHIFT)))
-                               continue;
-
-                       for (i = 0; i < num_rev; i++) {
-                               int mark;
-                               if (!(this_flag & (1u << (i + REV_SHIFT))))
-                                       mark = ' ';
-                               else if (is_merge)
-                                       mark = '-';
-                               else if (i == head_at)
-                                       mark = '*';
-                               else
-                                       mark = '+';
-                               putchar(mark);
-                       }
-                       putchar(' ');
-               }
-               show_one_commit(commit, no_name);
-
-               if (shown_merge_point && --extra < 0)
-                       break;
-       }
-       return 0;
-}
index 4eb9e04..e3067b8 100644 (file)
@@ -132,6 +132,7 @@ int main(int argc, char **argv)
        if (!prog) prog = "git-ssh-upload";
 
        setup_git_directory();
+       git_config(git_default_config);
 
        while (arg < argc && argv[arg][0] == '-') {
                if (argv[arg][1] == 't') {
@@ -158,6 +159,7 @@ int main(int argc, char **argv)
        }
        commit_id = argv[arg];
        url = argv[arg + 1];
+       write_ref_log_details = url;
 
        if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
                return 1;
index b675a0b..2da6661 100644 (file)
@@ -134,7 +134,7 @@ int main(int argc, char **argv)
        commit_id = argv[arg];
        url = argv[arg + 1];
        if (get_sha1(commit_id, sha1))
-               usage(ssh_push_usage);
+               die("Not a valid object name %s", commit_id);
        memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
        argv[arg] = hex;
 
index 4d175d8..da3c813 100755 (executable)
@@ -39,7 +39,6 @@ test_expect_success \
      echo nitfol >nitfol &&
      echo bozbar >bozbar &&
      echo rezrov >rezrov &&
-     echo yomin >yomin &&
      git-update-index --add nitfol bozbar rezrov &&
      treeH=`git-write-tree` &&
      echo treeH $treeH &&
@@ -56,7 +55,8 @@ test_expect_success \
 
 test_expect_success \
     '1, 2, 3 - no carry forward' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >1-3.out &&
      cmp M.out 1-3.out &&
@@ -66,15 +66,16 @@ test_expect_success \
      check_cache_at frotz clean &&
      check_cache_at nitfol clean'
 
-echo '+100644 X 0      yomin' >expected
-
 test_expect_success \
     '4 - carry forward local addition.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
+     echo "+100644 X 0 yomin" >expected &&
+     echo yomin >yomin &&
      git-update-index --add yomin &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >4.out || return 1
-     diff --unified=0 M.out 4.out >4diff.out
+     diff -U0 M.out 4.out >4diff.out
      compare_change 4diff.out expected &&
      check_cache_at yomin clean &&
      sum bozbar frotz nitfol >actual4.sum &&
@@ -85,13 +86,15 @@ test_expect_success \
 
 test_expect_success \
     '5 - carry forward local addition.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
+     git-read-tree -m -u $treeH &&
      echo yomin >yomin &&
      git-update-index --add yomin &&
      echo yomin yomin >yomin &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >5.out || return 1
-     diff --unified=0 M.out 5.out >5diff.out
+     diff -U0 M.out 5.out >5diff.out
      compare_change 5diff.out expected &&
      check_cache_at yomin dirty &&
      sum bozbar frotz nitfol >actual5.sum &&
@@ -103,11 +106,13 @@ test_expect_success \
 
 test_expect_success \
     '6 - local addition already has the same.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
+     echo frotz >frotz &&
      git-update-index --add frotz &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >6.out &&
-     diff --unified=0 M.out 6.out &&
+     diff -U0 M.out 6.out &&
      check_cache_at frotz clean &&
      sum bozbar frotz nitfol >actual3.sum &&
      cmp M.sum actual3.sum &&
@@ -117,13 +122,14 @@ test_expect_success \
 
 test_expect_success \
     '7 - local addition already has the same.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo frotz >frotz &&
      git-update-index --add frotz &&
      echo frotz frotz >frotz &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >7.out &&
-     diff --unified=0 M.out 7.out &&
+     diff -U0 M.out 7.out &&
      check_cache_at frotz dirty &&
      sum bozbar frotz nitfol >actual7.sum &&
      if cmp M.sum actual7.sum; then false; else :; fi &&
@@ -134,14 +140,16 @@ test_expect_success \
 
 test_expect_success \
     '8 - conflicting addition.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo frotz frotz >frotz &&
      git-update-index --add frotz &&
      if git-read-tree -m -u $treeH $treeM; then false; else :; fi'
 
 test_expect_success \
     '9 - conflicting addition.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo frotz frotz >frotz &&
      git-update-index --add frotz &&
      echo frotz >frotz &&
@@ -149,7 +157,8 @@ test_expect_success \
 
 test_expect_success \
     '10 - path removed.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo rezrov >rezrov &&
      git-update-index --add rezrov &&
      git-read-tree -m -u $treeH $treeM &&
@@ -160,7 +169,8 @@ test_expect_success \
 
 test_expect_success \
     '11 - dirty path removed.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo rezrov >rezrov &&
      git-update-index --add rezrov &&
      echo rezrov rezrov >rezrov &&
@@ -168,14 +178,16 @@ test_expect_success \
 
 test_expect_success \
     '12 - unmatching local changes being removed.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo rezrov rezrov >rezrov &&
      git-update-index --add rezrov &&
      if git-read-tree -m -u $treeH $treeM; then false; else :; fi'
 
 test_expect_success \
     '13 - unmatching local changes being removed.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo rezrov rezrov >rezrov &&
      git-update-index --add rezrov &&
      echo rezrov >rezrov &&
@@ -188,12 +200,13 @@ EOF
 
 test_expect_success \
     '14 - unchanged in two heads.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo nitfol nitfol >nitfol &&
      git-update-index --add nitfol &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >14.out || return 1
-     diff --unified=0 M.out 14.out >14diff.out
+     diff -U0 M.out 14.out >14diff.out
      compare_change 14diff.out expected &&
      sum bozbar frotz >actual14.sum &&
      grep -v nitfol M.sum > expected14.sum &&
@@ -207,13 +220,14 @@ test_expect_success \
 
 test_expect_success \
     '15 - unchanged in two heads.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo nitfol nitfol >nitfol &&
      git-update-index --add nitfol &&
      echo nitfol nitfol nitfol >nitfol &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >15.out || return 1
-     diff --unified=0 M.out 15.out >15diff.out
+     diff -U0 M.out 15.out >15diff.out
      compare_change 15diff.out expected &&
      check_cache_at nitfol dirty &&
      sum bozbar frotz >actual15.sum &&
@@ -227,14 +241,16 @@ test_expect_success \
 
 test_expect_success \
     '16 - conflicting local change.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo bozbar bozbar >bozbar &&
      git-update-index --add bozbar &&
      if git-read-tree -m -u $treeH $treeM; then false; else :; fi'
 
 test_expect_success \
     '17 - conflicting local change.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo bozbar bozbar >bozbar &&
      git-update-index --add bozbar &&
      echo bozbar bozbar bozbar >bozbar &&
@@ -242,25 +258,27 @@ test_expect_success \
 
 test_expect_success \
     '18 - local change already having a good result.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo gnusto >bozbar &&
      git-update-index --add bozbar &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >18.out &&
-     diff --unified=0 M.out 18.out &&
+     diff -U0 M.out 18.out &&
      check_cache_at bozbar clean &&
      sum bozbar frotz nitfol >actual18.sum &&
      cmp M.sum actual18.sum'
 
 test_expect_success \
     '19 - local change already having a good result, further modified.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo gnusto >bozbar &&
      git-update-index --add bozbar &&
      echo gnusto gnusto >bozbar &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >19.out &&
-     diff --unified=0 M.out 19.out &&
+     diff -U0 M.out 19.out &&
      check_cache_at bozbar dirty &&
      sum frotz nitfol >actual19.sum &&
      grep -v bozbar  M.sum > expected19.sum &&
@@ -273,19 +291,21 @@ test_expect_success \
 
 test_expect_success \
     '20 - no local change, use new tree.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo bozbar >bozbar &&
      git-update-index --add bozbar &&
      git-read-tree -m -u $treeH $treeM &&
      git-ls-files --stage >20.out &&
-     diff --unified=0 M.out 20.out &&
+     diff -U0 M.out 20.out &&
      check_cache_at bozbar clean &&
      sum bozbar frotz nitfol >actual20.sum &&
      cmp M.sum actual20.sum'
 
 test_expect_success \
     '21 - no local change, dirty cache.' \
-    'rm -f .git/index &&
+    'rm -f .git/index nitfol bozbar rezrov frotz &&
+     git-read-tree --reset -u $treeH &&
      echo bozbar >bozbar &&
      git-update-index --add bozbar &&
      echo gnusto gnusto >bozbar &&
@@ -294,7 +314,7 @@ test_expect_success \
 # Also make sure we did not break DF vs DF/DF case.
 test_expect_success \
     'DF vs DF/DF case setup.' \
-    'rm -f .git/index &&
+    'rm -f .git/index
      echo DF >DF &&
      git-update-index --add DF &&
      treeDF=`git-write-tree` &&
@@ -318,7 +338,7 @@ test_expect_success \
      git-update-index --add DF &&
      git-read-tree -m -u $treeDF $treeDFDF &&
      git-ls-files --stage >DFDFcheck.out &&
-     diff --unified=0 DFDF.out DFDFcheck.out &&
+     diff -U0 DFDF.out DFDFcheck.out &&
      check_cache_at DF/DF clean'
 
 test_done
index ab4dd5c..8260d57 100755 (executable)
@@ -229,7 +229,7 @@ test_expect_failure 'invalid key' 'git-repo-config inval.2key blabla'
 test_expect_success 'correct key' 'git-repo-config 123456.a123 987'
 
 test_expect_success 'hierarchical section' \
-       'git-repo-config 1.2.3.alpha beta'
+       'git-repo-config Version.1.2.3eX.Alpha beta'
 
 cat > expect << EOF
 [beta] ; silly comment # another comment
@@ -241,12 +241,30 @@ noIndent= sillyValue ; 'nother silly comment
        NoNewLine = wow2 for me
 [123456]
        a123 = 987
-[1.2.3]
-       alpha = beta
+[Version "1.2.3eX"]
+       Alpha = beta
 EOF
 
 test_expect_success 'hierarchical section value' 'cmp .git/config expect'
 
+cat > expect << EOF
+beta.noindent=sillyValue
+nextsection.nonewline=wow2 for me
+123456.a123=987
+version.1.2.3eX.alpha=beta
+EOF
+
+test_expect_success 'working --list' \
+       'git-repo-config --list > output && cmp output expect'
+
+cat > expect << EOF
+beta.noindent sillyValue
+nextsection.nonewline wow2 for me
+EOF
+
+test_expect_success '--get-regexp' \
+       'git-repo-config --get-regexp in > output && cmp output expect'
+
 cat > .git/config << EOF
 [novalue]
        variable
@@ -255,5 +273,41 @@ EOF
 test_expect_success 'get variable with no value' \
        'git-repo-config --get novalue.variable ^$'
 
+git-repo-config > output 2>&1
+
+test_expect_success 'no arguments, but no crash' \
+       "test $? = 129 && grep usage output"
+
+cat > .git/config << EOF
+[a.b]
+       c = d
+EOF
+
+git-repo-config a.x y
+
+cat > expect << EOF
+[a.b]
+       c = d
+[a]
+       x = y
+EOF
+
+test_expect_success 'new section is partial match of another' 'cmp .git/config expect'
+
+git-repo-config b.x y
+git-repo-config a.b c
+
+cat > expect << EOF
+[a.b]
+       c = d
+[a]
+       x = y
+       b = c
+[b]
+       x = y
+EOF
+
+test_expect_success 'new variable inserts into proper section' 'cmp .git/config expect'
+
 test_done
 
diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh
new file mode 100755 (executable)
index 0000000..df3e993
--- /dev/null
@@ -0,0 +1,213 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Shawn Pearce
+#
+
+test_description='Test git-update-ref and basic ref logging'
+. ./test-lib.sh
+
+Z=0000000000000000000000000000000000000000
+A=1111111111111111111111111111111111111111
+B=2222222222222222222222222222222222222222
+C=3333333333333333333333333333333333333333
+D=4444444444444444444444444444444444444444
+E=5555555555555555555555555555555555555555
+F=6666666666666666666666666666666666666666
+m=refs/heads/master
+
+test_expect_success \
+       "create $m" \
+       'git-update-ref $m $A &&
+        test $A = $(cat .git/$m)'
+test_expect_success \
+       "create $m" \
+       'git-update-ref $m $B $A &&
+        test $B = $(cat .git/$m)'
+rm -f .git/$m
+
+test_expect_success \
+       "create $m (by HEAD)" \
+       'git-update-ref HEAD $A &&
+        test $A = $(cat .git/$m)'
+test_expect_success \
+       "create $m (by HEAD)" \
+       'git-update-ref HEAD $B $A &&
+        test $B = $(cat .git/$m)'
+rm -f .git/$m
+
+test_expect_failure \
+       '(not) create HEAD with old sha1' \
+       'git-update-ref HEAD $A $B'
+test_expect_failure \
+       "(not) prior created .git/$m" \
+       'test -f .git/$m'
+rm -f .git/$m
+
+test_expect_success \
+       "create HEAD" \
+       'git-update-ref HEAD $A'
+test_expect_failure \
+       '(not) change HEAD with wrong SHA1' \
+       'git-update-ref HEAD $B $Z'
+test_expect_failure \
+       "(not) changed .git/$m" \
+       'test $B = $(cat .git/$m)'
+rm -f .git/$m
+
+mkdir -p .git/logs/refs/heads
+touch .git/logs/refs/heads/master
+test_expect_success \
+       "create $m (logged by touch)" \
+       'GIT_COMMITTER_DATE="2005-05-26 23:30" \
+        git-update-ref HEAD $A -m "Initial Creation" &&
+        test $A = $(cat .git/$m)'
+test_expect_success \
+       "update $m (logged by touch)" \
+       'GIT_COMMITTER_DATE="2005-05-26 23:31" \
+        git-update-ref HEAD $B $A -m "Switch" &&
+        test $B = $(cat .git/$m)'
+test_expect_success \
+       "set $m (logged by touch)" \
+       'GIT_COMMITTER_DATE="2005-05-26 23:41" \
+        git-update-ref HEAD $A &&
+        test $A = $(cat .git/$m)'
+
+cat >expect <<EOF
+$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000      Initial Creation
+$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000      Switch
+$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
+EOF
+test_expect_success \
+       "verifying $m's log" \
+       'diff expect .git/logs/$m'
+rm -rf .git/$m .git/logs expect
+
+test_expect_success \
+       'enable core.logAllRefUpdates' \
+       'git-repo-config core.logAllRefUpdates true &&
+        test true = $(git-repo-config --bool --get core.logAllRefUpdates)'
+
+test_expect_success \
+       "create $m (logged by config)" \
+       'GIT_COMMITTER_DATE="2005-05-26 23:32" \
+        git-update-ref HEAD $A -m "Initial Creation" &&
+        test $A = $(cat .git/$m)'
+test_expect_success \
+       "update $m (logged by config)" \
+       'GIT_COMMITTER_DATE="2005-05-26 23:33" \
+        git-update-ref HEAD $B $A -m "Switch" &&
+        test $B = $(cat .git/$m)'
+test_expect_success \
+       "set $m (logged by config)" \
+       'GIT_COMMITTER_DATE="2005-05-26 23:43" \
+        git-update-ref HEAD $A &&
+        test $A = $(cat .git/$m)'
+
+cat >expect <<EOF
+$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000      Initial Creation
+$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000      Switch
+$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
+EOF
+test_expect_success \
+       "verifying $m's log" \
+       'diff expect .git/logs/$m'
+rm -f .git/$m .git/logs/$m expect
+
+git-update-ref $m $D
+cat >.git/logs/$m <<EOF
+$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
+$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
+$F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
+$Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
+EOF
+
+ed="Thu, 26 May 2005 18:32:00 -0500"
+gd="Thu, 26 May 2005 18:33:00 -0500"
+ld="Thu, 26 May 2005 18:43:00 -0500"
+test_expect_success \
+       'Query "master@{May 25 2005}" (before history)' \
+       'rm -f o e
+        git-rev-parse --verify "master@{May 25 2005}" >o 2>e &&
+        test $C = $(cat o) &&
+        test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"'
+test_expect_success \
+       "Query master@{2005-05-25} (before history)" \
+       'rm -f o e
+        git-rev-parse --verify master@{2005-05-25} >o 2>e &&
+        test $C = $(cat o) &&
+        echo test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"'
+test_expect_success \
+       'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \
+       'rm -f o e
+        git-rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e &&
+        test $C = $(cat o) &&
+        test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"'
+test_expect_success \
+       'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \
+       'rm -f o e
+        git-rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e &&
+        test $A = $(cat o) &&
+        test "" = "$(cat e)"'
+test_expect_success \
+       'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \
+       'rm -f o e
+        git-rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e &&
+        test $B = $(cat o) &&
+        test "warning: Log .git/logs/$m has gap after $gd." = "$(cat e)"'
+test_expect_success \
+       'Query "master@{2005-05-26 23:38:00}" (middle of history)' \
+       'rm -f o e
+        git-rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e &&
+        test $Z = $(cat o) &&
+        test "" = "$(cat e)"'
+test_expect_success \
+       'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \
+       'rm -f o e
+        git-rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e &&
+        test $E = $(cat o) &&
+        test "" = "$(cat e)"'
+test_expect_success \
+       'Query "master@{2005-05-28}" (past end of history)' \
+       'rm -f o e
+        git-rev-parse --verify "master@{2005-05-28}" >o 2>e &&
+        test $D = $(cat o) &&
+        test "warning: Log .git/logs/$m unexpectedly ended on $ld." = "$(cat e)"'
+
+
+rm -f .git/$m .git/logs/$m expect
+
+test_expect_success \
+    'creating initial files' \
+    'echo TEST >F &&
+     git-add F &&
+        GIT_AUTHOR_DATE="2005-05-26 23:30" \
+        GIT_COMMITTER_DATE="2005-05-26 23:30" git-commit -m add -a &&
+        h_TEST=$(git-rev-parse --verify HEAD)
+        echo The other day this did not work. >M &&
+        echo And then Bob told me how to fix it. >>M &&
+        echo OTHER >F &&
+        GIT_AUTHOR_DATE="2005-05-26 23:41" \
+        GIT_COMMITTER_DATE="2005-05-26 23:41" git-commit -F M -a &&
+        h_OTHER=$(git-rev-parse --verify HEAD)
+        rm -f M'
+
+cat >expect <<EOF
+$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit: add
+$h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000   commit: The other day this did not work.
+EOF
+test_expect_success \
+       'git-commit logged updates' \
+       'diff expect .git/logs/$m'
+unset h_TEST h_OTHER
+
+test_expect_success \
+       'git-cat-file blob master:F (expect OTHER)' \
+       'test OTHER = $(git-cat-file blob master:F)'
+test_expect_success \
+       'git-cat-file blob master@{2005-05-26 23:30}:F (expect TEST)' \
+       'test TEST = $(git-cat-file blob "master@{2005-05-26 23:30}:F")'
+test_expect_success \
+       'git-cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \
+       'test OTHER = $(git-cat-file blob "master@{2005-05-26 23:42}:F")'
+
+test_done
diff --git a/t/t2101-update-index-reupdate.sh b/t/t2101-update-index-reupdate.sh
new file mode 100755 (executable)
index 0000000..a78ea7f
--- /dev/null
@@ -0,0 +1,84 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='git-update-index --again test.
+'
+
+. ./test-lib.sh
+
+cat > expected <<\EOF
+100644 3b18e512dba79e4c8300dd08aeb37f8e728b8dad 0      file1
+100644 9db8893856a8a02eaa73470054b7c1c5a7c82e47 0      file2
+EOF
+test_expect_success 'update-index --add' \
+       'echo hello world >file1 &&
+        echo goodbye people >file2 &&
+        git-update-index --add file1 file2 &&
+        git-ls-files -s >current &&
+        cmp current expected'
+
+test_expect_success 'update-index --again' \
+       'rm -f file1 &&
+       echo hello everybody >file2 &&
+       if git-update-index --again
+       then
+               echo should have refused to remove file1
+               exit 1
+       else
+               echo happy - failed as expected
+       fi &&
+        git-ls-files -s >current &&
+        cmp current expected'
+
+cat > expected <<\EOF
+100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0      file2
+EOF
+test_expect_success 'update-index --remove --again' \
+       'git-update-index --remove --again &&
+        git-ls-files -s >current &&
+        cmp current expected'
+
+test_expect_success 'first commit' 'git-commit -m initial'
+
+cat > expected <<\EOF
+100644 53ab446c3f4e42ce9bb728a0ccb283a101be4979 0      dir1/file3
+100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0      file2
+EOF
+test_expect_success 'update-index again' \
+       'mkdir -p dir1 &&
+       echo hello world >dir1/file3 &&
+       echo goodbye people >file2 &&
+       git-update-index --add file2 dir1/file3 &&
+       echo hello everybody >file2
+       echo happy >dir1/file3 &&
+       git-update-index --again &&
+       git-ls-files -s >current &&
+       cmp current expected'
+
+cat > expected <<\EOF
+100644 d7fb3f695f06c759dbf3ab00046e7cc2da22d10f 0      dir1/file3
+100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0      file2
+EOF
+test_expect_success 'update-index --update from subdir' \
+       'echo not so happy >file2 &&
+       cd dir1 &&
+       cat ../file2 >file3 &&
+       git-update-index --again &&
+       cd .. &&
+       git-ls-files -s >current &&
+       cmp current expected'
+
+cat > expected <<\EOF
+100644 594fb5bb1759d90998e2bf2a38261ae8e243c760 0      dir1/file3
+100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0      file2
+EOF
+test_expect_success 'update-index --update with pathspec' \
+       'echo very happy >file2 &&
+       cat file2 >dir1/file3 &&
+       git-update-index --again dir1/ &&
+       git-ls-files -s >current &&
+       cmp current expected'
+
+test_done
index c3de151..5b04efc 100755 (executable)
@@ -14,7 +14,8 @@ test_expect_success \
     'prepare an trivial repository' \
     'echo Hello > A &&
      git-update-index --add A &&
-     git-commit -m "Initial commit."'
+     git-commit -m "Initial commit." &&
+     HEAD=$(git-rev-parse --verify HEAD)'
 
 test_expect_success \
     'git branch --help should return success now.' \
@@ -32,4 +33,32 @@ test_expect_success \
     'git branch a/b/c should create a branch' \
     'git-branch a/b/c && test -f .git/refs/heads/a/b/c'
 
+cat >expect <<EOF
+0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000     branch: Created from HEAD
+EOF
+test_expect_success \
+    'git branch -l d/e/f should create a branch and a log' \
+       'GIT_COMMITTER_DATE="2005-05-26 23:30" \
+     git-branch -l d/e/f &&
+        test -f .git/refs/heads/d/e/f &&
+        test -f .git/logs/refs/heads/d/e/f &&
+        diff expect .git/logs/refs/heads/d/e/f'
+
+test_expect_success \
+    'git branch -d d/e/f should delete a branch and a log' \
+       'git-branch -d d/e/f &&
+        test ! -f .git/refs/heads/d/e/f &&
+        test ! -f .git/logs/refs/heads/d/e/f'
+
+cat >expect <<EOF
+0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000     checkout: Created from master^0
+EOF
+test_expect_success \
+    'git checkout -b g/h/i -l should create a branch and a log' \
+       'GIT_COMMITTER_DATE="2005-05-26 23:30" \
+     git-checkout -b g/h/i -l master &&
+        test -f .git/refs/heads/g/h/i &&
+        test -f .git/logs/refs/heads/g/h/i &&
+        diff expect .git/logs/refs/heads/g/h/i'
+
 test_done
index 72a93da..c12270e 100755 (executable)
@@ -40,9 +40,11 @@ test_expect_success 'git-ls-files no-funny' \
 t0=`git-write-tree`
 echo "$t0" >t0
 
-echo 'just space
+cat > expected <<\EOF
+just space
 no-funny
-"tabs\t,\" (dq) and spaces"' >expected
+"tabs\t,\" (dq) and spaces"
+EOF
 test_expect_success 'git-ls-files with-funny' \
        'git-update-index --add "$p1" &&
        git-ls-files >current &&
@@ -58,14 +60,18 @@ test_expect_success 'git-ls-files -z with-funny' \
 t1=`git-write-tree`
 echo "$t1" >t1
 
-echo 'just space
+cat > expected <<\EOF
+just space
 no-funny
-"tabs\t,\" (dq) and spaces"' >expected
+"tabs\t,\" (dq) and spaces"
+EOF
 test_expect_success 'git-ls-tree with funny' \
        'git-ls-tree -r $t1 | sed -e "s/^[^     ]*      //" >current &&
         diff -u expected current'
 
-echo 'A        "tabs\t,\" (dq) and spaces"' >expected
+cat > expected <<\EOF
+A      "tabs\t,\" (dq) and spaces"
+EOF
 test_expect_success 'git-diff-index with-funny' \
        'git-diff-index --name-status $t0 >current &&
        diff -u expected current'
@@ -84,53 +90,62 @@ test_expect_success 'git-diff-tree -z with-funny' \
        'git-diff-tree -z --name-status $t0 $t1 | tr \\0 \\012 >current &&
        diff -u expected current'
 
-echo 'CNUM     no-funny        "tabs\t,\" (dq) and spaces"' >expected
+cat > expected <<\EOF
+CNUM   no-funny        "tabs\t,\" (dq) and spaces"
+EOF
 test_expect_success 'git-diff-tree -C with-funny' \
        'git-diff-tree -C --find-copies-harder --name-status \
                $t0 $t1 | sed -e 's/^C[0-9]*/CNUM/' >current &&
        diff -u expected current'
 
-echo 'RNUM     no-funny        "tabs\t,\" (dq) and spaces"' >expected
+cat > expected <<\EOF
+RNUM   no-funny        "tabs\t,\" (dq) and spaces"
+EOF
 test_expect_success 'git-diff-tree delete with-funny' \
        'git-update-index --force-remove "$p0" &&
        git-diff-index -M --name-status \
                $t0 | sed -e 's/^R[0-9]*/RNUM/' >current &&
        diff -u expected current'
 
-echo 'diff --git a/no-funny "b/tabs\t,\" (dq) and spaces"
+cat > expected <<\EOF
+diff --git a/no-funny "b/tabs\t,\" (dq) and spaces"
 similarity index NUM%
 rename from no-funny
-rename to "tabs\t,\" (dq) and spaces"' >expected
-
+rename to "tabs\t,\" (dq) and spaces"
+EOF
 test_expect_success 'git-diff-tree delete with-funny' \
        'git-diff-index -M -p $t0 |
         sed -e "s/index [0-9]*%/index NUM%/" >current &&
         diff -u expected current'
 
 chmod +x "$p1"
-echo 'diff --git a/no-funny "b/tabs\t,\" (dq) and spaces"
+cat > expected <<\EOF
+diff --git a/no-funny "b/tabs\t,\" (dq) and spaces"
 old mode 100644
 new mode 100755
 similarity index NUM%
 rename from no-funny
-rename to "tabs\t,\" (dq) and spaces"' >expected
-
+rename to "tabs\t,\" (dq) and spaces"
+EOF
 test_expect_success 'git-diff-tree delete with-funny' \
        'git-diff-index -M -p $t0 |
         sed -e "s/index [0-9]*%/index NUM%/" >current &&
         diff -u expected current'
 
-echo >expected ' "tabs\t,\" (dq) and spaces"
- 1 files changed, 0 insertions(+), 0 deletions(-)'
+cat >expected <<\EOF
+ "tabs\t,\" (dq) and spaces"
+ 1 files changed, 0 insertions(+), 0 deletions(-)
+EOF
 test_expect_success 'git-diff-tree rename with-funny applied' \
        'git-diff-index -M -p $t0 |
         git-apply --stat | sed -e "s/|.*//" -e "s/ *\$//" >current &&
         diff -u expected current'
 
-echo >expected ' no-funny
+cat > expected <<\EOF
+ no-funny
  "tabs\t,\" (dq) and spaces"
- 2 files changed, 3 insertions(+), 3 deletions(-)'
-
+ 2 files changed, 3 insertions(+), 3 deletions(-)
+EOF
 test_expect_success 'git-diff-tree delete with-funny applied' \
        'git-diff-index -p $t0 |
         git-apply --stat | sed -e "s/|.*//" -e "s/ *\$//" >current &&
index b141f89..e83bbee 100755 (executable)
@@ -30,6 +30,7 @@ test_expect_success \
      git-commit -m "Add C." &&
 
      git-checkout -f master &&
+     rm -f B C &&
 
      echo Third >> A &&
      git-update-index A &&
index acaa4d6..201d164 100755 (executable)
@@ -8,30 +8,34 @@ test_description='Test of the various options to git-rm.'
 . ./test-lib.sh
 
 # Setup some files to be removed, some with funny characters
-touch -- foo bar baz 'space embedded' -q
-git-add -- foo bar baz 'space embedded' -q
-git-commit -m "add normal files"
-test_tabs=y
-if touch -- 'tab       embedded' 'newline
-embedded'
-then
-git-add -- 'tab        embedded' 'newline
+test_expect_success \
+    'Initialize test directory' \
+    "touch -- foo bar baz 'space embedded' -q &&
+     git-add -- foo bar baz 'space embedded' -q &&
+     git-commit -m 'add normal files' &&
+     test_tabs=y &&
+     if touch -- 'tab  embedded' 'newline
 embedded'
-git-commit -m "add files with tabs and newlines"
-else
-    say 'Your filesystem does not allow tabs in filenames.'
-    test_tabs=n
-fi
+     then
+     git-add -- 'tab   embedded' 'newline
+embedded' &&
+     git-commit -m 'add files with tabs and newlines'
+     else
+         say 'Your filesystem does not allow tabs in filenames.'
+         test_tabs=n
+     fi"
 
 # Later we will try removing an unremovable path to make sure
 # git-rm barfs, but if the test is run as root that cannot be
 # arranged.
-: >test-file
-chmod a-w .
-rm -f test-file
-test -f test-file && test_failed_remove=y
-chmod 775 .
-rm -f test-file
+test_expect_success \
+    'Determine rm behavior' \
+    ': >test-file
+     chmod a-w .
+     rm -f test-file
+     test -f test-file && test_failed_remove=y
+     chmod 775 .
+     rm -f test-file'
 
 test_expect_success \
     'Pre-check that foo exists and is in index before git-rm foo' \
index 769274a..56eda63 100755 (executable)
@@ -191,7 +191,7 @@ test_expect_success \
     'rm -fr Z [A-Z][A-Z] &&
      git-read-tree $tree_A &&
      git-checkout-index -f -a &&
-     git-read-tree -m $tree_O || return 1
+     git-read-tree --reset $tree_O || return 1
      git-update-index --refresh >/dev/null ;# this can exit non-zero
      git-diff-files >.test-a &&
      cmp_diff_files_output .test-a .test-recursive-OA'
@@ -201,7 +201,7 @@ test_expect_success \
     'rm -fr Z [A-Z][A-Z] &&
      git-read-tree $tree_B &&
      git-checkout-index -f -a &&
-     git-read-tree -m $tree_O || return 1
+     git-read-tree --reset $tree_O || return 1
      git-update-index --refresh >/dev/null ;# this can exit non-zero
      git-diff-files >.test-a &&
      cmp_diff_files_output .test-a .test-recursive-OB'
@@ -211,7 +211,7 @@ test_expect_success \
     'rm -fr Z [A-Z][A-Z] &&
      git-read-tree $tree_B &&
      git-checkout-index -f -a &&
-     git-read-tree -m $tree_A || return 1
+     git-read-tree --reset $tree_A || return 1
      git-update-index --refresh >/dev/null ;# this can exit non-zero
      git-diff-files >.test-a &&
      cmp_diff_files_output .test-a .test-recursive-AB'
diff --git a/t/t4012-diff-binary.sh b/t/t4012-diff-binary.sh
new file mode 100755 (executable)
index 0000000..323606c
--- /dev/null
@@ -0,0 +1,80 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='Binary diff and apply
+'
+
+. ./test-lib.sh
+
+test_expect_success 'prepare repository' \
+       'echo AIT >a && echo BIT >b && echo CIT >c && echo DIT >d &&
+        git-update-index --add a b c d &&
+        echo git >a &&
+        cat ../test4012.png >b &&
+        echo git >c &&
+        cat b b >d'
+
+cat > expected <<\EOF
+ a |    2 +-
+ b |  Bin
+ c |    2 +-
+ d |  Bin
+ 4 files changed, 2 insertions(+), 2 deletions(-)
+EOF
+test_expect_success 'diff without --binary' \
+       'git-diff | git-apply --stat --summary >current &&
+        cmp current expected'
+
+test_expect_success 'diff with --binary' \
+       'git-diff --binary | git-apply --stat --summary >current &&
+        cmp current expected'
+
+# apply needs to be able to skip the binary material correctly
+# in order to report the line number of a corrupt patch.
+test_expect_success 'apply detecting corrupt patch correctly' \
+       'git-diff | sed -e 's/-CIT/xCIT/' >broken &&
+        if git-apply --stat --summary broken 2>detected
+        then
+               echo unhappy - should have detected an error
+               (exit 1)
+        else
+               echo happy
+        fi &&
+        detected=`cat detected` &&
+        detected=`expr "$detected" : "fatal.*at line \\([0-9]*\\)\$"` &&
+        detected=`sed -ne "${detected}p" broken` &&
+        test "$detected" = xCIT'
+
+test_expect_success 'apply detecting corrupt patch correctly' \
+       'git-diff --binary | sed -e 's/-CIT/xCIT/' >broken &&
+        if git-apply --stat --summary broken 2>detected
+        then
+               echo unhappy - should have detected an error
+               (exit 1)
+        else
+               echo happy
+        fi &&
+        detected=`cat detected` &&
+        detected=`expr "$detected" : "fatal.*at line \\([0-9]*\\)\$"` &&
+        detected=`sed -ne "${detected}p" broken` &&
+        test "$detected" = xCIT'
+
+test_expect_success 'initial commit' 'git-commit -a -m initial'
+
+# Try removal (b), modification (d), and creation (e).
+test_expect_success 'diff-index with --binary' \
+       'echo AIT >a && mv b e && echo CIT >c && cat e >d &&
+        git-update-index --add --remove a b c d e &&
+        tree0=`git-write-tree` &&
+        git-diff --cached --binary >current &&
+        git-apply --stat --summary current'
+
+test_expect_success 'apply binary patch' \
+       'git-reset --hard &&
+        git-apply --binary --index <current &&
+        tree1=`git-write-tree` &&
+        test "$tree1" = "$tree0"'
+
+test_done
index 26b131d..026fac8 100755 (executable)
@@ -20,14 +20,10 @@ do
   for j in 0 1 2 3
   do
     test $i -eq $j && continue
-    diff -u frotz.$i frotz.$j |
-    sed -e '
-       /^---/s|.*|--- a/frotz|
-       /^+++/s|.*|+++ b/frotz|' >diff.$i-$j
     cat frotz.$i >frotz
     test_expect_success \
         "apply diff between $i and $j" \
-       "git-apply <diff.$i-$j && diff frotz.$j frotz"
+       "git-apply <../t4101/diff.$i-$j && diff frotz.$j frotz"
   done
 done
 
diff --git a/t/t4101/diff.0-1 b/t/t4101/diff.0-1
new file mode 100644 (file)
index 0000000..1010a88
--- /dev/null
@@ -0,0 +1,6 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,3 @@
+ a
+ b
++c
diff --git a/t/t4101/diff.0-2 b/t/t4101/diff.0-2
new file mode 100644 (file)
index 0000000..36460a2
--- /dev/null
@@ -0,0 +1,7 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,2 @@
+ a
+-b
++b
+\ No newline at end of file
diff --git a/t/t4101/diff.0-3 b/t/t4101/diff.0-3
new file mode 100644 (file)
index 0000000..b281c43
--- /dev/null
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,3 @@
+ a
+-b
++c
++b
+\ No newline at end of file
diff --git a/t/t4101/diff.1-0 b/t/t4101/diff.1-0
new file mode 100644 (file)
index 0000000..f0a2e92
--- /dev/null
@@ -0,0 +1,6 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,2 @@
+ a
+ b
+-c
diff --git a/t/t4101/diff.1-2 b/t/t4101/diff.1-2
new file mode 100644 (file)
index 0000000..2a440a5
--- /dev/null
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,2 @@
+ a
+-b
+-c
++b
+\ No newline at end of file
diff --git a/t/t4101/diff.1-3 b/t/t4101/diff.1-3
new file mode 100644 (file)
index 0000000..61aff97
--- /dev/null
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,3 @@
+ a
+-b
+ c
++b
+\ No newline at end of file
diff --git a/t/t4101/diff.2-0 b/t/t4101/diff.2-0
new file mode 100644 (file)
index 0000000..c2e71ee
--- /dev/null
@@ -0,0 +1,7 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,2 @@
+ a
+-b
+\ No newline at end of file
++b
diff --git a/t/t4101/diff.2-1 b/t/t4101/diff.2-1
new file mode 100644 (file)
index 0000000..a66d9fd
--- /dev/null
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,3 @@
+ a
+-b
+\ No newline at end of file
++b
++c
diff --git a/t/t4101/diff.2-3 b/t/t4101/diff.2-3
new file mode 100644 (file)
index 0000000..5633c83
--- /dev/null
@@ -0,0 +1,7 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,3 @@
+ a
++c
+ b
+\ No newline at end of file
diff --git a/t/t4101/diff.3-0 b/t/t4101/diff.3-0
new file mode 100644 (file)
index 0000000..10b1a41
--- /dev/null
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,2 @@
+ a
+-c
+-b
+\ No newline at end of file
++b
diff --git a/t/t4101/diff.3-1 b/t/t4101/diff.3-1
new file mode 100644 (file)
index 0000000..c799c60
--- /dev/null
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,3 @@
+ a
++b
+ c
+-b
+\ No newline at end of file
diff --git a/t/t4101/diff.3-2 b/t/t4101/diff.3-2
new file mode 100644 (file)
index 0000000..f8d1ba6
--- /dev/null
@@ -0,0 +1,7 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,2 @@
+ a
+-c
+ b
+\ No newline at end of file
diff --git a/t/t4113-apply-ending.sh b/t/t4113-apply-ending.sh
new file mode 100755 (executable)
index 0000000..7fd0cf6
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Catalin Marinas
+#
+
+test_description='git-apply trying to add an ending line.
+
+'
+. ./test-lib.sh
+
+# setup
+
+cat >test-patch <<\EOF
+diff --git a/file b/file
+--- a/file
++++ b/file
+@@ -1,2 +1,3 @@
+ a
+ b
++c
+EOF
+
+echo 'a' >file
+echo 'b' >>file
+echo 'c' >>file
+
+test_expect_success setup \
+    'git-update-index --add file'
+
+# test
+
+test_expect_failure 'apply at the end' \
+    'git-apply --index test-patch'
+
+cat >test-patch <<\EOF
+diff a/file b/file
+--- a/file
++++ b/file
+@@ -1,2 +1,3 @@
++a
+ b
+ c
+EOF
+
+echo >file 'a
+b
+c'
+git-update-index file
+
+test_expect_failure 'apply at the beginning' \
+       'git-apply --index test-patch'
+
+test_done
diff --git a/t/t5100-mailinfo.sh b/t/t5100-mailinfo.sh
new file mode 100755 (executable)
index 0000000..17c1b80
--- /dev/null
@@ -0,0 +1,28 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git-mailinfo and git-mailsplit test'
+
+. ./test-lib.sh
+
+test_expect_success 'split sample box' \
+       'git-mailsplit -o. ../t5100/sample.mbox >last &&
+       last=`cat last` &&
+       echo total is $last &&
+       test `cat last` = 5'
+
+for mail in `echo 00*`
+do
+       test_expect_success "mailinfo $mail" \
+               "git-mailinfo -u msg$mail patch$mail <$mail >info$mail &&
+               echo msg &&
+               diff ../t5100/msg$mail msg$mail &&
+               echo patch &&
+               diff ../t5100/patch$mail patch$mail &&
+               echo info &&
+               diff ../t5100/info$mail info$mail"
+done
+
+test_done
diff --git a/t/t5100/info0001 b/t/t5100/info0001
new file mode 100644 (file)
index 0000000..8c05277
--- /dev/null
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: a commit.
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0002 b/t/t5100/info0002
new file mode 100644 (file)
index 0000000..49bb0fe
--- /dev/null
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: another patch
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0003 b/t/t5100/info0003
new file mode 100644 (file)
index 0000000..bd0d122
--- /dev/null
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: third patch
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0004 b/t/t5100/info0004
new file mode 100644 (file)
index 0000000..616c309
--- /dev/null
@@ -0,0 +1,5 @@
+Author: YOSHIFUJI Hideaki / 吉藤英明
+Email: yoshfuji@linux-ipv6.org
+Subject: GIT: Try all addresses for given remote name
+Date: Thu, 21 Jul 2005 09:10:36 -0400 (EDT)
+
diff --git a/t/t5100/info0005 b/t/t5100/info0005
new file mode 100644 (file)
index 0000000..46a46fc
--- /dev/null
@@ -0,0 +1,5 @@
+Author: David Kågedal
+Email: davidk@lysator.liu.se
+Subject: Fixed two bugs in git-cvsimport-script.
+Date: Mon, 15 Aug 2005 20:18:25 +0200
+
diff --git a/t/t5100/msg0001 b/t/t5100/msg0001
new file mode 100644 (file)
index 0000000..b275a9a
--- /dev/null
@@ -0,0 +1,2 @@
+Here is a patch from A U Thor.
+
diff --git a/t/t5100/msg0002 b/t/t5100/msg0002
new file mode 100644 (file)
index 0000000..e2546ec
--- /dev/null
@@ -0,0 +1,21 @@
+Here is a patch from A U Thor.  This addresses the issue raised in the
+message:
+
+From: Nit Picker <nit.picker@example.net>
+Subject: foo is too old
+Message-Id: <nitpicker.12121212@example.net>
+
+Hopefully this would fix the problem stated there.
+
+
+I have included an extra blank line above, but it does not have to be
+stripped away here, along with the                                
+whitespaces at the end of the above line.  They are expected to be squashed
+when the message is made into a commit log by stripspace,
+Also, there are three blank lines after this paragraph,
+two truly blank and another full of spaces in between.
+
+            
+
+Hope this helps.
+
diff --git a/t/t5100/msg0003 b/t/t5100/msg0003
new file mode 100644 (file)
index 0000000..1ac6810
--- /dev/null
@@ -0,0 +1,9 @@
+Here is a patch from A U Thor.  This addresses the issue raised in the
+message:
+
+From: Nit Picker <nit.picker@example.net>
+Subject: foo is too old
+Message-Id: <nitpicker.12121212@example.net>
+
+Hopefully this would fix the problem stated there.
+
diff --git a/t/t5100/msg0004 b/t/t5100/msg0004
new file mode 100644 (file)
index 0000000..6f8ba3b
--- /dev/null
@@ -0,0 +1,7 @@
+Hello.
+
+Try all addresses for given remote name until it succeeds.
+Also supports IPv6.
+
+Signed-of-by: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
+
diff --git a/t/t5100/msg0005 b/t/t5100/msg0005
new file mode 100644 (file)
index 0000000..dd94cd7
--- /dev/null
@@ -0,0 +1,13 @@
+The git-cvsimport-script had a copule of small bugs that prevented me
+from importing a big CVS repository.
+
+The first was that it didn't handle removed files with a multi-digit
+primary revision number.
+
+The second was that it was asking the CVS server for "F" messages,
+although they were not handled.
+
+I also updated the documentation for that script to correspond to
+actual flags.
+
+Signed-off-by: David Kågedal <davidk@lysator.liu.se>
diff --git a/t/t5100/patch0001 b/t/t5100/patch0001
new file mode 100644 (file)
index 0000000..8ce1551
--- /dev/null
@@ -0,0 +1,14 @@
+---
+ foo |    2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun  9 00:44:04 PDT 2006
++Fri Jun  9 00:44:13 PDT 2006
+-- 
+1.4.0.g6f2b
+
diff --git a/t/t5100/patch0002 b/t/t5100/patch0002
new file mode 100644 (file)
index 0000000..8ce1551
--- /dev/null
@@ -0,0 +1,14 @@
+---
+ foo |    2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun  9 00:44:04 PDT 2006
++Fri Jun  9 00:44:13 PDT 2006
+-- 
+1.4.0.g6f2b
+
diff --git a/t/t5100/patch0003 b/t/t5100/patch0003
new file mode 100644 (file)
index 0000000..8ce1551
--- /dev/null
@@ -0,0 +1,14 @@
+---
+ foo |    2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun  9 00:44:04 PDT 2006
++Fri Jun  9 00:44:13 PDT 2006
+-- 
+1.4.0.g6f2b
+
diff --git a/t/t5100/patch0004 b/t/t5100/patch0004
new file mode 100644 (file)
index 0000000..196458e
--- /dev/null
@@ -0,0 +1,93 @@
+diff --git a/connect.c b/connect.c
+--- a/connect.c
++++ b/connect.c
+@@ -96,42 +96,57 @@ static enum protocol get_protocol(const 
+       die("I don't handle protocol '%s'", name);
+ }
+-static void lookup_host(const char *host, struct sockaddr *in)
+-{
+-      struct addrinfo *res;
+-      int ret;
+-
+-      ret = getaddrinfo(host, NULL, NULL, &res);
+-      if (ret)
+-              die("Unable to look up %s (%s)", host, gai_strerror(ret));
+-      *in = *res->ai_addr;
+-      freeaddrinfo(res);
+-}
++#define STR_(s)       # s
++#define STR(s)        STR_(s)
+ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
+ {
+-      struct sockaddr addr;
+-      int port = DEFAULT_GIT_PORT, sockfd;
+-      char *colon;
+-
+-      colon = strchr(host, ':');
+-      if (colon) {
+-              char *end;
+-              unsigned long n = strtoul(colon+1, &end, 0);
+-              if (colon[1] && !*end) {
+-                      *colon = 0;
+-                      port = n;
++      int sockfd = -1;
++      char *colon, *end;
++      char *port = STR(DEFAULT_GIT_PORT);
++      struct addrinfo hints, *ai0, *ai;
++      int gai;
++
++      if (host[0] == '[') {
++              end = strchr(host + 1, ']');
++              if (end) {
++                      *end = 0;
++                      end++;
++                      host++;
++              } else
++                      end = host;
++      } else
++              end = host;
++      colon = strchr(end, ':');
++
++      if (colon)
++              port = colon + 1;
++
++      memset(&hints, 0, sizeof(hints));
++      hints.ai_socktype = SOCK_STREAM;
++      hints.ai_protocol = IPPROTO_TCP;
++
++      gai = getaddrinfo(host, port, &hints, &ai);
++      if (gai)
++              die("Unable to look up %s (%s)", host, gai_strerror(gai));
++
++      for (ai0 = ai; ai; ai = ai->ai_next) {
++              sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
++              if (sockfd < 0)
++                      continue;
++              if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
++                      close(sockfd);
++                      sockfd = -1;
++                      continue;
+               }
++              break;
+       }
+-      lookup_host(host, &addr);
+-      ((struct sockaddr_in *)&addr)->sin_port = htons(port);
++      freeaddrinfo(ai0);
+-      sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
+       if (sockfd < 0)
+               die("unable to create socket (%s)", strerror(errno));
+-      if (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)
+-              die("unable to connect (%s)", strerror(errno));
++
+       fd[0] = sockfd;
+       fd[1] = sockfd;
+       packet_write(sockfd, "%s %s\n", prog, path);
+
+-- 
+YOSHIFUJI Hideaki @ USAGI Project  <yoshfuji@linux-ipv6.org>
+GPG-FP  : 9022 65EB 1ECF 3AD1 0BDF  80D8 4807 F894 E062 0EEA
+
diff --git a/t/t5100/patch0005 b/t/t5100/patch0005
new file mode 100644 (file)
index 0000000..7d24b24
--- /dev/null
@@ -0,0 +1,69 @@
+---
+
+ Documentation/git-cvsimport-script.txt |    9 ++++++++-
+ git-cvsimport-script                   |    4 ++--
+ 2 files changed, 10 insertions(+), 3 deletions(-)
+
+50452f9c0c2df1f04d83a26266ba704b13861632
+diff --git a/Documentation/git-cvsimport-script.txt b/Documentation/git-cvsimport-script.txt
+--- a/Documentation/git-cvsimport-script.txt
++++ b/Documentation/git-cvsimport-script.txt
+@@ -29,6 +29,10 @@ OPTIONS
+       currently, only the :local:, :ext: and :pserver: access methods 
+       are supported.
++-C <target-dir>::
++        The GIT repository to import to.  If the directory doesn't
++        exist, it will be created.  Default is the current directory.
++
+ -i::
+       Import-only: don't perform a checkout after importing.  This option
+       ensures the working directory and cache remain untouched and will
+@@ -44,7 +48,7 @@ OPTIONS
+ -p <options-for-cvsps>::
+       Additional options for cvsps.
+-      The options '-x' and '-A' are implicit and should not be used here.
++      The options '-u' and '-A' are implicit and should not be used here.
+       If you need to pass multiple options, separate them with a comma.
+@@ -57,6 +61,9 @@ OPTIONS
+ -h::
+       Print a short usage message and exit.
++-z <fuzz>::
++        Pass the timestamp fuzz factor to cvsps.
++
+ OUTPUT
+ ------
+ If '-v' is specified, the script reports what it is doing.
+diff --git a/git-cvsimport-script b/git-cvsimport-script
+--- a/git-cvsimport-script
++++ b/git-cvsimport-script
+@@ -190,7 +190,7 @@ sub conn {
+       $self->{'socketo'}->write("Root $repo\n");
+       # Trial and error says that this probably is the minimum set
+-      $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
++      $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E Checked-in Created Updated Merged Removed\n");
+       $self->{'socketo'}->write("valid-requests\n");
+       $self->{'socketo'}->flush();
+@@ -691,7 +691,7 @@ while(<CVS>) {
+               unlink($tmpname);
+               my $mode = pmode($cvs->{'mode'});
+               push(@new,[$mode, $sha, $fn]); # may be resurrected!
+-      } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
++      } elsif($state == 9 and /^\s+(\S+):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) {
+               my $fn = $1;
+               $fn =~ s#^/+##;
+               push(@old,$fn);
+
+-- 
+David Kågedal
+-
+To unsubscribe from this list: send the line "unsubscribe git" in
+the body of a message to majordomo@vger.kernel.org
+More majordomo info at  http://vger.kernel.org/majordomo-info.html
+
diff --git a/t/t5100/sample.mbox b/t/t5100/sample.mbox
new file mode 100644 (file)
index 0000000..a768454
--- /dev/null
@@ -0,0 +1,317 @@
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: [PATCH] a commit.
+
+Here is a patch from A U Thor.
+
+---
+ foo |    2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun  9 00:44:04 PDT 2006
++Fri Jun  9 00:44:13 PDT 2006
+-- 
+1.4.0.g6f2b
+
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: [PATCH] another patch
+
+Here is a patch from A U Thor.  This addresses the issue raised in the
+message:
+
+From: Nit Picker <nit.picker@example.net>
+Subject: foo is too old
+Message-Id: <nitpicker.12121212@example.net>
+
+Hopefully this would fix the problem stated there.
+
+
+I have included an extra blank line above, but it does not have to be
+stripped away here, along with the                                
+whitespaces at the end of the above line.  They are expected to be squashed
+when the message is made into a commit log by stripspace,
+Also, there are three blank lines after this paragraph,
+two truly blank and another full of spaces in between.
+
+            
+
+Hope this helps.
+
+---
+ foo |    2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun  9 00:44:04 PDT 2006
++Fri Jun  9 00:44:13 PDT 2006
+-- 
+1.4.0.g6f2b
+
+From nobody Mon Sep 17 00:00:00 2001
+From: Junio C Hamano <junio@kernel.org>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: re: [PATCH] another patch
+
+From: A U Thor <a.u.thor@example.com>
+Subject: [PATCH] third patch
+
+Here is a patch from A U Thor.  This addresses the issue raised in the
+message:
+
+From: Nit Picker <nit.picker@example.net>
+Subject: foo is too old
+Message-Id: <nitpicker.12121212@example.net>
+
+Hopefully this would fix the problem stated there.
+
+---
+ foo |    2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun  9 00:44:04 PDT 2006
++Fri Jun  9 00:44:13 PDT 2006
+-- 
+1.4.0.g6f2b
+
+From nobody Sat Aug 27 23:07:49 2005
+Path: news.gmane.org!not-for-mail
+Message-ID: <20050721.091036.01119516.yoshfuji@linux-ipv6.org>
+From: YOSHIFUJI Hideaki / =?iso-2022-jp?B?GyRCNUhGIzFRTEAbKEI=?= 
+       <yoshfuji@linux-ipv6.org>
+Newsgroups: gmane.comp.version-control.git
+Subject: [PATCH 1/2] GIT: Try all addresses for given remote name
+Date: Thu, 21 Jul 2005 09:10:36 -0400 (EDT)
+Lines: 99
+Organization: USAGI/WIDE Project
+Approved: news@gmane.org
+NNTP-Posting-Host: main.gmane.org
+Mime-Version: 1.0
+Content-Type: Text/Plain; charset=us-ascii
+Content-Transfer-Encoding: 7bit
+X-Trace: sea.gmane.org 1121951434 29350 80.91.229.2 (21 Jul 2005 13:10:34 GMT)
+X-Complaints-To: usenet@sea.gmane.org
+NNTP-Posting-Date: Thu, 21 Jul 2005 13:10:34 +0000 (UTC)
+
+Hello.
+
+Try all addresses for given remote name until it succeeds.
+Also supports IPv6.
+
+Signed-of-by: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
+
+diff --git a/connect.c b/connect.c
+--- a/connect.c
++++ b/connect.c
+@@ -96,42 +96,57 @@ static enum protocol get_protocol(const 
+       die("I don't handle protocol '%s'", name);
+ }
+-static void lookup_host(const char *host, struct sockaddr *in)
+-{
+-      struct addrinfo *res;
+-      int ret;
+-
+-      ret = getaddrinfo(host, NULL, NULL, &res);
+-      if (ret)
+-              die("Unable to look up %s (%s)", host, gai_strerror(ret));
+-      *in = *res->ai_addr;
+-      freeaddrinfo(res);
+-}
++#define STR_(s)       # s
++#define STR(s)        STR_(s)
+ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
+ {
+-      struct sockaddr addr;
+-      int port = DEFAULT_GIT_PORT, sockfd;
+-      char *colon;
+-
+-      colon = strchr(host, ':');
+-      if (colon) {
+-              char *end;
+-              unsigned long n = strtoul(colon+1, &end, 0);
+-              if (colon[1] && !*end) {
+-                      *colon = 0;
+-                      port = n;
++      int sockfd = -1;
++      char *colon, *end;
++      char *port = STR(DEFAULT_GIT_PORT);
++      struct addrinfo hints, *ai0, *ai;
++      int gai;
++
++      if (host[0] == '[') {
++              end = strchr(host + 1, ']');
++              if (end) {
++                      *end = 0;
++                      end++;
++                      host++;
++              } else
++                      end = host;
++      } else
++              end = host;
++      colon = strchr(end, ':');
++
++      if (colon)
++              port = colon + 1;
++
++      memset(&hints, 0, sizeof(hints));
++      hints.ai_socktype = SOCK_STREAM;
++      hints.ai_protocol = IPPROTO_TCP;
++
++      gai = getaddrinfo(host, port, &hints, &ai);
++      if (gai)
++              die("Unable to look up %s (%s)", host, gai_strerror(gai));
++
++      for (ai0 = ai; ai; ai = ai->ai_next) {
++              sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
++              if (sockfd < 0)
++                      continue;
++              if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
++                      close(sockfd);
++                      sockfd = -1;
++                      continue;
+               }
++              break;
+       }
+-      lookup_host(host, &addr);
+-      ((struct sockaddr_in *)&addr)->sin_port = htons(port);
++      freeaddrinfo(ai0);
+-      sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
+       if (sockfd < 0)
+               die("unable to create socket (%s)", strerror(errno));
+-      if (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)
+-              die("unable to connect (%s)", strerror(errno));
++
+       fd[0] = sockfd;
+       fd[1] = sockfd;
+       packet_write(sockfd, "%s %s\n", prog, path);
+
+-- 
+YOSHIFUJI Hideaki @ USAGI Project  <yoshfuji@linux-ipv6.org>
+GPG-FP  : 9022 65EB 1ECF 3AD1 0BDF  80D8 4807 F894 E062 0EEA
+
+From nobody Sat Aug 27 23:07:49 2005
+Path: news.gmane.org!not-for-mail
+Message-ID: <u5tacjjdpxq.fsf@lysator.liu.se>
+From: =?iso-8859-1?Q?David_K=E5gedal?= <davidk@lysator.liu.se>
+Newsgroups: gmane.comp.version-control.git
+Subject: [PATCH] Fixed two bugs in git-cvsimport-script.
+Date: Mon, 15 Aug 2005 20:18:25 +0200
+Lines: 83
+Approved: news@gmane.org
+NNTP-Posting-Host: main.gmane.org
+Mime-Version: 1.0
+Content-Type: text/plain; charset=iso-8859-1
+Content-Transfer-Encoding: QUOTED-PRINTABLE
+X-Trace: sea.gmane.org 1124130247 31839 80.91.229.2 (15 Aug 2005 18:24:07 GMT)
+X-Complaints-To: usenet@sea.gmane.org
+NNTP-Posting-Date: Mon, 15 Aug 2005 18:24:07 +0000 (UTC)
+Cc: "Junio C. Hamano" <junkio@cox.net>
+Original-X-From: git-owner@vger.kernel.org Mon Aug 15 20:24:05 2005
+
+The git-cvsimport-script had a copule of small bugs that prevented me
+from importing a big CVS repository.
+
+The first was that it didn't handle removed files with a multi-digit
+primary revision number.
+
+The second was that it was asking the CVS server for "F" messages,
+although they were not handled.
+
+I also updated the documentation for that script to correspond to
+actual flags.
+
+Signed-off-by: David K=E5gedal <davidk@lysator.liu.se>
+---
+
+ Documentation/git-cvsimport-script.txt |    9 ++++++++-
+ git-cvsimport-script                   |    4 ++--
+ 2 files changed, 10 insertions(+), 3 deletions(-)
+
+50452f9c0c2df1f04d83a26266ba704b13861632
+diff --git a/Documentation/git-cvsimport-script.txt b/Documentation/git=
+-cvsimport-script.txt
+--- a/Documentation/git-cvsimport-script.txt
++++ b/Documentation/git-cvsimport-script.txt
+@@ -29,6 +29,10 @@ OPTIONS
+       currently, only the :local:, :ext: and :pserver: access methods=20
+       are supported.
+=20
++-C <target-dir>::
++        The GIT repository to import to.  If the directory doesn't
++        exist, it will be created.  Default is the current directory.
++
+ -i::
+       Import-only: don't perform a checkout after importing.  This option
+       ensures the working directory and cache remain untouched and will
+@@ -44,7 +48,7 @@ OPTIONS
+=20
+ -p <options-for-cvsps>::
+       Additional options for cvsps.
+-      The options '-x' and '-A' are implicit and should not be used here.
++      The options '-u' and '-A' are implicit and should not be used here.
+=20
+       If you need to pass multiple options, separate them with a comma.
+=20
+@@ -57,6 +61,9 @@ OPTIONS
+ -h::
+       Print a short usage message and exit.
+=20
++-z <fuzz>::
++        Pass the timestamp fuzz factor to cvsps.
++
+ OUTPUT
+ ------
+ If '-v' is specified, the script reports what it is doing.
+diff --git a/git-cvsimport-script b/git-cvsimport-script
+--- a/git-cvsimport-script
++++ b/git-cvsimport-script
+@@ -190,7 +190,7 @@ sub conn {
+       $self->{'socketo'}->write("Root $repo\n");
+=20
+       # Trial and error says that this probably is the minimum set
+-      $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mo=
+de M Mbinary E F Checked-in Created Updated Merged Removed\n");
++      $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mo=
+de M Mbinary E Checked-in Created Updated Merged Removed\n");
+=20
+       $self->{'socketo'}->write("valid-requests\n");
+       $self->{'socketo'}->flush();
+@@ -691,7 +691,7 @@ while(<CVS>) {
+               unlink($tmpname);
+               my $mode =3D pmode($cvs->{'mode'});
+               push(@new,[$mode, $sha, $fn]); # may be resurrected!
+-      } elsif($state =3D=3D 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(=
+DEAD\)\s*$/) {
++      } elsif($state =3D=3D 9 and /^\s+(\S+):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)=
+\(DEAD\)\s*$/) {
+               my $fn =3D $1;
+               $fn =3D~ s#^/+##;
+               push(@old,$fn);
+
+--=20
+David K=E5gedal
+-
+To unsubscribe from this list: send the line "unsubscribe git" in
+the body of a message to majordomo@vger.kernel.org
+More majordomo info at  http://vger.kernel.org/majordomo-info.html
+
index 92f12d9..f7625a6 100755 (executable)
@@ -12,11 +12,11 @@ test_description='Testing multi_ack pack fetching
 
 # Some convenience functions
 
-function add () {
-       local name=$1
-       local text="$@"
-       local branch=${name:0:1}
-       local parents=""
+add () {
+       name=$1
+       text="$@"
+       branch=`echo $name | sed -e 's/^\(.\).*$/\1/'`
+       parents=""
 
        shift
        while test $1; do
@@ -36,13 +36,13 @@ function add () {
        eval ${branch}TIP=$commit
 }
 
-function count_objects () {
+count_objects () {
        ls .git/objects/??/* 2>>log2.txt | wc -l | tr -d " "
 }
 
-function test_expect_object_count () {
-       local message=$1
-       local count=$2
+test_expect_object_count () {
+       message=$1
+       count=$2
 
        output="$(count_objects)"
        test_expect_success \
@@ -50,18 +50,18 @@ function test_expect_object_count () {
                "test $count = $output"
 }
 
-function pull_to_client () {
-       local number=$1
-       local heads=$2
-       local count=$3
-       local no_strict_count_check=$4
+pull_to_client () {
+       number=$1
+       heads=$2
+       count=$3
+       no_strict_count_check=$4
 
        cd client
        test_expect_success "$number pull" \
                "git-fetch-pack -k -v .. $heads"
        case "$heads" in *A*) echo $ATIP > .git/refs/heads/A;; esac
        case "$heads" in *B*) echo $BTIP > .git/refs/heads/B;; esac
-       git-symbolic-ref HEAD refs/heads/${heads:0:1}
+       git-symbolic-ref HEAD refs/heads/`echo $heads | sed -e 's/^\(.\).*$/\1/'`
 
        test_expect_success "fsck" 'git-fsck-objects --full > fsck.txt 2>&1'
 
diff --git a/t/t5700-clone-reference.sh b/t/t5700-clone-reference.sh
new file mode 100755 (executable)
index 0000000..916ee15
--- /dev/null
@@ -0,0 +1,78 @@
+#!/bin/sh
+#
+# Copyright (C) 2006 Martin Waitz <tali@admingilde.org>
+#
+
+test_description='test clone --reference'
+. ./test-lib.sh
+
+base_dir=`pwd`
+
+test_expect_success 'preparing first repository' \
+'test_create_repo A && cd A &&
+echo first > file1 &&
+git add file1 &&
+git commit -m initial'
+
+cd "$base_dir"
+
+test_expect_success 'preparing second repository' \
+'git clone A B && cd B &&
+echo second > file2 &&
+git add file2 &&
+git commit -m addition &&
+git repack -a -d &&
+git prune'
+
+cd "$base_dir"
+
+test_expect_success 'cloning with reference' \
+'git clone -l -s --reference B A C'
+
+cd "$base_dir"
+
+test_expect_success 'existance of info/alternates' \
+'test `wc -l <C/.git/objects/info/alternates` = 2'
+
+cd "$base_dir"
+
+test_expect_success 'pulling from reference' \
+'cd C &&
+git pull ../B'
+
+cd "$base_dir"
+
+test_expect_success 'that reference gets used' \
+'cd C &&
+echo "0 objects, 0 kilobytes" > expected &&
+git count-objects > current &&
+diff expected current'
+
+cd "$base_dir"
+
+test_expect_success 'updating origin' \
+'cd A &&
+echo third > file3 &&
+git add file3 &&
+git commit -m update &&
+git repack -a -d &&
+git prune'
+
+cd "$base_dir"
+
+test_expect_success 'pulling changes from origin' \
+'cd C &&
+git pull origin'
+
+cd "$base_dir"
+
+# the 2 local objects are commit and tree from the merge
+test_expect_success 'that alternate to origin gets used' \
+'cd C &&
+echo "2 objects" > expected &&
+git count-objects | cut -d, -f1 > current &&
+diff expected current'
+
+cd "$base_dir"
+
+test_done
diff --git a/t/t5710-info-alternate.sh b/t/t5710-info-alternate.sh
new file mode 100755 (executable)
index 0000000..097d037
--- /dev/null
@@ -0,0 +1,105 @@
+#!/bin/sh
+#
+# Copyright (C) 2006 Martin Waitz <tali@admingilde.org>
+#
+
+test_description='test transitive info/alternate entries'
+. ./test-lib.sh
+
+# test that a file is not reachable in the current repository
+# but that it is after creating a info/alternate entry
+reachable_via() {
+       alternate="$1"
+       file="$2"
+       if git cat-file -e "HEAD:$file"; then return 1; fi
+       echo "$alternate" >> .git/objects/info/alternate
+       git cat-file -e "HEAD:$file"
+}
+
+test_valid_repo() {
+       git fsck-objects --full > fsck.log &&
+       test `wc -l < fsck.log` = 0
+}
+
+base_dir=`pwd`
+
+test_expect_success 'preparing first repository' \
+'test_create_repo A && cd A &&
+echo "Hello World" > file1 &&
+git add file1 &&
+git commit -m "Initial commit" file1 &&
+git repack -a -d &&
+git prune'
+
+cd "$base_dir"
+
+test_expect_success 'preparing second repository' \
+'git clone -l -s A B && cd B &&
+echo "foo bar" > file2 &&
+git add file2 &&
+git commit -m "next commit" file2 &&
+git repack -a -d -l &&
+git prune'
+
+cd "$base_dir"
+
+test_expect_success 'preparing third repository' \
+'git clone -l -s B C && cd C &&
+echo "Goodbye, cruel world" > file3 &&
+git add file3 &&
+git commit -m "one more" file3 &&
+git repack -a -d -l &&
+git prune'
+
+cd "$base_dir"
+
+test_expect_failure 'creating too deep nesting' \
+'git clone -l -s C D &&
+git clone -l -s D E &&
+git clone -l -s E F &&
+git clone -l -s F G &&
+test_valid_repo'
+
+cd "$base_dir"
+
+test_expect_success 'validity of third repository' \
+'cd C &&
+test_valid_repo'
+
+cd "$base_dir"
+
+test_expect_success 'validity of fourth repository' \
+'cd D &&
+test_valid_repo'
+
+cd "$base_dir"
+
+test_expect_success 'breaking of loops' \
+"echo '$base_dir/B/.git/objects' >> '$base_dir'/A/.git/objects/info/alternates&&
+cd C &&
+test_valid_repo"
+
+cd "$base_dir"
+
+test_expect_failure 'that info/alternates is neccessary' \
+'cd C &&
+rm .git/objects/info/alternates &&
+test_valid_repo'
+
+cd "$base_dir"
+
+test_expect_success 'that relative alternate is possible for current dir' \
+'cd C &&
+echo "../../../B/.git/objects" > .git/objects/info/alternates &&
+test_valid_repo'
+
+cd "$base_dir"
+
+test_expect_failure 'that relative alternate is only possible for current dir' \
+'cd D &&
+test_valid_repo'
+
+cd "$base_dir"
+
+test_done
+
index c6752af..d402621 100755 (executable)
@@ -69,7 +69,9 @@ on_committer_date()
 {
     _date=$1
     shift 1
-    GIT_COMMITTER_DATE=$_date "$@"
+    export GIT_COMMITTER_DATE="$_date"
+    "$@"
+    unset GIT_COMMITTER_DATE
 }
 
 # Execute a command and suppress any error output.
index a2d24b5..5ac2564 100755 (executable)
@@ -111,6 +111,7 @@ test_expect_success 'pull renaming branch into unrenaming one' \
 
 test_expect_success 'pull renaming branch into another renaming one' \
 '
+       rm -f B
        git reset --hard
        git checkout red
        git pull . white && {
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
new file mode 100755 (executable)
index 0000000..a61da1e
--- /dev/null
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+test_description='git-send-email'
+. ./test-lib.sh
+
+PROG='git send-email'
+test_expect_success \
+    'prepare reference tree' \
+    'echo "1A quick brown fox jumps over the" >file &&
+     echo "lazy dog" >>file &&
+     git add file
+     GIT_AUTHOR_NAME="A" git commit -a -m "Initial."'
+
+test_expect_success \
+    'Setup helper tool' \
+    '(echo "#!/bin/sh"
+      echo shift
+      echo for a
+      echo do
+      echo "  echo \"!\$a!\""
+      echo "done >commandline"
+      echo "cat > msgtxt"
+      ) >fake.sendmail
+     chmod +x ./fake.sendmail
+     git add fake.sendmail
+     GIT_AUTHOR_NAME="A" git commit -a -m "Second."'
+
+test_expect_success \
+    'Extract patches and send' \
+    'git format-patch -n HEAD^1
+     git send-email -from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" ./0001*txt'
+
+cat >expected <<\EOF
+!nobody@example.com!
+!author@example.com!
+EOF
+test_expect_success \
+    'Verify commandline' \
+    'diff commandline expected'
+
+test_done
diff --git a/t/test4012.png b/t/test4012.png
new file mode 100644 (file)
index 0000000..7b181d1
Binary files /dev/null and b/t/test4012.png differ
diff --git a/tar-tree.c b/tar-tree.c
deleted file mode 100644 (file)
index fc60a90..0000000
+++ /dev/null
@@ -1,350 +0,0 @@
-/*
- * Copyright (c) 2005, 2006 Rene Scharfe
- */
-#include <time.h>
-#include "cache.h"
-#include "tree-walk.h"
-#include "commit.h"
-#include "strbuf.h"
-#include "tar.h"
-
-#define RECORDSIZE     (512)
-#define BLOCKSIZE      (RECORDSIZE * 20)
-
-static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]";
-
-static char block[BLOCKSIZE];
-static unsigned long offset;
-
-static time_t archive_time;
-
-/* tries hard to write, either succeeds or dies in the attempt */
-static void reliable_write(void *buf, unsigned long size)
-{
-       while (size > 0) {
-               long ret = xwrite(1, buf, size);
-               if (ret < 0) {
-                       if (errno == EPIPE)
-                               exit(0);
-                       die("git-tar-tree: %s", strerror(errno));
-               } else if (!ret) {
-                       die("git-tar-tree: disk full?");
-               }
-               size -= ret;
-               buf += ret;
-       }
-}
-
-/* writes out the whole block, but only if it is full */
-static void write_if_needed(void)
-{
-       if (offset == BLOCKSIZE) {
-               reliable_write(block, BLOCKSIZE);
-               offset = 0;
-       }
-}
-
-/* acquire the next record from the buffer; user must call write_if_needed() */
-static char *get_record(void)
-{
-       char *p = block + offset;
-       memset(p, 0, RECORDSIZE);
-       offset += RECORDSIZE;
-       return p;
-}
-
-/*
- * The end of tar archives is marked by 1024 nul bytes and after that
- * follows the rest of the block (if any).
- */
-static void write_trailer(void)
-{
-       get_record();
-       write_if_needed();
-       get_record();
-       write_if_needed();
-       while (offset) {
-               get_record();
-               write_if_needed();
-       }
-}
-
-/*
- * queues up writes, so that all our write(2) calls write exactly one
- * full block; pads writes to RECORDSIZE
- */
-static void write_blocked(void *buf, unsigned long size)
-{
-       unsigned long tail;
-
-       if (offset) {
-               unsigned long chunk = BLOCKSIZE - offset;
-               if (size < chunk)
-                       chunk = size;
-               memcpy(block + offset, buf, chunk);
-               size -= chunk;
-               offset += chunk;
-               buf += chunk;
-               write_if_needed();
-       }
-       while (size >= BLOCKSIZE) {
-               reliable_write(buf, BLOCKSIZE);
-               size -= BLOCKSIZE;
-               buf += BLOCKSIZE;
-       }
-       if (size) {
-               memcpy(block + offset, buf, size);
-               offset += size;
-       }
-       tail = offset % RECORDSIZE;
-       if (tail)  {
-               memset(block + offset, 0, RECORDSIZE - tail);
-               offset += RECORDSIZE - tail;
-       }
-       write_if_needed();
-}
-
-static void strbuf_append_string(struct strbuf *sb, const char *s)
-{
-       int slen = strlen(s);
-       int total = sb->len + slen;
-       if (total > sb->alloc) {
-               sb->buf = xrealloc(sb->buf, total);
-               sb->alloc = total;
-       }
-       memcpy(sb->buf + sb->len, s, slen);
-       sb->len = total;
-}
-
-/*
- * pax extended header records have the format "%u %s=%s\n".  %u contains
- * the size of the whole string (including the %u), the first %s is the
- * keyword, the second one is the value.  This function constructs such a
- * string and appends it to a struct strbuf.
- */
-static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
-                                     const char *value, unsigned int valuelen)
-{
-       char *p;
-       int len, total, tmp;
-
-       /* "%u %s=%s\n" */
-       len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
-       for (tmp = len; tmp > 9; tmp /= 10)
-               len++;
-
-       total = sb->len + len;
-       if (total > sb->alloc) {
-               sb->buf = xrealloc(sb->buf, total);
-               sb->alloc = total;
-       }
-
-       p = sb->buf;
-       p += sprintf(p, "%u %s=", len, keyword);
-       memcpy(p, value, valuelen);
-       p += valuelen;
-       *p = '\n';
-       sb->len = total;
-}
-
-static unsigned int ustar_header_chksum(const struct ustar_header *header)
-{
-       char *p = (char *)header;
-       unsigned int chksum = 0;
-       while (p < header->chksum)
-               chksum += *p++;
-       chksum += sizeof(header->chksum) * ' ';
-       p += sizeof(header->chksum);
-       while (p < (char *)header + sizeof(struct ustar_header))
-               chksum += *p++;
-       return chksum;
-}
-
-static int get_path_prefix(const struct strbuf *path, int maxlen)
-{
-       int i = path->len;
-       if (i > maxlen)
-               i = maxlen;
-       while (i > 0 && path->buf[i] != '/')
-               i--;
-       return i;
-}
-
-static void write_entry(const unsigned char *sha1, struct strbuf *path,
-                        unsigned int mode, void *buffer, unsigned long size)
-{
-       struct ustar_header header;
-       struct strbuf ext_header;
-
-       memset(&header, 0, sizeof(header));
-       ext_header.buf = NULL;
-       ext_header.len = ext_header.alloc = 0;
-
-       if (!sha1) {
-               *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
-               mode = 0100666;
-               strcpy(header.name, "pax_global_header");
-       } else if (!path) {
-               *header.typeflag = TYPEFLAG_EXT_HEADER;
-               mode = 0100666;
-               sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
-       } else {
-               if (S_ISDIR(mode)) {
-                       *header.typeflag = TYPEFLAG_DIR;
-                       mode |= 0777;
-               } else if (S_ISLNK(mode)) {
-                       *header.typeflag = TYPEFLAG_LNK;
-                       mode |= 0777;
-               } else if (S_ISREG(mode)) {
-                       *header.typeflag = TYPEFLAG_REG;
-                       mode |= (mode & 0100) ? 0777 : 0666;
-               } else {
-                       error("unsupported file mode: 0%o (SHA1: %s)",
-                             mode, sha1_to_hex(sha1));
-                       return;
-               }
-               if (path->len > sizeof(header.name)) {
-                       int plen = get_path_prefix(path, sizeof(header.prefix));
-                       int rest = path->len - plen - 1;
-                       if (plen > 0 && rest <= sizeof(header.name)) {
-                               memcpy(header.prefix, path->buf, plen);
-                               memcpy(header.name, path->buf + plen + 1, rest);
-                       } else {
-                               sprintf(header.name, "%s.data",
-                                       sha1_to_hex(sha1));
-                               strbuf_append_ext_header(&ext_header, "path",
-                                                        path->buf, path->len);
-                       }
-               } else
-                       memcpy(header.name, path->buf, path->len);
-       }
-
-       if (S_ISLNK(mode) && buffer) {
-               if (size > sizeof(header.linkname)) {
-                       sprintf(header.linkname, "see %s.paxheader",
-                               sha1_to_hex(sha1));
-                       strbuf_append_ext_header(&ext_header, "linkpath",
-                                                buffer, size);
-               } else
-                       memcpy(header.linkname, buffer, size);
-       }
-
-       sprintf(header.mode, "%07o", mode & 07777);
-       sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
-       sprintf(header.mtime, "%011lo", archive_time);
-
-       /* XXX: should we provide more meaningful info here? */
-       sprintf(header.uid, "%07o", 0);
-       sprintf(header.gid, "%07o", 0);
-       strncpy(header.uname, "git", 31);
-       strncpy(header.gname, "git", 31);
-       sprintf(header.devmajor, "%07o", 0);
-       sprintf(header.devminor, "%07o", 0);
-
-       memcpy(header.magic, "ustar", 6);
-       memcpy(header.version, "00", 2);
-
-       sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
-
-       if (ext_header.len > 0) {
-               write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
-               free(ext_header.buf);
-       }
-       write_blocked(&header, sizeof(header));
-       if (S_ISREG(mode) && buffer && size > 0)
-               write_blocked(buffer, size);
-}
-
-static void write_global_extended_header(const unsigned char *sha1)
-{
-       struct strbuf ext_header;
-       ext_header.buf = NULL;
-       ext_header.len = ext_header.alloc = 0;
-       strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
-       write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
-       free(ext_header.buf);
-}
-
-static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
-{
-       int pathlen = path->len;
-
-       while (tree->size) {
-               const char *name;
-               const unsigned char *sha1;
-               unsigned mode;
-               void *eltbuf;
-               char elttype[20];
-               unsigned long eltsize;
-
-               sha1 = tree_entry_extract(tree, &name, &mode);
-               update_tree_entry(tree);
-
-               eltbuf = read_sha1_file(sha1, elttype, &eltsize);
-               if (!eltbuf)
-                       die("cannot read %s", sha1_to_hex(sha1));
-
-               path->len = pathlen;
-               strbuf_append_string(path, name);
-               if (S_ISDIR(mode))
-                       strbuf_append_string(path, "/");
-
-               write_entry(sha1, path, mode, eltbuf, eltsize);
-
-               if (S_ISDIR(mode)) {
-                       struct tree_desc subtree;
-                       subtree.buf = eltbuf;
-                       subtree.size = eltsize;
-                       traverse_tree(&subtree, path);
-               }
-               free(eltbuf);
-       }
-}
-
-int main(int argc, char **argv)
-{
-       unsigned char sha1[20], tree_sha1[20];
-       struct commit *commit;
-       struct tree_desc tree;
-       struct strbuf current_path;
-
-       current_path.buf = xmalloc(PATH_MAX);
-       current_path.alloc = PATH_MAX;
-       current_path.len = current_path.eof = 0;
-
-       setup_git_directory();
-       git_config(git_default_config);
-
-       switch (argc) {
-       case 3:
-               strbuf_append_string(&current_path, argv[2]);
-               strbuf_append_string(&current_path, "/");
-               /* FALLTHROUGH */
-       case 2:
-               if (get_sha1(argv[1], sha1) < 0)
-                       usage(tar_tree_usage);
-               break;
-       default:
-               usage(tar_tree_usage);
-       }
-
-       commit = lookup_commit_reference_gently(sha1, 1);
-       if (commit) {
-               write_global_extended_header(commit->object.sha1);
-               archive_time = commit->date;
-       } else
-               archive_time = time(NULL);
-
-       tree.buf = read_object_with_reference(sha1, tree_type, &tree.size,
-                                             tree_sha1);
-       if (!tree.buf)
-               die("not a reference to a tag, commit or tree object: %s",
-                   sha1_to_hex(sha1));
-
-       if (current_path.len > 0)
-               write_entry(tree_sha1, &current_path, 040777, NULL, 0);
-       traverse_tree(&tree, &current_path);
-       write_trailer();
-       free(current_path.buf);
-       return 0;
-}
index 9f7abb7..297c697 100644 (file)
@@ -37,7 +37,7 @@ static void entry_extract(struct tree_desc *t, struct name_entry *a)
 
 void update_tree_entry(struct tree_desc *desc)
 {
-       void *buf = desc->buf;
+       const void *buf = desc->buf;
        unsigned long size = desc->size;
        int len = strlen(buf) + 1 + 20;
 
@@ -47,22 +47,66 @@ void update_tree_entry(struct tree_desc *desc)
        desc->size = size - len;
 }
 
+static const char *get_mode(const char *str, unsigned int *modep)
+{
+       unsigned char c;
+       unsigned int mode = 0;
+
+       while ((c = *str++) != ' ') {
+               if (c < '0' || c > '7')
+                       return NULL;
+               mode = (mode << 3) + (c - '0');
+       }
+       *modep = mode;
+       return str;
+}
+
 const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
 {
-       void *tree = desc->buf;
+       const void *tree = desc->buf;
        unsigned long size = desc->size;
        int len = strlen(tree)+1;
        const unsigned char *sha1 = tree + len;
-       const char *path = strchr(tree, ' ');
+       const char *path;
        unsigned int mode;
 
-       if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
+       path = get_mode(tree, &mode);
+       if (!path || size < len + 20)
                die("corrupt tree file");
-       *pathp = path+1;
+       *pathp = path;
        *modep = canon_mode(mode);
        return sha1;
 }
 
+int tree_entry(struct tree_desc *desc, struct name_entry *entry)
+{
+       const void *tree = desc->buf, *path;
+       unsigned long len, size = desc->size;
+
+       if (!size)
+               return 0;
+
+       path = get_mode(tree, &entry->mode);
+       if (!path)
+               die("corrupt tree file");
+
+       entry->path = path;
+       len = strlen(path);
+       entry->pathlen = len;
+
+       path += len + 1;
+       entry->sha1 = path;
+
+       path += 20;
+       len = path - tree;
+       if (len > size)
+               die("corrupt tree file");
+
+       desc->buf = path;
+       desc->size = size - len;
+       return 1;
+}
+
 void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
 {
        struct name_entry *entry = xmalloc(n*sizeof(*entry));
index 47438fe..e57befa 100644 (file)
@@ -2,7 +2,7 @@
 #define TREE_WALK_H
 
 struct tree_desc {
-       void *buf;
+       const void *buf;
        unsigned long size;
 };
 
@@ -16,6 +16,9 @@ struct name_entry {
 void update_tree_entry(struct tree_desc *);
 const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);
 
+/* Helper function that does both of the above and returns true for success */
+int tree_entry(struct tree_desc *, struct name_entry *);
+
 void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1);
 
 typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry *entry, const char *base);
diff --git a/tree.c b/tree.c
index d599fb5..9bbe2da 100644 (file)
--- a/tree.c
+++ b/tree.c
@@ -3,11 +3,12 @@
 #include "blob.h"
 #include "commit.h"
 #include "tag.h"
+#include "tree-walk.h"
 #include <stdlib.h>
 
 const char *tree_type = "tree";
 
-static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
+static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
 {
        int len;
        unsigned int size;
@@ -77,19 +78,20 @@ int read_tree_recursive(struct tree *tree,
                        int stage, const char **match,
                        read_tree_fn_t fn)
 {
-       struct tree_entry_list *list;
+       struct tree_desc desc;
+       struct name_entry entry;
+
        if (parse_tree(tree))
                return -1;
-       list = tree->entries;
-       while (list) {
-               struct tree_entry_list *current = list;
-               list = list->next;
-               if (!match_tree_entry(base, baselen, current->name,
-                                     current->mode, match))
+
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+
+       while (tree_entry(&desc, &entry)) {
+               if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
                        continue;
 
-               switch (fn(current->item.any->sha1, base, baselen,
-                          current->name, current->mode, stage)) {
+               switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) {
                case 0:
                        continue;
                case READ_TREE_RECURSIVE:
@@ -97,18 +99,17 @@ int read_tree_recursive(struct tree *tree,
                default:
                        return -1;
                }
-               if (current->directory) {
+               if (S_ISDIR(entry.mode)) {
                        int retval;
-                       int pathlen = strlen(current->name);
                        char *newbase;
 
-                       newbase = xmalloc(baselen + 1 + pathlen);
+                       newbase = xmalloc(baselen + 1 + entry.pathlen);
                        memcpy(newbase, base, baselen);
-                       memcpy(newbase + baselen, current->name, pathlen);
-                       newbase[baselen + pathlen] = '/';
-                       retval = read_tree_recursive(current->item.tree,
+                       memcpy(newbase + baselen, entry.path, entry.pathlen);
+                       newbase[baselen + entry.pathlen] = '/';
+                       retval = read_tree_recursive(lookup_tree(entry.sha1),
                                                     newbase,
-                                                    baselen + pathlen + 1,
+                                                    baselen + entry.pathlen + 1,
                                                     stage, match, fn);
                        free(newbase);
                        if (retval)
@@ -143,61 +144,49 @@ struct tree *lookup_tree(const unsigned char *sha1)
        return (struct tree *) obj;
 }
 
-int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
+static int track_tree_refs(struct tree *item)
 {
-       void *bufptr = buffer;
-       struct tree_entry_list **list_p;
-       int n_refs = 0;
+       int n_refs = 0, i;
+       struct object_refs *refs;
+       struct tree_desc desc;
+       struct name_entry entry;
+
+       /* Count how many entries there are.. */
+       desc.buf = item->buffer;
+       desc.size = item->size;
+       while (desc.size) {
+               n_refs++;
+               update_tree_entry(&desc);
+       }
 
-       if (item->object.parsed)
-               return 0;
-       item->object.parsed = 1;
-       list_p = &item->entries;
-       while (size) {
+       /* Allocate object refs and walk it again.. */
+       i = 0;
+       refs = alloc_object_refs(n_refs);
+       desc.buf = item->buffer;
+       desc.size = item->size;
+       while (tree_entry(&desc, &entry)) {
                struct object *obj;
-               struct tree_entry_list *entry;
-               int len = 1+strlen(bufptr);
-               unsigned char *file_sha1 = bufptr + len;
-               char *path = strchr(bufptr, ' ');
-               unsigned int mode;
-               if (size < len + 20 || !path || 
-                   sscanf(bufptr, "%o", &mode) != 1)
-                       return -1;
 
-               entry = xmalloc(sizeof(struct tree_entry_list));
-               entry->name = strdup(path + 1);
-               entry->directory = S_ISDIR(mode) != 0;
-               entry->executable = (mode & S_IXUSR) != 0;
-               entry->symlink = S_ISLNK(mode) != 0;
-               entry->zeropad = *(char *)bufptr == '0';
-               entry->mode = mode;
-               entry->next = NULL;
-
-               bufptr += len + 20;
-               size -= len + 20;
-
-               if (entry->directory) {
-                       entry->item.tree = lookup_tree(file_sha1);
-                       obj = &entry->item.tree->object;
-               } else {
-                       entry->item.blob = lookup_blob(file_sha1);
-                       obj = &entry->item.blob->object;
-               }
-               if (obj)
-                       n_refs++;
-               *list_p = entry;
-               list_p = &entry->next;
+               if (S_ISDIR(entry.mode))
+                       obj = &lookup_tree(entry.sha1)->object;
+               else
+                       obj = &lookup_blob(entry.sha1)->object;
+               refs->ref[i++] = obj;
        }
+       set_object_refs(&item->object, refs);
+       return 0;
+}
 
-       if (track_object_refs) {
-               struct tree_entry_list *entry;
-               unsigned i = 0;
-               struct object_refs *refs = alloc_object_refs(n_refs);
-               for (entry = item->entries; entry; entry = entry->next)
-                       refs->ref[i++] = entry->item.any;
-               set_object_refs(&item->object, refs);
-       }
+int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
+{
+       if (item->object.parsed)
+               return 0;
+       item->object.parsed = 1;
+       item->buffer = buffer;
+       item->size = size;
 
+       if (track_object_refs)
+               track_tree_refs(item);
        return 0;
 }
 
@@ -206,7 +195,6 @@ int parse_tree(struct tree *item)
         char type[20];
         void *buffer;
         unsigned long size;
-        int ret;
 
        if (item->object.parsed)
                return 0;
@@ -219,9 +207,7 @@ int parse_tree(struct tree *item)
                return error("Object %s not a tree",
                             sha1_to_hex(item->object.sha1));
        }
-       ret = parse_tree_buffer(item, buffer, size);
-       free(buffer);
-       return ret;
+       return parse_tree_buffer(item, buffer, size);
 }
 
 struct tree *parse_tree_indirect(const unsigned char *sha1)
diff --git a/tree.h b/tree.h
index 330ab64..dd25c53 100644 (file)
--- a/tree.h
+++ b/tree.h
@@ -5,24 +5,10 @@
 
 extern const char *tree_type;
 
-struct tree_entry_list {
-       struct tree_entry_list *next;
-       unsigned directory : 1;
-       unsigned executable : 1;
-       unsigned symlink : 1;
-       unsigned zeropad : 1;
-       unsigned int mode;
-       char *name;
-       union {
-               struct object *any;
-               struct tree *tree;
-               struct blob *blob;
-       } item;
-};
-
 struct tree {
        struct object object;
-       struct tree_entry_list *entries;
+       void *buffer;
+       unsigned long size;
 };
 
 struct tree *lookup_tree(const unsigned char *sha1);
@@ -35,7 +21,7 @@ int parse_tree(struct tree *tree);
 struct tree *parse_tree_indirect(const unsigned char *sha1);
 
 #define READ_TREE_RECURSIVE 1
-typedef int (*read_tree_fn_t)(unsigned char *, const char *, int, const char *, unsigned int, int);
+typedef int (*read_tree_fn_t)(const unsigned char *, const char *, int, const char *, unsigned int, int);
 
 extern int read_tree_recursive(struct tree *tree,
                               const char *base, int baselen,
index 23a8562..ccddf1d 100644 (file)
@@ -27,8 +27,10 @@ int main(int argc, char **argv)
 {
        unsigned char sha1[20];
 
-       if (argc != 2 || get_sha1(argv[1], sha1))
+       if (argc != 2)
                usage("git-unpack-file <sha1>");
+       if (get_sha1(argv[1], sha1))
+               die("Not a valid object name %s", argv[1]);
 
        setup_git_directory();
        git_config(git_default_config);
index 1c1f13b..fbccc4a 100644 (file)
@@ -19,9 +19,6 @@
 static int allow_add;
 static int allow_remove;
 static int allow_replace;
-static int allow_unmerged; /* --refresh needing merge is not error */
-static int not_new; /* --refresh not having working tree files is not error */
-static int quiet; /* --refresh needing update is not error */
 static int info_only;
 static int force_remove;
 static int verbose;
@@ -29,23 +26,6 @@ static int mark_valid_only = 0;
 #define MARK_VALID 1
 #define UNMARK_VALID 2
 
-
-/* Three functions to allow overloaded pointer return; see linux/err.h */
-static inline void *ERR_PTR(long error)
-{
-       return (void *) error;
-}
-
-static inline long PTR_ERR(const void *ptr)
-{
-       return (long) ptr;
-}
-
-static inline long IS_ERR(const void *ptr)
-{
-       return (unsigned long)ptr > (unsigned long)-1000L;
-}
-
 static void report(const char *fmt, ...)
 {
        va_list vp;
@@ -148,167 +128,6 @@ static int add_file_to_cache(const char *path)
        return 0;
 }
 
-/*
- * "refresh" does not calculate a new sha1 file or bring the
- * cache up-to-date for mode/content changes. But what it
- * _does_ do is to "re-match" the stat information of a file
- * with the cache, so that you can refresh the cache for a
- * file that hasn't been changed but where the stat entry is
- * out of date.
- *
- * For example, you'd want to do this after doing a "git-read-tree",
- * to link up the stat cache details with the proper files.
- */
-static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
-{
-       struct stat st;
-       struct cache_entry *updated;
-       int changed, size;
-
-       if (lstat(ce->name, &st) < 0)
-               return ERR_PTR(-errno);
-
-       changed = ce_match_stat(ce, &st, really);
-       if (!changed) {
-               if (really && assume_unchanged &&
-                   !(ce->ce_flags & htons(CE_VALID)))
-                       ; /* mark this one VALID again */
-               else
-                       return NULL;
-       }
-
-       if (ce_modified(ce, &st, really))
-               return ERR_PTR(-EINVAL);
-
-       size = ce_size(ce);
-       updated = xmalloc(size);
-       memcpy(updated, ce, size);
-       fill_stat_cache_info(updated, &st);
-
-       /* In this case, if really is not set, we should leave
-        * CE_VALID bit alone.  Otherwise, paths marked with
-        * --no-assume-unchanged (i.e. things to be edited) will
-        * reacquire CE_VALID bit automatically, which is not
-        * really what we want.
-        */
-       if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
-               updated->ce_flags &= ~htons(CE_VALID);
-
-       return updated;
-}
-
-static int refresh_cache(int really)
-{
-       int i;
-       int has_errors = 0;
-
-       for (i = 0; i < active_nr; i++) {
-               struct cache_entry *ce, *new;
-               ce = active_cache[i];
-               if (ce_stage(ce)) {
-                       while ((i < active_nr) &&
-                              ! strcmp(active_cache[i]->name, ce->name))
-                               i++;
-                       i--;
-                       if (allow_unmerged)
-                               continue;
-                       printf("%s: needs merge\n", ce->name);
-                       has_errors = 1;
-                       continue;
-               }
-
-               new = refresh_entry(ce, really);
-               if (!new)
-                       continue;
-               if (IS_ERR(new)) {
-                       if (not_new && PTR_ERR(new) == -ENOENT)
-                               continue;
-                       if (really && PTR_ERR(new) == -EINVAL) {
-                               /* If we are doing --really-refresh that
-                                * means the index is not valid anymore.
-                                */
-                               ce->ce_flags &= ~htons(CE_VALID);
-                               active_cache_changed = 1;
-                       }
-                       if (quiet)
-                               continue;
-                       printf("%s: needs update\n", ce->name);
-                       has_errors = 1;
-                       continue;
-               }
-               active_cache_changed = 1;
-               /* You can NOT just free active_cache[i] here, since it
-                * might not be necessarily malloc()ed but can also come
-                * from mmap(). */
-               active_cache[i] = new;
-       }
-       return has_errors;
-}
-
-/*
- * We fundamentally don't like some paths: we don't want
- * dot or dot-dot anywhere, and for obvious reasons don't
- * want to recurse into ".git" either.
- *
- * Also, we don't want double slashes or slashes at the
- * end that can make pathnames ambiguous.
- */
-static int verify_dotfile(const char *rest)
-{
-       /*
-        * The first character was '.', but that
-        * has already been discarded, we now test
-        * the rest.
-        */
-       switch (*rest) {
-       /* "." is not allowed */
-       case '\0': case '/':
-               return 0;
-
-       /*
-        * ".git" followed by  NUL or slash is bad. This
-        * shares the path end test with the ".." case.
-        */
-       case 'g':
-               if (rest[1] != 'i')
-                       break;
-               if (rest[2] != 't')
-                       break;
-               rest += 2;
-       /* fallthrough */
-       case '.':
-               if (rest[1] == '\0' || rest[1] == '/')
-                       return 0;
-       }
-       return 1;
-}
-
-static int verify_path(const char *path)
-{
-       char c;
-
-       goto inside;
-       for (;;) {
-               if (!c)
-                       return 1;
-               if (c == '/') {
-inside:
-                       c = *path++;
-                       switch (c) {
-                       default:
-                               continue;
-                       case '/': case '\0':
-                               break;
-                       case '.':
-                               if (verify_dotfile(path))
-                                       continue;
-                       }
-                       return 0;
-               }
-               c = *path++;
-       }
-}
-
 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
                         const char *path, int stage)
 {
@@ -367,19 +186,19 @@ static void chmod_path(int flip, const char *path)
        die("git-update-index: cannot chmod %cx '%s'", flip, path);
 }
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 static void update_one(const char *path, const char *prefix, int prefix_length)
 {
        const char *p = prefix_path(prefix, prefix_length, path);
        if (!verify_path(p)) {
                fprintf(stderr, "Ignoring path %s\n", path);
-               return;
+               goto free_return;
        }
        if (mark_valid_only) {
                if (mark_valid(p))
                        die("Unable to mark file %s", path);
-               return;
+               goto free_return;
        }
        cache_tree_invalidate_path(active_cache_tree, path);
 
@@ -387,11 +206,14 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
                if (remove_file_from_cache(p))
                        die("git-update-index: unable to remove %s", path);
                report("remove '%s'", path);
-               return;
+               goto free_return;
        }
        if (add_file_to_cache(p))
                die("Unable to process file %s", path);
        report("add '%s'", path);
+ free_return:
+       if (p < path || p > path + strlen(path))
+               free((char*)p);
 }
 
 static void read_index_info(int line_termination)
@@ -485,7 +307,7 @@ static void read_index_info(int line_termination)
 }
 
 static const char update_index_usage[] =
-"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
+"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again] [--ignore-missing] [-z] [--verbose] [--] <file>...";
 
 static unsigned char head_sha1[20];
 static unsigned char merge_head_sha1[20];
@@ -500,11 +322,13 @@ static struct cache_entry *read_one_ent(const char *which,
        struct cache_entry *ce;
 
        if (get_tree_entry(ent, path, sha1, &mode)) {
-               error("%s: not in %s branch.", path, which);
+               if (which)
+                       error("%s: not in %s branch.", path, which);
                return NULL;
        }
        if (mode == S_IFDIR) {
-               error("%s: not a blob in %s branch.", path, which);
+               if (which)
+                       error("%s: not a blob in %s branch.", path, which);
                return NULL;
        }
        size = cache_entry_size(namelen);
@@ -589,7 +413,8 @@ static void read_head_pointers(void)
        }
 }
 
-static int do_unresolve(int ac, const char **av)
+static int do_unresolve(int ac, const char **av,
+                       const char *prefix, int prefix_length)
 {
        int i;
        int err = 0;
@@ -601,11 +426,57 @@ static int do_unresolve(int ac, const char **av)
 
        for (i = 1; i < ac; i++) {
                const char *arg = av[i];
-               err |= unresolve_one(arg);
+               const char *p = prefix_path(prefix, prefix_length, arg);
+               err |= unresolve_one(p);
+               if (p < arg || p > arg + strlen(arg))
+                       free((char*)p);
        }
        return err;
 }
 
+static int do_reupdate(int ac, const char **av,
+                      const char *prefix, int prefix_length)
+{
+       /* Read HEAD and run update-index on paths that are
+        * merged and already different between index and HEAD.
+        */
+       int pos;
+       int has_head = 1;
+       const char **pathspec = get_pathspec(prefix, av + 1);
+
+       if (read_ref(git_path("HEAD"), head_sha1))
+               /* If there is no HEAD, that means it is an initial
+                * commit.  Update everything in the index.
+                */
+               has_head = 0;
+ redo:
+       for (pos = 0; pos < active_nr; pos++) {
+               struct cache_entry *ce = active_cache[pos];
+               struct cache_entry *old = NULL;
+               int save_nr;
+
+               if (ce_stage(ce) || !ce_path_match(ce, pathspec))
+                       continue;
+               if (has_head)
+                       old = read_one_ent(NULL, head_sha1,
+                                          ce->name, ce_namelen(ce), 0);
+               if (old && ce->ce_mode == old->ce_mode &&
+                   !memcmp(ce->sha1, old->sha1, 20)) {
+                       free(old);
+                       continue; /* unchanged */
+               }
+               /* Be careful.  The working tree may not have the
+                * path anymore, in which case, under 'allow_remove',
+                * or worse yet 'allow_replace', active_nr may decrease.
+                */
+               save_nr = active_nr;
+               update_one(ce->name + prefix_length, prefix, prefix_length);
+               if (save_nr != active_nr)
+                       goto redo;
+       }
+       return 0;
+}
+
 int main(int argc, const char **argv)
 {
        int i, newfd, entries, has_errors = 0, line_termination = '\n';
@@ -614,12 +485,13 @@ int main(int argc, const char **argv)
        const char *prefix = setup_git_directory();
        int prefix_length = prefix ? strlen(prefix) : 0;
        char set_executable_bit = 0;
+       unsigned int refresh_flags = 0;
 
        git_config(git_default_config);
 
-       newfd = hold_index_file_for_update(&cache_file, get_index_file());
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file());
        if (newfd < 0)
-               die("unable to create new cachefile");
+               die("unable to create new index file");
 
        entries = read_cache();
        if (entries < 0)
@@ -634,7 +506,7 @@ int main(int argc, const char **argv)
                                continue;
                        }
                        if (!strcmp(path, "-q")) {
-                               quiet = 1;
+                               refresh_flags |= REFRESH_QUIET;
                                continue;
                        }
                        if (!strcmp(path, "--add")) {
@@ -650,15 +522,15 @@ int main(int argc, const char **argv)
                                continue;
                        }
                        if (!strcmp(path, "--unmerged")) {
-                               allow_unmerged = 1;
+                               refresh_flags |= REFRESH_UNMERGED;
                                continue;
                        }
                        if (!strcmp(path, "--refresh")) {
-                               has_errors |= refresh_cache(0);
+                               has_errors |= refresh_cache(refresh_flags);
                                continue;
                        }
                        if (!strcmp(path, "--really-refresh")) {
-                               has_errors |= refresh_cache(1);
+                               has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
                                continue;
                        }
                        if (!strcmp(path, "--cacheinfo")) {
@@ -717,13 +589,21 @@ int main(int argc, const char **argv)
                                break;
                        }
                        if (!strcmp(path, "--unresolve")) {
-                               has_errors = do_unresolve(argc - i, argv + i);
+                               has_errors = do_unresolve(argc - i, argv + i,
+                                                         prefix, prefix_length);
+                               if (has_errors)
+                                       active_cache_changed = 0;
+                               goto finish;
+                       }
+                       if (!strcmp(path, "--again")) {
+                               has_errors = do_reupdate(argc - i, argv + i,
+                                                        prefix, prefix_length);
                                if (has_errors)
                                        active_cache_changed = 0;
                                goto finish;
                        }
                        if (!strcmp(path, "--ignore-missing")) {
-                               not_new = 1;
+                               refresh_flags |= REFRESH_IGNORE_MISSING;
                                continue;
                        }
                        if (!strcmp(path, "--verbose")) {
@@ -743,6 +623,7 @@ int main(int argc, const char **argv)
                strbuf_init(&buf);
                while (1) {
                        char *path_name;
+                       const char *p;
                        read_line(&buf, stdin, line_termination);
                        if (buf.eof)
                                break;
@@ -750,11 +631,12 @@ int main(int argc, const char **argv)
                                path_name = unquote_c_style(buf.buf, NULL);
                        else
                                path_name = buf.buf;
-                       update_one(path_name, prefix, prefix_length);
-                       if (set_executable_bit) {
-                               const char *p = prefix_path(prefix, prefix_length, path_name);
+                       p = prefix_path(prefix, prefix_length, path_name);
+                       update_one(p, NULL, 0);
+                       if (set_executable_bit)
                                chmod_path(set_executable_bit, p);
-                       }
+                       if (p < path_name || p > path_name + strlen(path_name))
+                               free((char*) p);
                        if (path_name != buf.buf)
                                free(path_name);
                }
@@ -763,8 +645,8 @@ int main(int argc, const char **argv)
  finish:
        if (active_cache_changed) {
                if (write_cache(newfd, active_cache, active_nr) ||
-                   commit_index_file(&cache_file))
-                       die("Unable to write new cachefile");
+                   commit_lock_file(&lock_file))
+                       die("Unable to write new index file");
        }
 
        return has_errors ? 1 : 0;
index ba4bf51..a1e6bb9 100644 (file)
@@ -1,85 +1,56 @@
 #include "cache.h"
 #include "refs.h"
 
-static const char git_update_ref_usage[] = "git-update-ref <refname> <value> [<oldval>]";
-
-static int re_verify(const char *path, unsigned char *oldsha1, unsigned char *currsha1)
-{
-       char buf[40];
-       int fd = open(path, O_RDONLY), nr;
-       if (fd < 0)
-               return -1;
-       nr = read(fd, buf, 40);
-       close(fd);
-       if (nr != 40 || get_sha1_hex(buf, currsha1) < 0)
-               return -1;
-       return memcmp(oldsha1, currsha1, 20) ? -1 : 0;
-}
+static const char git_update_ref_usage[] =
+"git-update-ref <refname> <value> [<oldval>] [-m <reason>]";
 
 int main(int argc, char **argv)
 {
-       char *hex;
-       const char *refname, *value, *oldval, *path;
-       char *lockpath;
-       unsigned char sha1[20], oldsha1[20], currsha1[20];
-       int fd, written;
+       const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL;
+       struct ref_lock *lock;
+       unsigned char sha1[20], oldsha1[20];
+       int i;
 
        setup_git_directory();
        git_config(git_default_config);
-       if (argc < 3 || argc > 4)
+
+       for (i = 1; i < argc; i++) {
+               if (!strcmp("-m", argv[i])) {
+                       if (i+1 >= argc)
+                               usage(git_update_ref_usage);
+                       msg = argv[++i];
+                       if (!*msg)
+                               die("Refusing to perform update with empty message.");
+                       if (strchr(msg, '\n'))
+                               die("Refusing to perform update with \\n in message.");
+                       continue;
+               }
+               if (!refname) {
+                       refname = argv[i];
+                       continue;
+               }
+               if (!value) {
+                       value = argv[i];
+                       continue;
+               }
+               if (!oldval) {
+                       oldval = argv[i];
+                       continue;
+               }
+       }
+       if (!refname || !value)
                usage(git_update_ref_usage);
 
-       refname = argv[1];
-       value = argv[2];
-       oldval = argv[3];
-       if (get_sha1(value, sha1) < 0)
+       if (get_sha1(value, sha1))
                die("%s: not a valid SHA1", value);
        memset(oldsha1, 0, 20);
-       if (oldval && get_sha1(oldval, oldsha1) < 0)
+       if (oldval && get_sha1(oldval, oldsha1))
                die("%s: not a valid old SHA1", oldval);
 
-       path = resolve_ref(git_path("%s", refname), currsha1, !!oldval);
-       if (!path)
-               die("No such ref: %s", refname);
-
-       if (oldval) {
-               if (memcmp(currsha1, oldsha1, 20))
-                       die("Ref %s is at %s but expected %s", refname, sha1_to_hex(currsha1), sha1_to_hex(oldsha1));
-               /* Nothing to do? */
-               if (!memcmp(oldsha1, sha1, 20))
-                       exit(0);
-       }
-       path = strdup(path);
-       lockpath = mkpath("%s.lock", path);
-       if (safe_create_leading_directories(lockpath) < 0)
-               die("Unable to create all of %s", lockpath);
-
-       fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
-       if (fd < 0)
-               die("Unable to create %s", lockpath);
-       hex = sha1_to_hex(sha1);
-       hex[40] = '\n';
-       written = write(fd, hex, 41);
-       close(fd);
-       if (written != 41) {
-               unlink(lockpath);
-               die("Unable to write to %s", lockpath);
-       }
-
-       /*
-        * Re-read the ref after getting the lock to verify
-        */
-       if (oldval && re_verify(path, oldsha1, currsha1) < 0) {
-               unlink(lockpath);
-               die("Ref lock failed");
-       }
-
-       /*
-        * Finally, replace the old ref with the new one
-        */
-       if (rename(lockpath, path) < 0) {
-               unlink(lockpath);
-               die("Unable to create %s", path);
-       }
+       lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, 0);
+       if (!lock)
+               return 1;
+       if (write_ref_sha1(lock, sha1, msg) < 0)
+               return 1;
        return 0;
 }
index 895e7a3..bd07da6 100644 (file)
@@ -13,7 +13,7 @@ static char *prefix = NULL;
 static const char write_tree_usage[] =
 "git-write-tree [--missing-ok] [--prefix=<prefix>/]";
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 int main(int argc, char **argv)
 {
@@ -21,7 +21,7 @@ int main(int argc, char **argv)
 
        setup_git_directory();
 
-       newfd = hold_index_file_for_update(&cache_file, get_index_file());
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file());
        entries = read_cache();
 
        while (1 < argc) {
@@ -52,7 +52,7 @@ int main(int argc, char **argv)
                        die("git-write-tree: error building trees");
                if (0 <= newfd) {
                        if (!write_cache(newfd, active_cache, active_nr))
-                               commit_index_file(&cache_file);
+                               commit_lock_file(&lock_file);
                }
                /* Not being able to write is fine -- we are only interested
                 * in updating the cache-tree part, and if the next caller