1 #include "git-compat-util.h"
6 #include "run-command.h"
7 #include "spawn-pipe.h"
9 static char *server_capabilities
;
11 static int check_ref(const char *name
, int len
, unsigned int flags
)
16 if (len
< 5 || memcmp(name
, "refs/", 5))
19 /* Skip the "refs/" part */
23 /* REF_NORMAL means that we don't want the magic fake tag refs */
24 if ((flags
& REF_NORMAL
) && check_ref_format(name
) < 0)
27 /* REF_HEADS means that we want regular branch heads */
28 if ((flags
& REF_HEADS
) && !memcmp(name
, "heads/", 6))
31 /* REF_TAGS means that we want tags */
32 if ((flags
& REF_TAGS
) && !memcmp(name
, "tags/", 5))
35 /* All type bits clear means that we are ok with anything */
36 return !(flags
& ~REF_NORMAL
);
40 * Read all the refs from the other end
42 struct ref
**get_remote_heads(int in
, struct ref
**list
,
43 int nr_match
, char **match
,
49 unsigned char old_sha1
[20];
50 static char buffer
[1000];
54 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
57 if (buffer
[len
-1] == '\n')
60 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
61 die("protocol error: expected sha/ref, got '%s'", buffer
);
64 name_len
= strlen(name
);
65 if (len
!= name_len
+ 41) {
66 if (server_capabilities
)
67 free(server_capabilities
);
68 server_capabilities
= xstrdup(name
+ name_len
+ 1);
71 if (!check_ref(name
, name_len
, flags
))
73 if (nr_match
&& !path_match(name
, nr_match
, match
))
75 ref
= xcalloc(1, sizeof(*ref
) + len
- 40);
76 hashcpy(ref
->old_sha1
, old_sha1
);
77 memcpy(ref
->name
, buffer
+ 41, len
- 40);
84 int server_supports(const char *feature
)
86 return server_capabilities
&&
87 strstr(server_capabilities
, feature
) != NULL
;
90 int get_ack(int fd
, unsigned char *result_sha1
)
92 static char line
[1000];
93 int len
= packet_read_line(fd
, line
, sizeof(line
));
96 die("git-fetch-pack: expected ACK/NAK, got EOF");
97 if (line
[len
-1] == '\n')
99 if (!strcmp(line
, "NAK"))
101 if (!prefixcmp(line
, "ACK ")) {
102 if (!get_sha1_hex(line
+4, result_sha1
)) {
103 if (strstr(line
+45, "continue"))
108 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
111 int path_match(const char *path
, int nr
, char **match
)
114 int pathlen
= strlen(path
);
116 for (i
= 0; i
< nr
; i
++) {
120 if (!len
|| len
> pathlen
)
122 if (memcmp(path
+ pathlen
- len
, s
, len
))
124 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
139 * A:B means fast forward remote B with local A.
140 * +A:B means overwrite remote B with local A.
141 * +A is a shorthand for +A:A.
142 * A is a shorthand for A:A.
143 * :B means delete remote B.
145 static struct refspec
*parse_ref_spec(int nr_refspec
, char **refspec
)
148 struct refspec
*rs
= xcalloc(sizeof(*rs
), (nr_refspec
+ 1));
149 for (i
= 0; i
< nr_refspec
; i
++) {
156 ep
= strchr(sp
, ':');
166 rs
[nr_refspec
].src
= rs
[nr_refspec
].dst
= NULL
;
170 static int count_refspec_match(const char *pattern
,
172 struct ref
**matched_ref
)
174 int patlen
= strlen(pattern
);
175 struct ref
*matched_weak
= NULL
;
176 struct ref
*matched
= NULL
;
180 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
181 char *name
= refs
->name
;
182 int namelen
= strlen(name
);
185 if (namelen
< patlen
||
186 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
188 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
191 /* A match is "weak" if it is with refs outside
192 * heads or tags, and did not specify the pattern
193 * in full (e.g. "refs/remotes/origin/master") or at
194 * least from the toplevel (e.g. "remotes/origin/master");
195 * otherwise "git push $URL master" would result in
196 * ambiguity between remotes/origin/master and heads/master
197 * at the remote site.
199 if (namelen
!= patlen
&&
200 patlen
!= namelen
- 5 &&
201 prefixcmp(name
, "refs/heads/") &&
202 prefixcmp(name
, "refs/tags/")) {
203 /* We want to catch the case where only weak
204 * matches are found and there are multiple
205 * matches, and where more than one strong
206 * matches are found, as ambiguous. One
207 * strong match with zero or more weak matches
208 * are acceptable as a unique match.
219 *matched_ref
= matched_weak
;
223 *matched_ref
= matched
;
228 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
235 static struct ref
*try_explicit_object_name(const char *name
)
237 unsigned char sha1
[20];
242 ref
= xcalloc(1, sizeof(*ref
) + 20);
243 strcpy(ref
->name
, "(delete)");
244 hashclr(ref
->new_sha1
);
247 if (get_sha1(name
, sha1
))
249 len
= strlen(name
) + 1;
250 ref
= xcalloc(1, sizeof(*ref
) + len
);
251 memcpy(ref
->name
, name
, len
);
252 hashcpy(ref
->new_sha1
, sha1
);
256 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
257 struct ref
***dst_tail
, struct refspec
*rs
)
260 for (i
= errs
= 0; rs
[i
].src
; i
++) {
261 struct ref
*matched_src
, *matched_dst
;
263 matched_src
= matched_dst
= NULL
;
264 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
268 /* The source could be in the get_sha1() format
269 * not a reference name. :refs/other is a
270 * way to delete 'other' ref at the remote end.
272 matched_src
= try_explicit_object_name(rs
[i
].src
);
276 error("src refspec %s does not match any.",
281 error("src refspec %s matches more than one.",
285 switch (count_refspec_match(rs
[i
].dst
, dst
, &matched_dst
)) {
289 if (!memcmp(rs
[i
].dst
, "refs/", 5)) {
290 int len
= strlen(rs
[i
].dst
) + 1;
291 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
292 memcpy(matched_dst
->name
, rs
[i
].dst
, len
);
293 link_dst_tail(matched_dst
, dst_tail
);
295 else if (!strcmp(rs
[i
].src
, rs
[i
].dst
) &&
297 /* pushing "master:master" when
298 * remote does not have master yet.
300 int len
= strlen(matched_src
->name
) + 1;
301 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
302 memcpy(matched_dst
->name
, matched_src
->name
,
304 link_dst_tail(matched_dst
, dst_tail
);
308 error("dst refspec %s does not match any "
309 "existing ref on the remote and does "
310 "not start with refs/.", rs
[i
].dst
);
315 error("dst refspec %s matches more than one.",
321 if (matched_dst
->peer_ref
) {
323 error("dst ref %s receives from more than one src.",
327 matched_dst
->peer_ref
= matched_src
;
328 matched_dst
->force
= rs
[i
].force
;
334 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
336 for ( ; list
; list
= list
->next
)
337 if (!strcmp(list
->name
, name
))
342 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
343 int nr_refspec
, char **refspec
, int all
)
345 struct refspec
*rs
= parse_ref_spec(nr_refspec
, refspec
);
348 return match_explicit_refs(src
, dst
, dst_tail
, rs
);
350 /* pick the remainder */
351 for ( ; src
; src
= src
->next
) {
352 struct ref
*dst_peer
;
355 dst_peer
= find_ref_by_name(dst
, src
->name
);
356 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
359 /* Create a new one and link it */
360 int len
= strlen(src
->name
) + 1;
361 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
362 memcpy(dst_peer
->name
, src
->name
, len
);
363 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
364 link_dst_tail(dst_peer
, dst_tail
);
366 dst_peer
->peer_ref
= src
;
377 static enum protocol
get_protocol(const char *name
)
379 if (!strcmp(name
, "ssh"))
381 if (!strcmp(name
, "git"))
383 if (!strcmp(name
, "git+ssh"))
385 if (!strcmp(name
, "ssh+git"))
387 die("I don't handle protocol '%s'", name
);
391 #define STR(s) STR_(s)
396 * Returns a connected socket() fd, or else die()s.
398 static int git_tcp_connect_sock(char *host
)
400 int sockfd
= -1, saved_errno
= 0;
402 const char *port
= STR(DEFAULT_GIT_PORT
);
403 struct addrinfo hints
, *ai0
, *ai
;
406 if (host
[0] == '[') {
407 end
= strchr(host
+ 1, ']');
416 colon
= strchr(end
, ':');
425 memset(&hints
, 0, sizeof(hints
));
426 hints
.ai_socktype
= SOCK_STREAM
;
427 hints
.ai_protocol
= IPPROTO_TCP
;
429 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
431 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
433 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
434 sockfd
= socket(ai
->ai_family
,
435 ai
->ai_socktype
, ai
->ai_protocol
);
440 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
452 die("unable to connect a socket (%s)", strerror(saved_errno
));
460 * Returns a connected socket() fd, or else die()s.
462 static int git_tcp_connect_sock(char *host
)
464 int sockfd
= -1, saved_errno
= 0;
466 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
468 struct sockaddr_in sa
;
472 if (host
[0] == '[') {
473 end
= strchr(host
+ 1, ']');
482 colon
= strchr(end
, ':');
489 he
= gethostbyname(host
);
491 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
492 nport
= strtoul(port
, &ep
, 10);
493 if ( ep
== port
|| *ep
) {
495 struct servent
*se
= getservbyname(port
,"tcp");
497 die("Unknown port %s\n", port
);
501 for (ap
= he
->h_addr_list
; *ap
; ap
++) {
502 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
508 memset(&sa
, 0, sizeof sa
);
509 sa
.sin_family
= he
->h_addrtype
;
510 sa
.sin_port
= htons(nport
);
511 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
513 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
523 die("unable to connect a socket (%s)", strerror(saved_errno
));
531 static void git_tcp_connect(int fd
[2], char *host
)
534 int sockfd
= git_tcp_connect_sock(host
);
539 if (WSAStartup(MAKEWORD(2,2), &wsa
))
540 die("unable to initialize winsock subsystem, error %d",
542 atexit((void(*)(void)) WSACleanup
);
544 sockfd
= git_tcp_connect_sock(host
);
545 /* convert into a file descriptor */
546 if ((sockfd
= _open_osfhandle(sockfd
, O_RDWR
|O_BINARY
)) < 0)
547 die("unable to make a socket file descriptor: %s",
556 static char *git_proxy_command
;
557 static const char *rhost_name
;
558 static int rhost_len
;
560 static int git_proxy_command_options(const char *var
, const char *value
)
562 if (!strcmp(var
, "core.gitproxy")) {
567 if (git_proxy_command
)
570 * ;# matches www.kernel.org as well
571 * gitproxy = netcatter-1 for kernel.org
572 * gitproxy = netcatter-2 for sample.xz
573 * gitproxy = netcatter-default
575 for_pos
= strstr(value
, " for ");
577 /* matches everybody */
578 matchlen
= strlen(value
);
580 hostlen
= strlen(for_pos
+ 5);
581 if (rhost_len
< hostlen
)
583 else if (!strncmp(for_pos
+ 5,
584 rhost_name
+ rhost_len
- hostlen
,
586 ((rhost_len
== hostlen
) ||
587 rhost_name
[rhost_len
- hostlen
-1] == '.'))
588 matchlen
= for_pos
- value
;
593 /* core.gitproxy = none for kernel.org */
595 !memcmp(value
, "none", 4))
597 git_proxy_command
= xmalloc(matchlen
+ 1);
598 memcpy(git_proxy_command
, value
, matchlen
);
599 git_proxy_command
[matchlen
] = 0;
604 return git_default_config(var
, value
);
607 static int git_use_proxy(const char *host
)
610 rhost_len
= strlen(host
);
611 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
612 git_config(git_proxy_command_options
);
614 return (git_proxy_command
&& *git_proxy_command
);
617 static void git_proxy_connect(int fd
[2], char *host
)
619 const char *port
= STR(DEFAULT_GIT_PORT
);
622 struct child_process proxy
;
624 if (host
[0] == '[') {
625 end
= strchr(host
+ 1, ']');
634 colon
= strchr(end
, ':');
641 argv
[0] = git_proxy_command
;
645 memset(&proxy
, 0, sizeof(proxy
));
649 if (start_command(&proxy
))
650 die("cannot start proxy %s", argv
[0]);
651 fd
[0] = proxy
.out
; /* read from proxy stdout */
652 fd
[1] = proxy
.in
; /* write to proxy stdin */
655 #define MAX_CMD_LEN 1024
658 * This returns 0 if the transport protocol does not need fork(2),
659 * or a process id if it does. Once done, finish the connection
660 * with finish_connect() with the value returned from this function
661 * (it is safe to call finish_connect() with 0 to support the former
664 * Does not return a negative value on error; it just dies.
666 pid_t
git_connect(int fd
[2], char *url
, const char *prog
)
668 char *host
, *path
= url
;
673 enum protocol protocol
= PROTO_LOCAL
;
676 /* Without this we cannot rely on waitpid() to tell
677 * what happened to our children.
679 signal(SIGCHLD
, SIG_DFL
);
681 host
= strstr(url
, "://");
684 protocol
= get_protocol(url
);
692 if (host
[0] == '[') {
693 end
= strchr(host
+ 1, ']');
703 path
= strchr(end
, c
);
705 /* host must have at least 2 chars to catch DOS C:/path */
706 if (path
&& path
- end
> 1) {
707 protocol
= PROTO_SSH
;
714 die("No path specified. See 'man git-pull' for valid url syntax");
717 * null-terminate hostname and point path to ~ for URL's like this:
718 * ssh://host.xz/~user/repo
720 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
732 if (protocol
== PROTO_GIT
) {
733 /* These underlying connection commands die() if they
736 char *target_host
= xstrdup(host
);
737 if (git_use_proxy(host
))
738 git_proxy_connect(fd
, host
);
740 git_tcp_connect(fd
, host
);
742 * Separate original protocol components prog and path
743 * from extended components with a NUL byte.
755 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
756 die("unable to create pipe pair for communication");
758 char command
[MAX_CMD_LEN
];
759 char *posn
= command
;
760 int size
= MAX_CMD_LEN
;
763 of
|= add_to_string(&posn
, &size
, prog
, 0);
764 of
|= add_to_string(&posn
, &size
, " ", 0);
765 of
|= add_to_string(&posn
, &size
, path
, 1);
768 die("command line too long");
770 if (protocol
== PROTO_SSH
) {
771 const char *argv
[] = { NULL
, host
, command
, NULL
};
772 const char *ssh
= getenv("GIT_SSH");
773 if (!ssh
) ssh
= "ssh";
774 pid
= spawnvpe_pipe(ssh
, argv
, environ
, pipefd
[1], pipefd
[0]);
777 const char *argv
[] = { NULL
, "-c", command
, NULL
};
778 const char **env
= copy_environ();
779 env_unsetenv(env
, ALTERNATE_DB_ENVIRONMENT
);
780 env_unsetenv(env
, DB_ENVIRONMENT
);
781 env_unsetenv(env
, GIT_DIR_ENVIRONMENT
);
782 env_unsetenv(env
, GRAFT_ENVIRONMENT
);
783 env_unsetenv(env
, INDEX_ENVIRONMENT
);
784 pid
= spawnvpe_pipe("sh", argv
, env
, pipefd
[1], pipefd
[0]);
787 die("unable to fork");
788 fd
[0] = pipefd
[0][0];
789 fd
[1] = pipefd
[1][1];
795 int finish_connect(pid_t pid
)
800 while (waitpid(pid
, NULL
, 0) < 0) {