Commit 94f263f59be5be74945367b9793c57f297ed4a44

Ben Straub 2014-01-25T08:04:49

Add reflog params to set-head calls

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
diff --git a/include/git2/repository.h b/include/git2/repository.h
index a0453da..648667c 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -567,11 +567,15 @@ GIT_EXTERN(int) git_repository_hashfile(
  *
  * @param repo Repository pointer
  * @param refname Canonical name of the reference the HEAD should point at
+ * @param signature The identity that will used to populate the reflog entry
+ * @param log_message The one line long message to be appended to the reflog
  * @return 0 on success, or an error code
  */
 GIT_EXTERN(int) git_repository_set_head(
 	git_repository* repo,
-	const char* refname);
+	const char* refname,
+	const git_signature *signature,
+	const char *log_message);
 
 /**
  * Make the repository HEAD directly point to the Commit.
@@ -587,11 +591,15 @@ GIT_EXTERN(int) git_repository_set_head(
  *
  * @param repo Repository pointer
  * @param commitish Object id of the Commit the HEAD should point to
+ * @param signature The identity that will used to populate the reflog entry
+ * @param log_message The one line long message to be appended to the reflog
  * @return 0 on success, or an error code
  */
 GIT_EXTERN(int) git_repository_set_head_detached(
 	git_repository* repo,
-	const git_oid* commitish);
+	const git_oid* commitish,
+	const git_signature *signature,
+	const char *log_message);
 
 /**
  * Detach the HEAD.
diff --git a/src/clone.c b/src/clone.c
index 2e9d72a..2bf2fc5 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -152,21 +152,26 @@ static int reference_matches_remote_head(
 static int update_head_to_new_branch(
 	git_repository *repo,
 	const git_oid *target,
-	const char *name)
+	const char *name,
+	const char *reflog_message)
 {
 	git_reference *tracking_branch = NULL;
 	int error = create_tracking_branch(&tracking_branch, repo, target, name);
 
 	if (!error)
 		error = git_repository_set_head(
-			repo, git_reference_name(tracking_branch));
+			repo, git_reference_name(tracking_branch),
+			NULL, reflog_message);
 
 	git_reference_free(tracking_branch);
 
 	return error;
 }
 
-static int update_head_to_remote(git_repository *repo, git_remote *remote)
+static int update_head_to_remote(
+		git_repository *repo,
+		git_remote *remote,
+		const char *reflog_message)
 {
 	int error = 0;
 	size_t refs_len;
@@ -215,7 +220,8 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
 		error = update_head_to_new_branch(
 			repo,
 			&head_info.remote_head_oid,
-			git_buf_cstr(&head_info.branchname));
+			git_buf_cstr(&head_info.branchname),
+			reflog_message);
 		goto cleanup;
 	}
 
@@ -229,10 +235,11 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
 		error = update_head_to_new_branch(
 			repo,
 			&head_info.remote_head_oid,
-			git_buf_cstr(&head_info.branchname));
+			git_buf_cstr(&head_info.branchname),
+			reflog_message);
 	} else {
 		error = git_repository_set_head_detached(
-			repo, &head_info.remote_head_oid);
+			repo, &head_info.remote_head_oid, NULL, reflog_message);
 	}
 
 cleanup:
@@ -244,7 +251,8 @@ cleanup:
 static int update_head_to_branch(
 		git_repository *repo,
 		const char *remote_name,
-		const char *branch)
+		const char *branch,
+		const char *reflog_message)
 {
 	int retcode;
 	git_buf remote_branch_name = GIT_BUF_INIT;
@@ -259,7 +267,7 @@ static int update_head_to_branch(
 	if ((retcode = git_reference_lookup(&remote_ref, repo, git_buf_cstr(&remote_branch_name))) < 0)
 		goto cleanup;
 
-	retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch);
+	retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch, reflog_message);
 
 cleanup:
 	git_reference_free(remote_ref);
@@ -323,6 +331,7 @@ int git_clone_into(git_repository *repo, git_remote *remote, const git_checkout_
 {
 	int error = 0, old_fetchhead;
 	git_strarray refspecs;
+	git_buf reflog_message = GIT_BUF_INIT;
 
 	assert(repo && remote);
 
@@ -340,15 +349,16 @@ int git_clone_into(git_repository *repo, git_remote *remote, const git_checkout_
 
 	old_fetchhead = git_remote_update_fetchhead(remote);
 	git_remote_set_update_fetchhead(remote, 0);
+	git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
 
 	if ((error = git_remote_fetch(remote)) != 0)
 		goto cleanup;
 
 	if (branch)
-		error = update_head_to_branch(repo, git_remote_name(remote), branch);
+		error = update_head_to_branch(repo, git_remote_name(remote), branch, git_buf_cstr(&reflog_message));
 	/* Point HEAD to the same ref as the remote's head */
 	else
