Merge branch 'pf/editor-ignore-sigint'
[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_NODELETE:
48                 case REF_STATUS_REMOTE_REJECT:
49                         res = "error";
50                         break;
51
52                 case REF_STATUS_EXPECTING_REPORT:
53                 default:
54                         continue;
55                 }
56
57                 strbuf_reset(&buf);
58                 strbuf_addf(&buf, "%s %s", res, ref->name);
59                 if (ref->remote_status)
60                         msg = ref->remote_status;
61                 if (msg) {
62                         strbuf_addch(&buf, ' ');
63                         quote_two_c_style(&buf, "", msg, 0);
64                 }
65                 strbuf_addch(&buf, '\n');
66
67                 safe_write(1, buf.buf, buf.len);
68         }
69         strbuf_release(&buf);
70 }
71
72 int cmd_send_pack(int argc, const char **argv, const char *prefix)
73 {
74         int i, nr_refspecs = 0;
75         const char **refspecs = NULL;
76         const char *remote_name = NULL;
77         struct remote *remote = NULL;
78         const char *dest = NULL;
79         int fd[2];
80         struct child_process *conn;
81         struct extra_have_objects extra_have;
82         struct ref *remote_refs, *local_refs;
83         int ret;
84         int helper_status = 0;
85         int send_all = 0;
86         const char *receivepack = "git-receive-pack";
87         int flags;
88         int nonfastforward = 0;
89         int progress = -1;
90
91         argv++;
92         for (i = 1; i < argc; i++, argv++) {
93                 const char *arg = *argv;
94
95                 if (*arg == '-') {
96                         if (!prefixcmp(arg, "--receive-pack=")) {
97                                 receivepack = arg + 15;
98                                 continue;
99                         }
100                         if (!prefixcmp(arg, "--exec=")) {
101                                 receivepack = arg + 7;
102                                 continue;
103                         }
104                         if (!prefixcmp(arg, "--remote=")) {
105                                 remote_name = arg + 9;
106                                 continue;
107                         }
108                         if (!strcmp(arg, "--all")) {
109                                 send_all = 1;
110                                 continue;
111                         }
112                         if (!strcmp(arg, "--dry-run")) {
113                                 args.dry_run = 1;
114                                 continue;
115                         }
116                         if (!strcmp(arg, "--mirror")) {
117                                 args.send_mirror = 1;
118                                 continue;
119                         }
120                         if (!strcmp(arg, "--force")) {
121                                 args.force_update = 1;
122                                 continue;
123                         }
124                         if (!strcmp(arg, "--quiet")) {
125                                 args.quiet = 1;
126                                 continue;
127                         }
128                         if (!strcmp(arg, "--verbose")) {
129                                 args.verbose = 1;
130                                 continue;
131                         }
132                         if (!strcmp(arg, "--progress")) {
133                                 progress = 1;
134                                 continue;
135                         }
136                         if (!strcmp(arg, "--no-progress")) {
137                                 progress = 0;
138                                 continue;
139                         }
140                         if (!strcmp(arg, "--thin")) {
141                                 args.use_thin_pack = 1;
142                                 continue;
143                         }
144                         if (!strcmp(arg, "--stateless-rpc")) {
145                                 args.stateless_rpc = 1;
146                                 continue;
147                         }
148                         if (!strcmp(arg, "--helper-status")) {
149                                 helper_status = 1;
150                                 continue;
151                         }
152                         usage(send_pack_usage);
153                 }
154                 if (!dest) {
155                         dest = arg;
156                         continue;
157                 }
158                 refspecs = (const char **) argv;
159                 nr_refspecs = argc - i;
160                 break;
161         }
162         if (!dest)
163                 usage(send_pack_usage);
164         /*
165          * --all and --mirror are incompatible; neither makes sense
166          * with any refspecs.
167          */
168         if ((refspecs && (send_all || args.send_mirror)) ||
169             (send_all && args.send_mirror))
170                 usage(send_pack_usage);
171
172         if (remote_name) {
173                 remote = remote_get(remote_name);
174                 if (!remote_has_url(remote, dest)) {
175                         die("Destination %s is not a uri for %s",
176                             dest, remote_name);
177                 }
178         }
179
180         if (progress == -1)
181                 progress = !args.quiet && isatty(2);
182         args.progress = progress;
183
184         if (args.stateless_rpc) {
185                 conn = NULL;
186                 fd[0] = 0;
187                 fd[1] = 1;
188         } else {
189                 conn = git_connect(fd, dest, receivepack,
190                         args.verbose ? CONNECT_VERBOSE : 0);
191         }
192
193         memset(&extra_have, 0, sizeof(extra_have));
194
195         get_remote_heads(fd[0], &remote_refs, REF_NORMAL, &extra_have);
196
197         transport_verify_remote_names(nr_refspecs, refspecs);
198
199         local_refs = get_local_heads();
200
201         flags = MATCH_REFS_NONE;
202
203         if (send_all)
204                 flags |= MATCH_REFS_ALL;
205         if (args.send_mirror)
206                 flags |= MATCH_REFS_MIRROR;
207
208         /* match them up */
209         if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
210                 return -1;
211
212         set_ref_status_for_push(remote_refs, args.send_mirror,
213                 args.force_update);
214
215         ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
216
217         if (helper_status)
218                 print_helper_status(remote_refs);
219
220         close(fd[1]);
221         close(fd[0]);
222
223         ret |= finish_connect(conn);
224
225         if (!helper_status)
226                 transport_print_push_status(dest, remote_refs, args.verbose, 0, &nonfastforward);
227
228         if (!args.dry_run && remote) {
229                 struct ref *ref;
230                 for (ref = remote_refs; ref; ref = ref->next)
231                         transport_update_tracking_ref(remote, ref, args.verbose);
232         }
233
234         if (!ret && !transport_refs_pushed(remote_refs))
235                 fprintf(stderr, "Everything up-to-date\n");
236
237         return ret;
238 }