pack-objects: refactor unpack-unreachable expiration check
[git/mingw/j6t.git] / builtin / send-pack.c
blobb564a778455c352379127c3f06349e0ebbc81c98
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"
15 static const char send_pack_usage[] =
16 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
17 " --all and explicit <ref> specification are mutually exclusive.";
19 static struct send_pack_args args;
21 static void print_helper_status(struct ref *ref)
23 struct strbuf buf = STRBUF_INIT;
25 for (; ref; ref = ref->next) {
26 const char *msg = NULL;
27 const char *res;
29 switch(ref->status) {
30 case REF_STATUS_NONE:
31 res = "error";
32 msg = "no match";
33 break;
35 case REF_STATUS_OK:
36 res = "ok";
37 break;
39 case REF_STATUS_UPTODATE:
40 res = "ok";
41 msg = "up to date";
42 break;
44 case REF_STATUS_REJECT_NONFASTFORWARD:
45 res = "error";
46 msg = "non-fast forward";
47 break;
49 case REF_STATUS_REJECT_FETCH_FIRST:
50 res = "error";
51 msg = "fetch first";
52 break;
54 case REF_STATUS_REJECT_NEEDS_FORCE:
55 res = "error";
56 msg = "needs force";
57 break;
59 case REF_STATUS_REJECT_STALE:
60 res = "error";
61 msg = "stale info";
62 break;
64 case REF_STATUS_REJECT_ALREADY_EXISTS:
65 res = "error";
66 msg = "already exists";
67 break;
69 case REF_STATUS_REJECT_NODELETE:
70 case REF_STATUS_REMOTE_REJECT:
71 res = "error";
72 break;
74 case REF_STATUS_EXPECTING_REPORT:
75 default:
76 continue;
79 strbuf_reset(&buf);
80 strbuf_addf(&buf, "%s %s", res, ref->name);
81 if (ref->remote_status)
82 msg = ref->remote_status;
83 if (msg) {
84 strbuf_addch(&buf, ' ');
85 quote_two_c_style(&buf, "", msg, 0);
87 strbuf_addch(&buf, '\n');
89 write_or_die(1, buf.buf, buf.len);
91 strbuf_release(&buf);
94 int cmd_send_pack(int argc, const char **argv, const char *prefix)
96 int i, nr_refspecs = 0;
97 const char **refspecs = NULL;
98 const char *remote_name = NULL;
99 struct remote *remote = NULL;
100 const char *dest = NULL;
101 int fd[2];
102 struct child_process *conn;
103 struct sha1_array extra_have = SHA1_ARRAY_INIT;
104 struct sha1_array shallow = SHA1_ARRAY_INIT;
105 struct ref *remote_refs, *local_refs;
106 int ret;
107 int helper_status = 0;
108 int send_all = 0;
109 const char *receivepack = "git-receive-pack";
110 int flags;
111 unsigned int reject_reasons;
112 int progress = -1;
113 int from_stdin = 0;
114 struct push_cas_option cas = {0};
116 argv++;
117 for (i = 1; i < argc; i++, argv++) {
118 const char *arg = *argv;
120 if (*arg == '-') {
121 if (starts_with(arg, "--receive-pack=")) {
122 receivepack = arg + 15;
123 continue;
125 if (starts_with(arg, "--exec=")) {
126 receivepack = arg + 7;
127 continue;
129 if (starts_with(arg, "--remote=")) {
130 remote_name = arg + 9;
131 continue;
133 if (!strcmp(arg, "--all")) {
134 send_all = 1;
135 continue;
137 if (!strcmp(arg, "--dry-run")) {
138 args.dry_run = 1;
139 continue;
141 if (!strcmp(arg, "--mirror")) {
142 args.send_mirror = 1;
143 continue;
145 if (!strcmp(arg, "--force")) {
146 args.force_update = 1;
147 continue;
149 if (!strcmp(arg, "--quiet")) {
150 args.quiet = 1;
151 continue;
153 if (!strcmp(arg, "--verbose")) {
154 args.verbose = 1;
155 continue;
157 if (!strcmp(arg, "--signed")) {
158 args.push_cert = 1;
159 continue;
161 if (!strcmp(arg, "--progress")) {
162 progress = 1;
163 continue;
165 if (!strcmp(arg, "--no-progress")) {
166 progress = 0;
167 continue;
169 if (!strcmp(arg, "--thin")) {
170 args.use_thin_pack = 1;
171 continue;
173 if (!strcmp(arg, "--stateless-rpc")) {
174 args.stateless_rpc = 1;
175 continue;
177 if (!strcmp(arg, "--stdin")) {
178 from_stdin = 1;
179 continue;
181 if (!strcmp(arg, "--helper-status")) {
182 helper_status = 1;
183 continue;
185 if (!strcmp(arg, "--" CAS_OPT_NAME)) {
186 if (parse_push_cas_option(&cas, NULL, 0) < 0)
187 exit(1);
188 continue;
190 if (!strcmp(arg, "--no-" CAS_OPT_NAME)) {
191 if (parse_push_cas_option(&cas, NULL, 1) < 0)
192 exit(1);
193 continue;
195 if (starts_with(arg, "--" CAS_OPT_NAME "=")) {
196 if (parse_push_cas_option(&cas,
197 strchr(arg, '=') + 1, 0) < 0)
198 exit(1);
199 continue;
201 usage(send_pack_usage);
203 if (!dest) {
204 dest = arg;
205 continue;
207 refspecs = (const char **) argv;
208 nr_refspecs = argc - i;
209 break;
211 if (!dest)
212 usage(send_pack_usage);
214 if (from_stdin) {
215 struct argv_array all_refspecs = ARGV_ARRAY_INIT;
217 for (i = 0; i < nr_refspecs; i++)
218 argv_array_push(&all_refspecs, refspecs[i]);
220 if (args.stateless_rpc) {
221 const char *buf;
222 while ((buf = packet_read_line(0, NULL)))
223 argv_array_push(&all_refspecs, buf);
224 } else {
225 struct strbuf line = STRBUF_INIT;
226 while (strbuf_getline(&line, stdin, '\n') != EOF)
227 argv_array_push(&all_refspecs, line.buf);
228 strbuf_release(&line);
231 refspecs = all_refspecs.argv;
232 nr_refspecs = all_refspecs.argc;
236 * --all and --mirror are incompatible; neither makes sense
237 * with any refspecs.
239 if ((refspecs && (send_all || args.send_mirror)) ||
240 (send_all && args.send_mirror))
241 usage(send_pack_usage);
243 if (remote_name) {
244 remote = remote_get(remote_name);
245 if (!remote_has_url(remote, dest)) {
246 die("Destination %s is not a uri for %s",
247 dest, remote_name);
251 if (progress == -1)
252 progress = !args.quiet && isatty(2);
253 args.progress = progress;
255 if (args.stateless_rpc) {
256 conn = NULL;
257 fd[0] = 0;
258 fd[1] = 1;
259 } else {
260 conn = git_connect(fd, dest, receivepack,
261 args.verbose ? CONNECT_VERBOSE : 0);
264 get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL,
265 &extra_have, &shallow);
267 transport_verify_remote_names(nr_refspecs, refspecs);
269 local_refs = get_local_heads();
271 flags = MATCH_REFS_NONE;
273 if (send_all)
274 flags |= MATCH_REFS_ALL;
275 if (args.send_mirror)
276 flags |= MATCH_REFS_MIRROR;
278 /* match them up */
279 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
280 return -1;
282 if (!is_empty_cas(&cas))
283 apply_push_cas(&cas, remote, remote_refs);
285 set_ref_status_for_push(remote_refs, args.send_mirror,
286 args.force_update);
288 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
290 if (helper_status)
291 print_helper_status(remote_refs);
293 close(fd[1]);
294 close(fd[0]);
296 ret |= finish_connect(conn);
298 if (!helper_status)
299 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
301 if (!args.dry_run && remote) {
302 struct ref *ref;
303 for (ref = remote_refs; ref; ref = ref->next)
304 transport_update_tracking_ref(remote, ref, args.verbose);
307 if (!ret && !transport_refs_pushed(remote_refs))
308 fprintf(stderr, "Everything up-to-date\n");
310 return ret;