Commit Graph

1171 Commits

Author SHA1 Message Date
Vicent Marti
46f8566a13 Add methods to access internal attributes in git_repo
Added several methods to access:

	- The ODB behind a repo
	- The SHA1 id behind a generic repo object
	- The type of a generic repo object

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-09-12 23:43:21 +03:00
Vicent Marti
370ce56910 Fix: do not export custom types in the extern API
Some compilers give linking problems when exporting 'uint32_t' as a
return type in the external API. Use generic types instead.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-09-09 00:48:09 +03:00
Vicent Marti
9c9f4fc11c Add support for manually freeing repo objects
A new method 'git_repository_object_free' allows to manually force the
freeing of a repository object, even though they are still automatically
managed by the repository and don't need to be freed by the user.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 23:40:54 +02:00
Vicent Marti
f2408cc2ef Fix object handling in git_repository
All loaded objects through git_repository_lookup are properly parsed &
free'd on failure.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 19:59:32 +02:00
Vicent Marti
0e465f979b Add auxiliary method git__hexdump
New function in util.c to do a dump of a buffer's contents in
hexadecimal to stdout.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 18:49:04 +02:00
Vicent Marti
003c269094 Finish the tree object API
The interface for loading and parsing tree objects from a repository has
been completed with all the required accesor methods for attributes,
support for manipulating individual tree entries and a new unit test
t0901-readtree which tries to load and parse a tree object from a
repository.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 18:49:04 +02:00
Vicent Marti
ff17642dc2 Add unit tests for index manipulation
Three new unit tests, t06XX files have been added.

	t0601-read: tests for loading index files from disk,
				for creating in-memory indexes and for accessing
				index entries.
	t0602-write: tests for writing index files back to disk
	t0603-sort: tests for properly sorting the entries array of an index

Two test indexes have been added in 'tests/resources/':

	test/resources/index: a sample index from a libgit2 repository

	test/resources/gitgit.index: a sample index from a git.git
		repository (includes TREE extension data)

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 18:49:04 +02:00
Vicent Marti
1baa25ee9b Move test resources to a common directory
All the external resources used by the tests are now placed inside the
common 'tests/resources' directory.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 18:49:04 +02:00
Vicent Marti
68535125b3 Add support for git index files
The new 'git_index' structure is an in-memory representation
of a git index on disk; the 'git_index_entry' structures represent
each one of the file entries on the index.

The following calls for index instantiation have been added:

	git_index_alloc(): instantiate a new index structure
	git_index_free(): free an existing index
	git_index_clear(): clear all the entires in an existing file

The following calls for index reading and writing have been added:

	git_index_read(): update the contents of the index structure from
					  its file on disk.

		Internally implemented through:
			git_index__parse()

	Index files are stored on disk in network byte order; all integer fields
	inside them are properly converted to the machine's byte order when
	loading them in memory. The parsing engine also distinguishes
	between normal index entries and extended entries with 2 extra bytes
	of flags.

	The 'TREE' extension for index entries is also loaded into memory:
	Tree caches stored in Index files are loaded into the
	'git_index_tree' structure pointed by the 'tree' pointer inside
	'git_index'.

	'index->tree' points to the root node of the tree cache; the full tree
	can be traversed through each of the node's 'tree->children'.

	Index files can be written back to disk through:

	git_index_write(): atomic writing of existing index objects
		backed by internal method git_index__write()

The following calls for entry manipulation have been added:

	git_index_add(): insert an empty entry to the index

	git_index_find(): search an entry by its path name

	git_index__append(): appends a new index entry to the end of the
						 list, resizing the entries array if required

	New index entries are always inserted at the end of the array; since the
	index entries must be sorted for it to be internally consistent, the
	index object is only sorted once, and if required, before accessing the
	whole entriea array (e.g. before writing to disk, before traversing,
	etc).

	git_index__remove_pos(): remove an index entry in a specific position

	git_index__sort(): sort the entries in the array by path name

	The entries array is sorted stably and in place using an
	insertion sort, which ought to be the most efficient approach
	since the entries array is always mostly-sorted.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 18:49:04 +02:00
Vicent Marti
bd0a51c0dd Add support for atomic file locking
The struct 'git_filelock' represents an atomically-locked
file, git-style.

Locked files can be modified atomically through the new file lock
interface:

int git_filelock_init(git_filelock *lock, const char *path);
int git_filelock_lock(git_filelock *lock, int append);
void git_filelock_unlock(git_filelock *lock);
int git_filelock_commit(git_filelock *lock);
int git_filelock_write(git_filelock *lock, const char *buffer, size_t length);

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 18:49:04 +02:00
Vicent Marti
3315782cb4 Redesigned the walking/object lookup interface
The old 'git_revpool' object has been removed and
split into two distinct objects with separate
functionality, in order to have separate methods for
object management and object walking.

