]>
Commit | Line | Data |
---|---|---|
755225de LT |
1 | /* |
2 | * "git push" | |
3 | */ | |
4 | #include "cache.h" | |
5 | #include "refs.h" | |
6 | #include "run-command.h" | |
7 | #include "builtin.h" | |
5751f490 | 8 | #include "remote.h" |
9b288516 | 9 | #include "transport.h" |
755225de | 10 | |
11f2441f | 11 | static const char push_usage[] = "git-push [--all] [--dry-run] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]"; |
755225de | 12 | |
18184f79 | 13 | static int thin, verbose; |
d23842fd | 14 | static const char *receivepack; |
755225de | 15 | |
96f1e58f DR |
16 | static const char **refspec; |
17 | static int refspec_nr; | |
755225de LT |
18 | |
19 | static void add_refspec(const char *ref) | |
20 | { | |
21 | int nr = refspec_nr + 1; | |
22 | refspec = xrealloc(refspec, nr * sizeof(char *)); | |
23 | refspec[nr-1] = ref; | |
24 | refspec_nr = nr; | |
25 | } | |
26 | ||
755225de LT |
27 | static void set_refspecs(const char **refs, int nr) |
28 | { | |
8558fd9e DB |
29 | int i; |
30 | for (i = 0; i < nr; i++) { | |
31 | const char *ref = refs[i]; | |
32 | if (!strcmp("tag", ref)) { | |
33 | char *tag; | |
34 | int len; | |
35 | if (nr <= ++i) | |
36 | die("tag shorthand without <tag>"); | |
37 | len = strlen(refs[i]) + 11; | |
38 | tag = xmalloc(len); | |
39 | strcpy(tag, "refs/tags/"); | |
40 | strcat(tag, refs[i]); | |
41 | ref = tag; | |
411fb8ba | 42 | } |
8558fd9e | 43 | add_refspec(ref); |
755225de | 44 | } |
755225de LT |
45 | } |
46 | ||
9b288516 | 47 | static int do_push(const char *repo, int flags) |
755225de | 48 | { |
5751f490 | 49 | int i, errs; |
5751f490 | 50 | struct remote *remote = remote_get(repo); |
755225de | 51 | |
5751f490 | 52 | if (!remote) |
755225de LT |
53 | die("bad repository '%s'", repo); |
54 | ||
18184f79 SP |
55 | if (!refspec |
56 | && !(flags & TRANSPORT_PUSH_ALL) | |
57 | && remote->push_refspec_nr) { | |
8558fd9e DB |
58 | refspec = remote->push_refspec; |
59 | refspec_nr = remote->push_refspec_nr; | |
5751f490 | 60 | } |
fd1d1b05 | 61 | errs = 0; |
28b91f8a | 62 | for (i = 0; i < remote->url_nr; i++) { |
9b288516 | 63 | struct transport *transport = |
28b91f8a | 64 | transport_get(remote, remote->url[i]); |
60b7f38e | 65 | int err; |
9b288516 DB |
66 | if (receivepack) |
67 | transport_set_option(transport, | |
68 | TRANS_OPT_RECEIVEPACK, receivepack); | |
69 | if (thin) | |
70 | transport_set_option(transport, TRANS_OPT_THIN, "yes"); | |
71 | ||
bcc785f6 | 72 | if (verbose) |
28b91f8a | 73 | fprintf(stderr, "Pushing to %s\n", remote->url[i]); |
9b288516 DB |
74 | err = transport_push(transport, refspec_nr, refspec, flags); |
75 | err |= transport_disconnect(transport); | |
76 | ||
60b7f38e | 77 | if (!err) |
755225de | 78 | continue; |
39878b0c | 79 | |
28b91f8a | 80 | error("failed to push to '%s'", remote->url[i]); |
fd1d1b05 | 81 | errs++; |
755225de | 82 | } |
fd1d1b05 | 83 | return !!errs; |
755225de LT |
84 | } |
85 | ||
a633fca0 | 86 | int cmd_push(int argc, const char **argv, const char *prefix) |
755225de LT |
87 | { |
88 | int i; | |
9b288516 | 89 | int flags = 0; |
5751f490 | 90 | const char *repo = NULL; /* default repository */ |
755225de LT |
91 | |
92 | for (i = 1; i < argc; i++) { | |
93 | const char *arg = argv[i]; | |
94 | ||
95 | if (arg[0] != '-') { | |
96 | repo = arg; | |
97 | i++; | |
98 | break; | |
99 | } | |
bcc785f6 LT |
100 | if (!strcmp(arg, "-v")) { |
101 | verbose=1; | |
102 | continue; | |
103 | } | |
cc44c765 | 104 | if (!prefixcmp(arg, "--repo=")) { |
bcc785f6 LT |
105 | repo = arg+7; |
106 | continue; | |
107 | } | |
755225de | 108 | if (!strcmp(arg, "--all")) { |
9b288516 | 109 | flags |= TRANSPORT_PUSH_ALL; |
755225de LT |
110 | continue; |
111 | } | |
11f2441f | 112 | if (!strcmp(arg, "--dry-run")) { |
2e13e5d8 | 113 | flags |= TRANSPORT_PUSH_DRY_RUN; |
11f2441f BE |
114 | continue; |
115 | } | |
755225de | 116 | if (!strcmp(arg, "--tags")) { |
8558fd9e | 117 | add_refspec("refs/tags/*"); |
755225de LT |
118 | continue; |
119 | } | |
8f615493 | 120 | if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) { |
9b288516 | 121 | flags |= TRANSPORT_PUSH_FORCE; |
755225de LT |
122 | continue; |
123 | } | |
124 | if (!strcmp(arg, "--thin")) { | |
125 | thin = 1; | |
126 | continue; | |
127 | } | |
128 | if (!strcmp(arg, "--no-thin")) { | |
129 | thin = 0; | |
130 | continue; | |
131 | } | |
cc44c765 | 132 | if (!prefixcmp(arg, "--receive-pack=")) { |
9b288516 | 133 | receivepack = arg + 15; |
d23842fd UKK |
134 | continue; |
135 | } | |
cc44c765 | 136 | if (!prefixcmp(arg, "--exec=")) { |
9b288516 | 137 | receivepack = arg + 7; |
755225de LT |
138 | continue; |
139 | } | |
140 | usage(push_usage); | |
141 | } | |
142 | set_refspecs(argv + i, argc - i); | |
18184f79 | 143 | if ((flags & TRANSPORT_PUSH_ALL) && refspec) |
8558fd9e DB |
144 | usage(push_usage); |
145 | ||
9b288516 | 146 | return do_push(repo, flags); |
755225de | 147 | } |