Commit b0ca1b18e24723e50237dbe20de66bf39db38a2f

Vicent Marti 2014-07-02T15:29:05

Merge pull request #2452 from libgit2/cmn/clone-custom-repo Provide a callback to customize the repository on clone

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
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3938dcb..f471499 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,3 +25,10 @@ v0.21 + 1
 
   The remote_callbacks member has been preserved for convenience, although it
   is not used when a remote creation callback is supplied.
+
+* The git_clone_options struct now provides repository_cb and
+  repository_cb_payload to allow the user to create a repository with
+  custom options.
+
+* git_clone_into and git_clone_local_into have been removed from the
+  public API in favour of git_clone callbacks
diff --git a/include/git2/clone.h b/include/git2/clone.h
index c07928a..fa2e25b 100644
--- a/include/git2/clone.h
+++ b/include/git2/clone.h
@@ -74,6 +74,26 @@ typedef int (*git_remote_create_cb)(
 	void *payload);
 
 /**
+ * The signature of a function matchin git_repository_init, with an
+ * aditional void * as callback payload.
+ *
+ * Callers of git_clone my provide a function matching this signature
+ * to override the repository creation and customization process
+ * during a clone operation.
+ *
+ * @param out the resulting repository
+ * @param path path in which to create the repository
+ * @param bare whether the repository is bare. This is the value from the clone options
+ * @param payload payload specified by the options
+ * @return 0, or a negative value to indicate error
+ */
+typedef int (*git_repository_create_cb)(
+	git_repository **out,
+	const char *path,
+	int bare,
+	void *payload);
+
+/**
  * Clone options structure
  *
  * Use the GIT_CLONE_OPTIONS_INIT to get the default settings, like this:
@@ -126,6 +146,19 @@ typedef struct git_clone_options {
 	git_signature *signature;
 
 	/**
+	 * A callback used to create the new repository into which to
+	 * clone. If NULL, the 'bare' field will be used to determine
+	 * whether to create a bare repository.
+	 */
+	git_repository_create_cb repository_cb;
+
+	/**
+	 * An opaque payload to pass to the git_repository creation callback.
+	 * This parameter is ignored unless repository_cb is non-NULL.
+	 */
+	void *repository_cb_payload;
+
+	/**
 	 * A callback used to create the git_remote, prior to its being
 	 * used to perform the clone operation. See the documentation for
 	 * git_remote_create_cb for details. This parameter may be NULL,
@@ -158,9 +191,9 @@ GIT_EXTERN(int) git_clone_init_options(
 /**
  * Clone a remote repository.
  *
- * This version handles the simple case. If you'd like to create the
- * repository or remote with non-default settings, you can create and
- * configure them and then use `git_clone_into()`.
+ * By default this creates its repository and initial remote to match
+ * git's defaults. You can use the options in the callback to
+ * customize how these are created.
  *
  * @param out pointer that will receive the resulting repository object
  * @param url the remote repository to clone
@@ -177,59 +210,6 @@ GIT_EXTERN(int) git_clone(
 	const char *local_path,
 	const git_clone_options *options);
 
-/**
- * Clone into a repository
- *
- * After creating the repository and remote and configuring them for
- * paths and callbacks respectively, you can call this function to
- * perform the clone operation and optionally checkout files.
- *
- * @param repo the repository to use
- * @param remote the remote repository to clone from
- * @param co_opts options to use during checkout
- * @param branch the branch to checkout after the clone, pass NULL for the
- *        remote's default branch
- * @param signature The identity used when updating the reflog.
- * @return 0 on success, any non-zero return value from a callback
- *         function, or a negative value to indicate an error (use
- *         `giterr_last` for a detailed error message)
- */
-GIT_EXTERN(int) git_clone_into(
-	git_repository *repo,
-	git_remote *remote,
-	const git_checkout_options *co_opts,
-	const char *branch,
-	const git_signature *signature);
-
-/**
- * Perform a local clone into a repository
- *
- * A "local clone" bypasses any git-aware protocols and simply copies
- * over the object database from the source repository. It is often
- * faster than a git-aware clone, but no verification of the data is
- * performed, and can copy over too much data.
- *
- * @param repo the repository to use
- * @param remote the remote repository to clone from
- * @param co_opts options to use during checkout
- * @param branch the branch to checkout after the clone, pass NULL for the
- *        remote's default branch
- * @param link wether to use hardlinks instead of copying
- * objects. This is only possible if both repositories are on the same
- * filesystem.
- * @param signature the identity used when updating the reflog
- * @return 0 on success, any non-zero return value from a callback
- *         function, or a negative value to indicate an error (use
- *         `giterr_last` for a detailed error message)
- */
-GIT_EXTERN(int) git_clone_local_into(
-	git_repository *repo,
-	git_remote *remote,
-	const git_checkout_options *co_opts,
-	const char *branch,
-	int link,
-	const git_signature *signature);
-
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/src/clone.c b/src/clone.c
index a4ed1a2..8f0284a 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -24,6 +24,8 @@
 #include "repository.h"
 #include "odb.h"
 