*	A new object 'git_repository' does the high-level
	management of a repository's objects (commits, trees,
	tags, etc) on top of a 'git_odb'.

	Eventually, it will also manage other repository
	attributes (e.g. tag resolution, references, etc).

	See: src/git/repository.h

*	A new external method
		'git_repository_lookup(repo, oid, type)'
	has been added to the 'git_repository' API.

	All object lookups (git_XXX_lookup()) are now
	wrappers to this method, and duplicated code
	has been removed. The method does automatic type
	checking and returns a generic 'git_revpool_object'
	that can be cast to any specific object.

	See: src/git/repository.h

*	The external methods for object parsing of repository
	objects (git_XXX_parse()) have been removed.

	Loading objects from the repository is now managed
	through the 'lookup' functions. These objects are
	loaded with minimal information, and the relevant
	parsing is done automatically when the user requests
	any of the parsed attributes through accessor methods.

	An attribute has been added to 'git_repository' in
	order to force the parsing of all the repository objects
	immediately after lookup.

	See: src/git/commit.h
	See: src/git/tag.h
	See: src/git/tree.h

*	The previous walking functionality of the revpool
	is now found in 'git_revwalk', which does the actual
	revision walking on a repository; the attributes
	when walking through commits in a database have been
	decoupled from the actual commit objects.
	This increases performance when accessing commits
	during the walk and allows to have several
	'git_revwalk' instances working at the same time on
	top of the same repository, without having to load
	commits in memory several times.

	See: src/git/revwalk.h

*	The old 'git_revpool_table' has been renamed to
	'git_hashtable' and now works as a generic hashtable
	with support for any kind of object and custom hash
	functions.

	See: src/hashtable.h

*	All the relevant unit tests have been updated, renamed
	and grouped accordingly.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 18:48:55 +02:00
Vicent Marti
f875804487 Add loading and parsing of tag objects
Tag objects are now properly loaded from the revision pool.
New test t0801 checks for loading a parsing a series of tags, including
the tag of a tag.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-12 04:40:43 +02:00
Vicent Marti
364788e1d1 Refactor parsing methods
The 'parse_oid' and 'parse_person' methods which were used by the commit
parser are now global so they can be used when parsing other objects.

The 'git_commit_person' struct has been changed to a generic
'git_person'.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-07 00:59:58 +02:00
Vicent Marti
7e4f56a5bb Add packfile reading
Packed objects inside packfiles are now properly unpacked when calling
the git_odb__read_packed() method; delta'ed objects are also properly
generated when needed.

A new unit test 0204-readpack tries to read a couple hundred packed
objects from a standard packed repository.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-08-06 18:37:59 +02:00
Vicent Marti
d8603ed901 Add parsing of tree file contents.
The basic information (pointed trees and blobs) of each tree object in a
revision pool can now be parsed and queried.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-07-15 23:41:49 +02:00
Vicent Marti
3e590fb222 Changed test files to use tabs instead of spaces
Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-07-15 23:41:31 +02:00
Vicent Marti
52f2390b43 Add external API to access detailed commit attributes
The following new external methods have been added:

GIT_EXTERN(const char *) git_commit_message_short(git_commit *commit);
GIT_EXTERN(const char *) git_commit_message(git_commit *commit);
GIT_EXTERN(time_t) git_commit_time(git_commit *commit);
GIT_EXTERN(const git_commit_person *) git_commit_committer(git_commit *commit);
GIT_EXTERN(const git_commit_person *) git_commit_author(git_commit *commit);
GIT_EXTERN(const git_tree *) git_commit_tree(git_commit *commit);

A new structure, git_commit_person has been added to represent a
commit's author or committer.

The parsing of a commit has been split in two phases.
When adding a commit to the revision pool:
	- the commit's ODB object is opened
	- its raw contents are parsed for commit TIME, PARENTS and TREE
		(the minimal amount of data required to traverse the pool)
	- the commit's ODB object is closed

When querying for extended information on a commit:
	- the commit's ODB object is reopened
	- its raw contents are parsed for the requested information
	- the commit's ODB object remains open to handle additional queries

New unit tests have been added for the new functionality:

	In t0401-parse: parse_person_test
	In t0402-details: query_details_test

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-07-15 23:40:52 +02:00
Vicent Marti
225fe21522 Add support for tree objects in revision pools
Commits now store pointers to their tree objects.
Tree objects now work as separate git_revpool_object
entities.
Tree objects can be loaded and parsed inedependently
from commits.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-07-15 23:39:30 +02:00
Vicent Marti
40721f6b12 Changed revpool's object table to support arbitrary objects
git_revpool_object now has a type identifier for each object
type in a revpool (commits, trees, blobs, etc).

