Commit 6c08e69fd92028822cb98368e564c5cb7964c072

Vicent Martí 2012-06-07T12:30:20

Merge pull request #669 from nulltoken/topic/reset Add git_reset()

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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
diff --git a/include/git2.h b/include/git2.h
index 2630709..3460144 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -44,5 +44,6 @@
 #include "git2/indexer.h"
 #include "git2/submodule.h"
 #include "git2/notes.h"
+#include "git2/reset.h"
 
 #endif
diff --git a/include/git2/reset.h b/include/git2/reset.h
new file mode 100644
index 0000000..1251787
--- /dev/null
+++ b/include/git2/reset.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_git_reset_h__
+#define INCLUDE_git_reset_h__
+
+/**
+ * @file git2/reset.h
+ * @brief Git reset management routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * Sets the current head to the specified commit oid and optionally
+ * resets the index and working tree to match.
+ *
+ * When specifying a Soft kind of reset, the head will be moved to the commit.
+ *
+ * Specifying a Mixed kind of reset will trigger a Soft reset and the index will
+ * be replaced with the content of the commit tree.
+ *
+ * TODO: Implement remaining kinds of resets.
+ *
+ * @param repo Repository where to perform the reset operation.
+ *
+ * @param target Object 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 GIT_SUCCESS or an error code
+ */
+GIT_EXTERN(int) git_reset(git_repository *repo, const git_object *target, git_reset_type reset_type);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/include/git2/types.h b/include/git2/types.h
index cfb0acf..b4b48af 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -166,6 +166,12 @@ typedef enum {
 	GIT_BRANCH_REMOTE = 2,
 } git_branch_t;
 
+/** Kinds of reset operation. */
+typedef enum {
+	GIT_RESET_SOFT = 1,
+	GIT_RESET_MIXED = 2,
+} git_reset_type;
+
 typedef struct git_refspec git_refspec;
 typedef struct git_remote git_remote;
 
diff --git a/src/commit.c b/src/commit.c
index 2f40dc6..57eafaa 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -81,66 +81,6 @@ int git_commit_create_v(
 	return res;
 }
 
-/* Update the reference named `ref_name` so it points to `oid` */
-static int update_reference(git_repository *repo, git_oid *oid, const char *ref_name)
-{
-	git_reference *ref;
-	int res;
-
-	res = git_reference_lookup(&ref, repo, ref_name);
-
-	/* If we haven't found the reference at all, we assume we need to create
-	 * a new reference and that's it */
-	if (res == GIT_ENOTFOUND) {
-		giterr_clear();
-		return git_reference_create_oid(NULL, repo, ref_name, oid, 1);
-	}
-
-	if (res < 0)
-		return -1;
-
-	/* If we have found a reference, but it's symbolic, we need to update
-	 * the direct reference it points to */
-	if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
-		git_reference *aux;
-		const char *sym_target;
-
-		/* The target pointed at by this reference */
-		sym_target = git_reference_target(ref);
-
-		/* resolve the reference to the target it points to */
-		res = git_reference_resolve(&aux, ref);
-
-		/*
-		 * if the symbolic reference pointed to an inexisting ref,
-		 * this is means we're creating a new branch, for example.
-		 * We need to create a new direct reference with that name
-		 */
-		if (res == GIT_ENOTFOUND) {
-			giterr_clear();
-			res = git_reference_create_oid(NULL, repo, sym_target, oid, 1);
-			git_reference_free(ref);
-			return res;
-		}
-
-		/* free the original symbolic reference now; not before because
-		 * we're using the `sym_target` pointer */
-		git_reference_free(ref);
-
-		if (res < 0)
-			return -1;
-
-		/* store the newly found direct reference in its place */
-		ref = aux;
-	}
-
-	/* ref is made to point to `oid`: ref is either the original reference,
-	 * or the target of the symbolic reference we've looked up */
-	res = git_reference_set_oid(ref, oid);
-	git_reference_free(ref);
-	return res;
-}
-
 int git_commit_create(
 		git_oid *oid,
 		git_repository *repo,
@@ -192,7 +132,7 @@ int git_commit_create(
 	git_buf_free(&commit);
 
 	if (update_ref != NULL)
-		return update_reference(repo, oid, update_ref);
+		return git_reference__update(repo, oid, update_ref);
 
 	return 0;
 
diff --git a/src/refs.c b/src/refs.c
index 1ef3e13..1046857 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1705,3 +1705,62 @@ int git_reference_cmp(git_reference *ref1, git_reference *ref2)
 	return git_oid_cmp(&ref1->target.oid, &ref2->target.oid);
 }
 
+/* Update the reference named `ref_name` so it points to `oid` */
+int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name)
+{
+	git_reference *ref;
+	int res;
+
+	res = git_reference_lookup(&ref, repo, ref_name);
+
+	/* If we haven't found the reference at all, we assume we need to create
+	 * a new reference and that's it */
+	if (res == GIT_ENOTFOUND) {
+		giterr_clear();
+		return git_reference_create_oid(NULL, repo, ref_name, oid, 1);
+	}
+
+	if (res < 0)
+		return -1;
+
+	/* If we have found a reference, but it's symbolic, we need to update
+	 * the direct reference it points to */
+	if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
+		git_reference *aux;
+		const char *sym_target;
+
+		/* The target pointed at by this reference */
+		sym_target = git_reference_target(ref);
+
+		/* resolve the reference to the target it points to */
+		res = git_reference_resolve(&aux, ref);
+
+		/*
+		 * if the symbolic reference pointed to an inexisting ref,
+		 * this is means we're creating a new branch, for example.
+		 * We need to create a new direct reference with that name
+		 */
+		if (res == GIT_ENOTFOUND) {
+			giterr_clear();
+			res = git_reference_create_oid(NULL, repo, sym_target, oid, 1);
+			git_reference_free(ref);
+			return res;
+		}
+
+		/* free the original symbolic reference now; not before because
+		 * we're using the `sym_target` pointer */
+		git_reference_free(ref);
+
+		if (res < 0)
+			return -1;
+
+		/* store the newly found direct reference in its place */
+		ref = aux;
+	}
+
+	/* ref is made to point to `oid`: ref is either the original reference,
+	 * or the target of the symbolic reference we've looked up */
+	res = git_reference_set_oid(ref, oid);
+	git_reference_free(ref);
+	return res;
+}
diff --git a/src/refs.h b/src/refs.h
index 369e91e..0823502 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -54,6 +54,7 @@ void git_repository__refcache_free(git_refcache *refs);
 
 int git_reference__normalize_name(char *buffer_out, size_t out_size, const char *name);
 int git_reference__normalize_name_oid(char *buffer_out, size_t out_size, const char *name);
