Commit 68535125 by Vicent Marti

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>
parent bd0a51c0
#ifndef INCLUDE_git_index_h__
#define INCLUDE_git_index_h__
#include "common.h"
#include "oid.h"
/**
* @file git/index.h
* @brief Git index parsing and manipulation routines
* @defgroup git_index Git index parsing and manipulation routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/** Memory representation of an index file. */
typedef struct git_index git_index;
/** Memory representation of a file entry in the index. */
typedef struct git_index_entry git_index_entry;
/**
* Create a new Git index object as a memory representation
* of the Git index file in 'index_path'.
*
* @param index_path the path to the index file in disk
* @return the index object; NULL if the index could not be created
*/
GIT_EXTERN(git_index *) git_index_alloc(const char *index_path);
/**
* Clear the contents (all the entries) of an index object.
* This clears the index object in memory; changes must be manually
* written to disk for them to take effect.
*
* @param index an existing index object
*/
GIT_EXTERN(void) git_index_clear(git_index *index);
/**
* Free an existing index object.
*
* @param index an existing index object
*/
GIT_EXTERN(void) git_index_free(git_index *index);
/**
* Update the contents of an existing index object in memory
* by reading from the hard disk.
*
* @param index an existing index object
* @return 0 on success, otherwise an error code
*/
GIT_EXTERN(int) git_index_read(git_index *index);
/**
* Write an existing index object from memory back to disk
* using an atomic file lock.
*
* @param index an existing index object
* @return 0 on success, otherwise an error code
*/
GIT_EXTERN(int) git_index_write(git_index *index);
/**
* Find the first index of any entires which point to given
* path in the Git index.
*
* @param index an existing index object
* @param path path to search
* @return an index >= 0 if found, -1 otherwise
*/
GIT_EXTERN(int) git_index_find(git_index *index, const char *path);
/**
* Add a new empty entry to the index.
*
* @param index an existing index object
* @param path filename pointed to by the entry
* @param stage stage for the entry
* @return 0 on success, otherwise an error code
*/
GIT_EXTERN(int) git_index_add(git_index *index, const char *path, int stage);
/** @} */
GIT_END_DECL
#endif
This diff is collapsed. Click to expand it.
#ifndef INCLUDE_index_h__
#define INCLUDE_index_h__
#include "fileops.h"
#include "filelock.h"
#include "git/odb.h"
#include "git/index.h"
#define GIT_IDXENTRY_NAMEMASK (0x0fff)
#define GIT_IDXENTRY_STAGEMASK (0x3000)
#define GIT_IDXENTRY_EXTENDED (0x4000)
#define GIT_IDXENTRY_VALID (0x8000)
#define GIT_IDXENTRY_STAGESHIFT 12
typedef struct {
uint32_t seconds;
uint32_t nanoseconds;
} git_index_time;
struct git_index_entry {
git_index_time ctime;
git_index_time mtime;
uint32_t dev;
uint32_t ino;
uint32_t mode;
uint32_t uid;
uint32_t gid;
uint32_t file_size;
git_oid oid;
uint16_t flags;
uint16_t flags_extended;
char *path;
};
struct git_index_tree {
char *name;
struct git_index_tree *parent;
struct git_index_tree **children;
size_t children_count;
size_t entries;
git_oid oid;
};
typedef struct git_index_tree git_index_tree;
struct git_index {
char *index_file_path;
time_t last_modified;
git_index_entry *entries;
unsigned int entries_size;
unsigned int entry_count;
unsigned int sorted:1,
on_disk:1;
git_index_tree *tree;
};
int git_index__write(git_index *index, git_filelock *file);
void git_index__sort(git_index *index);
int git_index__parse(git_index *index, const char *buffer, size_t buffer_size);
int git_index__remove_pos(git_index *index, unsigned int position);
int git_index__append(git_index *index, const git_index_entry *entry);
void git_index_tree__free(git_index_tree *tree);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment