Commit c3a20d5cab7d17cf55e769c39a3d50882e3d341d

Vicent Marti 2010-11-14T22:11:46

Add support for 'index add' Actually add files to the index by creating their corresponding blob and storing it on the repository, then getting the hash and updating the index file. Signed-off-by: Vicent Marti <tanoku@gmail.com>

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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
diff --git a/src/git/common.h b/src/git/common.h
index 82f08ac..cbc77e9 100644
--- a/src/git/common.h
+++ b/src/git/common.h
@@ -105,6 +105,9 @@
 /** The queried object is currently busy */
 #define GIT_EBUSY (GIT_ERROR - 13)
 
+/** The index file is not backed up by an existing repository */
+#define GIT_EBAREINDEX (GIT_ERROR -14)
+
 
 GIT_BEGIN_DECL
 
diff --git a/src/git/index.h b/src/git/index.h
index 8220e62..6cf270b 100644
--- a/src/git/index.h
+++ b/src/git/index.h
@@ -47,21 +47,28 @@ typedef struct git_index_entry {
 
 /**
  * Create a new Git index object as a memory representation
- * of the Git index file in 'index_path'.
+ * of the Git index file in 'index_path', without a repository
+ * to back it.
  *
- * The argument 'working_dir' is the root path of the indexed
- * files in the index and is used to calculate the relative path
- * when inserting new entries from existing files on disk.
- *
- * If 'working _dir' is NULL (e.g for bare repositories), the
- * methods working on on-disk files will fail.
+ * Since there is no ODB behind this index, any Index methods
+ * which rely on the ODB (e.g. index_add) will fail with the
+ * GIT_EBAREINDEX error code.
  *
  * @param index the pointer for the new index
  * @param index_path the path to the index file in disk
- * @param working_dir working dir for the git repository
  * @return 0 on success; error code otherwise
  */
-GIT_EXTERN(int) git_index_open(git_index **index, const char *index_path, const char *working_dir);
+GIT_EXTERN(int) git_index_open_bare(git_index **index, const char *index_path);
+
+/**
+ * Open the Index inside the git repository pointed
+ * by 'repo'.
+ *
+ * @param repo the git repo which owns the index
+ * @param index_path the path to the index file in disk
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_index_open_inrepo(git_index **index, git_repository *repo);
 
 /**
  * Clear the contents (all the entries) of an index object.
@@ -108,14 +115,14 @@ GIT_EXTERN(int) git_index_write(git_index *index);
 GIT_EXTERN(int) git_index_find(git_index *index, const char *path);
 
 /**
- * Add a new empty entry to the index with a given path.
+ * Add or update an index entry from a file in disk.
  *
  * @param index an existing index object
- * @param path filename pointed to by the entry
+ * @param path filename to add
  * @param stage stage for the entry
  * @return 0 on success, otherwise an error code
  */
-GIT_EXTERN(int) git_index_add_bypath(git_index *index, const char *path, int stage);
+GIT_EXTERN(int) git_index_add(git_index *index, const char *path, int stage);
 
 /**
  * Remove an entry from the index 
@@ -127,13 +134,17 @@ GIT_EXTERN(int) git_index_add_bypath(git_index *index, const char *path, int sta
 GIT_EXTERN(int) git_index_remove(git_index *index, int position);
 
 /**
- * Add a new entry to the index 
+ * Insert an entry into the index.
+ * A full copy (including the 'path' string) of the given
+ * 'source_entry' will be inserted on the index; if the index
+ * already contains an entry for the same path, the entry
+ * will be updated.
  *
  * @param index an existing index object
  * @param source_entry new entry object
  * @return 0 on success, otherwise an error code
  */
-GIT_EXTERN(int) git_index_add(git_index *index, const git_index_entry *source_entry);
+GIT_EXTERN(int) git_index_insert(git_index *index, const git_index_entry *source_entry);
 
 /**
  * Get a pointer to one of the entries in the index
diff --git a/src/index.c b/src/index.c
index ed85f91..accb244 100644
--- a/src/index.c
+++ b/src/index.c
@@ -26,9 +26,11 @@
 #include <stddef.h>
 
 #include "common.h"
+#include "repository.h"
 #include "index.h"
 #include "hash.h"
 #include "git/odb.h"
+#include "git/blob.h"
 
 #define entry_padding(type, len) (8 - ((offsetof(type, path) + (len)) & 0x7))
 #define short_entry_padding(len) entry_padding(struct entry_short, len)
@@ -97,7 +99,7 @@ static int read_tree(git_index *index, const char *buffer, size_t buffer_size);
 static git_index_tree *read_tree_internal(const char **, const char *, git_index_tree *);
 
 
-int git_index_open(git_index **index_out, const char *index_path, const char *work_dir)
+static int index_initialize(git_index **index_out, git_repository *owner, const char *index_path)
 {
 	git_index *index;
 
@@ -115,8 +117,7 @@ int git_index_open(git_index **index_out, const char *index_path, const char *wo
 		return GIT_ENOMEM;
 	}
 
-	if (work_dir != NULL)
-		index->working_path = git__strdup(work_dir);
+	index->repository = owner;
 
 	/* Check if index file is stored on disk already */
 	if (gitfo_exists(index->index_file_path) == 0)
@@ -126,6 +127,16 @@ int git_index_open(git_index **index_out, const char *index_path, const char *wo
 	return GIT_SUCCESS;
 }
 
+int git_index_open_bare(git_index **index_out, const char *index_path)
+{
+	return index_initialize(index_out, NULL, index_path);
+}
+
+int git_index_open_inrepo(git_index **index_out, git_repository *repo)
+{
+	return index_initialize(index_out, repo, repo->path_index);
+}
+
 void git_index_clear(git_index *index)
 {
 	unsigned int i;
@@ -233,28 +244,49 @@ git_index_entry *git_index_get(git_index *index, int n)
 	return (n >= 0 && (unsigned int)n < index->entry_count) ? &index->entries[n] : NULL;
 }
 
-int git_index_add_bypath(git_index *index, const char *filename, int stage)
+int git_index_add(git_index *index, const char *rel_path, int stage)
 {
 	git_index_entry entry;
-	size_t path_length;
+	char full_path[GIT_PATH_MAX];
+	struct stat st;
+	int error;
 
-	memset(&entry, 0x0, sizeof(git_index_entry));
+	if (index->repository == NULL)
+		return GIT_EBAREINDEX;
 
-	path_length = strlen(filename);
+	strcpy(full_path, index->repository->path_workdir);
+	strcat(full_path, rel_path);
 
-	if (path_length < GIT_IDXENTRY_NAMEMASK)
-		entry.flags |= path_length;
-	else
-		entry.flags |= GIT_IDXENTRY_NAMEMASK;;
+	if (gitfo_exists(full_path) < 0)
+		return GIT_ENOTFOUND;
+
+	if (gitfo_stat(full_path, &st) < 0)
+		return GIT_EOSERR;
 
 	if (stage < 0 || stage > 3)
 		return GIT_ERROR;
 
-	entry.flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
+	memset(&entry, 0x0, sizeof(git_index_entry));
 
-	entry.path = git__strdup(filename);
+	entry.ctime.seconds = st.st_ctime;
+	entry.mtime.seconds = 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 = st.st_mode;
+	entry.uid = st.st_uid;
+	entry.gid = st.st_gid;
+	entry.file_size = st.st_size;
+
+	/* write the blob to disk and get the oid */
+	if ((error = git_blob_writefile(&entry.oid, index->repository, full_path)) < 0)
+		return error;
 
-	return git_index_add(index, &entry);
+	entry.flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
+	entry.path = (char *)rel_path; /* do not duplicate; index_insert already does this */
+
+	return git_index_insert(index, &entry);
 }
 
 void git_index__sort(git_index *index)
@@ -281,31 +313,66 @@ void git_index__sort(git_index *index)
 	index->sorted = 1;
 }
 
