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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bffcb25..fd69708 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,9 @@ v0.22 + 1
via `git_config_parse_path()` and `git_config_get_path()`
respectively.
+* `git_index_add_frombuffer()` can now create a blob from memory
+ buffer and add it to the index which is attached to a repository.
+
### API removals
### Breaking API changes
diff --git a/include/git2/index.h b/include/git2/index.h
index 4382c2f..1feeb6f 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -457,6 +457,38 @@ GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry);
GIT_EXTERN(int) git_index_add_bypath(git_index *index, const char *path);
/**
+ * Add or update an index entry from a buffer in memory
+ *
+ * This method will create a blob in the repository that owns the
+ * index and then add the index entry to the index. The `path` of the
+ * entry represents the position of the blob relative to the
+ * repository's root folder.
+ *
+ * If a previous index entry exists that has the same path as the
+ * given 'entry', it will be replaced. Otherwise, the 'entry' will be
+ * added. The `id` and the `file_size` of the 'entry' are updated with the
+ * real value of the blob.
+ *
+ * This forces the file to be added to the index, not looking
+ * at gitignore rules. Those rules can be evaluated through
+ * the git_status APIs (in status.h) before calling this.
+ *
+ * If this file currently is the result of a merge conflict, this
+ * file will no longer be marked as conflicting. The data about
+ * the conflict will be moved to the "resolve undo" (REUC) section.
+ *
+ * @param index an existing index object
+ * @param entry filename to add
+ * @param buffer data to be written into the blob
+ * @param len length of the data
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_index_add_frombuffer(
+ git_index *index,
+ git_index_entry *entry,
+ const void *buffer, size_t len);
+
+/**
* Remove an index entry corresponding to a file on disk
*
* The file `path` must be relative to the repository's
diff --git a/src/index.c b/src/index.c
index c122cdf..9880e8f 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)
{
diff --git a/tests/index/tests.c b/tests/index/tests.c
index 4cf7051..3c8060a 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -253,6 +253,128 @@ void test_index_tests__add(void)
git_repository_free(repo);
}
+void test_index_tests__add_frombuffer(void)
+{
+ git_index *index;
+ git_repository *repo;
+ git_index_entry entry;
+ const git_index_entry *returned_entry;
+
+ git_oid id1;
+ git_blob *blob;
+
+ const char *content = "hey there\n";
+
+ cl_set_cleanup(&cleanup_myrepo, NULL);
+
+ /* Intialize a new repository */
+ cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
+
+ /* Ensure we're the only guy in the room */
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_assert(git_index_entrycount(index) == 0);
+
+ /* Store the expected hash of the file/blob
+ * This has been generated by executing the following
+ * $ echo "hey there" | git hash-object --stdin
+ */
+ cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
+
+ /* Add the new file to the index */
+ memset(&entry, 0x0, sizeof(git_index_entry));
+ entry.mode = GIT_FILEMODE_BLOB;
+ entry.path = "test.txt";
+ cl_git_pass(git_index_add_frombuffer(index, &entry,
+ content, strlen(content)));
+
+ /* Wow... it worked! */
+ cl_assert(git_index_entrycount(index) == 1);
+ returned_entry = git_index_get_byindex(index, 0);
+
+ /* And the built-in hashing mechanism worked as expected */
+ cl_assert_equal_oid(&id1, &returned_entry->id);
+ /* And mode is the one asked */
+ cl_assert_equal_i(GIT_FILEMODE_BLOB, returned_entry->mode);
+
+ /* Test access by path instead of index */
+ cl_assert((returned_entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
+ cl_assert_equal_oid(&id1, &returned_entry->id);
+
+ /* Test the blob is in the repository */
+ cl_git_pass(git_blob_lookup(&blob, repo, &id1));
+ cl_assert_equal_s(
+ content, git_blob_rawcontent(blob));
+ git_blob_free(blob);
+
+ git_index_free(index);
+ git_repository_free(repo);
+}
+
+void test_index_tests__add_frombuffer_reset_entry(void)
+{
+ git_index *index;
+ git_repository *repo;
+ git_index_entry entry;
+ const git_index_entry *returned_entry;
+ git_filebuf file = GIT_FILEBUF_INIT;
+
+ git_oid id1;
+ git_blob *blob;
+ const char *old_content = "here\n";
+ const char *content = "hey there\n";
+
+ cl_set_cleanup(&cleanup_myrepo, NULL);
+
+ /* Intialize a new repository */
+ cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777));
+ cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0, 0666));
+ cl_git_pass(git_filebuf_write(&file, old_content, strlen(old_content)));
+ cl_git_pass(git_filebuf_commit(&file));
+
+ /* Store the expected hash of the file/blob
+ * This has been generated by executing the following
+ * $ echo "hey there" | git hash-object --stdin
+ */
+ cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
+
+ cl_git_pass(git_index_add_bypath(index, "test.txt"));
+
+ /* Add the new file to the index */
+ memset(&entry, 0x0, sizeof(git_index_entry));
+ entry.mode = GIT_FILEMODE_BLOB;
+ entry.path = "test.txt";
+ cl_git_pass(git_index_add_frombuffer(index, &entry,
+ content, strlen(content)));
+
+ /* Wow... it worked! */
+ cl_assert(git_index_entrycount(index) == 1);
+ returned_entry = git_index_get_byindex(index, 0);
+
+ /* And the built-in hashing mechanism worked as expected */
+ cl_assert_equal_oid(&id1, &returned_entry->id);
+ /* And mode is the one asked */
+ cl_assert_equal_i(GIT_FILEMODE_BLOB, returned_entry->mode);
+
+ /* Test access by path instead of index */
+ cl_assert((returned_entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
+ cl_assert_equal_oid(&id1, &returned_entry->id);
+ cl_assert_equal_i(0, returned_entry->dev);
+ cl_assert_equal_i(0, returned_entry->ino);
+ cl_assert_equal_i(0, returned_entry->uid);
+ cl_assert_equal_i(0, returned_entry->uid);
+ cl_assert_equal_i(10, returned_entry->file_size);
+
+ /* Test the blob is in the repository */
+ cl_git_pass(git_blob_lookup(&blob, repo, &id1));
+ cl_assert_equal_s(content, git_blob_rawcontent(blob));
+ git_blob_free(blob);
+
+ git_index_free(index);
+ git_repository_free(repo);
+}
+
static void cleanup_1397(void *opaque)
{
GIT_UNUSED(opaque);