+int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name);
 
 /**
  * Lookup a reference by name and try to resolve to an OID.
diff --git a/src/reset.c b/src/reset.c
new file mode 100644
index 0000000..14f7a23
--- /dev/null
+++ b/src/reset.c
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+#include "commit.h"
+#include "tag.h"
+#include "git2/reset.h"
+
+#define ERROR_MSG "Cannot perform reset"
+
+static int reset_error_invalid(const char *msg)
+{
+	giterr_set(GITERR_INVALID, "%s - %s", ERROR_MSG, msg);
+	return -1;
+}
+
+int git_reset(
+	git_repository *repo,
+	const git_object *target,
+	git_reset_type reset_type)
+{
+	git_otype target_type = GIT_OBJ_BAD;
+	git_object *commit = NULL;
+	git_index *index = NULL;
+	git_tree *tree = NULL;
+	int error = -1;
+
+	assert(repo && target);
+	assert(reset_type == GIT_RESET_SOFT || reset_type == GIT_RESET_MIXED);
+
+	if (git_object_owner(target) != repo)
+		return reset_error_invalid("The given target does not belong to this repository.");
+
+	if (reset_type == GIT_RESET_MIXED && git_repository_is_bare(repo))
+		return reset_error_invalid("Mixed reset is not allowed in a bare repository.");
+
+	target_type = git_object_type(target);
+
+	switch (target_type)
+	{
+	case GIT_OBJ_TAG:
+		if (git_tag_peel(&commit, (git_tag *)target) < 0)
+			goto cleanup;
+
+		if (git_object_type(commit) != GIT_OBJ_COMMIT) {
+			reset_error_invalid("The given target does not resolve to a commit.");
+			goto cleanup;
+		}
+		break;
+
+	case GIT_OBJ_COMMIT:
+		commit = (git_object *)target;
+		break;
+
+	default:
+		return reset_error_invalid("Only git_tag and git_commit objects are valid targets.");
+	}
+
+	//TODO: Check for unmerged entries
+
+	if (git_reference__update(repo, git_object_id(commit), GIT_HEAD_FILE) < 0)
+		goto cleanup;
+
+	if (reset_type == GIT_RESET_SOFT) {
+		error = 0;
+		goto cleanup;
+	}
+
+	if (git_commit_tree(&tree, (git_commit *)commit) < 0) {
+		giterr_set(GITERR_OBJECT, "%s - Failed to retrieve the commit tree.", ERROR_MSG);
+		goto cleanup;
+	}
+
+	if (git_repository_index(&index, repo) < 0) {
+		giterr_set(GITERR_OBJECT, "%s - Failed to retrieve the index.", ERROR_MSG);
+		goto cleanup;
+	}
+
+	if (git_index_read_tree(index, tree) < 0) {
+		giterr_set(GITERR_INDEX, "%s - Failed to update the index.", ERROR_MSG);
+		goto cleanup;
+	}
+
+	if (git_index_write(index) < 0) {
+		giterr_set(GITERR_INDEX, "%s - Failed to write the index.", ERROR_MSG);
+		goto cleanup;
+	}
+
+	error = 0;
+
+cleanup:
+	if (target_type == GIT_OBJ_TAG)
+		git_object_free(commit);
+
+	git_index_free(index);
+	git_tree_free(tree);
+
+	return error;
+}
diff --git a/tests-clar/reset/mixed.c b/tests-clar/reset/mixed.c
new file mode 100644
index 0000000..7cfff65
--- /dev/null
+++ b/tests-clar/reset/mixed.c
@@ -0,0 +1,47 @@
+#include "clar_libgit2.h"
+#include "posix.h"
+#include "reset_helpers.h"
+#include "path.h"
+
+static git_repository *repo;
+static git_object *target;
+
+void test_reset_mixed__initialize(void)
+{
+	repo = cl_git_sandbox_init("attr");
+	target = NULL;
+}
+
+void test_reset_mixed__cleanup(void)
+{
+	git_object_free(target);
+	cl_git_sandbox_cleanup();
+}
+
+void test_reset_mixed__cannot_reset_in_a_bare_repository(void)
+{
+	git_repository *bare;
+
+	cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git")));
+	cl_assert(git_repository_is_bare(bare) == true);
+
+	retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO);
+
+	cl_git_fail(git_reset(bare, target, GIT_RESET_MIXED));
+
+	git_repository_free(bare);
+}
+
+void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void)
+{
+	unsigned int status;
+
+	cl_git_pass(git_status_file(&status, repo, "macro_bad"));
+	cl_assert(status == GIT_STATUS_CURRENT);
+	retrieve_target_from_oid(&target, repo, "605812ab7fe421fdd325a935d35cb06a9234a7d7");
+
+	cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED));
+
+	cl_git_pass(git_status_file(&status, repo, "macro_bad"));
+	cl_assert(status == GIT_STATUS_WT_NEW);
+}
diff --git a/tests-clar/reset/reset_helpers.c b/tests-clar/reset/reset_helpers.c
new file mode 100644
index 0000000..17edca4
--- /dev/null
+++ b/tests-clar/reset/reset_helpers.c
@@ -0,0 +1,10 @@
+#include "clar_libgit2.h"
+#include "reset_helpers.h"
+
+void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha)
+{
+	git_oid oid;
+
+	cl_git_pass(git_oid_fromstr(&oid, sha));
+	cl_git_pass(git_object_lookup(object_out, repo, &oid, GIT_OBJ_ANY));
+}
diff --git a/tests-clar/reset/reset_helpers.h b/tests-clar/reset/reset_helpers.h
new file mode 100644
index 0000000..5dbe9d2
--- /dev/null
+++ b/tests-clar/reset/reset_helpers.h
@@ -0,0 +1,6 @@
+#include "common.h"
+
+#define KNOWN_COMMIT_IN_BARE_REPO "e90810b8df3e80c413d903f631643c716887138d"
+#define KNOWN_COMMIT_IN_ATTR_REPO "217878ab49e1314388ea2e32dc6fdb58a1b969e0"
+
+extern void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha);
diff --git a/tests-clar/reset/soft.c b/tests-clar/reset/soft.c
new file mode 100644
index 0000000..3200c15
--- /dev/null
+++ b/tests-clar/reset/soft.c
@@ -0,0 +1,102 @@
+#include "clar_libgit2.h"
+#include "reset_helpers.h"
+
+static git_repository *repo;
+static git_object *target;
+
+void test_reset_soft__initialize(void)
+{
+	repo = cl_git_sandbox_init("testrepo.git");
+}
+
+void test_reset_soft__cleanup(void)
+{
+	git_object_free(target);
+	cl_git_sandbox_cleanup();
+}
+
+static void assert_reset_soft(bool should_be_detached)
+{
+	git_oid oid;
+
+	cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
+	cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
+
+	retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
+
+	cl_assert(git_repository_head_detached(repo) == should_be_detached);
+
+	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+
+	cl_assert(git_repository_head_detached(repo) == should_be_detached);
+
+	cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
+	cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
+}
+
+void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void)
+{
+	assert_reset_soft(false);
+}
+
+static void detach_head(void)
+{
+	git_reference *head;
+	git_oid oid;
+
+	cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
+
+	cl_git_pass(git_reference_create_oid(&head, repo, "HEAD", &oid, true));
+	git_reference_free(head);
+}
+
+void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void)
+{
+	detach_head();
+
+	assert_reset_soft(true);
+}
+
+void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void)
+{
+	git_oid oid;
+	char raw_head_oid[GIT_OID_HEXSZ + 1];
+
+	cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
+	git_oid_fmt(raw_head_oid, &oid);
+	raw_head_oid[GIT_OID_HEXSZ] = '\0';
+
+	retrieve_target_from_oid(&target, repo, raw_head_oid);
+
+	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+
+	cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
+	cl_git_pass(git_oid_streq(&oid, raw_head_oid));
+}
+
+void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void)
+{
+	git_oid oid;
+
+	/* b25fa35 is a tag, pointing to another tag which points to commit e90810b */
+	retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
+
+	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+
+	cl_assert(git_repository_head_detached(repo) == false);
+	cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
+	cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
+}
+
+void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void)
+{
+	/* 53fc32d is the tree of commit e90810b */
+	retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016");
+
+	cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT));
+	git_object_free(target);
+
+	/* 521d87c is an annotated tag pointing to a blob */
+	retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91");
+	cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT));
+}