+static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link, const git_signature *signature);
+
 static int create_branch(
 	git_reference **branch,
 	git_repository *repo,
@@ -229,6 +231,13 @@ cleanup:
 	return retcode;
 }
 
+static int default_repository_create(git_repository **out, const char *path, int bare, void *payload)
+{
+	GIT_UNUSED(payload);
+
+	return git_repository_init(out, path, bare);
+}
+
 static int default_remote_create(
 		git_remote **out,
 		git_repository *repo,
@@ -322,7 +331,7 @@ static int checkout_branch(git_repository *repo, git_remote *remote, const git_c
 	return error;
 }
 
-int git_clone_into(git_repository *repo, git_remote *_remote, const git_checkout_options *co_opts, const char *branch, const git_signature *signature)
+static int clone_into(git_repository *repo, git_remote *_remote, const git_checkout_options *co_opts, const char *branch, const git_signature *signature)
 {
 	int error;
 	git_buf reflog_message = GIT_BUF_INIT;
@@ -396,6 +405,7 @@ int git_clone(
 	git_remote *origin;
 	git_clone_options options = GIT_CLONE_OPTIONS_INIT;
 	uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
+	git_repository_create_cb repository_cb;
 
 	assert(out && url && local_path);
 
@@ -415,17 +425,22 @@ int git_clone(
 	if (git_path_exists(local_path))
 		rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
 
-	if ((error = git_repository_init(&repo, local_path, options.bare)) < 0)
+	if (options.repository_cb)
+		repository_cb = options.repository_cb;
+	else
+		repository_cb = default_repository_create;
+
+	if ((error = repository_cb(&repo, local_path, options.bare, options.repository_cb_payload)) < 0)
 		return error;
 
 	if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
 		if (git_clone__should_clone_local(url, options.local)) {
 			int link = options.local != GIT_CLONE_LOCAL_NO_LINKS;
-			error = git_clone_local_into(
+			error = clone_local_into(
 				repo, origin, &options.checkout_opts,
 				options.checkout_branch, link, options.signature);
 		} else {
-			error = git_clone_into(
+			error = clone_into(
 				repo, origin, &options.checkout_opts,
 				options.checkout_branch, options.signature);
 		}
@@ -485,7 +500,7 @@ static bool can_link(const char *src, const char *dst, int link)
 #endif
 }
 
-int git_clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link, const git_signature *signature)
+static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link, const git_signature *signature)
 {
 	int error, flags;
 	git_repository *src;
diff --git a/tests/clone/local.c b/tests/clone/local.c
index a4406c1..c8ebc14 100644
--- a/tests/clone/local.c
+++ b/tests/clone/local.c
@@ -31,31 +31,24 @@ void test_clone_local__should_clone_local(void)
 void test_clone_local__hardlinks(void)
 {
 	git_repository *repo;
-	git_remote *remote;
-	git_signature *sig;
+	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
 	git_buf buf = GIT_BUF_INIT;
 	struct stat st;
 
-
 	/*
 	 * In this first clone, we just copy over, since the temp dir
 	 * will often be in a different filesystem, so we cannot
 	 * link. It also allows us to control the number of links
 	 */
-	cl_git_pass(git_repository_init(&repo, "./clone.git", true));
-	cl_git_pass(git_remote_create(&remote, repo, "origin", cl_fixture("testrepo.git")));
-	cl_git_pass(git_signature_now(&sig, "foo", "bar"));
-	cl_git_pass(git_clone_local_into(repo, remote, NULL, NULL, false, sig));
-
-	git_remote_free(remote);
+	opts.bare = true;
+	opts.local = GIT_CLONE_LOCAL_NO_LINKS;
+	cl_git_pass(git_clone(&repo, cl_fixture("testrepo.git"), "./clone.git", &opts));
 	git_repository_free(repo);
 
 	/* This second clone is in the same filesystem, so we can hardlink */
 
-	cl_git_pass(git_repository_init(&repo, "./clone2.git", true));
-	cl_git_pass(git_buf_puts(&buf, cl_git_path_url("clone.git")));
-	cl_git_pass(git_remote_create(&remote, repo, "origin", buf.ptr));
-	cl_git_pass(git_clone_local_into(repo, remote, NULL, NULL, true, sig));
+	opts.local = GIT_CLONE_LOCAL;
+	cl_git_pass(git_clone(&repo, cl_git_path_url("clone.git"), "./clone2.git", &opts));
 
 #ifndef GIT_WIN32
 	git_buf_clear(&buf);
@@ -65,14 +58,11 @@ void test_clone_local__hardlinks(void)
 	cl_assert_equal_i(2, st.st_nlink);
 #endif
 
-	git_remote_free(remote);
 	git_repository_free(repo);
 	git_buf_clear(&buf);
 
-	cl_git_pass(git_repository_init(&repo, "./clone3.git", true));
-	cl_git_pass(git_buf_puts(&buf, cl_git_path_url("clone.git")));
-	cl_git_pass(git_remote_create(&remote, repo, "origin", buf.ptr));
-	cl_git_pass(git_clone_local_into(repo, remote, NULL, NULL, false, sig));
+	opts.local = GIT_CLONE_LOCAL_NO_LINKS;
+	cl_git_pass(git_clone(&repo, cl_git_path_url("clone.git"), "./clone3.git", &opts));
 
 	git_buf_clear(&buf);
 	cl_git_pass(git_buf_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
@@ -80,7 +70,6 @@ void test_clone_local__hardlinks(void)
 	cl_git_pass(p_stat(buf.ptr, &st));
 	cl_assert_equal_i(1, st.st_nlink);
 
-	git_remote_free(remote);
 	git_repository_free(repo);
 
 	/* this one should automatically use links */
@@ -95,7 +84,6 @@ void test_clone_local__hardlinks(void)
 #endif
 
 	git_buf_free(&buf);
-	git_signature_free(sig);
 	git_repository_free(repo);
 
 	cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES));
diff --git a/tests/clone/nonetwork.c b/tests/clone/nonetwork.c
index ab3e8f5..824988a 100644
--- a/tests/clone/nonetwork.c
+++ b/tests/clone/nonetwork.c
@@ -279,23 +279,6 @@ void test_clone_nonetwork__clone_updates_reflog_properly(void)
 	assert_correct_reflog("refs/heads/master");
 }
 
-void test_clone_nonetwork__clone_into_updates_reflog_properly(void)
-{
-	git_remote *remote;
-	git_signature *sig;
-	cl_git_pass(git_signature_now(&sig, "Me", "foo@example.com"));
-
-	cl_git_pass(git_repository_init(&g_repo, "./foo", false));
-	cl_git_pass(git_remote_create(&remote, g_repo, "origin", cl_git_fixture_url("testrepo.git")));
-	cl_git_pass(git_clone_into(g_repo, remote, NULL, NULL, sig));
-
-	assert_correct_reflog("HEAD");
-	assert_correct_reflog("refs/heads/master");
-
-	git_remote_free(remote);
-	git_signature_free(sig);
-}
-
 static void cleanup_repository(void *path)
 {
 	if (g_repo) {
diff --git a/tests/network/fetchlocal.c b/tests/network/fetchlocal.c
index 0d23bef..8809f42 100644
--- a/tests/network/fetchlocal.c
+++ b/tests/network/fetchlocal.c
@@ -87,28 +87,43 @@ void test_network_fetchlocal__partial(void)
 	git_remote_free(origin);
 }
 
-void test_network_fetchlocal__clone_into_mirror(void)
+static int remote_mirror_cb(git_remote **out, git_repository *repo,
+			    const char *name, const char *url, void *payload)
 {
-	git_buf path = GIT_BUF_INIT;
-	git_repository *repo;
+	int error;
 	git_remote *remote;
-	git_reference *head;
 
-	cl_git_pass(git_repository_init(&repo, "./foo.git", true));
-	cl_git_pass(git_remote_create(&remote, repo, "origin", cl_git_fixture_url("testrepo.git")));
+	GIT_UNUSED(payload);
+
+	if ((error = git_remote_create(&remote, repo, name, url)) < 0)
+		return error;
 
 	git_remote_clear_refspecs(remote);
-	cl_git_pass(git_remote_add_fetch(remote, "+refs/*:refs/*"));
 
-	cl_git_pass(git_clone_into(repo, remote, NULL, NULL, NULL));
+	if ((error = git_remote_add_fetch(remote, "+refs/*:refs/*")) < 0) {
+		git_remote_free(remote);
+		return error;
+	}
+
+	*out = remote;
+	return 0;
+}
+
+void test_network_fetchlocal__clone_into_mirror(void)
+{
+	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
+	git_repository *repo;
+	git_reference *head;
+
+	opts.bare = true;
+	opts.remote_cb = remote_mirror_cb;
+	cl_git_pass(git_clone(&repo, cl_git_fixture_url("testrepo.git"), "./foo.git", &opts));
 
 	cl_git_pass(git_reference_lookup(&head, repo, "HEAD"));
 	cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
 	cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
 
-	git_remote_free(remote);
 	git_reference_free(head);
 	git_repository_free(repo);
-	git_buf_free(&path);
 	cl_fixture_cleanup("./foo.git");
 }
diff --git a/tests/online/clone.c b/tests/online/clone.c
index 4f4312a..2e2e976 100644
--- a/tests/online/clone.c
+++ b/tests/online/clone.c
@@ -124,65 +124,49 @@ void test_online_clone__can_checkout_a_cloned_repo(void)
 	git_buf_free(&path);
 }
 
-void test_online_clone__clone_into(void)
+static int remote_mirror_cb(git_remote **out, git_repository *repo,
+			    const char *name, const char *url, void *payload)
 {
-	git_buf path = GIT_BUF_INIT;
+	int error;
 	git_remote *remote;
-	git_reference *head;
-	git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
-	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
-
-	bool checkout_progress_cb_was_called = false,
-		  fetch_progress_cb_was_called = false;
-
-	checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
-	checkout_opts.progress_cb = &checkout_progress;
-	checkout_opts.progress_payload = &checkout_progress_cb_was_called;
+	git_remote_callbacks *callbacks = (git_remote_callbacks *) payload;
 
-	cl_git_pass(git_repository_init(&g_repo, "./foo", false));
-	cl_git_pass(git_remote_create(&remote, g_repo, "origin", LIVE_REPO_URL));
 
-	callbacks.transfer_progress = &fetch_progress;
-	callbacks.payload = &fetch_progress_cb_was_called;
-	git_remote_set_callbacks(remote, &callbacks);
+	if ((error = git_remote_create(&remote, repo, name, url)) < 0)
+		return error;
 
-	cl_git_pass(git_clone_into(g_repo, remote, &checkout_opts, NULL, NULL));
-
-	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
-	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path)));
+	if ((error = git_remote_set_callbacks(remote, callbacks)) < 0) {
+		git_remote_free(remote);
+		return error;
+	}
 
-	cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
-	cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
-	cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
+	git_remote_clear_refspecs(remote);
 
-	cl_assert_equal_i(true, checkout_progress_cb_was_called);
-	cl_assert_equal_i(true, fetch_progress_cb_was_called);
+	if ((error = git_remote_add_fetch(remote, "+refs/*:refs/*")) < 0) {
+		git_remote_free(remote);
+		return error;
+	}
 
-	git_remote_free(remote);
-	git_reference_free(head);
-	git_buf_free(&path);
+	*out = remote;
+	return 0;
 }
 
 void test_online_clone__clone_mirror(void)
 {
-	git_buf path = GIT_BUF_INIT;
-	git_remote *remote;
+	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
 	git_reference *head;
 	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
 
 	bool fetch_progress_cb_was_called = false;
 
-	cl_git_pass(git_repository_init(&g_repo, "./foo.git", true));
-	cl_git_pass(git_remote_create(&remote, g_repo, "origin", LIVE_REPO_URL));
-
 	callbacks.transfer_progress = &fetch_progress;
 	callbacks.payload = &fetch_progress_cb_was_called;
-	git_remote_set_callbacks(remote, &callbacks);
 
-	git_remote_clear_refspecs(remote);
-	cl_git_pass(git_remote_add_fetch(remote, "+refs/*:refs/*"));
+	opts.bare = true;
+	opts.remote_cb = remote_mirror_cb;
+	opts.remote_cb_payload = &callbacks;
 
-	cl_git_pass(git_clone_into(g_repo, remote, NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo.git", &opts));
 
 	cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
 	cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
@@ -190,9 +174,7 @@ void test_online_clone__clone_mirror(void)
 
 	cl_assert_equal_i(true, fetch_progress_cb_was_called);
 
-	git_remote_free(remote);
 	git_reference_free(head);
-	git_buf_free(&path);
 	git_repository_free(g_repo);
 	g_repo = NULL;