shithub: git9

Download patch

ref: e1e1a6cb1b310dbd811c7ebbd167811f5300d597
parent: b97f32fc695bf757b4ad0db22d0286e36751fa54
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Feb 22 23:29:51 EST 2021

git/clone: allow cloning a single branch.

Sometimes you only need one branch from a repo. This
was a pain in the ass before, involving initializing
the repository, pulling to get the hashes, and then
manually creating the branch because the empty repo
had no valid branches, so branch switchnig broke.

The change to support this was trivial, so why not.

--- a/clone
+++ b/clone
@@ -2,7 +2,7 @@
 rfork en
 . /sys/lib/git/common.rc
 
-flagfmt='d:debug'; args='remote [local]'
+flagfmt='d:debug, b:branch branch'; args='remote [local]'
 eval `''{aux/getflags $*} || exec aux/usage
 if(~ $debug 1)
 	debug=(-d)
@@ -14,6 +14,8 @@
 	exec aux/usage
 if(~ $#local 0)
 	local=`{basename $remote .git}
+if(~ $#branch 1)
+	branchflag=(-b $branch)
 
 if(test -e $local)
 	die 'repository already exists:' $local
@@ -29,18 +31,16 @@
 	>>.git/config {
 		echo '[remote "origin"]'
 		echo '	url='$remote
-		echo '	fetch=+refs/heads/*:refs/remotes/origin/*'
 	}
-	{git/fetch $debug $remote >[2=3] | awk '
+	{git/fetch  $debug $branchflag $remote >[2=3] | awk '
 		BEGIN{
-			headref=""
+			headref="refs/remotes/origin/"ENVIRON["branch"]
 			headhash=""
 		}
-		/^symref /{
+		/^symref / && headref == "" {
 			if($2 == "HEAD"){
 				gsub("^refs/heads", "refs/remotes/origin", $3)
 				gsub("^refs/tags", "refs/remotes/origin/tags", $3)
-				headref=$3
 			}
 		}
 		/^remote /{
@@ -92,7 +92,7 @@
 		}
 	}
 	if not{
-		echo no master branch >[1=2]
+		echo no default branch >[1=2]
 		echo check out your code with git/branch >[1=2]
 	}
 }