-		error = update_head_to_remote(repo, remote);
+		error = update_head_to_remote(repo, remote, git_buf_cstr(&reflog_message));
 
 	if (!error && should_checkout(repo, git_repository_is_bare(repo), co_opts))
 		error = git_checkout_head(repo, co_opts);
@@ -364,6 +374,7 @@ cleanup:
 	}
 
 	git_strarray_free(&refspecs);
+	git_buf_free(&reflog_message);
 
 	return error;
 }
diff --git a/src/refs.c b/src/refs.c
index 0f0a380..65e7e64 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -545,7 +545,7 @@ static int reference__rename(git_reference **out, git_reference *ref, const char
 
 	/* Update HEAD it was pointing to the reference being renamed */
 	if (should_head_be_updated &&
-		(error = git_repository_set_head(ref->db->repo, new_name)) < 0) {
+		(error = git_repository_set_head(ref->db->repo, new_name, signature, message)) < 0) {
 		giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference");
 		return error;
 	}
diff --git a/src/repository.c b/src/repository.c
index 285d889..2c1b602 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1830,7 +1830,9 @@ static bool looks_like_a_branch(const char *refname)
 
 int git_repository_set_head(
 	git_repository* repo,
-	const char* refname)
+	const char* refname,
+	const git_signature *signature,
+	const char *log_message)
 {
 	git_reference *ref,
 		*new_head = NULL;
@@ -1843,12 +1845,17 @@ int git_repository_set_head(
 		return error;
 
 	if (!error) {
-		if (git_reference_is_branch(ref))
-			error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, git_reference_name(ref), 1, NULL, NULL);
-		else
-			error = git_repository_set_head_detached(repo, git_reference_target(ref));
-	} else if (looks_like_a_branch(refname))
-		error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname, 1, NULL, NULL);
+		if (git_reference_is_branch(ref)) {
+			error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE,
+					git_reference_name(ref), true, signature, log_message);
+		} else {
+			error = git_repository_set_head_detached(repo, git_reference_target(ref),
+					signature, log_message);
+		}
+	} else if (looks_like_a_branch(refname)) {
+		error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname,
+				true, signature, log_message);
+	}
 
 	git_reference_free(ref);
 	git_reference_free(new_head);
@@ -1857,7 +1864,9 @@ int git_repository_set_head(
 
 int git_repository_set_head_detached(
 	git_repository* repo,
-	const git_oid* commitish)
+	const git_oid* commitish,
+	const git_signature *signature,
+	const char *log_message)
 {
 	int error;
 	git_object *object,
@@ -1872,7 +1881,7 @@ int git_repository_set_head_detached(
 	if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0)
 		goto cleanup;
 
-	error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), 1, NULL, NULL);
+	error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, signature, log_message);
 
 cleanup:
 	git_object_free(object);
diff --git a/tests/checkout/tree.c b/tests/checkout/tree.c
index 3c731c5..4e915e8 100644
--- a/tests/checkout/tree.c
+++ b/tests/checkout/tree.c
@@ -63,7 +63,7 @@ void test_checkout_tree__can_checkout_and_remove_directory(void)
 	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
 	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
 
-	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
+	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees", NULL, NULL));
 
 	cl_assert_equal_i(true, git_path_isdir("./testrepo/ab/"));
 	cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
@@ -78,7 +78,7 @@ void test_checkout_tree__can_checkout_and_remove_directory(void)
 	cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
 	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
 
-	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
+	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master", NULL, NULL));
 
 	/* This directory should no longer exist */
 	cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
