Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.c')
-rw-r--r--src/index.c345
1 files changed, 164 insertions, 181 deletions
diff --git a/src/index.c b/src/index.c
index 66e7a81da..f1ae9a710 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 the libgit2 contributors
+ * Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
@@ -135,22 +135,17 @@ int git_index_open(git_index **index_out, const char *index_path)
assert(index_out && index_path);
- index = git__malloc(sizeof(git_index));
- if (index == NULL)
- return GIT_ENOMEM;
-
- memset(index, 0x0, sizeof(git_index));
+ index = git__calloc(1, sizeof(git_index));
+ GITERR_CHECK_ALLOC(index);
index->index_file_path = git__strdup(index_path);
- if (index->index_file_path == NULL) {
- git__free(index);
- return GIT_ENOMEM;
- }
+ GITERR_CHECK_ALLOC(index->index_file_path);
- git_vector_init(&index->entries, 32, index_cmp);
+ if (git_vector_init(&index->entries, 32, index_cmp) < 0)
+ return -1;
/* Check if index file is stored on disk already */
- if (git_path_exists(index->index_file_path) == 0)
+ if (git_path_exists(index->index_file_path) == true)
index->on_disk = 1;
*index_out = index;
@@ -215,36 +210,35 @@ void git_index_clear(git_index *index)
int git_index_read(git_index *index)
{
- int error = GIT_SUCCESS, updated;
- git_fbuffer buffer = GIT_FBUFFER_INIT;
+ int error, updated;
+ git_buf buffer = GIT_BUF_INIT;
time_t mtime;
assert(index->index_file_path);
- if (!index->on_disk || git_path_exists(index->index_file_path) < 0) {
+ if (!index->on_disk || git_path_exists(index->index_file_path) == false) {
git_index_clear(index);
index->on_disk = 0;
- return GIT_SUCCESS;
+ return 0;
}
/* We don't want to update the mtime if we fail to parse the index */
mtime = index->last_modified;
- error = git_futils_readbuffer_updated(&buffer, index->index_file_path, &mtime, &updated);
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to read index");
+ error = git_futils_readbuffer_updated(
+ &buffer, index->index_file_path, &mtime, &updated);
+ if (error < 0)
+ return error;
if (updated) {
git_index_clear(index);
- error = parse_index(index, buffer.data, buffer.len);
+ error = parse_index(index, buffer.ptr, buffer.size);
- if (error == GIT_SUCCESS)
+ if (!error)
index->last_modified = mtime;
- git_futils_freebuffer(&buffer);
+ git_buf_free(&buffer);
}
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to parse index");
return error;
}
@@ -256,23 +250,24 @@ int git_index_write(git_index *index)
git_vector_sort(&index->entries);
- if ((error = git_filebuf_open(&file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to write index");
+ if ((error = git_filebuf_open(
+ &file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS)) < 0)
+ return error;
- if ((error = write_index(index, &file)) < GIT_SUCCESS) {
+ if ((error = write_index(index, &file)) < 0) {
git_filebuf_cleanup(&file);
- return git__rethrow(error, "Failed to write index");
+ return error;
}
- if ((error = git_filebuf_commit(&file, GIT_INDEX_FILE_MODE)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to write index");
+ if ((error = git_filebuf_commit(&file, GIT_INDEX_FILE_MODE)) < 0)
+ return error;
if (p_stat(index->index_file_path, &indexst) == 0) {
index->last_modified = indexst.st_mtime;
index->on_disk = 1;
}
- return GIT_SUCCESS;
+ return 0;
}
unsigned int git_index_entrycount(git_index *index)
@@ -293,6 +288,20 @@ git_index_entry *git_index_get(git_index *index, unsigned int n)
return git_vector_get(&index->entries, n);
}
+void git_index__init_entry_from_stat(struct stat *st, git_index_entry *entry)
+{
+ entry->ctime.seconds = (git_time_t)st->st_ctime;
+ entry->mtime.seconds = (git_time_t)st->st_mtime;
+ /* entry->mtime.nanoseconds = st->st_mtimensec; */
+ /* entry->ctime.nanoseconds = st->st_ctimensec; */
+ entry->dev = st->st_rdev;
+ entry->ino = st->st_ino;
+ entry->mode = index_create_mode(st->st_mode);
+ entry->uid = st->st_uid;
+ entry->gid = st->st_gid;
+ entry->file_size = st->st_size;
+}
+
static int index_entry_init(git_index_entry **entry_out, git_index *index, const char *rel_path, int stage)
{
git_index_entry *entry = NULL;
@@ -302,25 +311,20 @@ static int index_entry_init(git_index_entry **entry_out, git_index *index, const
git_buf full_path = GIT_BUF_INIT;
int error;
- if (INDEX_OWNER(index) == NULL)
- return git__throw(GIT_EBAREINDEX,
- "Failed to initialize entry. Repository is bare");
+ assert(stage >= 0 && stage <= 3);
- if (stage < 0 || stage > 3)
- return git__throw(GIT_ERROR,
- "Failed to initialize entry. Invalid stage %i", stage);
-
- workdir = git_repository_workdir(INDEX_OWNER(index));
- if (workdir == NULL)
- return git__throw(GIT_EBAREINDEX,
- "Failed to initialize entry. Cannot resolved workdir");
+ if (INDEX_OWNER(index) == NULL ||
+ (workdir = git_repository_workdir(INDEX_OWNER(index))) == NULL)
+ {
+ giterr_set(GITERR_INDEX,
+ "Could not initialize index entry. Repository is bare");
+ return -1;
+ }
- error = git_buf_joinpath(&full_path, workdir, rel_path);
- if (error < GIT_SUCCESS)
+ if ((error = git_buf_joinpath(&full_path, workdir, rel_path)) < 0)
return error;
- if (p_lstat(full_path.ptr, &st) < 0) {
- error = git__throw(GIT_ENOTFOUND, "Failed to initialize entry. '%s' cannot be opened. %s", full_path.ptr, strerror(errno));
+ if ((error = git_path_lstat(full_path.ptr, &st)) < 0) {
git_buf_free(&full_path);
return error;
}
@@ -332,34 +336,21 @@ static int index_entry_init(git_index_entry **entry_out, git_index *index, const
*/
/* write the blob to disk and get the oid */
- if ((error = git_blob_create_fromfile(&oid, INDEX_OWNER(index), rel_path)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to initialize index entry");
+ if ((error = git_blob_create_fromfile(&oid, INDEX_OWNER(index), rel_path)) < 0)
+ return error;
entry = git__calloc(1, sizeof(git_index_entry));
- if (!entry)
- return GIT_ENOMEM;
-
- entry->ctime.seconds = (git_time_t)st.st_ctime;
- entry->mtime.seconds = (git_time_t)st.st_mtime;
- /* entry.mtime.nanoseconds = st.st_mtimensec; */
- /* entry.ctime.nanoseconds = st.st_ctimensec; */
- entry->dev= st.st_rdev;
- entry->ino = st.st_ino;
- entry->mode = index_create_mode(st.st_mode);
- entry->uid = st.st_uid;
- entry->gid = st.st_gid;
- entry->file_size = st.st_size;
- entry->oid = oid;
+ GITERR_CHECK_ALLOC(entry);
+ git_index__init_entry_from_stat(&st, entry);
+
+ entry->oid = oid;
entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
entry->path = git__strdup(rel_path);
- if (entry->path == NULL) {
- git__free(entry);
- return GIT_ENOMEM;
- }
+ GITERR_CHECK_ALLOC(entry->path);
*entry_out = entry;
- return GIT_SUCCESS;
+ return 0;
}
static git_index_entry *index_entry_dup(const git_index_entry *source_entry)
@@ -394,10 +385,7 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace)
int position;
git_index_entry **entry_array;
- assert(index && entry);
-
- if (entry->path == NULL)
- return git__throw(GIT_EMISSINGOBJDATA, "Failed to insert into index. Entry has no path");
+ assert(index && entry && entry->path != NULL);
/* make sure that the path length flag is correct */
path_length = strlen(entry->path);
@@ -413,12 +401,8 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace)
* replacing is not requested: just insert entry at the end;
* the index is no longer sorted
*/
- if (!replace) {
- if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
- return GIT_ENOMEM;
-
- return GIT_SUCCESS;
- }
+ if (!replace)
+ return git_vector_insert(&index->entries, entry);
/* look if an entry with this path already exists */
position = git_index_find(index, entry->path);
@@ -427,12 +411,8 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace)
* if no entry exists add the entry at the end;
* the index is no longer sorted
*/
- if (position == GIT_ENOTFOUND) {
- if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
- return GIT_ENOMEM;
-
- return GIT_SUCCESS;
- }
+ if (position == GIT_ENOTFOUND)
+ return git_vector_insert(&index->entries, entry);
/* exists, replace it */
entry_array = (git_index_entry **) index->entries.contents;
@@ -440,7 +420,7 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace)
git__free(entry_array[position]);
entry_array[position] = entry;
- return GIT_SUCCESS;
+ return 0;
}
static int index_add(git_index *index, const char *path, int stage, int replace)
@@ -448,20 +428,15 @@ static int index_add(git_index *index, const char *path, int stage, int replace)
git_index_entry *entry = NULL;
int ret;
- ret = index_entry_init(&entry, index, path, stage);
- if (ret)
- goto err;
-
- ret = index_insert(index, entry, replace);
- if (ret)
- goto err;
+ if ((ret = index_entry_init(&entry, index, path, stage)) < 0 ||
+ (ret = index_insert(index, entry, replace)) < 0)
+ {
+ index_entry_free(entry);
+ return ret;
+ }
git_tree_cache_invalidate_path(index->tree, entry->path);
-
- return ret;
-err:
- index_entry_free(entry);
- return git__rethrow(ret, "Failed to append to index");
+ return 0;
}
int git_index_add(git_index *index, const char *path, int stage)
@@ -474,28 +449,23 @@ int git_index_append(git_index *index, const char *path, int stage)
return index_add(index, path, stage, 0);
}
-static int index_add2(git_index *index, const git_index_entry *source_entry,
- int replace)
+static int index_add2(
+ git_index *index, const git_index_entry *source_entry, int replace)
{
git_index_entry *entry = NULL;
int ret;
entry = index_entry_dup(source_entry);
- if (entry == NULL) {
- ret = GIT_ENOMEM;
- goto err;
- }
+ if (entry == NULL)
+ return -1;
- ret = index_insert(index, entry, replace);
- if (ret)
- goto err;
+ if ((ret = index_insert(index, entry, replace)) < 0) {
+ index_entry_free(entry);
+ return ret;
+ }
git_tree_cache_invalidate_path(index->tree, entry->path);
-
- return ret;
-err:
- index_entry_free(entry);
- return git__rethrow(ret, "Failed to append to index");
+ return 0;
}
int git_index_add2(git_index *index, const git_index_entry *source_entry)
@@ -514,13 +484,14 @@ int git_index_remove(git_index *index, int position)
git_index_entry *entry;
git_vector_sort(&index->entries);
+
entry = git_vector_get(&index->entries, position);
if (entry != NULL)
git_tree_cache_invalidate_path(index->tree, entry->path);
error = git_vector_remove(&index->entries, (unsigned int)position);
- if (error == GIT_SUCCESS)
+ if (!error)
index_entry_free(entry);
return error;
@@ -531,12 +502,22 @@ int git_index_find(git_index *index, const char *path)
return git_vector_bsearch2(&index->entries, index_srch, path);
}
+unsigned int git_index__prefix_position(git_index *index, const char *path)
+{
+ unsigned int pos;
+
+ git_vector_bsearch3(&pos, &index->entries, index_srch, path);
+
+ return pos;
+}
+
void git_index_uniq(git_index *index)
{
git_vector_uniq(&index->entries);
}
-const git_index_entry_unmerged *git_index_get_unmerged_bypath(git_index *index, const char *path)
+const git_index_entry_unmerged *git_index_get_unmerged_bypath(
+ git_index *index, const char *path)
{
int pos;
assert(index && path);
@@ -544,75 +525,87 @@ const git_index_entry_unmerged *git_index_get_unmerged_bypath(git_index *index,
if (!index->unmerged.length)
return NULL;
- if ((pos = git_vector_bsearch2(&index->unmerged, unmerged_srch, path)) < GIT_SUCCESS)
+ if ((pos = git_vector_bsearch2(&index->unmerged, unmerged_srch, path)) < 0)
return NULL;
return git_vector_get(&index->unmerged, pos);
}
-const git_index_entry_unmerged *git_index_get_unmerged_byindex(git_index *index, unsigned int n)
+const git_index_entry_unmerged *git_index_get_unmerged_byindex(
+ git_index *index, unsigned int n)
{
assert(index);
return git_vector_get(&index->unmerged, n);
}
+static int index_error_invalid(const char *message)
+{
+ giterr_set(GITERR_INDEX, "Invalid data in index - %s", message);
+ return -1;
+}
+
static int read_unmerged(git_index *index, const char *buffer, size_t size)
{
const char *endptr;
size_t len;
int i;
- git_vector_init(&index->unmerged, 16, unmerged_cmp);
+ if (git_vector_init(&index->unmerged, 16, unmerged_cmp) < 0)
+ return -1;
while (size) {
git_index_entry_unmerged *lost;
len = strlen(buffer) + 1;
if (size <= len)
- return git__throw(GIT_ERROR, "Failed to read unmerged entries");
+ return index_error_invalid("reading unmerged entries");
- if ((lost = git__malloc(sizeof(git_index_entry_unmerged))) == NULL)
- return GIT_ENOMEM;
+ lost = git__malloc(sizeof(git_index_entry_unmerged));
+ GITERR_CHECK_ALLOC(lost);
- if (git_vector_insert(&index->unmerged, lost) < GIT_SUCCESS)
- return git__throw(GIT_ERROR, "Failed to read unmerged entries");
+ if (git_vector_insert(&index->unmerged, lost) < 0)
+ return -1;
+ /* read NUL-terminated pathname for entry */
lost->path = git__strdup(buffer);
- if (!lost->path)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(lost->path);
size -= len;
buffer += len;
+ /* read 3 ASCII octal numbers for stage entries */
for (i = 0; i < 3; i++) {
int tmp;
- if (git__strtol32(&tmp, buffer, &endptr, 8) < GIT_SUCCESS ||
- !endptr || endptr == buffer || *endptr || (unsigned)tmp > UINT_MAX)
- return GIT_ERROR;
+ if (git__strtol32(&tmp, buffer, &endptr, 8) < 0 ||
+ !endptr || endptr == buffer || *endptr ||
+ (unsigned)tmp > UINT_MAX)
+ return index_error_invalid("reading unmerged entry stage");
lost->mode[i] = tmp;
len = (endptr + 1) - buffer;
if (size <= len)
- return git__throw(GIT_ERROR, "Failed to read unmerged entries");
+ return index_error_invalid("reading unmerged entry stage");
size -= len;
buffer += len;
}
+ /* read up to 3 OIDs for stage entries */
for (i = 0; i < 3; i++) {
if (!lost->mode[i])
continue;
if (size < 20)
- return git__throw(GIT_ERROR, "Failed to read unmerged entries");
+ return index_error_invalid("reading unmerged entry oid");
+
git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer);
size -= 20;
buffer += 20;
}
}
- return GIT_SUCCESS;
+ return 0;
}
static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffer_size)
@@ -658,7 +651,7 @@ static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffe
path_end = memchr(path_ptr, '\0', buffer_size);
if (path_end == NULL)
- return 0;
+ return 0;
path_length = path_end - path_ptr;
}
@@ -683,15 +676,15 @@ static int read_header(struct index_header *dest, const void *buffer)
dest->signature = ntohl(source->signature);
if (dest->signature != INDEX_HEADER_SIG)
- return GIT_EOBJCORRUPTED;
+ return index_error_invalid("incorrect header signature");
dest->version = ntohl(source->version);
if (dest->version != INDEX_VERSION_NUMBER_EXT &&
dest->version != INDEX_VERSION_NUMBER)
- return GIT_EOBJCORRUPTED;
+ return index_error_invalid("incorrect header version");
dest->entry_count = ntohl(source->entry_count);
- return GIT_SUCCESS;
+ return 0;
}
static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size)
@@ -714,10 +707,10 @@ static size_t read_extension(git_index *index, const char *buffer, size_t buffer
if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
/* tree cache */
if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {
- if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size) < GIT_SUCCESS)
+ if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size) < 0)
return 0;
} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
- if (read_unmerged(index, buffer + 8, dest.extension_size) < GIT_SUCCESS)
+ if (read_unmerged(index, buffer + 8, dest.extension_size) < 0)
return 0;
}
/* else, unsupported extension. We cannot parse this, but we can skip
@@ -739,21 +732,21 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
#define seek_forward(_increase) { \
if (_increase >= buffer_size) \
- return git__throw(GIT_EOBJCORRUPTED, "Failed to seek forward. Buffer size exceeded"); \
+ return index_error_invalid("ran out of data while parsing"); \
buffer += _increase; \
buffer_size -= _increase;\
}
if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
- return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer too small");
+ return index_error_invalid("insufficient buffer space");
/* Precalculate the SHA1 of the files's contents -- we'll match it to
* the provided SHA1 in the footer */
git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE);
/* Parse header */
- if (read_header(&header, buffer) < GIT_SUCCESS)
- return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header is corrupted");
+ if (read_header(&header, buffer) < 0)
+ return -1;
seek_forward(INDEX_HEADER_SIZE);
@@ -765,23 +758,22 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
git_index_entry *entry;
entry = git__malloc(sizeof(git_index_entry));
- if (entry == NULL)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(entry);
entry_size = read_entry(entry, buffer, buffer_size);
/* 0 bytes read means an object corruption */
if (entry_size == 0)
- return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Entry size is zero");
+ return index_error_invalid("invalid entry");
- if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
- return GIT_ENOMEM;
+ if (git_vector_insert(&index->entries, entry) < 0)
+ return -1;
seek_forward(entry_size);
}
if (i != header.entry_count)
- return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header entries changed while parsing");
+ return index_error_invalid("header entries changed while parsing");
/* There's still space for some extensions! */
while (buffer_size > INDEX_FOOTER_SIZE) {
@@ -791,43 +783,43 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
/* see if we have read any bytes from the extension */
if (extension_size == 0)
- return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Extension size is zero");
+ return index_error_invalid("extension size is zero");
seek_forward(extension_size);
}
if (buffer_size != INDEX_FOOTER_SIZE)
- return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer size does not match index footer size");
+ return index_error_invalid("buffer size does not match index footer size");
/* 160-bit SHA-1 over the content of the index file before this checksum. */
git_oid_fromraw(&checksum_expected, (const unsigned char *)buffer);
if (git_oid_cmp(&checksum_calculated, &checksum_expected) != 0)
- return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Calculated checksum does not match expected checksum");
+ return index_error_invalid("calculated checksum does not match expected");
#undef seek_forward
/* force sorting in the vector: the entries are
* assured to be sorted on the index */
index->entries.sorted = 1;
- return GIT_SUCCESS;
+ return 0;
}
static int is_index_extended(git_index *index)
{
unsigned int i, extended;
+ git_index_entry *entry;
extended = 0;
- for (i = 0; i < index->entries.length; ++i) {
- git_index_entry *entry;
- entry = git_vector_get(&index->entries, i);
+ git_vector_foreach(&index->entries, i, entry) {
entry->flags &= ~GIT_IDXENTRY_EXTENDED;
if (entry->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS) {
extended++;
entry->flags |= GIT_IDXENTRY_EXTENDED;
}
}
+
return extended;
}
@@ -845,8 +837,8 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry)
else
disk_size = short_entry_size(path_len);
- if (git_filebuf_reserve(file, &mem, disk_size) < GIT_SUCCESS)
- return GIT_ENOMEM;
+ if (git_filebuf_reserve(file, &mem, disk_size) < 0)
+ return -1;
ondisk = (struct entry_short *)mem;
@@ -888,7 +880,7 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry)
memcpy(path, entry->path, path_len);
- return GIT_SUCCESS;
+ return 0;
}
static int write_entries(git_index *index, git_filebuf *file)
@@ -898,16 +890,15 @@ static int write_entries(git_index *index, git_filebuf *file)
for (i = 0; i < index->entries.length; ++i) {
git_index_entry *entry;
entry = git_vector_get(&index->entries, i);
- if (write_disk_entry(file, entry) < GIT_SUCCESS)
- return GIT_ENOMEM;
+ if (write_disk_entry(file, entry) < 0)
+ return -1;
}
- return GIT_SUCCESS;
+ return 0;
}
static int write_index(git_index *index, git_filebuf *file)
{
- int error = GIT_SUCCESS;
git_oid hash_final;
struct index_header header;
@@ -922,11 +913,11 @@ static int write_index(git_index *index, git_filebuf *file)
header.version = htonl(is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER);
header.entry_count = htonl(index->entries.length);
- git_filebuf_write(file, &header, sizeof(struct index_header));
+ if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0)
+ return -1;
- error = write_entries(index, file);
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to write index");
+ if (write_entries(index, file) < 0)
+ return -1;
/* TODO: write extensions (tree cache) */
@@ -934,9 +925,7 @@ static int write_index(git_index *index, git_filebuf *file)
git_filebuf_hash(&hash_final, file);
/* write it at the end of the file */
- git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ);
-
- return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write index");
+ return git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ);
}
int git_index_entry_stage(const git_index_entry *entry)
@@ -946,36 +935,30 @@ int git_index_entry_stage(const git_index_entry *entry)
static int read_tree_cb(const char *root, git_tree_entry *tentry, void *data)
{
- int ret = GIT_SUCCESS;
git_index *index = data;
git_index_entry *entry = NULL;
git_buf path = GIT_BUF_INIT;
- if (entry_is_tree(tentry))
- goto exit;
+ if (git_tree_entry__is_tree(tentry))
+ return 0;
- ret = git_buf_joinpath(&path, root, tentry->filename);
- if (ret < GIT_SUCCESS)
- goto exit;
+ if (git_buf_joinpath(&path, root, tentry->filename) < 0)
+ return -1;
entry = git__calloc(1, sizeof(git_index_entry));
- if (!entry) {
- ret = GIT_ENOMEM;
- goto exit;
- }
+ GITERR_CHECK_ALLOC(entry);
entry->mode = tentry->attr;
entry->oid = tentry->oid;
entry->path = git_buf_detach(&path);
-
- ret = index_insert(index, entry, 0);
-
-exit:
git_buf_free(&path);
- if (ret < GIT_SUCCESS)
+ if (index_insert(index, entry, 0) < 0) {
index_entry_free(entry);
- return ret;
+ return -1;
+ }
+
+ return 0;
}
int git_index_read_tree(git_index *index, git_tree *tree)