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");
78 while (fgets(buffer, BUF_SIZE, f)) {
81 if (strncmp("URL: ", buffer, 5))
85 /* Remove whitespace at the head.. */
91 /* ..and at the end */
93 while (isspace(p[-1]))
102 die("remote '%s' has no URL", repo);
106 static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
108 const char *slash = strchr(repo, '/');
109 int n = slash ? slash - repo : 1000;
110 FILE *f = fopen(git_path("branches/%.*s", n, repo), "r");
116 s = fgets(buffer, BUF_SIZE, f);
125 while (isspace(p[-1]))
129 len += strlen(slash);
130 p = xmalloc(len + 1);
138 static int get_uri(const char *repo, const char *uri[MAX_URI])
143 n = get_remotes_uri(repo, uri);
147 n = get_branches_uri(repo, uri);
156 static int do_push(const char *repo)
158 const char *uri[MAX_URI];
159 int i, n = get_uri(repo, uri);
164 n = get_uri(repo, uri);
166 die("bad repository '%s'", repo);
168 argv = xmalloc((refspec_nr + 10) * sizeof(char *));
169 argv[0] = "dummy-send-pack";
172 argv[argc++] = "--all";
174 argv[argc++] = "--force";
176 argv[argc++] = execute;
178 argv[argc++] = "--thin";
180 argv[argc++] = "dummy-remote";
182 argv[argc++] = *refspec++;
185 for (i = 0; i < n; i++) {
187 const char *dest = uri[i];
188 const char *sender = "git-send-pack";
189 if (!strncmp(dest, "http://", 7) ||
190 !strncmp(dest, "https://", 8))
191 sender = "git-http-push";
194 error = run_command_v(argc, argv);
198 case -ERR_RUN_COMMAND_FORK:
199 die("unable to fork for %s", sender);
200 case -ERR_RUN_COMMAND_EXEC:
201 die("unable to exec %s", sender);
202 case -ERR_RUN_COMMAND_WAITPID:
203 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
204 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
205 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
206 die("%s died with strange error", sender);
214 int cmd_push(int argc, const char **argv, char **envp)
217 const char *repo = "origin"; // default repository
219 for (i = 1; i < argc; i++) {
220 const char *arg = argv[i];
227 if (!strcmp(arg, "--all")) {
231 if (!strcmp(arg, "--tags")) {
235 if (!strcmp(arg, "--force")) {
239 if (!strcmp(arg, "--thin")) {
243 if (!strcmp(arg, "--no-thin")) {
247 if (!strncmp(arg, "--exec=", 7)) {
253 set_refspecs(argv + i, argc - i);
254 return do_push(repo);