@@ -163,7 +163,7 @@ void test_checkout_tree__can_switch_branches(void)
 	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
 
 	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
-	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
+	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir", NULL, NULL));
 
 	cl_assert(git_path_isfile("testrepo/README"));
 	cl_assert(git_path_isfile("testrepo/branch_file.txt"));
@@ -183,7 +183,7 @@ void test_checkout_tree__can_switch_branches(void)
 	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
 
 	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
-	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
+	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees", NULL, NULL));
 
 	cl_assert(git_path_isfile("testrepo/README"));
 	cl_assert(git_path_isfile("testrepo/branch_file.txt"));
@@ -253,7 +253,7 @@ static int checkout_tree_with_blob_ignored_in_workdir(int strategy, bool isdir)
 	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
 
 	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
-	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
+	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir", NULL, NULL));
 
 	cl_assert(git_path_isfile("testrepo/README"));
 	cl_assert(git_path_isfile("testrepo/branch_file.txt"));
@@ -313,7 +313,7 @@ void test_checkout_tree__can_overwrite_ignored_by_default(void)
 {
 	cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, false));
 
-	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
+	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees", NULL, NULL));
 
 	cl_assert(git_path_isfile("testrepo/ab/4.txt"));
 
@@ -334,7 +334,7 @@ void test_checkout_tree__can_overwrite_ignored_folder_by_default(void)
 {
 	cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, true));
 
-	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
+	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees", NULL, NULL));
 
 	cl_assert(git_path_isfile("testrepo/ab/4.txt"));
 
@@ -367,7 +367,7 @@ void test_checkout_tree__can_update_only(void)
 	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
 
 	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
-	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
+	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir", NULL, NULL));
 
 	assert_on_branch(g_repo, "dir");
 
@@ -396,7 +396,7 @@ void test_checkout_tree__can_checkout_with_pattern(void)
 
 	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
 	cl_git_pass(
-		git_repository_set_head_detached(g_repo, git_object_id(g_object)));
+		git_repository_set_head_detached(g_repo, git_object_id(g_object), NULL, NULL));
 
 	git_object_free(g_object);
 	g_object = NULL;
@@ -435,7 +435,7 @@ void test_checkout_tree__can_disable_pattern_match(void)
 
 	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
 	cl_git_pass(
-		git_repository_set_head_detached(g_repo, git_object_id(g_object)));
+		git_repository_set_head_detached(g_repo, git_object_id(g_object), NULL, NULL));
 
 	git_object_free(g_object);
 	g_object = NULL;
@@ -687,7 +687,7 @@ void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
 	cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
 
 	cl_git_pass(git_checkout_tree(g_repo, (git_object *)commit, &opts));
-	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
+	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master", NULL, NULL));
 
 	cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777));
 	cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n");
diff --git a/tests/checkout/typechange.c b/tests/checkout/typechange.c
index 6cf99ac..d88864c 100644
--- a/tests/checkout/typechange.c
+++ b/tests/checkout/typechange.c
@@ -122,7 +122,7 @@ void test_checkout_typechange__checkout_typechanges_safe(void)
 		cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
 
 		cl_git_pass(
-			git_repository_set_head_detached(g_repo, git_object_id(obj)));
+			git_repository_set_head_detached(g_repo, git_object_id(obj), NULL, NULL));
 
 		assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true);
 
@@ -231,7 +231,7 @@ void test_checkout_typechange__checkout_with_conflicts(void)
 		cl_assert(!git_path_exists("typechanges/untracked"));
 
 		cl_git_pass(
-			git_repository_set_head_detached(g_repo, git_object_id(obj)));
+			git_repository_set_head_detached(g_repo, git_object_id(obj), NULL, NULL));
 
 		assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true);
 
diff --git a/tests/clone/nonetwork.c b/tests/clone/nonetwork.c
index 3cd5fb7..863412c 100644
--- a/tests/clone/nonetwork.c
+++ b/tests/clone/nonetwork.c
@@ -217,7 +217,7 @@ void test_clone_nonetwork__can_detached_head(void)
 	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
 
 	cl_git_pass(git_revparse_single(&obj, g_repo, "master~1"));
-	cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj)));
+	cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj), NULL, NULL));
 
 	cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options));
 
