create a git config file in newly cloned repositories
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
diff --git a/got/got.c b/got/got.c
index 294153f..7b17cf3 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1053,6 +1053,11 @@ cmd_clone(int argc, char *argv[])
struct got_object_id *pack_hash = NULL;
int ch, fetchfd = -1;
struct got_fetch_progress_arg fpa;
+ char *git_url = NULL;
+ char *gitconfig_path = NULL;
+ char *gitconfig = NULL;
+ FILE *gitconfig_file = NULL;
+ ssize_t n;
TAILQ_INIT(&refs);
TAILQ_INIT(&symrefs);
@@ -1081,16 +1086,28 @@ cmd_clone(int argc, char *argv[])
if (error)
goto done;
-#ifndef PROFILE
if (strcmp(proto, "git") == 0) {
+#ifndef PROFILE
if (pledge("stdio rpath wpath cpath fattr flock proc exec "
"sendfd dns inet unveil", NULL) == -1)
err(1, "pledge");
+#endif
+ git_url = strdup(argv[0]);
+ if (git_url == NULL) {
+ error = got_error_from_errno("strdup");
+ goto done;
+ }
} else if (strcmp(proto, "git+ssh") == 0 ||
strcmp(proto, "ssh") == 0) {
+#ifndef PROFILE
if (pledge("stdio rpath wpath cpath fattr flock proc exec "
"sendfd unveil", NULL) == -1)
err(1, "pledge");
+#endif
+ if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
+ error = got_error_from_errno("asprintf");
+ goto done;
+ }
} else if (strcmp(proto, "http") == 0 ||
strcmp(proto, "git+http") == 0) {
error = got_error_path(proto, GOT_ERR_NOT_IMPL);
@@ -1099,7 +1116,6 @@ cmd_clone(int argc, char *argv[])
error = got_error_path(proto, GOT_ERR_BAD_PROTO);
goto done;
}
-#endif
if (dirname == NULL) {
if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
error = got_error_from_errno("asprintf");
@@ -1205,9 +1221,36 @@ 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, "w");
+ if (gitconfig_file == NULL) {
+ error = got_error_from_errno2("fopen", gitconfig_path);
+ goto done;
+ }
+ if (asprintf(&gitconfig,
+ "[core]\n"
+ "\trepositoryformatversion = 0\n"
+ "\tbare = true\n"
+ "[remote \"origin\"]\n"
+ "\turl = %s\n"
+ "\tfetch = +refs/heads/*:refs/remotes/origin/*\n",
+ git_url) == -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);
done:
if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
error = got_error_from_errno("close");
+ if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
+ error = got_error_from_errno("fclose");
if (repo)
got_repo_close(repo);
TAILQ_FOREACH(pe, &refs, entry) {
@@ -1227,6 +1270,8 @@ done:
free(server_path);
free(repo_name);
free(default_destdir);
+ free(gitconfig_path);
+ free(git_url);
return error;
}
diff --git a/include/got_repository.h b/include/got_repository.h
index d3b000f..106390c 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -65,6 +65,7 @@ char *got_repo_get_path_objects(struct got_repository *);
char *got_repo_get_path_objects_pack(struct got_repository *);
char *got_repo_get_path_refs(struct got_repository *);
char *got_repo_get_path_packed_refs(struct got_repository *);
+char *got_repo_get_path_gitconfig(struct got_repository *);
struct got_reference;
diff --git a/lib/repository.c b/lib/repository.c
index 734d7b1..1c7c3d1 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -153,13 +153,10 @@ get_path_head(struct got_repository *repo)
return get_path_git_child(repo, GOT_HEAD_FILE);
}
-static const struct got_error *
-get_path_gitconfig(char **p, struct got_repository *repo)
+char *
+got_repo_get_path_gitconfig(struct got_repository *repo)
{
- *p = get_path_git_child(repo, GOT_GITCONFIG);
- if (*p == NULL)
- return got_error_from_errno("asprintf");
- return NULL;
+ return get_path_git_child(repo, GOT_GITCONFIG);
}
void
@@ -498,9 +495,9 @@ read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
}
/* Read repository's .git/config file. */
- err = get_path_gitconfig(&repo_gitconfig_path, repo);
- if (err)
- return err;
+ repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
+ if (repo_gitconfig_path == NULL)
+ return got_error_from_errno("got_repo_get_path_gitconfig");
err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
&repo->gitconfig_author_name, &repo->gitconfig_author_email,