]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/send-pack.c
replace {pre,suf}fixcmp() with {starts,ends}_with()
[thirdparty/git.git] / builtin / send-pack.c
CommitLineData
c2e86add 1#include "builtin.h"
2a9c3fe8 2#include "commit.h"
584c6cc9 3#include "refs.h"
f3a3214e 4#include "pkt-line.h"
de1a2fdd 5#include "sideband.h"
38b1c662 6#include "run-command.h"
6b62816c 7#include "remote.h"
47a59185 8#include "connect.h"
96249c04 9#include "send-pack.h"
de1a2fdd 10#include "quote.h"
f1863d0d 11#include "transport.h"
ff5effdf 12#include "version.h"
61221472 13
2a245013 14static const char send_pack_usage[] =
1b1dd23f 15"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
18bd8821 16" --all and explicit <ref> specification are mutually exclusive.";
96249c04 17
6828f72f 18static struct send_pack_args args;
61221472 19
de1a2fdd
SP
20static void print_helper_status(struct ref *ref)
21{
22 struct strbuf buf = STRBUF_INIT;
23
24 for (; ref; ref = ref->next) {
25 const char *msg = NULL;
26 const char *res;
27
28 switch(ref->status) {
29 case REF_STATUS_NONE:
30 res = "error";
31 msg = "no match";
32 break;
33
34 case REF_STATUS_OK:
35 res = "ok";
36 break;
37
38 case REF_STATUS_UPTODATE:
39 res = "ok";
40 msg = "up to date";
41 break;
42
43 case REF_STATUS_REJECT_NONFASTFORWARD:
44 res = "error";
45 msg = "non-fast forward";
dbfeddb1
CR
46 break;
47
75e5c0dc
JH
48 case REF_STATUS_REJECT_FETCH_FIRST:
49 res = "error";
50 msg = "fetch first";
51 break;
52
53 case REF_STATUS_REJECT_NEEDS_FORCE:
54 res = "error";
55 msg = "needs force";
56 break;
57
631b5ef2
JH
58 case REF_STATUS_REJECT_STALE:
59 res = "error";
60 msg = "stale info";
61 break;
62
dbfeddb1
CR
63 case REF_STATUS_REJECT_ALREADY_EXISTS:
64 res = "error";
65 msg = "already exists";
de1a2fdd
SP
66 break;
67
68 case REF_STATUS_REJECT_NODELETE:
69 case REF_STATUS_REMOTE_REJECT:
70 res = "error";
71 break;
72
73 case REF_STATUS_EXPECTING_REPORT:
74 default:
75 continue;
76 }
77
78 strbuf_reset(&buf);
79 strbuf_addf(&buf, "%s %s", res, ref->name);
80 if (ref->remote_status)
81 msg = ref->remote_status;
82 if (msg) {
83 strbuf_addch(&buf, ' ');
84 quote_two_c_style(&buf, "", msg, 0);
85 }
86 strbuf_addch(&buf, '\n');
87
cdf4fb8e 88 write_or_die(1, buf.buf, buf.len);
de1a2fdd
SP
89 }
90 strbuf_release(&buf);
91}
92
96249c04 93int cmd_send_pack(int argc, const char **argv, const char *prefix)
61221472 94{
64fcef2d
DB
95 int i, nr_refspecs = 0;
96 const char **refspecs = NULL;
96249c04 97 const char *remote_name = NULL;
b516968f 98 struct remote *remote = NULL;
96249c04 99 const char *dest = NULL;
64fcef2d
DB
100 int fd[2];
101 struct child_process *conn;
102 struct extra_have_objects extra_have;
6d2bf96e 103 struct ref *remote_refs, *local_refs;
64fcef2d 104 int ret;
de1a2fdd 105 int helper_status = 0;
64fcef2d
DB
106 int send_all = 0;
107 const char *receivepack = "git-receive-pack";
108 int flags;
10643d4e 109 unsigned int reject_reasons;
391b1f20 110 int progress = -1;
28f5d176 111 struct push_cas_option cas = {0};
84a9b58c 112
61221472 113 argv++;
d089391c 114 for (i = 1; i < argc; i++, argv++) {
96249c04 115 const char *arg = *argv;
61221472
LT
116
117 if (*arg == '-') {
59556548 118 if (starts_with(arg, "--receive-pack=")) {
64fcef2d 119 receivepack = arg + 15;
d23842fd
UKK
120 continue;
121 }
59556548 122 if (starts_with(arg, "--exec=")) {
64fcef2d 123 receivepack = arg + 7;
61221472
LT
124 continue;
125 }
59556548 126 if (starts_with(arg, "--remote=")) {
b516968f
DB
127 remote_name = arg + 9;
128 continue;
129 }
d089391c 130 if (!strcmp(arg, "--all")) {
64fcef2d 131 send_all = 1;
d089391c
LT
132 continue;
133 }
a63103ae 134 if (!strcmp(arg, "--dry-run")) {
96249c04 135 args.dry_run = 1;
a63103ae
BE
136 continue;
137 }
28b9d6e5
AW
138 if (!strcmp(arg, "--mirror")) {
139 args.send_mirror = 1;
140 continue;
141 }
2a9c3fe8 142 if (!strcmp(arg, "--force")) {
96249c04 143 args.force_update = 1;
2a9c3fe8
LT
144 continue;
145 }
c207e34f
CB
146 if (!strcmp(arg, "--quiet")) {
147 args.quiet = 1;
148 continue;
149 }
41f93a2c 150 if (!strcmp(arg, "--verbose")) {
96249c04 151 args.verbose = 1;
41f93a2c
LT
152 continue;
153 }
391b1f20
JK
154 if (!strcmp(arg, "--progress")) {
155 progress = 1;
156 continue;
157 }
158 if (!strcmp(arg, "--no-progress")) {
159 progress = 0;
160 continue;
161 }
2245be3e 162 if (!strcmp(arg, "--thin")) {
96249c04 163 args.use_thin_pack = 1;
2245be3e
JH
164 continue;
165 }
de1a2fdd
SP
166 if (!strcmp(arg, "--stateless-rpc")) {
167 args.stateless_rpc = 1;
168 continue;
169 }
170 if (!strcmp(arg, "--helper-status")) {
171 helper_status = 1;
172 continue;
173 }
28f5d176
JH
174 if (!strcmp(arg, "--" CAS_OPT_NAME)) {
175 if (parse_push_cas_option(&cas, NULL, 0) < 0)
176 exit(1);
177 continue;
178 }
179 if (!strcmp(arg, "--no-" CAS_OPT_NAME)) {
180 if (parse_push_cas_option(&cas, NULL, 1) < 0)
181 exit(1);
182 continue;
183 }
59556548 184 if (starts_with(arg, "--" CAS_OPT_NAME "=")) {
28f5d176 185 if (parse_push_cas_option(&cas,
77aa9348 186 strchr(arg, '=') + 1, 0) < 0)
28f5d176
JH
187 exit(1);
188 continue;
189 }
61221472
LT
190 usage(send_pack_usage);
191 }
d089391c
LT
192 if (!dest) {
193 dest = arg;
194 continue;
195 }
64fcef2d
DB
196 refspecs = (const char **) argv;
197 nr_refspecs = argc - i;
61221472
LT
198 break;
199 }
200 if (!dest)
201 usage(send_pack_usage);
28b9d6e5
AW
202 /*
203 * --all and --mirror are incompatible; neither makes sense
204 * with any refspecs.
205 */
64fcef2d
DB
206 if ((refspecs && (send_all || args.send_mirror)) ||
207 (send_all && args.send_mirror))
0bc3cdfc 208 usage(send_pack_usage);
37adac76 209
b516968f
DB
210 if (remote_name) {
211 remote = remote_get(remote_name);
28b91f8a 212 if (!remote_has_url(remote, dest)) {
b516968f
DB
213 die("Destination %s is not a uri for %s",
214 dest, remote_name);
215 }
216 }
217
391b1f20
JK
218 if (progress == -1)
219 progress = !args.quiet && isatty(2);
220 args.progress = progress;
8d32e60d 221
de1a2fdd
SP
222 if (args.stateless_rpc) {
223 conn = NULL;
224 fd[0] = 0;
225 fd[1] = 1;
226 } else {
5a277f3f 227 conn = git_connect(fd, dest, receivepack,
de1a2fdd
SP
228 args.verbose ? CONNECT_VERBOSE : 0);
229 }
96249c04 230
64fcef2d
DB
231 memset(&extra_have, 0, sizeof(extra_have));
232
85edf4f5 233 get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL, &extra_have);
64fcef2d 234
f1863d0d 235 transport_verify_remote_names(nr_refspecs, refspecs);
64fcef2d
DB
236
237 local_refs = get_local_heads();
96249c04 238
64fcef2d
DB
239 flags = MATCH_REFS_NONE;
240
241 if (send_all)
242 flags |= MATCH_REFS_ALL;
243 if (args.send_mirror)
244 flags |= MATCH_REFS_MIRROR;
245
246 /* match them up */
29753cdd 247 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
64fcef2d 248 return -1;
96249c04 249
91048a95
JH
250 if (!is_empty_cas(&cas))
251 apply_push_cas(&cas, remote, remote_refs);
252
20e8b465
TRC
253 set_ref_status_for_push(remote_refs, args.send_mirror,
254 args.force_update);
255
64fcef2d 256 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
96249c04 257
de1a2fdd
SP
258 if (helper_status)
259 print_helper_status(remote_refs);
260
64fcef2d 261 close(fd[1]);
7f8e9828 262 close(fd[0]);
64fcef2d 263
98158e9c 264 ret |= finish_connect(conn);
64fcef2d 265
de1a2fdd 266 if (!helper_status)
10643d4e 267 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
64fcef2d
DB
268
269 if (!args.dry_run && remote) {
270 struct ref *ref;
271 for (ref = remote_refs; ref; ref = ref->next)
f1863d0d 272 transport_update_tracking_ref(remote, ref, args.verbose);
64fcef2d
DB
273 }
274
f1863d0d 275 if (!ret && !transport_refs_pushed(remote_refs))
64fcef2d
DB
276 fprintf(stderr, "Everything up-to-date\n");
277
278 return ret;
61221472 279}