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 #define MAX_REFSPECS 10
72 static int current_refspec = 0;
73 static char *refspecs_[MAX_REFSPECS];
75 static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
78 FILE *f = fopen(git_path("remotes/%s", repo), "r");
82 while (fgets(buffer, BUF_SIZE, f)) {
86 if (!strncmp("URL: ", buffer, 5)) {
89 } else if (!strncmp("Push: ", buffer, 6)) {
95 /* Remove whitespace at the head.. */
101 /* ..and at the end */
103 while (isspace(p[-1]))
106 if (!is_refspec && n < MAX_URI)
107 uri[n++] = strdup(s);
108 else if (is_refspec && current_refspec < MAX_REFSPECS)
109 refspecs_[current_refspec++] = strdup(s);
113 die("remote '%s' has no URL", repo);
117 static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
119 const char *slash = strchr(repo, '/');
120 int n = slash ? slash - repo : 1000;
121 FILE *f = fopen(git_path("branches/%.*s", n, repo), "r");
127 s = fgets(buffer, BUF_SIZE, f);
136 while (isspace(p[-1]))
140 len += strlen(slash);
141 p = xmalloc(len + 1);
149 static int get_uri(const char *repo, const char *uri[MAX_URI])
156 n = get_remotes_uri(repo, uri);
160 n = get_branches_uri(repo, uri);
169 static int do_push(const char *repo)
171 const char *uri[MAX_URI];
172 int i, n = get_uri(repo, uri);
177 n = get_uri(repo, uri);
179 die("bad repository '%s'", repo);
182 set_refspecs((const char**)refspecs_, current_refspec);
184 argv = xmalloc((refspec_nr + 10) * sizeof(char *));
185 argv[0] = "dummy-send-pack";
188 argv[argc++] = "--all";
190 argv[argc++] = "--force";
192 argv[argc++] = execute;
194 argv[argc++] = "--thin";
196 argv[argc++] = "dummy-remote";
198 argv[argc++] = *refspec++;
201 for (i = 0; i < n; i++) {
203 const char *dest = uri[i];
204 const char *sender = "git-send-pack";
205 if (!strncmp(dest, "http://", 7) ||
206 !strncmp(dest, "https://", 8))
207 sender = "git-http-push";
210 error = run_command_v(argc, argv);
214 case -ERR_RUN_COMMAND_FORK:
215 die("unable to fork for %s", sender);
216 case -ERR_RUN_COMMAND_EXEC:
217 die("unable to exec %s", sender);
218 case -ERR_RUN_COMMAND_WAITPID:
219 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
220 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
221 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
222 die("%s died with strange error", sender);
230 int cmd_push(int argc, const char **argv, char **envp)
233 const char *repo = "origin"; // default repository
235 for (i = 1; i < argc; i++) {
236 const char *arg = argv[i];
243 if (!strcmp(arg, "--all")) {
247 if (!strcmp(arg, "--tags")) {
251 if (!strcmp(arg, "--force")) {
255 if (!strcmp(arg, "--thin")) {
259 if (!strcmp(arg, "--no-thin")) {
263 if (!strncmp(arg, "--exec=", 7)) {
269 set_refspecs(argv + i, argc - i);
270 return do_push(repo);