Merge branch 'js/color-diff'
authorJunio C Hamano <junkio@cox.net>
Sun, 13 Aug 2006 02:24:47 +0000 (19:24 -0700)
committerJunio C Hamano <junkio@cox.net>
Sun, 13 Aug 2006 02:24:47 +0000 (19:24 -0700)
55 files changed:
.gitignore
Documentation/git-grep.txt
Documentation/git.txt
INSTALL
Makefile
builtin-add.c
builtin-apply.c
builtin-checkout-index.c [new file with mode: 0644]
builtin-count-objects.c [new file with mode: 0644]
builtin-count.c [deleted file]
builtin-grep.c
builtin-help.c [deleted file]
builtin-mv.c
builtin-name-rev.c [new file with mode: 0644]
builtin-pack-objects.c [new file with mode: 0644]
builtin-read-tree.c
builtin-rm.c
builtin-symbolic-ref.c [new file with mode: 0644]
builtin-unpack-objects.c [new file with mode: 0644]
builtin-update-index.c
builtin-verify-pack.c [new file with mode: 0644]
builtin-write-tree.c
builtin.h
cache.h
checkout-index.c [deleted file]
config.mak.in
configure.ac
convert-objects.c
git-am.sh
git-compat-util.h
git-sh-setup.sh
git-svn.perl
git.c
gitk
gitweb/README
gitweb/git-logo.png [new file with mode: 0644]
gitweb/gitweb.cgi [deleted file]
gitweb/gitweb.css
gitweb/gitweb.perl [new file with mode: 0755]
help.c [new file with mode: 0644]
http-fetch.c
http-push.c
index-pack.c
local-fetch.c
lockfile.c
name-rev.c [deleted file]
pack-objects.c [deleted file]
pager.c
refs.c
sha1_file.c
symbolic-ref.c [deleted file]
t/t4013-diff-various.sh
t/t7002-grep.sh
unpack-objects.c [deleted file]
verify-pack.c [deleted file]
index fb0fa3f16a7c8a42c5d0d1be62666cc4ea4c6575..55cd9844d61e202670076968b20dcb0fda68732e 100644 (file)
@@ -125,6 +125,7 @@ git-verify-tag
 git-whatchanged
 git-write-tree
 git-core-*/?*
+gitweb/gitweb.cgi
 test-date
 test-delta
 test-dump-cache-tree
@@ -140,7 +141,7 @@ config.mak
 autom4te.cache
 config.log
 config.status
-config.mak.in
 config.mak.autogen
+config.mak.append
 configure
 git-blame
index dc7683383c3b4dc4c6bede834155ae56132aafc3..7545dd9a3e6c3cc16fd46e44830af4767978d2be 100644 (file)
@@ -11,7 +11,7 @@ SYNOPSIS
 [verse]
 'git-grep' [--cached]
           [-a | --text] [-I] [-i | --ignore-case] [-w | --word-regexp]
-          [-v | --invert-match]
+          [-v | --invert-match] [--full-name]
           [-E | --extended-regexp] [-G | --basic-regexp] [-F | --fixed-strings]
           [-n] [-l | --files-with-matches] [-L | --files-without-match]
           [-c | --count]
@@ -47,6 +47,12 @@ OPTIONS
 -v | --invert-match::
        Select non-matching lines.
 
+--full-name::
+       When run from a subdirectory, the command usually
+       outputs paths relative to the current directory.  This
+       option forces paths to be output relative to the project
+       top directory.
+
 -E | --extended-regexp | -G | --basic-regexp::
        Use POSIX extended/basic regexp for patterns.  Default
        is to use basic regexp.
index bcf187a11cfaf754ecb93a2210139dd88f6a9d32..3de5fa9c82de4d49a041480ce6681f69a97f167c 100644 (file)
@@ -633,6 +633,9 @@ git Diffs
 
 other
 ~~~~~
+'GIT_PAGER'::
+       This environment variable overrides `$PAGER`.
+
 'GIT_TRACE'::
        If this variable is set git will print `trace:` messages on
        stderr telling about alias expansion, built-in command
diff --git a/INSTALL b/INSTALL
index ba9778cd4de187878b6445be5fbf490d76879f30..fa9bf74a208697bd4c6cd5c91f3335b2f5475a49 100644 (file)
--- a/INSTALL
+++ b/INSTALL
@@ -16,7 +16,7 @@ install" would not work.
 Alternatively you can use autoconf generated ./configure script to
 set up install paths (via config.mak.autogen), so you can write instead
 
-       $ autoconf ;# as yourself if ./configure doesn't exist yet
+       $ make configure ;# as yourself
        $ ./configure --prefix=/usr ;# as yourself
        $ make all doc ;# as yourself
        # make install install-doc ;# as root
index 0761d6c6eddd7894f2d1625cfed0ef2623661a32..5f4bb8d8f8963b31a1a8e48c2f6fb6ed01db3673 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -27,7 +27,7 @@ all:
 # Define NO_C99_FORMAT if your formatted IO functions (printf/scanf et.al.)
 # do not support the 'size specifiers' introduced by C99, namely ll, hh,
 # j, z, t. (representing long long int, char, intmax_t, size_t, ptrdiff_t).
-# some c compilers supported these specifiers prior to C99 as an extension.
+# some C compilers supported these specifiers prior to C99 as an extension.
 #
 # Define NO_STRCASESTR if you don't have strcasestr.
 #
@@ -121,6 +121,15 @@ template_dir = $(prefix)/share/git-core/templates/
 GIT_PYTHON_DIR = $(prefix)/share/git-core/python
 # DESTDIR=
 
+# default configuration for gitweb
+GITWEB_CONFIG = gitweb_config.perl
+GITWEB_SITENAME =
+GITWEB_PROJECTROOT = /pub/git
+GITWEB_LIST =
+GITWEB_HOMETEXT = indextext.html
+GITWEB_CSS = gitweb.css
+GITWEB_LOGO = git-logo.png
+
 export prefix bindir gitexecdir template_dir GIT_PYTHON_DIR
 
 CC = gcc
@@ -173,32 +182,23 @@ SIMPLE_PROGRAMS = \
 
 # ... and all the rest that could be moved out of bindir to gitexecdir
 PROGRAMS = \
-       git-checkout-index$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-merge-base$X \
-       git-merge-index$X git-mktag$X git-mktree$X git-pack-objects$X git-patch-id$X \
+       git-merge-index$X git-mktag$X git-mktree$X git-patch-id$X \
        git-peek-remote$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-unpack-file$X \
-       git-unpack-objects$X git-update-server-info$X \
+       git-update-server-info$X \
        git-upload-pack$X git-verify-pack$X \
-       git-symbolic-ref$X \
-       git-name-rev$X git-pack-redundant$X git-var$X \
+       git-pack-redundant$X git-var$X \
        git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
 
-BUILT_INS = git-log$X git-whatchanged$X git-show$X git-update-ref$X \
-       git-count-objects$X git-diff$X git-push$X git-mailsplit$X \
-       git-grep$X git-add$X git-rm$X git-rev-list$X git-stripspace$X \
-       git-check-ref-format$X git-rev-parse$X git-mailinfo$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-write-tree$X \
-       git-apply$X git-show-branch$X git-diff-files$X git-update-index$X \
-       git-diff-index$X git-diff-stages$X git-diff-tree$X git-cat-file$X \
-       git-fmt-merge-msg$X git-prune$X git-mv$X git-prune-packed$X \
-       git-repo-config$X
+BUILT_INS = \
+       git-format-patch$X git-show$X git-whatchanged$X \
+       git-get-tar-commit-id$X \
+       $(patsubst builtin-%.o,git-%$X,$(BUILTIN_OBJS))
 
 # what 'all' will build and 'install' will install, in gitexecdir
 ALL_PROGRAMS = $(PROGRAMS) $(SIMPLE_PROGRAMS) $(SCRIPTS)
@@ -227,7 +227,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 dir.h path-list.h
+       tree-walk.h log-tree.h dir.h path-list.h builtin.h
 
 DIFF_OBJS = \
        diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -242,20 +242,50 @@ LIB_OBJS = \
        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 \
-       alloc.o merge-file.o path-list.o $(DIFF_OBJS)
+       alloc.o merge-file.o path-list.o help.o $(DIFF_OBJS)
 
 BUILTIN_OBJS = \
-       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-update-index.o \
-       builtin-ls-files.o builtin-ls-tree.o builtin-write-tree.o \
-       builtin-read-tree.o builtin-commit-tree.o builtin-mailinfo.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 builtin-mailsplit.o builtin-stripspace.o \
-       builtin-update-ref.o builtin-fmt-merge-msg.o builtin-prune.o \
-       builtin-mv.o builtin-prune-packed.o builtin-repo-config.o
+       builtin-add.o \
+       builtin-apply.o \
+       builtin-cat-file.o \
+       builtin-checkout-index.o \
+       builtin-check-ref-format.o \
+       builtin-commit-tree.o \
+       builtin-count-objects.o \
+       builtin-diff.o \
+       builtin-diff-files.o \
+       builtin-diff-index.o \
+       builtin-diff-stages.o \
+       builtin-diff-tree.o \
+       builtin-fmt-merge-msg.o \
+       builtin-grep.o \
+       builtin-init-db.o \
+       builtin-log.o \
+       builtin-ls-files.o \
+       builtin-ls-tree.o \
+       builtin-mailinfo.o \
+       builtin-mailsplit.o \
+       builtin-mv.o \
+       builtin-name-rev.o \
+       builtin-pack-objects.o \
+       builtin-prune.o \
+       builtin-prune-packed.o \
+       builtin-push.o \
+       builtin-read-tree.o \
+       builtin-repo-config.o \
+       builtin-rev-list.o \
+       builtin-rev-parse.o \
+       builtin-rm.o \
+       builtin-show-branch.o \
+       builtin-stripspace.o \
+       builtin-symbolic-ref.o \
+       builtin-tar-tree.o \
+       builtin-unpack-objects.o \
+       builtin-update-index.o \
+       builtin-update-ref.o \
+       builtin-upload-tar.o \
+       builtin-verify-pack.o \
+       builtin-write-tree.o
 
 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
 LIBS = $(GITLIBS) -lz
@@ -523,7 +553,7 @@ LIB_OBJS += $(COMPAT_OBJS)
 export prefix TAR INSTALL DESTDIR SHELL_PATH template_dir
 ### Build rules
 
-all: $(ALL_PROGRAMS) $(BUILT_INS) git$X gitk
+all: $(ALL_PROGRAMS) $(BUILT_INS) git$X gitk gitweb/gitweb.cgi
 
 all:
        $(MAKE) -C templates
@@ -536,7 +566,7 @@ git$X: git.c common-cmds.h $(BUILTIN_OBJS) $(GITLIBS) GIT-CFLAGS
                $(ALL_CFLAGS) -o $@ $(filter %.c,$^) \
                $(BUILTIN_OBJS) $(ALL_LDFLAGS) $(LIBS)
 
-builtin-help.o: common-cmds.h
+help.o: common-cmds.h
 
 $(BUILT_INS): git$X
        rm -f $@ && ln git$X $@
@@ -581,6 +611,22 @@ git-status: git-commit
        cp $< $@+
        mv $@+ $@
 
+gitweb/gitweb.cgi: gitweb/gitweb.perl
+       rm -f $@ $@+
+       sed -e '1s|#!.*perl|#!$(PERL_PATH_SQ)|' \
+           -e 's|++GIT_VERSION++|$(GIT_VERSION)|g' \
+           -e 's|++GIT_BINDIR++|$(bindir)|g' \
+           -e 's|++GITWEB_CONFIG++|$(GITWEB_CONFIG)|g' \
+           -e 's|++GITWEB_SITENAME++|$(GITWEB_SITENAME)|g' \
+           -e 's|++GITWEB_PROJECTROOT++|$(GITWEB_PROJECTROOT)|g' \
+           -e 's|++GITWEB_LIST++|$(GITWEB_LIST)|g' \
+           -e 's|++GITWEB_HOMETEXT++|$(GITWEB_HOMETEXT)|g' \
+           -e 's|++GITWEB_CSS++|$(GITWEB_CSS)|g' \
+           -e 's|++GITWEB_LOGO++|$(GITWEB_LOGO)|g' \
+           $< >$@+
+       chmod +x $@+
+       mv $@+ $@
+
 git-instaweb: git-instaweb.sh gitweb/gitweb.cgi gitweb/gitweb.css
        rm -f $@ $@+
        sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
@@ -591,10 +637,17 @@ git-instaweb: git-instaweb.sh gitweb/gitweb.cgi gitweb/gitweb.css
            -e '/@@GITWEB_CGI@@/d' \
            -e '/@@GITWEB_CSS@@/r gitweb/gitweb.css' \
            -e '/@@GITWEB_CSS@@/d' \
-           $@.sh | sed "s|/usr/bin/git|$(bindir)/git|" > $@+
+           $@.sh > $@+
        chmod +x $@+
        mv $@+ $@
 
+configure: configure.ac
+       rm -f $@ $<+
+       sed -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
+           $< > $<+
+       autoconf -o $@ $<+
+       rm -f $<+
+
 # These can record GIT_VERSION
 git$X git.spec \
        $(patsubst %.sh,%,$(SCRIPT_SH)) \
@@ -786,10 +839,11 @@ clean:
        rm -f $(ALL_PROGRAMS) $(BUILT_INS) git$X
        rm -f *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h TAGS tags
        rm -rf autom4te.cache
-       rm -f config.log config.mak.autogen configure config.status config.cache
+       rm -f configure config.log config.mak.autogen config.mak.append config.status config.cache
        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
+       rm -f gitweb/gitweb.cgi
        $(MAKE) -C Documentation/ clean
        $(MAKE) -C templates clean
        $(MAKE) -C t/ clean
index 096b611b5b0eef1c5a96dcaddc21aa874bdaee29..0cb9c812006ba4826122b57b9732e895b0eed905 100644 (file)
@@ -93,9 +93,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 
        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");
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
 
        if (read_cache() < 0)
                die("index file corrupt");
