Commit Diff


commit - 5e08262609e4b99fae21c63ff12d7c9cbfc981e2
commit + a9c2d4c277afebe2aff309e7b43a32185fc37cd1
blob - 16044da028a60088852c19c49c0f5e341d3658b2
blob + 3cdf1de314061058b3bc30f1516b7688040143bf
--- got/got.c
+++ got/got.c
@@ -923,18 +923,17 @@ fetch_progress(void *arg, const char *message, off_t p
 }
 
 static const struct got_error *
-create_symref(const char *refname, struct got_reference *target_ref,
-    int verbosity, struct got_repository *repo)
+create_symref(struct got_reference **head_symref, const char *refname,
+    struct got_reference *target_ref, int verbosity,
+    struct got_repository *repo)
 {
 	const struct got_error *err;
-	struct got_reference *head_symref;
 
-	err = got_ref_alloc_symref(&head_symref, refname, target_ref);
+	err = got_ref_alloc_symref(head_symref, refname, target_ref);
 	if (err)
 		return err;
 
-	err = got_ref_write(head_symref, repo);
-	got_ref_close(head_symref);
+	err = got_ref_write(*head_symref, repo);
 	if (err == NULL && verbosity > 0) {
 		printf("Created reference %s: %s\n", GOT_REF_HEAD,
 		    got_ref_get_name(target_ref));
@@ -1063,7 +1062,7 @@ cmd_clone(int argc, char *argv[])
 	const char *uri, *dirname;
 	char *proto, *host, *port, *repo_name, *server_path;
 	char *default_destdir = NULL, *id_str = NULL;
-	const char *repo_path, *remote_repo_path;
+	const char *repo_path;
 	struct got_repository *repo = NULL;
 	struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
 	struct got_pathlist_entry *pe;
@@ -1078,7 +1077,7 @@ cmd_clone(int argc, char *argv[])
 	ssize_t n;
 	int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
 	int list_refs_only = 0;
-	struct got_reference *head_symref = NULL;
+	struct got_reference *default_head = NULL;
 
 	TAILQ_INIT(&refs);
 	TAILQ_INIT(&symrefs);
@@ -1301,6 +1300,7 @@ cmd_clone(int argc, char *argv[])
 		const char *refname = pe->path;
 		const char *target = pe->data;
 		char *remote_refname = NULL, *remote_target = NULL;
+		struct got_reference *head_symref;
 
 		if (strcmp(refname, GOT_REF_HEAD) != 0)
 			continue;
@@ -1314,11 +1314,18 @@ cmd_clone(int argc, char *argv[])
 			goto done;
 		}
 
-		error = create_symref(refname, target_ref, verbosity, repo);
+		error = create_symref(&head_symref, refname, target_ref,
+		    verbosity, repo);
 		got_ref_close(target_ref);
 		if (error)
 			goto done;
 
+		/* First HEAD reference listed is the default branch. */
+		if (default_head == NULL)
+			default_head = head_symref;
+		else
+			got_ref_close(head_symref);
+
 		if (mirror_references)
 			continue;
 
@@ -1348,11 +1355,12 @@ cmd_clone(int argc, char *argv[])
 			}
 			goto done;
 		}
-		error = create_symref(remote_refname, target_ref,
-		    verbosity - 1, repo);
+		error = create_symref(&head_symref, remote_refname,
+		    target_ref, verbosity - 1, repo);
 		free(remote_refname);
 		free(remote_target);
 		got_ref_close(target_ref);
+		got_ref_close(head_symref);
 		if (error)
 			goto done;
 	}
@@ -1375,8 +1383,8 @@ cmd_clone(int argc, char *argv[])
 				goto done;
 			}
 
-			error = create_symref(GOT_REF_HEAD, target_ref,
-			    verbosity, repo);
+			error = create_symref(&default_head, GOT_REF_HEAD,
+			    target_ref, verbosity, repo);
 			got_ref_close(target_ref);
 			if (error)
 				goto done;
@@ -1396,9 +1404,6 @@ cmd_clone(int argc, char *argv[])
 		goto done;
 	}
 	got_path_strip_trailing_slashes(server_path);
-	remote_repo_path = server_path;
-	while (remote_repo_path[0] == '/')
-		remote_repo_path++;
 	if (asprintf(&gotconfig,
 	    "remote \"%s\" {\n"
 	    "\tserver %s\n"
@@ -1409,7 +1414,7 @@ cmd_clone(int argc, char *argv[])
 	    "}\n",
 	    GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
 	    port ? "\tport " : "", port ? port : "", port ? "\n" : "",
-	    remote_repo_path,
+	    server_path,
 	    mirror_references ? "\tmirror-references yes\n" : "") == -1) {
 		error = got_error_from_errno("asprintf");
 		goto done;
@@ -1458,8 +1463,8 @@ cmd_clone(int argc, char *argv[])
 		 * If the server specified a default branch, use just that one.
 		 * Otherwise fall back to fetching all branches on next fetch.
 		 */
-		if (head_symref) {
-			branchname = got_ref_get_symref_target(head_symref);
+		if (default_head) {
+			branchname = got_ref_get_symref_target(default_head);
 			if (strncmp(branchname, "refs/heads/", 11) == 0)
 				branchname += 11;
 		} else
@@ -1499,8 +1504,8 @@ done:
 		error = got_error_from_errno("fclose");
 	if (repo)
 		got_repo_close(repo);
-	if (head_symref)
-		got_ref_close(head_symref);
+	if (default_head)
+		got_ref_close(default_head);
 	TAILQ_FOREACH(pe, &refs, entry) {
 		free((void *)pe->path);
 		free(pe->data);
blob - 7ad2a2f1a59a577580c1f8c552f7c07b5cbd9b76
blob + 090d275c54ed17e7ed475a292bfd074d1b4cec35
--- regress/cmdline/clone.sh
+++ regress/cmdline/clone.sh
@@ -85,7 +85,42 @@ test_clone_basic() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
 	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/master:refs/remotes/origin/master
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
+	fi
 	test_done "$testroot" "$ret"
 }
 
@@ -149,7 +184,42 @@ test_clone_branch() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
 	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/foo:refs/remotes/origin/foo
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
+	fi
 	test_done "$testroot" "$ret"
 }
 
@@ -189,7 +259,42 @@ test_clone_all() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
 	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/*:refs/remotes/origin/*
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
+	fi
 	test_done "$testroot" "$ret"
 }
 
@@ -224,7 +329,44 @@ test_clone_mirror() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
 	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+	mirror-references yes
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/*:refs/*
+	mirror = true
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
+	fi
 	test_done "$testroot" "$ret"
 }
 
@@ -259,7 +401,44 @@ test_clone_mirror_all() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
 	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+	mirror-references yes
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/*:refs/*
+	mirror = true
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
+	fi
 	test_done "$testroot" "$ret"
 }
 
@@ -298,7 +477,42 @@ test_clone_reference() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
 	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/master:refs/remotes/origin/master
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
+	fi
 	test_done "$testroot" "$ret"
 }
 
@@ -335,7 +549,42 @@ test_clone_branch_and_reference() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
 	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/foo:refs/remotes/origin/foo
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
+	fi
 	test_done "$testroot" "$ret"
 }
 
@@ -369,7 +618,44 @@ test_clone_reference_mirror() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
 	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+	mirror-references yes
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/*:refs/*
+	mirror = true
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
+	fi
 	test_done "$testroot" "$ret"
 }