diff options
author | Damien PROFETA <damien.profeta@amadeus.com> | 2015-02-05 11:40:16 +0100 |
---|---|---|
committer | Damien PROFETA <damien.profeta@amadeus.com> | 2015-02-25 10:24:13 +0100 |
commit | a275fbc0f7773e6c5888ae7ce766e7a9eb3a5e65 (patch) | |
tree | 34fced6b3b392f03f12cadc625fc4fd74f941043 /src | |
parent | 366e53d3da72805a3db1a1e77d4666416fcb5a93 (diff) | |
download | libgit2-a275fbc0f7773e6c5888ae7ce766e7a9eb3a5e65.tar.gz |
Add API to add a memory buffer to an index
git_index_add_frombuffer enables now to store a memory buffer in the odb
and to store an entry in the index directly if the index is attached to a
repository.
Diffstat (limited to 'src')
-rw-r--r-- | src/index.c | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/src/index.c b/src/index.c index c122cdf6d..9880e8fe4 100644 --- a/src/index.c +++ b/src/index.c @@ -1082,6 +1082,58 @@ static int index_conflict_to_reuc(git_index *index, const char *path) return ret; } +static bool valid_filemode(const int filemode) +{ + return (filemode == GIT_FILEMODE_BLOB || + filemode == GIT_FILEMODE_BLOB_EXECUTABLE || + filemode == GIT_FILEMODE_LINK || + filemode == GIT_FILEMODE_COMMIT); +} + +int git_index_add_frombuffer( + git_index *index, git_index_entry *source_entry, + const void *buffer, size_t len) +{ + git_index_entry *entry = NULL; + int error = 0; + git_oid id; + + assert(index && source_entry->path); + + if (INDEX_OWNER(index) == NULL) + return create_index_error(-1, + "Could not initialize index entry. " + "Index is not backed up by an existing repository."); + + if (!valid_filemode(source_entry->mode)) { + giterr_set(GITERR_INDEX, "invalid filemode"); + return -1; + } + + if (index_entry_dup(&entry, INDEX_OWNER(index), source_entry) < 0) + return -1; + + error = git_blob_create_frombuffer(&id, INDEX_OWNER(index), buffer, len); + if (error < 0) { + index_entry_free(entry); + return error; + } + + git_oid_cpy(&entry->id, &id); + entry->file_size = len; + + if ((error = index_insert(index, &entry, 1)) < 0) + return error; + + /* Adding implies conflict was resolved, move conflict entries to REUC */ + if ((error = index_conflict_to_reuc(index, entry->path)) < 0 && error != GIT_ENOTFOUND) + return error; + + git_tree_cache_invalidate_path(index->tree, entry->path); + return 0; +} + + int git_index_add_bypath(git_index *index, const char *path) { git_index_entry *entry = NULL; @@ -1116,14 +1168,6 @@ int git_index_remove_bypath(git_index *index, const char *path) return 0; } -static bool valid_filemode(const int filemode) -{ - return (filemode == GIT_FILEMODE_BLOB || - filemode == GIT_FILEMODE_BLOB_EXECUTABLE || - filemode == GIT_FILEMODE_LINK || - filemode == GIT_FILEMODE_COMMIT); -} - int git_index_add(git_index *index, const git_index_entry *source_entry) { |