index be2c7152cd7ee8c8076cd4bbd5660dd2f85b0fd1..9cf477c701d3224efe86418113691768410beae9 100644 (file)
@@ -2234,12 +2234,9 @@ static int apply_patch(int fd, const char *filename,
                apply = 0;
 
        write_index = check_index && apply;
-       if (write_index && newfd < 0) {
+       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");
-       }
+                                                 get_index_file(), 1);
        if (check_index) {
                if (read_cache() < 0)
                        die("unable to read index file");
diff --git a/builtin-checkout-index.c b/builtin-checkout-index.c
new file mode 100644 (file)
index 0000000..8d0dbad
--- /dev/null
@@ -0,0 +1,309 @@
+/*
+ * Check-out files from the "current cache directory"
+ *
+ * Copyright (C) 2005 Linus Torvalds
+ *
+ * Careful: order of argument flags does matter. For example,
+ *
+ *     git-checkout-index -a -f file.c
+ *
+ * Will first check out all files listed in the cache (but not
+ * overwrite any old ones), and then force-checkout "file.c" a
+ * second time (ie that one _will_ overwrite any old contents
+ * with the same filename).
+ *
+ * Also, just doing "git-checkout-index" does nothing. You probably
+ * meant "git-checkout-index -a". And if you want to force it, you
+ * want "git-checkout-index -f -a".
+ *
+ * Intuitiveness is not the goal here. Repeatability is. The
+ * reason for the "no arguments means no work" thing is that
+ * from scripts you are supposed to be able to do things like
+ *
+ *     find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
+ *
+ * or:
+ *
+ *     find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
+ *
+ * which will force all existing *.h files to be replaced with
+ * their cached copies. If an empty command line implied "all",
+ * then this would force-refresh everything in the cache, which
+ * was not the point.
+ *
+ * Oh, and the "--" is just a good idea when you know the rest
+ * will be filenames. Just so that you wouldn't have a filename
+ * of "-a" causing problems (not possible in the above example,
+ * but get used to it in scripting!).
+ */
+#include "cache.h"
+#include "strbuf.h"
+#include "quote.h"
+#include "cache-tree.h"
+
+#define CHECKOUT_ALL 4
+static int line_termination = '\n';
+static int checkout_stage; /* default to checkout stage0 */
+static int to_tempfile;
+static char topath[4][MAXPATHLEN+1];
+
+static struct checkout state;
+
+static void write_tempfile_record(const char *name, int prefix_length)
+{
+       int i;
+
+       if (CHECKOUT_ALL == checkout_stage) {
+               for (i = 1; i < 4; i++) {
+                       if (i > 1)
+                               putchar(' ');
+                       if (topath[i][0])
+                               fputs(topath[i], stdout);
+                       else
+                               putchar('.');
+               }
+       } else
+               fputs(topath[checkout_stage], stdout);
+
+       putchar('\t');
+       write_name_quoted("", 0, name + prefix_length,
+               line_termination, stdout);
+       putchar(line_termination);
+
+       for (i = 0; i < 4; i++) {
+               topath[i][0] = 0;
+       }
+}
+
+static int checkout_file(const char *name, int prefix_length)
+{
+       int namelen = strlen(name);
+       int pos = cache_name_pos(name, namelen);
+       int has_same_name = 0;
+       int did_checkout = 0;
+       int errs = 0;
+
+       if (pos < 0)
+               pos = -pos - 1;
+
+       while (pos < active_nr) {
+               struct cache_entry *ce = active_cache[pos];
+               if (ce_namelen(ce) != namelen ||
+                   memcmp(ce->name, name, namelen))
+                       break;
+               has_same_name = 1;
+               pos++;
+               if (ce_stage(ce) != checkout_stage
+                   && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
+                       continue;
+               did_checkout = 1;
+               if (checkout_entry(ce, &state,
+                   to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
+                       errs++;
+       }
+
+       if (did_checkout) {
+               if (to_tempfile)
+                       write_tempfile_record(name, prefix_length);
+               return errs > 0 ? -1 : 0;
+       }
+
+       if (!state.quiet) {
+               fprintf(stderr, "git-checkout-index: %s ", name);
+               if (!has_same_name)
+                       fprintf(stderr, "is not in the cache");
+               else if (checkout_stage)
+                       fprintf(stderr, "does not exist at stage %d",
+                               checkout_stage);
+               else
+                       fprintf(stderr, "is unmerged");
+               fputc('\n', stderr);
+       }
+       return -1;
+}
+
+static int checkout_all(const char *prefix, int prefix_length)
+{
+       int i, errs = 0;
+       struct cache_entry* last_ce = NULL;
+
+       for (i = 0; i < active_nr ; i++) {
+               struct cache_entry *ce = active_cache[i];
+               if (ce_stage(ce) != checkout_stage
+                   && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
+                       continue;
+               if (prefix && *prefix &&
+                   (ce_namelen(ce) <= prefix_length ||
+                    memcmp(prefix, ce->name, prefix_length)))
+                       continue;
+               if (last_ce && to_tempfile) {
+                       if (ce_namelen(last_ce) != ce_namelen(ce)
+                           || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
+                               write_tempfile_record(last_ce->name, prefix_length);
+               }
+               if (checkout_entry(ce, &state,
+                   to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
+                       errs++;
+               last_ce = ce;
+       }
+       if (last_ce && to_tempfile)
+               write_tempfile_record(last_ce->name, prefix_length);
+       if (errs)
+               /* we have already done our error reporting.
+                * exit with the same code as die().
+                */
+               exit(128);
+       return 0;
+}
+
+static const char checkout_cache_usage[] =
+"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
+
+static struct lock_file lock_file;
+
+int cmd_checkout_index(int argc, const char **argv, const char *prefix)
+{
+       int i;
+       int newfd = -1;
+       int all = 0;
+       int read_from_stdin = 0;
+       int prefix_length;
+
+       git_config(git_default_config);
+       state.base_dir = "";
+       prefix_length = prefix ? strlen(prefix) : 0;
+
+       if (read_cache() < 0) {
+               die("invalid cache");
+       }
+
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+
+               if (!strcmp(arg, "--")) {
+                       i++;
+                       break;
+               }
+               if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
+                       all = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
+                       state.force = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
+                       state.quiet = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
+                       state.not_new = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
+                       state.refresh_cache = 1;
+                       if (newfd < 0)
+                               newfd = hold_lock_file_for_update
+                                       (&lock_file, get_index_file(), 1);
+                       if (newfd < 0)
+                               die("cannot open index.lock file.");
+                       continue;
+               }
+               if (!strcmp(arg, "-z")) {
+                       line_termination = 0;
+                       continue;
+               }
+               if (!strcmp(arg, "--stdin")) {
+                       if (i != argc - 1)
+                               die("--stdin must be at the end");
+                       read_from_stdin = 1;
+                       i++; /* do not consider arg as a file name */
+                       break;
+               }
+               if (!strcmp(arg, "--temp")) {
+                       to_tempfile = 1;
+                       continue;
+               }
+               if (!strncmp(arg, "--prefix=", 9)) {
+                       state.base_dir = arg+9;
+                       state.base_dir_len = strlen(state.base_dir);
+                       continue;
+               }
+               if (!strncmp(arg, "--stage=", 8)) {
+                       if (!strcmp(arg + 8, "all")) {
+                               to_tempfile = 1;
+                               checkout_stage = CHECKOUT_ALL;
+                       } else {
+                               int ch = arg[8];
+                               if ('1' <= ch && ch <= '3')
+                                       checkout_stage = arg[8] - '0';
+                               else
+                                       die("stage should be between 1 and 3 or all");
+                       }
+                       continue;
+               }
+               if (arg[0] == '-')
+                       usage(checkout_cache_usage);
+               break;
+       }
+
+       if (state.base_dir_len || to_tempfile) {
+               /* when --prefix is specified we do not
+                * want to update cache.
+                */
+               if (state.refresh_cache) {
+                       close(newfd); newfd = -1;
+                       rollback_lock_file(&lock_file);
+               }
+               state.refresh_cache = 0;
+       }
+
+       /* 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");
+               p = prefix_path(prefix, prefix_length, arg);
+               checkout_file(p, prefix_length);
+               if (p < arg || p > arg + strlen(arg))
+                       free((char*)p);
+       }
+
+       if (read_from_stdin) {
+               struct strbuf buf;
+               if (all)
+                       die("git-checkout-index: don't mix '--all' and '--stdin'");
+               strbuf_init(&buf);
+               while (1) {
+                       char *path_name;
+                       const char *p;
+
+                       read_line(&buf, stdin, line_termination);
+                       if (buf.eof)
+                               break;
+                       if (line_termination && buf.buf[0] == '"')
+                               path_name = unquote_c_style(buf.buf, NULL);
+                       else
+                               path_name = buf.buf;
+                       p = prefix_path(prefix, prefix_length, path_name);
+                       checkout_file(p, prefix_length);
+                       if (p < path_name || p > path_name + strlen(path_name))
+                               free((char *)p);
+                       if (path_name != buf.buf)
+                               free(path_name);
+               }
+       }
+
+       if (all)
+               checkout_all(prefix, prefix_length);
+
+       if (0 <= newfd &&
+           (write_cache(newfd, active_cache, active_nr) ||
+            close(newfd) || commit_lock_file(&lock_file)))
+               die("Unable to write new index file");
+       return 0;
+}
diff --git a/builtin-count-objects.c b/builtin-count-objects.c
new file mode 100644 (file)
index 0000000..1d3729a
--- /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, const char *prefix)
+{
+       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-count.c b/builtin-count.c
deleted file mode 100644 (file)
index 1d3729a..0000000
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * 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, const char *prefix)
-{
-       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;
-}
index 93b7e07b30ced0b4f4bad544d9bd8dbedadf452c..a561612e7efd31f92a8becd8f94516877146ad7f 100644 (file)
@@ -123,6 +123,7 @@ struct grep_opt {
        struct grep_pat *pattern_list;
        struct grep_pat **pattern_tail;
        struct grep_expr *pattern_expression;
+       int prefix_length;
        regex_t regexp;
        unsigned linenum:1;
        unsigned invert:1;
@@ -136,6 +137,7 @@ struct grep_opt {
 #define GREP_BINARY_TEXT       2
        unsigned binary:2;
        unsigned extended:1;
+       unsigned relative:1;
        int regflags;
        unsigned pre_context;
        unsigned post_context;
@@ -632,19 +634,40 @@ static int grep_buffer(struct grep_opt *opt, const char *name,
        return !!last_hit;
 }
 
-static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
+static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
 {
        unsigned long size;
        char *data;
        char type[20];
+       char *to_free = NULL;
        int hit;
+
        data = read_sha1_file(sha1, type, &size);
        if (!data) {
                error("'%s': unable to read %s", name, sha1_to_hex(sha1));
                return 0;
        }
+       if (opt->relative && opt->prefix_length) {
+               static char name_buf[PATH_MAX];
+               char *cp;
+               int name_len = strlen(name) - opt->prefix_length + 1;
+
+               if (!tree_name_len)
+                       name += opt->prefix_length;
+               else {
+                       if (ARRAY_SIZE(name_buf) <= name_len)
+                               cp = to_free = xmalloc(name_len);
+                       else
+                               cp = name_buf;
+                       memcpy(cp, name, tree_name_len);
+                       strcpy(cp + tree_name_len,
+                              name + tree_name_len + opt->prefix_length);
+                       name = cp;
+               }
+       }
        hit = grep_buffer(opt, name, data, size);
        free(data);
+       free(to_free);
        return hit;
 }
 
@@ -674,6 +697,8 @@ static int grep_file(struct grep_opt *opt, const char *filename)
                return 0;
        }
        close(i);
+       if (opt->relative && opt->prefix_length)
+               filename += opt->prefix_length;
        i = grep_buffer(opt, filename, data, st.st_size);
        free(data);
        return i;
@@ -720,7 +745,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
        char *argptr = randarg;
        struct grep_pat *p;
 
-       if (opt->extended)
+       if (opt->extended || (opt->relative && opt->prefix_length))
                return -1;
        len = nr = 0;
        push_arg("grep");
@@ -845,7 +870,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
                if (!pathspec_matches(paths, ce->name))
                        continue;
                if (cached)
-                       hit |= grep_sha1(opt, ce->sha1, ce->name);
+                       hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
                else
                        hit |= grep_file(opt, ce->name);
        }
@@ -860,11 +885,12 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
        int hit = 0;
        struct name_entry entry;
        char *down;
-       char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
+       int tn_len = strlen(tree_name);
+       char *path_buf = xmalloc(PATH_MAX + tn_len + 100);
 
-       if (tree_name[0]) {
-               int offset = sprintf(path_buf, "%s:", tree_name);
-               down = path_buf + offset;
+       if (tn_len) {
+               tn_len = sprintf(path_buf, "%s:", tree_name);
+               down = path_buf + tn_len;
                strcat(down, base);
        }
        else {
@@ -886,7 +912,7 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
                if (!pathspec_matches(paths, down))
                        ;
                else if (S_ISREG(entry.mode))
-                       hit |= grep_sha1(opt, entry.sha1, path_buf);
+                       hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
                else if (S_ISDIR(entry.mode)) {
                        char type[20];
                        struct tree_desc sub;
@@ -907,7 +933,7 @@ static int grep_object(struct grep_opt *opt, const char **paths,
                       struct object *obj, const char *name)
 {
        if (obj->type == OBJ_BLOB)
-               return grep_sha1(opt, obj->sha1, name);
+               return grep_sha1(opt, obj->sha1, name, 0);
        if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
                struct tree_desc tree;
                void *data;
@@ -945,6 +971,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
        int i;
 
        memset(&opt, 0, sizeof(opt));
+       opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
+       opt.relative = 1;
        opt.pattern_tail = &opt.pattern_list;
        opt.regflags = REG_NEWLINE;
 
@@ -1118,6 +1146,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        }
                        die(emsg_missing_argument, arg);
                }
+               if (!strcmp("--full-name", arg)) {
+                       opt.relative = 0;
+                       continue;
+               }
                if (!strcmp("--", arg)) {
                        /* later processing wants to have this at argv[1] */
                        argv--;
@@ -1176,8 +1208,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        verify_filename(prefix, argv[j]);
        }
 
-       if (i < argc)
+       if (i < argc) {
                paths = get_pathspec(prefix, argv + i);
+               if (opt.prefix_length && opt.relative) {
+                       /* Make sure we do not get outside of paths */
+                       for (i = 0; paths[i]; i++)
+                               if (strncmp(prefix, paths[i], opt.prefix_length))
+                                       die("git-grep: cannot generate relative filenames containing '..'");
+               }
+       }
        else if (prefix) {
                paths = xcalloc(2, sizeof(const char *));
                paths[0] = prefix;
diff --git a/builtin-help.c b/builtin-help.c
deleted file mode 100644 (file)
index 7a7f775..0000000
+++ /dev/null
@@ -1,234 +0,0 @@
-/*
- * builtin-help.c
- *
- * Builtin help-related commands (help, usage, version)
- */
-#include <sys/ioctl.h>
-#include "cache.h"
-#include "builtin.h"
-#include "exec_cmd.h"
-#include "common-cmds.h"
-
-
-/* most GUI terminals set COLUMNS (although some don't export it) */
-static int term_columns(void)
-{
-       char *col_string = getenv("COLUMNS");
-       int n_cols = 0;
-
-       if (col_string && (n_cols = atoi(col_string)) > 0)
-               return n_cols;
-
-#ifdef TIOCGWINSZ
-       {
-               struct winsize ws;
-               if (!ioctl(1, TIOCGWINSZ, &ws)) {
-                       if (ws.ws_col)
-                               return ws.ws_col;
-               }
-       }
-#endif
-
-       return 80;
-}
-
-static void oom(void)
-{
-       fprintf(stderr, "git: out of memory\n");
-       exit(1);
-}
-
-static inline void mput_char(char c, unsigned int num)
-{
-       while(num--)
-               putchar(c);
-}
-
-static struct cmdname {
-       size_t len;
-       char name[1];
-} **cmdname;
-static int cmdname_alloc, cmdname_cnt;
-
-static void add_cmdname(const char *name, int len)
-{
-       struct cmdname *ent;
-       if (cmdname_alloc <= cmdname_cnt) {
-               cmdname_alloc = cmdname_alloc + 200;
-               cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
-               if (!cmdname)
-                       oom();
-       }
-       ent = malloc(sizeof(*ent) + len);
-       if (!ent)
-               oom();
-       ent->len = len;
-       memcpy(ent->name, name, len);
-       ent->name[len] = 0;
-       cmdname[cmdname_cnt++] = ent;
-}
-
-static int cmdname_compare(const void *a_, const void *b_)
-{
-       struct cmdname *a = *(struct cmdname **)a_;
-       struct cmdname *b = *(struct cmdname **)b_;
-       return strcmp(a->name, b->name);
-}
-
-static void pretty_print_string_list(struct cmdname **cmdname, int longest)
-{
-       int cols = 1, rows;
-       int space = longest + 1; /* min 1 SP between words */
-       int max_cols = term_columns() - 1; /* don't print *on* the edge */
-       int i, j;
-
-       if (space < max_cols)
-               cols = max_cols / space;
-       rows = (cmdname_cnt + cols - 1) / cols;
-
-       qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
-
-       for (i = 0; i < rows; i++) {
-               printf("  ");
-
-               for (j = 0; j < cols; j++) {
-                       int n = j * rows + i;
-                       int size = space;
-                       if (n >= cmdname_cnt)
-                               break;
-                       if (j == cols-1 || n + rows >= cmdname_cnt)
-                               size = 1;
-                       printf("%-*s", size, cmdname[n]->name);
-               }
-               putchar('\n');
-       }
-}
-
-static void list_commands(const char *exec_path, const char *pattern)
-{
-       unsigned int longest = 0;
-       char path[PATH_MAX];
-       int dirlen;
-       DIR *dir = opendir(exec_path);
-       struct dirent *de;
-
-       if (!dir) {
-               fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
-               exit(1);
-       }
-
-       dirlen = strlen(exec_path);
-       if (PATH_MAX - 20 < dirlen) {
-               fprintf(stderr, "git: insanely long exec-path '%s'\n",
-                       exec_path);
-               exit(1);
-       }
-
-       memcpy(path, exec_path, dirlen);
-       path[dirlen++] = '/';
-
-       while ((de = readdir(dir)) != NULL) {
-               struct stat st;
-               int entlen;
-
-               if (strncmp(de->d_name, "git-", 4))
-                       continue;
-               strcpy(path+dirlen, de->d_name);
-               if (stat(path, &st) || /* stat, not lstat */
-                   !S_ISREG(st.st_mode) ||
-                   !(st.st_mode & S_IXUSR))
-                       continue;
-
-               entlen = strlen(de->d_name);
-               if (has_extension(de->d_name, entlen, ".exe"))
-                       entlen -= 4;
-
-               if (longest < entlen)
-                       longest = entlen;
-
-               add_cmdname(de->d_name + 4, entlen-4);
-       }
-       closedir(dir);
-
-       printf("git commands available in '%s'\n", exec_path);
-       printf("----------------------------");
-       mput_char('-', strlen(exec_path));
-       putchar('\n');
-       pretty_print_string_list(cmdname, longest - 4);
-       putchar('\n');
-}
-
-static void list_common_cmds_help(void)
-{
-       int i, longest = 0;
-
-       for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
-               if (longest < strlen(common_cmds[i].name))
-                       longest = strlen(common_cmds[i].name);
-       }
-
-       puts("The most commonly used git commands are:");
-       for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
-               printf("    %s", common_cmds[i].name);
-               mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
-               puts(common_cmds[i].help);
-       }
-       puts("(use 'git help -a' to get a list of all installed git commands)");
-}
-
-static void show_man_page(const char *git_cmd)
-{
-       const char *page;
-
-       if (!strncmp(git_cmd, "git", 3))
-               page = git_cmd;
-       else {
-               int page_len = strlen(git_cmd) + 4;
-               char *p = malloc(page_len + 1);
-               strcpy(p, "git-");
-               strcpy(p + 4, git_cmd);
-               p[page_len] = 0;
-               page = p;
-       }
-
-       execlp("man", "man", page, NULL);
-}
-
-void help_unknown_cmd(const char *cmd)
-{
-       printf("git: '%s' is not a git-command\n\n", cmd);
-       list_common_cmds_help();
-       exit(1);
-}
-
-int cmd_version(int argc, const char **argv, const char *prefix)
-{
-       printf("git version %s\n", git_version_string);
-       return 0;
-}
-
-int cmd_help(int argc, const char **argv, const char *prefix)
-{
-       const char *help_cmd = argc > 1 ? argv[1] : NULL;
-       const char *exec_path = git_exec_path();
-
-       if (!help_cmd) {
-               printf("usage: %s\n\n", git_usage_string);
-               list_common_cmds_help();
-               exit(1);
-       }
-
-       else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
-               printf("usage: %s\n\n", git_usage_string);
-               if(exec_path)
-                       list_commands(exec_path, "git-*");
-               exit(1);
-       }
-
-       else
-               show_man_page(help_cmd);
-
-       return 0;
-}
-
-
index ce8187c1e96833e1a6db2fa368a84b02fec7b0c2..a731f8d9cfed783fb59cb64db93f854ef0a4ebf9 100644 (file)
@@ -72,10 +72,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 
        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");
-
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
        if (read_cache() < 0)
                die("index file corrupt");
 
diff --git a/builtin-name-rev.c b/builtin-name-rev.c
new file mode 100644 (file)
index 0000000..571bba4
--- /dev/null
@@ -0,0 +1,256 @@
+#include <stdlib.h>
+#include "builtin.h"
+#include "cache.h"
+#include "commit.h"
+#include "tag.h"
+#include "refs.h"
+
+static const char name_rev_usage[] =
+       "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
+
+typedef struct rev_name {
+       const char *tip_name;
+       int merge_traversals;
+       int generation;
+} rev_name;
+
+static long cutoff = LONG_MAX;
+
+static void name_rev(struct commit *commit,
+               const char *tip_name, int merge_traversals, int generation,
+               int deref)
+{
+       struct rev_name *name = (struct rev_name *)commit->util;
+       struct commit_list *parents;
+       int parent_number = 1;
+
+       if (!commit->object.parsed)
+               parse_commit(commit);
+
+       if (commit->date < cutoff)
+               return;
+
+       if (deref) {
+               char *new_name = xmalloc(strlen(tip_name)+3);
+               strcpy(new_name, tip_name);
+               strcat(new_name, "^0");
+               tip_name = new_name;
+
+               if (generation)
+                       die("generation: %d, but deref?", generation);
+       }
+
+       if (name == NULL) {
+               name = xmalloc(sizeof(rev_name));
+               commit->util = name;
+               goto copy_data;
+       } else if (name->merge_traversals > merge_traversals ||
+                       (name->merge_traversals == merge_traversals &&
+                        name->generation > generation)) {
+copy_data:
+               name->tip_name = tip_name;
+               name->merge_traversals = merge_traversals;
+               name->generation = generation;
+       } else
+               return;
+
+       for (parents = commit->parents;
+                       parents;
+                       parents = parents->next, parent_number++) {
+               if (parent_number > 1) {
+                       char *new_name = xmalloc(strlen(tip_name)+8);
+
+                       if (generation > 0)
+                               sprintf(new_name, "%s~%d^%d", tip_name,
+                                               generation, parent_number);
+                       else
+                               sprintf(new_name, "%s^%d", tip_name, parent_number);
+
+                       name_rev(parents->item, new_name,
+                               merge_traversals + 1 , 0, 0);
+               } else {
+                       name_rev(parents->item, tip_name, merge_traversals,
+                               generation + 1, 0);
+               }
+       }
+}
+
+static int tags_only = 0;
+
+static int name_ref(const char *path, const unsigned char *sha1)
+{
+       struct object *o = parse_object(sha1);
+       int deref = 0;
+
+       if (tags_only && strncmp(path, "refs/tags/", 10))
+               return 0;
+
+       while (o && o->type == OBJ_TAG) {
+               struct tag *t = (struct tag *) o;
+               if (!t->tagged)
+                       break; /* broken repository */
+               o = parse_object(t->tagged->sha1);
+               deref = 1;
+       }
+       if (o && o->type == OBJ_COMMIT) {
+               struct commit *commit = (struct commit *)o;
+
+               if (!strncmp(path, "refs/heads/", 11))
+                       path = path + 11;
+               else if (!strncmp(path, "refs/", 5))
+                       path = path + 5;
+
+               name_rev(commit, strdup(path), 0, 0, deref);
+       }
+       return 0;
+}
+
+/* returns a static buffer */
+static const char* get_rev_name(struct object *o)
+{
+       static char buffer[1024];
+       struct rev_name *n;
+       struct commit *c;
+
+       if (o->type != OBJ_COMMIT)
+               return "undefined";
+       c = (struct commit *) o;
+       n = c->util;
+       if (!n)
+               return "undefined";
+
+       if (!n->generation)
+               return n->tip_name;
+
+       snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
+
+       return buffer;
+}
+
+int cmd_name_rev(int argc, const char **argv, const char *prefix)
+{
+       struct object_array revs = { 0, 0, NULL };
+       int as_is = 0, all = 0, transform_stdin = 0;
+
+       git_config(git_default_config);
+
+       if (argc < 2)
+               usage(name_rev_usage);
+
+       for (--argc, ++argv; argc; --argc, ++argv) {
+               unsigned char sha1[20];
+               struct object *o;
+               struct commit *commit;
+
+               if (!as_is && (*argv)[0] == '-') {
+                       if (!strcmp(*argv, "--")) {
+                               as_is = 1;
+                               continue;
+                       } else if (!strcmp(*argv, "--tags")) {
+                               tags_only = 1;
+                               continue;
+                       } else if (!strcmp(*argv, "--all")) {
+                               if (argc > 1)
+                                       die("Specify either a list, or --all, not both!");
+                               all = 1;
+                               cutoff = 0;
+                               continue;
+                       } else if (!strcmp(*argv, "--stdin")) {
+                               if (argc > 1)
+                                       die("Specify either a list, or --stdin, not both!");
+                               transform_stdin = 1;
+                               cutoff = 0;
+                               continue;
+                       }
+                       usage(name_rev_usage);
+               }
+
+               if (get_sha1(*argv, sha1)) {
+                       fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
+                                       *argv);
+                       continue;
+               }
+
+               o = deref_tag(parse_object(sha1), *argv, 0);
+               if (!o || o->type != OBJ_COMMIT) {
+                       fprintf(stderr, "Could not get commit for %s. Skipping.\n",
+                                       *argv);
+                       continue;
+               }
+
+               commit = (struct commit *)o;
+
+               if (cutoff > commit->date)
+                       cutoff = commit->date;
+
+               add_object_array((struct object *)commit, *argv, &revs);
+       }
+
+       for_each_ref(name_ref);
+
+       if (transform_stdin) {
+               char buffer[2048];
+               char *p, *p_start;
+
+               while (!feof(stdin)) {
+                       int forty = 0;
+                       p = fgets(buffer, sizeof(buffer), stdin);
+                       if (!p)
+                               break;
+
+                       for (p_start = p; *p; p++) {
+#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
+                               if (!ishex(*p))
+                                       forty = 0;
+                               else if (++forty == 40 &&
+                                               !ishex(*(p+1))) {
+                                       unsigned char sha1[40];
+                                       const char *name = "undefined";
+                                       char c = *(p+1);
+
+                                       forty = 0;
+
+                                       *(p+1) = 0;
+                                       if (!get_sha1(p - 39, sha1)) {
+                                               struct object *o =
+                                                       lookup_object(sha1);
+                                               if (o)
+                                                       name = get_rev_name(o);
+                                       }
+                                       *(p+1) = c;
+
+                                       if (!strcmp(name, "undefined"))
+                                               continue;
+
+                                       fwrite(p_start, p - p_start + 1, 1,
+                                              stdout);
+                                       printf(" (%s)", name);
+                                       p_start = p + 1;
+                               }
+                       }
+
+                       /* flush */
+                       if (p_start != p)
+                               fwrite(p_start, p - p_start, 1, stdout);
+               }
+       } else if (all) {
+               int i, max;
+
+               max = get_max_object_index();
+               for (i = 0; i < max; i++) {
+                       struct object * obj = get_indexed_object(i);
+                       if (!obj)
+                               continue;
+                       printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
+               }
+       } else {
+               int i;
+               for (i = 0; i < revs.nr; i++)
+                       printf("%s %s\n",
+                               revs.objects[i].name,
+                               get_rev_name(revs.objects[i].item));
+       }
+
+       return 0;
+}
+
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
new file mode 100644 (file)
index 0000000..2301cd5
--- /dev/null
@@ -0,0 +1,1376 @@
+#include "builtin.h"
+#include "cache.h"
+#include "object.h"
+#include "blob.h"
+#include "commit.h"
+#include "tag.h"
+#include "tree.h"
+#include "delta.h"
+#include "pack.h"
+#include "csum-file.h"
+#include "tree-walk.h"
+#include <sys/time.h>
+#include <signal.h>
+
+static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
+
+struct object_entry {
+       unsigned char sha1[20];
+       unsigned long size;     /* uncompressed size */
+       unsigned long offset;   /* offset into the final pack file;
+                                * nonzero if already written.
+                                */
+       unsigned int depth;     /* delta depth */
+       unsigned int delta_limit;       /* base adjustment for in-pack delta */
+       unsigned int hash;      /* name hint hash */
+       enum object_type type;
+       enum object_type in_pack_type;  /* could be delta */
+       unsigned long delta_size;       /* delta data size (uncompressed) */
+       struct object_entry *delta;     /* delta base object */
+       struct packed_git *in_pack;     /* already in pack */
+       unsigned int in_pack_offset;
+       struct object_entry *delta_child; /* deltified objects who bases me */
+       struct object_entry *delta_sibling; /* other deltified objects who
+                                            * uses the same base as me
+                                            */
+       int preferred_base;     /* we do not pack this, but is encouraged to
+                                * be used as the base objectto delta huge
+                                * objects against.
+                                */
+};
+
+/*
+ * Objects we are going to pack are collected in objects array (dynamically
+ * expanded).  nr_objects & nr_alloc controls this array.  They are stored
+ * in the order we see -- typically rev-list --objects order that gives us
+ * nice "minimum seek" order.
+ *
+ * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
+ * elements in the objects array.  The former is used to build the pack
+ * index (lists object names in the ascending order to help offset lookup),
+ * and the latter is used to group similar things together by try_delta()
+ * heuristics.
+ */
+
+static unsigned char object_list_sha1[20];
+static int non_empty = 0;
+static int no_reuse_delta = 0;
+static int local = 0;
+static int incremental = 0;
+static struct object_entry **sorted_by_sha, **sorted_by_type;
+static struct object_entry *objects = NULL;
+static int nr_objects = 0, nr_alloc = 0, nr_result = 0;
+static const char *base_name;
+static unsigned char pack_file_sha1[20];
+static int progress = 1;
+static volatile sig_atomic_t progress_update = 0;
+static int window = 10;
+
+/*
+ * The object names in objects array are hashed with this hashtable,
+ * to help looking up the entry by object name.  Binary search from
+ * sorted_by_sha is also possible but this was easier to code and faster.
+ * This hashtable is built after all the objects are seen.
+ */
+static int *object_ix = NULL;
+static int object_ix_hashsz = 0;
+
+/*
+ * Pack index for existing packs give us easy access to the offsets into
+ * corresponding pack file where each object's data starts, but the entries
+ * do not store the size of the compressed representation (uncompressed
+ * size is easily available by examining the pack entry header).  We build
+ * a hashtable of existing packs (pack_revindex), and keep reverse index
+ * here -- pack index file is sorted by object name mapping to offset; this
+ * pack_revindex[].revindex array is an ordered list of offsets, so if you
+ * know the offset of an object, next offset is where its packed
+ * representation ends.
+ */
+struct pack_revindex {
+       struct packed_git *p;
+       unsigned long *revindex;
+} *pack_revindex = NULL;
+static int pack_revindex_hashsz = 0;
+
+/*
+ * stats
+ */
+static int written = 0;
+static int written_delta = 0;
+static int reused = 0;
+static int reused_delta = 0;
+
+static int pack_revindex_ix(struct packed_git *p)
+{
+       unsigned long ui = (unsigned long)p;
+       int i;
+
+       ui = ui ^ (ui >> 16); /* defeat structure alignment */
+       i = (int)(ui % pack_revindex_hashsz);
+       while (pack_revindex[i].p) {
+               if (pack_revindex[i].p == p)
+                       return i;
+               if (++i == pack_revindex_hashsz)
+                       i = 0;
+       }
+       return -1 - i;
+}
+
+static void prepare_pack_ix(void)
+{
+       int num;
+       struct packed_git *p;
+       for (num = 0, p = packed_git; p; p = p->next)
+               num++;
+       if (!num)
+               return;
+       pack_revindex_hashsz = num * 11;
+       pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
+       for (p = packed_git; p; p = p->next) {
+               num = pack_revindex_ix(p);
+               num = - 1 - num;
+               pack_revindex[num].p = p;
+       }
+       /* revindex elements are lazily initialized */
+}
+
+static int cmp_offset(const void *a_, const void *b_)
+{
+       unsigned long a = *(unsigned long *) a_;
+       unsigned long b = *(unsigned long *) b_;
+       if (a < b)
+               return -1;
+       else if (a == b)
+               return 0;
+       else
+               return 1;
+}
+
+/*
+ * Ordered list of offsets of objects in the pack.
+ */
+static void prepare_pack_revindex(struct pack_revindex *rix)
+{
+       struct packed_git *p = rix->p;
+       int num_ent = num_packed_objects(p);
+       int i;
+       void *index = p->index_base + 256;
+
+       rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
+       for (i = 0; i < num_ent; i++) {
+               unsigned int hl = *((unsigned int *)((char *) index + 24*i));
+               rix->revindex[i] = ntohl(hl);
+       }
+       /* This knows the pack format -- the 20-byte trailer
+        * follows immediately after the last object data.
+        */
+       rix->revindex[num_ent] = p->pack_size - 20;
+       qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset);
+}
+
+static unsigned long find_packed_object_size(struct packed_git *p,
+                                            unsigned long ofs)
+{
+       int num;
+       int lo, hi;
+       struct pack_revindex *rix;
+       unsigned long *revindex;
+       num = pack_revindex_ix(p);
+       if (num < 0)
+               die("internal error: pack revindex uninitialized");
+       rix = &pack_revindex[num];
+       if (!rix->revindex)
+               prepare_pack_revindex(rix);
+       revindex = rix->revindex;
+       lo = 0;
+       hi = num_packed_objects(p) + 1;
+       do {
+               int mi = (lo + hi) / 2;
+               if (revindex[mi] == ofs) {
+                       return revindex[mi+1] - ofs;
+               }
+               else if (ofs < revindex[mi])
+                       hi = mi;
+               else
+                       lo = mi + 1;
+       } while (lo < hi);
+       die("internal error: pack revindex corrupt");
+}
+
+static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
+{
+       unsigned long othersize, delta_size;
+       char type[10];
+       void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
+       void *delta_buf;
+
+       if (!otherbuf)
+               die("unable to read %s", sha1_to_hex(entry->delta->sha1));
+        delta_buf = diff_delta(otherbuf, othersize,
+                              buf, size, &delta_size, 0);
+        if (!delta_buf || delta_size != entry->delta_size)
+               die("delta size changed");
+        free(buf);
+        free(otherbuf);
+       return delta_buf;
+}
+
+/*
+ * The per-object header is a pretty dense thing, which is
+ *  - first byte: low four bits are "size", then three bits of "type",
+ *    and the high bit is "size continues".
+ *  - each byte afterwards: low seven bits are size continuation,
+ *    with the high bit being "size continues"
+ */
+static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
+{
+       int n = 1;
+       unsigned char c;
+
+       if (type < OBJ_COMMIT || type > OBJ_DELTA)
+               die("bad type %d", type);
+
+       c = (type << 4) | (size & 15);
+       size >>= 4;
+       while (size) {
+               *hdr++ = c | 0x80;
+               c = size & 0x7f;
+               size >>= 7;
+               n++;
+       }
+       *hdr = c;
+       return n;
+}
+
+static unsigned long write_object(struct sha1file *f,
+                                 struct object_entry *entry)
+{
+       unsigned long size;
+       char type[10];
+       void *buf;
+       unsigned char header[10];
+       unsigned hdrlen, datalen;
+       enum object_type obj_type;
+       int to_reuse = 0;
+
+       if (entry->preferred_base)
+               return 0;
+
+       obj_type = entry->type;
+       if (! entry->in_pack)
+               to_reuse = 0;   /* can't reuse what we don't have */
+       else if (obj_type == OBJ_DELTA)
+               to_reuse = 1;   /* check_object() decided it for us */
+       else if (obj_type != entry->in_pack_type)
+               to_reuse = 0;   /* pack has delta which is unusable */
+       else if (entry->delta)
+               to_reuse = 0;   /* we want to pack afresh */
+       else
+               to_reuse = 1;   /* we have it in-pack undeltified,
+                                * and we do not need to deltify it.
+                                */
+
+       if (! to_reuse) {
+               buf = read_sha1_file(entry->sha1, type, &size);
+               if (!buf)
+                       die("unable to read %s", sha1_to_hex(entry->sha1));
+               if (size != entry->size)
+                       die("object %s size inconsistency (%lu vs %lu)",
+                           sha1_to_hex(entry->sha1), size, entry->size);
+               if (entry->delta) {
+                       buf = delta_against(buf, size, entry);
+                       size = entry->delta_size;
+                       obj_type = OBJ_DELTA;
+               }
+               /*
+                * The object header is a byte of 'type' followed by zero or
+                * more bytes of length.  For deltas, the 20 bytes of delta
+                * sha1 follows that.
+                */
+               hdrlen = encode_header(obj_type, size, header);
+               sha1write(f, header, hdrlen);
+
+               if (entry->delta) {
+                       sha1write(f, entry->delta, 20);
+                       hdrlen += 20;
+               }
+               datalen = sha1write_compressed(f, buf, size);
+               free(buf);
+       }
+       else {
+               struct packed_git *p = entry->in_pack;
+               use_packed_git(p);
+
+               datalen = find_packed_object_size(p, entry->in_pack_offset);
+               buf = (char *) p->pack_base + entry->in_pack_offset;
+               sha1write(f, buf, datalen);
+               unuse_packed_git(p);
+               hdrlen = 0; /* not really */
+               if (obj_type == OBJ_DELTA)
+                       reused_delta++;
+               reused++;
+       }
+       if (obj_type == OBJ_DELTA)
+               written_delta++;
+       written++;
+       return hdrlen + datalen;
+}
+
+static unsigned long write_one(struct sha1file *f,
+                              struct object_entry *e,
+                              unsigned long offset)
+{
+       if (e->offset)
+               /* offset starts from header size and cannot be zero
+                * if it is written already.
+                */
+               return offset;
+       e->offset = offset;
+       offset += write_object(f, e);
+       /* if we are deltified, write out its base object. */
+       if (e->delta)
+               offset = write_one(f, e->delta, offset);
+       return offset;
+}
+
+static void write_pack_file(void)
+{
+       int i;
+       struct sha1file *f;
+       unsigned long offset;
+       struct pack_header hdr;
+       unsigned last_percent = 999;
+       int do_progress = 0;
+
+       if (!base_name)
+               f = sha1fd(1, "<stdout>");
+       else {
+               f = sha1create("%s-%s.%s", base_name,
+                              sha1_to_hex(object_list_sha1), "pack");
+               do_progress = progress;
+       }
+       if (do_progress)
+               fprintf(stderr, "Writing %d objects.\n", nr_result);
+
+       hdr.hdr_signature = htonl(PACK_SIGNATURE);
+       hdr.hdr_version = htonl(PACK_VERSION);
+       hdr.hdr_entries = htonl(nr_result);
+       sha1write(f, &hdr, sizeof(hdr));
+       offset = sizeof(hdr);
+       if (!nr_result)
+               goto done;
+       for (i = 0; i < nr_objects; i++) {
+               offset = write_one(f, objects + i, offset);
+               if (do_progress) {
+                       unsigned percent = written * 100 / nr_result;
+                       if (progress_update || percent != last_percent) {
+                               fprintf(stderr, "%4u%% (%u/%u) done\r",
+                                       percent, written, nr_result);
+                               progress_update = 0;
+                               last_percent = percent;
+                       }
+               }
+       }
+       if (do_progress)
+               fputc('\n', stderr);
+ done:
+       sha1close(f, pack_file_sha1, 1);
+}
+
+static void write_index_file(void)
+{
+       int i;
+       struct sha1file *f = sha1create("%s-%s.%s", base_name,
+                                       sha1_to_hex(object_list_sha1), "idx");
+       struct object_entry **list = sorted_by_sha;
+       struct object_entry **last = list + nr_result;
+       unsigned int array[256];
+
+       /*
+        * Write the first-level table (the list is sorted,
+        * but we use a 256-entry lookup to be able to avoid
+        * having to do eight extra binary search iterations).
+        */
+       for (i = 0; i < 256; i++) {
+               struct object_entry **next = list;
+               while (next < last) {
+                       struct object_entry *entry = *next;
+                       if (entry->sha1[0] != i)
+                               break;
+                       next++;
+               }
+               array[i] = htonl(next - sorted_by_sha);
+               list = next;
+       }
+       sha1write(f, array, 256 * sizeof(int));
+
+       /*
+        * Write the actual SHA1 entries..
+        */
+       list = sorted_by_sha;
+       for (i = 0; i < nr_result; i++) {
+               struct object_entry *entry = *list++;
+               unsigned int offset = htonl(entry->offset);
+               sha1write(f, &offset, 4);
+               sha1write(f, entry->sha1, 20);
+       }
+       sha1write(f, pack_file_sha1, 20);
+       sha1close(f, NULL, 1);
+}
+
+static int locate_object_entry_hash(const unsigned char *sha1)
+{
+       int i;
+       unsigned int ui;
+       memcpy(&ui, sha1, sizeof(unsigned int));
+       i = ui % object_ix_hashsz;
+       while (0 < object_ix[i]) {
+               if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20))
+                       return i;
+               if (++i == object_ix_hashsz)
+                       i = 0;
+       }
+       return -1 - i;
+}
+
+static struct object_entry *locate_object_entry(const unsigned char *sha1)
+{
+       int i;
+
+       if (!object_ix_hashsz)
+               return NULL;
+
+       i = locate_object_entry_hash(sha1);
+       if (0 <= i)
+               return &objects[object_ix[i]-1];
+       return NULL;
+}
+
+static void rehash_objects(void)
+{
+       int i;
+       struct object_entry *oe;
+
+       object_ix_hashsz = nr_objects * 3;
+       if (object_ix_hashsz < 1024)
+               object_ix_hashsz = 1024;
+       object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
+       memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
+       for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
+               int ix = locate_object_entry_hash(oe->sha1);
+               if (0 <= ix)
+                       continue;
+               ix = -1 - ix;
+               object_ix[ix] = i + 1;
+       }
+}
+
+static unsigned name_hash(const char *name)
+{
+       unsigned char c;
+       unsigned hash = 0;
+
+       /*
+        * 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.
+        */
+       while ((c = *name++) != 0) {
+               if (isspace(c))
+                       continue;
+               hash = (hash >> 2) + (c << 24);
+       }
+       return hash;
+}
+
+static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
+{
+       unsigned int idx = nr_objects;
+       struct object_entry *entry;
+       struct packed_git *p;
+       unsigned int found_offset = 0;
+       struct packed_git *found_pack = NULL;
+       int ix, status = 0;
+
+       if (!exclude) {
+               for (p = packed_git; p; p = p->next) {
+                       struct pack_entry e;
+                       if (find_pack_entry_one(sha1, &e, p)) {
+                               if (incremental)
+                                       return 0;
+                               if (local && !p->pack_local)
+                                       return 0;
+                               if (!found_pack) {
+                                       found_offset = e.offset;
+                                       found_pack = e.p;
+                               }
+                       }
+               }
+       }
+       if ((entry = locate_object_entry(sha1)) != NULL)
+               goto already_added;
+
+       if (idx >= nr_alloc) {
+               unsigned int needed = (idx + 1024) * 3 / 2;
+               objects = xrealloc(objects, needed * sizeof(*entry));
+               nr_alloc = needed;
+       }
+       entry = objects + idx;
+       nr_objects = idx + 1;
+       memset(entry, 0, sizeof(*entry));
+       memcpy(entry->sha1, sha1, 20);
+       entry->hash = hash;
+
+       if (object_ix_hashsz * 3 <= nr_objects * 4)
+               rehash_objects();
+       else {
+               ix = locate_object_entry_hash(entry->sha1);
+               if (0 <= ix)
+                       die("internal error in object hashing.");
+               object_ix[-1 - ix] = idx + 1;
+       }
+       status = 1;
+
+ already_added:
+       if (progress_update) {
+               fprintf(stderr, "Counting objects...%d\r", nr_objects);
+               progress_update = 0;
+       }
+       if (exclude)
+               entry->preferred_base = 1;
+       else {
+               if (found_pack) {
+                       entry->in_pack = found_pack;
+                       entry->in_pack_offset = found_offset;
+               }
+       }
+       return status;
+}
+
+struct pbase_tree_cache {
+       unsigned char sha1[20];
+       int ref;
+       int temporary;
+       void *tree_data;
+       unsigned long tree_size;
+};
+
+static struct pbase_tree_cache *(pbase_tree_cache[256]);
+static int pbase_tree_cache_ix(const unsigned char *sha1)
+{
+       return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
+}
+static int pbase_tree_cache_ix_incr(int ix)
+{
+       return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
+}
+
+static struct pbase_tree {
+       struct pbase_tree *next;
+       /* This is a phony "cache" entry; we are not
+        * going to evict it nor find it through _get()
+        * mechanism -- this is for the toplevel node that
+        * would almost always change with any commit.
+        */
+       struct pbase_tree_cache pcache;
+} *pbase_tree;
+
+static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
+{
+       struct pbase_tree_cache *ent, *nent;
+       void *data;
+       unsigned long size;
+       char type[20];
+       int neigh;
+       int my_ix = pbase_tree_cache_ix(sha1);
+       int available_ix = -1;
+
+       /* pbase-tree-cache acts as a limited hashtable.
+        * your object will be found at your index or within a few
+        * slots after that slot if it is cached.
+        */
+       for (neigh = 0; neigh < 8; neigh++) {
+               ent = pbase_tree_cache[my_ix];
+               if (ent && !memcmp(ent->sha1, sha1, 20)) {
+                       ent->ref++;
+                       return ent;
+               }
+               else if (((available_ix < 0) && (!ent || !ent->ref)) ||
+                        ((0 <= available_ix) &&
+                         (!ent && pbase_tree_cache[available_ix])))
+                       available_ix = my_ix;
+               if (!ent)
+                       break;
+               my_ix = pbase_tree_cache_ix_incr(my_ix);
+       }
+
+       /* Did not find one.  Either we got a bogus request or
+        * we need to read and perhaps cache.
+        */
+       data = read_sha1_file(sha1, type, &size);
+       if (!data)
+               return NULL;
+       if (strcmp(type, tree_type)) {
+               free(data);
+               return NULL;
+       }
+
+       /* We need to either cache or return a throwaway copy */
+
+       if (available_ix < 0)
+               ent = NULL;
+       else {
+               ent = pbase_tree_cache[available_ix];
+               my_ix = available_ix;
+       }
+
+       if (!ent) {
+               nent = xmalloc(sizeof(*nent));
+               nent->temporary = (available_ix < 0);
+       }
+       else {
+               /* evict and reuse */
+               free(ent->tree_data);
+               nent = ent;
+       }
+       memcpy(nent->sha1, sha1, 20);
+       nent->tree_data = data;
+       nent->tree_size = size;
+       nent->ref = 1;
+       if (!nent->temporary)
+               pbase_tree_cache[my_ix] = nent;
+       return nent;
+}
+
+static void pbase_tree_put(struct pbase_tree_cache *cache)
+{
+       if (!cache->temporary) {
+               cache->ref--;
+               return;
+       }
+       free(cache->tree_data);
+       free(cache);
+}
+
+static int name_cmp_len(const char *name)
+{
+       int i;
+       for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
+               ;
+       return i;
+}
+
+static void add_pbase_object(struct tree_desc *tree,
+                            const char *name,
+                            int cmplen,
+                            const char *fullname)
+{
+       struct name_entry entry;
+
+       while (tree_entry(tree,&entry)) {
+               unsigned long size;
+               char type[20];
+
+               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(fullname);
+                       add_object_entry(entry.sha1, hash, 1);
+                       return;
+               }
+               if (!strcmp(type, tree_type)) {
+                       struct tree_desc sub;
+                       struct pbase_tree_cache *tree;
+                       const char *down = name+cmplen+1;
+                       int downlen = name_cmp_len(down);
+
+                       tree = pbase_tree_get(entry.sha1);
+                       if (!tree)
+                               return;
+                       sub.buf = tree->tree_data;
+                       sub.size = tree->tree_size;
+
+                       add_pbase_object(&sub, down, downlen, fullname);
+                       pbase_tree_put(tree);
+               }
+       }
+}
+
+static unsigned *done_pbase_paths;
+static int done_pbase_paths_num;
+static int done_pbase_paths_alloc;
+static int done_pbase_path_pos(unsigned hash)
+{
+       int lo = 0;
+       int hi = done_pbase_paths_num;
+       while (lo < hi) {
+               int mi = (hi + lo) / 2;
+               if (done_pbase_paths[mi] == hash)
+                       return mi;
+               if (done_pbase_paths[mi] < hash)
+                       hi = mi;
+               else
+                       lo = mi + 1;
+       }
+       return -lo-1;
+}
+
+static int check_pbase_path(unsigned hash)
+{
+       int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
+       if (0 <= pos)
+               return 1;
+       pos = -pos - 1;
+       if (done_pbase_paths_alloc <= done_pbase_paths_num) {
+               done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
+               done_pbase_paths = xrealloc(done_pbase_paths,
+                                           done_pbase_paths_alloc *
+                                           sizeof(unsigned));
+       }
+       done_pbase_paths_num++;
+       if (pos < done_pbase_paths_num)
+               memmove(done_pbase_paths + pos + 1,
+                       done_pbase_paths + pos,
+                       (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
+       done_pbase_paths[pos] = hash;
+       return 0;
+}
+
+static void add_preferred_base_object(char *name, unsigned hash)
+{
+       struct pbase_tree *it;
+       int cmplen = name_cmp_len(name);
+
+       if (check_pbase_path(hash))
+               return;
+
+       for (it = pbase_tree; it; it = it->next) {
+               if (cmplen == 0) {
+                       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, name, cmplen, name);
+               }
+       }
+}
+
+static void add_preferred_base(unsigned char *sha1)
+{
+       struct pbase_tree *it;
+       void *data;
+       unsigned long size;
+       unsigned char tree_sha1[20];
+
+       data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
+       if (!data)
+               return;
+
+       for (it = pbase_tree; it; it = it->next) {
+               if (!memcmp(it->pcache.sha1, tree_sha1, 20)) {
+                       free(data);
+                       return;
+               }
+       }
+
+       it = xcalloc(1, sizeof(*it));
+       it->next = pbase_tree;
+       pbase_tree = it;
+
+       memcpy(it->pcache.sha1, tree_sha1, 20);
+       it->pcache.tree_data = data;
+       it->pcache.tree_size = size;
+}
+
+static void check_object(struct object_entry *entry)
+{
+       char type[20];
+
+       if (entry->in_pack && !entry->preferred_base) {
+               unsigned char base[20];
+               unsigned long size;
+               struct object_entry *base_entry;
+
+               /* We want in_pack_type even if we do not reuse delta.
+                * There is no point not reusing non-delta representations.
+                */
+               check_reuse_pack_delta(entry->in_pack,
+                                      entry->in_pack_offset,
+                                      base, &size,
+                                      &entry->in_pack_type);
+
+               /* Check if it is delta, and the base is also an object
+                * we are going to pack.  If so we will reuse the existing
+                * delta.
+                */
+               if (!no_reuse_delta &&
+                   entry->in_pack_type == OBJ_DELTA &&
+                   (base_entry = locate_object_entry(base)) &&
+                   (!base_entry->preferred_base)) {
+
+                       /* Depth value does not matter - find_deltas()
+                        * will never consider reused delta as the
+                        * base object to deltify other objects
+                        * against, in order to avoid circular deltas.
+                        */
+
+                       /* uncompressed size of the delta data */
+                       entry->size = entry->delta_size = size;
+                       entry->delta = base_entry;
+                       entry->type = OBJ_DELTA;
+
+                       entry->delta_sibling = base_entry->delta_child;
+                       base_entry->delta_child = entry;
+
+                       return;
+               }
+               /* Otherwise we would do the usual */
+       }
+
+       if (sha1_object_info(entry->sha1, type, &entry->size))
+               die("unable to get type of object %s",
+                   sha1_to_hex(entry->sha1));
+
+       if (!strcmp(type, commit_type)) {
+               entry->type = OBJ_COMMIT;
+       } else if (!strcmp(type, tree_type)) {
+               entry->type = OBJ_TREE;
+       } else if (!strcmp(type, blob_type)) {
+               entry->type = OBJ_BLOB;
+       } else if (!strcmp(type, tag_type)) {
+               entry->type = OBJ_TAG;
+       } else
+               die("unable to pack object %s of type %s",
+                   sha1_to_hex(entry->sha1), type);
+}
+
+static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
+{
+       struct object_entry *child = me->delta_child;
+       unsigned int m = n;
+       while (child) {
+               unsigned int c = check_delta_limit(child, n + 1);
+               if (m < c)
+                       m = c;
+               child = child->delta_sibling;
+       }
+       return m;
+}
+
+static void get_object_details(void)
+{
+       int i;
+       struct object_entry *entry;
+
+       prepare_pack_ix();
+       for (i = 0, entry = objects; i < nr_objects; i++, entry++)
+               check_object(entry);
+
+       if (nr_objects == nr_result) {
+               /*
+                * Depth of objects that depend on the entry -- this
+                * is subtracted from depth-max to break too deep
+                * delta chain because of delta data reusing.
+                * However, we loosen this restriction when we know we
+                * are creating a thin pack -- it will have to be
+                * expanded on the other end anyway, so do not
+                * artificially cut the delta chain and let it go as
+                * deep as it wants.
+                */
+               for (i = 0, entry = objects; i < nr_objects; i++, entry++)
+                       if (!entry->delta && entry->delta_child)
+                               entry->delta_limit =
+                                       check_delta_limit(entry, 1);
+       }
+}
+
+typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
+
+static entry_sort_t current_sort;
+
+static int sort_comparator(const void *_a, const void *_b)
+{
+       struct object_entry *a = *(struct object_entry **)_a;
+       struct object_entry *b = *(struct object_entry **)_b;
+       return current_sort(a,b);
+}
+
+static struct object_entry **create_sorted_list(entry_sort_t sort)
+{
+       struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
+       int i;
+
+       for (i = 0; i < nr_objects; i++)
+               list[i] = objects + i;
+       current_sort = sort;
+       qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
+       return list;
+}
+
+static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
+{
+       return memcmp(a->sha1, b->sha1, 20);
+}
+
+static struct object_entry **create_final_object_list(void)
+{
+       struct object_entry **list;
+       int i, j;
+
+       for (i = nr_result = 0; i < nr_objects; i++)
+               if (!objects[i].preferred_base)
+                       nr_result++;
+       list = xmalloc(nr_result * sizeof(struct object_entry *));
+       for (i = j = 0; i < nr_objects; i++) {
+               if (!objects[i].preferred_base)
+                       list[j++] = objects + i;
+       }
+       current_sort = sha1_sort;
+       qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator);
+       return list;
+}
+
+static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
+{
+       if (a->type < b->type)
+               return -1;
+       if (a->type > b->type)
+               return 1;
+       if (a->hash < b->hash)
+               return -1;
+       if (a->hash > b->hash)
+               return 1;
+       if (a->preferred_base < b->preferred_base)
+               return -1;
+       if (a->preferred_base > b->preferred_base)
+               return 1;
+       if (a->size < b->size)
+               return -1;
+       if (a->size > b->size)
+               return 1;
+       return a < b ? -1 : (a > b);
+}
+
+struct unpacked {
+       struct object_entry *entry;
+       void *data;
+       struct delta_index *index;
+};
+
+/*
+ * We search for deltas _backwards_ in a list sorted by type and
+ * by size, so that we see progressively smaller and smaller files.
+ * That's because we prefer deltas to be from the bigger file
+ * to the smaller - deletes are potentially cheaper, but perhaps
+ * more importantly, the bigger file is likely the more recent
+ * one.
+ */
+static int try_delta(struct unpacked *trg, struct unpacked *src,
+                    unsigned max_depth)
+{
+       struct object_entry *trg_entry = trg->entry;
+       struct object_entry *src_entry = src->entry;
+       unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
+       char type[10];
+       void *delta_buf;
+
+       /* Don't bother doing diffs between different types */
+       if (trg_entry->type != src_entry->type)
+               return -1;
+
+       /* We do not compute delta to *create* objects we are not
+        * going to pack.
+        */
+       if (trg_entry->preferred_base)
+               return -1;
+
+       /*
+        * We do not bother to try a delta that we discarded
+        * on an earlier try, but only when reusing delta data.
+        */
+       if (!no_reuse_delta && trg_entry->in_pack &&
+           trg_entry->in_pack == src_entry->in_pack)
+               return 0;
+
+       /*
+        * 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 (trg_entry->delta_child) {
+               if (max_depth <= trg_entry->delta_limit)
+                       return 0;
+               max_depth -= trg_entry->delta_limit;
+       }
+       if (src_entry->depth >= max_depth)
+               return 0;
+
+       /* Now some size filtering heuristics. */
+       trg_size = trg_entry->size;
+       max_size = trg_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 < trg_size ? trg_size - src_size : 0;
+       if (sizediff >= max_size)
+               return 0;
+
+       /* Load data if not already done */
+       if (!trg->data) {
+               trg->data = read_sha1_file(trg_entry->sha1, type, &sz);
+               if (sz != trg_size)
+                       die("object %s inconsistent object length (%lu vs %lu)",
+                           sha1_to_hex(trg_entry->sha1), sz, trg_size);
+       }
+       if (!src->data) {
+               src->data = read_sha1_file(src_entry->sha1, type, &sz);
+               if (sz != src_size)
+                       die("object %s inconsistent object length (%lu vs %lu)",
+                           sha1_to_hex(src_entry->sha1), sz, src_size);
+       }
+       if (!src->index) {
+               src->index = create_delta_index(src->data, src_size);
+               if (!src->index)
+                       die("out of memory");
+       }
+
+       delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
+       if (!delta_buf)
+               return 0;
+
+       trg_entry->delta = src_entry;
+       trg_entry->delta_size = delta_size;
+       trg_entry->depth = src_entry->depth + 1;
+       free(delta_buf);
+       return 1;
+}
+
+static void progress_interval(int signum)
+{
+       progress_update = 1;
+}
+
+static void find_deltas(struct object_entry **list, int window, int depth)
+{
+       int i, idx;
+       unsigned int array_size = window * sizeof(struct unpacked);
+       struct unpacked *array = xmalloc(array_size);
+       unsigned processed = 0;
+       unsigned last_percent = 999;
+
+       memset(array, 0, array_size);
+       i = nr_objects;
+       idx = 0;
+       if (progress)
+               fprintf(stderr, "Deltifying %d objects.\n", nr_result);
+
+       while (--i >= 0) {
+               struct object_entry *entry = list[i];
+               struct unpacked *n = array + idx;
+               int j;
+
+               if (!entry->preferred_base)
+                       processed++;
+
+               if (progress) {
+                       unsigned percent = processed * 100 / nr_result;
+                       if (percent != last_percent || progress_update) {
+                               fprintf(stderr, "%4u%% (%u/%u) done\r",
+                                       percent, processed, nr_result);
+                               progress_update = 0;
+                               last_percent = percent;
+                       }
+               }
+
+               if (entry->delta)
+                       /* This happens if we decided to reuse existing
+                        * delta from a pack.  "!no_reuse_delta &&" is implied.
+                        */
+                       continue;
+
+               if (entry->size < 50)
+                       continue;
+               free_delta_index(n->index);
+               n->index = NULL;
+               free(n->data);
+               n->data = NULL;
+               n->entry = entry;
+
+               j = window;
+               while (--j > 0) {
+                       unsigned int other_idx = idx + j;
+                       struct unpacked *m;
+                       if (other_idx >= window)
+                               other_idx -= window;
+                       m = array + other_idx;
+                       if (!m->entry)
+                               break;
+                       if (try_delta(n, m, depth) < 0)
+                               break;
+               }
+               /* 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.
+                */
+               if (entry->delta && depth <= entry->depth)
+                       continue;
+
+               idx++;
+               if (idx >= window)
+                       idx = 0;
+       }
+
+       if (progress)
+               fputc('\n', stderr);
+
+       for (i = 0; i < window; ++i) {
+               free_delta_index(array[i].index);
+               free(array[i].data);
+       }
+       free(array);
+}
+
+static void prepare_pack(int window, int depth)
+{
+       get_object_details();
+       sorted_by_type = create_sorted_list(type_size_sort);
+       if (window && depth)
+               find_deltas(sorted_by_type, window+1, depth);
+}
+
+static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
+{
+       static const char cache[] = "pack-cache/pack-%s.%s";
+       char *cached_pack, *cached_idx;
+       int ifd, ofd, ifd_ix = -1;
+
+       cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
+       ifd = open(cached_pack, O_RDONLY);
+       if (ifd < 0)
+               return 0;
+
+       if (!pack_to_stdout) {
+               cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
+               ifd_ix = open(cached_idx, O_RDONLY);
+               if (ifd_ix < 0) {
+                       close(ifd);
+                       return 0;
+               }
+       }
+
+       if (progress)
+               fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
+                       sha1_to_hex(sha1));
+
+       if (pack_to_stdout) {
+               if (copy_fd(ifd, 1))
+                       exit(1);
+               close(ifd);
+       }
+       else {
+               char name[PATH_MAX];
+               snprintf(name, sizeof(name),
+                        "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
+               ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
+               if (ofd < 0)
+                       die("unable to open %s (%s)", name, strerror(errno));
+               if (copy_fd(ifd, ofd))
+                       exit(1);
+               close(ifd);
+
+               snprintf(name, sizeof(name),
+                        "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
+               ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
+               if (ofd < 0)
+                       die("unable to open %s (%s)", name, strerror(errno));
+               if (copy_fd(ifd_ix, ofd))
+                       exit(1);
+               close(ifd_ix);
+               puts(sha1_to_hex(sha1));
+       }
+
+       return 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 int git_pack_config(const char *k, const char *v)
+{
+       if(!strcmp(k, "pack.window")) {
+               window = git_config_int(k, v);
+               return 0;
+       }
+       return git_default_config(k, v);
+}
+
+int cmd_pack_objects(int argc, const char **argv, const char *prefix)
+{
+       SHA_CTX ctx;
+       char line[40 + 1 + PATH_MAX + 2];
+       int depth = 10, pack_to_stdout = 0;
+       struct object_entry **list;
+       int num_preferred_base = 0;
+       int i;
+
+       git_config(git_pack_config);
+
+       progress = isatty(2);
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+
+               if (*arg == '-') {
+                       if (!strcmp("--non-empty", arg)) {
+                               non_empty = 1;
+                               continue;
+                       }
+                       if (!strcmp("--local", arg)) {
+                               local = 1;
+                               continue;
+                       }
+                       if (!strcmp("--progress", arg)) {
+                               progress = 1;
+                               continue;
+                       }
+                       if (!strcmp("--incremental", arg)) {
+                               incremental = 1;
+                               continue;
+                       }
+                       if (!strncmp("--window=", arg, 9)) {
+                               char *end;
+                               window = strtoul(arg+9, &end, 0);
+                               if (!arg[9] || *end)
+                                       usage(pack_usage);
+                               continue;
+                       }
+                       if (!strncmp("--depth=", arg, 8)) {
+                               char *end;
+                               depth = strtoul(arg+8, &end, 0);
+                               if (!arg[8] || *end)
+                                       usage(pack_usage);
+                               continue;
+                       }
+                       if (!strcmp("--progress", arg)) {
+                               progress = 1;
+                               continue;
+                       }
+                       if (!strcmp("-q", arg)) {
+                               progress = 0;
+                               continue;
+                       }
+                       if (!strcmp("--no-reuse-delta", arg)) {
+                               no_reuse_delta = 1;
+                               continue;
+                       }
+                       if (!strcmp("--stdout", arg)) {
+                               pack_to_stdout = 1;
+                               continue;
+                       }
+                       usage(pack_usage);
+               }
+               if (base_name)
+                       usage(pack_usage);
+               base_name = arg;
+       }
+
+       if (pack_to_stdout != !base_name)
+               usage(pack_usage);
+
+       prepare_packed_git();
+
+       if (progress) {
+               fprintf(stderr, "Generating pack...\n");
+               setup_progress_signal();
+       }
+
+       for (;;) {
+               unsigned char sha1[20];
+               unsigned hash;
+
+               if (!fgets(line, sizeof(line), stdin)) {
+                       if (feof(stdin))
+                               break;
+                       if (!ferror(stdin))
+                               die("fgets returned NULL, not EOF, not error!");
+                       if (errno != EINTR)
+                               die("fgets: %s", strerror(errno));
+                       clearerr(stdin);
+                       continue;
+               }
+
+               if (line[0] == '-') {
+                       if (get_sha1_hex(line+1, sha1))
+                               die("expected edge sha1, got garbage:\n %s",
+                                   line+1);
+                       if (num_preferred_base++ < window)
+                               add_preferred_base(sha1);
+                       continue;
+               }
+               if (get_sha1_hex(line, sha1))
+                       die("expected sha1, got garbage:\n %s", line);
+               hash = name_hash(line+41);
+               add_preferred_base_object(line+41, hash);
+               add_object_entry(sha1, hash, 0);
+       }
+       if (progress)
+               fprintf(stderr, "Done counting %d objects.\n", nr_objects);
+       sorted_by_sha = create_final_object_list();
+       if (non_empty && !nr_result)
+               return 0;
+
+       SHA1_Init(&ctx);
+       list = sorted_by_sha;
+       for (i = 0; i < nr_result; i++) {
+               struct object_entry *entry = *list++;
+               SHA1_Update(&ctx, entry->sha1, 20);
+       }
+       SHA1_Final(object_list_sha1, &ctx);
+       if (progress && (nr_objects != nr_result))
+               fprintf(stderr, "Result has %d objects.\n", nr_result);
+
+       if (reuse_cached_pack(object_list_sha1, pack_to_stdout))
+               ;
+       else {
+               if (nr_result)
+                       prepare_pack(window, depth);
+               if (progress && pack_to_stdout) {
+                       /* the other end usually displays progress itself */
+                       struct itimerval v = {{0,},};
+                       setitimer(ITIMER_REAL, &v, NULL);
+                       signal(SIGALRM, SIG_IGN );
+                       progress_update = 0;
+               }
+               write_pack_file();
+               if (!pack_to_stdout) {
+                       write_index_file();
+                       puts(sha1_to_hex(object_list_sha1));
+               }
+       }
+       if (progress)
+               fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
+                       nr_result, written, written_delta, reused, reused_delta);
+       return 0;
+}
index b30160a5b36c3a2cd2cf941728dc4c3d6e76c36d..71a7026df4bec244acdf36ef9559eecc5b65fae3 100644 (file)
@@ -884,9 +884,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
 
        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");
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
 
        git_config(git_default_config);
 
