Commit b0b9fd32450227e36e2deae75e86a844ee34bd01

Vicent Marti 2012-12-14T02:41:53

Merge remote-tracking branch 'origin/clone-auth' into development

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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
diff --git a/examples/network/clone.c b/examples/network/clone.c
index 7596523..fd82f44 100644
--- a/examples/network/clone.c
+++ b/examples/network/clone.c
@@ -47,9 +47,25 @@ static void checkout_progress(const char *path, size_t cur, size_t tot, void *pa
 	print_progress(pd);
 }
 
+static int cred_acquire(git_cred **out, const char *url, unsigned int allowed_types, void *payload)
+{
+	char username[128] = {0};
+	char password[128] = {0};
+
+	printf("Username: ");
+	scanf("%s", username);
+
+	/* Yup. Right there on your terminal. Careful where you copy/paste output. */
+	printf("Password: ");
+	scanf("%s", password);
+
+	return git_cred_userpass_plaintext_new(out, username, password);
+}
+
 int do_clone(git_repository *repo, int argc, char **argv)
 {
 	progress_data pd;
+	git_remote *origin = NULL;
 	git_repository *cloned_repo = NULL;
 	git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT;
 	const char *url = argv[1];
@@ -65,14 +81,24 @@ int do_clone(git_repository *repo, int argc, char **argv)
 	}
 
 	// Set up options
-	memset(&checkout_opts, 0, sizeof(checkout_opts));
 	checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
 	checkout_opts.progress_cb = checkout_progress;
 	memset(&pd, 0, sizeof(pd));
 	checkout_opts.progress_payload = &pd;
 
+	// Create the origin remote, and set up for auth
+	error = git_remote_new(&origin, NULL, "origin", url, GIT_REMOTE_DEFAULT_FETCH);
+	if (error != 0) {
+		const git_error *err = giterr_last();
+		if (err) printf("ERROR %d: %s\n", err->klass, err->message);
+		else printf("ERROR %d: no detailed info\n", error);
+		return error;
+	}
+	git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL);
+
 	// Do the clone
