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;
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 int get_branches_uri(const char *repo, const char *uri[MAX_URI])
120 const char *slash = strchr(repo, '/');
121 int n = slash ? slash - repo : 1000;
122 FILE *f = fopen(git_path("branches/%.*s", n, repo), "r");
128 s = fgets(buffer, BUF_SIZE, f);
137 while (isspace(p[-1]))
141 len += strlen(slash);
142 p = xmalloc(len + 1);
151 * Read remotes and branches file, fill the push target URI
152 * list. If there is no command line refspecs, read Push: lines
153 * to set up the *refspec list as well.
154 * return the number of push target URIs
156 static int read_config(const char *repo, const char *uri[MAX_URI])
161 n = get_remotes_uri(repo, uri);
165 n = get_branches_uri(repo, uri);
174 static int do_push(const char *repo)
176 const char *uri[MAX_URI];
182 n = read_config(repo, uri);
184 die("bad repository '%s'", repo);
186 argv = xmalloc((refspec_nr + 10) * sizeof(char *));
187 argv[0] = "dummy-send-pack";
190 argv[argc++] = "--all";
192 argv[argc++] = "--force";
194 argv[argc++] = execute;
196 argv[argc++] = "--thin";
198 argv[argc++] = "dummy-remote";
200 argv[argc++] = *refspec++;
203 for (i = 0; i < n; i++) {
205 const char *dest = uri[i];
206 const char *sender = "git-send-pack";
207 if (!strncmp(dest, "http://", 7) ||
208 !strncmp(dest, "https://", 8))
209 sender = "git-http-push";
212 error = run_command_v(argc, argv);
216 case -ERR_RUN_COMMAND_FORK:
217 die("unable to fork for %s", sender);
218 case -ERR_RUN_COMMAND_EXEC:
219 die("unable to exec %s", sender);
220 case -ERR_RUN_COMMAND_WAITPID:
221 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
222 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
223 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
224 die("%s died with strange error", sender);
232 int cmd_push(int argc, const char **argv, char **envp)
235 const char *repo = "origin"; // default repository
237 for (i = 1; i < argc; i++) {
238 const char *arg = argv[i];
245 if (!strcmp(arg, "--all")) {
249 if (!strcmp(arg, "--tags")) {
253 if (!strcmp(arg, "--force")) {
257 if (!strcmp(arg, "--thin")) {
261 if (!strcmp(arg, "--no-thin")) {
265 if (!strncmp(arg, "--exec=", 7)) {
271 set_refspecs(argv + i, argc - i);
272 return do_push(repo);