index 8af3d7eb48e70dc9a640c2e96a058903fb9fddd8..593d86744c41a1f28258b3adb6e6cd556a64b61f 100644 (file)
@@ -52,9 +52,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 
        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");
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
 
        if (read_cache() < 0)
                die("index file corrupt");
diff --git a/builtin-symbolic-ref.c b/builtin-symbolic-ref.c
new file mode 100644 (file)
index 0000000..b4ec6f2
--- /dev/null
@@ -0,0 +1,35 @@
+#include "builtin.h"
+#include "cache.h"
+
+static const char git_symbolic_ref_usage[] =
+"git-symbolic-ref name [ref]";
+
+static void check_symref(const char *HEAD)
+{
+       unsigned char sha1[20];
+       const char *git_HEAD = strdup(git_path("%s", HEAD));
+       const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 0);
+       if (git_refs_heads_master) {
+               /* we want to strip the .git/ part */
+               int pfxlen = strlen(git_HEAD) - strlen(HEAD);
+               puts(git_refs_heads_master + pfxlen);
+       }
+       else
+               die("No such ref: %s", HEAD);
+}
+
+int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
+{
+       git_config(git_default_config);
+       switch (argc) {
+       case 2:
+               check_symref(argv[1]);
+               break;
+       case 3:
+               create_symref(strdup(git_path("%s", argv[1])), argv[2]);
+               break;
+       default:
+               usage(git_symbolic_ref_usage);
+       }
+       return 0;
+}
diff --git a/builtin-unpack-objects.c b/builtin-unpack-objects.c
new file mode 100644 (file)
index 0000000..09d264d
--- /dev/null
@@ -0,0 +1,310 @@
+#include "builtin.h"
+#include "cache.h"
+#include "object.h"
+#include "delta.h"
+#include "pack.h"
+#include "blob.h"
+#include "commit.h"
+#include "tag.h"
+#include "tree.h"
+
+#include <sys/time.h>
+
+static int dry_run, quiet;
+static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
+
+/* We always read in 4kB chunks. */
+static unsigned char buffer[4096];
+static unsigned long offset, len, eof;
+static SHA_CTX ctx;
+
+/*
+ * Make sure at least "min" bytes are available in the buffer, and
+ * return the pointer to the buffer.
+ */
+static void * fill(int min)
+{
+       if (min <= len)
+               return buffer + offset;
+       if (eof)
+               die("unable to fill input");
+       if (min > sizeof(buffer))
+               die("cannot fill %d bytes", min);
+       if (offset) {
+               SHA1_Update(&ctx, buffer, offset);
+               memcpy(buffer, buffer + offset, len);
+               offset = 0;
+       }
+       do {
+               int ret = xread(0, buffer + len, sizeof(buffer) - len);
+               if (ret <= 0) {
+                       if (!ret)
+                               die("early EOF");
+                       die("read error on input: %s", strerror(errno));
+               }
+               len += ret;
+       } while (len < min);
+       return buffer;
+}
+
+static void use(int bytes)
+{
+       if (bytes > len)
+               die("used more bytes than were available");
+       len -= bytes;
+       offset += bytes;
+}
+
+static void *get_data(unsigned long size)
+{
+       z_stream stream;
+       void *buf = xmalloc(size);
+
+       memset(&stream, 0, sizeof(stream));
+
+       stream.next_out = buf;
+       stream.avail_out = size;
+       stream.next_in = fill(1);
+       stream.avail_in = len;
+       inflateInit(&stream);
+
+       for (;;) {
+               int ret = inflate(&stream, 0);
+               use(len - stream.avail_in);
+               if (stream.total_out == size && ret == Z_STREAM_END)
+                       break;
+               if (ret != Z_OK)
+                       die("inflate returned %d\n", ret);
+               stream.next_in = fill(1);
+               stream.avail_in = len;
+       }
+       inflateEnd(&stream);
+       return buf;
+}
+
+struct delta_info {
+       unsigned char base_sha1[20];
+       unsigned long size;
+       void *delta;
+       struct delta_info *next;
+};
+
+static struct delta_info *delta_list;
+
+static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
+{
+       struct delta_info *info = xmalloc(sizeof(*info));
+
+       memcpy(info->base_sha1, base_sha1, 20);
+       info->size = size;
+       info->delta = delta;
+       info->next = delta_list;
+       delta_list = info;
+}
+
+static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
+
+static void write_object(void *buf, unsigned long size, const char *type)
+{
+       unsigned char sha1[20];
+       if (write_sha1_file(buf, size, type, sha1) < 0)
+               die("failed to write object");
+       added_object(sha1, type, buf, size);
+}
+
+static int resolve_delta(const char *type,
+       void *base, unsigned long base_size,
+       void *delta, unsigned long delta_size)
+{
+       void *result;
+       unsigned long result_size;
+
+       result = patch_delta(base, base_size,
+                            delta, delta_size,
+                            &result_size);
+       if (!result)
+               die("failed to apply delta");
+       free(delta);
+       write_object(result, result_size, type);
+       free(result);
+       return 0;
+}
+
+static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
+{
+       struct delta_info **p = &delta_list;
+       struct delta_info *info;
+
+       while ((info = *p) != NULL) {
+               if (!memcmp(info->base_sha1, sha1, 20)) {
+                       *p = info->next;
+                       p = &delta_list;
+                       resolve_delta(type, data, size, info->delta, info->size);
+                       free(info);
+                       continue;
+               }
+               p = &info->next;
+       }
+}
+
+static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
+{
+       void *buf = get_data(size);
+       const char *type;
+
+       switch (kind) {
+       case OBJ_COMMIT: type = commit_type; break;
+       case OBJ_TREE:   type = tree_type; break;
+       case OBJ_BLOB:   type = blob_type; break;
+       case OBJ_TAG:    type = tag_type; break;
+       default: die("bad type %d", kind);
+       }
+       if (!dry_run)
+               write_object(buf, size, type);
+       free(buf);
+       return 0;
+}
+
+static int unpack_delta_entry(unsigned long delta_size)
+{
+       void *delta_data, *base;
+       unsigned long base_size;
+       char type[20];
+       unsigned char base_sha1[20];
+       int result;
+
+       memcpy(base_sha1, fill(20), 20);
+       use(20);
+
+       delta_data = get_data(delta_size);
+       if (dry_run) {
+               free(delta_data);
+               return 0;
+       }
+
+       if (!has_sha1_file(base_sha1)) {
+               add_delta_to_list(base_sha1, delta_data, delta_size);
+               return 0;
+       }
+       base = read_sha1_file(base_sha1, type, &base_size);
+       if (!base)
+               die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
+       result = resolve_delta(type, base, base_size, delta_data, delta_size);
+       free(base);
+       return result;
+}
+
+static void unpack_one(unsigned nr, unsigned total)
+{
+       unsigned shift;
+       unsigned char *pack, c;
+       unsigned long size;
+       enum object_type type;
+
+       pack = fill(1);
+       c = *pack;
+       use(1);
+       type = (c >> 4) & 7;
+       size = (c & 15);
+       shift = 4;
+       while (c & 0x80) {
+               pack = fill(1);
+               c = *pack++;
+               use(1);
+               size += (c & 0x7f) << shift;
+               shift += 7;
+       }
+       if (!quiet) {
+               static unsigned long last_sec;
+               static unsigned last_percent;
+               struct timeval now;
+               unsigned percentage = (nr * 100) / total;
+
+               gettimeofday(&now, NULL);
+               if (percentage != last_percent || now.tv_sec != last_sec) {
+                       last_sec = now.tv_sec;
+                       last_percent = percentage;
+                       fprintf(stderr, "%4u%% (%u/%u) done\r", percentage, nr, total);
+               }
+       }
+       switch (type) {
+       case OBJ_COMMIT:
+       case OBJ_TREE:
+       case OBJ_BLOB:
+       case OBJ_TAG:
+               unpack_non_delta_entry(type, size);
+               return;
+       case OBJ_DELTA:
+               unpack_delta_entry(size);
+               return;
+       default:
+               die("bad object type %d", type);
+       }
+}
+
+static void unpack_all(void)
+{
+       int i;
+       struct pack_header *hdr = fill(sizeof(struct pack_header));
+       unsigned nr_objects = ntohl(hdr->hdr_entries);
+
+       if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
+               die("bad pack file");
+       if (!pack_version_ok(hdr->hdr_version))
+               die("unknown pack file version %d", ntohl(hdr->hdr_version));
+       fprintf(stderr, "Unpacking %d objects\n", nr_objects);
+
+       use(sizeof(struct pack_header));
+       for (i = 0; i < nr_objects; i++)
+               unpack_one(i+1, nr_objects);
+       if (delta_list)
+               die("unresolved deltas left after unpacking");
+}
+
+int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
+{
+       int i;
+       unsigned char sha1[20];
+
+       quiet = !isatty(2);
+
+       for (i = 1 ; i < argc; i++) {
+               const char *arg = argv[i];
+
+               if (*arg == '-') {
+                       if (!strcmp(arg, "-n")) {
+                               dry_run = 1;
+                               continue;
+                       }
+                       if (!strcmp(arg, "-q")) {
+                               quiet = 1;
+                               continue;
+                       }
+                       usage(unpack_usage);
+               }
+
+               /* We don't take any non-flag arguments now.. Maybe some day */
+               usage(unpack_usage);
+       }
+       SHA1_Init(&ctx);
+       unpack_all();
+       SHA1_Update(&ctx, buffer, offset);
+       SHA1_Final(sha1, &ctx);
+       if (memcmp(fill(20), sha1, 20))
+               die("final sha1 did not match");
+       use(20);
+
+       /* Write the last part of the buffer to stdout */
+       while (len) {
+               int ret = xwrite(1, buffer + offset, len);
+               if (ret <= 0)
+                       break;
+               len -= ret;
+               offset += ret;
+       }
+
+       /* All done */
+       if (!quiet)
+               fprintf(stderr, "\n");
+       return 0;
+}
index 24dca47d8d6e54429aa89b04e4d67bda04fa0173..d2556f376b293d2907cd8416e99f9c0bd31781ca 100644 (file)
@@ -491,9 +491,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
        /* We can't free this memory, it becomes part of a linked list parsed atexit() */
        lock_file = xcalloc(1, sizeof(struct lock_file));
 
-       newfd = hold_lock_file_for_update(lock_file, get_index_file());
-       if (newfd < 0)
-               die("unable to create new cachefile");
+       newfd = hold_lock_file_for_update(lock_file, get_index_file(), 1);
 
        entries = read_cache();
        if (entries < 0)
diff --git a/builtin-verify-pack.c b/builtin-verify-pack.c
new file mode 100644 (file)
index 0000000..7d39d9b
--- /dev/null
@@ -0,0 +1,79 @@
+#include "builtin.h"
+#include "cache.h"
+#include "pack.h"
+
+static int verify_one_pack(const char *path, int verbose)
+{
+       char arg[PATH_MAX];
+       int len;
+       struct packed_git *pack;
+       int err;
+
+       len = strlcpy(arg, path, PATH_MAX);
+       if (len >= PATH_MAX)
+               return error("name too long: %s", path);
+
+       /*
+        * In addition to "foo.idx" we accept "foo.pack" and "foo";
+        * normalize these forms to "foo.idx" for add_packed_git().
+        */
+       if (has_extension(arg, ".pack")) {
+               strcpy(arg + len - 5, ".idx");
+               len--;
+       } else if (!has_extension(arg, ".idx")) {
+               if (len + 4 >= PATH_MAX)
+                       return error("name too long: %s.idx", arg);
+               strcpy(arg + len, ".idx");
+               len += 4;
+       }
+
+       /*
+        * add_packed_git() uses our buffer (containing "foo.idx") to
+        * build the pack filename ("foo.pack").  Make sure it fits.
+        */
+       if (len + 1 >= PATH_MAX) {
+               arg[len - 4] = '\0';
+               return error("name too long: %s.pack", arg);
+       }
+
+       pack = add_packed_git(arg, len, 1);
+       if (!pack)
+               return error("packfile %s not found.", arg);
+
+       err = verify_pack(pack, verbose);
+       free(pack);
+
+       return err;
+}
+
+static const char verify_pack_usage[] = "git-verify-pack [-v] <pack>...";
+
+int cmd_verify_pack(int argc, const char **argv, const char *prefix)
+{
+       int err = 0;
+       int verbose = 0;
+       int no_more_options = 0;
+       int nothing_done = 1;
+
+       while (1 < argc) {
+               if (!no_more_options && argv[1][0] == '-') {
+                       if (!strcmp("-v", argv[1]))
+                               verbose = 1;
+                       else if (!strcmp("--", argv[1]))
+                               no_more_options = 1;
+                       else
+                               usage(verify_pack_usage);
+               }
+               else {
+                       if (verify_one_pack(argv[1], verbose))
+                               err = 1;
+                       nothing_done = 0;
+               }
+               argc--; argv++;
+       }
+
+       if (nothing_done)
+               usage(verify_pack_usage);
+
+       return err;
+}
index 6b62d7dc8c8b17bd0ff6b639a9bab18400d2177d..ca06149f186449407c6536fcc3caa37cd4101ce3 100644 (file)
@@ -18,7 +18,7 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
        /* We can't free this memory, it becomes part of a linked list parsed atexit() */
        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 
-       newfd = hold_lock_file_for_update(lock_file, get_index_file());
+       newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0);
 
        entries = read_cache();
        if (entries < 0)
index 26ebcaf213111724e8131e9edc8eeb8672e8071e..ade58c4a1f06b161f00caa015192fa89c137d273 100644 (file)
--- a/builtin.h
+++ b/builtin.h
@@ -8,57 +8,57 @@ extern const char git_version_string[];
 extern const char git_usage_string[];
 
 extern void help_unknown_cmd(const char *cmd);
+extern int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, const char *msg, const char *patch);
+extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip);
+extern void stripspace(FILE *in, FILE *out);
+extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
 
-extern int cmd_help(int argc, const char **argv, const char *prefix);
-extern int cmd_version(int argc, const char **argv, const char *prefix);
-
-extern int cmd_whatchanged(int argc, const char **argv, const char *prefix);
-extern int cmd_show(int argc, const char **argv, const char *prefix);
-extern int cmd_log(int argc, const char **argv, const char *prefix);
-extern int cmd_diff(int argc, const char **argv, const char *prefix);
-extern int cmd_format_patch(int argc, const char **argv, const char *prefix);
-extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
-
-extern int cmd_prune(int argc, const char **argv, const char *prefix);
-extern int cmd_prune_packed(int argc, const char **argv, const char *prefix);
-
-extern int cmd_push(int argc, const char **argv, const char *prefix);
-extern int cmd_grep(int argc, const char **argv, const char *prefix);
-extern int cmd_rm(int argc, const char **argv, const char *prefix);
 extern int cmd_add(int argc, const char **argv, const char *prefix);
-extern int cmd_rev_list(int argc, const char **argv, const char *prefix);
+extern int cmd_apply(int argc, const char **argv, const char *prefix);
+extern int cmd_cat_file(int argc, const char **argv, const char *prefix);
+extern int cmd_checkout_index(int argc, const char **argv, const char *prefix);
 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
-extern int cmd_init_db(int argc, const char **argv, const char *prefix);
-extern int cmd_tar_tree(int argc, const char **argv, const char *prefix);
-extern int cmd_upload_tar(int argc, const char **argv, const char *prefix);
-extern int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix);
-extern int cmd_ls_files(int argc, const char **argv, const char *prefix);
-extern int cmd_ls_tree(int argc, const char **argv, const char *prefix);
-extern int cmd_read_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
-extern int cmd_apply(int argc, const char **argv, const char *prefix);
-extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
+extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
 extern int cmd_diff_files(int argc, const char **argv, const char *prefix);
 extern int cmd_diff_index(int argc, const char **argv, const char *prefix);
+extern int cmd_diff(int argc, const char **argv, const char *prefix);
 extern int cmd_diff_stages(int argc, const char **argv, const char *prefix);
 extern int cmd_diff_tree(int argc, const char **argv, const char *prefix);
-extern int cmd_cat_file(int argc, const char **argv, const char *prefix);
-extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
-extern int cmd_update_index(int argc, const char **argv, const char *prefix);
-extern int cmd_update_ref(int argc, const char **argv, const char *prefix);
 extern int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix);
+extern int cmd_format_patch(int argc, const char **argv, const char *prefix);
+extern int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix);
+extern int cmd_grep(int argc, const char **argv, const char *prefix);
+extern int cmd_help(int argc, const char **argv, const char *prefix);
+extern int cmd_init_db(int argc, const char **argv, const char *prefix);
+extern int cmd_log(int argc, const char **argv, const char *prefix);
+extern int cmd_ls_files(int argc, const char **argv, const char *prefix);
+extern int cmd_ls_tree(int argc, const char **argv, const char *prefix);
+extern int cmd_mailinfo(int argc, const char **argv, const char *prefix);
+extern int cmd_mailsplit(int argc, const char **argv, const char *prefix);
 extern int cmd_mv(int argc, const char **argv, const char *prefix);
+extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
+extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);
+extern int cmd_prune(int argc, const char **argv, const char *prefix);
+extern int cmd_prune_packed(int argc, const char **argv, const char *prefix);
+extern int cmd_push(int argc, const char **argv, const char *prefix);
+extern int cmd_read_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_repo_config(int argc, const char **argv, const char *prefix);
-
+extern int cmd_rev_list(int argc, const char **argv, const char *prefix);
+extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
+extern int cmd_rm(int argc, const char **argv, const char *prefix);
+extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
+extern int cmd_show(int argc, const char **argv, const char *prefix);
+extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
+extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
+extern int cmd_tar_tree(int argc, const char **argv, const char *prefix);
+extern int cmd_unpack_objects(int argc, const char **argv, const char *prefix);
+extern int cmd_update_index(int argc, const char **argv, const char *prefix);
+extern int cmd_update_ref(int argc, const char **argv, const char *prefix);
+extern int cmd_upload_tar(int argc, const char **argv, const char *prefix);
+extern int cmd_version(int argc, const char **argv, const char *prefix);
+extern int cmd_whatchanged(int argc, const char **argv, const char *prefix);
 extern int cmd_write_tree(int argc, const char **argv, const char *prefix);
-extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
+extern int cmd_verify_pack(int argc, const char **argv, const char *prefix);
 
-extern int cmd_mailsplit(int argc, const char **argv, const char *prefix);
-extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip);
-
-extern int cmd_mailinfo(int argc, const char **argv, const char *prefix);
-extern int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, const char *msg, const char *patch);
-
-extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
-extern void stripspace(FILE *in, FILE *out);
 #endif
diff --git a/cache.h b/cache.h
index b8c21e07b2e714de8e4e662b31a41ff06c5e0c9a..b2ab2088e37d39a4c8e1927591015bf05a813233 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -175,7 +175,7 @@ struct lock_file {
        struct lock_file *next;
        char filename[PATH_MAX];
 };
-extern int hold_lock_file_for_update(struct lock_file *, const char *path);
+extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
 extern int commit_lock_file(struct lock_file *);
 extern void rollback_lock_file(struct lock_file *);
 
