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 "send-pack.h"
9#include "quote.h"
10#include "transport.h"
11#include "version.h"
1213
static const char send_pack_usage[] =
14"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
15" --all and explicit <ref> specification are mutually exclusive.";
1617
static struct send_pack_args args;
1819
static void print_helper_status(struct ref *ref)
20{
21struct strbuf buf = STRBUF_INIT;
2223
for (; ref; ref = ref->next) {
24const char *msg = NULL;
25const char *res;
2627
switch(ref->status) {
28case REF_STATUS_NONE:
29res = "error";
30msg = "no match";
31break;
3233
case REF_STATUS_OK:
34res = "ok";
35break;
3637
case REF_STATUS_UPTODATE:
38res = "ok";
39msg = "up to date";
40break;
4142
case REF_STATUS_REJECT_NONFASTFORWARD:
43res = "error";
44msg = "non-fast forward";
45break;
4647
case REF_STATUS_REJECT_FETCH_FIRST:
48res = "error";
49msg = "fetch first";
50break;
5152
case REF_STATUS_REJECT_NEEDS_FORCE:
53res = "error";
54msg = "needs force";
55break;
5657
case REF_STATUS_REJECT_ALREADY_EXISTS:
58res = "error";
59msg = "already exists";
60break;
6162
case REF_STATUS_REJECT_NODELETE:
63case REF_STATUS_REMOTE_REJECT:
64res = "error";
65break;
6667
case REF_STATUS_EXPECTING_REPORT:
68default:
69continue;
70}
7172
strbuf_reset(&buf);
73strbuf_addf(&buf, "%s %s", res, ref->name);
74if (ref->remote_status)
75msg = ref->remote_status;
76if (msg) {
77strbuf_addch(&buf, ' ');
78quote_two_c_style(&buf, "", msg, 0);
79}
80strbuf_addch(&buf, '\n');
8182
safe_write(1, buf.buf, buf.len);
83}
84strbuf_release(&buf);
85}
8687
int cmd_send_pack(int argc, const char **argv, const char *prefix)
88{
89int i, nr_refspecs = 0;
90const char **refspecs = NULL;
91const char *remote_name = NULL;
92struct remote *remote = NULL;
93const char *dest = NULL;
94int fd[2];
95struct child_process *conn;
96struct extra_have_objects extra_have;
97struct ref *remote_refs, *local_refs;
98int ret;
99int helper_status = 0;
100int send_all = 0;
101const char *receivepack = "git-receive-pack";
102int flags;
103unsigned int reject_reasons;
104int progress = -1;
105106
argv++;
107for (i = 1; i < argc; i++, argv++) {
108const char *arg = *argv;
109110
if (*arg == '-') {
111if (!prefixcmp(arg, "--receive-pack=")) {
112receivepack = arg + 15;
113continue;
114}
115if (!prefixcmp(arg, "--exec=")) {
116receivepack = arg + 7;
117continue;
118}
119if (!prefixcmp(arg, "--remote=")) {
120remote_name = arg + 9;
121continue;
122}
123if (!strcmp(arg, "--all")) {
124send_all = 1;
125continue;
126}
127if (!strcmp(arg, "--dry-run")) {
128args.dry_run = 1;
129continue;
130}
131if (!strcmp(arg, "--mirror")) {
132args.send_mirror = 1;
133continue;
134}
135if (!strcmp(arg, "--force")) {
136args.force_update = 1;
137continue;
138}
139if (!strcmp(arg, "--quiet")) {
140args.quiet = 1;
141continue;
142}
143if (!strcmp(arg, "--verbose")) {
144args.verbose = 1;
145continue;
146}
147if (!strcmp(arg, "--progress")) {
148progress = 1;
149continue;
150}
151if (!strcmp(arg, "--no-progress")) {
152progress = 0;
153continue;
154}
155if (!strcmp(arg, "--thin")) {
156args.use_thin_pack = 1;
157continue;
158}
159if (!strcmp(arg, "--stateless-rpc")) {
160args.stateless_rpc = 1;
161continue;
162}
163if (!strcmp(arg, "--helper-status")) {
164helper_status = 1;
165continue;
166}
167usage(send_pack_usage);
168}
169if (!dest) {
170dest = arg;
171continue;
172}
173refspecs = (const char **) argv;
174nr_refspecs = argc - i;
175break;
176}
177if (!dest)
178usage(send_pack_usage);
179/*
180* --all and --mirror are incompatible; neither makes sense
181* with any refspecs.
182*/
183if ((refspecs && (send_all || args.send_mirror)) ||
184(send_all && args.send_mirror))
185usage(send_pack_usage);
186187
if (remote_name) {
188remote = remote_get(remote_name);
189if (!remote_has_url(remote, dest)) {
190die("Destination %s is not a uri for %s",
191dest, remote_name);
192}
193}
194195
if (progress == -1)
196progress = !args.quiet && isatty(2);
197args.progress = progress;
198199
if (args.stateless_rpc) {
200conn = NULL;
201fd[0] = 0;
202fd[1] = 1;
203} else {
204conn = git_connect(fd, dest, receivepack,
205args.verbose ? CONNECT_VERBOSE : 0);
206}
207208
memset(&extra_have, 0, sizeof(extra_have));
209210
get_remote_heads(fd[0], &remote_refs, REF_NORMAL, &extra_have);
211212
transport_verify_remote_names(nr_refspecs, refspecs);
213214
local_refs = get_local_heads();
215216
flags = MATCH_REFS_NONE;
217218
if (send_all)
219flags |= MATCH_REFS_ALL;
220if (args.send_mirror)
221flags |= MATCH_REFS_MIRROR;
222223
/* match them up */
224if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
225return -1;
226227
set_ref_status_for_push(remote_refs, args.send_mirror,
228args.force_update);
229230
ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
231232
if (helper_status)
233print_helper_status(remote_refs);
234235
close(fd[1]);
236close(fd[0]);
237238
ret |= finish_connect(conn);
239240
if (!helper_status)
241transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
242243
if (!args.dry_run && remote) {
244struct ref *ref;
245for (ref = remote_refs; ref; ref = ref->next)
246transport_update_tracking_ref(remote, ref, args.verbose);
247}
248249
if (!ret && !transport_refs_pushed(remote_refs))
250fprintf(stderr, "Everything up-to-date\n");
251252
return ret;
253}