Merge branch 'bc/http-backend-allow-405' into maint
[git] / builtin / send-pack.c
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"
12
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.";
16
17 static struct send_pack_args args;
18
19 static void print_helper_status(struct ref *ref)
20 {
21         struct strbuf buf = STRBUF_INIT;
22
23         for (; ref; ref = ref->next) {
24                 const char *msg = NULL;
25                 const char *res;
26
27                 switch(ref->status) {
28                 case REF_STATUS_NONE:
29                         res = "error";
30                         msg = "no match";
31                         break;
32
33                 case REF_STATUS_OK:
34                         res = "ok";
35                         break;
36
37                 case REF_STATUS_UPTODATE:
38                         res = "ok";
39                         msg = "up to date";
40                         break;
41
42                 case REF_STATUS_REJECT_NONFASTFORWARD:
43                         res = "error";
44                         msg = "non-fast forward";
45                         break;
46
47                 case REF_STATUS_REJECT_FETCH_FIRST:
48                         res = "error";
49                         msg = "fetch first";
50                         break;
51
52                 case REF_STATUS_REJECT_NEEDS_FORCE:
53                         res = "error";
54                         msg = "needs force";
55                         break;
56
57                 case REF_STATUS_REJECT_ALREADY_EXISTS:
58                         res = "error";
59                         msg = "already exists";
60                         break;
61
62                 case REF_STATUS_REJECT_NODELETE:
63                 case REF_STATUS_REMOTE_REJECT:
64                         res = "error";
65                         break;
66
67                 case REF_STATUS_EXPECTING_REPORT:
68                 default:
69                         continue;
70                 }
71
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);
79                 }
80                 strbuf_addch(&buf, '\n');
81
82                 write_or_die(1, buf.buf, buf.len);
83         }
84         strbuf_release(&buf);
85 }
86
87 int cmd_send_pack(int argc, const char **argv, const char *prefix)
88 {
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;
105
106         argv++;
107         for (i = 1; i < argc; i++, argv++) {
108                 const char *arg = *argv;
109
110                 if (*arg == '-') {
111                         if (!prefixcmp(arg, "--receive-pack=")) {
112                                 receivepack = arg + 15;
113                                 continue;
114                         }
115                         if (!prefixcmp(arg, "--exec=")) {
116                                 receivepack = arg + 7;
117                                 continue;
118                         }
119                         if (!prefixcmp(arg, "--remote=")) {
120                                 remote_name = arg + 9;
121                                 continue;
122                         }
123                         if (!strcmp(arg, "--all")) {
124                                 send_all = 1;
125                                 continue;
126                         }
127                         if (!strcmp(arg, "--dry-run")) {
128                                 args.dry_run = 1;
129                                 continue;
130                         }
131                         if (!strcmp(arg, "--mirror")) {
132                                 args.send_mirror = 1;
133                                 continue;
134                         }
135                         if (!strcmp(arg, "--force")) {
136                                 args.force_update = 1;
137                                 continue;
138                         }
139                         if (!strcmp(arg, "--quiet")) {
140                                 args.quiet = 1;
141                                 continue;
142                         }
143                         if (!strcmp(arg, "--verbose")) {
144                                 args.verbose = 1;
145                                 continue;
146                         }
147                         if (!strcmp(arg, "--progress")) {
148                                 progress = 1;
149                                 continue;
150                         }
151                         if (!strcmp(arg, "--no-progress")) {
152                                 progress = 0;
153                                 continue;
154                         }
155                         if (!strcmp(arg, "--thin")) {
156                                 args.use_thin_pack = 1;
157                                 continue;
158                         }
159                         if (!strcmp(arg, "--stateless-rpc")) {
160                                 args.stateless_rpc = 1;
161                                 continue;
162                         }
163                         if (!strcmp(arg, "--helper-status")) {
164                                 helper_status = 1;
165                                 continue;
166                         }
167                         usage(send_pack_usage);
168                 }
169                 if (!dest) {
170                         dest = arg;
171                         continue;
172                 }
173                 refspecs = (const char **) argv;
174                 nr_refspecs = argc - i;
175                 break;
176         }
177         if (!dest)
178                 usage(send_pack_usage);
179         /*
180          * --all and --mirror are incompatible; neither makes sense
181          * with any refspecs.
182          */
183         if ((refspecs && (send_all || args.send_mirror)) ||
184             (send_all && args.send_mirror))
185                 usage(send_pack_usage);
186
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);
192                 }
193         }
194
195         if (progress == -1)
196                 progress = !args.quiet && isatty(2);
197         args.progress = progress;
198
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);
206         }
207
208         memset(&extra_have, 0, sizeof(extra_have));
209
210         get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL, &extra_have);
211
212         transport_verify_remote_names(nr_refspecs, refspecs);
213
214         local_refs = get_local_heads();
215
216         flags = MATCH_REFS_NONE;
217
218         if (send_all)
219                 flags |= MATCH_REFS_ALL;
220         if (args.send_mirror)
221                 flags |= MATCH_REFS_MIRROR;
222
223         /* match them up */
224         if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
225                 return -1;
226
227         set_ref_status_for_push(remote_refs, args.send_mirror,
228                 args.force_update);
229
230         ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
231
232         if (helper_status)
233                 print_helper_status(remote_refs);
234
235         close(fd[1]);
236         close(fd[0]);
237
238         ret |= finish_connect(conn);
239
240         if (!helper_status)
241                 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
242
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);
247         }
248
249         if (!ret && !transport_refs_pushed(remote_refs))
250                 fprintf(stderr, "Everything up-to-date\n");
251
252         return ret;
253 }