diff --git a/checkout-index.c b/checkout-index.c
deleted file mode 100644 (file)
index 61152f3..0000000
+++ /dev/null
@@ -1,311 +0,0 @@
-/*
- * Check-out files from the "current cache directory"
- *
- * Copyright (C) 2005 Linus Torvalds
- *
- * Careful: order of argument flags does matter. For example,
- *
- *     git-checkout-index -a -f file.c
- *
- * Will first check out all files listed in the cache (but not
- * overwrite any old ones), and then force-checkout "file.c" a
- * second time (ie that one _will_ overwrite any old contents
- * with the same filename).
- *
- * Also, just doing "git-checkout-index" does nothing. You probably
- * meant "git-checkout-index -a". And if you want to force it, you
- * want "git-checkout-index -f -a".
- *
- * Intuitiveness is not the goal here. Repeatability is. The
- * reason for the "no arguments means no work" thing is that
- * from scripts you are supposed to be able to do things like
- *
- *     find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
- *
- * or:
- *
- *     find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
- *
- * which will force all existing *.h files to be replaced with
- * their cached copies. If an empty command line implied "all",
- * then this would force-refresh everything in the cache, which
- * was not the point.
- *
- * Oh, and the "--" is just a good idea when you know the rest
- * will be filenames. Just so that you wouldn't have a filename
- * of "-a" causing problems (not possible in the above example,
- * but get used to it in scripting!).
- */
-#include "cache.h"
-#include "strbuf.h"
-#include "quote.h"
-#include "cache-tree.h"
-
-#define CHECKOUT_ALL 4
-static const char *prefix;
-static int prefix_length;
-static int line_termination = '\n';
-static int checkout_stage; /* default to checkout stage0 */
-static int to_tempfile;
-static char topath[4][MAXPATHLEN+1];
-
-static struct checkout state;
-
-static void write_tempfile_record (const char *name)
-{
-       int i;
-
-       if (CHECKOUT_ALL == checkout_stage) {
-               for (i = 1; i < 4; i++) {
-                       if (i > 1)
-                               putchar(' ');
-                       if (topath[i][0])
-                               fputs(topath[i], stdout);
-                       else
-                               putchar('.');
-               }
-       } else
-               fputs(topath[checkout_stage], stdout);
-
-       putchar('\t');
-       write_name_quoted("", 0, name + prefix_length,
-               line_termination, stdout);
-       putchar(line_termination);
-
-       for (i = 0; i < 4; i++) {
-               topath[i][0] = 0;
-       }
-}
-
-static int checkout_file(const char *name)
-{
-       int namelen = strlen(name);
-       int pos = cache_name_pos(name, namelen);
-       int has_same_name = 0;
-       int did_checkout = 0;
-       int errs = 0;
-
-       if (pos < 0)
-               pos = -pos - 1;
-
-       while (pos < active_nr) {
-               struct cache_entry *ce = active_cache[pos];
-               if (ce_namelen(ce) != namelen ||
-                   memcmp(ce->name, name, namelen))
-                       break;
-               has_same_name = 1;
-               pos++;
-               if (ce_stage(ce) != checkout_stage
-                   && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
-                       continue;
-               did_checkout = 1;
-               if (checkout_entry(ce, &state,
-                   to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
-                       errs++;
-       }
-
-       if (did_checkout) {
-               if (to_tempfile)
-                       write_tempfile_record(name);
-               return errs > 0 ? -1 : 0;
-       }
-
-       if (!state.quiet) {
-               fprintf(stderr, "git-checkout-index: %s ", name);
-               if (!has_same_name)
-                       fprintf(stderr, "is not in the cache");
-               else if (checkout_stage)
-                       fprintf(stderr, "does not exist at stage %d",
-                               checkout_stage);
-               else
-                       fprintf(stderr, "is unmerged");
-               fputc('\n', stderr);
-       }
-       return -1;
-}
-
-static int checkout_all(void)
-{
-       int i, errs = 0;
-       struct cache_entry* last_ce = NULL;
-
-       for (i = 0; i < active_nr ; i++) {
-               struct cache_entry *ce = active_cache[i];
-               if (ce_stage(ce) != checkout_stage
-                   && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
-                       continue;
-               if (prefix && *prefix &&
-                   (ce_namelen(ce) <= prefix_length ||
-                    memcmp(prefix, ce->name, prefix_length)))
-                       continue;
-               if (last_ce && to_tempfile) {
-                       if (ce_namelen(last_ce) != ce_namelen(ce)
-                           || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
-                               write_tempfile_record(last_ce->name);
-               }
-               if (checkout_entry(ce, &state,
-                   to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
-                       errs++;
-               last_ce = ce;
-       }
-       if (last_ce && to_tempfile)
-               write_tempfile_record(last_ce->name);
-       if (errs)
-               /* we have already done our error reporting.
-                * exit with the same code as die().
-                */
-               exit(128);
-       return 0;
-}
-
-static const char checkout_cache_usage[] =
-"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
-
-static struct lock_file lock_file;
-
-int main(int argc, char **argv)
-{
-       int i;
-       int newfd = -1;
-       int all = 0;
-       int read_from_stdin = 0;
-
-       state.base_dir = "";
-       prefix = setup_git_directory();
-       git_config(git_default_config);
-       prefix_length = prefix ? strlen(prefix) : 0;
-
-       if (read_cache() < 0) {
-               die("invalid cache");
-       }
-
-       for (i = 1; i < argc; i++) {
-               const char *arg = argv[i];
-
-               if (!strcmp(arg, "--")) {
-                       i++;
-                       break;
-               }
-               if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
-                       all = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
-                       state.force = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
-                       state.quiet = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
-                       state.not_new = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
-                       state.refresh_cache = 1;
-                       if (newfd < 0)
-                               newfd = hold_lock_file_for_update
-                                       (&lock_file, get_index_file());
-                       if (newfd < 0)
-                               die("cannot open index.lock file.");
-                       continue;
-               }
-               if (!strcmp(arg, "-z")) {
-                       line_termination = 0;
-                       continue;
-               }
-               if (!strcmp(arg, "--stdin")) {
-                       if (i != argc - 1)
-                               die("--stdin must be at the end");
-                       read_from_stdin = 1;
-                       i++; /* do not consider arg as a file name */
-                       break;
-               }
-               if (!strcmp(arg, "--temp")) {
-                       to_tempfile = 1;
-                       continue;
-               }
-               if (!strncmp(arg, "--prefix=", 9)) {
-                       state.base_dir = arg+9;
-                       state.base_dir_len = strlen(state.base_dir);
-                       continue;
-               }
-               if (!strncmp(arg, "--stage=", 8)) {
-                       if (!strcmp(arg + 8, "all")) {
-                               to_tempfile = 1;
-                               checkout_stage = CHECKOUT_ALL;
-                       } else {
-                               int ch = arg[8];
-                               if ('1' <= ch && ch <= '3')
-                                       checkout_stage = arg[8] - '0';
-                               else
-                                       die("stage should be between 1 and 3 or all");
-                       }
-                       continue;
-               }
-               if (arg[0] == '-')
-                       usage(checkout_cache_usage);
-               break;
-       }
-
-       if (state.base_dir_len || to_tempfile) {
-               /* when --prefix is specified we do not
-                * want to update cache.
-                */
-               if (state.refresh_cache) {
-                       close(newfd); newfd = -1;
-                       rollback_lock_file(&lock_file);
-               }
-               state.refresh_cache = 0;
-       }
-
-       /* 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");
-               p = prefix_path(prefix, prefix_length, arg);
-               checkout_file(p);
-               if (p < arg || p > arg + strlen(arg))
-                       free((char*)p);
-       }
-
-       if (read_from_stdin) {
-               struct strbuf buf;
-               if (all)
-                       die("git-checkout-index: don't mix '--all' and '--stdin'");
-               strbuf_init(&buf);
-               while (1) {
-                       char *path_name;
-                       const char *p;
-
-                       read_line(&buf, stdin, line_termination);
-                       if (buf.eof)
-                               break;
-                       if (line_termination && buf.buf[0] == '"')
-                               path_name = unquote_c_style(buf.buf, NULL);
-                       else
-                               path_name = buf.buf;
-                       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);
-               }
-       }
-
-       if (all)
-               checkout_all();
-
-       if (0 <= newfd &&
-           (write_cache(newfd, active_cache, active_nr) ||
-            close(newfd) || commit_lock_file(&lock_file)))
-               die("Unable to write new index file");
-       return 0;
-}
index 04f508ab90527f5d71462b245b6b598d689c1853..369e6116e0aa63bc9e6ca88996b2dcfc12ebb967 100644 (file)
@@ -22,3 +22,19 @@ VPATH = @srcdir@
 export exec_prefix mandir
 export srcdir VPATH
 
+NO_PYTHON=@NO_PYTHON@
+NEEDS_SSL_WITH_CRYPTO=@NEEDS_SSL_WITH_CRYPTO@
+NO_OPENSSL=@NO_OPENSSL@
+NO_CURL=@NO_CURL@
+NO_EXPAT=@NO_EXPAT@
+NEEDS_LIBICONV=@NEEDS_LIBICONV@
+NEEDS_SOCKET=@NEEDS_SOCKET@
+NO_D_INO_IN_DIRENT=@NO_D_INO_IN_DIRENT@
+NO_D_TYPE_IN_DIRENT=@NO_D_TYPE_IN_DIRENT@
+NO_SOCKADDR_STORAGE=@NO_SOCKADDR_STORAGE@
+NO_IPV6=@NO_IPV6@
+NO_C99_FORMAT=@NO_C99_FORMAT@
+NO_STRCASESTR=@NO_STRCASESTR@
+NO_STRLCPY=@NO_STRLCPY@
+NO_SETENV=@NO_SETENV@
+
index c1f7751e6f8af39e812fb14963174ef8cb83d29f..e890131c46a2036d5d720a184b00571a46428b9d 100644 (file)
@@ -2,7 +2,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_PREREQ(2.59)
-AC_INIT([git], [1.4.1], [git@vger.kernel.org])
+AC_INIT([git], [@@GIT_VERSION@@], [git@vger.kernel.org])
 
 AC_CONFIG_SRCDIR([git.c])
 
@@ -19,6 +19,77 @@ echo "# ${config_append}.  Generated by configure." > "${config_append}"
 # Append LINE to file ${config_append}
 AC_DEFUN([GIT_CONF_APPEND_LINE],
 [echo "$1" >> "${config_append}"])# GIT_CONF_APPEND_LINE
+#
+# GIT_ARG_SET_PATH(PROGRAM)
+# -------------------------
+# Provide --with-PROGRAM=PATH option to set PATH to PROGRAM
+AC_DEFUN([GIT_ARG_SET_PATH],
+[AC_ARG_WITH([$1],
+ [AS_HELP_STRING([--with-$1=PATH],
+                 [provide PATH to $1])],
+ [GIT_CONF_APPEND_PATH($1)],[])
+])# GIT_ARG_SET_PATH
+#
+# GIT_CONF_APPEND_PATH(PROGRAM)
+# ------------------------------
+# Parse --with-PROGRAM=PATH option to set PROGRAM_PATH=PATH
+# Used by GIT_ARG_SET_PATH(PROGRAM)
+AC_DEFUN([GIT_CONF_APPEND_PATH],
+[PROGRAM=m4_toupper($1); \
+if test "$withval" = "no"; then \
+       AC_MSG_ERROR([You cannot use git without $1]); \
+else \
+       if test "$withval" = "yes"; then \
+               AC_MSG_WARN([You should provide path for --with-$1=PATH]); \
+       else \
+               GIT_CONF_APPEND_LINE(${PROGRAM}_PATH=$withval); \
+       fi; \
+fi; \
+]) # GIT_CONF_APPEND_PATH
+#
+# GIT_PARSE_WITH(PACKAGE)
+# -----------------------
+# For use in AC_ARG_WITH action-if-found, for packages default ON.
+# * Set NO_PACKAGE=YesPlease for --without-PACKAGE
+# * Set PACKAGEDIR=PATH for --with-PACKAGE=PATH
+# * Unset NO_PACKAGE for --with-PACKAGE without ARG
+AC_DEFUN([GIT_PARSE_WITH],
+[PACKAGE=m4_toupper($1); \
+if test "$withval" = "no"; then \
+       m4_toupper(NO_$1)=YesPlease; \
+elif test "$withval" = "yes"; then \
+       m4_toupper(NO_$1)=; \
+else \
+       m4_toupper(NO_$1)=; \
+       GIT_CONF_APPEND_LINE(${PACKAGE}DIR=$withval); \
+fi \
+])# GIT_PARSE_WITH
+
+
+## Site configuration related to programs (before tests)
+## --with-PACKAGE[=ARG] and --without-PACKAGE
+#
+# Define SHELL_PATH to provide path to shell.
+GIT_ARG_SET_PATH(shell)
+#
+# Define PERL_PATH to provide path to Perl.
+GIT_ARG_SET_PATH(perl)
+#
+# Define NO_PYTHON if you want to lose all benefits of the recursive merge.
+# Define PYTHON_PATH to provide path to Python.
+AC_ARG_WITH(python,[AS_HELP_STRING([--with-python=PATH], [provide PATH to python])
+AS_HELP_STRING([--without-python], [don't use python scripts])],
+ [if test "$withval" = "no"; then \
+    NO_PYTHON=YesPlease; \
+  elif test "$withval" = "yes"; then \
+    NO_PYTHON=; \
+  else \
+    NO_PYTHON=; \
+    PYTHON_PATH=$withval; \
+  fi; \
+ ])
+AC_SUBST(NO_PYTHON)
+AC_SUBST(PYTHON_PATH)
 
 
 ## Checks for programs.
@@ -30,6 +101,16 @@ AC_CHECK_TOOL(AR, ar, :)
 AC_CHECK_PROGS(TAR, [gtar tar])
 #
 # Define NO_PYTHON if you want to lose all benefits of the recursive merge.
+# Define PYTHON_PATH to provide path to Python.
+if test -z "$NO_PYTHON"; then
+       if test -z "$PYTHON_PATH"; then
+               AC_PATH_PROGS(PYTHON_PATH, [python python2.4 python2.3 python2])
+       fi
+       if test -n "$PYTHON_PATH"; then
+               GIT_CONF_APPEND_LINE([PYTHON_PATH=@PYTHON_PATH@])
+               NO_PYTHON=""
+       fi
+fi
 
 
 ## Checks for libraries.
@@ -37,32 +118,42 @@ AC_MSG_NOTICE([CHECKS for libraries])
 #
 # Define NO_OPENSSL environment variable if you do not have OpenSSL.
 # Define NEEDS_SSL_WITH_CRYPTO if you need -lcrypto with -lssl (Darwin).
-AC_CHECK_LIB([ssl], [SHA1_Init],[],
-[AC_CHECK_LIB([crypto], [SHA1_INIT],
- [GIT_CONF_APPEND_LINE(NEEDS_SSL_WITH_CRYPTO=YesPlease)],
- [GIT_CONF_APPEND_LINE(NO_OPENSSL=YesPlease)])])
+AC_CHECK_LIB([crypto], [SHA1_Init],
+[NEEDS_SSL_WITH_CRYPTO=],
+[AC_CHECK_LIB([ssl], [SHA1_Init],
+ [NEEDS_SSL_WITH_CRYPTO=YesPlease
+  NEEDS_SSL_WITH_CRYPTO=],
+ [NO_OPENSSL=YesPlease])])
+AC_SUBST(NEEDS_SSL_WITH_CRYPTO)
+AC_SUBST(NO_OPENSSL)
 #
 # Define NO_CURL if you do not have curl installed.  git-http-pull and
 # git-http-push are not built, and you cannot use http:// and https://
 # transports.
-AC_CHECK_LIB([curl], [curl_global_init],[],
-[GIT_CONF_APPEND_LINE(NO_CURL=YesPlease)])
+AC_CHECK_LIB([curl], [curl_global_init],
+[NO_CURL=],
+[NO_CURL=YesPlease])
+AC_SUBST(NO_CURL)
 #
 # Define NO_EXPAT if you do not have expat installed.  git-http-push is
 # not built, and you cannot push using http:// and https:// transports.
-AC_CHECK_LIB([expat], [XML_ParserCreate],[],
-[GIT_CONF_APPEND_LINE(NO_EXPAT=YesPlease)])
+AC_CHECK_LIB([expat], [XML_ParserCreate],
+[NO_EXPAT=],
+[NO_EXPAT=YesPlease])
+AC_SUBST(NO_EXPAT)
 #
 # Define NEEDS_LIBICONV if linking with libc is not enough (Darwin).
-AC_CHECK_LIB([c], [iconv],[],
-[AC_CHECK_LIB([iconv],[iconv],
- [GIT_CONF_APPEND_LINE(NEEDS_LIBICONV=YesPlease)],[])])
+AC_CHECK_LIB([c], [iconv],
+[NEEDS_LIBICONV=],
+[NEEDS_LIBICONV=YesPlease])
+AC_SUBST(NEEDS_LIBICONV)
 #
 # Define NEEDS_SOCKET if linking with libc is not enough (SunOS,
 # Patrick Mauritz).
-AC_CHECK_LIB([c], [socket],[],
-[AC_CHECK_LIB([socket],[socket],
- [GIT_CONF_APPEND_LINE(NEEDS_SOCKET=YesPlease)],[])])
+AC_CHECK_LIB([c], [socket],
+[NEEDS_SOCKET=],
+[NEEDS_SOCKET=YesPlease])
+AC_SUBST(NEEDS_SOCKET)
 
 
 ## Checks for header files.
@@ -72,21 +163,63 @@ AC_CHECK_LIB([c], [socket],[],
 AC_MSG_NOTICE([CHECKS for typedefs, structures, and compiler characteristics])
 #
 # Define NO_D_INO_IN_DIRENT if you don't have d_ino in your struct dirent.
-AC_CHECK_MEMBER(struct dirent.d_ino,[],
-[GIT_CONF_APPEND_LINE(NO_D_INO_IN_DIRENT=YesPlease)],
+AC_CHECK_MEMBER(struct dirent.d_ino,
+[NO_D_INO_IN_DIRENT=],
+[NO_D_INO_IN_DIRENT=YesPlease],
 [#include <dirent.h>])
+AC_SUBST(NO_D_INO_IN_DIRENT)
 #
 # Define NO_D_TYPE_IN_DIRENT if your platform defines DT_UNKNOWN but lacks
 # d_type in struct dirent (latest Cygwin -- will be fixed soonish).
-AC_CHECK_MEMBER(struct dirent.d_type,[],
-[GIT_CONF_APPEND_LINE(NO_D_TYPE_IN_DIRENT=YesPlease)],
+AC_CHECK_MEMBER(struct dirent.d_type,
+[NO_D_TYPE_IN_DIRENT=],
+[NO_D_TYPE_IN_DIRENT=YesPlease],
 [#include <dirent.h>])
+AC_SUBST(NO_D_TYPE_IN_DIRENT)
 #
 # Define NO_SOCKADDR_STORAGE if your platform does not have struct
 # sockaddr_storage.
-AC_CHECK_TYPE(struct sockaddr_storage,[],
-[GIT_CONF_APPEND_LINE(NO_SOCKADDR_STORAGE=YesPlease)],
+AC_CHECK_TYPE(struct sockaddr_storage,
+[NO_SOCKADDR_STORAGE=],
+[NO_SOCKADDR_STORAGE=YesPlease],
 [#include <netinet/in.h>])
+AC_SUBST(NO_SOCKADDR_STORAGE)
+#
+# Define NO_IPV6 if you lack IPv6 support and getaddrinfo().
+AC_CHECK_TYPE([struct addrinfo],[
+ AC_CHECK_FUNC([getaddrinfo],
+  [NO_IPV6=],
+  [NO_IPV6=YesPlease])
+],[NO_IPV6=YesPlease],[
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+])
+AC_SUBST(NO_IPV6)
+#
+# Define NO_C99_FORMAT if your formatted IO functions (printf/scanf et.al.)
+# do not support the 'size specifiers' introduced by C99, namely ll, hh,
+# j, z, t. (representing long long int, char, intmax_t, size_t, ptrdiff_t).
+# some C compilers supported these specifiers prior to C99 as an extension.
+AC_CACHE_CHECK(whether formatted IO functions support C99 size specifiers,
+ ac_cv_c_c99_format,
+[# Actually git uses only %z (%zu) in alloc.c, and %t (%td) in mktag.c
+AC_RUN_IFELSE(
+       [AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
+               [[char buf[64];
+               if (sprintf(buf, "%lld%hhd%jd%zd%td", (long long int)1, (char)2, (intmax_t)3, (size_t)4, (ptrdiff_t)5) != 5)
+                 exit(1);
+               else if (strcmp(buf, "12345"))
+                 exit(2);]])],
+       [ac_cv_c_c99_format=yes],
+       [ac_cv_c_c99_format=no])
+])
+if test $ac_cv_c_c99_format = no; then
+       NO_C99_FORMAT=YesPlease
+else
+       NO_C99_FORMAT=
+fi
+AC_SUBST(NO_C99_FORMAT)
 
 
 ## Checks for library functions.
@@ -94,21 +227,25 @@ AC_CHECK_TYPE(struct sockaddr_storage,[],
 AC_MSG_NOTICE([CHECKS for library functions])
 #
 # Define NO_STRCASESTR if you don't have strcasestr.
-AC_CHECK_FUNC(strcasestr,[],
-[GIT_CONF_APPEND_LINE(NO_STRCASESTR=YesPlease)])
+AC_CHECK_FUNC(strcasestr,
+[NO_STRCASESTR=],
+[NO_STRCASESTR=YesPlease])
+AC_SUBST(NO_STRCASESTR)
 #
 # Define NO_STRLCPY if you don't have strlcpy.
-AC_CHECK_FUNC(strlcpy,[],
-[GIT_CONF_APPEND_LINE(NO_STRLCPY=YesPlease)])
+AC_CHECK_FUNC(strlcpy,
+[NO_STRLCPY=],
+[NO_STRLCPY=YesPlease])
+AC_SUBST(NO_STRLCPY)
 #
 # Define NO_SETENV if you don't have setenv in the C library.
-AC_CHECK_FUNC(setenv,[],
-[GIT_CONF_APPEND_LINE(NO_SETENV=YesPlease)])
+AC_CHECK_FUNC(setenv,
+[NO_SETENV=],
+[NO_SETENV=YesPlease])
+AC_SUBST(NO_SETENV)
 #
 # Define NO_MMAP if you want to avoid mmap.
 #
-# Define NO_IPV6 if you lack IPv6 support and getaddrinfo().
-#
 # Define NO_ICONV if your libc does not properly support iconv.
 
 
@@ -125,9 +262,11 @@ AC_CHECK_FUNC(setenv,[],
 # a missing newline at the end of the file.
 
 
-## Site configuration
+## Site configuration (override autodetection)
 ## --with-PACKAGE[=ARG] and --without-PACKAGE
-# Define NO_SVN_TESTS if you want to skip time-consuming SVN interopability
+AC_MSG_NOTICE([CHECKS for site configuration])
+#
+# Define NO_SVN_TESTS if you want to skip time-consuming SVN interoperability
 # tests.  These tests take up a significant amount of the total test time
 # but are not needed unless you plan to talk to SVN repos.
 #
@@ -145,21 +284,51 @@ AC_CHECK_FUNC(setenv,[],
 # Define NO_OPENSSL environment variable if you do not have OpenSSL.
 # This also implies MOZILLA_SHA1.
 #
+# Define OPENSSLDIR=/foo/bar if your openssl header and library files are in
+# /foo/bar/include and /foo/bar/lib directories.
+AC_ARG_WITH(openssl,
+AS_HELP_STRING([--with-openssl],[use OpenSSL library (default is YES)])
+AS_HELP_STRING([],              [ARG can be prefix for openssl library and headers]),\
+GIT_PARSE_WITH(openssl))
+#
 # Define NO_CURL if you do not have curl installed.  git-http-pull and
 # git-http-push are not built, and you cannot use http:// and https://
 # transports.
 #
 # Define CURLDIR=/foo/bar if your curl header and library files are in
 # /foo/bar/include and /foo/bar/lib directories.
+AC_ARG_WITH(curl,
+AS_HELP_STRING([--with-curl],[support http(s):// transports (default is YES)])
+AS_HELP_STRING([],           [ARG can be also prefix for curl library and headers]),
+GIT_PARSE_WITH(curl))
 #
 # Define NO_EXPAT if you do not have expat installed.  git-http-push is
 # not built, and you cannot push using http:// and https:// transports.
 #
-# Define NO_MMAP if you want to avoid mmap.
+# Define EXPATDIR=/foo/bar if your expat header and library files are in
+# /foo/bar/include and /foo/bar/lib directories.
+AC_ARG_WITH(expat,
+AS_HELP_STRING([--with-expat],
+[support git-push using http:// and https:// transports via WebDAV (default is YES)])
+AS_HELP_STRING([],            [ARG can be also prefix for expat library and headers]),
+GIT_PARSE_WITH(expat))
+#
+# Define NO_FINK if you are building on Darwin/Mac OS X, have Fink
+# installed in /sw, but don't want GIT to link against any libraries
+# installed there.  If defined you may specify your own (or Fink's)
+# include directories and library directories by defining CFLAGS
+# and LDFLAGS appropriately.
 #
-# Define NO_PYTHON if you want to loose all benefits of the recursive merge.
+# Define NO_DARWIN_PORTS if you are building on Darwin/Mac OS X,
+# have DarwinPorts installed in /opt/local, but don't want GIT to
+# link against any libraries installed there.  If defined you may
+# specify your own (or DarwinPort's) include directories and
+# library directories by defining CFLAGS and LDFLAGS appropriately.
 #
+# Define NO_MMAP if you want to avoid mmap.
+
 ## --enable-FEATURE[=ARG] and --disable-FEATURE
+#
 # Define COLLISION_CHECK below if you believe that SHA1's
 # 1461501637330902918203684832716283019655932542976 hashes do not give you
 # sufficient guarantee that no collisions between objects will ever happen.
index ebea8e472bb2eede02219999b0cfd9a63f211d4b..168771ed857dd9e95b1e14da40f1e39d18f7b2cf 100644 (file)
@@ -1,5 +1,6 @@
 #define _XOPEN_SOURCE 500 /* glibc2 and AIX 5.3L need this */
 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
+#define _GNU_SOURCE
 #include <time.h>
 #include "cache.h"
 #include "blob.h"
index 04f01194356ea0c91a01eed682b068c15e313ee6..d0af786aec3f797943290cdc63fa77f393900160 100755 (executable)
--- a/git-am.sh
+++ b/git-am.sh
@@ -45,6 +45,12 @@ go_next () {
        this=$next
 }
 
+cannot_fallback () {
+       echo "$1"
+       echo "Cannot fall back to three-way merge."
+       exit 1
+}
+
 fall_back_3way () {
     O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
 
@@ -52,19 +58,23 @@ fall_back_3way () {
     mkdir "$dotest/patch-merge-tmp-dir"
 
     # First see if the patch records the index info that we can use.
-    if git-apply -z --index-info "$dotest/patch" \
-       >"$dotest/patch-merge-index-info" 2>/dev/null &&
-       GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
-       git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
-       GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
-       git-write-tree >"$dotest/patch-merge-base+" &&
-       # index has the base tree now.
-       GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
+    git-apply -z --index-info "$dotest/patch" \
+       >"$dotest/patch-merge-index-info" &&
+    GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
+    git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
+    GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
+    git-write-tree >"$dotest/patch-merge-base+" ||
+    cannot_fallback "Patch does not record usable index information."
+
+    echo Using index info to reconstruct a base tree...
+    if 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
+        cannot_fallback "Did you hand edit your patch?
+It does not apply to blobs recorded in its index."
     fi
 
     test -f "$dotest/patch-merge-index" &&
index dd9209365253971c70db772929fb4541f1c609b2..b2e18954c03ff502053cb74d142faab7d2a8dacb 100644 (file)
@@ -139,9 +139,10 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
        }
 }
 
-static inline int has_extension(const char *filename, int len, const char *ext)
+static inline int has_extension(const char *filename, const char *ext)
 {
-       int extlen = strlen(ext);
+       size_t len = strlen(filename);
+       size_t extlen = strlen(ext);
        return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
 }
 
index d15747f1ed8f875a3c1bb7129e50bfbc8d2b033f..42f9b1c125578a7158392b53ec8792d6c6bbb273 100755 (executable)
@@ -35,17 +35,12 @@ case "$1" in
        exit
 esac
 
+# Make sure we are in a valid repository of a vintage we understand.
 if [ -z "$SUBDIRECTORY_OK" ]
 then
        : ${GIT_DIR=.git}
-       : ${GIT_OBJECT_DIRECTORY="$GIT_DIR/objects"}
-
-       # Make sure we are in a valid repository of a vintage we understand.
-       GIT_DIR="$GIT_DIR" git repo-config --get core.nosuch >/dev/null
-       if test $? = 128
-       then
-           exit
-       fi
+       GIT_DIR=$(GIT_DIR="$GIT_DIR" git-rev-parse --git-dir) || exit
 else
        GIT_DIR=$(git-rev-parse --git-dir) || exit
 fi
+: ${GIT_OBJECT_DIRECTORY="$GIT_DIR/objects"}
index 6453771f9c2ce27dcecb7bf208cd60feb4881aa3..0d58bb9b37944728baa7d8af17d714f6ee8b4509 100755 (executable)
@@ -31,6 +31,7 @@
 use File::Path qw/mkpath/;
 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
 use File::Spec qw//;
+use File::Copy qw/copy/;
 use POSIX qw/strftime/;
 use IPC::Open3;
 use Memoize;
@@ -77,9 +78,6 @@
                'copy-similarity|C=i'=> \$_cp_similarity
 );
 
-# 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 } ],
@@ -1160,27 +1158,24 @@ sub repo_path_split {
                }
        }
 
-       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";
-               }
+               my $tmp = libsvn_connect($full_url);
+               my $url = $tmp->get_repos_root;
+               $full_url =~ s#^\Q$url\E/*##;
+               push @repo_path_split_cache, qr/^(\Q$url\E)/;
+               return ($url, $full_url);
        } else {
+               my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
+               $path =~ s#^/+##;
+               my @paths = split(m#/+#, $path);
                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);
        }
-       push @repo_path_split_cache, qr/^(\Q$url\E)/;
-       $path = join('/',@paths);
-       return ($url, $path);
 }
 
 sub setup_git_svn {
@@ -1760,43 +1755,6 @@ sub svn_info {
 
 sub sys { system(@_) == 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 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) = @_;
 
@@ -1824,9 +1782,11 @@ sub do_update_index {
                                                'text-base',"$f.svn-base");
                                $tb =~ s#^/##;
                        }
+                       my @s = stat($x);
                        unlink $x or croak $!;
-                       eol_cp($tb, $x);
+                       copy($tb, $x);
                        chmod(($mode &~ umask), $x) or croak $!;
+                       utime $s[8], $s[9], $x;
                }
                print $ui $x,"\0";
        }
@@ -2617,7 +2577,9 @@ sub libsvn_connect {
 sub libsvn_get_file {
        my ($gui, $f, $rev) = @_;
        my $p = $f;
-       return unless ($p =~ s#^\Q$SVN_PATH\E/##);
+       if (length $SVN_PATH > 0) {
+               return unless ($p =~ s#^\Q$SVN_PATH\E/##);
+       }
 
        my ($hash, $pid, $in, $out);
        my $pool = SVN::Pool->new;
@@ -2664,6 +2626,7 @@ sub libsvn_log_entry {
        if (defined $_authors && ! defined $users{$author}) {
                die "Author: $author not defined in $_authors file\n";
        }
+       $msg = '' if ($rev == 0 && !defined $msg);
        return { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S",
                author => $author, msg => $msg."\n", parents => $parents || [] }
 }
diff --git a/git.c b/git.c
index 18ba14ade1bb60b59fd0288130ba098e9e76dac3..5da7787d867365b5a018fd3806fc5d0bfd9b92a2 100644 (file)
--- a/git.c
+++ b/git.c
@@ -213,8 +213,8 @@ static int handle_alias(int *argcp, const char ***argv)
 
 const char git_version_string[] = GIT_VERSION;
 
-#define NEEDS_PREFIX 1
-#define USE_PAGER 2
+#define RUN_SETUP      (1<<0)
+#define USE_PAGER      (1<<1)
 
 static void handle_internal_command(int argc, const char **argv, char **envp)
 {
@@ -224,47 +224,53 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
                int (*fn)(int, const char **, const char *);
                int option;
        } commands[] = {
-               { "version", cmd_version },
-               { "help", cmd_help },
-               { "log", cmd_log, NEEDS_PREFIX | USE_PAGER },
-               { "whatchanged", cmd_whatchanged, NEEDS_PREFIX | USE_PAGER },
-               { "show", cmd_show, NEEDS_PREFIX | USE_PAGER },
-               { "push", cmd_push, NEEDS_PREFIX },
-               { "format-patch", cmd_format_patch, NEEDS_PREFIX },
+               { "add", cmd_add, RUN_SETUP },
+               { "apply", cmd_apply },
+               { "cat-file", cmd_cat_file, RUN_SETUP },
+               { "checkout-index", cmd_checkout_index, RUN_SETUP },
+               { "check-ref-format", cmd_check_ref_format },
+               { "commit-tree", cmd_commit_tree, RUN_SETUP },
                { "count-objects", cmd_count_objects },
-               { "diff", cmd_diff, NEEDS_PREFIX },
-               { "grep", cmd_grep, NEEDS_PREFIX },
-               { "rm", cmd_rm, NEEDS_PREFIX },
-               { "add", cmd_add, NEEDS_PREFIX },
-               { "rev-list", cmd_rev_list, NEEDS_PREFIX },
-               { "init-db", cmd_init_db },
+               { "diff", cmd_diff, RUN_SETUP },
+               { "diff-files", cmd_diff_files, RUN_SETUP },
+               { "diff-index", cmd_diff_index, RUN_SETUP },
+               { "diff-stages", cmd_diff_stages, RUN_SETUP },
+               { "diff-tree", cmd_diff_tree, RUN_SETUP },
+               { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
+               { "format-patch", cmd_format_patch, RUN_SETUP },
                { "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, NEEDS_PREFIX },
-               { "ls-tree", cmd_ls_tree, NEEDS_PREFIX },
-               { "tar-tree", cmd_tar_tree, NEEDS_PREFIX },
-               { "read-tree", cmd_read_tree, NEEDS_PREFIX },
-               { "commit-tree", cmd_commit_tree, NEEDS_PREFIX },
-               { "apply", cmd_apply },
-               { "show-branch", cmd_show_branch, NEEDS_PREFIX },
-               { "diff-files", cmd_diff_files, NEEDS_PREFIX },
-               { "diff-index", cmd_diff_index, NEEDS_PREFIX },
-               { "diff-stages", cmd_diff_stages, NEEDS_PREFIX },
-               { "diff-tree", cmd_diff_tree, NEEDS_PREFIX },
-               { "cat-file", cmd_cat_file, NEEDS_PREFIX },
-               { "rev-parse", cmd_rev_parse, NEEDS_PREFIX },
-               { "write-tree", cmd_write_tree, NEEDS_PREFIX },
-               { "mailsplit", cmd_mailsplit },
+               { "grep", cmd_grep, RUN_SETUP },
+               { "help", cmd_help },
+               { "init-db", cmd_init_db },
+               { "log", cmd_log, RUN_SETUP | USE_PAGER },
+               { "ls-files", cmd_ls_files, RUN_SETUP },
+               { "ls-tree", cmd_ls_tree, RUN_SETUP },
                { "mailinfo", cmd_mailinfo },
-               { "stripspace", cmd_stripspace },
-               { "update-index", cmd_update_index, NEEDS_PREFIX },
-               { "update-ref", cmd_update_ref, NEEDS_PREFIX },
-               { "fmt-merge-msg", cmd_fmt_merge_msg, NEEDS_PREFIX },
-               { "prune", cmd_prune, NEEDS_PREFIX },
-               { "mv", cmd_mv, NEEDS_PREFIX },
-               { "prune-packed", cmd_prune_packed, NEEDS_PREFIX },
+               { "mailsplit", cmd_mailsplit },
+               { "mv", cmd_mv, RUN_SETUP },
+               { "name-rev", cmd_name_rev, RUN_SETUP },
+               { "pack-objects", cmd_pack_objects, RUN_SETUP },
+               { "prune", cmd_prune, RUN_SETUP },
+               { "prune-packed", cmd_prune_packed, RUN_SETUP },
+               { "push", cmd_push, RUN_SETUP },
+               { "read-tree", cmd_read_tree, RUN_SETUP },
                { "repo-config", cmd_repo_config },
+               { "rev-list", cmd_rev_list, RUN_SETUP },
+               { "rev-parse", cmd_rev_parse, RUN_SETUP },
+               { "rm", cmd_rm, RUN_SETUP },
+               { "show-branch", cmd_show_branch, RUN_SETUP },
+               { "show", cmd_show, RUN_SETUP | USE_PAGER },
+               { "stripspace", cmd_stripspace },
+               { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
+               { "tar-tree", cmd_tar_tree, RUN_SETUP },
+               { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
+               { "update-index", cmd_update_index, RUN_SETUP },
+               { "update-ref", cmd_update_ref, RUN_SETUP },
+               { "upload-tar", cmd_upload_tar },
+               { "version", cmd_version },
+               { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER },
+               { "write-tree", cmd_write_tree, RUN_SETUP },
+               { "verify-pack", cmd_verify_pack },
        };
        int i;
 
@@ -281,7 +287,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
                        continue;
 
                prefix = NULL;
-               if (p->option & NEEDS_PREFIX)
+               if (p->option & RUN_SETUP)
                        prefix = setup_git_directory();
                if (p->option & USE_PAGER)
                        setup_pager();
diff --git a/gitk b/gitk
index 5acaadf495f16d2515ca406fe68b212228b7024f..a92ab007b4e22d51bc9b1c6ac2a739a7bbdfcfcc 100755 (executable)
--- a/gitk
+++ b/gitk
@@ -312,7 +312,7 @@ proc getcommit {id} {
 
 proc readrefs {} {
     global tagids idtags headids idheads tagcontents
-    global otherrefids idotherrefs
+    global otherrefids idotherrefs mainhead
 
     foreach v {tagids idtags headids idheads otherrefids idotherrefs} {
        catch {unset $v}
@@ -358,6 +358,13 @@ proc readrefs {} {
        }
     }
     close $refd
+    set mainhead {}
+    catch {
+       set thehead [exec git symbolic-ref HEAD]
+       if {[string match "refs/heads/*" $thehead]} {
+           set mainhead [string range $thehead 11 end]
+       }
+    }
 }
 
 proc show_error {w top msg} {
@@ -386,6 +393,7 @@ proc makewindow {} {
     global rowctxmenu mergemax wrapcomment
     global highlight_files gdttype
     global searchstring sstring
+    global bgcolor fgcolor bglist fglist diffcolors
 
     menu .bar
     .bar add cascade -label "File" -menu .bar.file
@@ -446,18 +454,19 @@ proc makewindow {} {
     .ctop add .ctop.top
     set canv .ctop.top.clist.canv
     canvas $canv -height $geometry(canvh) -width $geometry(canv1) \
-       -bg white -bd 0 \
+       -background $bgcolor -bd 0 \
        -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
     .ctop.top.clist add $canv
     set canv2 .ctop.top.clist.canv2
     canvas $canv2 -height $geometry(canvh) -width $geometry(canv2) \
-       -bg white -bd 0 -yscrollincr $linespc
+       -background $bgcolor -bd 0 -yscrollincr $linespc
     .ctop.top.clist add $canv2
     set canv3 .ctop.top.clist.canv3
     canvas $canv3 -height $geometry(canvh) -width $geometry(canv3) \
-       -bg white -bd 0 -yscrollincr $linespc
+       -background $bgcolor -bd 0 -yscrollincr $linespc
     .ctop.top.clist add $canv3
     bind .ctop.top.clist <Configure> {resizeclistpanes %W %w}
+    lappend bglist $canv $canv2 $canv3
 
     set sha1entry .ctop.top.bar.sha1
     set entries $sha1entry
@@ -563,19 +572,22 @@ proc makewindow {} {
     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 \
+    text $ctext -background $bgcolor -foreground $fgcolor \
+       -state disabled -font $textfont \
        -width $geometry(ctextw) -height $geometry(ctexth) \
        -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
+    lappend bglist $ctext
+    lappend fglist $ctext
 
     $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
-    $ctext tag conf d1 -fore "#00a000"
+    $ctext tag conf hunksep -fore [lindex $diffcolors 2]
+    $ctext tag conf d0 -fore [lindex $diffcolors 0]
+    $ctext tag conf d1 -fore [lindex $diffcolors 1]
     $ctext tag conf m0 -fore red
     $ctext tag conf m1 -fore blue
     $ctext tag conf m2 -fore green
@@ -608,11 +620,15 @@ proc makewindow {} {
     pack .ctop.cdet.right.mode -side top -fill x
     set cflist .ctop.cdet.right.cfiles
     set indent [font measure $mainfont "nn"]
-    text $cflist -width $geometry(cflistw) -background white -font $mainfont \
+    text $cflist -width $geometry(cflistw) \
+       -background $bgcolor -foreground $fgcolor \
+       -font $mainfont \
        -tabs [list $indent [expr {2 * $indent}]] \
        -yscrollcommand ".ctop.cdet.right.sb set" \
        -cursor [. cget -cursor] \
        -spacing1 1 -spacing3 1
+    lappend bglist $cflist
+    lappend fglist $cflist
     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
@@ -747,6 +763,7 @@ proc savestuff {w} {
     global maxwidth showneartags
     global viewname viewfiles viewargs viewperm nextviewnum
     global cmitmode wrapcomment
+    global colors bgcolor fgcolor diffcolors
 
     if {$stuffsaved} return
     if {![winfo viewable .]} return
@@ -761,6 +778,10 @@ proc savestuff {w} {
        puts $f [list set cmitmode $cmitmode]
        puts $f [list set wrapcomment $wrapcomment]
        puts $f [list set showneartags $showneartags]
+       puts $f [list set bgcolor $bgcolor]
+       puts $f [list set fgcolor $fgcolor]
+       puts $f [list set colors $colors]
+       puts $f [list set diffcolors $diffcolors]
        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}]"
@@ -2870,11 +2891,11 @@ proc drawlines {id} {
 }
 
 proc drawcmittext {id row col rmx} {
-    global linespc canv canv2 canv3 canvy0
+    global linespc canv canv2 canv3 canvy0 fgcolor
     global commitlisted commitinfo rowidlist
     global rowtextx idpos idtags idheads idotherrefs
     global linehtag linentag linedtag
-    global mainfont canvxmax boldrows boldnamerows
+    global mainfont canvxmax boldrows boldnamerows fgcolor
 
     set ofill [expr {[lindex $commitlisted $row]? "blue": "white"}]
     set x [xc $row $col]
@@ -2882,7 +2903,7 @@ proc drawcmittext {id row col rmx} {
     set orad [expr {$linespc / 3}]
     set t [$canv create oval [expr {$x - $orad}] [expr {$y - $orad}] \
               [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
-              -fill $ofill -outline black -width 1]
+              -fill $ofill -outline $fgcolor -width 1 -tags circle]
     $canv raise $t
     $canv bind $t <1> {selcanvline {} %x %y}
     set xt [xc $row [llength [lindex $rowidlist $row]]]
@@ -2910,13 +2931,13 @@ proc drawcmittext {id row col rmx} {
            lappend nfont bold
        }
     }
-    set linehtag($row) [$canv create text $xt $y -anchor w \
-                           -text $headline -font $font]
+    set linehtag($row) [$canv create text $xt $y -anchor w -fill $fgcolor \
+                           -text $headline -font $font -tags text]
     $canv bind $linehtag($row) <Button-3> "rowmenu %X %Y $id"
-    set linentag($row) [$canv2 create text 3 $y -anchor w \
-                           -text $name -font $nfont]
-    set linedtag($row) [$canv3 create text 3 $y -anchor w \
-                           -text $date -font $mainfont]
+    set linentag($row) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
+                           -text $name -font $nfont -tags text]
+    set linedtag($row) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
+                           -text $date -font $mainfont -tags text]
     set xr [expr {$xt + [font measure $mainfont $headline]}]
     if {$xr > $canvxmax} {
        set canvxmax $xr
@@ -3136,9 +3157,9 @@ proc bindline {t id} {
 }
 
 proc drawtags {id x xt y1} {
-    global idtags idheads idotherrefs
+    global idtags idheads idotherrefs mainhead
     global linespc lthickness
-    global canv mainfont commitrow rowtextx curview
+    global canv mainfont commitrow rowtextx curview fgcolor bgcolor
 
     set marks {}
     set ntags 0
@@ -3163,8 +3184,14 @@ proc drawtags {id x xt y1} {
     set yb [expr {$yt + $linespc - 1}]
     set xvals {}
     set wvals {}
+    set i -1
     foreach tag $marks {
-       set wid [font measure $mainfont $tag]
+       incr i
+       if {$i >= $ntags && $i < $ntags + $nheads && $tag eq $mainhead} {
+           set wid [font measure [concat $mainfont bold] $tag]
+       } else {
+           set wid [font measure $mainfont $tag]
+       }
        lappend xvals $xt
        lappend wvals $wid
        set xt [expr {$xt + $delta + $wid + $lthickness + $linespc}]
@@ -3175,6 +3202,7 @@ proc drawtags {id x xt y1} {
     foreach tag $marks x $xvals wid $wvals {
        set xl [expr {$x + $delta}]
        set xr [expr {$x + $delta + $wid + $lthickness}]
+       set font $mainfont
        if {[incr ntags -1] >= 0} {
            # draw a tag
            set t [$canv create polygon $x [expr {$yt + $delta}] $xl $yt \
@@ -3186,6 +3214,9 @@ proc drawtags {id x xt y1} {
            # draw a head or other ref
            if {[incr nheads -1] >= 0} {
                set col green
+               if {$tag eq $mainhead} {
+                   lappend font bold
+               }
            } else {
                set col "#ddddff"
            }
@@ -3201,8 +3232,8 @@ proc drawtags {id x xt y1} {
                        -width 0 -fill "#ffddaa" -tags tag.$id
            }
        }
-       set t [$canv create text $xl $y1 -anchor w -text $tag \
-                  -font $mainfont -tags tag.$id]
+       set t [$canv create text $xl $y1 -anchor w -text $tag -fill $fgcolor \
+                  -font $font -tags [list tag.$id text]]
        if {$ntags >= 0} {
            $canv bind $t <1> [list showtag $tag 1]
        }
@@ -3223,10 +3254,11 @@ proc xcoord {i level ln} {
 }
 
 proc show_status {msg} {
-    global canv mainfont
+    global canv mainfont fgcolor
 
     clear_display
-    $canv create text 3 3 -anchor nw -text $msg -font $mainfont -tags textitems
+    $canv create text 3 3 -anchor nw -text $msg -font $mainfont \
+       -tags text -fill $fgcolor
 }
 
 proc finishcommits {} {
@@ -4574,7 +4606,8 @@ proc linehover {} {
     set t [$canv create rectangle $x0 $y0 $x1 $y1 \
               -fill \#ffff80 -outline black -width 1 -tags hover]
     $canv raise $t
-    set t [$canv create text $x $y -anchor nw -text $text -tags hover -font $mainfont]
+    set t [$canv create text $x $y -anchor nw -text $text -tags hover \
+              -font $mainfont]
     $canv raise $t
 }
 
@@ -5242,6 +5275,7 @@ proc doquit {} {
 proc doprefs {} {
     global maxwidth maxgraphpct diffopts
     global oldprefs prefstop showneartags
+    global bgcolor fgcolor ctext diffcolors
 
     set top .gitkprefs
     set prefstop $top
@@ -5265,6 +5299,7 @@ proc doprefs {} {
        -font optionfont
     spinbox $top.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
     grid x $top.maxpctl $top.maxpct -sticky w
+
     label $top.ddisp -text "Diff display options"
     grid $top.ddisp - -sticky w -pady 10
     label $top.diffoptl -text "Options for diff program" \
@@ -5276,6 +5311,34 @@ proc doprefs {} {
     checkbutton $top.ntag.b -variable showneartags
     pack $top.ntag.b $top.ntag.l -side left
     grid x $top.ntag -sticky w
+
+    label $top.cdisp -text "Colors: press to choose"
+    grid $top.cdisp - -sticky w -pady 10
+    label $top.bg -padx 40 -relief sunk -background $bgcolor
+    button $top.bgbut -text "Background" -font optionfont \
+       -command [list choosecolor bgcolor 0 $top.bg background setbg]
+    grid x $top.bgbut $top.bg -sticky w
+    label $top.fg -padx 40 -relief sunk -background $fgcolor
+    button $top.fgbut -text "Foreground" -font optionfont \
+       -command [list choosecolor fgcolor 0 $top.fg foreground setfg]
+    grid x $top.fgbut $top.fg -sticky w
+    label $top.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
+    button $top.diffoldbut -text "Diff: old lines" -font optionfont \
+       -command [list choosecolor diffcolors 0 $top.diffold "diff old lines" \
+                     [list $ctext tag conf d0 -foreground]]
+    grid x $top.diffoldbut $top.diffold -sticky w
+    label $top.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
+    button $top.diffnewbut -text "Diff: new lines" -font optionfont \
+       -command [list choosecolor diffcolors 1 $top.diffnew "diff new lines" \
+                     [list $ctext tag conf d1 -foreground]]
+    grid x $top.diffnewbut $top.diffnew -sticky w
+    label $top.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
+    button $top.hunksepbut -text "Diff: hunk header" -font optionfont \
+       -command [list choosecolor diffcolors 2 $top.hunksep \
+                     "diff hunk header" \
+                     [list $ctext tag conf hunksep -foreground]]
+    grid x $top.hunksepbut $top.hunksep -sticky w
+
     frame $top.buts
     button $top.buts.ok -text "OK" -command prefsok
     button $top.buts.can -text "Cancel" -command prefscan
@@ -5285,6 +5348,35 @@ proc doprefs {} {
     grid $top.buts - - -pady 10 -sticky ew
 }
 
+proc choosecolor {v vi w x cmd} {
+    global $v
+
+    set c [tk_chooseColor -initialcolor [lindex [set $v] $vi] \
+              -title "Gitk: choose color for $x"]
+    if {$c eq {}} return
+    $w conf -background $c
+    lset $v $vi $c
+    eval $cmd $c
+}
+
+proc setbg {c} {
+    global bglist
+
+    foreach w $bglist {
+       $w conf -background $c
+    }
+}
+
+proc setfg {c} {
+    global fglist canv
+
+    foreach w $fglist {
+       $w conf -foreground $c
+    }
+    allcanvs itemconf text -fill $c
+    $canv itemconf circle -outline $c
+}
+
 proc prefscan {} {
     global maxwidth maxgraphpct diffopts
     global oldprefs prefstop showneartags
@@ -5620,6 +5712,9 @@ set wrapcomment "none"
 set showneartags 1
 
 set colors {green red blue magenta darkgrey brown orange}
+set bgcolor white
+set fgcolor black
+set diffcolors {red "#00a000" blue}
 
 catch {source ~/.gitk}
 
index 8d672762eab427f3b732626d6b3f8e05a7e8f8c9..27c6dac1436ca9def413430336f3f29d88247fcb 100644 (file)
@@ -5,5 +5,33 @@ The one working on:
 
 From the git version 1.4.0 gitweb is bundled with git.
 
-Any comment/question/concern to:
+
+How to configure gitweb for your local system:
+
+You can specify the following configuration variables when building GIT:
+ * GITWEB_SITENAME
+   Shown in the title of all generated pages, defaults to the servers name.
+ * GITWEB_PROJECTROOT
+   The root directory for all projects shown by gitweb.
+ * GITWEB_LIST
+   points to a directory to scan for projects (defaults to project root)
+   or to a file for explicit listing of projects.
+ * GITWEB_HOMETEXT
+   points to an .html file which is included on the gitweb project
+   overview page.
+ * GITWEB_CSS
+   Points to the location where you put gitweb.css on your web server.
+ * GITWEB_LOGO
+   Points to the location where you put git-logo.png on your web server.
+ * GITWEB_CONFIG
+   This file will be loaded using 'require'.  If the environment
+   $GITWEB_CONFIG is set when gitweb.cgi is executed the file in the
+   environment variable will be loaded instead of the file
+   specified when gitweb.cgi was created.
+
+Originally written by:
   Kay Sievers <kay.sievers@vrfy.org>
+
+Any comment/question/concern to:
+  Git mailing list <git@vger.kernel.org>
+
diff --git a/gitweb/git-logo.png b/gitweb/git-logo.png
new file mode 100644 (file)
index 0000000..16ae8d5
Binary files /dev/null and b/gitweb/git-logo.png differ
diff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi
deleted file mode 100755 (executable)
index e5fca63..0000000
+++ /dev/null
@@ -1,2696 +0,0 @@
-#!/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';
-
-our $cgi = new CGI;
-our $version = "267";
-our $my_url = $cgi->url();
-our $my_uri = $cgi->url(-absolute => 1);
-our $rss_link = "";
-
-# core git executable to use
-# this can just be "git" if your webserver has a sensible PATH
-our $GIT = "/usr/bin/git";
-
-# absolute fs-path which will be prepended to the project path
-#our $projectroot = "/pub/scm";
-our $projectroot = "/home/kay/public_html/pub/scm";
-
-# version of the core git binary
-our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
-
-# location for temporary files needed for diffs
-our $git_temp = "/tmp/gitweb";
-if (! -d $git_temp) {
-    mkdir($git_temp, 0700) || die_error("Couldn't mkdir $git_temp");
-}
-
-# target of the home link on top of all pages
-our $home_link = $my_uri;
-
-# name of your site or organization to appear in page titles
-# replace this with something more descriptive for clearer bookmarks
-our $site_name = $ENV{'SERVER_NAME'} || "Untitled";
-
-# html text to include at home page
-our $home_text = "indextext.html";
-
-# URI of default stylesheet
-our $stylesheet = "gitweb.css";
-
-# source of projects list
-#our $projects_list = $projectroot;
-our $projects_list = "index/index.aux";
-
-# default blob_plain mimetype and default charset for text/plain blob
-our $default_blob_plain_mimetype = 'text/plain';
-our $default_text_plain_charset  = undef;
-
-# file to use for guessing MIME types before trying /etc/mime.types
-# (relative to the current git repository)
-our $mimetypes_file = undef;
-
-# input validation and dispatch
-our $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;
-       }
-}
-
-our $order = $cgi->param('o');
-if (defined $order) {
-       if ($order =~ m/[^0-9a-zA-Z_]/) {
-               undef $order;
-               die_error(undef, "Invalid order parameter.");
-       }
-}
-
-our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
-if (defined $project) {
-       $project =~ s|^/||; $project =~ s|/$||;
-       $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;
-}
-
-our $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.");
-       }
-}
-
-our $hash = $cgi->param('h');
-if (defined $hash) {
-       $hash = validate_input($hash);
-       if (!defined($hash)) {
-               die_error(undef, "Invalid hash parameter.");
-       }
-}
-
-our $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.");
-       }
-}
-
-our $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.");
-       }
-}
-
-our $page = $cgi->param('pg');
-if (defined $page) {
-       if ($page =~ m/[^0-9]$/) {
-               undef $page;
-               die_error(undef, "Invalid page parameter.");
-       }
-}
-
-our $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_blame2();
-       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;
-}
-
-# CSS class for given age value (in seconds)
-sub age_class {
-       my $age = shift;
-
-       if ($age < 60*60*2) {
-               return "age0";
-       } elsif ($age < 60*60*24*2) {
-               return "age1";
-       } else {
-               return "age2";
-       }
-}
-
-sub git_header_html {
-       my $status = shift || "200 OK";
-       my $expires = shift;
-
-       my $title = "$site_name git";
-       if (defined $project) {
-               $title .= " - $project";
-               if (defined $action) {
-                       $title .= "/$action";
-                       if (defined $file_name) {
-                               $title .= " - $file_name";
-                               if ($action eq "tree" && $file_name !~ m|/$|) {
-                                       $title .= "/";
-                               }
-                       }
-               }
-       }
-       my $content_type;
-       # require explicit support from the UA if we are to send the page as
-       # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
-       # we have to do this because MSIE sometimes globs '*/*', pretending to
-       # support xhtml+xml but choking when it gets what it asked for.
-       if ($cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {
-               $content_type = 'application/xhtml+xml';
-       } else {
-               $content_type = 'text/html';
-       }
-       print $cgi->header(-type=>$content_type,  -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 -->
-<!-- git core binaries version $git_version -->
-<head>
-<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
-<meta name="robots" content="index, nofollow"/>
-<title>$title</title>
-<link rel="stylesheet" type="text/css" href="$stylesheet"/>
-$rss_link
-</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_base) {
-                       $search_hash = $hash_base;
-               } elsif (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, "-|", "$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, "-|", $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, "-|", "$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, "-|", "$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, "-|", "$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, "-|", "$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 $diff_class = "";
-                       if ($char eq '+') {
-                               $diff_class = " add";
-                       } elsif ($char eq "-") {
-                               $diff_class = " rem";
-                       } elsif ($char eq "@") {
-                               $diff_class = " chunk_header";
-                       } 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=\"diff$diff_class\">" . 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 class=\"project_list\">\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";
-               print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" . $pr->{'commit'}{'age_string'} . "</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, "-|", "$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'}")}, esc_html($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_print_page_path {
-       my $name = shift;
-       my $type = shift;
-
-       if (!defined $name) {
-               print "<div class=\"page_path\"><b>/</b></div>\n";
-       } elsif ($type =~ "blob") {
-               print "<div class=\"page_path\"><b>" .
-                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
-       } else {
-               print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
-       }
-}
-
-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_blame2 {
-       my $fd;
-       my $ftype;
-       die_error(undef, "Permission denied.") if (!git_get_project_config_bool ('blame'));
-       die_error('404 Not Found', "File name not defined") 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 looking up file");
-       }
-       $ftype = git_get_type($hash);
-       if ($ftype !~ "blob") {
-               die_error("400 Bad Request", "object is not a blob");
-       }
-       open ($fd, "-|", $GIT, "blame", '-l', $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";
-       git_print_page_path($file_name, $ftype);
-       my @rev_color = (qw(light dark));
-       my $num_colors = scalar(@rev_color);
-       my $current_color = 0;
-       my $last_rev;
-       print "<div class=\"page_body\">\n";
-       print "<table class=\"blame\">\n";
-       print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
-       while (<$fd>) {
-               /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
-               my $full_rev = $1;
-               my $rev = substr($full_rev, 0, 8);
-               my $lineno = $2;
-               my $data = $3;
-
-               if (!defined $last_rev) {
-                       $last_rev = $full_rev;
-               } elsif ($last_rev ne $full_rev) {
-                       $last_rev = $full_rev;
-                       $current_color = ++$current_color % $num_colors;
-               }
-               print "<tr class=\"$rev_color[$current_color]\">\n";
-               print "<td class=\"sha1\">" .
-                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$full_rev;f=$file_name")}, esc_html($rev)) . "</td>\n";
-               print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" . esc_html($lineno) . "</a></td>\n";
-               print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
-               print "</tr>\n";
-       }
-       print "</table>\n";
-       print "</div>";
-       close $fd or print "Reading blob failed\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, "-|", $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";
-       git_print_page_path($file_name);
-       print "<div class=\"page_body\">\n";
-       print <<HTML;
-<table class="blame">
-  <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_class;
-
-               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" class="error">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_class  = age_class($age);
-               $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);
-
-               print <<HTML;
-  <tr class="$line_class[$line_class_num]">
-    <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
-    <td class="$age_class">$age_str</td>
-    <td>$author</td>
-    <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
-    <td class="pre">$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, "-|", "$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 mimetype_guess_file {
-       my $filename = shift;
-       my $mimemap = shift;
-       -r $mimemap or return undef;
-
-       my %mimemap;
-       open(MIME, $mimemap) or return undef;
-       while (<MIME>) {
-               my ($mime, $exts) = split(/\t+/);
-               my @exts = split(/\s+/, $exts);
-               foreach my $ext (@exts) {
-                       $mimemap{$ext} = $mime;
-               }
-       }
-       close(MIME);
-
-       $filename =~ /\.(.*?)$/;
-       return $mimemap{$1};
-}
-
-sub mimetype_guess {
-       my $filename = shift;
-       my $mime;
-       $filename =~ /\./ or return undef;
-
-       if ($mimetypes_file) {
-               my $file = $mimetypes_file;
-               #$file =~ m#^/# or $file = "$projectroot/$path/$file";
-               $mime = mimetype_guess_file($filename, $file);
-       }
-       $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
-       return $mime;
-}
-
-sub git_blob_plain_mimetype {
-       my $fd = shift;
-       my $filename = shift;
-
-       if ($filename) {
-               my $mime = mimetype_guess($filename);
-               $mime and return $mime;
-       }
-
-       # just in case
-       return $default_blob_plain_mimetype unless $fd;
-
-       if (-T $fd) {
-               return 'text/plain' .
-                      ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
-       } elsif (! $filename) {
-               return 'application/octet-stream';
-       } elsif ($filename =~ m/\.png$/i) {
-               return 'image/png';
-       } elsif ($filename =~ m/\.gif$/i) {
-               return 'image/gif';
-       } elsif ($filename =~ m/\.jpe?g$/i) {
-               return 'image/jpeg';
-       } else {
-               return 'application/octet-stream';
-       }
-}
-
-sub git_blob_plain {
-       if (!defined $hash) {
-                if (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.");
-                } else {
-                        die_error(undef, "No file name defined.");
-                }
-        }
-       my $type = shift;
-       open my $fd, "-|", "$GIT cat-file blob $hash" or die_error("Couldn't cat $file_name, $hash");
-
-       $type ||= git_blob_plain_mimetype($fd, $file_name);
-
-       # save as filename, even when no $file_name is given
-       my $save_as = "$hash";
-       if (defined $file_name) {
-               $save_as = $file_name;
-       } elsif ($type =~ m/^text\//) {
-               $save_as .= '.txt';
-       }
-
-       print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
-       undef $/;
-       binmode STDOUT, ':raw';
-       print <$fd>;
-       binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
-       $/ = "\n";
-       close $fd;
-}
-
-sub git_blob {
-       if (!defined $hash) {
-                if (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.");
-                } else {
-                        die_error(undef, "No file name defined.");
-                }
-        }
-       my $have_blame = git_get_project_config_bool ('blame');
-       open my $fd, "-|", "$GIT cat-file blob $hash" or die_error(undef, "Open failed.");
-       my $mimetype = git_blob_plain_mimetype($fd, $file_name);
-       if ($mimetype !~ m/^text\//) {
-               close $fd;
-               return git_blob_plain($mimetype);
-       }
-       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";
-       }
-       git_print_page_path($file_name, "blob");
-       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_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, "-|", "$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/");
-       }
-       git_print_page_path($file_name);
-       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 class=\"mode\">" . 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=$t_hash;hb=$hash_base;f=$base$t_name")}, "history") .
-                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
-                             "</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") .
-                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") .
-                             "</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, "-|", "$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, "-|", "$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>$site_name 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, "-|", "$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, "-|", "$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/>\n";
-       if (defined $file_name && defined $co{'parent'}) {
-               my $parent = $co{'parent'};
-               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;hb=$parent;f=$file_name")}, "blame") . "\n";
-       }
-       print "<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 class=\"atnight\">%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 class=\"sha1\">$co{'id'}</td></tr>\n";
-       print "<tr>" .
-             "<td>tree</td>" .
-             "<td class=\"sha1\">" .
-             $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 class=\"sha1\">" . $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 class=\"signoff\">" . 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 class=\"diff_tree\">\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 class=\"file_status new\">[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 class=\"file_status deleted\">[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;hb=$hash;f=$file")}, "history") .
-                             "</td>\n"
-               } elsif ($status eq "M" || $status eq "T") {
-                       my $mode_chnge = "";
-                       if ($from_mode != $to_mode) {
-                               $mode_chnge = " <span class=\"file_status mode_chnge\">[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;hb=$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 class=\"file_status moved\">[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";
-       }
-       git_print_page_path($file_name, "blob");
-       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, "-|", "$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, "-|", "$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, "-|", "$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_base) {
-               $hash_base = git_read_head($project);
-       }
-       my $ftype;
-       my %co = git_read_commit($hash_base);
-       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_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/><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'})) . "\n" .
-             "</div>\n";
-       if (!defined $hash && defined $file_name) {
-               $hash = git_get_hash_by_path($hash_base, $file_name);
-       }
-       if (defined $hash) {
-               $ftype = git_get_type($hash);
-       }
-       git_print_page_path($file_name, $ftype);
-
-       open my $fd, "-|",
-               "$GIT rev-list --full-history $hash_base -- \'$file_name\'";
-       print "<table cellspacing=\"0\">\n";
-       my $alternate = 0;
-       while (my $line = <$fd>) {
-               if ($line =~ m/^([0-9a-fA-F]{40})/){
-                       my $commit = $1;
-                       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_base, $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";
-               }
-       }
-       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, "-|", "$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 class=\"match\">$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, "-|", "$GIT rev-list $hash | $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 class=\"match\">" . 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, "-|", "$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();
-}
index fffdb13d09a40397745861bb6ec5dcc79c798b0d..47c1ade87edbe4727c088435385dd66ecfe13fec 100644 (file)
@@ -171,6 +171,10 @@ tr.dark {
        background-color: #f6f6f0;
 }
 
+tr.dark2 {
+       background-color: #f6f6f0;
+}
+
 tr.dark:hover {
        background-color: #edece6;
 }
@@ -181,12 +185,16 @@ td {
        vertical-align: top;
 }
 
-td.link {
+td.link, td.selflink {
        padding: 2px 5px;
        font-family: sans-serif;
        font-size: 10px;
 }
 
+td.selflink {
+       padding-right: 0px;
+}
+
 td.sha1 {
        font-family: monospace;
 }
@@ -196,6 +204,10 @@ td.error {
        background-color: yellow;
 }
 
+td.current_head {
+       text-decoration: underline;
+}
+
 table.diff_tree span.file_status.new {
        color: #008000;
 }
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
new file mode 100755 (executable)
index 0000000..626fcc9
--- /dev/null
@@ -0,0 +1,2590 @@
+#!/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';
+use File::Find qw();
+binmode STDOUT, ':utf8';
+
+our $cgi = new CGI;
+our $version = "++GIT_VERSION++";
+our $my_url = $cgi->url();
+our $my_uri = $cgi->url(-absolute => 1);
+
+# core git executable to use
+# this can just be "git" if your webserver has a sensible PATH
+our $GIT = "++GIT_BINDIR++/git";
+
+# absolute fs-path which will be prepended to the project path
+#our $projectroot = "/pub/scm";
+our $projectroot = "++GITWEB_PROJECTROOT++";
+
+# location for temporary files needed for diffs
+our $git_temp = "/tmp/gitweb";
+
+# target of the home link on top of all pages
+our $home_link = $my_uri;
+
+# name of your site or organization to appear in page titles
+# replace this with something more descriptive for clearer bookmarks
+our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
+
+# html text to include at home page
+our $home_text = "++GITWEB_HOMETEXT++";
+
+# URI of default stylesheet
+our $stylesheet = "++GITWEB_CSS++";
+# URI of GIT logo
+our $logo = "++GITWEB_LOGO++";
+
+# source of projects list
+our $projects_list = "++GITWEB_LIST++";
+
+# default blob_plain mimetype and default charset for text/plain blob
+our $default_blob_plain_mimetype = 'text/plain';
+our $default_text_plain_charset  = undef;
+
+# file to use for guessing MIME types before trying /etc/mime.types
+# (relative to the current git repository)
+our $mimetypes_file = undef;
+
+our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
+require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
+
+# version of the core git binary
+our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
+
+$projects_list ||= $projectroot;
+if (! -d $git_temp) {
+       mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
+}
+
+# ======================================================================
+# input validation and dispatch
+our $action = $cgi->param('a');
+if (defined $action) {
+       if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
+               die_error(undef, "Invalid action parameter");
+       }
+       # action which does not check rest of parameters
+       if ($action eq "opml") {
+               git_opml();
+               exit;
+       }
+}
+
+our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
+if (defined $project) {
+       $project =~ s|^/||;
+       $project =~ s|/$||;
+}
+if (defined $project && $project) {
+       if (!validate_input($project)) {
+               die_error(undef, "Invalid project parameter");
+       }
+       if (!(-d "$projectroot/$project")) {
+               die_error(undef, "No such directory");
+       }
+       if (!(-e "$projectroot/$project/HEAD")) {
+               die_error(undef, "No such project");
+       }
+       $ENV{'GIT_DIR'} = "$projectroot/$project";
+} else {
+       git_project_list();
+       exit;
+}
+
+our $file_name = $cgi->param('f');
+if (defined $file_name) {
+       if (!validate_input($file_name)) {
+               die_error(undef, "Invalid file parameter");
+       }
+}
+
+our $hash = $cgi->param('h');
+if (defined $hash) {
+       if (!validate_input($hash)) {
+               die_error(undef, "Invalid hash parameter");
+       }
+}
+
+our $hash_parent = $cgi->param('hp');
+if (defined $hash_parent) {
+       if (!validate_input($hash_parent)) {
+               die_error(undef, "Invalid hash parent parameter");
+       }
+}
+
+our $hash_base = $cgi->param('hb');
+if (defined $hash_base) {
+       if (!validate_input($hash_base)) {
+               die_error(undef, "Invalid hash base parameter");
+       }
+}
+
+our $page = $cgi->param('pg');
+if (defined $page) {
+       if ($page =~ m/[^0-9]$/) {
+               die_error(undef, "Invalid page parameter");
+       }
+}
+
+our $searchtext = $cgi->param('s');
+if (defined $searchtext) {
+       if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
+               die_error(undef, "Invalid search parameter");
+       }
+       $searchtext = quotemeta $searchtext;
+}
+
+# dispatch
+my %actions = (
+       "blame" => \&git_blame2,
+       "blobdiff" => \&git_blobdiff,
+       "blobdiff_plain" => \&git_blobdiff_plain,
+       "blob" => \&git_blob,
+       "blob_plain" => \&git_blob_plain,
+       "commitdiff" => \&git_commitdiff,
+       "commitdiff_plain" => \&git_commitdiff_plain,
+       "commit" => \&git_commit,
+       "heads" => \&git_heads,
+       "history" => \&git_history,
+       "log" => \&git_log,
+       "rss" => \&git_rss,
+       "search" => \&git_search,
+       "shortlog" => \&git_shortlog,
+       "summary" => \&git_summary,
+       "tag" => \&git_tag,
+       "tags" => \&git_tags,
+       "tree" => \&git_tree,
+);
+
+$action = 'summary' if (!defined($action));
+if (!defined($actions{$action})) {
+       die_error(undef, "Unknown action");
+}
+$actions{$action}->();
+exit;
+
+## ======================================================================
+## validation, quoting/unquoting and escaping
+
+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;
+}
+
+# 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);
+       $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
+       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;
+}
+
+# escape tabs (convert tabs to spaces)
+sub untabify {
+       my $line = shift;
+
+       while ((my $pos = index($line, "\t")) != -1) {
+               if (my $count = (8 - ($pos % 8))) {
+                       my $spaces = ' ' x $count;
+                       $line =~ s/\t/$spaces/;
+               }
+       }
+
+       return $line;
+}
+
+## ----------------------------------------------------------------------
+## HTML aware string manipulation
+
+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 = " ...";
+               $body =~ s/&[^;]*$//; # remove chopped character entities
+       }
+       return "$body$tail";
+}
+
+## ----------------------------------------------------------------------
+## functions returning short strings
+
+# CSS class for given age value (in seconds)
+sub age_class {
+       my $age = shift;
+
+       if ($age < 60*60*2) {
+               return "age0";
+       } elsif ($age < 60*60*24*2) {
+               return "age1";
+       } else {
+               return "age2";
+       }
+}
+
+# convert age in seconds to "nn units ago" string
+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;
+}
+
+# convert file mode in octal to symbolic file mode string
+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 '----------';
+       }
+}
+
+# convert file mode in octal to file type string
+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";
+       }
+}
+
+## ----------------------------------------------------------------------
+## functions returning short HTML fragments, or transforming HTML fragments
+## which don't beling to other sections
+
+# format line of commit message or tag comment
+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;
+}
+
+# format marker of refs pointing to given object
+sub git_get_referencing {
+       my ($refs, $id) = @_;
+
+       if (defined $refs->{$id}) {
+               return ' <span class="tag">' . esc_html($refs->{$id}) . '</span>';
+       } else {
+               return "";
+       }
+}
+
+## ----------------------------------------------------------------------
+## git utility subroutines, invoking git commands
+
+# get HEAD ref of given project as hash
+sub git_read_head {
+       my $project = shift;
+       my $oENV = $ENV{'GIT_DIR'};
+       my $retval = undef;
+       $ENV{'GIT_DIR'} = "$projectroot/$project";
+       if (open my $fd, "-|", $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;
+}
+
+# get type of given object
+sub git_get_type {
+       my $hash = shift;
+
+       open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
+       my $type = <$fd>;
+       close $fd or return;
+       chomp $type;
+       return $type;
+}
+
+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
+}
+
+# get hash of given path at given ref
+sub git_get_hash_by_path {
+       my $base = shift;
+       my $path = shift || return undef;
+
+       my $tree = $base;
+
+       open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
+               or die_error(undef, "Open git-ls-tree failed");
+       my $line = <$fd>;
+       close $fd or return undef;
+
+       #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
+       $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
+       return $3;
+}
+
+## ......................................................................
+## git utility functions, directly accessing git repository
+
+# assumes that PATH is not symref
+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_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 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;
+               # attention: for $type == "" it saves only last path part of ref name
+               # e.g. from 'refs/heads/jn/gitweb' it would leave only 'gitweb'
+               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;
+}
+
+## ----------------------------------------------------------------------
+## parse to hash functions
+
+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;
+}
+
+sub git_read_tag {
+       my $tag_id = shift;
+       my %tag;
+       my @comment;
+
+       open my $fd, "-|", $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 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, "-|", $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;
+}
+
+## ......................................................................
+## parse to array of hashes functions
+
+sub git_read_refs {
+       my $ref_dir = shift;
+       my @reflist;
+
+       my @refs;
+       my $pfxlen = length("$projectroot/$project/$ref_dir");
+       File::Find::find(sub {
+               return if (/^\./);
+               if (-f $_) {
+                       push @refs, substr($File::Find::name, $pfxlen + 1);
+               }
+       }, "$projectroot/$project/$ref_dir");
+
+       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'};
+               } else {
+                       $ref_item{'reftype'} = $type;
+                       $ref_item{'name'} = $ref_file;
+                       $ref_item{'refid'} = $ref_id;
+               }
+
+               push @reflist, \%ref_item;
+       }
+       # sort tags by age
+       @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
+       return \@reflist;
+}
+
+## ----------------------------------------------------------------------
+## filesystem-related functions
+
+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);
+}
+
+## ......................................................................
+## mimetype related functions
+
+sub mimetype_guess_file {
+       my $filename = shift;
+       my $mimemap = shift;
+       -r $mimemap or return undef;
+
+       my %mimemap;
+       open(MIME, $mimemap) or return undef;
+       while (<MIME>) {
+               my ($mime, $exts) = split(/\t+/);
+               if (defined $exts) {
+                       my @exts = split(/\s+/, $exts);
+                       foreach my $ext (@exts) {
+                               $mimemap{$ext} = $mime;
+                       }
+               }
+       }
+       close(MIME);
+
+       $filename =~ /\.(.*?)$/;
+       return $mimemap{$1};
+}
+
+sub mimetype_guess {
+       my $filename = shift;
+       my $mime;
+       $filename =~ /\./ or return undef;
+
+       if ($mimetypes_file) {
+               my $file = $mimetypes_file;
+               #$file =~ m#^/# or $file = "$projectroot/$path/$file";
+               $mime = mimetype_guess_file($filename, $file);
+       }
+       $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
+       return $mime;
+}
+
+sub git_blob_plain_mimetype {
+       my $fd = shift;
+       my $filename = shift;
+
+       if ($filename) {
+               my $mime = mimetype_guess($filename);
+               $mime and return $mime;
+       }
+
+       # just in case
+       return $default_blob_plain_mimetype unless $fd;
+
+       if (-T $fd) {
+               return 'text/plain' .
+                      ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
+       } elsif (! $filename) {
+               return 'application/octet-stream';
+       } elsif ($filename =~ m/\.png$/i) {
+               return 'image/png';
+       } elsif ($filename =~ m/\.gif$/i) {
+               return 'image/gif';
+       } elsif ($filename =~ m/\.jpe?g$/i) {
+               return 'image/jpeg';
+       } else {
+               return 'application/octet-stream';
+       }
+}
+
+## ======================================================================
+## functions printing HTML: header, footer, error page
+
+sub git_header_html {
+       my $status = shift || "200 OK";
+       my $expires = shift;
+
+       my $title = "$site_name git";
+       if (defined $project) {
+               $title .= " - $project";
+               if (defined $action) {
+                       $title .= "/$action";
+                       if (defined $file_name) {
+                               $title .= " - $file_name";
+                               if ($action eq "tree" && $file_name !~ m|/$|) {
+                                       $title .= "/";
+                               }
+                       }
+               }
+       }
+       my $content_type;
+       # require explicit support from the UA if we are to send the page as
+       # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
+       # we have to do this because MSIE sometimes globs '*/*', pretending to
+       # support xhtml+xml but choking when it gets what it asked for.
+       if (defined $cgi->http('HTTP_ACCEPT') && $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {
+               $content_type = 'application/xhtml+xml';
+       } else {
+               $content_type = 'text/html';
+       }
+       print $cgi->header(-type=>$content_type, -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 -->
+<!-- git core binaries version $git_version -->
+<head>
+<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
+<meta name="robots" content="index, nofollow"/>
+<title>$title</title>
+<link rel="stylesheet" type="text/css" href="$stylesheet"/>
+EOF
+       if (defined $project) {
+               printf('<link rel="alternate" title="%s log" '.
+                      'href="%s" type="application/rss+xml"/>'."\n",
+                      esc_param($project),
+                      esc_param("$my_uri?p=$project;a=rss"));
+       }
+
+       print "</head>\n" .
+             "<body>\n" .
+             "<div class=\"page_header\">\n" .
+             "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
+             "<img src=\"$logo\" 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_base) {
+                       $search_hash = $hash_base;
+               } elsif (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;
+}
+
+## ----------------------------------------------------------------------
+## functions printing or outputting HTML: navigation
+
+sub git_page_nav {
+       my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
+       $extra = '' if !defined $extra; # pager or formats
+
+       my @navs = qw(summary shortlog log commit commitdiff tree);
+       if ($suppress) {
+               @navs = grep { $_ ne $suppress } @navs;
+       }
+
+       my %arg = map { $_, ''} @navs;
+       if (defined $head) {
+               for (qw(commit commitdiff)) {
+                       $arg{$_} = ";h=$head";
+               }
+               if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
+                       for (qw(shortlog log)) {
+                               $arg{$_} = ";h=$head";
+                       }
+               }
+       }
+       $arg{tree} .= ";h=$treehead" if defined $treehead;
+       $arg{tree} .= ";hb=$treebase" if defined $treebase;
+
+       print "<div class=\"page_nav\">\n" .
+               (join " | ",
+                map { $_ eq $current
+                                        ? $_
+                                        : $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$_$arg{$_}")}, "$_")
+                                }
+                @navs);
+       print "<br/>\n$extra<br/>\n" .
+             "</div>\n";
+}
+
+sub git_get_paging_nav {
+       my ($action, $hash, $head, $page, $nrevs) = @_;
+       my $paging_nav;
+
+
+       if ($hash ne $head || $page) {
+               $paging_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action")}, "HEAD");
+       } else {
+               $paging_nav .= "HEAD";
+       }
+
+       if ($page > 0) {
+               $paging_nav .= " &sdot; " .
+                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page-1)),
+                                -accesskey => "p", -title => "Alt-p"}, "prev");
+       } else {
+               $paging_nav .= " &sdot; prev";
+       }
+
+       if ($nrevs >= (100 * ($page+1)-1)) {
+               $paging_nav .= " &sdot; " .
+                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page+1)),
+                                -accesskey => "n", -title => "Alt-n"}, "next");
+       } else {
+               $paging_nav .= " &sdot; next";
+       }
+
+       return $paging_nav;
+}
+
+## ......................................................................
+## functions printing or outputting HTML: div
+
+sub git_header_div {
+       my ($action, $title, $hash, $hash_base) = @_;
+       my $rest = '';
+
+       $rest .= ";h=$hash" if $hash;
+       $rest .= ";hb=$hash_base" if $hash_base;
+
+       print "<div class=\"header\">\n" .
+             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action$rest"),
+                      -class => "title"}, $title ? $title : $action) . "\n" .
+             "</div>\n";
+}
+
+sub git_print_page_path {
+       my $name = shift;
+       my $type = shift;
+
+       if (!defined $name) {
+               print "<div class=\"page_path\"><b>/</b></div>\n";
+       } elsif (defined $type && $type eq 'blob') {
+               print "<div class=\"page_path\"><b>" .
+                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
+       } else {
+               print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
+       }
+}
+
+## ......................................................................
+## functions printing large fragments of HTML
+
+sub git_shortlog_body {
+       # uses global variable $project
+       my ($revlist, $from, $to, $refs, $extra) = @_;
+       $from = 0 unless defined $from;
+       $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
+
+       print "<table class=\"shortlog\" cellspacing=\"0\">\n";
+       my $alternate = 0;
+       for (my $i = $from; $i <= $to; $i++) {
+               my $commit = $revlist->[$i];
+               #my $ref = defined $refs ? git_get_referencing($refs, $commit) : '';
+               my $ref = git_get_referencing($refs, $commit);
+               my %co = git_read_commit($commit);
+               if ($alternate) {
+                       print "<tr class=\"dark\">\n";
+               } else {
+                       print "<tr class=\"light\">\n";
+               }
+               $alternate ^= 1;
+               # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
+               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'}) . "$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>\n";
+       }
+       if (defined $extra) {
+               print "<tr>\n" .
+                     "<td colspan=\"4\">$extra</td>\n" .
+                     "</tr>\n";
+       }
+       print "</table>\n";
+}
+
+sub git_tags_body {
+       # uses global variable $project
+       my ($taglist, $from, $to, $extra) = @_;
+       $from = 0 unless defined $from;
+       $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
+
+       print "<table class=\"tags\" cellspacing=\"0\">\n";
+       my $alternate = 0;
+       for (my $i = $from; $i <= $to; $i++) {
+               my $entry = $taglist->[$i];
+               my %tag = %$entry;
+               my $comment_lines = $tag{'comment'};
+               my $comment = shift @$comment_lines;
+               my $comment_short;
+               if (defined $comment) {
+                       $comment_short = 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) {
+                       if (length($comment_short) < length($comment)) {
+                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
+                                              -class => "list", -title => $comment}, $comment_short);
+                       } else {
+                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
+                                              -class => "list"}, $comment);
+                       }
+               }
+               print "</td>\n" .
+                     "<td class=\"selflink\">";
+               if ($tag{'type'} eq "tag") {
+                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag");
+               } else {
+                       print "&nbsp;";
+               }
+               print "</td>\n" .
+                     "<td class=\"link\">" . " | " .
+                     $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");
+               } elsif ($tag{'reftype'} eq "blob") {
+                       print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$tag{'refid'}")}, "raw");
+               }
+               print "</td>\n" .
+                     "</tr>";
+       }
+       if (defined $extra) {
+               print "<tr>\n" .
+                     "<td colspan=\"5\">$extra</td>\n" .
+                     "</tr>\n";
+       }
+       print "</table>\n";
+}
+
+sub git_heads_body {
+       # uses global variable $project
+       my ($taglist, $head, $from, $to, $extra) = @_;
+       $from = 0 unless defined $from;
+       $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
+
+       print "<table class=\"heads\" cellspacing=\"0\">\n";
+       my $alternate = 0;
+       for (my $i = $from; $i <= $to; $i++) {
+               my $entry = $taglist->[$i];
+               my %tag = %$entry;
+               my $curr = $tag{'id'} eq $head;
+               if ($alternate) {
+                       print "<tr class=\"dark\">\n";
+               } else {
+                       print "<tr class=\"light\">\n";
+               }
+               $alternate ^= 1;
+               print "<td><i>$tag{'age'}</i></td>\n" .
+                     ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<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>";
+       }
+       if (defined $extra) {
+               print "<tr>\n" .
+                     "<td colspan=\"3\">$extra</td>\n" .
+                     "</tr>\n";
+       }
+       print "</table>\n";
+}
+
+## ----------------------------------------------------------------------
+## functions printing large fragments, format as one of arguments
+
+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, "-|", $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, "-|", $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 $diff_class = "";
+                       if ($char eq '+') {
+                               $diff_class = " add";
+                       } elsif ($char eq "-") {
+                               $diff_class = " rem";
+                       } elsif ($char eq "@") {
+                               $diff_class = " chunk_header";
+                       } elsif ($char eq "\\") {
+                               # skip errors
+                               next;
+                       }
+                       $line = untabify($line);
+                       print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
+               }
+       }
+       close $fd;
+
+       if (defined $from) {
+               unlink($from_tmp);
+       }
+       if (defined $to) {
+               unlink($to_tmp);
+       }
+}
+
+
+## ======================================================================
+## ======================================================================
+## actions
+
+sub git_project_list {
+       my $order = $cgi->param('o');
+       if (defined $order && $order !~ m/project|descr|owner|age/) {
+               die_error(undef, "Unknown order parameter");
+       }
+
+       my @list = git_read_projects();
+       my @projects;
+       if (!@list) {
+               die_error(undef, "No projects 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 class=\"project_list\">\n" .
+             "<tr>\n";
+       $order ||= "project";
+       if ($order eq "project") {
+               @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
+               print "<th>Project</th>\n";
+       } else {
+               print "<th>" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
+                              -class => "header"}, "Project") .
+                     "</th>\n";
+       }
+       if ($order eq "descr") {
+               @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
+               print "<th>Description</th>\n";
+       } else {
+               print "<th>" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
+                              -class => "header"}, "Description") .
+                     "</th>\n";
+       }
+       if ($order eq "owner") {
+               @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
+               print "<th>Owner</th>\n";
+       } else {
+               print "<th>" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
+                              -class => "header"}, "Owner") .
+                     "</th>\n";
+       }
+       if ($order eq "age") {
+               @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
+               print "<th>Last Change</th>\n";
+       } else {
+               print "<th>" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
+                              -class => "header"}, "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>" . esc_html($pr->{'descr'}) . "</td>\n" .
+                     "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
+               print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
+                     $pr->{'commit'}{'age_string'} . "</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 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();
+       git_page_nav('summary','', $head);
+
+       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, "-|", $GIT, "rev-list", "--max-count=17", git_read_head($project)
+               or die_error(undef, "Open git-rev-list failed");
+       my @revlist = map { chomp; $_ } <$fd>;
+       close $fd;
+       git_header_div('shortlog');
+       git_shortlog_body(\@revlist, 0, 15, $refs,
+                         $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "..."));
+
+       my $taglist = git_read_refs("refs/tags");
+       if (defined @$taglist) {
+               git_header_div('tags');
+               git_tags_body($taglist, 0, 15,
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "..."));
+       }
+
+       my $headlist = git_read_refs("refs/heads");
+       if (defined @$headlist) {
+               git_header_div('heads');
+               git_heads_body($headlist, $head, 0, 15,
+                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "..."));
+       }
+
+       git_footer_html();
+}
+
+sub git_tag {
+       my $head = git_read_head($project);
+       git_header_html();
+       git_page_nav('','', $head,undef,$head);
+       my %tag = git_read_tag($hash);
+       git_header_div('commit', esc_html($tag{'name'}), $hash);
+       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_blame2 {
+       my $fd;
+       my $ftype;
+       die_error(undef, "Permission denied") if (!git_get_project_config_bool ('blame'));
+       die_error('404 Not Found', "File name not defined") if (!$file_name);
+       $hash_base ||= git_read_head($project);
+       die_error(undef, "Couldn't find base commit") 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 looking up file");
+       }
+       $ftype = git_get_type($hash);
+       if ($ftype !~ "blob") {
+               die_error("400 Bad Request", "Object is not a blob");
+       }
+       open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
+               or die_error(undef, "Open git-blame failed");
+       git_header_html();
+       my $formats_nav =
+               $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");
+       git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
+       git_header_div('commit', esc_html($co{'title'}), $hash_base);
+       git_print_page_path($file_name, $ftype);
+       my @rev_color = (qw(light2 dark2));
+       my $num_colors = scalar(@rev_color);
+       my $current_color = 0;
+       my $last_rev;
+       print "<div class=\"page_body\">\n";
+       print "<table class=\"blame\">\n";
+       print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
+       while (<$fd>) {
+               /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
+               my $full_rev = $1;
+               my $rev = substr($full_rev, 0, 8);
+               my $lineno = $2;
+               my $data = $3;
+
+               if (!defined $last_rev) {
+                       $last_rev = $full_rev;
+               } elsif ($last_rev ne $full_rev) {
+                       $last_rev = $full_rev;
+                       $current_color = ++$current_color % $num_colors;
+               }
+               print "<tr class=\"$rev_color[$current_color]\">\n";
+               print "<td class=\"sha1\">" .
+                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$full_rev;f=$file_name")}, esc_html($rev)) . "</td>\n";
+               print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" . esc_html($lineno) . "</a></td>\n";
+               print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
+               print "</tr>\n";
+       }
+       print "</table>\n";
+       print "</div>";
+       close $fd or print "Reading blob failed\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', "File name not defined") if (!$file_name);
+       $hash_base ||= git_read_head($project);
+       die_error(undef, "Couldn't find base commit") 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, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
+               or die_error(undef, "Open git-annotate failed");
+       git_header_html();
+       my $formats_nav =
+               $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");
+       git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
+       git_header_div('commit', esc_html($co{'title'}), $hash_base);
+       git_print_page_path($file_name, 'blob');
+       print "<div class=\"page_body\">\n";
+       print <<HTML;
+<table class="blame">
+  <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_class;
+
+               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" class="error">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_class  = age_class($age);
+               $author     = esc_html ($author);
+               $author     =~ s/ /&nbsp;/g;
+
+               $data = untabify($data);
+               $data = esc_html ($data);
+
+               print <<HTML;
+  <tr class="$line_class[$line_class_num]">
+    <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
+    <td class="$age_class">$age_str</td>
+    <td>$author</td>
+    <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
+    <td class="pre">$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();
+       git_page_nav('','', $head,undef,$head);
+       git_header_div('summary', $project);
+
+       my $taglist = git_read_refs("refs/tags");
+       if (defined @$taglist) {
+               git_tags_body($taglist);
+       }
+       git_footer_html();
+}
+
+sub git_heads {
+       my $head = git_read_head($project);
+       git_header_html();
+       git_page_nav('','', $head,undef,$head);
+       git_header_div('summary', $project);
+
+       my $taglist = git_read_refs("refs/heads");
+       if (defined @$taglist) {
+               git_heads_body($taglist, $head);
+       }
+       git_footer_html();
+}
+
+sub git_blob_plain {
+       if (!defined $hash) {
+               if (defined $file_name) {
+                       my $base = $hash_base || git_read_head($project);
+                       $hash = git_get_hash_by_path($base, $file_name, "blob")
+                               or die_error(undef, "Error lookup file");
+               } else {
+                       die_error(undef, "No file name defined");
+               }
+       }
+       my $type = shift;
+       open my $fd, "-|", $GIT, "cat-file", "blob", $hash
+               or die_error(undef, "Couldn't cat $file_name, $hash");
+
+       $type ||= git_blob_plain_mimetype($fd, $file_name);
+
+       # save as filename, even when no $file_name is given
+       my $save_as = "$hash";
+       if (defined $file_name) {
+               $save_as = $file_name;
+       } elsif ($type =~ m/^text\//) {
+               $save_as .= '.txt';
+       }
+
+       print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
+       undef $/;
+       binmode STDOUT, ':raw';
+       print <$fd>;
+       binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
+       $/ = "\n";
+       close $fd;
+}
+
+sub git_blob {
+       if (!defined $hash) {
+               if (defined $file_name) {
+                       my $base = $hash_base || git_read_head($project);
+                       $hash = git_get_hash_by_path($base, $file_name, "blob")
+                               or die_error(undef, "Error lookup file");
+               } else {
+                       die_error(undef, "No file name defined");
+               }
+       }
+       my $have_blame = git_get_project_config_bool ('blame');
+       open my $fd, "-|", $GIT, "cat-file", "blob", $hash
+               or die_error(undef, "Couldn't cat $file_name, $hash");
+       my $mimetype = git_blob_plain_mimetype($fd, $file_name);
+       if ($mimetype !~ m/^text\//) {
+               close $fd;
+               return git_blob_plain($mimetype);
+       }
+       git_header_html();
+       my $formats_nav = '';
+       if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
+               if (defined $file_name) {
+                       if ($have_blame) {
+                               $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
+                       }
+                       $formats_nav .=
+                               $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");
+               } else {
+                       $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain");
+               }
+               git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
+               git_header_div('commit', esc_html($co{'title'}), $hash_base);
+       } else {
+               print "<div class=\"page_nav\">\n" .
+                     "<br/><br/></div>\n" .
+                     "<div class=\"title\">$hash</div>\n";
+       }
+       git_print_page_path($file_name, "blob");
+       print "<div class=\"page_body\">\n";
+       my $nr;
+       while (my $line = <$fd>) {
+               chomp $line;
+               $nr++;
+               $line = untabify($line);
+               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_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, "-|", $GIT, "ls-tree", '-z', $hash
+               or die_error(undef, "Open git-ls-tree failed");
+       my @entries = map { chomp; $_ } <$fd>;
+       close $fd or die_error(undef, "Reading tree failed");
+       $/ = "\n";
+
+       my $refs = read_info_ref();
+       my $ref = git_get_referencing($refs, $hash_base);
+       git_header_html();
+       my $base_key = "";
+       my $base = "";
+       my $have_blame = git_get_project_config_bool ('blame');
+       if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
+               $base_key = ";hb=$hash_base";
+               git_page_nav('tree','', $hash_base);
+               git_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
+       } 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/");
+       }
+       git_print_page_path($file_name, 'tree');
+       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 class=\"mode\">" . 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");
+                       if ($have_blame) {
+                               print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame");
+                       }
+                       print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$t_hash;hb=$hash_base;f=$base$t_name")}, "history") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
+                             "</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") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") .
+                             "</td>\n";
+               }
+               print "</tr>\n";
+       }
+       print "</table>\n" .
+             "</div>";
+       git_footer_html();
+}
+
+sub git_log {
+       my $head = git_read_head($project);
+       if (!defined $hash) {
+               $hash = $head;
+       }
+       if (!defined $page) {
+               $page = 0;
+       }
+       my $refs = read_info_ref();
+
+       my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
+       open my $fd, "-|", $GIT, "rev-list", $limit, $hash
+               or die_error(undef, "Open git-rev-list failed");
+       my @revlist = map { chomp; $_ } <$fd>;
+       close $fd;
+
+       my $paging_nav = git_get_paging_nav('log', $hash, $head, $page, $#revlist);
+
+       git_header_html();
+       git_page_nav('log','', $hash,undef,undef, $paging_nav);
+
+       if (!@revlist) {
+               my %co = git_read_commit($hash);
+
+               git_header_div('summary', $project);
+               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 = git_get_referencing($refs, $commit);
+               my %co = git_read_commit($commit);
+               next if !%co;
+               my %ad = date_str($co{'author_epoch'});
+               git_header_div('commit',
+                              "<span class=\"age\">$co{'age_string'}</span>" .
+                              esc_html($co{'title'}) . $ref,
+                              $commit);
+               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 $parent = $co{'parent'};
+       if (!defined $parent) {
+               $parent = "--root";
+       }
+       open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
+               or die_error(undef, "Open git-diff-tree failed");
+       my @difftree = map { chomp; $_ } <$fd>;
+       close $fd or die_error(undef, "Reading git-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 = git_get_referencing($refs, $co{'id'});
+       my $formats_nav = '';
+       if (defined $file_name && defined $co{'parent'}) {
+               my $parent = $co{'parent'};
+               $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;hb=$parent;f=$file_name")}, "blame");
+       }
+       git_header_html(undef, $expires);
+       git_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
+                    $hash, $co{'tree'}, $hash,
+                    $formats_nav);
+
+       if (defined $co{'parent'}) {
+               git_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
+       } else {
+               git_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
+       }
+       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 class=\"atnight\">%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 class=\"sha1\">$co{'id'}</td></tr>\n";
+       print "<tr>" .
+             "<td>tree</td>" .
+             "<td class=\"sha1\">" .
+             $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 class=\"sha1\">" . $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 class=\"signoff\">" . 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 class=\"diff_tree\">\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 class=\"file_status new\">[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=$parent;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
+                             "<td><span class=\"file_status deleted\">[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=$parent;f=$file")}, "blob") .
+                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$parent;f=$file")}, "history") .
+                             "</td>\n"
+               } elsif ($status eq "M" || $status eq "T") {
+                       my $mode_chnge = "";
+                       if ($from_mode != $to_mode) {
+                               $mode_chnge = " <span class=\"file_status mode_chnge\">[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;hb=$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 class=\"file_status moved\">[moved from " .
+                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$parent;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))) {
+               my $formats_nav =
+                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
+               git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
+               git_header_div('commit', esc_html($co{'title'}), $hash_base);
+       } else {
+               print "<div class=\"page_nav\">\n" .
+                     "<br/><br/></div>\n" .
+                     "<div class=\"title\">$hash vs $hash_parent</div>\n";
+       }
+       git_print_page_path($file_name, "blob");
+       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'} || '--root';
+       }
+       open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
+               or die_error(undef, "Open git-diff-tree failed");
+       my @difftree = map { chomp; $_ } <$fd>;
+       close $fd or die_error(undef, "Reading git-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 = git_get_referencing($refs, $co{'id'});
+       my $formats_nav =
+               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
+       git_header_html(undef, $expires);
+       git_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
+       git_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
+       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'
+               if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
+                       next;
+               }
+               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_parent;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_parent;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);
+       my %co = git_read_commit($hash);
+       if (!%co) {
+               die_error(undef, "Unknown commit object");
+       }
+       if (!defined $hash_parent) {
+               $hash_parent = $co{'parent'} || '--root';
+       }
+       open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
+               or die_error(undef, "Open git-diff-tree 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, "-|", $GIT, "rev-list", "HEAD";
+       my @commits = map { chomp; $_ } <$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 %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) {
+               if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
+                       next;
+               }
+               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_base) {
+               $hash_base = git_read_head($project);
+       }
+       my $ftype;
+       my %co = git_read_commit($hash_base);
+       if (!%co) {
+               die_error(undef, "Unknown commit object");
+       }
+       my $refs = read_info_ref();
+       git_header_html();
+       git_page_nav('','', $hash_base,$co{'tree'},$hash_base);
+       git_header_div('commit', esc_html($co{'title'}), $hash_base);
+       if (!defined $hash && defined $file_name) {
+               $hash = git_get_hash_by_path($hash_base, $file_name);
+       }
+       if (defined $hash) {
+               $ftype = git_get_type($hash);
+       }
+       git_print_page_path($file_name, $ftype);
+
+       open my $fd, "-|",
+               $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       while (my $line = <$fd>) {
+               if ($line =~ m/^([0-9a-fA-F]{40})/){
+                       my $commit = $1;
+                       my %co = git_read_commit($commit);
+                       if (!%co) {
+                               next;
+                       }
+                       my $ref = git_get_referencing($refs, $commit);
+                       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=$ftype;hb=$commit;f=$file_name")}, $ftype);
+                       my $blob = git_get_hash_by_path($hash_base, $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";
+               }
+       }
+       print "</table>\n";
+       close $fd;
+       git_footer_html();
+}
+
+sub git_search {
+       if (!defined $searchtext) {
+               die_error(undef, "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();
+       git_page_nav('','', $hash,$co{'tree'},$hash);
+       git_header_div('commit', esc_html($co{'title'}), $hash);
+
+       print "<table cellspacing=\"0\">\n";
+       my $alternate = 0;
+       if ($commit_search) {
+               $/ = "\0";
+               open my $fd, "-|", $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 class=\"match\">$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, "-|", "$GIT rev-list $hash | $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 class=\"match\">" . 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();
+
+       my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
+       open my $fd, "-|", $GIT, "rev-list", $limit, $hash
+               or die_error(undef, "Open git-rev-list failed");
+       my @revlist = map { chomp; $_ } <$fd>;
+       close $fd;
+
+       my $paging_nav = git_get_paging_nav('shortlog', $hash, $head, $page, $#revlist);
+       my $next_link = '';
+       if ($#revlist >= (100 * ($page+1)-1)) {
+               $next_link =
+                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)),
+                                -title => "Alt-n"}, "next");
+       }
+
+
+       git_header_html();
+       git_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
+       git_header_div('summary', $project);
+
+       git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
+
+       git_footer_html();
+}
+
+## ......................................................................
+## feeds (RSS, OPML)
+
+sub git_rss {
+       # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
+       open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_read_head($project)
+               or die_error(undef, "Open git-rev-list failed");
+       my @revlist = map { chomp; $_ } <$fd>;
+       close $fd or die_error(undef, "Reading git-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, "-|", $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>$site_name 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";
+}
diff --git a/help.c b/help.c
new file mode 100644 (file)
index 0000000..6484cb9
--- /dev/null
+++ b/help.c
@@ -0,0 +1,234 @@
+/*
+ * builtin-help.c
+ *
+ * Builtin help-related commands (help, usage, version)
+ */
+#include <sys/ioctl.h>
+#include "cache.h"
+#include "builtin.h"
+#include "exec_cmd.h"
+#include "common-cmds.h"
+
+
+/* most GUI terminals set COLUMNS (although some don't export it) */
+static int term_columns(void)
+{
+       char *col_string = getenv("COLUMNS");
+       int n_cols = 0;
+
+       if (col_string && (n_cols = atoi(col_string)) > 0)
+               return n_cols;
+
+#ifdef TIOCGWINSZ
+       {
+               struct winsize ws;
+               if (!ioctl(1, TIOCGWINSZ, &ws)) {
+                       if (ws.ws_col)
+                               return ws.ws_col;
+               }
+       }
+#endif
+
+       return 80;
+}
+
+static void oom(void)
+{
+       fprintf(stderr, "git: out of memory\n");
+       exit(1);
+}
+
+static inline void mput_char(char c, unsigned int num)
+{
+       while(num--)
+               putchar(c);
+}
+
+static struct cmdname {
+       size_t len;
+       char name[1];
+} **cmdname;
+static int cmdname_alloc, cmdname_cnt;
+
+static void add_cmdname(const char *name, int len)
+{
+       struct cmdname *ent;
+       if (cmdname_alloc <= cmdname_cnt) {
+               cmdname_alloc = cmdname_alloc + 200;
+               cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
+               if (!cmdname)
+                       oom();
+       }
+       ent = malloc(sizeof(*ent) + len);
+       if (!ent)
+               oom();
+       ent->len = len;
+       memcpy(ent->name, name, len);
+       ent->name[len] = 0;
+       cmdname[cmdname_cnt++] = ent;
+}
+
+static int cmdname_compare(const void *a_, const void *b_)
+{
+       struct cmdname *a = *(struct cmdname **)a_;
+       struct cmdname *b = *(struct cmdname **)b_;
+       return strcmp(a->name, b->name);
+}
+
+static void pretty_print_string_list(struct cmdname **cmdname, int longest)
+{
+       int cols = 1, rows;
+       int space = longest + 1; /* min 1 SP between words */
+       int max_cols = term_columns() - 1; /* don't print *on* the edge */
+       int i, j;
+
+       if (space < max_cols)
+               cols = max_cols / space;
+       rows = (cmdname_cnt + cols - 1) / cols;
+
+       qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
+
+       for (i = 0; i < rows; i++) {
+               printf("  ");
+
+               for (j = 0; j < cols; j++) {
+                       int n = j * rows + i;
+                       int size = space;
+                       if (n >= cmdname_cnt)
+                               break;
+                       if (j == cols-1 || n + rows >= cmdname_cnt)
+                               size = 1;
+                       printf("%-*s", size, cmdname[n]->name);
+               }
+               putchar('\n');
+       }
+}
+
+static void list_commands(const char *exec_path, const char *pattern)
+{
+       unsigned int longest = 0;
+       char path[PATH_MAX];
+       int dirlen;
+       DIR *dir = opendir(exec_path);
+       struct dirent *de;
+
+       if (!dir) {
+               fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
+               exit(1);
+       }
+
+       dirlen = strlen(exec_path);
+       if (PATH_MAX - 20 < dirlen) {
+               fprintf(stderr, "git: insanely long exec-path '%s'\n",
+                       exec_path);
+               exit(1);
+       }
+
+       memcpy(path, exec_path, dirlen);
+       path[dirlen++] = '/';
+
+       while ((de = readdir(dir)) != NULL) {
+               struct stat st;
+               int entlen;
+
+               if (strncmp(de->d_name, "git-", 4))
+                       continue;
+               strcpy(path+dirlen, de->d_name);
+               if (stat(path, &st) || /* stat, not lstat */
+                   !S_ISREG(st.st_mode) ||
+                   !(st.st_mode & S_IXUSR))
+                       continue;
+
+               entlen = strlen(de->d_name);
+               if (has_extension(de->d_name, ".exe"))
+                       entlen -= 4;
+
+               if (longest < entlen)
+                       longest = entlen;
+
+               add_cmdname(de->d_name + 4, entlen-4);
+       }
+       closedir(dir);
+
+       printf("git commands available in '%s'\n", exec_path);
+       printf("----------------------------");
+       mput_char('-', strlen(exec_path));
+       putchar('\n');
+       pretty_print_string_list(cmdname, longest - 4);
+       putchar('\n');
+}
+
+static void list_common_cmds_help(void)
+{
+       int i, longest = 0;
+
+       for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
+               if (longest < strlen(common_cmds[i].name))
+                       longest = strlen(common_cmds[i].name);
+       }
+
+       puts("The most commonly used git commands are:");
+       for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
+               printf("    %s", common_cmds[i].name);
+               mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
+               puts(common_cmds[i].help);
+       }
+       puts("(use 'git help -a' to get a list of all installed git commands)");
+}
+
+static void show_man_page(const char *git_cmd)
+{
+       const char *page;
+
+       if (!strncmp(git_cmd, "git", 3))
+               page = git_cmd;
+       else {
+               int page_len = strlen(git_cmd) + 4;
+               char *p = malloc(page_len + 1);
+               strcpy(p, "git-");
+               strcpy(p + 4, git_cmd);
+               p[page_len] = 0;
+               page = p;
+       }
+
+       execlp("man", "man", page, NULL);
+}
+
+void help_unknown_cmd(const char *cmd)
+{
+       printf("git: '%s' is not a git-command\n\n", cmd);
+       list_common_cmds_help();
+       exit(1);
+}
+
+int cmd_version(int argc, const char **argv, const char *prefix)
+{
+       printf("git version %s\n", git_version_string);
+       return 0;
+}
+
+int cmd_help(int argc, const char **argv, const char *prefix)
+{
+       const char *help_cmd = argc > 1 ? argv[1] : NULL;
+       const char *exec_path = git_exec_path();
+
+       if (!help_cmd) {
+               printf("usage: %s\n\n", git_usage_string);
+               list_common_cmds_help();
+               exit(1);
+       }
+
+       else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
+               printf("usage: %s\n\n", git_usage_string);
+               if(exec_path)
+                       list_commands(exec_path, "git-*");
+               exit(1);
+       }
+
+       else
+               show_man_page(help_cmd);
+
+       return 0;
+}
+
+
index 6ea39f0589e74767be31eed87d2248370108b5f7..de5fc44e660e3eb6f52191dd0983744447a2fe46 100644 (file)
@@ -870,7 +870,7 @@ static void process_ls_pack(struct remote_ls_ctx *ls)
 
        if (strlen(ls->dentry_name) == 63 &&
            !strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
-           has_extension(ls->dentry_name, 63, ".pack")) {
+           has_extension(ls->dentry_name, ".pack")) {
                get_sha1_hex(ls->dentry_name + 18, sha1);
                setup_index(ls->repo, sha1);
        }