-int git_index_add(git_index *index, const git_index_entry *source_entry)
+int git_index_insert(git_index *index, const git_index_entry *source_entry)
 {
 	git_index_entry *offset;
+	size_t path_length;
+	int position;
 
-	/* Resize the entries array */
-	if (index->entry_count + 1 > index->entries_size) {
-		git_index_entry *new_entries;
-		size_t new_size;
+	assert(index && source_entry);
 
-		new_size = (unsigned int)(index->entries_size * 1.5f);
-		if ((new_entries = git__malloc(new_size * sizeof(git_index_entry))) == NULL)
-			return GIT_ENOMEM;
+	if (source_entry->path == NULL)
+		return GIT_EMISSINGOBJDATA;
 
-		memcpy(new_entries, index->entries, index->entry_count * sizeof(git_index_entry));
-		free(index->entries);
+	position = git_index_find(index, source_entry->path);
 
-		index->entries_size = new_size;
-		index->entries = new_entries;
-	}
+	if (position == GIT_ENOTFOUND) {
+
+		/* Resize the entries array */
+		if (index->entry_count + 1 > index->entries_size) {
+			git_index_entry *new_entries;
+			size_t new_size;
+
+			new_size = (unsigned int)(index->entries_size * 1.5f);
+			if ((new_entries = git__malloc(new_size * sizeof(git_index_entry))) == NULL)
+				return GIT_ENOMEM;
+
+			memcpy(new_entries, index->entries, index->entry_count * sizeof(git_index_entry));
+			free(index->entries);
+
+			index->entries_size = new_size;
+			index->entries = new_entries;
+		}
 
-	offset = &index->entries[index->entry_count];
-	index->entry_count++;
+		offset = &index->entries[index->entry_count];
+		index->entry_count++;
+		index->sorted = 0;
+	
+	} else {
+		offset = &index->entries[position];
+		free(offset->path);
+	}
 
 	memcpy(offset, source_entry, sizeof(git_index_entry));
-	index->sorted = 0;
+
+	/* duplicate the path string so we own it */
+	offset->path = git__strdup(source_entry->path);
+	if (offset->path == NULL)
+		return GIT_ENOMEM;
+
+	/* make sure that the path length flag is correct */
+	path_length = strlen(offset->path);
+
+	offset->flags &= ~GIT_IDXENTRY_NAMEMASK;
+
+	if (path_length < GIT_IDXENTRY_NAMEMASK)
+		offset->flags |= path_length & GIT_IDXENTRY_NAMEMASK;
+	else
+		offset->flags |= GIT_IDXENTRY_NAMEMASK;;
+
+	/* TODO: force the extended index entry flag? */
+
+	assert(offset->path);
 
 	return GIT_SUCCESS;
 }
