Commit 390a3c81410a219b13aa6f333949c803524c8bd2

Russell Belfer 2013-02-11T11:44:00

Merge pull request #1190 from nulltoken/topic/reset-paths reset: Allow the selective reset of pathspecs

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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
diff --git a/include/git2/index.h b/include/git2/index.h
index 9f9d144..da95ee9 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -400,13 +400,14 @@ GIT_EXTERN(int) git_index_add_bypath(git_index *index, const char *path);
 GIT_EXTERN(int) git_index_remove_bypath(git_index *index, const char *path);
 
 /**
- * Find the first index of any entries which point to given
+ * Find the first position of any entries which point to given
  * path in the Git index.
  *
  * @param at_pos the address to which the position of the index entry is written (optional)
  * @param index an existing index object
  * @param path path to search
- * @return 0 if found, < 0 otherwise (GIT_ENOTFOUND)
+ * @return a zero-based position in the index if found;
+ * GIT_ENOTFOUND otherwise
  */
 GIT_EXTERN(int) git_index_find(size_t *at_pos, git_index *index, const char *path);
 
diff --git a/include/git2/reset.h b/include/git2/reset.h
index 00d25df..c7c9519 100644
--- a/include/git2/reset.h
+++ b/include/git2/reset.h
@@ -28,7 +28,7 @@ typedef enum {
  * Sets the current head to the specified commit oid and optionally
  * resets the index and working tree to match.
  *
- * SOFT reset means the head will be moved to the commit.
+ * SOFT reset means the Head will be moved to the commit.
  *
  * MIXED reset will trigger a SOFT reset, plus the index will be replaced
  * with the content of the commit tree.
@@ -41,18 +41,41 @@ typedef enum {
  *
  * @param repo Repository where to perform the reset operation.
  *
- * @param target Object to which the Head should be moved to. This object
+ * @param target Committish to which the Head should be moved to. This object
  * must belong to the given `repo` and can either be a git_commit or a
  * git_tag. When a git_tag is being passed, it should be dereferencable
  * to a git_commit which oid will be used as the target of the branch.
  *
  * @param reset_type Kind of reset operation to perform.
  *
- * @return 0 on success or an error code < 0
+ * @return 0 on success or an error code
  */
 GIT_EXTERN(int) git_reset(
 	git_repository *repo, git_object *target, git_reset_t reset_type);
 
+/**
+ * Updates some entries in the index from the target commit tree.
+ *
+ * The scope of the updated entries is determined by the paths
+ * being passed in the `pathspec` parameters.
+ *
+ * Passing a NULL `target` will result in removing
+ * entries in the index matching the provided pathspecs.
+ *
+ * @param repo Repository where to perform the reset operation.
+ *
+ * @param target The committish which content will be used to reset the content
+ * of the index.
+ *
+ * @param pathspecs List of pathspecs to operate on.
+ *
+ * @return 0 on success or an error code < 0
+ */
+GIT_EXTERN(int) git_reset_default(
+    git_repository *repo,
+    git_object *target,
+    git_strarray* pathspecs);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/src/branch.c b/src/branch.c
index 936947a..11ecbe9 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -429,7 +429,7 @@ int git_branch_tracking_name(
 	if (tracking_branch_name_out)
 		git_buf_copy_cstr(tracking_branch_name_out, buffer_size, &buf);
 
-	error = buf.size + 1;
+	error = (int)buf.size + 1;
 
 cleanup:
 	git_buf_free(&buf);
diff --git a/src/diff_output.c b/src/diff_output.c
index 4f1064b..c2c2591 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -1603,8 +1603,8 @@ int git_diff_patch_get_line_in_hunk(
 	if (line_origin) *line_origin = line->origin;
 	if (content) *content = line->ptr;
 	if (content_len) *content_len = line->len;
-	if (old_lineno) *old_lineno = line->oldno;
-	if (new_lineno) *new_lineno = line->newno;
+	if (old_lineno) *old_lineno = (int)line->oldno;
+	if (new_lineno) *new_lineno = (int)line->newno;
 
 	return 0;
 
diff --git a/src/diff_output.h b/src/diff_output.h
index ae1a491..0833556 100644
--- a/src/diff_output.h
+++ b/src/diff_output.h
@@ -42,7 +42,7 @@ typedef struct diff_patch_line diff_patch_line;
 struct diff_patch_line {
 	const char *ptr;
 	size_t len;
-	int lines, oldno, newno;
+	size_t lines, oldno, newno;
 	char origin;
 };
 
diff --git a/src/index.c b/src/index.c
index f5bf954..25156d0 100644
--- a/src/index.c
+++ b/src/index.c
@@ -929,7 +929,7 @@ int git_index_conflict_add(git_index *index,
 			goto on_error;
 	}
 
-    return 0;
+	return 0;
 
 on_error:
 	for (i = 0; i < 3; i++) {
diff --git a/src/reset.c b/src/reset.c
index b637e37..700aac8 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -9,6 +9,7 @@
 #include "commit.h"
 #include "tag.h"
 #include "merge.h"
+#include "diff.h"
 #include "git2/reset.h"
 #include "git2/checkout.h"
 #include "git2/merge.h"
@@ -54,6 +55,79 @@ cleanup:
 	return error;
 }
 
+int git_reset_default(
+	git_repository *repo,
+	git_object *target,
+	git_strarray* pathspecs)
+{
+	git_object *commit = NULL;
+	git_tree *tree = NULL;
+	git_diff_list *diff = NULL;
+	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+	size_t i;
+	git_diff_delta *delta;
+	git_index_entry entry;
+	int error;
+	git_index *index = NULL;
+
+	assert(pathspecs != NULL && pathspecs->count > 0); 
+
+	memset(&entry, 0, sizeof(git_index_entry));
+
+	if ((error = git_repository_index(&index, repo)) < 0)
+		goto cleanup;
+
+	if (target) {
+		if (git_object_owner(target) != repo) {
+			giterr_set(GITERR_OBJECT,
+				"%s_default - The given target does not belong to this repository.", ERROR_MSG);
+			return -1;
+		}
+
+		if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0 ||
+			(error = git_commit_tree(&tree, (git_commit *)commit)) < 0)
+			goto cleanup;
+	}
+
+	opts.pathspec = *pathspecs;
+	opts.flags = GIT_DIFF_REVERSE;
+
+	if ((error = git_diff_tree_to_index(
+		&diff, repo, tree, index, &opts)) < 0)
+			goto cleanup;
+
+	git_vector_foreach(&diff->deltas, i, delta) {
+		if ((error = git_index_conflict_remove(index, delta->old_file.path)) < 0)
+			goto cleanup;
+
+		assert(delta->status == GIT_DELTA_ADDED ||
+			delta->status == GIT_DELTA_MODIFIED ||
+			delta->status == GIT_DELTA_DELETED);
+
+		if (delta->status == GIT_DELTA_DELETED) {
+			if ((error = git_index_remove(index, delta->old_file.path, 0)) < 0)
+				goto cleanup;
+		} else {
+			entry.mode = delta->new_file.mode;
+			git_oid_cpy(&entry.oid, &delta->new_file.oid);
+			entry.path = (char *)delta->new_file.path;
+
+			if ((error = git_index_add(index, &entry)) < 0)
+				goto cleanup;
+		}
+	}
+
+	error = git_index_write(index);
+
+cleanup:
+	git_object_free(commit);
+	git_tree_free(tree);
+	git_index_free(index);
+	git_diff_list_free(diff);
+
+	return error;
+}
+
 int git_reset(
 	git_repository *repo,
 	git_object *target,
diff --git a/src/tree.c b/src/tree.c
index e05105a..59bd92a 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -355,7 +355,7 @@ size_t git_tree_entrycount(const git_tree *tree)
 unsigned int git_treebuilder_entrycount(git_treebuilder *bld)
 {
 	assert(bld);
-	return bld->entries.length;
+	return (int)bld->entries.length;
 }
 
 static int tree_error(const char *str, const char *path)
diff --git a/tests-clar/checkout/crlf.c b/tests-clar/checkout/crlf.c
index 856d32b..38c0080 100644
--- a/tests-clar/checkout/crlf.c
+++ b/tests-clar/checkout/crlf.c
@@ -16,12 +16,7 @@ static git_repository *g_repo;
 
 void test_checkout_crlf__initialize(void)
 {
-	git_tree *tree;
-
 	g_repo = cl_git_sandbox_init("crlf");
-
-	cl_git_pass(git_repository_head_tree(&tree, g_repo));
-	git_tree_free(tree);
 }
 
 void test_checkout_crlf__cleanup(void)
diff --git a/tests-clar/clone/empty.c b/tests-clar/clone/empty.c
index 4e53557..e611bc2 100644
--- a/tests-clar/clone/empty.c
+++ b/tests-clar/clone/empty.c
@@ -46,7 +46,7 @@ void test_clone_empty__can_clone_an_empty_local_repo_barely(void)
 	cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, local_name));
 
 	/* ...one can still retrieve the name of the remote tracking reference */
-	cl_assert_equal_i(strlen("refs/remotes/origin/master") + 1, 
+	cl_assert_equal_i((int)strlen("refs/remotes/origin/master") + 1U, 
 		git_branch_tracking_name(tracking_name, 1024, g_repo_cloned, local_name));
 }
 
diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c
index affa761..18e90f9 100644
--- a/tests-clar/diff/patch.c
+++ b/tests-clar/diff/patch.c
@@ -251,13 +251,13 @@ static void check_single_patch_stats(
 	cl_git_pass(git_diff_get_patch(&patch, &delta, diff, 0));
 	cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status);
 
-	cl_assert_equal_i(hunks, (int)git_diff_patch_num_hunks(patch));
+	cl_assert_equal_sz(hunks, git_diff_patch_num_hunks(patch));
 
 	cl_git_pass(
 		git_diff_patch_line_stats(NULL, &actual_adds, &actual_dels, patch));
 
-	cl_assert_equal_i(adds, actual_adds);
-	cl_assert_equal_i(dels, actual_dels);
+	cl_assert_equal_sz(adds, actual_adds);
+	cl_assert_equal_sz(dels, actual_dels);
 
 	git_diff_patch_free(patch);
 	git_diff_list_free(diff);
diff --git a/tests-clar/refs/branches/trackingname.c b/tests-clar/refs/branches/trackingname.c
index ea90583..5aee333 100644
--- a/tests-clar/refs/branches/trackingname.c
+++ b/tests-clar/refs/branches/trackingname.c
@@ -37,6 +37,6 @@ void test_refs_branches_trackingname__can_retrieve_the_local_tracking_reference_
 
 void test_refs_branches_trackingname__can_return_the_size_of_thelocal_tracking_reference_name_of_a_local_branch(void)
 {
-	cl_assert_equal_i(strlen("refs/heads/master") + 1,
+	cl_assert_equal_i((int)strlen("refs/heads/master") + 1,
 		git_branch_tracking_name(NULL, 0, repo, "refs/heads/track-local"));
 }
diff --git a/tests-clar/reset/default.c b/tests-clar/reset/default.c
new file mode 100644
index 0000000..506d971
--- /dev/null
+++ b/tests-clar/reset/default.c
@@ -0,0 +1,180 @@
+#include "clar_libgit2.h"
+#include "posix.h"
+#include "reset_helpers.h"
+#include "path.h"
+
+static git_repository *_repo;
+static git_object *_target;
+static git_strarray _pathspecs;
+static git_index *_index;
+
+static void initialize(const char *repo_name)
+{
+	_repo = cl_git_sandbox_init(repo_name);
+	cl_git_pass(git_repository_index(&_index, _repo));
+
+	_target = NULL;
+
+	_pathspecs.strings = NULL;
+	_pathspecs.count = 0;
+}
+
+void test_reset_default__initialize(void)
+{
+	initialize("status");
+}
+
+void test_reset_default__cleanup(void)
+{
+	git_object_free(_target);
+	_target = NULL;
+
+	git_index_free(_index);
+	_index = NULL;
+
+	cl_git_sandbox_cleanup();
+}
+
+static void assert_content_in_index(
+	git_strarray *pathspecs,
+	bool should_exist,
+	git_strarray *expected_shas)
+{
+	size_t i, pos;
+	int error;
+
+	for (i = 0; i < pathspecs->count; i++) {
+		error = git_index_find(&pos, _index, pathspecs->strings[i]);
+
+		if (should_exist) {
+			const git_index_entry *entry;
+
+			cl_assert(error != GIT_ENOTFOUND);
+
+			entry = git_index_get_byindex(_index, pos);
+			cl_assert(entry != NULL);
+
+			if (!expected_shas)
+				continue;
+
+			cl_git_pass(git_oid_streq(&entry->oid, expected_shas->strings[i]));
+		} else
+			cl_assert_equal_i(should_exist, error != GIT_ENOTFOUND);
+	}
+}
+
+void test_reset_default__resetting_filepaths_against_a_null_target_removes_them_from_the_index(void)
+{
+	char *paths[] = { "staged_changes", "staged_new_file" };
+
+	_pathspecs.strings = paths;
+	_pathspecs.count = 2;
+
+	assert_content_in_index(&_pathspecs, true, NULL);
+
+	cl_git_pass(git_reset_default(_repo, NULL, &_pathspecs));
+
+	assert_content_in_index(&_pathspecs, false, NULL);
+}
+
+/*
+ * $ git ls-files --cached -s --abbrev=7 -- "staged*"
+ * 100644 55d316c 0        staged_changes
+ * 100644 a6be623 0        staged_changes_file_deleted
+ * ...
+ *
+ * $ git reset 0017bd4 -- staged_changes staged_changes_file_deleted
+ * Unstaged changes after reset:
+ * ...
+ *
+ * $ git ls-files --cached -s --abbrev=7 -- "staged*"
+ * 100644 32504b7 0        staged_changes
+ * 100644 061d42a 0        staged_changes_file_deleted
+ * ...
+ */
+void test_reset_default__resetting_filepaths_replaces_their_corresponding_index_entries(void)
+{
+	git_strarray before, after;
+
+	char *paths[] = { "staged_changes", "staged_changes_file_deleted" };
+	char *before_shas[] = { "55d316c9ba708999f1918e9677d01dfcae69c6b9",
+		"a6be623522ce87a1d862128ac42672604f7b468b" };
+	char *after_shas[] = { "32504b727382542f9f089e24fddac5e78533e96c",
+		"061d42a44cacde5726057b67558821d95db96f19" };
+
+	_pathspecs.strings = paths;
+	_pathspecs.count = 2;
+	before.strings = before_shas;
+	before.count = 2;
+	after.strings = after_shas;
+	after.count = 2;
+
+	cl_git_pass(git_revparse_single(&_target, _repo, "0017bd4"));
+	assert_content_in_index(&_pathspecs, true, &before);
+
+	cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
+
+	assert_content_in_index(&_pathspecs, true, &after);
+}
+
+/*
+ * $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt
+ * 100644 1f85ca5 1        conflicts-one.txt
+ * 100644 6aea5f2 2        conflicts-one.txt
+ * 100644 516bd85 3        conflicts-one.txt
+ *
+ * $  git reset 9a05ccb -- conflicts-one.txt
+ * Unstaged changes after reset:
+ * ...
+ *
+ * $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt
+ * 100644 1f85ca5 0        conflicts-one.txt
+ *
+ */
+void test_reset_default__resetting_filepaths_clears_previous_conflicts(void)
+{
+	git_index_entry *conflict_entry[3];
+	git_strarray after;
+
+	char *paths[] = { "conflicts-one.txt" };
+	char *after_shas[] = { "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81" };
+
+	test_reset_default__cleanup();
+	initialize("mergedrepo");
+
+	_pathspecs.strings = paths;
+	_pathspecs.count = 1;
+	after.strings = after_shas;
+	after.count = 1;
+
+	cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
+		&conflict_entry[2], _index, "conflicts-one.txt"));
+
+	cl_git_pass(git_revparse_single(&_target, _repo, "9a05ccb"));
+	cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
+
+	assert_content_in_index(&_pathspecs, true, &after);
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_index_conflict_get(&conflict_entry[0],
+		&conflict_entry[1], &conflict_entry[2], _index, "conflicts-one.txt"));
+}
+
+/*
+$  git reset HEAD -- "I_am_not_there.txt" "me_neither.txt"
+Unstaged changes after reset:
+...
+*/
+void test_reset_default__resetting_unknown_filepaths_does_not_fail(void)
+{
+	char *paths[] = { "I_am_not_there.txt", "me_neither.txt" };
+
+	_pathspecs.strings = paths;
+	_pathspecs.count = 2;
+
+	assert_content_in_index(&_pathspecs, false, NULL);
+
+	cl_git_pass(git_revparse_single(&_target, _repo, "HEAD"));
+	cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
+
+	assert_content_in_index(&_pathspecs, false, NULL);
+}