allow an interrupted 'got clone' to be resumed by 'got fetch' Set up config files as soon as possible such that 'got fetch' has enough information in order to resume an interrupted clone operation. We need to receive at least a default HEAD branch name from the server before we can set up our configuration files. The previous code also verified whether the default branch name supplied by the server resolved to a commit received in the pack file. This check cannot be done early but a wrong branch name in a config file can be fixed easily.
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
diff --git a/got/got.c b/got/got.c
index 7173a47..cf23cb0 100644
--- a/got/got.c
+++ b/got/got.c
@@ -859,17 +859,62 @@ struct got_fetch_progress_arg {
int last_p_indexed;
int last_p_resolved;
int verbosity;
+
+ struct got_repository *repo;
+
+ int create_configs;
+ int configs_created;
+ struct {
+ struct got_pathlist_head *symrefs;
+ struct got_pathlist_head *wanted_branches;
+ const char *proto;
+ const char *host;
+ const char *port;
+ const char *remote_repo_path;
+ const char *git_url;
+ int fetch_all_branches;
+ int mirror_references;
+ } config_info;
};
+/* XXX forward declaration */
+static const struct got_error *
+create_config_files(const char *proto, const char *host, const char *port,
+ const char *remote_repo_path, const char *git_url, int fetch_all_branches,
+ int mirror_references, struct got_pathlist_head *symrefs,
+ struct got_pathlist_head *wanted_branches, struct got_repository *repo);
+
static const struct got_error *
fetch_progress(void *arg, const char *message, off_t packfile_size,
int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
{
+ const struct got_error *err = NULL;
struct got_fetch_progress_arg *a = arg;
char scaled_size[FMT_SCALED_STRSIZE];
int p_indexed, p_resolved;
int print_size = 0, print_indexed = 0, print_resolved = 0;
+ /*
+ * In order to allow a failed clone to be resumed with 'got fetch'
+ * we try to create configuration files as soon as possible.
+ * Once the server has sent information about its default branch
+ * we have all required information.
+ */
+ if (a->create_configs && !a->configs_created &&
+ !TAILQ_EMPTY(a->config_info.symrefs)) {
+ err = create_config_files(a->config_info.proto,
+ a->config_info.host, a->config_info.port,
+ a->config_info.remote_repo_path,
+ a->config_info.git_url,
+ a->config_info.fetch_all_branches,
+ a->config_info.mirror_references,
+ a->config_info.symrefs,
+ a->config_info.wanted_branches, a->repo);
+ if (err)
+ return err;
+ a->configs_created = 1;
+ }
+
if (a->verbosity < 0)
return NULL;
@@ -923,21 +968,22 @@ fetch_progress(void *arg, const char *message, off_t packfile_size,
}
static const struct got_error *
-create_symref(struct got_reference **head_symref, const char *refname,
- struct got_reference *target_ref, int verbosity,
- struct got_repository *repo)
+create_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);
+ 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));
}
+ got_ref_close(head_symref);
return err;
}
@@ -1057,7 +1103,7 @@ 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,
- char *remote_repo_path, int fetch_all_branches, int mirror_references,
+ const char *remote_repo_path, int fetch_all_branches, int mirror_references,
struct got_repository *repo)
{
const struct got_error *err = NULL;
@@ -1077,7 +1123,6 @@ create_gotconfig(const char *proto, const char *host, const char *port,
err = got_error_from_errno2("fopen", gotconfig_path);
goto done;
}
- got_path_strip_trailing_slashes(remote_repo_path);
if (asprintf(&gotconfig,
"remote \"%s\" {\n"
"\tserver %s\n"
@@ -1107,7 +1152,7 @@ done:
}
static const struct got_error *
-create_gitconfig(const char *git_url, struct got_reference *default_head,
+create_gitconfig(const char *git_url, const char *default_branch,
int fetch_all_branches, int mirror_references, struct got_repository *repo)
{
const struct got_error *err = NULL;
@@ -1154,8 +1199,8 @@ create_gitconfig(const char *git_url, struct got_reference *default_head,
* If the server specified a default branch, use just that one.
* Otherwise fall back to fetching all branches on next fetch.
*/
- if (default_head) {
- branchname = got_ref_get_symref_target(default_head);
+ if (default_branch) {
+ branchname = default_branch;
if (strncmp(branchname, "refs/heads/", 11) == 0)
branchname += 11;
} else
@@ -1184,6 +1229,48 @@ done:
}
static const struct got_error *
+create_config_files(const char *proto, const char *host, const char *port,
+ const char *remote_repo_path, const char *git_url, int fetch_all_branches,
+ int mirror_references, struct got_pathlist_head *symrefs,
+ struct got_pathlist_head *wanted_branches, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ const char *default_branch = NULL;
+ struct got_pathlist_entry *pe;
+
+ /*
+ * If we asked for a set of wanted branches then use the first
+ * one of those.
+ */
+ if (!TAILQ_EMPTY(wanted_branches)) {
+ pe = TAILQ_FIRST(wanted_branches);
+ default_branch = pe->path;
+ } else {
+ /* First HEAD ref listed by server is the default branch. */
+ TAILQ_FOREACH(pe, symrefs, entry) {
+ const char *refname = pe->path;
+ const char *target = pe->data;
+
+ if (strcmp(refname, GOT_REF_HEAD) != 0)
+ continue;
+
+ default_branch = target;
+ break;
+ }
+ }
+
+ /* Create got.conf(5). */
+ err = create_gotconfig(proto, host, port, remote_repo_path,
+ fetch_all_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);
+}
+
+static const struct got_error *
cmd_clone(int argc, char *argv[])
{
const struct got_error *error = NULL;
@@ -1201,7 +1288,6 @@ cmd_clone(int argc, char *argv[])
char *git_url = NULL;
int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
int list_refs_only = 0;
- struct got_reference *default_head = NULL;
TAILQ_INIT(&refs);
TAILQ_INIT(&symrefs);
@@ -1360,6 +1446,18 @@ cmd_clone(int argc, char *argv[])
fpa.last_p_indexed = -1;
fpa.last_p_resolved = -1;
fpa.verbosity = verbosity;
+ fpa.create_configs = 1;
+ fpa.configs_created = 0;
+ fpa.repo = repo;
+ fpa.config_info.symrefs = &symrefs;
+ fpa.config_info.wanted_branches = &wanted_branches;
+ fpa.config_info.proto = proto;
+ fpa.config_info.host = host;
+ fpa.config_info.port = port;
+ fpa.config_info.remote_repo_path = server_path;
+ fpa.config_info.git_url = git_url;
+ fpa.config_info.fetch_all_branches = fetch_all_branches;
+ fpa.config_info.mirror_references = mirror_references;
error = got_fetch_pack(&pack_hash, &refs, &symrefs,
GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
fetch_all_branches, &wanted_branches, &wanted_refs,
@@ -1424,7 +1522,6 @@ 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;
@@ -1438,18 +1535,11 @@ cmd_clone(int argc, char *argv[])
goto done;
}
- error = create_symref(&head_symref, refname, target_ref,
- verbosity, repo);
+ error = create_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;
@@ -1479,12 +1569,11 @@ cmd_clone(int argc, char *argv[])
}
goto done;
}
- error = create_symref(&head_symref, remote_refname,
- target_ref, verbosity - 1, repo);
+ error = create_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;
}
@@ -1507,8 +1596,8 @@ cmd_clone(int argc, char *argv[])
goto done;
}
- error = create_symref(&default_head, GOT_REF_HEAD,
- target_ref, verbosity, repo);
+ error = create_symref(GOT_REF_HEAD, target_ref,
+ verbosity, repo);
got_ref_close(target_ref);
if (error)
goto done;
@@ -1516,18 +1605,6 @@ cmd_clone(int argc, char *argv[])
}
}
- /* Create got.conf(5). */
- error = create_gotconfig(proto, host, port, server_path,
- fetch_all_branches, mirror_references, repo);
- if (error)
- goto done;
-
- /* Create a config file Git can understand. */
- error = create_gitconfig(git_url, default_head, fetch_all_branches,
- mirror_references, repo);
- if (error)
- goto done;
-
if (verbosity >= 0)
printf("Created %s repository '%s'\n",
mirror_references ? "mirrored" : "cloned", repo_path);
@@ -1542,8 +1619,6 @@ done:
error = got_error_from_errno("close");
if (repo)
got_repo_close(repo);
- if (default_head)
- got_ref_close(default_head);
TAILQ_FOREACH(pe, &refs, entry) {
free((void *)pe->path);
free(pe->data);
@@ -2072,6 +2147,10 @@ cmd_fetch(int argc, char *argv[])
fpa.last_p_indexed = -1;
fpa.last_p_resolved = -1;
fpa.verbosity = verbosity;
+ fpa.repo = repo;
+ fpa.create_configs = 0;
+ fpa.configs_created = 0;
+ memset(&fpa.config_info, 0, sizeof(fpa.config_info));
error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
remote->mirror_references, fetch_all_branches, &wanted_branches,
&wanted_refs, list_refs_only, verbosity, fetchfd, repo,