-	error = git_clone(&cloned_repo, url, path, &checkout_opts, &fetch_progress, &pd);
+	error = git_clone(&cloned_repo, origin, path, &checkout_opts, &fetch_progress, &pd);
+	git_remote_free(origin);
 	printf("\n");
 	if (error != 0) {
 		const git_error *err = giterr_last();
diff --git a/include/git2/clone.h b/include/git2/clone.h
index 2a02723..8de8e0e 100644
--- a/include/git2/clone.h
+++ b/include/git2/clone.h
@@ -27,7 +27,7 @@ GIT_BEGIN_DECL
  * HEAD.
  *
  * @param out pointer that will receive the resulting repository object
- * @param origin_url repository to clone from
+ * @param origin_remote a remote which will act as the initial fetch source
  * @param workdir_path local directory to clone to
  * @param fetch_progress_cb optional callback for fetch progress. Be aware that
  * this is called inline with network and indexing operations, so performance
@@ -40,7 +40,7 @@ GIT_BEGIN_DECL
  */
 GIT_EXTERN(int) git_clone(
 		git_repository **out,
-		const char *origin_url,
+		git_remote *origin_remote,
 		const char *workdir_path,
 		git_checkout_opts *checkout_opts,
 		git_transfer_progress_callback fetch_progress_cb,
@@ -50,7 +50,7 @@ GIT_EXTERN(int) git_clone(
  * Create a bare clone of a remote repository.
  *
  * @param out pointer that will receive the resulting repository object
- * @param origin_url repository to clone from
+ * @param origin_remote a remote which will act as the initial fetch source
  * @param dest_path local directory to clone to
  * @param fetch_progress_cb optional callback for fetch progress. Be aware that
  * this is called inline with network and indexing operations, so performance
@@ -60,7 +60,7 @@ GIT_EXTERN(int) git_clone(
  */
 GIT_EXTERN(int) git_clone_bare(
 		git_repository **out,
-		const char *origin_url,
+		git_remote *origin_remote,
 		const char *dest_path,
 		git_transfer_progress_callback fetch_progress_cb,
 		void *fetch_progress_payload);
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 82aff38..f3b0a94 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -24,6 +24,14 @@
  */
 GIT_BEGIN_DECL
 
+/**
+ * Use this when creating a remote with git_remote_new to get the default fetch
+ * behavior produced by git_remote_add.  It corresponds to this fetchspec (note
+ * the spaces between '/' and '*' to avoid C compiler errors):
+ * "+refs/heads/ *:refs/remotes/<remote_name>/ *"
+ */
+#define GIT_REMOTE_DEFAULT_FETCH ""
+
 typedef int (*git_remote_rename_problem_cb)(const char *problematic_refspec, void *payload);
 /*
  * TODO: This functions still need to be implemented:
@@ -43,15 +51,25 @@ typedef int (*git_remote_rename_problem_cb)(const char *problematic_refspec, voi
  * See `git_tag_create()` for rules about valid names.
  *
  * @param out pointer to the new remote object
- * @param repo the associated repository
- * @param name the optional remote's name
+ * @param repo the associated repository. May be NULL for a "dangling" remote.
+ * @param name the optional remote's name. May be NULL.
  * @param url the remote repository's URL
- * @param fetch the fetch refspec to use for this remote
+ * @param fetch the fetch refspec to use for this remote. May be NULL for defaults.
  * @return 0, GIT_EINVALIDSPEC or an error code
  */
 GIT_EXTERN(int) git_remote_new(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch);
 
 /**
+ * Sets the owning repository for the remote.  This is only allowed on
+ * dangling remotes.
+ *
+ * @param remote the remote to configure
+ * @param repo the repository that will own the remote
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_remote_set_repository(git_remote *remote, git_repository *repo);
+
+/**
  * Get the information for a particular remote
  *
  * The name will be checked for validity.
diff --git a/src/clone.c b/src/clone.c
index 7d49b39..aa6c43f 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -262,15 +262,14 @@ cleanup:
 
 static int setup_remotes_and_fetch(
 		git_repository *repo,
-		const char *origin_url,
+		git_remote *origin,
 		git_transfer_progress_callback progress_cb,
 		void *progress_payload)
 {
 	int retcode = GIT_ERROR;
-	git_remote *origin = NULL;
 
-	/* Create the "origin" remote */
-	if (!git_remote_add(&origin, repo, GIT_REMOTE_ORIGIN, origin_url)) {
+	/* Add the origin remote */
+	if (!git_remote_set_repository(origin, repo) && !git_remote_save(origin)) {
 		/*
 		 * Don't write FETCH_HEAD, we'll check out the remote tracking
 		 * branch ourselves based on the server's default.
@@ -290,7 +289,6 @@ static int setup_remotes_and_fetch(
 			}
 			git_remote_disconnect(origin);
 		}
-		git_remote_free(origin);
 	}
 
 	return retcode;
@@ -325,7 +323,7 @@ static bool should_checkout(
 
 static int clone_internal(
 	git_repository **out,
-	const char *origin_url,
+	git_remote *origin_remote,
 	const char *path,
 	git_transfer_progress_callback fetch_progress_cb,
 	void *fetch_progress_payload,
@@ -340,7 +338,7 @@ static int clone_internal(
 	}
 
 	if (!(retcode = git_repository_init(&repo, path, is_bare))) {
-		if ((retcode = setup_remotes_and_fetch(repo, origin_url,
+		if ((retcode = setup_remotes_and_fetch(repo, origin_remote,
 						fetch_progress_cb, fetch_progress_payload)) < 0) {
 			/* Failed to fetch; clean up */
 			git_repository_free(repo);
@@ -359,16 +357,16 @@ static int clone_internal(
 
 int git_clone_bare(
 		git_repository **out,
-		const char *origin_url,
+		git_remote *origin_remote,
 		const char *dest_path,
 		git_transfer_progress_callback fetch_progress_cb,
 		void *fetch_progress_payload)
 {
-	assert(out && origin_url && dest_path);
+	assert(out && origin_remote && dest_path);
 
 	return clone_internal(
 		out,
-		origin_url,
+		origin_remote,
 		dest_path,
 		fetch_progress_cb,
 		fetch_progress_payload,
@@ -379,17 +377,17 @@ int git_clone_bare(
 
 int git_clone(
 		git_repository **out,
-		const char *origin_url,
+		git_remote *origin_remote,
 		const char *workdir_path,
 		git_checkout_opts *checkout_opts,
 		git_transfer_progress_callback fetch_progress_cb,
 		void *fetch_progress_payload)
 {
-	assert(out && origin_url && workdir_path);
+	assert(out && origin_remote && workdir_path);
 
 	return clone_internal(
 		out,
-		origin_url,
+		origin_remote,
 		workdir_path,
 		fetch_progress_cb,
 		fetch_progress_payload,
diff --git a/src/remote.c b/src/remote.c
index 670904b..28ce88a 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -86,9 +86,11 @@ cleanup:
 int git_remote_new(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch)
 {
 	git_remote *remote;
+	git_buf fetchbuf = GIT_BUF_INIT;
+	int error = -1;
 
 	/* name is optional */
-	assert(out && repo && url);
+	assert(out && url);
 
 	remote = git__calloc(1, sizeof(git_remote));
 	GITERR_CHECK_ALLOC(remote);
@@ -98,20 +100,26 @@ int git_remote_new(git_remote **out, git_repository *repo, const char *name, con
 	remote->update_fetchhead = 1;
 
 	if (git_vector_init(&remote->refs, 32, NULL) < 0)
-		return -1;
+		goto on_error;
 
 	remote->url = git__strdup(url);
 	GITERR_CHECK_ALLOC(remote->url);
 
 	if (name != NULL) {
-		int error;
 		if ((error = ensure_remote_name_is_valid(name)) < 0) {
-			git_remote_free(remote);
-			return error;
+			error = GIT_EINVALIDSPEC;
+			goto on_error;
 		}
 
 		remote->name = git__strdup(name);
 		GITERR_CHECK_ALLOC(remote->name);
+
+		/* An empty name indicates to use a sensible default for the fetchspec. */
+		if (fetch && !(*fetch)) {
+			if (git_buf_printf(&fetchbuf, "+refs/heads/*:refs/remotes/%s/*", remote->name) < 0)
+				goto on_error;
+			fetch = git_buf_cstr(&fetchbuf);
+		}
 	}
 
 	if (fetch != NULL) {
@@ -125,11 +133,26 @@ int git_remote_new(git_remote **out, git_repository *repo, const char *name, con
 	}
 
 	*out = remote;
+	git_buf_free(&fetchbuf);
 	return 0;
 
 on_error:
 	git_remote_free(remote);
-	return -1;
+	git_buf_free(&fetchbuf);
+	return error;
+}
+
+int git_remote_set_repository(git_remote *remote, git_repository *repo)
+{
+	assert(repo);
+
+	if (remote->repo) {
+		giterr_set(GITERR_INVALID, "Remotes can't change repositiories.");
+		return GIT_ERROR;
+	}
+
+	remote->repo = repo;
+	return 0;
 }
 
 int git_remote_load(git_remote **out, git_repository *repo, const char *name)
@@ -289,6 +312,11 @@ int git_remote_save(const git_remote *remote)
 
 	assert(remote);
 
+	if (!remote->repo) {
+		giterr_set(GITERR_INVALID, "Can't save a dangling remote.");
+		return GIT_ERROR;
+	}
+
 	if ((error = ensure_remote_name_is_valid(remote->name)) < 0)
 		return error;
 
@@ -543,7 +571,7 @@ int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_ur
 
 	assert(remote);
 
-	if (!proxy_url)
+	if (!proxy_url || !remote->repo)
 		return -1;
 
 	*proxy_url = NULL;
@@ -745,6 +773,11 @@ int git_remote_update_tips(git_remote *remote)
 
 	assert(remote);
 
+	if (!remote->repo) {
+		giterr_set(GITERR_INVALID, "Can't update tips on a dangling remote.");
+		return GIT_ERROR;
+	}
+
 	spec = &remote->fetch;
 	
 	if (git_repository_odb__weakptr(&odb, remote->repo) < 0)
@@ -1293,49 +1326,51 @@ int git_remote_rename(
 
 	assert(remote && new_name);
 
-	if ((error = ensure_remote_doesnot_exist(remote->repo, new_name)) < 0)
-		return error;
-
 	if ((error = ensure_remote_name_is_valid(new_name)) < 0)
 		return error;
 
-	if (!remote->name) {
-		if ((error = rename_fetch_refspecs(
-			remote,
-			new_name,
-			callback,
-			payload)) < 0)
+	if (remote->repo) {
+		if ((error = ensure_remote_doesnot_exist(remote->repo, new_name)) < 0)
 			return error;
 
-		remote->name = git__strdup(new_name);
+		if (!remote->name) {
+			if ((error = rename_fetch_refspecs(
+				remote,
+				new_name,
+				callback,
+				payload)) < 0)
+				return error;
 
-		return git_remote_save(remote);
-	}
+			remote->name = git__strdup(new_name);
 
-	if ((error = rename_remote_config_section(
-		remote->repo,
-		remote->name,
-		new_name)) < 0)
-			return error;
+			return git_remote_save(remote);
+		}
 
-	if ((error = update_branch_remote_config_entry(
-		remote->repo,
-		remote->name,
-		new_name)) < 0)
-			return error;
+		if ((error = rename_remote_config_section(
+			remote->repo,
+			remote->name,
+			new_name)) < 0)
+				return error;
 
-	if ((error = rename_remote_references(
-		remote->repo,
-		remote->name,
-		new_name)) < 0)
-			return error;
+		if ((error = update_branch_remote_config_entry(
+			remote->repo,
+			remote->name,
+			new_name)) < 0)
+				return error;
 
-	if ((error = rename_fetch_refspecs(
-		remote,
-		new_name,
-		callback,
-		payload)) < 0)
-		return error;
+		if ((error = rename_remote_references(
+			remote->repo,
+			remote->name,
+			new_name)) < 0)
+				return error;
+
+		if ((error = rename_fetch_refspecs(
+			remote,
+			new_name,
+			callback,
+			payload)) < 0)
+			return error;
+	}
 
 	git__free(remote->name);
 	remote->name = git__strdup(new_name);
diff --git a/tests-clar/clone/network.c b/tests-clar/clone/network.c
index d519e45..15ad95b 100644
--- a/tests-clar/clone/network.c
+++ b/tests-clar/clone/network.c
@@ -5,14 +5,22 @@
 
 CL_IN_CATEGORY("network")
 
-#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
-#define LIVE_EMPTYREPO_URL "git://github.com/libgit2/TestEmptyRepository"
+#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
+#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
 
 static git_repository *g_repo;
+static git_remote *g_origin;
 
 void test_clone_network__initialize(void)
 {
 	g_repo = NULL;
+	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH));
+}
+
+void test_clone_network__cleanup(void)
+{
+	git_remote_free(g_origin);
+	g_origin = NULL;
 }
 
 static void cleanup_repository(void *path)
@@ -31,7 +39,7 @@ void test_clone_network__network_full(void)
 
 	cl_set_cleanup(&cleanup_repository, "./test2");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./test2", NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, g_origin, "./test2", NULL, NULL, NULL));
 	cl_assert(!git_repository_is_bare(g_repo));
 	cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
 
@@ -45,7 +53,7 @@ void test_clone_network__network_bare(void)
 
 	cl_set_cleanup(&cleanup_repository, "./test");
 
-	cl_git_pass(git_clone_bare(&g_repo, LIVE_REPO_URL, "./test", NULL, NULL));
+	cl_git_pass(git_clone_bare(&g_repo, g_origin, "./test", NULL, NULL));
 	cl_assert(git_repository_is_bare(g_repo));
 	cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
 
@@ -57,7 +65,7 @@ void test_clone_network__cope_with_already_existing_directory(void)
 	cl_set_cleanup(&cleanup_repository, "./foo");
 
 	p_mkdir("./foo", GIT_DIR_MODE);
-	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
 	git_repository_free(g_repo); g_repo = NULL;
 }
 
@@ -67,7 +75,10 @@ void test_clone_network__empty_repository(void)
 
 	cl_set_cleanup(&cleanup_repository, "./empty");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_EMPTYREPO_URL, "./empty", NULL, NULL, NULL));
+	git_remote_free(g_origin);
+	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_EMPTYREPO_URL, GIT_REMOTE_DEFAULT_FETCH));
+
+	cl_git_pass(git_clone(&g_repo, g_origin, "./empty", NULL, NULL, NULL));
 
 	cl_assert_equal_i(true, git_repository_is_empty(g_repo));
 	cl_assert_equal_i(true, git_repository_head_orphan(g_repo));
@@ -85,7 +96,7 @@ void test_clone_network__can_prevent_the_checkout_of_a_standard_repo(void)
 
 	cl_set_cleanup(&cleanup_repository, "./no-checkout");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./no-checkout", NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, g_origin, "./no-checkout", NULL, NULL, NULL));
 
 	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
 	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path)));
@@ -121,7 +132,7 @@ void test_clone_network__can_checkout_a_cloned_repo(void)
 
 	cl_set_cleanup(&cleanup_repository, "./default-checkout");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./default-checkout", &opts,
+	cl_git_pass(git_clone(&g_repo, g_origin, "./default-checkout", &opts,
 				&fetch_progress, &fetch_progress_cb_was_called));
 
 	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c
index 8432708..17a7a36 100644
--- a/tests-clar/clone/nonetwork.c
+++ b/tests-clar/clone/nonetwork.c
@@ -6,10 +6,18 @@
 #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
 
 static git_repository *g_repo;
+static git_remote *g_origin = NULL;
 
 void test_clone_nonetwork__initialize(void)
 {
 	g_repo = NULL;
+	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", cl_git_fixture_url("testrepo.git"), GIT_REMOTE_DEFAULT_FETCH));
+}
+
+void test_clone_nonetwork__cleanup(void)
+{
+	git_remote_free(g_origin);
+	g_origin = NULL;
 }
 
 static void cleanup_repository(void *path)
@@ -25,18 +33,20 @@ static void cleanup_repository(void *path)
 void test_clone_nonetwork__bad_url(void)
 {
 	/* Clone should clean up the mess if the URL isn't a git repository */
-	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", NULL, NULL, NULL));
+	git_remote_free(g_origin);
+	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", "not_a_repo", GIT_REMOTE_DEFAULT_FETCH));
+
+	cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
 	cl_assert(!git_path_exists("./foo"));
-	cl_git_fail(git_clone_bare(&g_repo, "not_a_repo", "./foo.git", NULL, NULL));
+	cl_git_fail(git_clone_bare(&g_repo, g_origin, "./foo.git", NULL, NULL));
 	cl_assert(!git_path_exists("./foo.git"));
 }
 
 void test_clone_nonetwork__local(void)
 {
-	const char *src = cl_git_fixture_url("testrepo.git");
 	cl_set_cleanup(&cleanup_repository, "./local");
 
-	cl_git_pass(git_clone(&g_repo, src, "./local", NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, g_origin, "./local", NULL, NULL, NULL));
 }
 
 void test_clone_nonetwork__local_absolute_path(void)
@@ -49,10 +59,9 @@ void test_clone_nonetwork__local_absolute_path(void)
 
 void test_clone_nonetwork__local_bare(void)
 {
-	const char *src = cl_git_fixture_url("testrepo.git");
 	cl_set_cleanup(&cleanup_repository, "./local.git");
 
-	cl_git_pass(git_clone_bare(&g_repo, src, "./local.git", NULL, NULL));
+	cl_git_pass(git_clone_bare(&g_repo, g_origin, "./local.git", NULL, NULL));
 }
 
 void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
@@ -60,7 +69,7 @@ void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
 	cl_set_cleanup(&cleanup_repository, "./foo");
 
 	cl_git_mkfile("./foo", "Bar!");
-	cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL, NULL, NULL));
+	cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
 }
 
 void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
@@ -69,5 +78,5 @@ void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(vo
 
 	p_mkdir("./foo", GIT_DIR_MODE);
 	cl_git_mkfile("./foo/bar", "Baz!");
-	cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL, NULL, NULL));
+	cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
 }