Trees can now be stored in the revision pool.

git_revpool_tableit now supports filtering objects by their
type when iterating through the object table.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-07-15 23:39:22 +02:00
Vicent Marti
b231ef3acd Add new tests: t0502-table, t0503-tableit
"t0502-table" tests for basic functionality of the objects
table:
	table_create	(creating a new object table)
	table_populate	(fill & lookup on the object table)
	table_resize	(dynamically resize the table)

"t0503-tableit" tests the iterator for object tables:
	table_iterator (make sure the iterator reaches all objects)

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-07-15 23:37:05 +02:00
Vicent Marti
088a731f00 Fixed memory leaks in test suite
Created commit objects in t0401-parse weren't being freed properly.
Updated the API documentation to note that commit objects are owned
by the revision pool and should not be freed manually.

The parents list of each commit was being freed twice after each test.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-07-10 12:15:12 -07:00
Vicent Marti
58b0cbea74 Actually free all commits when freeing a commit pool
Previously the objects table was being freed, but not
the actuall commits. All git_commit objects are freed
and hence invalidated when freeing the git_rp object
they belong to.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-07-10 12:14:30 -07:00
Vicent Marti
3cd8b687d5 Fix warning in util.h (signed vs unsigned comparison)
This fix had been delayed by Ramsay because on 32-bit systems it
highlights the fact that off_t is set to an invalid value.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2010-07-10 12:08:04 -07:00
Ramsay Jones
ca3939e682 msvc: Disable a level 4 warning and change -W3 to -W4
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
2010-06-07 19:46:17 +01:00
Ramsay Jones
0aaf8708b0 Makefile(s): Add -Wextra to CFLAGS
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
2010-06-07 19:45:57 +01:00
Ramsay Jones
4386ee2acb Add a 'git__' prefix to the block-sha1 functions
This reduces the global namespace pollution. These functions
were the only remaining external symbols (with the exception
of an PPC_SHA1 build) which did not start with 'git', and
since these are private library symbols the 'git__' prefix is
appropriate.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
2010-06-07 19:45:32 +01:00
Ramsay Jones
d03f567593 Remove the sha1.h header file and inline the content into hash.c
Given that the sha1.h header file should never be included into
any other file, since it represents an implementation detail of
hash.c, we remove the header and inline it's content.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
2010-06-07 19:45:12 +01:00
Ramsay Jones
bc874e116f msvc: Select the "fast" definition of the {get,put}_be32() macros
On Intel machines, the msvc compiler defines the CPU architecture
macros _M_IX86 and _M_X64 (equivalent to __i386__ and __x86_64__
respectively). Use these macros in the pre-processor expression
to select the "fast" definition of the {get,put}_be32() macros.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
2010-06-07 19:44:55 +01:00
Ramsay Jones
e045299989 Makefile: Add a target to generate C preprocessor output files
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
2010-06-07 19:44:32 +01:00
Ramsay Jones
552e23ba56 Fix a bug in the git_oid_to_string() function
When git_oid_to_string() was passed a buffer size larger than
GIT_OID_HEXSZ+1, the function placed the c-string NUL char at
the wrong position. Fix the code to place the NUL at the end
of the (possibly truncated) oid string.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
2010-06-07 19:44:04 +01:00
Ramsay Jones
f29249340c Style: Do not use (C99) // comments
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:56 +02:00
Ramsay Jones
b2bc567f25 Style: Fix brace placement and spacing
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:56 +02:00
Ramsay Jones
702bd70595 Fix a memory leak shown by valgrind
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:56 +02:00
Ramsay Jones
28f9832928 Fix inconsistent definition of off_t on Linux
In order to avoid inconsistent definitions of type off_t, all
compilation units should include the "common.h" header file
before certain system headers (those which directly or indirectly
lead to the definition of off_t). The "common.h" header contains
the definition of _FILE_OFFSET_BITS to select 64-bit file offsets.

The symptom of this inconsistency, while compiling with -Wextra, is
the following warning:

    In file included from src/common.h:50,
                     from src/commit.c:28:
    src/util.h: In function git__is_sizet:
    src/util.h:41: warning: comparison between signed and unsigned

In order to fix the problem, we simply remove the #include <time.h>
statement at the head of src/commit.c.  Note that src/commit.h also
includes <time.h>.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:56 +02:00
Ramsay Jones
c1b62b2ede Fix a "dereference of type-punned pointer" compiler warning
gcc (4.4.0) issues the following warning:

    src/revobject.c:33: warning: dereferencing type-punned pointer \
        will break strict-aliasing rules