@@ -500,6 +567,7 @@ static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffe
 		return 0;
 
 	dest->path = git__strdup(path_ptr);
+	assert(dest->path);
 
 	return entry_size;
 }
@@ -602,6 +670,9 @@ int git_index__parse(git_index *index, const char *buffer, size_t buffer_size)
 		seek_forward(entry_size);
 	}
 
+	if (i != index->entry_count)
+		return GIT_EOBJCORRUPTED;
+
 	/* There's still space for some extensions! */
 	while (buffer_size > INDEX_FOOTER_SIZE) {
 		size_t extension_size;
diff --git a/src/index.h b/src/index.h
index 6a3c11e..818f1f1 100644
--- a/src/index.h
+++ b/src/index.h
@@ -26,9 +26,8 @@ struct git_index_tree {
 typedef struct git_index_tree git_index_tree;
 
 struct git_index {
-
+	git_repository *repository;
 	char *index_file_path;
-	char *working_path;
 
 	time_t last_modified;
 
diff --git a/src/repository.c b/src/repository.c
index 4beb3e0..ef51b08 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -196,7 +196,7 @@ void git_repository_free(git_repository *repo)
 git_index *git_repository_index(git_repository *repo)
 {
 	if (repo->index == NULL) {
-		if (git_index_open(&repo->index, repo->path_index, repo->path_workdir) < 0)
+		if (git_index_open_inrepo(&repo->index, repo) < 0)
 			return NULL;
 
 		assert(repo->index && repo->index->on_disk);
@@ -298,8 +298,6 @@ static int write_back(git_object *object)
 
 	object->source.raw.len = object->source.written_bytes;
 
-	git_obj_hash(&new_id, &object->source.raw);
-
 	if ((error = git_odb_write(&new_id, object->repo->db, &object->source.raw)) < 0)
 		return error;
 
diff --git a/tests/t0601-read.c b/tests/t0601-read.c
index 133a266..6156745 100644
--- a/tests/t0601-read.c
+++ b/tests/t0601-read.c
@@ -29,7 +29,7 @@ struct test_entry TEST_ENTRIES[] = {
 BEGIN_TEST(index_loadempty_test)
 	git_index *index;
 
-	must_pass(git_index_open(&index, "in-memory-index", NULL));
+	must_pass(git_index_open_bare(&index, "in-memory-index"));
 	must_be_true(index->on_disk == 0);
 
 	must_pass(git_index_read(index));
@@ -45,7 +45,7 @@ BEGIN_TEST(index_load_test)
 	git_index *index;
 	unsigned int i;
 
-	must_pass(git_index_open(&index, TEST_INDEX_PATH, NULL));
+	must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
 	must_be_true(index->on_disk);
 
 	must_pass(git_index_read(index));
@@ -68,7 +68,7 @@ END_TEST
 BEGIN_TEST(index2_load_test)
 	git_index *index;
 
-	must_pass(git_index_open(&index, TEST_INDEX2_PATH, NULL));
+	must_pass(git_index_open_bare(&index, TEST_INDEX2_PATH));
 	must_be_true(index->on_disk);
 
 	must_pass(git_index_read(index));
@@ -85,7 +85,7 @@ BEGIN_TEST(index_find_test)
 	git_index *index;
 	unsigned int i;
 
-	must_pass(git_index_open(&index, TEST_INDEX_PATH, NULL));
+	must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
 	must_pass(git_index_read(index));
 
 	for (i = 0; i < ARRAY_SIZE(TEST_ENTRIES); ++i) {
@@ -100,7 +100,7 @@ BEGIN_TEST(index_findempty_test)
 	git_index *index;
 	unsigned int i;
 
-	must_pass(git_index_open(&index, "fake-index", NULL));
+	must_pass(git_index_open_bare(&index, "fake-index"));
 
 	for (i = 0; i < ARRAY_SIZE(TEST_ENTRIES); ++i) {
 		int idx = git_index_find(index, TEST_ENTRIES[i].path);
diff --git a/tests/t0602-write.c b/tests/t0602-write.c
index 415183f..6463773 100644
--- a/tests/t0602-write.c
+++ b/tests/t0602-write.c
@@ -35,7 +35,7 @@ BEGIN_TEST(index_load_test)
 	git_index *index;
 	git_filelock out_file;
 
-	must_pass(git_index_open(&index, TEST_INDEX_PATH, NULL));
+	must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
 	must_pass(git_index_read(index));
 	must_be_true(index->on_disk);
 
diff --git a/tests/t0603-sort.c b/tests/t0603-sort.c
index 0ee14d7..d47c93c 100644
--- a/tests/t0603-sort.c
+++ b/tests/t0603-sort.c
@@ -36,7 +36,7 @@ BEGIN_TEST(index_sort_test)
 	git_index *index;
 	unsigned int i;
 
-	must_pass(git_index_open(&index, TEST_INDEX_PATH, NULL));
+	must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
 	must_pass(git_index_read(index));
 
 	randomize_entries(index);
@@ -54,7 +54,7 @@ END_TEST
 BEGIN_TEST(index_sort_empty_test)
 	git_index *index;
 
-	must_pass(git_index_open(&index, "fake-index", NULL));
+	must_pass(git_index_open_bare(&index, "fake-index"));
 
 	git_index__sort(index);
 	must_be_true(index->sorted);