6 #include "run-command.h"
11 static const char push_usage[] = "git push [--all] [--tags] [--force] <repository> [<refspec>...]";
13 static int all = 0, tags = 0, force = 0, thin = 1;
14 static const char *execute = NULL;
16 #define BUF_SIZE (2084)
17 static char buffer[BUF_SIZE];
19 static const char **refspec = NULL;
20 static int refspec_nr = 0;
22 static void add_refspec(const char *ref)
24 int nr = refspec_nr + 1;
25 refspec = xrealloc(refspec, nr * sizeof(char *));
30 static int expand_one_ref(const char *ref, const unsigned char *sha1)
32 /* Ignore the "refs/" at the beginning of the refname */
35 if (strncmp(ref, "tags/", 5))
38 add_refspec(strdup(ref));
42 static void expand_refspecs(void)
46 die("cannot mix '--all' and a refspec");
49 * No need to expand "--all" - we'll just use
50 * the "--all" flag to send-pack
56 for_each_ref(expand_one_ref);
59 static void set_refspecs(const char **refs, int nr)
62 size_t bytes = nr * sizeof(char *);
64 refspec = xrealloc(refspec, bytes);
65 memcpy(refspec, refs, bytes);
71 static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
74 FILE *f = fopen(git_path("remotes/%s", repo), "r");
75 int has_explicit_refspec = refspec_nr || all || tags;
79 while (fgets(buffer, BUF_SIZE, f)) {
83 if (!strncmp("URL: ", buffer, 5)) {
86 } else if (!strncmp("Push: ", buffer, 6)) {
92 /* Remove whitespace at the head.. */
98 /* ..and at the end */
100 while (isspace(p[-1]))
105 uri[n++] = strdup(s);
107 error("more than %d URL's specified, ignoreing the rest", MAX_URI);
109 else if (is_refspec && !has_explicit_refspec)
110 add_refspec(strdup(s));
114 die("remote '%s' has no URL", repo);
118 static const char **config_uri;
119 static const char *config_repo;
120 static int config_repo_len;
121 static int config_current_uri;
122 static int config_get_refspecs;
124 static int get_remote_config(const char* key, const char* value)
126 if (!strncmp(key, "remote.", 7) &&
127 !strncmp(key + 7, config_repo, config_repo_len)) {
128 if (!strcmp(key + 7 + config_repo_len, ".url")) {
129 if (config_current_uri < MAX_URI)
130 config_uri[config_current_uri++] = strdup(value);
132 error("more than %d URL's specified, ignoring the rest", MAX_URI);
134 else if (config_get_refspecs &&
135 !strcmp(key + 7 + config_repo_len, ".push"))
136 add_refspec(strdup(value));
141 static int get_config_remotes_uri(const char *repo, const char *uri[MAX_URI])
143 config_repo_len = strlen(repo);
145 config_current_uri = 0;
147 config_get_refspecs = !(refspec_nr || all || tags);
149 git_config(get_remote_config);
150 return config_current_uri;
153 static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
155 const char *slash = strchr(repo, '/');
156 int n = slash ? slash - repo : 1000;
157 FILE *f = fopen(git_path("branches/%.*s", n, repo), "r");
163 s = fgets(buffer, BUF_SIZE, f);
172 while (isspace(p[-1]))
176 len += strlen(slash);
177 p = xmalloc(len + 1);
186 * Read remotes and branches file, fill the push target URI
187 * list. If there is no command line refspecs, read Push: lines
188 * to set up the *refspec list as well.
189 * return the number of push target URIs
191 static int read_config(const char *repo, const char *uri[MAX_URI])
196 n = get_remotes_uri(repo, uri);
200 n = get_config_remotes_uri(repo, uri);
204 n = get_branches_uri(repo, uri);
213 static int do_push(const char *repo)
215 const char *uri[MAX_URI];
221 n = read_config(repo, uri);
223 die("bad repository '%s'", repo);
225 argv = xmalloc((refspec_nr + 10) * sizeof(char *));
226 argv[0] = "dummy-send-pack";
229 argv[argc++] = "--all";
231 argv[argc++] = "--force";
233 argv[argc++] = execute;
236 for (i = 0; i < n; i++) {
238 int dest_argc = common_argc;
239 int dest_refspec_nr = refspec_nr;
240 const char **dest_refspec = refspec;
241 const char *dest = uri[i];
242 const char *sender = "git-send-pack";
243 if (!strncmp(dest, "http://", 7) ||
244 !strncmp(dest, "https://", 8))
245 sender = "git-http-push";
247 argv[dest_argc++] = "--thin";
249 argv[dest_argc++] = dest;
250 while (dest_refspec_nr--)
251 argv[dest_argc++] = *dest_refspec++;
252 argv[dest_argc] = NULL;
253 error = run_command_v(argc, argv);
257 case -ERR_RUN_COMMAND_FORK:
258 die("unable to fork for %s", sender);
259 case -ERR_RUN_COMMAND_EXEC:
260 die("unable to exec %s", sender);
261 case -ERR_RUN_COMMAND_WAITPID:
262 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
263 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
264 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
265 die("%s died with strange error", sender);
273 int cmd_push(int argc, const char **argv, char **envp)
276 const char *repo = "origin"; // default repository
278 for (i = 1; i < argc; i++) {
279 const char *arg = argv[i];
286 if (!strcmp(arg, "--all")) {
290 if (!strcmp(arg, "--tags")) {
294 if (!strcmp(arg, "--force")) {
298 if (!strcmp(arg, "--thin")) {
302 if (!strcmp(arg, "--no-thin")) {
306 if (!strncmp(arg, "--exec=", 7)) {
312 set_refspecs(argv + i, argc - i);
313 return do_push(repo);