index 4021e7d9274b713a7b5c48d66f9ddf21ddc07094..d45733ef64ecb8c153a0f62cfb193eed332402e6 100644 (file)
@@ -530,7 +530,7 @@ static void start_put(struct transfer_request *request)
        request->dest = xmalloc(strlen(request->url) + 14);
        sprintf(request->dest, "Destination: %s", request->url);
        posn += 38;
-       *(posn++) = '.';
+       *(posn++) = '_';
        strcpy(posn, request->lock->token);
 
        slot = get_active_slot();
index a91e39ecd6469c04da92391bdb24e5a05691b617..b20659c2591f2669e03570c9809249b37ec58c38 100644 (file)
@@ -447,7 +447,7 @@ int main(int argc, char **argv)
                usage(index_pack_usage);
        if (!index_name) {
                int len = strlen(pack_name);
-               if (!has_extension(pack_name, len, ".pack"))
+               if (!has_extension(pack_name, ".pack"))
                        die("packfile name '%s' does not end with '.pack'",
                            pack_name);
                index_name_buf = xmalloc(len);
index b6ec170c015661ec602a6c2bd2b2f10754dbbd9c..7d01845d392ab891c8cfe9db9a218e4c2d70e53e 100644 (file)
@@ -44,7 +44,7 @@ static int setup_indices(void)
        while ((de = readdir(dir)) != NULL) {
                int namelen = strlen(de->d_name);
                if (namelen != 50 ||
-                   !has_extension(de->d_name, namelen, ".pack"))
+                   !has_extension(de->d_name, ".pack"))
                        continue;
                get_sha1_hex(de->d_name + 5, sha1);
                setup_index(sha1);
index 2346e0e9ef0dbd247daf9d77c373029b491068c4..2a2fea3cb6bd1de059e7c0f8c2008c5fe8376b93 100644 (file)
@@ -22,7 +22,7 @@ static void remove_lock_file_on_signal(int signo)
        raise(signo);
 }
 
-int hold_lock_file_for_update(struct lock_file *lk, const char *path)
+static int lock_file(struct lock_file *lk, const char *path)
 {
        int fd;
        sprintf(lk->filename, "%s.lock", path);
@@ -41,6 +41,14 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path)
        return fd;
 }
 
+int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error)
+{
+       int fd = lock_file(lk, path);
+       if (fd < 0 && die_on_error)
+               die("unable to create '%s': %s", path, strerror(errno));
+       return fd;
+}
+
 int commit_lock_file(struct lock_file *lk)
 {
        char result_file[PATH_MAX];
diff --git a/name-rev.c b/name-rev.c
deleted file mode 100644 (file)
index f92f14e..0000000
+++ /dev/null
@@ -1,256 +0,0 @@
-#include <stdlib.h>
-#include "cache.h"
-#include "commit.h"
-#include "tag.h"
-#include "refs.h"
-
-static const char name_rev_usage[] =
-       "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
-
-typedef struct rev_name {
-       const char *tip_name;
-       int merge_traversals;
-       int generation;
-} rev_name;
-
-static long cutoff = LONG_MAX;
-
-static void name_rev(struct commit *commit,
-               const char *tip_name, int merge_traversals, int generation,
-               int deref)
-{
-       struct rev_name *name = (struct rev_name *)commit->util;
-       struct commit_list *parents;
-       int parent_number = 1;
-
-       if (!commit->object.parsed)
-               parse_commit(commit);
-
-       if (commit->date < cutoff)
-               return;
-
-       if (deref) {
-               char *new_name = xmalloc(strlen(tip_name)+3);
-               strcpy(new_name, tip_name);
-               strcat(new_name, "^0");
-               tip_name = new_name;
-
-               if (generation)
-                       die("generation: %d, but deref?", generation);
-       }
-
-       if (name == NULL) {
-               name = xmalloc(sizeof(rev_name));
-               commit->util = name;
-               goto copy_data;
-       } else if (name->merge_traversals > merge_traversals ||
-                       (name->merge_traversals == merge_traversals &&
-                        name->generation > generation)) {
-copy_data:
-               name->tip_name = tip_name;
-               name->merge_traversals = merge_traversals;
-               name->generation = generation;
-       } else
-               return;
-
-       for (parents = commit->parents;
-                       parents;
-                       parents = parents->next, parent_number++) {
-               if (parent_number > 1) {
-                       char *new_name = xmalloc(strlen(tip_name)+8);
-
-                       if (generation > 0)
-                               sprintf(new_name, "%s~%d^%d", tip_name,
-                                               generation, parent_number);
-                       else
-                               sprintf(new_name, "%s^%d", tip_name, parent_number);
-
-                       name_rev(parents->item, new_name,
-                               merge_traversals + 1 , 0, 0);
-               } else {
-                       name_rev(parents->item, tip_name, merge_traversals,
-                               generation + 1, 0);
-               }
-       }
-}
-
-static int tags_only = 0;
-
-static int name_ref(const char *path, const unsigned char *sha1)
-{
-       struct object *o = parse_object(sha1);
-       int deref = 0;
-
-       if (tags_only && strncmp(path, "refs/tags/", 10))
-               return 0;
-
-       while (o && o->type == OBJ_TAG) {
-               struct tag *t = (struct tag *) o;
-               if (!t->tagged)
-                       break; /* broken repository */
-               o = parse_object(t->tagged->sha1);
-               deref = 1;
-       }
-       if (o && o->type == OBJ_COMMIT) {
-               struct commit *commit = (struct commit *)o;
-
-               if (!strncmp(path, "refs/heads/", 11))
-                       path = path + 11;
-               else if (!strncmp(path, "refs/", 5))
-                       path = path + 5;
-
-               name_rev(commit, strdup(path), 0, 0, deref);
-       }
-       return 0;
-}
-
-/* returns a static buffer */
-static const char* get_rev_name(struct object *o)
-{
-       static char buffer[1024];
-       struct rev_name *n;
-       struct commit *c;
-
-       if (o->type != OBJ_COMMIT)
-               return "undefined";
-       c = (struct commit *) o;
-       n = c->util;
-       if (!n)
-               return "undefined";
-
-       if (!n->generation)
-               return n->tip_name;
-
-       snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
-
-       return buffer;
-}
-
-int main(int argc, char **argv)
-{
-       struct object_array revs = { 0, 0, NULL };
-       int as_is = 0, all = 0, transform_stdin = 0;
-
-       setup_git_directory();
-       git_config(git_default_config);
-
-       if (argc < 2)
-               usage(name_rev_usage);
-
-       for (--argc, ++argv; argc; --argc, ++argv) {
-               unsigned char sha1[20];
-               struct object *o;
-               struct commit *commit;
-
-               if (!as_is && (*argv)[0] == '-') {
-                       if (!strcmp(*argv, "--")) {
-                               as_is = 1;
-                               continue;
-                       } else if (!strcmp(*argv, "--tags")) {
-                               tags_only = 1;
-                               continue;
-                       } else if (!strcmp(*argv, "--all")) {
-                               if (argc > 1)
-                                       die("Specify either a list, or --all, not both!");
-                               all = 1;
-                               cutoff = 0;
-                               continue;
-                       } else if (!strcmp(*argv, "--stdin")) {
-                               if (argc > 1)
-                                       die("Specify either a list, or --stdin, not both!");
-                               transform_stdin = 1;
-                               cutoff = 0;
-                               continue;
-                       }
-                       usage(name_rev_usage);
-               }
-
-               if (get_sha1(*argv, sha1)) {
-                       fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
-                                       *argv);
-                       continue;
-               }
-
-               o = deref_tag(parse_object(sha1), *argv, 0);
-               if (!o || o->type != OBJ_COMMIT) {
-                       fprintf(stderr, "Could not get commit for %s. Skipping.\n",
-                                       *argv);
-                       continue;
-               }
-
-               commit = (struct commit *)o;
-
-               if (cutoff > commit->date)
-                       cutoff = commit->date;
-
-               add_object_array((struct object *)commit, *argv, &revs);
-       }
-
-       for_each_ref(name_ref);
-
-       if (transform_stdin) {
-               char buffer[2048];
-               char *p, *p_start;
-
-               while (!feof(stdin)) {
-                       int forty = 0;
-                       p = fgets(buffer, sizeof(buffer), stdin);
-                       if (!p)
-                               break;
-
-                       for (p_start = p; *p; p++) {
-#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
-                               if (!ishex(*p))
-                                       forty = 0;
-                               else if (++forty == 40 &&
-                                               !ishex(*(p+1))) {
-                                       unsigned char sha1[40];
-                                       const char *name = "undefined";
-                                       char c = *(p+1);
-
-                                       forty = 0;
-
-                                       *(p+1) = 0;
-                                       if (!get_sha1(p - 39, sha1)) {
-                                               struct object *o =
-                                                       lookup_object(sha1);
-                                               if (o)
-                                                       name = get_rev_name(o);
-                                       }
-                                       *(p+1) = c;
-
-                                       if (!strcmp(name, "undefined"))
-                                               continue;
-
-                                       fwrite(p_start, p - p_start + 1, 1,
-                                              stdout);
-                                       printf(" (%s)", name);
-                                       p_start = p + 1;
-                               }
-                       }
-
-                       /* flush */
-                       if (p_start != p)
-                               fwrite(p_start, p - p_start, 1, stdout);
-               }
-       } else if (all) {
-               int i, max;
-
-               max = get_max_object_index();
-               for (i = 0; i < max; i++) {
-                       struct object * obj = get_indexed_object(i);
-                       if (!obj)
-                               continue;
-                       printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
-               }
-       } else {
-               int i;
-               for (i = 0; i < revs.nr; i++)
-                       printf("%s %s\n",
-                               revs.objects[i].name,
-                               get_rev_name(revs.objects[i].item));
-       }
-
-       return 0;
-}
-
diff --git a/pack-objects.c b/pack-objects.c
deleted file mode 100644 (file)
index 861c7f0..0000000
+++ /dev/null
@@ -1,1376 +0,0 @@
-#include "cache.h"
-#include "object.h"
-#include "blob.h"
-#include "commit.h"
-#include "tag.h"
-#include "tree.h"
-#include "delta.h"
-#include "pack.h"
-#include "csum-file.h"
-#include "tree-walk.h"
-#include <sys/time.h>
-#include <signal.h>
-
-static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
-
-struct object_entry {
-       unsigned char sha1[20];
-       unsigned long size;     /* uncompressed size */
-       unsigned long offset;   /* offset into the final pack file;
-                                * nonzero if already written.
-                                */
-       unsigned int depth;     /* delta depth */
-       unsigned int delta_limit;       /* base adjustment for in-pack delta */
-       unsigned int hash;      /* name hint hash */
-       enum object_type type;
-       enum object_type in_pack_type;  /* could be delta */
-       unsigned long delta_size;       /* delta data size (uncompressed) */
-       struct object_entry *delta;     /* delta base object */
-       struct packed_git *in_pack;     /* already in pack */
-       unsigned int in_pack_offset;
-       struct object_entry *delta_child; /* deltified objects who bases me */
-       struct object_entry *delta_sibling; /* other deltified objects who
-                                            * uses the same base as me
-                                            */
-       int preferred_base;     /* we do not pack this, but is encouraged to
-                                * be used as the base objectto delta huge
-                                * objects against.
-                                */
-};
-
-/*
- * Objects we are going to pack are collected in objects array (dynamically
- * expanded).  nr_objects & nr_alloc controls this array.  They are stored
- * in the order we see -- typically rev-list --objects order that gives us
- * nice "minimum seek" order.
- *
- * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
- * elements in the objects array.  The former is used to build the pack
- * index (lists object names in the ascending order to help offset lookup),
- * and the latter is used to group similar things together by try_delta()
- * heuristics.
- */
-
-static unsigned char object_list_sha1[20];
-static int non_empty = 0;
-static int no_reuse_delta = 0;
-static int local = 0;
-static int incremental = 0;
-static struct object_entry **sorted_by_sha, **sorted_by_type;
-static struct object_entry *objects = NULL;
-static int nr_objects = 0, nr_alloc = 0, nr_result = 0;
-static const char *base_name;
-static unsigned char pack_file_sha1[20];
-static int progress = 1;
-static volatile sig_atomic_t progress_update = 0;
-static int window = 10;
-
-/*
- * The object names in objects array are hashed with this hashtable,
- * to help looking up the entry by object name.  Binary search from
- * sorted_by_sha is also possible but this was easier to code and faster.
- * This hashtable is built after all the objects are seen.
- */
-static int *object_ix = NULL;
-static int object_ix_hashsz = 0;
-
-/*
- * Pack index for existing packs give us easy access to the offsets into
- * corresponding pack file where each object's data starts, but the entries
- * do not store the size of the compressed representation (uncompressed
- * size is easily available by examining the pack entry header).  We build
- * a hashtable of existing packs (pack_revindex), and keep reverse index
- * here -- pack index file is sorted by object name mapping to offset; this
- * pack_revindex[].revindex array is an ordered list of offsets, so if you
- * know the offset of an object, next offset is where its packed
- * representation ends.
- */
-struct pack_revindex {
-       struct packed_git *p;
-       unsigned long *revindex;
-} *pack_revindex = NULL;
-static int pack_revindex_hashsz = 0;
-
-/*
- * stats
- */
-static int written = 0;
-static int written_delta = 0;
-static int reused = 0;
-static int reused_delta = 0;
-
-static int pack_revindex_ix(struct packed_git *p)
-{
-       unsigned long ui = (unsigned long)p;
-       int i;
-
-       ui = ui ^ (ui >> 16); /* defeat structure alignment */
-       i = (int)(ui % pack_revindex_hashsz);
-       while (pack_revindex[i].p) {
-               if (pack_revindex[i].p == p)
-                       return i;
-               if (++i == pack_revindex_hashsz)
-                       i = 0;
-       }
-       return -1 - i;
-}
-
-static void prepare_pack_ix(void)
-{
-       int num;
-       struct packed_git *p;
-       for (num = 0, p = packed_git; p; p = p->next)
-               num++;
-       if (!num)
-               return;
-       pack_revindex_hashsz = num * 11;
-       pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
-       for (p = packed_git; p; p = p->next) {
-               num = pack_revindex_ix(p);
-               num = - 1 - num;
-               pack_revindex[num].p = p;
-       }
-       /* revindex elements are lazily initialized */
-}
-
-static int cmp_offset(const void *a_, const void *b_)
-{
-       unsigned long a = *(unsigned long *) a_;
-       unsigned long b = *(unsigned long *) b_;
-       if (a < b)
-               return -1;
-       else if (a == b)
-               return 0;
-       else
-               return 1;
-}
-
-/*
- * Ordered list of offsets of objects in the pack.
- */
-static void prepare_pack_revindex(struct pack_revindex *rix)
-{
-       struct packed_git *p = rix->p;
-       int num_ent = num_packed_objects(p);
-       int i;
-       void *index = p->index_base + 256;
-
-       rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
-       for (i = 0; i < num_ent; i++) {
-               unsigned int hl = *((unsigned int *)((char *) index + 24*i));
-               rix->revindex[i] = ntohl(hl);
-       }
-       /* This knows the pack format -- the 20-byte trailer
-        * follows immediately after the last object data.
-        */
-       rix->revindex[num_ent] = p->pack_size - 20;
-       qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset);
-}
-
-static unsigned long find_packed_object_size(struct packed_git *p,
-                                            unsigned long ofs)
-{
-       int num;
-       int lo, hi;
-       struct pack_revindex *rix;
-       unsigned long *revindex;
-       num = pack_revindex_ix(p);
-       if (num < 0)
-               die("internal error: pack revindex uninitialized");
-       rix = &pack_revindex[num];
-       if (!rix->revindex)
-               prepare_pack_revindex(rix);
-       revindex = rix->revindex;
-       lo = 0;
-       hi = num_packed_objects(p) + 1;
-       do {
-               int mi = (lo + hi) / 2;
-               if (revindex[mi] == ofs) {
-                       return revindex[mi+1] - ofs;
-               }
-               else if (ofs < revindex[mi])
-                       hi = mi;
-               else
-                       lo = mi + 1;
-       } while (lo < hi);
-       die("internal error: pack revindex corrupt");
-}
-
-static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
-{
-       unsigned long othersize, delta_size;
-       char type[10];
-       void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
-       void *delta_buf;
-
-       if (!otherbuf)
-               die("unable to read %s", sha1_to_hex(entry->delta->sha1));
-        delta_buf = diff_delta(otherbuf, othersize,
-                              buf, size, &delta_size, 0);
-        if (!delta_buf || delta_size != entry->delta_size)
-               die("delta size changed");
-        free(buf);
-        free(otherbuf);
-       return delta_buf;
-}
-
-/*
- * The per-object header is a pretty dense thing, which is
- *  - first byte: low four bits are "size", then three bits of "type",
- *    and the high bit is "size continues".
- *  - each byte afterwards: low seven bits are size continuation,
- *    with the high bit being "size continues"
- */
-static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
-{
-       int n = 1;
-       unsigned char c;
-
-       if (type < OBJ_COMMIT || type > OBJ_DELTA)
-               die("bad type %d", type);
-
-       c = (type << 4) | (size & 15);
-       size >>= 4;
-       while (size) {
-               *hdr++ = c | 0x80;
-               c = size & 0x7f;
-               size >>= 7;
-               n++;
-       }
-       *hdr = c;
-       return n;
-}
-
-static unsigned long write_object(struct sha1file *f,
-                                 struct object_entry *entry)
-{
-       unsigned long size;
-       char type[10];
-       void *buf;
-       unsigned char header[10];
-       unsigned hdrlen, datalen;
-       enum object_type obj_type;
-       int to_reuse = 0;
-
-       if (entry->preferred_base)
-               return 0;
-
-       obj_type = entry->type;
-       if (! entry->in_pack)
-               to_reuse = 0;   /* can't reuse what we don't have */
-       else if (obj_type == OBJ_DELTA)
-               to_reuse = 1;   /* check_object() decided it for us */
-       else if (obj_type != entry->in_pack_type)
-               to_reuse = 0;   /* pack has delta which is unusable */
-       else if (entry->delta)
-               to_reuse = 0;   /* we want to pack afresh */
-       else
-               to_reuse = 1;   /* we have it in-pack undeltified,
-                                * and we do not need to deltify it.
-                                */
-
-       if (! to_reuse) {
-               buf = read_sha1_file(entry->sha1, type, &size);
-               if (!buf)
-                       die("unable to read %s", sha1_to_hex(entry->sha1));
-               if (size != entry->size)
-                       die("object %s size inconsistency (%lu vs %lu)",
-                           sha1_to_hex(entry->sha1), size, entry->size);
-               if (entry->delta) {
-                       buf = delta_against(buf, size, entry);
-                       size = entry->delta_size;
-                       obj_type = OBJ_DELTA;
-               }
-               /*
-                * The object header is a byte of 'type' followed by zero or
-                * more bytes of length.  For deltas, the 20 bytes of delta
-                * sha1 follows that.
-                */
-               hdrlen = encode_header(obj_type, size, header);
-               sha1write(f, header, hdrlen);
-
-               if (entry->delta) {
-                       sha1write(f, entry->delta, 20);
-                       hdrlen += 20;
-               }
-               datalen = sha1write_compressed(f, buf, size);
-               free(buf);
-       }
-       else {
-               struct packed_git *p = entry->in_pack;
-               use_packed_git(p);
-
-               datalen = find_packed_object_size(p, entry->in_pack_offset);
-               buf = (char *) p->pack_base + entry->in_pack_offset;
-               sha1write(f, buf, datalen);
-               unuse_packed_git(p);
-               hdrlen = 0; /* not really */
-               if (obj_type == OBJ_DELTA)
-                       reused_delta++;
-               reused++;
-       }
-       if (obj_type == OBJ_DELTA)
-               written_delta++;
-       written++;
-       return hdrlen + datalen;
-}
-
-static unsigned long write_one(struct sha1file *f,
-                              struct object_entry *e,
-                              unsigned long offset)
-{
-       if (e->offset)
-               /* offset starts from header size and cannot be zero
-                * if it is written already.
-                */
-               return offset;
-       e->offset = offset;
-       offset += write_object(f, e);
-       /* if we are deltified, write out its base object. */
-       if (e->delta)
-               offset = write_one(f, e->delta, offset);
-       return offset;
-}
-
-static void write_pack_file(void)
-{
-       int i;
-       struct sha1file *f;
-       unsigned long offset;
-       struct pack_header hdr;
-       unsigned last_percent = 999;
-       int do_progress = 0;
-
-       if (!base_name)
-               f = sha1fd(1, "<stdout>");
-       else {
-               f = sha1create("%s-%s.%s", base_name,
-                              sha1_to_hex(object_list_sha1), "pack");
-               do_progress = progress;
-       }
-       if (do_progress)
-               fprintf(stderr, "Writing %d objects.\n", nr_result);
-
-       hdr.hdr_signature = htonl(PACK_SIGNATURE);
-       hdr.hdr_version = htonl(PACK_VERSION);
-       hdr.hdr_entries = htonl(nr_result);
-       sha1write(f, &hdr, sizeof(hdr));
-       offset = sizeof(hdr);
-       if (!nr_result)
-               goto done;
-       for (i = 0; i < nr_objects; i++) {
-               offset = write_one(f, objects + i, offset);
-               if (do_progress) {
-                       unsigned percent = written * 100 / nr_result;
-                       if (progress_update || percent != last_percent) {
-                               fprintf(stderr, "%4u%% (%u/%u) done\r",
-                                       percent, written, nr_result);
-                               progress_update = 0;
-                               last_percent = percent;
-                       }
-               }
-       }
-       if (do_progress)
-               fputc('\n', stderr);
- done:
-       sha1close(f, pack_file_sha1, 1);
-}
-
-static void write_index_file(void)
-{
-       int i;
-       struct sha1file *f = sha1create("%s-%s.%s", base_name,
-                                       sha1_to_hex(object_list_sha1), "idx");
-       struct object_entry **list = sorted_by_sha;
-       struct object_entry **last = list + nr_result;
-       unsigned int array[256];
-
-       /*
-        * Write the first-level table (the list is sorted,
-        * but we use a 256-entry lookup to be able to avoid
-        * having to do eight extra binary search iterations).
-        */
-       for (i = 0; i < 256; i++) {
-               struct object_entry **next = list;
-               while (next < last) {
-                       struct object_entry *entry = *next;
-                       if (entry->sha1[0] != i)
-                               break;
-                       next++;
-               }
-               array[i] = htonl(next - sorted_by_sha);
-               list = next;
-       }
-       sha1write(f, array, 256 * sizeof(int));
-
-       /*
-        * Write the actual SHA1 entries..
-        */
-       list = sorted_by_sha;
-       for (i = 0; i < nr_result; i++) {
-               struct object_entry *entry = *list++;
-               unsigned int offset = htonl(entry->offset);
-               sha1write(f, &offset, 4);
-               sha1write(f, entry->sha1, 20);
-       }
-       sha1write(f, pack_file_sha1, 20);
-       sha1close(f, NULL, 1);
-}
-
-static int locate_object_entry_hash(const unsigned char *sha1)
-{
-       int i;
-       unsigned int ui;
-       memcpy(&ui, sha1, sizeof(unsigned int));
-       i = ui % object_ix_hashsz;
-       while (0 < object_ix[i]) {
-               if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20))
-                       return i;
-               if (++i == object_ix_hashsz)
-                       i = 0;
-       }
-       return -1 - i;
-}
-
-static struct object_entry *locate_object_entry(const unsigned char *sha1)
-{
-       int i;
-
-       if (!object_ix_hashsz)
-               return NULL;
-
-       i = locate_object_entry_hash(sha1);
-       if (0 <= i)
-               return &objects[object_ix[i]-1];
-       return NULL;
-}
-
-static void rehash_objects(void)
-{
-       int i;
-       struct object_entry *oe;
-
-       object_ix_hashsz = nr_objects * 3;
-       if (object_ix_hashsz < 1024)
-               object_ix_hashsz = 1024;
-       object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
-       memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
-       for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
-               int ix = locate_object_entry_hash(oe->sha1);
-               if (0 <= ix)
-                       continue;
-               ix = -1 - ix;
-               object_ix[ix] = i + 1;
-       }
-}
-
-static unsigned name_hash(const char *name)
-{
-       unsigned char c;
-       unsigned hash = 0;
-
-       /*
-        * 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.
-        */
-       while ((c = *name++) != 0) {
-               if (isspace(c))
-                       continue;
-               hash = (hash >> 2) + (c << 24);
-       }
-       return hash;
-}
-
-static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
-{
-       unsigned int idx = nr_objects;
-       struct object_entry *entry;
-       struct packed_git *p;
-       unsigned int found_offset = 0;
-       struct packed_git *found_pack = NULL;
-       int ix, status = 0;
-
-       if (!exclude) {
-               for (p = packed_git; p; p = p->next) {
-                       struct pack_entry e;
-                       if (find_pack_entry_one(sha1, &e, p)) {
-                               if (incremental)
-                                       return 0;
-                               if (local && !p->pack_local)
-                                       return 0;
-                               if (!found_pack) {
-                                       found_offset = e.offset;
-                                       found_pack = e.p;
-                               }
-                       }
-               }
-       }
-       if ((entry = locate_object_entry(sha1)) != NULL)
-               goto already_added;
-
-       if (idx >= nr_alloc) {
-               unsigned int needed = (idx + 1024) * 3 / 2;
-               objects = xrealloc(objects, needed * sizeof(*entry));
-               nr_alloc = needed;
-       }
-       entry = objects + idx;
-       nr_objects = idx + 1;
-       memset(entry, 0, sizeof(*entry));
-       memcpy(entry->sha1, sha1, 20);
-       entry->hash = hash;
-
-       if (object_ix_hashsz * 3 <= nr_objects * 4)
-               rehash_objects();
-       else {
-               ix = locate_object_entry_hash(entry->sha1);
-               if (0 <= ix)
-                       die("internal error in object hashing.");
-               object_ix[-1 - ix] = idx + 1;
-       }
-       status = 1;
-
- already_added:
-       if (progress_update) {
-               fprintf(stderr, "Counting objects...%d\r", nr_objects);
-               progress_update = 0;
-       }
-       if (exclude)
-               entry->preferred_base = 1;
-       else {
-               if (found_pack) {
-                       entry->in_pack = found_pack;
-                       entry->in_pack_offset = found_offset;
-               }
-       }
-       return status;
-}
-
-struct pbase_tree_cache {
-       unsigned char sha1[20];
-       int ref;
-       int temporary;
-       void *tree_data;
-       unsigned long tree_size;
-};
-
-static struct pbase_tree_cache *(pbase_tree_cache[256]);
-static int pbase_tree_cache_ix(const unsigned char *sha1)
-{
-       return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
-}
-static int pbase_tree_cache_ix_incr(int ix)
-{
-       return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
-}
-
-static struct pbase_tree {
-       struct pbase_tree *next;
-       /* This is a phony "cache" entry; we are not
-        * going to evict it nor find it through _get()
-        * mechanism -- this is for the toplevel node that
-        * would almost always change with any commit.
-        */
-       struct pbase_tree_cache pcache;
-} *pbase_tree;
-
-static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
-{
-       struct pbase_tree_cache *ent, *nent;
-       void *data;
-       unsigned long size;
-       char type[20];
-       int neigh;
-       int my_ix = pbase_tree_cache_ix(sha1);
-       int available_ix = -1;
-
-       /* pbase-tree-cache acts as a limited hashtable.
-        * your object will be found at your index or within a few
-        * slots after that slot if it is cached.
-        */
-       for (neigh = 0; neigh < 8; neigh++) {
-               ent = pbase_tree_cache[my_ix];
-               if (ent && !memcmp(ent->sha1, sha1, 20)) {
-                       ent->ref++;
-                       return ent;
-               }
-               else if (((available_ix < 0) && (!ent || !ent->ref)) ||
-                        ((0 <= available_ix) &&
-                         (!ent && pbase_tree_cache[available_ix])))
-                       available_ix = my_ix;
-               if (!ent)
-                       break;
-               my_ix = pbase_tree_cache_ix_incr(my_ix);
-       }
-
-       /* Did not find one.  Either we got a bogus request or
-        * we need to read and perhaps cache.
-        */
-       data = read_sha1_file(sha1, type, &size);
-       if (!data)
-               return NULL;
-       if (strcmp(type, tree_type)) {
-               free(data);
-               return NULL;
-       }
-
-       /* We need to either cache or return a throwaway copy */
-
-       if (available_ix < 0)
-               ent = NULL;
-       else {
-               ent = pbase_tree_cache[available_ix];
-               my_ix = available_ix;
-       }
-
-       if (!ent) {
-               nent = xmalloc(sizeof(*nent));
-               nent->temporary = (available_ix < 0);
-       }
-       else {
-               /* evict and reuse */
-               free(ent->tree_data);
-               nent = ent;
-       }
-       memcpy(nent->sha1, sha1, 20);
-       nent->tree_data = data;
-       nent->tree_size = size;
-       nent->ref = 1;
-       if (!nent->temporary)
-               pbase_tree_cache[my_ix] = nent;
-       return nent;
-}
-
-static void pbase_tree_put(struct pbase_tree_cache *cache)
-{
-       if (!cache->temporary) {
-               cache->ref--;
-               return;
-       }
-       free(cache->tree_data);
-       free(cache);
-}
-
-static int name_cmp_len(const char *name)
-{
-       int i;
-       for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
-               ;
-       return i;
-}
-
-static void add_pbase_object(struct tree_desc *tree,
-                            const char *name,
-                            int cmplen,
-                            const char *fullname)
-{
-       struct name_entry entry;
-
-       while (tree_entry(tree,&entry)) {
-               unsigned long size;
-               char type[20];
-
-               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(fullname);
-                       add_object_entry(entry.sha1, hash, 1);
-                       return;
-               }
-               if (!strcmp(type, tree_type)) {
-                       struct tree_desc sub;
-                       struct pbase_tree_cache *tree;
-                       const char *down = name+cmplen+1;
-                       int downlen = name_cmp_len(down);
-
-                       tree = pbase_tree_get(entry.sha1);
-                       if (!tree)
-                               return;
-                       sub.buf = tree->tree_data;
-                       sub.size = tree->tree_size;
-
-                       add_pbase_object(&sub, down, downlen, fullname);
-                       pbase_tree_put(tree);
-               }
-       }
-}
-
-static unsigned *done_pbase_paths;
-static int done_pbase_paths_num;
-static int done_pbase_paths_alloc;
-static int done_pbase_path_pos(unsigned hash)
-{
-       int lo = 0;
-       int hi = done_pbase_paths_num;
-       while (lo < hi) {
-               int mi = (hi + lo) / 2;
-               if (done_pbase_paths[mi] == hash)
-                       return mi;
-               if (done_pbase_paths[mi] < hash)
-                       hi = mi;
-               else
-                       lo = mi + 1;
-       }
-       return -lo-1;
-}
-
-static int check_pbase_path(unsigned hash)
-{
-       int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
-       if (0 <= pos)
-               return 1;
-       pos = -pos - 1;
-       if (done_pbase_paths_alloc <= done_pbase_paths_num) {
-               done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
-               done_pbase_paths = xrealloc(done_pbase_paths,
-                                           done_pbase_paths_alloc *
-                                           sizeof(unsigned));
-       }
-       done_pbase_paths_num++;
-       if (pos < done_pbase_paths_num)
-               memmove(done_pbase_paths + pos + 1,
-                       done_pbase_paths + pos,
-                       (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
-       done_pbase_paths[pos] = hash;
-       return 0;
-}
-
-static void add_preferred_base_object(char *name, unsigned hash)
-{
-       struct pbase_tree *it;
-       int cmplen = name_cmp_len(name);
-
-       if (check_pbase_path(hash))
-               return;
-
-       for (it = pbase_tree; it; it = it->next) {
-               if (cmplen == 0) {
-                       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, name, cmplen, name);
-               }
-       }
-}
-
-static void add_preferred_base(unsigned char *sha1)
-{
-       struct pbase_tree *it;
-       void *data;
-       unsigned long size;
-       unsigned char tree_sha1[20];
-
-       data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
-       if (!data)
-               return;
-
-       for (it = pbase_tree; it; it = it->next) {
-               if (!memcmp(it->pcache.sha1, tree_sha1, 20)) {
-                       free(data);
-                       return;
-               }
-       }
-
-       it = xcalloc(1, sizeof(*it));
-       it->next = pbase_tree;
-       pbase_tree = it;
-
-       memcpy(it->pcache.sha1, tree_sha1, 20);
-       it->pcache.tree_data = data;
-       it->pcache.tree_size = size;
-}
-
-static void check_object(struct object_entry *entry)
-{
-       char type[20];
-
-       if (entry->in_pack && !entry->preferred_base) {
-               unsigned char base[20];
-               unsigned long size;
-               struct object_entry *base_entry;
-
-               /* We want in_pack_type even if we do not reuse delta.
-                * There is no point not reusing non-delta representations.
-                */
-               check_reuse_pack_delta(entry->in_pack,
-                                      entry->in_pack_offset,
-                                      base, &size,
-                                      &entry->in_pack_type);
-
-               /* Check if it is delta, and the base is also an object
-                * we are going to pack.  If so we will reuse the existing
-                * delta.
-                */
-               if (!no_reuse_delta &&
-                   entry->in_pack_type == OBJ_DELTA &&
-                   (base_entry = locate_object_entry(base)) &&
-                   (!base_entry->preferred_base)) {
-
-                       /* Depth value does not matter - find_deltas()
-                        * will never consider reused delta as the
-                        * base object to deltify other objects
-                        * against, in order to avoid circular deltas.
-                        */
-
-                       /* uncompressed size of the delta data */
-                       entry->size = entry->delta_size = size;
-                       entry->delta = base_entry;
-                       entry->type = OBJ_DELTA;
-
-                       entry->delta_sibling = base_entry->delta_child;
-                       base_entry->delta_child = entry;
-
-                       return;
-               }
-               /* Otherwise we would do the usual */
-       }
-
-       if (sha1_object_info(entry->sha1, type, &entry->size))
-               die("unable to get type of object %s",
-                   sha1_to_hex(entry->sha1));
-
-       if (!strcmp(type, commit_type)) {
-               entry->type = OBJ_COMMIT;
-       } else if (!strcmp(type, tree_type)) {
-               entry->type = OBJ_TREE;
-       } else if (!strcmp(type, blob_type)) {
-               entry->type = OBJ_BLOB;
-       } else if (!strcmp(type, tag_type)) {
-               entry->type = OBJ_TAG;
-       } else
-               die("unable to pack object %s of type %s",
-                   sha1_to_hex(entry->sha1), type);
-}
-
-static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
-{
-       struct object_entry *child = me->delta_child;
-       unsigned int m = n;
-       while (child) {
-               unsigned int c = check_delta_limit(child, n + 1);
-               if (m < c)
-                       m = c;
-               child = child->delta_sibling;
-       }
-       return m;
-}
-
-static void get_object_details(void)
-{
-       int i;
-       struct object_entry *entry;
-
-       prepare_pack_ix();
-       for (i = 0, entry = objects; i < nr_objects; i++, entry++)
-               check_object(entry);
-
-       if (nr_objects == nr_result) {
-               /*
-                * Depth of objects that depend on the entry -- this
-                * is subtracted from depth-max to break too deep
-                * delta chain because of delta data reusing.
-                * However, we loosen this restriction when we know we
-                * are creating a thin pack -- it will have to be
-                * expanded on the other end anyway, so do not
-                * artificially cut the delta chain and let it go as
-                * deep as it wants.
-                */
-               for (i = 0, entry = objects; i < nr_objects; i++, entry++)
-                       if (!entry->delta && entry->delta_child)
-                               entry->delta_limit =
-                                       check_delta_limit(entry, 1);
-       }
-}
-
-typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
-
-static entry_sort_t current_sort;
-
-static int sort_comparator(const void *_a, const void *_b)
-{
-       struct object_entry *a = *(struct object_entry **)_a;
-       struct object_entry *b = *(struct object_entry **)_b;
-       return current_sort(a,b);
-}
-
-static struct object_entry **create_sorted_list(entry_sort_t sort)
-{
-       struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
-       int i;
-
-       for (i = 0; i < nr_objects; i++)
-               list[i] = objects + i;
-       current_sort = sort;
-       qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
-       return list;
-}
-
-static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
-{
-       return memcmp(a->sha1, b->sha1, 20);
-}
-
-static struct object_entry **create_final_object_list(void)
-{
-       struct object_entry **list;
-       int i, j;
-
-       for (i = nr_result = 0; i < nr_objects; i++)
-               if (!objects[i].preferred_base)
-                       nr_result++;
-       list = xmalloc(nr_result * sizeof(struct object_entry *));
-       for (i = j = 0; i < nr_objects; i++) {
-               if (!objects[i].preferred_base)
-                       list[j++] = objects + i;
-       }
-       current_sort = sha1_sort;
-       qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator);
-       return list;
-}
-
-static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
-{
-       if (a->type < b->type)
-               return -1;
-       if (a->type > b->type)
-               return 1;
-       if (a->hash < b->hash)
-               return -1;
-       if (a->hash > b->hash)
-               return 1;
-       if (a->preferred_base < b->preferred_base)
-               return -1;
-       if (a->preferred_base > b->preferred_base)
-               return 1;
-       if (a->size < b->size)
-               return -1;
-       if (a->size > b->size)
-               return 1;
-       return a < b ? -1 : (a > b);
-}
-
-struct unpacked {
-       struct object_entry *entry;
-       void *data;
-       struct delta_index *index;
-};
-
-/*
- * We search for deltas _backwards_ in a list sorted by type and
- * by size, so that we see progressively smaller and smaller files.
- * That's because we prefer deltas to be from the bigger file
- * to the smaller - deletes are potentially cheaper, but perhaps
- * more importantly, the bigger file is likely the more recent
- * one.
- */
-static int try_delta(struct unpacked *trg, struct unpacked *src,
-                    unsigned max_depth)
-{
-       struct object_entry *trg_entry = trg->entry;
-       struct object_entry *src_entry = src->entry;
-       unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
-       char type[10];
-       void *delta_buf;
-
-       /* Don't bother doing diffs between different types */
-       if (trg_entry->type != src_entry->type)
-               return -1;
-
-       /* We do not compute delta to *create* objects we are not
-        * going to pack.
-        */
-       if (trg_entry->preferred_base)
-               return -1;
-
-       /*
-        * We do not bother to try a delta that we discarded
-        * on an earlier try, but only when reusing delta data.
-        */
-       if (!no_reuse_delta && trg_entry->in_pack &&
-           trg_entry->in_pack == src_entry->in_pack)
-               return 0;
-
-       /*
-        * 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 (trg_entry->delta_child) {
-               if (max_depth <= trg_entry->delta_limit)
-                       return 0;
-               max_depth -= trg_entry->delta_limit;
-       }
-       if (src_entry->depth >= max_depth)
-               return 0;
-
-       /* Now some size filtering heuristics. */
-       trg_size = trg_entry->size;
-       max_size = trg_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 < trg_size ? trg_size - src_size : 0;
-       if (sizediff >= max_size)
-               return 0;
-
-       /* Load data if not already done */
-       if (!trg->data) {
-               trg->data = read_sha1_file(trg_entry->sha1, type, &sz);
-               if (sz != trg_size)
-                       die("object %s inconsistent object length (%lu vs %lu)",
-                           sha1_to_hex(trg_entry->sha1), sz, trg_size);
-       }
-       if (!src->data) {
-               src->data = read_sha1_file(src_entry->sha1, type, &sz);
-               if (sz != src_size)
-                       die("object %s inconsistent object length (%lu vs %lu)",
-                           sha1_to_hex(src_entry->sha1), sz, src_size);
-       }
-       if (!src->index) {
-               src->index = create_delta_index(src->data, src_size);
-               if (!src->index)
-                       die("out of memory");
-       }
-
-       delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
-       if (!delta_buf)
-               return 0;
-
-       trg_entry->delta = src_entry;
-       trg_entry->delta_size = delta_size;
-       trg_entry->depth = src_entry->depth + 1;
-       free(delta_buf);
-       return 1;
-}
-
-static void progress_interval(int signum)
-{
-       progress_update = 1;
-}
-
-static void find_deltas(struct object_entry **list, int window, int depth)
-{
-       int i, idx;
-       unsigned int array_size = window * sizeof(struct unpacked);
-       struct unpacked *array = xmalloc(array_size);
-       unsigned processed = 0;
-       unsigned last_percent = 999;
-
-       memset(array, 0, array_size);
-       i = nr_objects;
-       idx = 0;
-       if (progress)
-               fprintf(stderr, "Deltifying %d objects.\n", nr_result);
-
-       while (--i >= 0) {
-               struct object_entry *entry = list[i];
-               struct unpacked *n = array + idx;
-               int j;
-
-               if (!entry->preferred_base)
-                       processed++;
-
-               if (progress) {
-                       unsigned percent = processed * 100 / nr_result;
-                       if (percent != last_percent || progress_update) {
-                               fprintf(stderr, "%4u%% (%u/%u) done\r",
-                                       percent, processed, nr_result);
-                               progress_update = 0;
-                               last_percent = percent;
-                       }
-               }
-
-               if (entry->delta)
-                       /* This happens if we decided to reuse existing
-                        * delta from a pack.  "!no_reuse_delta &&" is implied.
-                        */
-                       continue;
-
-               if (entry->size < 50)
-                       continue;
-               free_delta_index(n->index);
-               n->index = NULL;
-               free(n->data);
-               n->data = NULL;
-               n->entry = entry;
-
-               j = window;
-               while (--j > 0) {
-                       unsigned int other_idx = idx + j;
-                       struct unpacked *m;
-                       if (other_idx >= window)
-                               other_idx -= window;
-                       m = array + other_idx;
-                       if (!m->entry)
-                               break;
-                       if (try_delta(n, m, depth) < 0)
-                               break;
-               }
-               /* 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.
-                */
-               if (entry->delta && depth <= entry->depth)
-                       continue;
-
-               idx++;
-               if (idx >= window)
-                       idx = 0;
-       }
-
-       if (progress)
-               fputc('\n', stderr);
-
-       for (i = 0; i < window; ++i) {
-               free_delta_index(array[i].index);
-               free(array[i].data);
-       }
-       free(array);
-}
-
-static void prepare_pack(int window, int depth)
-{
-       get_object_details();
-       sorted_by_type = create_sorted_list(type_size_sort);
-       if (window && depth)
-               find_deltas(sorted_by_type, window+1, depth);
-}
-
-static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
-{
-       static const char cache[] = "pack-cache/pack-%s.%s";
-       char *cached_pack, *cached_idx;
-       int ifd, ofd, ifd_ix = -1;
-
-       cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
-       ifd = open(cached_pack, O_RDONLY);
-       if (ifd < 0)
-               return 0;
-
-       if (!pack_to_stdout) {
-               cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
-               ifd_ix = open(cached_idx, O_RDONLY);
-               if (ifd_ix < 0) {
-                       close(ifd);
-                       return 0;
-               }
-       }
-
-       if (progress)
-               fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
-                       sha1_to_hex(sha1));
-
-       if (pack_to_stdout) {
-               if (copy_fd(ifd, 1))
-                       exit(1);
-               close(ifd);
-       }
-       else {
-               char name[PATH_MAX];
-               snprintf(name, sizeof(name),
-                        "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
-               ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
-               if (ofd < 0)
-                       die("unable to open %s (%s)", name, strerror(errno));
-               if (copy_fd(ifd, ofd))
-                       exit(1);
-               close(ifd);
-
-               snprintf(name, sizeof(name),
-                        "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
-               ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
-               if (ofd < 0)
-                       die("unable to open %s (%s)", name, strerror(errno));
-               if (copy_fd(ifd_ix, ofd))
-                       exit(1);
-               close(ifd_ix);
-               puts(sha1_to_hex(sha1));
-       }
-
-       return 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 int git_pack_config(const char *k, const char *v)
-{
-       if(!strcmp(k, "pack.window")) {
-               window = git_config_int(k, v);
-               return 0;
-       }
-       return git_default_config(k, v);
-}
-
-int main(int argc, char **argv)
-{
-       SHA_CTX ctx;
-       char line[40 + 1 + PATH_MAX + 2];
-       int depth = 10, pack_to_stdout = 0;
-       struct object_entry **list;
-       int num_preferred_base = 0;
-       int i;
-
-       setup_git_directory();
-       git_config(git_pack_config);
-
-       progress = isatty(2);
-       for (i = 1; i < argc; i++) {
-               const char *arg = argv[i];
-
-               if (*arg == '-') {
-                       if (!strcmp("--non-empty", arg)) {
-                               non_empty = 1;
-                               continue;
-                       }
-                       if (!strcmp("--local", arg)) {
-                               local = 1;
-                               continue;
-                       }
-                       if (!strcmp("--progress", arg)) {
-                               progress = 1;
-                               continue;
-                       }
-                       if (!strcmp("--incremental", arg)) {
-                               incremental = 1;
-                               continue;
-                       }
-                       if (!strncmp("--window=", arg, 9)) {
-                               char *end;
-                               window = strtoul(arg+9, &end, 0);
-                               if (!arg[9] || *end)
-                                       usage(pack_usage);
-                               continue;
-                       }
-                       if (!strncmp("--depth=", arg, 8)) {
-                               char *end;
-                               depth = strtoul(arg+8, &end, 0);
-                               if (!arg[8] || *end)
-                                       usage(pack_usage);
-                               continue;
-                       }
-                       if (!strcmp("--progress", arg)) {
-                               progress = 1;
-                               continue;
-                       }
-                       if (!strcmp("-q", arg)) {
-                               progress = 0;
-                               continue;
-                       }
-                       if (!strcmp("--no-reuse-delta", arg)) {
-                               no_reuse_delta = 1;
-                               continue;
-                       }
-                       if (!strcmp("--stdout", arg)) {
-                               pack_to_stdout = 1;
-                               continue;
-                       }
-                       usage(pack_usage);
-               }
-               if (base_name)
-                       usage(pack_usage);
-               base_name = arg;
-       }
-
-       if (pack_to_stdout != !base_name)
-               usage(pack_usage);
-
-       prepare_packed_git();
-
-       if (progress) {
-               fprintf(stderr, "Generating pack...\n");
-               setup_progress_signal();
-       }
-
-       for (;;) {
-               unsigned char sha1[20];
-               unsigned hash;
-
-               if (!fgets(line, sizeof(line), stdin)) {
-                       if (feof(stdin))
-                               break;
-                       if (!ferror(stdin))
-                               die("fgets returned NULL, not EOF, not error!");
-                       if (errno != EINTR)
-                               die("fgets: %s", strerror(errno));
-                       clearerr(stdin);
-                       continue;
-               }
-
-               if (line[0] == '-') {
-                       if (get_sha1_hex(line+1, sha1))
-                               die("expected edge sha1, got garbage:\n %s",
-                                   line+1);
-                       if (num_preferred_base++ < window)
-                               add_preferred_base(sha1);
-                       continue;
-               }
-               if (get_sha1_hex(line, sha1))
-                       die("expected sha1, got garbage:\n %s", line);
-               hash = name_hash(line+41);
-               add_preferred_base_object(line+41, hash);
-               add_object_entry(sha1, hash, 0);
-       }
-       if (progress)
-               fprintf(stderr, "Done counting %d objects.\n", nr_objects);
-       sorted_by_sha = create_final_object_list();
-       if (non_empty && !nr_result)
-               return 0;
-
-       SHA1_Init(&ctx);
-       list = sorted_by_sha;
-       for (i = 0; i < nr_result; i++) {
-               struct object_entry *entry = *list++;
-               SHA1_Update(&ctx, entry->sha1, 20);
-       }
-       SHA1_Final(object_list_sha1, &ctx);
-       if (progress && (nr_objects != nr_result))
-               fprintf(stderr, "Result has %d objects.\n", nr_result);
-
-       if (reuse_cached_pack(object_list_sha1, pack_to_stdout))
-               ;
-       else {
-               if (nr_result)
-                       prepare_pack(window, depth);
-               if (progress && pack_to_stdout) {
-                       /* the other end usually displays progress itself */
-                       struct itimerval v = {{0,},};
-                       setitimer(ITIMER_REAL, &v, NULL);
-                       signal(SIGALRM, SIG_IGN );
-                       progress_update = 0;
-               }
-               write_pack_file();
-               if (!pack_to_stdout) {
-                       write_index_file();
-                       puts(sha1_to_hex(object_list_sha1));
-               }
-       }
-       if (progress)
-               fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
-                       nr_result, written, written_delta, reused, reused_delta);
-       return 0;
-}
diff --git a/pager.c b/pager.c
index 280f57f796c84d1c1fd54e295b542c3b33490b3e..dcb398da8e703de2999badb976dee7322eff470b 100644 (file)
--- a/pager.c
+++ b/pager.c
@@ -15,10 +15,12 @@ void setup_pager(void)
 {
        pid_t pid;
        int fd[2];
-       const char *pager = getenv("PAGER");
+       const char *pager = getenv("GIT_PAGER");
 
        if (!isatty(1))
                return;
+       if (!pager)
+               pager = getenv("PAGER");
        if (!pager)
                pager = "less";
        else if (!*pager || !strcmp(pager, "cat"))
diff --git a/refs.c b/refs.c
index b01835f634603f2f89c188bd56aae6fb45983ba3..86ef91661400376f6bdc2af37fac7982f8872df9 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -147,7 +147,7 @@ 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 (has_extension(de->d_name, namelen, ".lock"))
+                       if (has_extension(de->d_name, ".lock"))
                                continue;
                        memcpy(path + baselen, de->d_name, namelen+1);
                        if (stat(git_path("%s", path), &st) < 0)
@@ -319,13 +319,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path,
 
        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;
-       }
+       lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file, 1);
 
        return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
 }
