Commit 4aa547dbcb2bafca9102632f6594b7854a57c643

Stefan Sperling 2020-03-19T17:23:24

create git config file earlier such that interrupted clones can be re-fetched

diff --git a/got/got.c b/got/got.c
index 71c7b6e..9981ae6 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1011,6 +1011,32 @@ cmd_clone(int argc, char *argv[])
 	if (verbosity >= 0)
 		printf("Connected to %s:%s\n", host, port);
 
+	/* Create a config file git-fetch(1) can understand. */
+	gitconfig_path = got_repo_get_path_gitconfig(repo);
+	if (gitconfig_path == NULL) {
+		error = got_error_from_errno("got_repo_get_path_gitconfig");
+		goto done;
+	}
+	gitconfig_file = fopen(gitconfig_path, "a");
+	if (gitconfig_file == NULL) {
+		error = got_error_from_errno2("fopen", gitconfig_path);
+		goto done;
+	}
+	if (asprintf(&gitconfig,
+	    "[remote \"%s\"]\n"
+	    "\turl = %s\n"
+	    "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
+	    GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
+	    GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
+		error = got_error_from_errno("asprintf");
+		goto done;
+	}
+	n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
+	if (n != strlen(gitconfig)) {
+		error = got_ferror(gitconfig_file, GOT_ERR_IO);
+		goto done;
+	}
+
 	fpa.last_scaled_size[0] = '\0';
 	fpa.last_p_indexed = -1;
 	fpa.last_p_resolved = -1;
@@ -1091,32 +1117,6 @@ cmd_clone(int argc, char *argv[])
 		break;
 	}
 
-	/* Create a config file so Git can understand this repository. */
-	gitconfig_path = got_repo_get_path_gitconfig(repo);
-	if (gitconfig_path == NULL) {
-		error = got_error_from_errno("got_repo_get_path_gitconfig");
-		goto done;
-	}
-	gitconfig_file = fopen(gitconfig_path, "a");
-	if (gitconfig_file == NULL) {
-		error = got_error_from_errno2("fopen", gitconfig_path);
-		goto done;
-	}
-	if (asprintf(&gitconfig,
-	    "[remote \"%s\"]\n"
-	    "\turl = %s\n"
-	    "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
-	    GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
-	    GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
-		error = got_error_from_errno("asprintf");
-		goto done;
-	}
-	n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
-	if (n != strlen(gitconfig)) {
-		error = got_ferror(gitconfig_file, GOT_ERR_IO);
-		goto done;
-	}
-
 	if (verbosity >= 0)
 		printf("Created cloned repository '%s'\n", repo_path);
 done: