Commit Diff


commit - 1fc091f308f7aba6185a85cf0a8a3a8441c11458
commit + 11024cb9b69a71de22ae46ccddcb4ef3b6730553
blob - ca4d0566b09e2a63178bab27175754b8f9c4d944
blob + b44e7c50a8734b0886b2b7b9f3132fd713250738
--- got/got.c
+++ got/got.c
@@ -2287,7 +2287,8 @@ cmd_fetch(int argc, char *argv[])
 	const char *remote_name;
 	char *proto = NULL, *host = NULL, *port = NULL;
 	char *repo_name = NULL, *server_path = NULL;
-	const struct got_remote_repo *remotes, *remote = NULL;
+	const struct got_remote_repo *remotes;
+	struct got_remote_repo *remote = NULL;
 	int nremotes;
 	char *id_str = NULL;
 	struct got_repository *repo = NULL;
@@ -2449,7 +2450,10 @@ cmd_fetch(int argc, char *argv[])
 			    worktree_conf);
 			for (i = 0; i < nremotes; i++) {
 				if (strcmp(remotes[i].name, remote_name) == 0) {
-					remote = &remotes[i];
+					error = got_repo_remote_repo_dup(&remote,
+					    &remotes[i]);
+					if (error)
+						goto done;
 					break;
 				}
 			}
@@ -2462,7 +2466,10 @@ cmd_fetch(int argc, char *argv[])
 			    repo_conf);
 			for (i = 0; i < nremotes; i++) {
 				if (strcmp(remotes[i].name, remote_name) == 0) {
-					remote = &remotes[i];
+					error = got_repo_remote_repo_dup(&remote,
+					    &remotes[i]);
+					if (error)
+						goto done;
 					break;
 				}
 			}
@@ -2472,7 +2479,10 @@ cmd_fetch(int argc, char *argv[])
 		got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
 		for (i = 0; i < nremotes; i++) {
 			if (strcmp(remotes[i].name, remote_name) == 0) {
-				remote = &remotes[i];
+				error = got_repo_remote_repo_dup(&remote,
+				    &remotes[i]);
+				if (error)
+					goto done;
 				break;
 			}
 		}
@@ -2795,6 +2805,8 @@ done:
 	got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
 	got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
 	got_ref_list_free(&remote_refs);
+	got_repo_free_remote_repo_data(remote);
+	free(remote);
 	free(head_refname);
 	free(id_str);
 	free(cwd);
@@ -9692,7 +9704,8 @@ cmd_send(int argc, char *argv[])
 	const char *remote_name;
 	char *proto = NULL, *host = NULL, *port = NULL;
 	char *repo_name = NULL, *server_path = NULL;
-	const struct got_remote_repo *remotes, *remote = NULL;
+	const struct got_remote_repo *remotes;
+	struct got_remote_repo *remote = NULL;
 	int nremotes, nbranches = 0, ndelete_branches = 0;
 	struct got_repository *repo = NULL;
 	struct got_worktree *worktree = NULL;
@@ -9827,7 +9840,10 @@ cmd_send(int argc, char *argv[])
 			    worktree_conf);
 			for (i = 0; i < nremotes; i++) {
 				if (strcmp(remotes[i].name, remote_name) == 0) {
-					remote = &remotes[i];
+					error = got_repo_remote_repo_dup(&remote,
+					    &remotes[i]);
+					if (error)
+						goto done;
 					break;
 				}
 			}
@@ -9840,7 +9856,10 @@ cmd_send(int argc, char *argv[])
 			    repo_conf);
 			for (i = 0; i < nremotes; i++) {
 				if (strcmp(remotes[i].name, remote_name) == 0) {
-					remote = &remotes[i];
+					error = got_repo_remote_repo_dup(&remote,
+					    &remotes[i]);
+					if (error)
+						goto done;
 					break;
 				}
 			}
@@ -9850,7 +9869,10 @@ cmd_send(int argc, char *argv[])
 		got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
 		for (i = 0; i < nremotes; i++) {
 			if (strcmp(remotes[i].name, remote_name) == 0) {
-				remote = &remotes[i];
+				error = got_repo_remote_repo_dup(&remote,
+				    &remotes[i]);
+				if (error)
+					goto done;
 				break;
 			}
 		}
@@ -10040,6 +10062,8 @@ done:
 	}
 	if (ref)
 		got_ref_close(ref);
+	got_repo_free_remote_repo_data(remote);
+	free(remote);
 	got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
 	got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
 	got_ref_list_free(&all_branches);
blob - 17dcc926d4cae7fa2f169eb003ed4aeebba92346
blob + c53bf9434590b858c1ce59018954e81ff934dfc1
--- include/got_repository.h
+++ include/got_repository.h
@@ -88,6 +88,14 @@ struct got_remote_repo {
 };
 
 /*
+ * Return a deep copy of a given remote_repo. The result should be
+ * freed with got_repo_free_remote_repo_data() and then free(3).
+ * Return NULL on failure.
+ */
+const struct got_error *got_repo_remote_repo_dup(struct got_remote_repo **,
+    const struct got_remote_repo *);
+
+/*
  * Free data allocated for the specified remote repository.
  * Do not free the remote_repo pointer itself.
  */
blob - 23dbb7bc21c9b25bb0c764457101d99b91e7795e
blob + 7e5e0f7b581f411d9771694a15496eeb78bffb1c
--- lib/repository.c
+++ lib/repository.c
@@ -902,11 +902,115 @@ got_repo_close(struct got_repository *repo)
 
 	return err;
 }
+
+const struct got_error *
+got_repo_remote_repo_dup(struct got_remote_repo **newp,
+    const struct got_remote_repo *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_remote_repo *new;
+	int i;
+
+	new = calloc(1, sizeof(*new));
+	if (new == NULL)
+		return got_error_from_errno("calloc");
+	
+	if (repo->name) {
+		new->name = strdup(repo->name);
+		if (new->name == NULL) {
+			err = got_error_from_errno("strdup");
+			goto done;
+		}
+	}
+
+	if (repo->fetch_url) {
+		new->fetch_url = strdup(repo->fetch_url);
+		if (new->fetch_url == NULL) {
+			err = got_error_from_errno("strdup");
+			goto done;
+		}
+	}
 
+	if (repo->send_url) {
+		new->send_url = strdup(repo->send_url);
+		if (new->send_url == NULL) {
+			err = got_error_from_errno("strdup");
+			goto done;
+		}
+	}
+
+	new->mirror_references = repo->mirror_references;
+
+	new->nfetch_branches = repo->nfetch_branches;
+	if (repo->fetch_branches) {
+		new->fetch_branches = calloc(repo->nfetch_branches,
+		    sizeof(char *));
+		if (new->fetch_branches == NULL) {
+			err = got_error_from_errno("calloc");
+			goto done;
+		}
+		for (i = 0; i < repo->nfetch_branches; i++) {
+			new->fetch_branches[i] = strdup(
+			    repo->fetch_branches[i]);
+			if (new->fetch_branches[i] == NULL) {
+				err = got_error_from_errno("strdup");
+				goto done;
+			}
+		}
+	}
+
+	new->nsend_branches = repo->nsend_branches;
+	if (repo->send_branches) {
+		new->send_branches = calloc(repo->nsend_branches,
+		    sizeof(char *));
+		if (new->send_branches == NULL) {
+			err = got_error_from_errno("calloc");
+			goto done;
+		}
+		for (i = 0; i < repo->nsend_branches; i++) {
+			new->send_branches[i] = strdup(
+			    repo->send_branches[i]);
+			if (new->send_branches[i] == NULL) {
+				err = got_error_from_errno("strdup");
+				goto done;
+			}
+		}
+	}
+
+	new->nfetch_refs = repo->nfetch_refs;
+	if (repo->fetch_refs) {
+		new->fetch_refs = calloc(repo->nfetch_refs,
+		    sizeof(char *));
+		if (new->fetch_refs == NULL) {
+			err = got_error_from_errno("calloc");
+			goto done;
+		}
+		for (i = 0; i < repo->nfetch_refs; i++) {
+			new->fetch_refs[i] = strdup(
+			    repo->fetch_refs[i]);
+			if (new->fetch_refs[i] == NULL) {
+				err = got_error_from_errno("strdup");
+				goto done;
+			}
+		}
+	}
+done:
+	if (err) {
+		got_repo_free_remote_repo_data(new);
+		free(new);
+	} else
+		*newp = new;
+
+	return err;
+}
+
 void
 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
 {
 	int i;
+
+	if (repo == NULL)
+		return;
 
 	free(repo->name);
 	repo->name = NULL;
@@ -924,6 +1028,11 @@ got_repo_free_remote_repo_data(struct got_remote_repo 
 	free(repo->send_branches);
 	repo->send_branches = NULL;
 	repo->nsend_branches = 0;
+	for (i = 0; i < repo->nfetch_refs; i++)
+		free(repo->fetch_refs[i]);
+	free(repo->fetch_refs);
+	repo->fetch_refs = NULL;
+	repo->nfetch_refs = 0;
 }
 
 const struct got_error *