index a1bb01ca3569279318333f8088ade35614167612..3db956dd5c96c6563a9505775570fbdfb4a8b720 100644 (file)
@@ -590,7 +590,7 @@ static void prepare_packed_git_one(char *objdir, int local)
                int namelen = strlen(de->d_name);
                struct packed_git *p;
 
-               if (!has_extension(de->d_name, namelen, ".idx"))
+               if (!has_extension(de->d_name, ".idx"))
                        continue;
 
                /* we have .idx.  Is it a file we can map? */
diff --git a/symbolic-ref.c b/symbolic-ref.c
deleted file mode 100644 (file)
index 193c87c..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-#include "cache.h"
-
-static const char git_symbolic_ref_usage[] =
-"git-symbolic-ref name [ref]";
-
-static void check_symref(const char *HEAD)
-{
-       unsigned char sha1[20];
-       const char *git_HEAD = strdup(git_path("%s", HEAD));
-       const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 0);
-       if (git_refs_heads_master) {
-               /* we want to strip the .git/ part */
-               int pfxlen = strlen(git_HEAD) - strlen(HEAD);
-               puts(git_refs_heads_master + pfxlen);
-       }
-       else
-               die("No such ref: %s", HEAD);
-}
-
-int main(int argc, const char **argv)
-{
-       setup_git_directory();
-       git_config(git_default_config);
-       switch (argc) {
-       case 2:
-               check_symref(argv[1]);
-               break;
-       case 3:
-               create_symref(strdup(git_path("%s", argv[1])), argv[2]);
-               break;
-       default:
-               usage(git_symbolic_ref_usage);
-       }
-       return 0;
-}
index b24c829f0f987a3a3951f1adfa7caaf2082da471..71c454356fbbcd5f67bdfc0b6ef7785fa8b712c1 100755 (executable)
@@ -88,7 +88,7 @@ test_expect_success setup '
 +*+ [initial] Initial
 EOF
 
