refs.c: simplify strbufs in reflog setup and writing
[git/mingw.git] / builtin / send-pack.c
blob23b2962cb0c1443b202bcab0b73023109e06999c
1 #include "builtin.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "run-command.h"
7 #include "remote.h"
8 #include "connect.h"
9 #include "send-pack.h"
10 #include "quote.h"
11 #include "transport.h"
12 #include "version.h"
13 #include "sha1-array.h"
14 #include "gpg-interface.h"
16 static const char send_pack_usage[] =
17 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] [<host>:]<directory> [<ref>...]\n"
18 " --all and explicit <ref> specification are mutually exclusive.";
20 static struct send_pack_args args;
22 static void print_helper_status(struct ref *ref)
24 struct strbuf buf = STRBUF_INIT;
26 for (; ref; ref = ref->next) {
27 const char *msg = NULL;
28 const char *res;
30 switch(ref->status) {
31 case REF_STATUS_NONE:
32 res = "error";
33 msg = "no match";
34 break;
36 case REF_STATUS_OK:
37 res = "ok";
38 break;
40 case REF_STATUS_UPTODATE:
41 res = "ok";
42 msg = "up to date";
43 break;
45 case REF_STATUS_REJECT_NONFASTFORWARD:
46 res = "error";
47 msg = "non-fast forward";
48 break;
50 case REF_STATUS_REJECT_FETCH_FIRST:
51 res = "error";
52 msg = "fetch first";
53 break;
55 case REF_STATUS_REJECT_NEEDS_FORCE:
56 res = "error";
57 msg = "needs force";
58 break;
60 case REF_STATUS_REJECT_STALE:
61 res = "error";
62 msg = "stale info";
63 break;
65 case REF_STATUS_REJECT_ALREADY_EXISTS:
66 res = "error";
67 msg = "already exists";
68 break;
70 case REF_STATUS_REJECT_NODELETE:
71 case REF_STATUS_REMOTE_REJECT:
72 res = "error";
73 break;
75 case REF_STATUS_EXPECTING_REPORT:
76 default:
77 continue;
80 strbuf_reset(&buf);
81 strbuf_addf(&buf, "%s %s", res, ref->name);
82 if (ref->remote_status)
83 msg = ref->remote_status;
84 if (msg) {
85 strbuf_addch(&buf, ' ');
86 quote_two_c_style(&buf, "", msg, 0);
88 strbuf_addch(&buf, '\n');
90 write_or_die(1, buf.buf, buf.len);
92 strbuf_release(&buf);
95 int cmd_send_pack(int argc, const char **argv, const char *prefix)
97 int i, nr_refspecs = 0;
98 const char **refspecs = NULL;
99 const char *remote_name = NULL;
100 struct remote *remote = NULL;
101 const char *dest = NULL;
102 int fd[2];
103 struct child_process *conn;
104 struct sha1_array extra_have = SHA1_ARRAY_INIT;
105 struct sha1_array shallow = SHA1_ARRAY_INIT;
106 struct ref *remote_refs, *local_refs;
107 int ret;
108 int helper_status = 0;
109 int send_all = 0;
110 const char *receivepack = "git-receive-pack";
111 int flags;
112 unsigned int reject_reasons;
113 int progress = -1;
114 int from_stdin = 0;
115 struct push_cas_option cas = {0};
117 git_config(git_gpg_config, NULL);
119 argv++;
120 for (i = 1; i < argc; i++, argv++) {
121 const char *arg = *argv;
123 if (*arg == '-') {
124 if (starts_with(arg, "--receive-pack=")) {
125 receivepack = arg + 15;
126 continue;
128 if (starts_with(arg, "--exec=")) {
129 receivepack = arg + 7;
130 continue;
132 if (starts_with(arg, "--remote=")) {
133 remote_name = arg + 9;
134 continue;
136 if (!strcmp(arg, "--all")) {
137 send_all = 1;
138 continue;
140 if (!strcmp(arg, "--dry-run")) {
141 args.dry_run = 1;
142 continue;
144 if (!strcmp(arg, "--mirror")) {
145 args.send_mirror = 1;
146 continue;
148 if (!strcmp(arg, "--force")) {
149 args.force_update = 1;
150 continue;
152 if (!strcmp(arg, "--quiet")) {
153 args.quiet = 1;
154 continue;
156 if (!strcmp(arg, "--verbose")) {
157 args.verbose = 1;
158 continue;
160 if (!strcmp(arg, "--signed")) {
161 args.push_cert = 1;
162 continue;
164 if (!strcmp(arg, "--progress")) {
165 progress = 1;
166 continue;
168 if (!strcmp(arg, "--no-progress")) {
169 progress = 0;
170 continue;
172 if (!strcmp(arg, "--thin")) {
173 args.use_thin_pack = 1;
174 continue;
176 if (!strcmp(arg, "--atomic")) {
177 args.atomic = 1;
178 continue;
180 if (!strcmp(arg, "--stateless-rpc")) {
181 args.stateless_rpc = 1;
182 continue;
184 if (!strcmp(arg, "--stdin")) {
185 from_stdin = 1;
186 continue;
188 if (!strcmp(arg, "--helper-status")) {
189 helper_status = 1;
190 continue;
192 if (!strcmp(arg, "--" CAS_OPT_NAME)) {
193 if (parse_push_cas_option(&cas, NULL, 0) < 0)
194 exit(1);
195 continue;
197 if (!strcmp(arg, "--no-" CAS_OPT_NAME)) {
198 if (parse_push_cas_option(&cas, NULL, 1) < 0)
199 exit(1);
200 continue;
202 if (starts_with(arg, "--" CAS_OPT_NAME "=")) {
203 if (parse_push_cas_option(&cas,
204 strchr(arg, '=') + 1, 0) < 0)
205 exit(1);
206 continue;
208 usage(send_pack_usage);
210 if (!dest) {
211 dest = arg;
212 continue;
214 refspecs = (const char **) argv;
215 nr_refspecs = argc - i;
216 break;
218 if (!dest)
219 usage(send_pack_usage);
221 if (from_stdin) {
222 struct argv_array all_refspecs = ARGV_ARRAY_INIT;
224 for (i = 0; i < nr_refspecs; i++)
225 argv_array_push(&all_refspecs, refspecs[i]);
227 if (args.stateless_rpc) {
228 const char *buf;
229 while ((buf = packet_read_line(0, NULL)))
230 argv_array_push(&all_refspecs, buf);
231 } else {
232 struct strbuf line = STRBUF_INIT;
233 while (strbuf_getline(&line, stdin, '\n') != EOF)
234 argv_array_push(&all_refspecs, line.buf);
235 strbuf_release(&line);
238 refspecs = all_refspecs.argv;
239 nr_refspecs = all_refspecs.argc;
243 * --all and --mirror are incompatible; neither makes sense
244 * with any refspecs.
246 if ((refspecs && (send_all || args.send_mirror)) ||
247 (send_all && args.send_mirror))
248 usage(send_pack_usage);
250 if (remote_name) {
251 remote = remote_get(remote_name);
252 if (!remote_has_url(remote, dest)) {
253 die("Destination %s is not a uri for %s",
254 dest, remote_name);
258 if (progress == -1)
259 progress = !args.quiet && isatty(2);
260 args.progress = progress;
262 if (args.stateless_rpc) {
263 conn = NULL;
264 fd[0] = 0;
265 fd[1] = 1;
266 } else {
267 conn = git_connect(fd, dest, receivepack,
268 args.verbose ? CONNECT_VERBOSE : 0);
271 get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL,
272 &extra_have, &shallow);
274 transport_verify_remote_names(nr_refspecs, refspecs);
276 local_refs = get_local_heads();
278 flags = MATCH_REFS_NONE;
280 if (send_all)
281 flags |= MATCH_REFS_ALL;
282 if (args.send_mirror)
283 flags |= MATCH_REFS_MIRROR;
285 /* match them up */
286 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
287 return -1;
289 if (!is_empty_cas(&cas))
290 apply_push_cas(&cas, remote, remote_refs);
292 set_ref_status_for_push(remote_refs, args.send_mirror,
293 args.force_update);
295 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
297 if (helper_status)
298 print_helper_status(remote_refs);
300 close(fd[1]);
301 close(fd[0]);
303 ret |= finish_connect(conn);
305 if (!helper_status)
306 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
308 if (!args.dry_run && remote) {
309 struct ref *ref;
310 for (ref = remote_refs; ref; ref = ref->next)
311 transport_update_tracking_ref(remote, ref, args.verbose);
314 if (!ret && !transport_refs_pushed(remote_refs))
315 fprintf(stderr, "Everything up-to-date\n");
317 return ret;