add commit count options to git-shortlog
[git/jnareb-git.git] / builtin-push.c
blobf5150ed82d269dd825a45cc9c369c71e777bac05
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] [-f | --force] <repository> [<refspec>...]";
13 static int all, tags, force, thin = 1;
14 static const char *execute;
16 #define BUF_SIZE (2084)
17 static char buffer[BUF_SIZE];
19 static const char **refspec;
20 static int refspec_nr;
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 add_refspec(xstrdup(ref));
37 return 0;
40 static void expand_refspecs(void)
42 if (all) {
43 if (refspec_nr)
44 die("cannot mix '--all' and a refspec");
47 * No need to expand "--all" - we'll just use
48 * the "--all" flag to send-pack
50 return;
52 if (!tags)
53 return;
54 for_each_ref(expand_one_ref);
57 static void set_refspecs(const char **refs, int nr)
59 if (nr) {
60 size_t bytes = nr * sizeof(char *);
62 refspec = xrealloc(refspec, bytes);
63 memcpy(refspec, refs, bytes);
64 refspec_nr = nr;
66 expand_refspecs();
69 static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
71 int n = 0;
72 FILE *f = fopen(git_path("remotes/%s", repo), "r");
73 int has_explicit_refspec = refspec_nr || all || tags;
75 if (!f)
76 return -1;
77 while (fgets(buffer, BUF_SIZE, f)) {
78 int is_refspec;
79 char *s, *p;
81 if (!strncmp("URL:", buffer, 4)) {
82 is_refspec = 0;
83 s = buffer + 4;
84 } else if (!strncmp("Push:", buffer, 5)) {
85 is_refspec = 1;
86 s = buffer + 5;
87 } else
88 continue;
90 /* Remove whitespace at the head.. */
91 while (isspace(*s))
92 s++;
93 if (!*s)
94 continue;
96 /* ..and at the end */
97 p = s + strlen(s);
98 while (isspace(p[-1]))
99 *--p = 0;
101 if (!is_refspec) {
102 if (n < MAX_URI)
103 uri[n++] = xstrdup(s);
104 else
105 error("more than %d URL's specified, ignoring the rest", MAX_URI);
107 else if (is_refspec && !has_explicit_refspec)
108 add_refspec(xstrdup(s));
110 fclose(f);
111 if (!n)
112 die("remote '%s' has no URL", repo);
113 return n;
116 static const char **config_uri;
117 static const char *config_repo;
118 static int config_repo_len;
119 static int config_current_uri;
120 static int config_get_refspecs;
122 static int get_remote_config(const char* key, const char* value)
124 if (!strncmp(key, "remote.", 7) &&
125 !strncmp(key + 7, config_repo, config_repo_len)) {
126 if (!strcmp(key + 7 + config_repo_len, ".url")) {
127 if (config_current_uri < MAX_URI)
128 config_uri[config_current_uri++] = xstrdup(value);
129 else
130 error("more than %d URL's specified, ignoring the rest", MAX_URI);
132 else if (config_get_refspecs &&
133 !strcmp(key + 7 + config_repo_len, ".push"))
134 add_refspec(xstrdup(value));
136 return 0;
139 static int get_config_remotes_uri(const char *repo, const char *uri[MAX_URI])
141 config_repo_len = strlen(repo);
142 config_repo = repo;
143 config_current_uri = 0;
144 config_uri = uri;
145 config_get_refspecs = !(refspec_nr || all || tags);
147 git_config(get_remote_config);
148 return config_current_uri;
151 static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
153 const char *slash = strchr(repo, '/');
154 int n = slash ? slash - repo : 1000;
155 FILE *f = fopen(git_path("branches/%.*s", n, repo), "r");
156 char *s, *p;
157 int len;
159 if (!f)
160 return 0;
161 s = fgets(buffer, BUF_SIZE, f);
162 fclose(f);
163 if (!s)
164 return 0;
165 while (isspace(*s))
166 s++;
167 if (!*s)
168 return 0;
169 p = s + strlen(s);
170 while (isspace(p[-1]))
171 *--p = 0;
172 len = p - s;
173 if (slash)
174 len += strlen(slash);
175 p = xmalloc(len + 1);
176 strcpy(p, s);
177 if (slash)
178 strcat(p, slash);
179 uri[0] = p;
180 return 1;
184 * Read remotes and branches file, fill the push target URI
185 * list. If there is no command line refspecs, read Push: lines
186 * to set up the *refspec list as well.
187 * return the number of push target URIs
189 static int read_config(const char *repo, const char *uri[MAX_URI])
191 int n;
193 if (*repo != '/') {
194 n = get_remotes_uri(repo, uri);
195 if (n > 0)
196 return n;
198 n = get_config_remotes_uri(repo, uri);
199 if (n > 0)
200 return n;
202 n = get_branches_uri(repo, uri);
203 if (n > 0)
204 return n;
207 uri[0] = repo;
208 return 1;
211 static int do_push(const char *repo)
213 const char *uri[MAX_URI];
214 int i, n;
215 int common_argc;
216 const char **argv;
217 int argc;
219 n = read_config(repo, uri);
220 if (n <= 0)
221 die("bad repository '%s'", repo);
223 argv = xmalloc((refspec_nr + 10) * sizeof(char *));
224 argv[0] = "dummy-send-pack";
225 argc = 1;
226 if (all)
227 argv[argc++] = "--all";
228 if (force)
229 argv[argc++] = "--force";
230 if (execute)
231 argv[argc++] = execute;
232 common_argc = argc;
234 for (i = 0; i < n; i++) {
235 int err;
236 int dest_argc = common_argc;
237 int dest_refspec_nr = refspec_nr;
238 const char **dest_refspec = refspec;
239 const char *dest = uri[i];
240 const char *sender = "git-send-pack";
241 if (!strncmp(dest, "http://", 7) ||
242 !strncmp(dest, "https://", 8))
243 sender = "git-http-push";
244 else if (thin)
245 argv[dest_argc++] = "--thin";
246 argv[0] = sender;
247 argv[dest_argc++] = dest;
248 while (dest_refspec_nr--)
249 argv[dest_argc++] = *dest_refspec++;
250 argv[dest_argc] = NULL;
251 err = run_command_v(argc, argv);
252 if (!err)
253 continue;
254 switch (err) {
255 case -ERR_RUN_COMMAND_FORK:
256 die("unable to fork for %s", sender);
257 case -ERR_RUN_COMMAND_EXEC:
258 die("unable to exec %s", sender);
259 case -ERR_RUN_COMMAND_WAITPID:
260 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
261 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
262 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
263 die("%s died with strange error", sender);
264 default:
265 return -err;
268 return 0;
271 int cmd_push(int argc, const char **argv, const char *prefix)
273 int i;
274 const char *repo = "origin"; /* default repository */
276 for (i = 1; i < argc; i++) {
277 const char *arg = argv[i];
279 if (arg[0] != '-') {
280 repo = arg;
281 i++;
282 break;
284 if (!strcmp(arg, "--all")) {
285 all = 1;
286 continue;
288 if (!strcmp(arg, "--tags")) {
289 tags = 1;
290 continue;
292 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
293 force = 1;
294 continue;
296 if (!strcmp(arg, "--thin")) {
297 thin = 1;
298 continue;
300 if (!strcmp(arg, "--no-thin")) {
301 thin = 0;
302 continue;
304 if (!strncmp(arg, "--exec=", 7)) {
305 execute = arg;
306 continue;
308 usage(push_usage);
310 set_refspecs(argv + i, argc - i);
311 return do_push(repo);