-V=`git version | sed -e 's/^git version //'`
+V=`git version | sed -e 's/^git version //' -e 's/\./\\./g'`
 while read cmd
 do
        case "$cmd" in
@@ -103,7 +103,9 @@ do
        test_expect_success "git $cmd" '
                {
                        echo "\$ git $cmd"
-                       git $cmd | sed -e "s/$V/g-i-t--v-e-r-s-i-o-n/"
+                       git $cmd |
+                       sed -e "s/^\\(-*\\)$V\\(-*\\)\$/\\1g-i-t--v-e-r-s-i-o-n\2/" \
+                           -e "s/^\\( *boundary=\"-*\\)$V\\(-*\\)\"\$/\\1g-i-t--v-e-r-s-i-o-n\2\"/"
                        echo "\$"
                } >"$actual" &&
                if test -f "$expect"
index 00a7d762ce6867d4f14d8157ddee4daec3dea0f6..6bfb899ed10606886ea3ce444f636b687bbfb66d 100755 (executable)
@@ -3,7 +3,7 @@
 # Copyright (c) 2006 Junio C Hamano
 #
 
-test_description='git grep -w
+test_description='git grep various.
 '
 
 . ./test-lib.sh
@@ -19,7 +19,9 @@ test_expect_success setup '
        echo x x xx x >x &&
        echo y yy >y &&
        echo zzz > z &&
-       git add file x y z &&
+       mkdir t &&
+       echo test >t/t &&
+       git add file x y z t/t &&
        git commit -m initial
 '
 
@@ -80,6 +82,31 @@ do
                        diff expected actual
                fi
        '
+
+       test_expect_success "grep $L (t-1)" '
+               echo "${HC}t/t:1:test" >expected &&
+               git grep -n -e test $H >actual &&
+               diff expected actual
+       '
+
+       test_expect_success "grep $L (t-2)" '
+               echo "${HC}t:1:test" >expected &&
+               (
+                       cd t &&
+                       git grep -n -e test $H
+               ) >actual &&
+               diff expected actual
+       '
+
+       test_expect_success "grep $L (t-3)" '
+               echo "${HC}t/t:1:test" >expected &&
+               (
+                       cd t &&
+                       git grep --full-name -n -e test $H
+               ) >actual &&
+               diff expected actual
+       '
+
 done
 
 test_done
diff --git a/unpack-objects.c b/unpack-objects.c
deleted file mode 100644 (file)
index 48c1ee7..0000000
+++ /dev/null
@@ -1,311 +0,0 @@
-#include "cache.h"
-#include "object.h"
-#include "delta.h"
-#include "pack.h"
-#include "blob.h"
-#include "commit.h"
-#include "tag.h"
-#include "tree.h"
-
-#include <sys/time.h>
-
-static int dry_run, quiet;
-static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
-
-/* We always read in 4kB chunks. */
-static unsigned char buffer[4096];
-static unsigned long offset, len, eof;
-static SHA_CTX ctx;
-
-/*
- * Make sure at least "min" bytes are available in the buffer, and
- * return the pointer to the buffer.
- */
-static void * fill(int min)
-{
-       if (min <= len)
-               return buffer + offset;
-       if (eof)
-               die("unable to fill input");
-       if (min > sizeof(buffer))
-               die("cannot fill %d bytes", min);
-       if (offset) {
-               SHA1_Update(&ctx, buffer, offset);
-               memcpy(buffer, buffer + offset, len);
-               offset = 0;
-       }
-       do {
-               int ret = xread(0, buffer + len, sizeof(buffer) - len);
-               if (ret <= 0) {
-                       if (!ret)
-                               die("early EOF");
-                       die("read error on input: %s", strerror(errno));
-               }
-               len += ret;
-       } while (len < min);
-       return buffer;
-}
-
-static void use(int bytes)
-{
-       if (bytes > len)
-               die("used more bytes than were available");
-       len -= bytes;
-       offset += bytes;
-}
-
-static void *get_data(unsigned long size)
-{
-       z_stream stream;
-       void *buf = xmalloc(size);
-
-       memset(&stream, 0, sizeof(stream));
-
-       stream.next_out = buf;
-       stream.avail_out = size;
-       stream.next_in = fill(1);
-       stream.avail_in = len;
-       inflateInit(&stream);
-
-       for (;;) {
-               int ret = inflate(&stream, 0);
-               use(len - stream.avail_in);
-               if (stream.total_out == size && ret == Z_STREAM_END)
-                       break;
-               if (ret != Z_OK)
-                       die("inflate returned %d\n", ret);
-               stream.next_in = fill(1);
-               stream.avail_in = len;
-       }
-       inflateEnd(&stream);
-       return buf;
-}
-
-struct delta_info {
-       unsigned char base_sha1[20];
-       unsigned long size;
-       void *delta;
-       struct delta_info *next;
-};
-
-static struct delta_info *delta_list;
-
-static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
-{
-       struct delta_info *info = xmalloc(sizeof(*info));
-
-       memcpy(info->base_sha1, base_sha1, 20);
-       info->size = size;
-       info->delta = delta;
-       info->next = delta_list;
-       delta_list = info;
-}
-
-static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
-
-static void write_object(void *buf, unsigned long size, const char *type)
-{
-       unsigned char sha1[20];
-       if (write_sha1_file(buf, size, type, sha1) < 0)
-               die("failed to write object");
-       added_object(sha1, type, buf, size);
-}
-
-static int resolve_delta(const char *type,
-       void *base, unsigned long base_size, 
-       void *delta, unsigned long delta_size)
-{
-       void *result;
-       unsigned long result_size;
-
-       result = patch_delta(base, base_size,
-                            delta, delta_size,
-                            &result_size);
-       if (!result)
-               die("failed to apply delta");
-       free(delta);
-       write_object(result, result_size, type);
-       free(result);
-       return 0;
-}
-
-static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
-{
-       struct delta_info **p = &delta_list;
-       struct delta_info *info;
-
-       while ((info = *p) != NULL) {
-               if (!memcmp(info->base_sha1, sha1, 20)) {
-                       *p = info->next;
-                       p = &delta_list;
-                       resolve_delta(type, data, size, info->delta, info->size);
-                       free(info);
-                       continue;
-               }
-               p = &info->next;
-       }
-}
-
-static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
-{
-       void *buf = get_data(size);
-       const char *type;
-
-       switch (kind) {
-       case OBJ_COMMIT: type = commit_type; break;
-       case OBJ_TREE:   type = tree_type; break;
-       case OBJ_BLOB:   type = blob_type; break;
-       case OBJ_TAG:    type = tag_type; break;
-       default: die("bad type %d", kind);
-       }
-       if (!dry_run)
-               write_object(buf, size, type);
-       free(buf);
-       return 0;
-}
-
-static int unpack_delta_entry(unsigned long delta_size)
-{
-       void *delta_data, *base;
-       unsigned long base_size;
-       char type[20];
-       unsigned char base_sha1[20];
-       int result;
-
-       memcpy(base_sha1, fill(20), 20);
-       use(20);
-
-       delta_data = get_data(delta_size);
-       if (dry_run) {
-               free(delta_data);
-               return 0;
-       }
-
-       if (!has_sha1_file(base_sha1)) {
-               add_delta_to_list(base_sha1, delta_data, delta_size);
-               return 0;
-       }
-       base = read_sha1_file(base_sha1, type, &base_size);
-       if (!base)
-               die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
-       result = resolve_delta(type, base, base_size, delta_data, delta_size);
-       free(base);
-       return result;
-}
-
-static void unpack_one(unsigned nr, unsigned total)
-{
-       unsigned shift;
-       unsigned char *pack, c;
-       unsigned long size;
-       enum object_type type;
-
-       pack = fill(1);
-       c = *pack;
-       use(1);
-       type = (c >> 4) & 7;
-       size = (c & 15);
-       shift = 4;
-       while (c & 0x80) {
-               pack = fill(1);
-               c = *pack++;
-               use(1);
-               size += (c & 0x7f) << shift;
-               shift += 7;
-       }
-       if (!quiet) {
-               static unsigned long last_sec;
-               static unsigned last_percent;
-               struct timeval now;
-               unsigned percentage = (nr * 100) / total;
-
-               gettimeofday(&now, NULL);
-               if (percentage != last_percent || now.tv_sec != last_sec) {
-                       last_sec = now.tv_sec;
-                       last_percent = percentage;
-                       fprintf(stderr, "%4u%% (%u/%u) done\r", percentage, nr, total);
-               }
-       }
-       switch (type) {
-       case OBJ_COMMIT:
-       case OBJ_TREE:
-       case OBJ_BLOB:
-       case OBJ_TAG:
-               unpack_non_delta_entry(type, size);
-               return;
-       case OBJ_DELTA:
-               unpack_delta_entry(size);
-               return;
-       default:
-               die("bad object type %d", type);
-       }
-}
-
-static void unpack_all(void)
-{
-       int i;
-       struct pack_header *hdr = fill(sizeof(struct pack_header));
-       unsigned nr_objects = ntohl(hdr->hdr_entries);
-
-       if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
-               die("bad pack file");
-       if (!pack_version_ok(hdr->hdr_version))
-               die("unknown pack file version %d", ntohl(hdr->hdr_version));
-       fprintf(stderr, "Unpacking %d objects\n", nr_objects);
-
-       use(sizeof(struct pack_header));
-       for (i = 0; i < nr_objects; i++)
-               unpack_one(i+1, nr_objects);
-       if (delta_list)
-               die("unresolved deltas left after unpacking");
-}
-
-int main(int argc, char **argv)
-{
-       int i;
-       unsigned char sha1[20];
-
-       setup_git_directory();
-
-       quiet = !isatty(2);
-
-       for (i = 1 ; i < argc; i++) {
-               const char *arg = argv[i];
-
-               if (*arg == '-') {
-                       if (!strcmp(arg, "-n")) {
-                               dry_run = 1;
-                               continue;
-                       }
-                       if (!strcmp(arg, "-q")) {
-                               quiet = 1;
-                               continue;
-                       }
-                       usage(unpack_usage);
-               }
-
-               /* We don't take any non-flag arguments now.. Maybe some day */
-               usage(unpack_usage);
-       }
-       SHA1_Init(&ctx);
-       unpack_all();
-       SHA1_Update(&ctx, buffer, offset);
-       SHA1_Final(sha1, &ctx);
-       if (memcmp(fill(20), sha1, 20))
-               die("final sha1 did not match");
-       use(20);
-
-       /* Write the last part of the buffer to stdout */
-       while (len) {
-               int ret = xwrite(1, buffer + offset, len);
-               if (ret <= 0)
-                       break;
-               len -= ret;
-               offset += ret;
-       }
-
-       /* All done */
-       if (!quiet)
-               fprintf(stderr, "\n");
-       return 0;
-}
diff --git a/verify-pack.c b/verify-pack.c
deleted file mode 100644 (file)
index f440a39..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-#include "cache.h"
-#include "pack.h"
-
-static int verify_one_pack(const char *path, int verbose)
-{
-       char arg[PATH_MAX];
-       int len;
-       struct packed_git *pack;
-       int err;
-
-       len = strlcpy(arg, path, PATH_MAX);
-       if (len >= PATH_MAX)
-               return error("name too long: %s", path);
-
-       /*
-        * In addition to "foo.idx" we accept "foo.pack" and "foo";
-        * normalize these forms to "foo.idx" for add_packed_git().
-        */
-       if (has_extension(arg, len, ".pack")) {
-               strcpy(arg + len - 5, ".idx");
-               len--;
-       } else if (!has_extension(arg, len, ".idx")) {
-               if (len + 4 >= PATH_MAX)
-                       return error("name too long: %s.idx", arg);
-               strcpy(arg + len, ".idx");
-               len += 4;
-       }
-
-       /*
-        * add_packed_git() uses our buffer (containing "foo.idx") to
-        * build the pack filename ("foo.pack").  Make sure it fits.
-        */
-       if (len + 1 >= PATH_MAX) {
-               arg[len - 4] = '\0';
-               return error("name too long: %s.pack", arg);
-       }
-
-       pack = add_packed_git(arg, len, 1);
-       if (!pack)
-               return error("packfile %s not found.", arg);
-
-       err = verify_pack(pack, verbose);
-       free(pack);
-
-       return err;
-}
-
-static const char verify_pack_usage[] = "git-verify-pack [-v] <pack>...";
-
-int main(int ac, char **av)
-{
-       int err = 0;
-       int verbose = 0;
-       int no_more_options = 0;
-       int nothing_done = 1;
-
-       while (1 < ac) {
-               if (!no_more_options && av[1][0] == '-') {
-                       if (!strcmp("-v", av[1]))
-                               verbose = 1;
-                       else if (!strcmp("--", av[1]))
-                               no_more_options = 1;
-                       else
-                               usage(verify_pack_usage);
-               }
-               else {
-                       if (verify_one_pack(av[1], verbose))
-                               err = 1;
-                       nothing_done = 0;
-               }
-               ac--; av++;
-       }
-
-       if (nothing_done)
-               usage(verify_pack_usage);
-
-       return err;
-}