diff --git a/tests-clar/fetchhead/network.c b/tests-clar/fetchhead/network.c
index 46cb977..aed5b5a 100644
--- a/tests-clar/fetchhead/network.c
+++ b/tests-clar/fetchhead/network.c
@@ -10,10 +10,17 @@ CL_IN_CATEGORY("network")
 #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
 
 static git_repository *g_repo;
+static git_remote *g_origin;
 
 void test_fetchhead_network__initialize(void)
 {
 	g_repo = NULL;
+	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH));
+}
+
+void test_fetchhead_network__cleanup(void)
+{
+	g_origin = NULL;
 }
 
 static void cleanup_repository(void *path)
@@ -31,7 +38,7 @@ static void fetchhead_test_clone(void)
 {
 	cl_set_cleanup(&cleanup_repository, "./test1");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./test1", NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, g_origin, "./test1", NULL, NULL, NULL));
 }
 
 static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead)
diff --git a/tests-clar/network/fetch.c b/tests-clar/network/fetch.c
index a5e2c02..246435b 100644
--- a/tests-clar/network/fetch.c
+++ b/tests-clar/network/fetch.c
@@ -89,7 +89,8 @@ void test_network_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_dat
 	git_remote *remote;
 	bool invoked = false;
 
-	cl_git_pass(git_clone_bare(&_repository, "https://github.com/libgit2/TestGitRepository.git", "./fetch/lg2", NULL, NULL));
+	cl_git_pass(git_remote_new(&remote, NULL, "origin", "https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DEFAULT_FETCH));
+	cl_git_pass(git_clone_bare(&_repository, remote, "./fetch/lg2", NULL, NULL));
 	git_repository_free(_repository);
 
 	cl_git_pass(git_repository_open(&_repository, "./fetch/lg2"));
diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c
index 70df001..b2ed8e7 100644
--- a/tests-clar/network/remotes.c
+++ b/tests-clar/network/remotes.c
@@ -326,3 +326,17 @@ void test_network_remotes__check_structure_version(void)
 	err = giterr_last();
 	cl_assert_equal_i(GITERR_INVALID, err->klass);
 }
+
+void test_network_remotes__dangling(void)
+{
+	cl_git_pass(git_remote_new(&_remote, NULL, "upstream", "git://github.com/libgit2/libgit2", NULL));
+
+	cl_git_pass(git_remote_rename(_remote, "newname", NULL, NULL));
+	cl_assert_equal_s(git_remote_name(_remote), "newname");
+
+	cl_git_fail(git_remote_save(_remote));
+	cl_git_fail(git_remote_update_tips(_remote));
+
+	cl_git_pass(git_remote_set_repository(_remote, _repo));
+	cl_git_pass(git_remote_save(_remote));
+}