Push code for transport library
This moves the code to call push backends into a library that can be extended to make matching fetch and push decisions based on the URL it gets, and which could be changed to have built-in implementations instead of calling external programs. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Daniel Barkalow committed
Sep 10, 2007 at 23:03 UTC
9b288516ee63cea91dd9d102edcdf112caea4c75
4 files changed
+280
-62
Makefile
+2
-1
@@ -310,7 +310,8 @@ LIB_OBJS = \
310
write_or_die.o trace.o list-objects.o grep.o match-trees.o \
311
alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
312
color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
313
- convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o
313
+ convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
314
+ transport.o
315
316
BUILTIN_OBJS = \
317
builtin-add.o \
builtin-push.c
+21
-61
@@ -6,10 +6,11 @@
6
#include "run-command.h"
7
#include "builtin.h"
8
#include "remote.h"
9
+#include "transport.h"
10
11
static const char push_usage[] = "git-push [--all] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]";
12
12
-static int all, force, thin, verbose;
13
+static int all, thin, verbose;
14
static const char *receivepack;
15
16
static const char **refspec;
@@ -43,80 +44,38 @@ static void set_refspecs(const char **refs, int nr)
44
}
45
}
46
46
-static int do_push(const char *repo)
47
+static int do_push(const char *repo, int flags)
48
{
49
int i, errs;
49
- int common_argc;
50
- const char **argv;
51
- int argc;
50
struct remote *remote = remote_get(repo);
51
52
if (!remote)
53
die("bad repository '%s'", repo);
54
57
- if (remote->receivepack) {
58
- char *rp = xmalloc(strlen(remote->receivepack) + 16);
59
- sprintf(rp, "--receive-pack=%s", remote->receivepack);
60
- receivepack = rp;
61
- }
55
if (!refspec && !all && remote->push_refspec_nr) {
56
refspec = remote->push_refspec;
57
refspec_nr = remote->push_refspec_nr;
58
}
66
-
67
- argv = xmalloc((refspec_nr + 10) * sizeof(char *));
68
- argv[0] = "dummy-send-pack";
69
- argc = 1;
70
- if (all)
71
- argv[argc++] = "--all";
72
- if (force)
73
- argv[argc++] = "--force";
74
- if (receivepack)
75
- argv[argc++] = receivepack;
76
- common_argc = argc;
77
-
59
errs = 0;
60
for (i = 0; i < remote->uri_nr; i++) {
61
+ struct transport *transport =
62
+ transport_get(remote, remote->uri[i], 0);
63
int err;
81
- int dest_argc = common_argc;
82
- int dest_refspec_nr = refspec_nr;
83
- const char **dest_refspec = refspec;
84
- const char *dest = remote->uri[i];
85
- const char *sender = "send-pack";
86
- if (!prefixcmp(dest, "http://") ||
87
- !prefixcmp(dest, "https://"))
88
- sender = "http-push";
89
- else {
90
- char *rem = xmalloc(strlen(remote->name) + 10);
91
- sprintf(rem, "--remote=%s", remote->name);
92
- argv[dest_argc++] = rem;
93
- if (thin)
94
- argv[dest_argc++] = "--thin";
95
- }
96
- argv[0] = sender;
97
- argv[dest_argc++] = dest;
98
- while (dest_refspec_nr--)
99
- argv[dest_argc++] = *dest_refspec++;
100
- argv[dest_argc] = NULL;
64
+ if (receivepack)
65
+ transport_set_option(transport,
66
+ TRANS_OPT_RECEIVEPACK, receivepack);
67
+ if (thin)
68
+ transport_set_option(transport, TRANS_OPT_THIN, "yes");
69
+
70
if (verbose)
102
- fprintf(stderr, "Pushing to %s\n", dest);
103
- err = run_command_v_opt(argv, RUN_GIT_CMD);
71
+ fprintf(stderr, "Pushing to %s\n", remote->uri[i]);
72
+ err = transport_push(transport, refspec_nr, refspec, flags);
73
+ err |= transport_disconnect(transport);
74
+
75
if (!err)
76
continue;
77
78
error("failed to push to '%s'", remote->uri[i]);
108
- switch (err) {
109
- case -ERR_RUN_COMMAND_FORK:
110
- error("unable to fork for %s", sender);
111
- case -ERR_RUN_COMMAND_EXEC:
112
- error("unable to exec %s", sender);
113
- break;
114
- case -ERR_RUN_COMMAND_WAITPID:
115
- case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
116
- case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
117
- case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
118
- error("%s died with strange error", sender);
119
- }
79
errs++;
80
}
81
return !!errs;
@@ -125,6 +84,7 @@ static int do_push(const char *repo)
84
int cmd_push(int argc, const char **argv, const char *prefix)
85
{
86
int i;
87
+ int flags = 0;
88
const char *repo = NULL; /* default repository */
89
90
for (i = 1; i < argc; i++) {
@@ -144,7 +104,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
104
continue;
105
}
106
if (!strcmp(arg, "--all")) {
147
- all = 1;
107
+ flags |= TRANSPORT_PUSH_ALL;
108
continue;
109
}
110
if (!strcmp(arg, "--tags")) {
@@ -152,7 +112,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
112
continue;
113
}
114
if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
155
- force = 1;
115
+ flags |= TRANSPORT_PUSH_FORCE;
116
continue;
117
}
118
if (!strcmp(arg, "--thin")) {
@@ -164,11 +124,11 @@ int cmd_push(int argc, const char **argv, const char *prefix)
124
continue;
125
}
126
if (!prefixcmp(arg, "--receive-pack=")) {
167
- receivepack = arg;
127
+ receivepack = arg + 15;
128
continue;
129
}
130
if (!prefixcmp(arg, "--exec=")) {
171
- receivepack = arg;
131
+ receivepack = arg + 7;
132
continue;
133
}
134
usage(push_usage);
@@ -177,5 +137,5 @@ int cmd_push(int argc, const char **argv, const char *prefix)
137
if (all && refspec)
138
usage(push_usage);
139
180
- return do_push(repo);
140
+ return do_push(repo, flags);
141
}
transport.c
new
+196
@@ -0,0 +1,196 @@
1
+#include "cache.h"
2
+#include "transport.h"
3
+#include "run-command.h"
4
+
5
+static const struct transport_ops rsync_transport;
6
+
7
+static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) {
8
+ const char **argv;
9
+ int argc;
10
+ int err;
11
+
12
+ argv = xmalloc((refspec_nr + 11) * sizeof(char *));
13
+ argv[0] = "http-push";
14
+ argc = 1;
15
+ if (flags & TRANSPORT_PUSH_ALL)
16
+ argv[argc++] = "--all";
17
+ if (flags & TRANSPORT_PUSH_FORCE)
18
+ argv[argc++] = "--force";
19
+ argv[argc++] = transport->url;
20
+ while (refspec_nr--)
21
+ argv[argc++] = *refspec++;
22
+ argv[argc] = NULL;
23
+ err = run_command_v_opt(argv, RUN_GIT_CMD);
24
+ switch (err) {
25
+ case -ERR_RUN_COMMAND_FORK:
26
+ error("unable to fork for %s", argv[0]);
27
+ case -ERR_RUN_COMMAND_EXEC:
28
+ error("unable to exec %s", argv[0]);
29
+ break;
30
+ case -ERR_RUN_COMMAND_WAITPID:
31
+ case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
32
+ case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
33
+ case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
34
+ error("%s died with strange error", argv[0]);
35
+ }
36
+ return !!err;
37
+}
38
+
39
+static const struct transport_ops curl_transport = {
40
+ /* set_option */ NULL,
41
+ /* push */ curl_transport_push
42
+};
43
+
44
+static const struct transport_ops bundle_transport = {
45
+};
46
+
47
+struct git_transport_data {
48
+ unsigned thin : 1;
49
+
50
+ const char *receivepack;
51
+};
52
+
53
+static int set_git_option(struct transport *connection,
54
+ const char *name, const char *value)
55
+{
56
+ struct git_transport_data *data = connection->data;
57
+ if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
58
+ data->receivepack = value;
59
+ return 0;
60
+ } else if (!strcmp(name, TRANS_OPT_THIN)) {
61
+ data->thin = !!value;
62
+ return 0;
63
+ }
64
+ return 1;
65
+}
66
+
67
+static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) {
68
+ struct git_transport_data *data = transport->data;
69
+ const char **argv;
70
+ char *rem;
71
+ int argc;
72
+ int err;
73
+
74
+ argv = xmalloc((refspec_nr + 11) * sizeof(char *));
75
+ argv[0] = "send-pack";
76
+ argc = 1;
77
+ if (flags & TRANSPORT_PUSH_ALL)
78
+ argv[argc++] = "--all";
79
+ if (flags & TRANSPORT_PUSH_FORCE)
80
+ argv[argc++] = "--force";
81
+ if (data->receivepack) {
82
+ char *rp = xmalloc(strlen(data->receivepack) + 16);
83
+ sprintf(rp, "--receive-pack=%s", data->receivepack);
84
+ argv[argc++] = rp;
85
+ }
86
+ if (data->thin)
87
+ argv[argc++] = "--thin";
88
+ rem = xmalloc(strlen(transport->remote->name) + 10);
89
+ sprintf(rem, "--remote=%s", transport->remote->name);
90
+ argv[argc++] = rem;
91
+ argv[argc++] = transport->url;
92
+ while (refspec_nr--)
93
+ argv[argc++] = *refspec++;
94
+ argv[argc] = NULL;
95
+ err = run_command_v_opt(argv, RUN_GIT_CMD);
96
+ switch (err) {
97
+ case -ERR_RUN_COMMAND_FORK:
98
+ error("unable to fork for %s", argv[0]);
99
+ case -ERR_RUN_COMMAND_EXEC:
100
+ error("unable to exec %s", argv[0]);
101
+ break;
102
+ case -ERR_RUN_COMMAND_WAITPID:
103
+ case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
104
+ case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
105
+ case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
106
+ error("%s died with strange error", argv[0]);
107
+ }
108
+ return !!err;
109
+}
110
+
111
+static const struct transport_ops git_transport = {
112
+ /* set_option */ set_git_option,
113
+ /* push */ git_transport_push
114
+};
115
+
116
+static int is_local(const char *url)
117
+{
118
+ const char *colon = strchr(url, ':');
119
+ const char *slash = strchr(url, '/');
120
+ return !colon || (slash && slash < colon);
121
+}
122
+
123
+static int is_file(const char *url)
124
+{
125
+ struct stat buf;
126
+ if (stat(url, &buf))
127
+ return 0;
128
+ return S_ISREG(buf.st_mode);
129
+}
130
+
131
+struct transport *transport_get(struct remote *remote, const char *url,
132
+ int fetch)
133
+{
134
+ struct transport *ret = NULL;
135
+ if (!prefixcmp(url, "rsync://")) {
136
+ ret = xmalloc(sizeof(*ret));
137
+ ret->data = NULL;
138
+ ret->ops = &rsync_transport;
139
+ } else if (!prefixcmp(url, "http://") || !prefixcmp(url, "https://") ||
140
+ !prefixcmp(url, "ftp://")) {
141
+ ret = xmalloc(sizeof(*ret));
142
+ ret->ops = &curl_transport;
143
+ ret->data = NULL;
144
+ } else if (is_local(url) && is_file(url)) {
145
+ ret = xmalloc(sizeof(*ret));
146
+ ret->data = NULL;
147
+ ret->ops = &bundle_transport;
148
+ } else {
149
+ struct git_transport_data *data = xcalloc(1, sizeof(*data));
150
+ ret = xcalloc(1, sizeof(*ret));
151
+ ret->data = data;
152
+ data->thin = 1;
153
+ data->receivepack = "git-receive-pack";
154
+ if (remote && remote->receivepack)
155
+ data->receivepack = remote->receivepack;
156
+ ret->ops = &git_transport;
157
+ }
158
+ if (ret) {
159
+ ret->remote = remote;
160
+ ret->url = url;
161
+ ret->fetch = !!fetch;
162
+ }
163
+ return ret;
164
+}
165
+
166
+int transport_set_option(struct transport *transport,
167
+ const char *name, const char *value)
168
+{
169
+ int ret = 1;
170
+ if (transport->ops->set_option)
171
+ ret = transport->ops->set_option(transport, name, value);
172
+ if (ret < 0)
173
+ fprintf(stderr, "For '%s' option %s cannot be set to '%s'\n",
174
+ transport->url, name, value);
175
+ if (ret > 0)
176
+ fprintf(stderr, "For '%s' option %s is ignored\n",
177
+ transport->url, name);
178
+ return ret;
179
+}
180
+
181
+int transport_push(struct transport *transport,
182
+ int refspec_nr, const char **refspec, int flags)
183
+{
184
+ if (!transport->ops->push)
185
+ return 1;
186
+ return transport->ops->push(transport, refspec_nr, refspec, flags);
187
+}
188
+
189
+int transport_disconnect(struct transport *transport)
190
+{
191
+ int ret = 0;
192
+ if (transport->ops->disconnect)
193
+ ret = transport->ops->disconnect(transport);
194
+ free(transport);
195
+ return ret;
196
+}
transport.h
new
+61
@@ -0,0 +1,61 @@
1
+#ifndef TRANSPORT_H
2
+#define TRANSPORT_H
3
+
4
+#include "cache.h"
5
+#include "remote.h"
6
+
7
+struct transport {
8
+ unsigned verbose : 1;
9
+ unsigned fetch : 1;
10
+ struct remote *remote;
11
+ const char *url;
12
+
13
+ void *data;
14
+
15
+ struct ref *remote_refs;
16
+
17
+ const struct transport_ops *ops;
18
+};
19
+
20
+#define TRANSPORT_PUSH_ALL 1
21
+#define TRANSPORT_PUSH_FORCE 2
22
+
23
+struct transport_ops {
24
+ /**
25
+ * Returns 0 if successful, positive if the option is not
26
+ * recognized or is inapplicable, and negative if the option
27
+ * is applicable but the value is invalid.
28
+ **/
29
+ int (*set_option)(struct transport *connection, const char *name,
30
+ const char *value);
31
+
32
+ int (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags);
33
+
34
+ int (*disconnect)(struct transport *connection);
35
+};
36
+
37
+/* Returns a transport suitable for the url */
38
+struct transport *transport_get(struct remote *remote, const char *url,
39
+ int fetch);
40
+
41
+/* Transport options which apply to git:// and scp-style URLs */
42
+
43
+/* The program to use on the remote side to receive a pack */
44
+#define TRANS_OPT_RECEIVEPACK "receivepack"
45
+
46
+/* Transfer the data as a thin pack if not null */
47
+#define TRANS_OPT_THIN "thin"
48
+
49
+/**
50
+ * Returns 0 if the option was used, non-zero otherwise. Prints a
51
+ * message to stderr if the option is not used.
52
+ **/
53
+int transport_set_option(struct transport *transport, const char *name,
54
+ const char *value);
55
+
56
+int transport_push(struct transport *connection,
57
+ int refspec_nr, const char **refspec, int flags);
58
+
59
+int transport_disconnect(struct transport *transport);
60
+
61
+#endif