Commit 15d3c221e34cad474e87c43111417186822c887c

Stefan Sperling 2021-01-05T13:47:37

make 'got clone' pin the fetched branch in got.conf(5) Avoids relying on the server-side HEAD ref by default during future fetches.

diff --git a/got/got.1 b/got/got.1
index 0d05ef4..84e6393 100644
--- a/got/got.1
+++ b/got/got.1
@@ -193,6 +193,8 @@ and
 .Pa config
 files of the cloned repository to store the
 .Ar repository-url
+and
+.Ar branch
 for future use by
 .Cm got fetch
 or
diff --git a/got/got.c b/got/got.c
index 992f88f..1a0fbb7 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1163,15 +1163,22 @@ create_wanted_ref(const char *refname, struct got_object_id *id,
 
 static const struct got_error *
 create_gotconfig(const char *proto, const char *host, const char *port,
-    const char *remote_repo_path, int fetch_all_branches, int mirror_references,
-    struct got_repository *repo)
+    const char *remote_repo_path, const char *default_branch,
+    int fetch_all_branches, int mirror_references, struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	char *gotconfig_path = NULL;
 	char *gotconfig = NULL;
 	FILE *gotconfig_file = NULL;
+	const char *branchname = NULL;
 	ssize_t n;
 
+	if (default_branch) {
+		branchname = default_branch;
+		if (strncmp(branchname, "refs/heads/", 11) == 0)
+			branchname += 11;
+	}
+
 	/* Create got.conf(5). */
 	gotconfig_path = got_repo_get_path_gotconfig(repo);
 	if (gotconfig_path == NULL) {
@@ -1189,11 +1196,15 @@ create_gotconfig(const char *proto, const char *host, const char *port,
 	    "\tprotocol %s\n"
 	    "%s%s%s"
 	    "\trepository \"%s\"\n"
+	    "%s%s%s"
 	    "%s"
 	    "}\n",
 	    GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
 	    port ? "\tport " : "", port ? port : "", port ? "\n" : "",
 	    remote_repo_path,
+	    branchname ? "\tbranch { \"" : "",
+	    branchname ? branchname : "", 
+	    branchname ? "\" }\n" : "", 
 	    mirror_references ? "\tmirror-references yes\n" : "") == -1) {
 		err = got_error_from_errno("asprintf");
 		goto done;
@@ -1321,7 +1332,7 @@ create_config_files(const char *proto, const char *host, const char *port,
 
 	/* Create got.conf(5). */
 	err = create_gotconfig(proto, host, port, remote_repo_path,
-	    fetch_all_branches, mirror_references, repo);
+	    default_branch, fetch_all_branches, mirror_references, repo);
 	if (err)
 		return err;
 
diff --git a/regress/cmdline/clone.sh b/regress/cmdline/clone.sh
index 090d275..256c4fc 100755
--- a/regress/cmdline/clone.sh
+++ b/regress/cmdline/clone.sh
@@ -94,6 +94,7 @@ remote "origin" {
 	server 127.0.0.1
 	protocol ssh
 	repository "$testroot/repo"
+	branch { "master" }
 }
 EOF
 	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
@@ -193,6 +194,7 @@ remote "origin" {
 	server 127.0.0.1
 	protocol ssh
 	repository "$testroot/repo"
+	branch { "foo" }
 }
 EOF
 	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
@@ -268,6 +270,7 @@ remote "origin" {
 	server 127.0.0.1
 	protocol ssh
 	repository "$testroot/repo"
+	branch { "master" }
 }
 EOF
 	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
@@ -338,6 +341,7 @@ remote "origin" {
 	server 127.0.0.1
 	protocol ssh
 	repository "$testroot/repo"
+	branch { "master" }
 	mirror-references yes
 }
 EOF
@@ -410,6 +414,7 @@ remote "origin" {
 	server 127.0.0.1
 	protocol ssh
 	repository "$testroot/repo"
+	branch { "master" }
 	mirror-references yes
 }
 EOF
@@ -486,6 +491,7 @@ remote "origin" {
 	server 127.0.0.1
 	protocol ssh
 	repository "$testroot/repo"
+	branch { "master" }
 }
 EOF
 	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
@@ -558,6 +564,7 @@ remote "origin" {
 	server 127.0.0.1
 	protocol ssh
 	repository "$testroot/repo"
+	branch { "foo" }
 }
 EOF
 	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
@@ -627,6 +634,7 @@ remote "origin" {
 	server 127.0.0.1
 	protocol ssh
 	repository "$testroot/repo"
+	branch { "master" }
 	mirror-references yes
 }
 EOF
diff --git a/regress/cmdline/fetch.sh b/regress/cmdline/fetch.sh
index 86e2e5c..1fc5c95 100755
--- a/regress/cmdline/fetch.sh
+++ b/regress/cmdline/fetch.sh
@@ -874,6 +874,25 @@ test_fetch_update_headref() {
 	got ref -l -r $testroot/repo-clone > $testroot/stdout
 
 	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got fetch -q -r $testroot/repo-clone -a
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
 	echo "refs/heads/foo: $commit_id" >> $testroot/stdout.expected
 	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
 	echo "refs/remotes/origin/HEAD: refs/remotes/origin/foo" \