diff --git a/tests/repo/head.c b/tests/repo/head.c
index 101e30f..06e837d 100644
--- a/tests/repo/head.c
+++ b/tests/repo/head.c
@@ -54,7 +54,7 @@ void test_repo_head__set_head_Attaches_HEAD_to_un_unborn_branch_when_the_branch_
 {
 	git_reference *head;
 
-	cl_git_pass(git_repository_set_head(repo, "refs/heads/doesnt/exist/yet"));
+	cl_git_pass(git_repository_set_head(repo, "refs/heads/doesnt/exist/yet", NULL, NULL));
 
 	cl_assert_equal_i(false, git_repository_head_detached(repo));
 
@@ -63,19 +63,19 @@ void test_repo_head__set_head_Attaches_HEAD_to_un_unborn_branch_when_the_branch_
 
 void test_repo_head__set_head_Returns_ENOTFOUND_when_the_reference_doesnt_exist(void)
 {
-	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head(repo, "refs/tags/doesnt/exist/yet"));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head(repo, "refs/tags/doesnt/exist/yet", NULL, NULL));
 }
 
 void test_repo_head__set_head_Fails_when_the_reference_points_to_a_non_commitish(void)
 {
-	cl_git_fail(git_repository_set_head(repo, "refs/tags/point_to_blob"));
+	cl_git_fail(git_repository_set_head(repo, "refs/tags/point_to_blob", NULL, NULL));
 }
 
 void test_repo_head__set_head_Attaches_HEAD_when_the_reference_points_to_a_branch(void)
 {
 	git_reference *head;
 
-	cl_git_pass(git_repository_set_head(repo, "refs/heads/br2"));
+	cl_git_pass(git_repository_set_head(repo, "refs/heads/br2", NULL, NULL));
 
 	cl_assert_equal_i(false, git_repository_head_detached(repo));
 
@@ -102,7 +102,7 @@ static void assert_head_is_correctly_detached(void)
 
 void test_repo_head__set_head_Detaches_HEAD_when_the_reference_doesnt_point_to_a_branch(void)
 {
-	cl_git_pass(git_repository_set_head(repo, "refs/tags/test"));
+	cl_git_pass(git_repository_set_head(repo, "refs/tags/test", NULL, NULL));
 
 	cl_assert_equal_i(true, git_repository_head_detached(repo));
 
@@ -115,7 +115,7 @@ void test_repo_head__set_head_detached_Return_ENOTFOUND_when_the_object_doesnt_e
 
 	cl_git_pass(git_oid_fromstr(&oid, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
 
-	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head_detached(repo, &oid));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head_detached(repo, &oid, NULL, NULL));
 }
 
 void test_repo_head__set_head_detached_Fails_when_the_object_isnt_a_commitish(void)
@@ -124,7 +124,7 @@ void test_repo_head__set_head_detached_Fails_when_the_object_isnt_a_commitish(vo
 
 	cl_git_pass(git_revparse_single(&blob, repo, "point_to_blob"));
 
-	cl_git_fail(git_repository_set_head_detached(repo, git_object_id(blob)));
+	cl_git_fail(git_repository_set_head_detached(repo, git_object_id(blob), NULL, NULL));
 
 	git_object_free(blob);
 }
@@ -136,7 +136,7 @@ void test_repo_head__set_head_detached_Detaches_HEAD_and_make_it_point_to_the_pe
 	cl_git_pass(git_revparse_single(&tag, repo, "tags/test"));
 	cl_assert_equal_i(GIT_OBJ_TAG, git_object_type(tag));
 
-	cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag)));
+	cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag), NULL, NULL));
 
 	assert_head_is_correctly_detached();
 
diff --git a/tests/status/submodules.c b/tests/status/submodules.c
index dc7990c..80ff162 100644
--- a/tests/status/submodules.c
+++ b/tests/status/submodules.c
@@ -140,7 +140,7 @@ void test_status_submodules__moved_head(void)
 	/* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
 	cl_git_pass(
 		git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
-	cl_git_pass(git_repository_set_head_detached(smrepo, &oid));
+	cl_git_pass(git_repository_set_head_detached(smrepo, &oid, NULL, NULL));
 
 	/* first do a normal status, which should now include the submodule */