Commit 7c7ff7d1 by Russell Belfer

Migrate index, oid, and utils to new errors

This includes a few cleanups that came up while converting
these files.

This commit introduces a could new git error classes, including
the catchall class: GITERR_INVALID which I'm using as the class
for invalid and out of range values which are detected at too low
a level of library to use a higher level classification.  For
example, an overflow error in parsing an integer or a bad letter
in parsing an OID string would generate an error in this class.
parent fd771427
......@@ -123,12 +123,14 @@ typedef struct {
typedef enum {
GITERR_NOMEMORY,
GITERR_OS,
GITERR_INVALID,
GITERR_REFERENCE,
GITERR_ZLIB,
GITERR_REPOSITORY,
GITERR_CONFIG,
GITERR_REGEX,
GITERR_ODB
GITERR_ODB,
GITERR_INDEX
} git_error_class;
/**
......
......@@ -108,10 +108,10 @@ mode_t git_futils_canonical_mode(mode_t raw_mode)
return S_IFREG | GIT_CANONICAL_PERMS(raw_mode);
else if (S_ISLNK(raw_mode))
return S_IFLNK;
else if (S_ISDIR(raw_mode))
return S_IFDIR;
else if (S_ISGITLINK(raw_mode))
return S_IFGITLINK;
else if (S_ISDIR(raw_mode))
return S_IFDIR;
else
return 0;
}
......
......@@ -135,19 +135,14 @@ 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) == true)
......@@ -215,7 +210,7 @@ void git_index_clear(git_index *index)
int git_index_read(git_index *index)
{
int error = GIT_SUCCESS, updated;
int error, updated;
git_buf buffer = GIT_BUF_INIT;
time_t mtime;
......@@ -224,27 +219,26 @@ int git_index_read(git_index *index)
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.ptr, buffer.size);
if (error == GIT_SUCCESS)
if (!error)
index->last_modified = mtime;
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,21 +311,17 @@ 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");
if (stage < 0 || stage > 3)
return git__throw(GIT_ERROR,
"Failed to initialize entry. Invalid stage %i", stage);
assert(stage >= 0 && stage <= 3);
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 ((error = git_path_lstat(full_path.ptr, &st)) < 0) {
......@@ -331,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)
......@@ -393,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);
......@@ -412,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);
......@@ -426,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;
......@@ -439,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)
......@@ -447,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)
......@@ -473,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)
......@@ -513,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;
......@@ -535,7 +507,8 @@ 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);
......@@ -549,69 +522,81 @@ const git_index_entry_unmerged *git_index_get_unmerged_bypath(git_index *index,
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)
......@@ -657,7 +642,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;
}
......@@ -682,15 +667,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)
......@@ -713,10 +698,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
......@@ -738,21 +723,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);
......@@ -764,23 +749,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) {
......@@ -790,43 +774,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;
}
......@@ -844,8 +828,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;
......@@ -887,7 +871,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)
......@@ -897,16 +881,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;
......@@ -921,11 +904,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) */
......@@ -933,9 +916,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)
......@@ -945,36 +926,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;
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)
......
......@@ -31,4 +31,6 @@ struct git_index {
git_vector unmerged;
};
extern void git_index__init_entry_from_stat(struct stat *st, git_index_entry *entry);
#endif
......@@ -395,7 +395,6 @@ static void workdir_iterator__free(git_iterator *self)
static int workdir_iterator__update_entry(workdir_iterator *wi)
{
int error;
git_path_with_stat *ps = git_vector_get(&wi->stack->entries, wi->stack->index);
git_buf_truncate(&wi->path, wi->root_len);
......@@ -412,24 +411,18 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
/* if there is an error processing the entry, treat as ignored */
wi->is_ignored = 1;
/* TODO: remove shared code for struct stat conversion with index.c */
wi->entry.ctime.seconds = (git_time_t)ps->st.st_ctime;
wi->entry.mtime.seconds = (git_time_t)ps->st.st_mtime;
wi->entry.dev = ps->st.st_rdev;
wi->entry.ino = ps->st.st_ino;
git_index__init_entry_from_stat(&ps->st, &wi->entry);
/* need different mode here to keep directories during iteration */
wi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
wi->entry.uid = ps->st.st_uid;
wi->entry.gid = ps->st.st_gid;
wi->entry.file_size = ps->st.st_size;
/* if this is a file type we don't handle, treat as ignored */
if (wi->entry.mode == 0)
return 0;
/* okay, we are far enough along to look up real ignore rule */
error = git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored);
if (error < 0)
return 0;
if (git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored) < 0)
return 0; /* if error, ignore it and ignore file */
/* detect submodules */
if (S_ISDIR(wi->entry.mode) &&
......
......@@ -13,13 +13,19 @@
static char to_hex[] = "0123456789abcdef";
static int oid_error_invalid(const char *msg)
{
giterr_set(GITERR_INVALID, "Unable to parse OID - %s", msg);
return -1;
}
int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
{
size_t p;
int v;
if (length < 4)
return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is too short");
return oid_error_invalid("input too short");
if (length > GIT_OID_HEXSZ)
length = GIT_OID_HEXSZ;
......@@ -29,7 +35,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
| git__fromhex(str[p + 1]);
if (v < 0)
return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash");
return oid_error_invalid("contains invalid characters");
out->id[p / 2] = (unsigned char)v;
}
......@@ -37,7 +43,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
if (length % 2) {
v = (git__fromhex(str[p + 0]) << 4);
if (v < 0)
return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash");
return oid_error_invalid("contains invalid characters");
out->id[p / 2] = (unsigned char)v;
p += 2;
......@@ -45,7 +51,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2);
return GIT_SUCCESS;
return 0;
}
int git_oid_fromstr(git_oid *out, const char *str)
......@@ -109,8 +115,9 @@ char *git_oid_to_string(char *out, size_t n, const git_oid *oid)
return out;
}
int git_oid__parse(git_oid *oid, const char **buffer_out,
const char *buffer_end, const char *header)
int git_oid__parse(
git_oid *oid, const char **buffer_out,
const char *buffer_end, const char *header)
{
const size_t sha_len = GIT_OID_HEXSZ;
const size_t header_len = strlen(header);
......@@ -118,20 +125,20 @@ int git_oid__parse(git_oid *oid, const char **buffer_out,
const char *buffer = *buffer_out;
if (buffer + (header_len + sha_len + 1) > buffer_end)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer too small");
return oid_error_invalid("input is too short");
if (memcmp(buffer, header, header_len) != 0)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer and header do not match");
return oid_error_invalid("did not match expected header");
if (buffer[header_len + sha_len] != '\n')
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer not terminated correctly");
return oid_error_invalid("not terminated correctly");
if (git_oid_fromstr(oid, buffer + header_len) < GIT_SUCCESS)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Failed to generate sha1");
if (git_oid_fromstr(oid, buffer + header_len) < 0)
return -1;
*buffer_out = buffer + (header_len + sha_len + 1);
return GIT_SUCCESS;
return 0;
}
void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid)
......@@ -182,12 +189,11 @@ int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, unsigned int len)
int git_oid_streq(const git_oid *a, const char *str)
{
git_oid id;
int error;
if ((error = git_oid_fromstr(&id, str)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to convert '%s' to oid.", str);
if (git_oid_fromstr(&id, str) < 0)
return -1;
return git_oid_cmp(a, &id) == 0 ? GIT_SUCCESS : GIT_ERROR;
return git_oid_cmp(a, &id) == 0 ? 0 : -1;
}
int git_oid_iszero(const git_oid *oid_a)
......@@ -216,15 +222,14 @@ struct git_oid_shorten {
static int resize_trie(git_oid_shorten *self, size_t new_size)
{
self->nodes = git__realloc(self->nodes, new_size * sizeof(trie_node));
if (self->nodes == NULL)
return GIT_ENOMEM;
GITERR_CHECK_ALLOC(self->nodes);
if (new_size > self->size) {
memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node));
}
self->size = new_size;
return GIT_SUCCESS;
return 0;
}
static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid)
......@@ -233,7 +238,7 @@ static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, co
node_index idx_leaf;
if (os->node_count >= os->size) {
if (resize_trie(os, os->size * 2) < GIT_SUCCESS)
if (resize_trie(os, os->size * 2) < 0)
return NULL;
}
......@@ -255,13 +260,11 @@ git_oid_shorten *git_oid_shorten_new(size_t min_length)
{
git_oid_shorten *os;
os = git__malloc(sizeof(git_oid_shorten));
os = git__calloc(1, sizeof(git_oid_shorten));
if (os == NULL)
return NULL;
memset(os, 0x0, sizeof(git_oid_shorten));
if (resize_trie(os, 16) < GIT_SUCCESS) {
if (resize_trie(os, 16) < 0) {
git__free(os);
return NULL;
}
......@@ -329,7 +332,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
node_index idx;
if (os->full)
return GIT_ENOMEM;
return -1;
if (text_oid == NULL)
return os->min_length;
......@@ -341,8 +344,10 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
int c = git__fromhex(text_oid[i]);
trie_node *node;
if (c == -1)
return git__throw(GIT_ENOTOID, "Failed to shorten OID. Invalid hex value");
if (c == -1) {
giterr_set(GITERR_INVALID, "Unable to shorten OID - invalid hex value");
return -1;
}
node = &os->nodes[idx];
......@@ -353,13 +358,12 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
node->tail = NULL;
node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]);
if (node == NULL)
return GIT_ENOMEM;
GITERR_CHECK_ALLOC(node);
}
if (node->children[c] == 0) {
if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL)
return GIT_ENOMEM;
return -1;
break;
}
......
......@@ -112,34 +112,40 @@ int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int ba
}
Return:
if (ndig == 0)
return git__throw(GIT_ENOTNUM, "Failed to convert string to long. Not a number");
if (ndig == 0) {
giterr_set(GITERR_INVALID, "Failed to convert string to long. Not a number");
return -1;
}
if (endptr)
*endptr = p;
if (ovfl)
return git__throw(GIT_EOVERFLOW, "Failed to convert string to long. Overflow error");
if (ovfl) {
giterr_set(GITERR_INVALID, "Failed to convert string to long. Overflow error");
return -1;
}
*result = neg ? -n : n;
return GIT_SUCCESS;
return 0;
}
int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base)
{
int error = GIT_SUCCESS;
int error;
int32_t tmp_int;
int64_t tmp_long;
if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < GIT_SUCCESS)
if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < 0)
return error;
tmp_int = tmp_long & 0xFFFFFFFF;
if (tmp_int != tmp_long)
return git__throw(GIT_EOVERFLOW, "Failed to convert. '%s' is too large", nptr);
if (tmp_int != tmp_long) {
giterr_set(GITERR_INVALID, "Failed to convert. '%s' is too large", nptr);
return -1;
}
*result = tmp_int;
return error;
}
......
......@@ -10,9 +10,9 @@ void test_core_oid__initialize(void)
void test_core_oid__streq(void)
{
cl_assert(git_oid_streq(&id, str_oid) == GIT_SUCCESS);
cl_assert(git_oid_streq(&id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") == GIT_ERROR);
cl_assert(git_oid_streq(&id, str_oid) == 0);
cl_assert(git_oid_streq(&id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") == -1);
cl_assert(git_oid_streq(&id, "deadbeef") == GIT_ENOTOID);
cl_assert(git_oid_streq(&id, "I'm not an oid.... :)") == GIT_ENOTOID);
cl_assert(git_oid_streq(&id, "deadbeef") == -1);
cl_assert(git_oid_streq(&id, "I'm not an oid.... :)") == -1);
}
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