store all branches passed via 'got clone -b' in got.conf(5) and git-config(1)
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
diff --git a/got/got.1 b/got/got.1
index 8560ea5..02dba06 100644
--- a/got/got.1
+++ b/got/got.1
@@ -195,7 +195,7 @@ files of the cloned repository to store the
.Ar repository-url
and
.Ar branch
-for future use by
+arguments for future use by
.Cm got fetch
or
.Xr git-fetch 1 .
diff --git a/got/got.c b/got/got.c
index dc1e82d..4c56b96 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1164,19 +1164,40 @@ 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, const char *default_branch,
- int fetch_all_branches, int mirror_references, struct got_repository *repo)
+ struct got_pathlist_head *wanted_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;
+ char *branches = NULL;
ssize_t n;
- if (default_branch) {
+ if (!TAILQ_EMPTY(wanted_branches)) {
+ struct got_pathlist_entry *pe;
+ TAILQ_FOREACH(pe, wanted_branches, entry) {
+ char *s;
+ branchname = pe->path;
+ if (strncmp(branchname, "refs/heads/", 11) == 0)
+ branchname += 11;
+ if (asprintf(&s, "%s\"%s\" ",
+ branches ? branches : "", branchname) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+ free(branches);
+ branches = s;
+ }
+ } else if (default_branch) {
branchname = default_branch;
if (strncmp(branchname, "refs/heads/", 11) == 0)
branchname += 11;
+ if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
}
/* Create got.conf(5). */
@@ -1201,10 +1222,8 @@ create_gotconfig(const char *proto, const char *host, const char *port,
"}\n",
GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
port ? "\tport " : "", port ? port : "", port ? "\n" : "",
- remote_repo_path,
- branchname ? "\tbranch { \"" : "",
- branchname ? branchname : "",
- branchname ? "\" }\n" : "",
+ remote_repo_path, branches ? "\tbranch { " : "",
+ branches ? branches : "", branches ? "}\n" : "",
mirror_references ? "\tmirror-references yes\n" : "") == -1) {
err = got_error_from_errno("asprintf");
goto done;
@@ -1219,17 +1238,21 @@ done:
if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
err = got_error_from_errno2("fclose", gotconfig_path);
free(gotconfig_path);
+ free(branches);
return err;
}
static const struct got_error *
create_gitconfig(const char *git_url, const char *default_branch,
- int fetch_all_branches, int mirror_references, struct got_repository *repo)
+ int fetch_all_branches, struct got_pathlist_head *wanted_branches,
+ int mirror_references, struct got_repository *repo)
{
const struct got_error *err = NULL;
char *gitconfig_path = NULL;
char *gitconfig = NULL;
FILE *gitconfig_file = NULL;
+ char *branches = NULL;
+ const char *branchname, *mirror = NULL;
ssize_t n;
/* Create a config file Git can understand. */
@@ -1244,28 +1267,38 @@ create_gitconfig(const char *git_url, const char *default_branch,
goto done;
}
if (mirror_references) {
- if (asprintf(&gitconfig,
- "[remote \"%s\"]\n"
- "\turl = %s\n"
- "\tfetch = +refs/*:refs/*\n"
- "\tmirror = true\n",
- GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
- err = got_error_from_errno("asprintf");
+ mirror = "\tmirror = true\n";
+ branches = strdup("\tfetch = +refs/*:refs/*\n");
+ if (branches == NULL) {
+ err = got_error_from_errno("strdup");
goto done;
}
} else if (fetch_all_branches) {
- if (asprintf(&gitconfig,
- "[remote \"%s\"]\n"
- "\turl = %s\n"
+ if (asprintf(&branches,
"\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
- GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
+ } else if (!TAILQ_EMPTY(wanted_branches)) {
+ struct got_pathlist_entry *pe;
+ TAILQ_FOREACH(pe, wanted_branches, entry) {
+ char *s;
+ branchname = pe->path;
+ if (strncmp(branchname, "refs/heads/", 11) == 0)
+ branchname += 11;
+ if (asprintf(&s,
+ "%s\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
+ branches ? branches : "",
+ branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
+ branchname) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+ free(branches);
+ branches = s;
+ }
} else {
- const char *branchname;
-
/*
* If the server specified a default branch, use just that one.
* Otherwise fall back to fetching all branches on next fetch.
@@ -1276,17 +1309,24 @@ create_gitconfig(const char *git_url, const char *default_branch,
branchname += 11;
} else
branchname = "*"; /* fall back to all branches */
- if (asprintf(&gitconfig,
- "[remote \"%s\"]\n"
- "\turl = %s\n"
+ if (asprintf(&branches,
"\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
- GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
branchname) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
}
+ if (asprintf(&gitconfig,
+ "[remote \"%s\"]\n"
+ "\turl = %s\n"
+ "%s"
+ "%s",
+ GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
+ mirror ? mirror : "") == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
if (n != strlen(gitconfig)) {
err = got_ferror(gitconfig_file, GOT_ERR_IO);
@@ -1296,6 +1336,7 @@ done:
if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
err = got_error_from_errno2("fclose", gitconfig_path);
free(gitconfig_path);
+ free(branches);
return err;
}
@@ -1332,13 +1373,13 @@ 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,
- default_branch, fetch_all_branches, mirror_references, repo);
+ default_branch, wanted_branches, mirror_references, repo);
if (err)
return err;
/* Create a config file Git can understand. */
return create_gitconfig(git_url, default_branch, fetch_all_branches,
- mirror_references, repo);
+ wanted_branches, mirror_references, repo);
}
static const struct got_error *
diff --git a/regress/cmdline/clone.sh b/regress/cmdline/clone.sh
index 256c4fc..cf7f3ac 100755
--- a/regress/cmdline/clone.sh
+++ b/regress/cmdline/clone.sh
@@ -667,6 +667,77 @@ EOF
test_done "$testroot" "$ret"
}
+test_clone_multiple_branches() {
+ local testroot=`test_init clone_multiple_branches`
+ local testurl=ssh://127.0.0.1/$testroot
+ local commit_id=`git_show_head $testroot/repo`
+
+ got branch -r $testroot/repo -c $commit_id foo
+ got branch -r $testroot/repo -c $commit_id bar
+
+ got clone -q -b foo -b bar $testurl/repo $testroot/repo-clone
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got clone command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -l -r $testroot/repo-clone > $testroot/stdout
+
+ echo "HEAD: refs/heads/foo" > $testroot/stdout.expected
+ echo "refs/heads/bar: $commit_id" >> $testroot/stdout.expected
+ echo "refs/heads/foo: $commit_id" >> $testroot/stdout.expected
+ echo "refs/remotes/origin/bar: $commit_id" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/foo: $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
+
+ cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+ server 127.0.0.1
+ protocol ssh
+ repository "$testroot/repo"
+ branch { "foo" "bar" }
+}
+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
+ fetch = +refs/heads/bar:refs/remotes/origin/bar
+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"
+}
+
test_parseargs "$@"
run_test test_clone_basic
run_test test_clone_list
@@ -677,3 +748,4 @@ run_test test_clone_mirror_all
run_test test_clone_reference
run_test test_clone_branch_and_reference
run_test test_clone_reference_mirror
+run_test test_clone_multiple_branches