Update git to v1.8.2.1

This requires a small change to how we handle notes, but otherwise just
works.

Note that we can't use anything from v1.8.0 until v1.8.2.1 because some
of the symbols that we need for graph drawing were made private in
v1.8.0 and this was not reverted until v1.8.2.1.

Signed-off-by: John Keeping <john@keeping.me.uk>
Return const char * in cgit_{httpscheme, hosturl, rooturl}()

The return values of these functions are essentially constant and should
never be modified.

Note that this will introduce a compiler warning when we try to free the
return value of any of these functions. However, given that all of these
currently return statically allocated strings in some cases, they need
to be refactored before this can be done anyway.

Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
Maŕk cgit_environment members const

These reflect the values of environment variables and should never be
changed. Add another xstrdup() when we assign environment variables to
strings that are potentially non-constant.

Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
Do not unnecessarily strdup() environment variables

This reverts the memory duplication introduced in commit 60a2627, while
keeping everything else that has been cleaned up. The environment
variables are never modified, so we do not need to call xstrdupn() here.

Also, remove xstrdupn() which is no longer needed.

Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
Makefile: re-include cgit.conf in cgit.mk

This avoids needed to export every variable that might be used in
cgit.mk from the top-level Makefile.

Signed-off-by: John Keeping <john@keeping.me.uk>
cgit.mk: fix dependency handling

Git calculates the dependency files to be included using a simply
expanded Makefile variable, so it does not include the CGit objects that
are added after that Makefile has been processed.

We therefore need to include the dependency files ourselves in order to
get the dependency calculations right.  Do this.

Signed-off-by: John Keeping <john@keeping.me.uk>
Always #include corresponding .h in .c files

While doing this, remove declarations from header files where the
corresponding definition is declared "static" in order to avoid build
errors.

Also re-order existing headers in ui-*.c so that the file-specific
header always comes immediately after "cgit.h", helping with future
consistency.

Signed-off-by: John Keeping <john@keeping.me.uk>
html.c: Replace strdup() with xstrdup()

Use the xstrdup() wrapper which already bails out if strdup() returns a
NULL pointer.

Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
ui-refs.c: Remove global header variable

print_tag_header() is only called from cgit_print_tags() -- the
conditional invocation in print_tag() is never executed since
print_tag() is only called by cgit_print_tags() which already executes
print_tag_header() before (resulting in the global variable being always
set in when the condition is evaluated).

Remove the global variable and the conditional invocation.

Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
ui-refs.c: Refactor print_tag()

The code snippets for OBJ_TAG and other object types are almost
equivalent. Merge them and use a couple of inline if conditions to
select proper fields.

Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
Fix out-of-bounds memory accesses with virtual_root=""

The CGit configuration variable virtual_root is normalized so that it
does not have a trailing '/' character, but it is allowed to be empty
(the empty string and NULL have different meanings here) and there is
code that is insufficiently cautious when checking if it ends in a '/':

	if (virtual_root[strlen(virtual_root) - 1] != '/')

Clearly this check is redundant, but rather than simply removing it we
get a slight efficiency improvement by switching the normalization so
that the virtual_root variable always ends in '/'.  Do this with a new
"ensure_end" helper.

Signed-off-by: John Keeping <john@keeping.me.uk>
Mark char* fields in struct cgit_page as const

Signed-off-by: John Keeping <john@keeping.me.uk>
html.c: add various strbuf and varadic helpers

This adds the fmtalloc helper, html_txtf, html_vtxtf, and html_attrf.

These takes a printf style format string like htmlf but escapes the
resulting string.  The html_vtxtf variant takes a va_list whereas
html_txtf is variadic.

Signed-off-by: John Keeping <john@keeping.me.uk>
shared.c: add strbuf_ensure_end

This is a small helper so that we can easily ensure that a strbuf ends
with the specified character.

Signed-off-by: John Keeping <john@keeping.me.uk>
Convert cgit_print_error to a variadic function

This removes many uses of "fmt" which uses a fixed size static pool of
fixed size buffers.  Instead of relying on these, we now pass around
argument lists for as long as possible before using a strbuf to render
content of an arbitrary size.

Signed-off-by: John Keeping <john@keeping.me.uk>
Remove redundant calls to fmt("%s", ...)

After this change there is one remaining call 'fmt("%s", delim)' in
ui-shared.c but is needed as delim is stack allocated and so cannot be
returned from the function.

Signed-off-by: John Keeping <john@keeping.me.uk>
use struct strbuf instead of static buffers

Use "struct strbuf" from Git to remove the limit on file path length.

Notes on scan-tree:
This is slightly involved since I decided to pass the strbuf into
add_repo() and modify if whenever a new file name is required, which
should avoid any extra allocations within that function.  The pattern
there is to append the filename, use it and then reset the buffer to its
original length (retaining a trailing '/').

Notes on ui-snapshot:
Since write_archive modifies the argv array passed to it we
copy the argv_array values into a new array of char* and then free the
original argv_array structure and the new array without worrying about
what the values now look like.

Signed-off-by: John Keeping <john@keeping.me.uk>
Do not load user or system gitconfig and gitattributes

While doing any kind of git loading, unset HOME variables and set
NOSYSTEM variables so that cgit does not load any settings that a user
may have set for his own /usr/bin/git usage.

This fixes a fatal error introduced with git 1.8, whereupon git would
fatally exit when failing to access particular files.

The result of this is that only repo-local configuration files are
accessed:

zx2c4@thinkpad ~/Projects/cgit $ HOME=/root QUERY_STRING="url=foo/log"
CGIT_CONFIG=tests/trash/cgitrc strace -e access ./cgit >/dev/null
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
access("repos/foo/.git/objects", X_OK) = 0
access("repos/foo/.git/refs", X_OK) = 0
access("repos/foo/.git/config", R_OK) = 0
access("repos/foo/.git/config", R_OK) = 0
access("repos/foo/.git/objects/b3/bafdbf0183f4897ef8b1319cb8c490ed54717e", F_OK) = 0
access("repos/foo/.git/objects/b3/bafdbf0183f4897ef8b1319cb8c490ed54717e", F_OK) = 0
access("repos/foo/.git/objects/b3/bafdbf0183f4897ef8b1319cb8c490ed54717e", F_OK) = 0
access("repos/foo/.git/objects/b3/bafdbf0183f4897ef8b1319cb8c490ed54717e", F_OK) = 0
+++ exited with 0 +++

Reported-by: Ferry Huberts <ferry.huberts@pelagic.nl>
Tested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Tested-by: Ferry Huberts <ferry.huberts@pelagic.nl>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
tests: use Git's test framework

This allows tests to run in parallel as well as letting us use "prove"
or another TAP harness to run the tests.

Git's test framework requires Git to be fully built before letting any
tests run, so add a new target to the top-level Makefile which builds
all of Git instead of just libgit.a and make the "test" target depend on
that.

Signed-off-by: John Keeping <john@keeping.me.uk>
tests/.gitignore: update for using Git's test infrastructure

Signed-off-by: John Keeping <john@keeping.me.uk>