We suppress the warning by copying the first 4 bytes from the oid
structure into an 'unsigned int' using memcpy(). This will also
fix any potential alignment issues on certain platforms.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:56 +02:00
Ramsay Jones
6e0fa05b58 Fix a doxygen warning
In particular, doxygen issues the following warning:

    .../src/git/revwalk.h:86: Warning: The following parameters of \
        gitrp_sorting(git_revpool *pool, unsigned int sort_mode) are \
        not documented:
          parameter 'pool'

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:56 +02:00
Ramsay Jones
84b9cec72c Fix sparse warnings: "symbol not declared. Should it be static?"
In particular, sparse issues the following warnings:

    src/revobject.c:29:14: warning: symbol 'max_load_factor' was \
        not declared. Should it be static?
    src/revobject.c:31:14: warning: symbol 'git_revpool_table__hash' was \
        not declared. Should it be static?

In order to suppress these warnings, we simply declare them as
static, since they are not (currently) referenced outside of this
file.

In the case of max_load_factor, this is probably correct. However,
this may not be appropriate for git_revpool_table__hash(), given
how it is named. So, this should either be re-named to reflect it's
non-external status, or a declaration needs to be added to the
revobject.h header file.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:55 +02:00
Ramsay Jones
ee1765e529 Fix sparse warnings: "Using plain integer as NULL pointer"
In order to suppress this warning, we could simply replace the
constant 0 with NULL. However, in this case, replacing the
comparison with 0 by !buffer is more idiomatic.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:55 +02:00
Ramsay Jones
468b12adb1 msvc: tests/t0403-lists.c: Fix a compiler warning
For more recent versions of msvc, the time_t type, as returned by
the time() function, is a 64-bit type. The srand() function, however,
expects an 'unsigned int' input parameter, leading to the warning.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:55 +02:00
Ramsay Jones
4e0d6d864d msvc: Fix an "conversion, loss of data" compiler warning
In particular, the compiler issues the following warning:

    src/revwalk.c(61) : warning C4244: '=' : conversion from \
        'unsigned int' to 'unsigned char', possible loss of data

In order to suppress the warning, we change the type of the
sorting "enum" field of the git_revpool structure to be consistent
with the sort_mode parameter of the gitrp_sorting() function.

Note that if the size of the git_revpool structure is an issue,
then we could change the type of the sort_mode parameter instead.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:55 +02:00
Ramsay Jones
5440906feb msvc: Fix some compiler warnings
In particular, the compiler issues the following warnings:

    src/revobject.c(29) : warning C4305: 'initializing' : truncation \
        from 'double' to 'const float'
    src/revobject.c(56) : warning C4244: '=' : conversion from \
        'const float' to 'unsigned int', possible loss of data
    src/revobject.c(149) : warning C4244: '=' : conversion from \
        'const float' to 'unsigned int', possible loss of data

In order to suppress the warnings we change the type of max_load_factor
to double, rather than change the initialiser to 0.65f, and cast the
result type of the expressions to 'unsigned int' as expected by the
assignment operators. Note that double should be able to represent all
unsigned int values without loss.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:55 +02:00
Ramsay Jones
8a7d625f53 Fix some "signed/unsigned comparison" compilation warnings
These warnings are issued by both gcc (-Wextra) and msvc (-W3).

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:55 +02:00
Ramsay Jones
5b7487bee6 Fix a memory corruption runtime error
On the msvc build, the tests t0401-parse and t0501-walk both
crash with a runtime error (ACCESS_VIOLATION). This is caused
by writing to un-allocated memory due to an under-allocation
of a git_revpool_table data structure.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:55 +02:00
Ramsay Jones
331578fb1d msvc: Fix a "declaration after statement" compilation error
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 11:18:55 +02:00
Vicent Marti
de141d4bb9 Improved error handling on auxilirary functions.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 10:32:07 +02:00
Vicent Marti
c2550609e3 Use the first 4 bytes of an OID as hash, instead of full hashing.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 10:32:07 +02:00
Vicent Marti
6bb7aa1318 Added new error codes. Improved error handling.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 10:32:07 +02:00
Vicent Marti
0daa6cdcad Removed trailing whitespace.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 10:32:07 +02:00
Vicent Marti
9b3577eda0 Fixed brace placement and converted spaces to tabs.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 10:32:07 +02:00
Vicent Marti
0cf02ff92d Added t0501-walk (simple test for all revision pool walking modes)
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Signed-off-by: Andreas Ericsson <ae@op5.se>
2010-06-02 10:32:07 +02:00