builtin-push: make it official.
[git/jrn.git] / builtin-push.c
blob9a861b5afea701ddaa31fba902d535710afda13d
1 /*
2 * "git push"
3 */
4 #include "cache.h"
5 #include "refs.h"
6 #include "run-command.h"
7 #include "builtin.h"
9 #define MAX_URI (16)
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 *));
26 refspec[nr-1] = ref;
27 refspec_nr = nr;
30 static int expand_one_ref(const char *ref, const unsigned char *sha1)
32 /* Ignore the "refs/" at the beginning of the refname */
33 ref += 5;
35 if (strncmp(ref, "tags/", 5))
36 return 0;
38 add_refspec(strdup(ref));
39 return 0;
42 static void expand_refspecs(void)
44 if (all) {
45 if (refspec_nr)
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
52 return;
54 if (!tags)
55 return;
56 for_each_ref(expand_one_ref);
59 static void set_refspecs(const char **refs, int nr)
61 if (nr) {
62 size_t bytes = nr * sizeof(char *);
64 refspec = xrealloc(refspec, bytes);
65 memcpy(refspec, refs, bytes);
66 refspec_nr = nr;
68 expand_refspecs();
71 static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
73 int n = 0;
74 FILE *f = fopen(git_path("remotes/%s", repo), "r");
75 int has_explicit_refspec = refspec_nr;
77 if (!f)
78 return -1;
79 while (fgets(buffer, BUF_SIZE, f)) {
80 int is_refspec;
81 char *s, *p;
83 if (!strncmp("URL: ", buffer, 5)) {
84 is_refspec = 0;
85 s = buffer + 5;
86 } else if (!strncmp("Push: ", buffer, 6)) {
87 is_refspec = 1;
88 s = buffer + 6;
89 } else
90 continue;
92 /* Remove whitespace at the head.. */
93 while (isspace(*s))
94 s++;
95 if (!*s)
96 continue;
98 /* ..and at the end */
99 p = s + strlen(s);
100 while (isspace(p[-1]))
101 *--p = 0;
103 if (!is_refspec) {
104 if (n < MAX_URI)
105 uri[n++] = strdup(s);
106 else
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));
112 fclose(f);
113 if (!n)
114 die("remote '%s' has no URL", repo);
115 return n;
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");
123 char *s, *p;
124 int len;
126 if (!f)
127 return 0;
128 s = fgets(buffer, BUF_SIZE, f);
129 fclose(f);
130 if (!s)
131 return 0;
132 while (isspace(*s))
133 s++;
134 if (!*s)
135 return 0;
136 p = s + strlen(s);
137 while (isspace(p[-1]))
138 *--p = 0;
139 len = p - s;
140 if (slash)
141 len += strlen(slash);
142 p = xmalloc(len + 1);
143 strcpy(p, s);
144 if (slash)
145 strcat(p, slash);
146 uri[0] = p;
147 return 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])
158 int n;
160 if (*repo != '/') {
161 n = get_remotes_uri(repo, uri);
162 if (n > 0)
163 return n;
165 n = get_branches_uri(repo, uri);
166 if (n > 0)
167 return n;
170 uri[0] = repo;
171 return 1;
174 static int do_push(const char *repo)
176 const char *uri[MAX_URI];
177 int i, n;
178 int remote;
179 const char **argv;
180 int argc;
182 n = read_config(repo, uri);
183 if (n <= 0)
184 die("bad repository '%s'", repo);
186 argv = xmalloc((refspec_nr + 10) * sizeof(char *));
187 argv[0] = "dummy-send-pack";
188 argc = 1;
189 if (all)
190 argv[argc++] = "--all";
191 if (force)
192 argv[argc++] = "--force";
193 if (execute)
194 argv[argc++] = execute;
195 if (thin)
196 argv[argc++] = "--thin";
197 remote = argc;
198 argv[argc++] = "dummy-remote";
199 while (refspec_nr--)
200 argv[argc++] = *refspec++;
201 argv[argc] = NULL;
203 for (i = 0; i < n; i++) {
204 int error;
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";
210 argv[0] = sender;
211 argv[remote] = dest;
212 error = run_command_v(argc, argv);
213 if (!error)
214 continue;
215 switch (error) {
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);
225 default:
226 return -error;
229 return 0;
232 int cmd_push(int argc, const char **argv, char **envp)
234 int i;
235 const char *repo = "origin"; // default repository
237 for (i = 1; i < argc; i++) {
238 const char *arg = argv[i];
240 if (arg[0] != '-') {
241 repo = arg;
242 i++;
243 break;
245 if (!strcmp(arg, "--all")) {
246 all = 1;
247 continue;
249 if (!strcmp(arg, "--tags")) {
250 tags = 1;
251 continue;
253 if (!strcmp(arg, "--force")) {
254 force = 1;
255 continue;
257 if (!strcmp(arg, "--thin")) {
258 thin = 1;
259 continue;
261 if (!strcmp(arg, "--no-thin")) {
262 thin = 0;
263 continue;
265 if (!strncmp(arg, "--exec=", 7)) {
266 execute = arg;
267 continue;
269 usage(push_usage);
271 set_refspecs(argv + i, argc - i);
272 return do_push(repo);