bash prompt: mention that PROMPT_COMMAND mode is faster
[alt-git.git] / builtin / send-pack.c
blob152c4ea092a44ff9e70e293ddeecb4ad7105e25d
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"
13 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.";
17 static struct send_pack_args args;
19 static void print_helper_status(struct ref *ref)
21 struct strbuf buf = STRBUF_INIT;
23 for (; ref; ref = ref->next) {
24 const char *msg = NULL;
25 const char *res;
27 switch(ref->status) {
28 case REF_STATUS_NONE:
29 res = "error";
30 msg = "no match";
31 break;
33 case REF_STATUS_OK:
34 res = "ok";
35 break;
37 case REF_STATUS_UPTODATE:
38 res = "ok";
39 msg = "up to date";
40 break;
42 case REF_STATUS_REJECT_NONFASTFORWARD:
43 res = "error";
44 msg = "non-fast forward";
45 break;
47 case REF_STATUS_REJECT_FETCH_FIRST:
48 res = "error";
49 msg = "fetch first";
50 break;
52 case REF_STATUS_REJECT_NEEDS_FORCE:
53 res = "error";
54 msg = "needs force";
55 break;
57 case REF_STATUS_REJECT_ALREADY_EXISTS:
58 res = "error";
59 msg = "already exists";
60 break;
62 case REF_STATUS_REJECT_NODELETE:
63 case REF_STATUS_REMOTE_REJECT:
64 res = "error";
65 break;
67 case REF_STATUS_EXPECTING_REPORT:
68 default:
69 continue;
72 strbuf_reset(&buf);
73 strbuf_addf(&buf, "%s %s", res, ref->name);
74 if (ref->remote_status)
75 msg = ref->remote_status;
76 if (msg) {
77 strbuf_addch(&buf, ' ');
78 quote_two_c_style(&buf, "", msg, 0);
80 strbuf_addch(&buf, '\n');
82 write_or_die(1, buf.buf, buf.len);
84 strbuf_release(&buf);
87 int cmd_send_pack(int argc, const char **argv, const char *prefix)
89 int i, nr_refspecs = 0;
90 const char **refspecs = NULL;
91 const char *remote_name = NULL;
92 struct remote *remote = NULL;
93 const char *dest = NULL;
94 int fd[2];
95 struct child_process *conn;
96 struct extra_have_objects extra_have;
97 struct ref *remote_refs, *local_refs;
98 int ret;
99 int helper_status = 0;
100 int send_all = 0;
101 const char *receivepack = "git-receive-pack";
102 int flags;
103 unsigned int reject_reasons;
104 int progress = -1;
106 argv++;
107 for (i = 1; i < argc; i++, argv++) {
108 const char *arg = *argv;
110 if (*arg == '-') {
111 if (!prefixcmp(arg, "--receive-pack=")) {
112 receivepack = arg + 15;
113 continue;
115 if (!prefixcmp(arg, "--exec=")) {
116 receivepack = arg + 7;
117 continue;
119 if (!prefixcmp(arg, "--remote=")) {
120 remote_name = arg + 9;
121 continue;
123 if (!strcmp(arg, "--all")) {
124 send_all = 1;
125 continue;
127 if (!strcmp(arg, "--dry-run")) {
128 args.dry_run = 1;
129 continue;
131 if (!strcmp(arg, "--mirror")) {
132 args.send_mirror = 1;
133 continue;
135 if (!strcmp(arg, "--force")) {
136 args.force_update = 1;
137 continue;
139 if (!strcmp(arg, "--quiet")) {
140 args.quiet = 1;
141 continue;
143 if (!strcmp(arg, "--verbose")) {
144 args.verbose = 1;
145 continue;
147 if (!strcmp(arg, "--progress")) {
148 progress = 1;
149 continue;
151 if (!strcmp(arg, "--no-progress")) {
152 progress = 0;
153 continue;
155 if (!strcmp(arg, "--thin")) {
156 args.use_thin_pack = 1;
157 continue;
159 if (!strcmp(arg, "--stateless-rpc")) {
160 args.stateless_rpc = 1;
161 continue;
163 if (!strcmp(arg, "--helper-status")) {
164 helper_status = 1;
165 continue;
167 usage(send_pack_usage);
169 if (!dest) {
170 dest = arg;
171 continue;
173 refspecs = (const char **) argv;
174 nr_refspecs = argc - i;
175 break;
177 if (!dest)
178 usage(send_pack_usage);
180 * --all and --mirror are incompatible; neither makes sense
181 * with any refspecs.
183 if ((refspecs && (send_all || args.send_mirror)) ||
184 (send_all && args.send_mirror))
185 usage(send_pack_usage);
187 if (remote_name) {
188 remote = remote_get(remote_name);
189 if (!remote_has_url(remote, dest)) {
190 die("Destination %s is not a uri for %s",
191 dest, remote_name);
195 if (progress == -1)
196 progress = !args.quiet && isatty(2);
197 args.progress = progress;
199 if (args.stateless_rpc) {
200 conn = NULL;
201 fd[0] = 0;
202 fd[1] = 1;
203 } else {
204 conn = git_connect(fd, dest, receivepack,
205 args.verbose ? CONNECT_VERBOSE : 0);
208 memset(&extra_have, 0, sizeof(extra_have));
210 get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL, &extra_have);
212 transport_verify_remote_names(nr_refspecs, refspecs);
214 local_refs = get_local_heads();
216 flags = MATCH_REFS_NONE;
218 if (send_all)
219 flags |= MATCH_REFS_ALL;
220 if (args.send_mirror)
221 flags |= MATCH_REFS_MIRROR;
223 /* match them up */
224 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
225 return -1;
227 set_ref_status_for_push(remote_refs, args.send_mirror,
228 args.force_update);
230 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
232 if (helper_status)
233 print_helper_status(remote_refs);
235 close(fd[1]);
236 close(fd[0]);
238 ret |= finish_connect(conn);
240 if (!helper_status)
241 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
243 if (!args.dry_run && remote) {
244 struct ref *ref;
245 for (ref = remote_refs; ref; ref = ref->next)
246 transport_update_tracking_ref(remote, ref, args.verbose);
249 if (!ret && !transport_refs_pushed(remote_refs))
250 fprintf(stderr, "Everything up-to-date\n");
252 return ret;