3 #include "run-command.h"
5 #include "fetch-pack.h"
15 * We copy packed-refs and refs/ into a temporary file, then read the
16 * loose refs recursively (sorting whenever possible), and then inserting
17 * those packed refs that are not yet in the list (not validating, but
18 * assuming that the file is sorted).
20 * Appears refactoring this from refs.c is too cumbersome.
23 static int str_cmp(const void *a
, const void *b
)
28 return strcmp(s1
, s2
);
31 /* path->buf + name_offset is expected to point to "refs/" */
33 static int read_loose_refs(struct strbuf
*path
, int name_offset
,
36 DIR *dir
= opendir(path
->buf
);
47 memset (&list
, 0, sizeof(list
));
49 while ((de
= readdir(dir
))) {
50 if (is_dot_or_dotdot(de
->d_name
))
52 ALLOC_GROW(list
.entries
, list
.nr
+ 1, list
.alloc
);
53 list
.entries
[list
.nr
++] = xstrdup(de
->d_name
);
59 qsort(list
.entries
, list
.nr
, sizeof(char *), str_cmp
);
62 strbuf_addch(path
, '/');
64 for (i
= 0; i
< list
.nr
; i
++, strbuf_setlen(path
, pathlen
+ 1)) {
65 strbuf_addstr(path
, list
.entries
[i
]);
66 if (read_loose_refs(path
, name_offset
, tail
)) {
67 int fd
= open(path
->buf
, O_RDONLY
);
73 next
= alloc_ref(path
->buf
+ name_offset
);
74 if (read_in_full(fd
, buffer
, 40) != 40 ||
75 get_sha1_hex(buffer
, next
->old_sha1
)) {
85 strbuf_setlen(path
, pathlen
);
87 for (i
= 0; i
< list
.nr
; i
++)
88 free(list
.entries
[i
]);
94 /* insert the packed refs for which no loose refs were found */
96 static void insert_packed_refs(const char *packed_refs
, struct ref
**list
)
98 FILE *f
= fopen(packed_refs
, "r");
99 static char buffer
[PATH_MAX
];
107 if (!fgets(buffer
, sizeof(buffer
), f
)) {
112 if (hexval(buffer
[0]) > 0xf)
114 len
= strlen(buffer
);
115 if (len
&& buffer
[len
- 1] == '\n')
116 buffer
[--len
] = '\0';
119 while ((*list
)->next
&&
120 (cmp
= strcmp(buffer
+ 41,
121 (*list
)->next
->name
)) > 0)
122 list
= &(*list
)->next
;
123 if (!(*list
)->next
|| cmp
< 0) {
124 struct ref
*next
= alloc_ref(buffer
+ 41);
126 if (get_sha1_hex(buffer
, next
->old_sha1
)) {
127 warning ("invalid SHA-1: %s", buffer
);
131 next
->next
= (*list
)->next
;
132 (*list
)->next
= next
;
133 list
= &(*list
)->next
;
138 static const char *rsync_url(const char *url
)
140 return prefixcmp(url
, "rsync://") ? skip_prefix(url
, "rsync:") : url
;
143 static struct ref
*get_refs_via_rsync(struct transport
*transport
, int for_push
)
145 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
146 struct ref dummy
, *tail
= &dummy
;
147 struct child_process rsync
;
154 /* copy the refs to the temporary directory */
156 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
157 if (!mkdtemp(temp_dir
.buf
))
158 die_errno ("Could not make temporary directory");
159 temp_dir_len
= temp_dir
.len
;
161 strbuf_addstr(&buf
, rsync_url(transport
->url
));
162 strbuf_addstr(&buf
, "/refs");
164 memset(&rsync
, 0, sizeof(rsync
));
166 rsync
.stdout_to_stderr
= 1;
168 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
170 args
[3] = temp_dir
.buf
;
173 if (run_command(&rsync
))
174 die ("Could not run rsync to get refs");
177 strbuf_addstr(&buf
, rsync_url(transport
->url
));
178 strbuf_addstr(&buf
, "/packed-refs");
182 if (run_command(&rsync
))
183 die ("Could not run rsync to get refs");
185 /* read the copied refs */
187 strbuf_addstr(&temp_dir
, "/refs");
188 read_loose_refs(&temp_dir
, temp_dir_len
+ 1, &tail
);
189 strbuf_setlen(&temp_dir
, temp_dir_len
);
192 strbuf_addstr(&temp_dir
, "/packed-refs");
193 insert_packed_refs(temp_dir
.buf
, &tail
);
194 strbuf_setlen(&temp_dir
, temp_dir_len
);
196 if (remove_dir_recursively(&temp_dir
, 0))
197 warning ("Error removing temporary directory %s.",
200 strbuf_release(&buf
);
201 strbuf_release(&temp_dir
);
206 static int fetch_objs_via_rsync(struct transport
*transport
,
207 int nr_objs
, const struct ref
**to_fetch
)
209 struct strbuf buf
= STRBUF_INIT
;
210 struct child_process rsync
;
214 strbuf_addstr(&buf
, rsync_url(transport
->url
));
215 strbuf_addstr(&buf
, "/objects/");
217 memset(&rsync
, 0, sizeof(rsync
));
219 rsync
.stdout_to_stderr
= 1;
221 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
222 args
[2] = "--ignore-existing";
223 args
[3] = "--exclude";
226 args
[6] = get_object_directory();
229 /* NEEDSWORK: handle one level of alternates */
230 result
= run_command(&rsync
);
232 strbuf_release(&buf
);
237 static int write_one_ref(const char *name
, const unsigned char *sha1
,
238 int flags
, void *data
)
240 struct strbuf
*buf
= data
;
244 /* when called via for_each_ref(), flags is non-zero */
245 if (flags
&& prefixcmp(name
, "refs/heads/") &&
246 prefixcmp(name
, "refs/tags/"))
249 strbuf_addstr(buf
, name
);
250 if (safe_create_leading_directories(buf
->buf
) ||
251 !(f
= fopen(buf
->buf
, "w")) ||
252 fprintf(f
, "%s\n", sha1_to_hex(sha1
)) < 0 ||
254 return error("problems writing temporary file %s", buf
->buf
);
255 strbuf_setlen(buf
, len
);
259 static int write_refs_to_temp_dir(struct strbuf
*temp_dir
,
260 int refspec_nr
, const char **refspec
)
264 for (i
= 0; i
< refspec_nr
; i
++) {
265 unsigned char sha1
[20];
268 if (dwim_ref(refspec
[i
], strlen(refspec
[i
]), sha1
, &ref
) != 1)
269 return error("Could not get ref %s", refspec
[i
]);
271 if (write_one_ref(ref
, sha1
, 0, temp_dir
)) {
280 static int rsync_transport_push(struct transport
*transport
,
281 int refspec_nr
, const char **refspec
, int flags
)
283 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
285 struct child_process rsync
;
286 const char *args
[10];
288 if (flags
& TRANSPORT_PUSH_MIRROR
)
289 return error("rsync transport does not support mirror mode");
291 /* first push the objects */
293 strbuf_addstr(&buf
, rsync_url(transport
->url
));
294 strbuf_addch(&buf
, '/');
296 memset(&rsync
, 0, sizeof(rsync
));
298 rsync
.stdout_to_stderr
= 1;
302 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
303 args
[i
++] = "--dry-run";
304 if (transport
->verbose
> 0)
306 args
[i
++] = "--ignore-existing";
307 args
[i
++] = "--exclude";
309 args
[i
++] = get_object_directory();
313 if (run_command(&rsync
))
314 return error("Could not push objects to %s",
315 rsync_url(transport
->url
));
317 /* copy the refs to the temporary directory; they could be packed. */
319 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
320 if (!mkdtemp(temp_dir
.buf
))
321 die_errno ("Could not make temporary directory");
322 strbuf_addch(&temp_dir
, '/');
324 if (flags
& TRANSPORT_PUSH_ALL
) {
325 if (for_each_ref(write_one_ref
, &temp_dir
))
327 } else if (write_refs_to_temp_dir(&temp_dir
, refspec_nr
, refspec
))
331 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
332 args
[i
++] = "--dry-run";
333 if (!(flags
& TRANSPORT_PUSH_FORCE
))
334 args
[i
++] = "--ignore-existing";
335 args
[i
++] = temp_dir
.buf
;
336 args
[i
++] = rsync_url(transport
->url
);
338 if (run_command(&rsync
))
339 result
= error("Could not push to %s",
340 rsync_url(transport
->url
));
342 if (remove_dir_recursively(&temp_dir
, 0))
343 warning ("Could not remove temporary directory %s.",
346 strbuf_release(&buf
);
347 strbuf_release(&temp_dir
);
353 static int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
)
358 if (flags
& TRANSPORT_PUSH_MIRROR
)
359 return error("http transport does not support mirror mode");
361 argv
= xmalloc((refspec_nr
+ 12) * sizeof(char *));
362 argv
[0] = "http-push";
364 if (flags
& TRANSPORT_PUSH_ALL
)
365 argv
[argc
++] = "--all";
366 if (flags
& TRANSPORT_PUSH_FORCE
)
367 argv
[argc
++] = "--force";
368 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
369 argv
[argc
++] = "--dry-run";
370 if (flags
& TRANSPORT_PUSH_VERBOSE
)
371 argv
[argc
++] = "--verbose";
372 argv
[argc
++] = transport
->url
;
374 argv
[argc
++] = *refspec
++;
376 return !!run_command_v_opt(argv
, RUN_GIT_CMD
);
381 struct bundle_transport_data
{
383 struct bundle_header header
;
386 static struct ref
*get_refs_from_bundle(struct transport
*transport
, int for_push
)
388 struct bundle_transport_data
*data
= transport
->data
;
389 struct ref
*result
= NULL
;
397 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
399 die ("Could not read bundle '%s'.", transport
->url
);
400 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
401 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
402 struct ref
*ref
= alloc_ref(e
->name
);
403 hashcpy(ref
->old_sha1
, e
->sha1
);
410 static int fetch_refs_from_bundle(struct transport
*transport
,
411 int nr_heads
, const struct ref
**to_fetch
)
413 struct bundle_transport_data
*data
= transport
->data
;
414 return unbundle(&data
->header
, data
->fd
);
417 static int close_bundle(struct transport
*transport
)
419 struct bundle_transport_data
*data
= transport
->data
;
426 struct git_transport_data
{
429 unsigned followtags
: 1;
431 struct child_process
*conn
;
433 const char *uploadpack
;
434 const char *receivepack
;
435 struct extra_have_objects extra_have
;
438 static int set_git_option(struct transport
*connection
,
439 const char *name
, const char *value
)
441 struct git_transport_data
*data
= connection
->data
;
442 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
443 data
->uploadpack
= value
;
445 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
446 data
->receivepack
= value
;
448 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
449 data
->thin
= !!value
;
451 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
452 data
->followtags
= !!value
;
454 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
455 data
->keep
= !!value
;
457 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
461 data
->depth
= atoi(value
);
467 static int connect_setup(struct transport
*transport
, int for_push
, int verbose
)
469 struct git_transport_data
*data
= transport
->data
;
470 data
->conn
= git_connect(data
->fd
, transport
->url
,
471 for_push
? data
->receivepack
: data
->uploadpack
,
472 verbose
? CONNECT_VERBOSE
: 0);
476 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
478 struct git_transport_data
*data
= transport
->data
;
481 connect_setup(transport
, for_push
, 0);
482 get_remote_heads(data
->fd
[0], &refs
, 0, NULL
,
483 for_push
? REF_NORMAL
: 0, &data
->extra_have
);
488 static int fetch_refs_via_pack(struct transport
*transport
,
489 int nr_heads
, const struct ref
**to_fetch
)
491 struct git_transport_data
*data
= transport
->data
;
492 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
493 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
494 const struct ref
*refs
;
495 char *dest
= xstrdup(transport
->url
);
496 struct fetch_pack_args args
;
498 struct ref
*refs_tmp
= NULL
;
500 memset(&args
, 0, sizeof(args
));
501 args
.uploadpack
= data
->uploadpack
;
502 args
.keep_pack
= data
->keep
;
504 args
.use_thin_pack
= data
->thin
;
505 args
.include_tag
= data
->followtags
;
506 args
.verbose
= (transport
->verbose
> 0);
507 args
.quiet
= (transport
->verbose
< 0);
508 args
.no_progress
= args
.quiet
|| (!transport
->progress
&& !isatty(1));
509 args
.depth
= data
->depth
;
511 for (i
= 0; i
< nr_heads
; i
++)
512 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
515 connect_setup(transport
, 0, 0);
516 get_remote_heads(data
->fd
[0], &refs_tmp
, 0, NULL
, 0, NULL
);
519 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
520 refs_tmp
? refs_tmp
: transport
->remote_refs
,
521 dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
524 if (finish_connect(data
->conn
))
530 for (i
= 0; i
< nr_heads
; i
++)
535 return (refs
? 0 : -1);
538 static int push_had_errors(struct ref
*ref
)
540 for (; ref
; ref
= ref
->next
) {
541 switch (ref
->status
) {
542 case REF_STATUS_NONE
:
543 case REF_STATUS_UPTODATE
:
553 static int refs_pushed(struct ref
*ref
)
555 for (; ref
; ref
= ref
->next
) {
556 switch(ref
->status
) {
557 case REF_STATUS_NONE
:
558 case REF_STATUS_UPTODATE
:
567 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
571 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
577 if (!remote_find_tracking(remote
, &rs
)) {
579 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
581 delete_ref(rs
.dst
, NULL
, 0);
583 update_ref("update by push", rs
.dst
,
584 ref
->new_sha1
, NULL
, 0, 0);
589 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
591 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
, int porcelain
)
595 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
597 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
599 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
601 fprintf(stdout
, "%s\n", summary
);
603 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
605 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
607 fputs(prettify_refname(to
->name
), stderr
);
617 static const char *status_abbrev(unsigned char sha1
[20])
619 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
622 static void print_ok_ref_status(struct ref
*ref
, int porcelain
)
625 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
, porcelain
);
626 else if (is_null_sha1(ref
->old_sha1
))
627 print_ref_status('*',
628 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
630 ref
, ref
->peer_ref
, NULL
, porcelain
);
636 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
637 if (ref
->nonfastforward
) {
638 strcat(quickref
, "...");
640 msg
= "forced update";
642 strcat(quickref
, "..");
646 strcat(quickref
, status_abbrev(ref
->new_sha1
));
648 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
, porcelain
);
652 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
, int porcelain
)
655 fprintf(stderr
, "To %s\n", dest
);
657 switch(ref
->status
) {
658 case REF_STATUS_NONE
:
659 print_ref_status('X', "[no match]", ref
, NULL
, NULL
, porcelain
);
661 case REF_STATUS_REJECT_NODELETE
:
662 print_ref_status('!', "[rejected]", ref
, NULL
,
663 "remote does not support deleting refs", porcelain
);
665 case REF_STATUS_UPTODATE
:
666 print_ref_status('=', "[up to date]", ref
,
667 ref
->peer_ref
, NULL
, porcelain
);
669 case REF_STATUS_REJECT_NONFASTFORWARD
:
670 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
671 "non-fast-forward", porcelain
);
673 case REF_STATUS_REMOTE_REJECT
:
674 print_ref_status('!', "[remote rejected]", ref
,
675 ref
->deletion
? NULL
: ref
->peer_ref
,
676 ref
->remote_status
, porcelain
);
678 case REF_STATUS_EXPECTING_REPORT
:
679 print_ref_status('!', "[remote failure]", ref
,
680 ref
->deletion
? NULL
: ref
->peer_ref
,
681 "remote failed to report status", porcelain
);
684 print_ok_ref_status(ref
, porcelain
);
691 static void print_push_status(const char *dest
, struct ref
*refs
,
692 int verbose
, int porcelain
, int * nonfastforward
)
698 for (ref
= refs
; ref
; ref
= ref
->next
)
699 if (ref
->status
== REF_STATUS_UPTODATE
)
700 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
703 for (ref
= refs
; ref
; ref
= ref
->next
)
704 if (ref
->status
== REF_STATUS_OK
)
705 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
708 for (ref
= refs
; ref
; ref
= ref
->next
) {
709 if (ref
->status
!= REF_STATUS_NONE
&&
710 ref
->status
!= REF_STATUS_UPTODATE
&&
711 ref
->status
!= REF_STATUS_OK
)
712 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
713 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
)
718 static void verify_remote_names(int nr_heads
, const char **heads
)
722 for (i
= 0; i
< nr_heads
; i
++) {
723 const char *local
= heads
[i
];
724 const char *remote
= strrchr(heads
[i
], ':');
729 /* A matching refspec is okay. */
730 if (remote
== local
&& remote
[1] == '\0')
733 remote
= remote
? (remote
+ 1) : local
;
734 switch (check_ref_format(remote
)) {
736 case CHECK_REF_FORMAT_ONELEVEL
:
737 /* ok but a single level -- that is fine for
740 case CHECK_REF_FORMAT_WILDCARD
:
741 /* ok but ends with a pattern-match character */
744 die("remote part of refspec is not a valid name in %s",
749 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
751 struct git_transport_data
*data
= transport
->data
;
752 struct send_pack_args args
;
756 struct ref
*tmp_refs
;
757 connect_setup(transport
, 1, 0);
759 get_remote_heads(data
->fd
[0], &tmp_refs
, 0, NULL
, REF_NORMAL
,
763 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
764 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
765 args
.use_thin_pack
= data
->thin
;
766 args
.verbose
= !!(flags
& TRANSPORT_PUSH_VERBOSE
);
767 args
.quiet
= !!(flags
& TRANSPORT_PUSH_QUIET
);
768 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
770 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
775 ret
|= finish_connect(data
->conn
);
781 static int disconnect_git(struct transport
*transport
)
783 struct git_transport_data
*data
= transport
->data
;
785 packet_flush(data
->fd
[1]);
788 finish_connect(data
->conn
);
795 static int is_local(const char *url
)
797 const char *colon
= strchr(url
, ':');
798 const char *slash
= strchr(url
, '/');
799 return !colon
|| (slash
&& slash
< colon
) ||
800 has_dos_drive_prefix(url
);
803 static int is_file(const char *url
)
808 return S_ISREG(buf
.st_mode
);
811 struct transport
*transport_get(struct remote
*remote
, const char *url
)
813 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
815 ret
->remote
= remote
;
818 if (!prefixcmp(url
, "rsync:")) {
819 ret
->get_refs_list
= get_refs_via_rsync
;
820 ret
->fetch
= fetch_objs_via_rsync
;
821 ret
->push
= rsync_transport_push
;
823 } else if (!prefixcmp(url
, "http://")
824 || !prefixcmp(url
, "https://")
825 || !prefixcmp(url
, "ftp://")) {
826 transport_helper_init(ret
, "curl");
828 error("git was compiled without libcurl support.");
830 ret
->push
= curl_transport_push
;
833 } else if (is_local(url
) && is_file(url
)) {
834 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
836 ret
->get_refs_list
= get_refs_from_bundle
;
837 ret
->fetch
= fetch_refs_from_bundle
;
838 ret
->disconnect
= close_bundle
;
841 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
843 ret
->set_option
= set_git_option
;
844 ret
->get_refs_list
= get_refs_via_connect
;
845 ret
->fetch
= fetch_refs_via_pack
;
846 ret
->push_refs
= git_transport_push
;
847 ret
->disconnect
= disconnect_git
;
851 data
->uploadpack
= "git-upload-pack";
852 if (remote
&& remote
->uploadpack
)
853 data
->uploadpack
= remote
->uploadpack
;
854 data
->receivepack
= "git-receive-pack";
855 if (remote
&& remote
->receivepack
)
856 data
->receivepack
= remote
->receivepack
;
862 int transport_set_option(struct transport
*transport
,
863 const char *name
, const char *value
)
865 if (transport
->set_option
)
866 return transport
->set_option(transport
, name
, value
);
870 int transport_push(struct transport
*transport
,
871 int refspec_nr
, const char **refspec
, int flags
,
875 verify_remote_names(refspec_nr
, refspec
);
878 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
879 if (transport
->push_refs
) {
880 struct ref
*remote_refs
=
881 transport
->get_refs_list(transport
, 1);
882 struct ref
*local_refs
= get_local_heads();
883 int match_flags
= MATCH_REFS_NONE
;
884 int verbose
= flags
& TRANSPORT_PUSH_VERBOSE
;
885 int quiet
= flags
& TRANSPORT_PUSH_QUIET
;
886 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
889 if (flags
& TRANSPORT_PUSH_ALL
)
890 match_flags
|= MATCH_REFS_ALL
;
891 if (flags
& TRANSPORT_PUSH_MIRROR
)
892 match_flags
|= MATCH_REFS_MIRROR
;
894 if (match_refs(local_refs
, &remote_refs
,
895 refspec_nr
, refspec
, match_flags
)) {
899 ret
= transport
->push_refs(transport
, remote_refs
, flags
);
901 if (!quiet
|| push_had_errors(remote_refs
))
902 print_push_status(transport
->url
, remote_refs
,
903 verbose
| porcelain
, porcelain
,
906 if (!(flags
& TRANSPORT_PUSH_DRY_RUN
)) {
908 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
909 update_tracking_ref(transport
->remote
, ref
, verbose
);
912 if (!quiet
&& !ret
&& !refs_pushed(remote_refs
))
913 fprintf(stderr
, "Everything up-to-date\n");
919 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
921 if (!transport
->remote_refs
)
922 transport
->remote_refs
= transport
->get_refs_list(transport
, 0);
923 return transport
->remote_refs
;
926 int transport_fetch_refs(struct transport
*transport
, const struct ref
*refs
)
929 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
930 const struct ref
**heads
= NULL
;
931 const struct ref
*rm
;
933 for (rm
= refs
; rm
; rm
= rm
->next
) {
936 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
938 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
939 heads
[nr_heads
++] = rm
;
944 * When deepening of a shallow repository is requested,
945 * then local and remote refs are likely to still be equal.
946 * Just feed them all to the fetch method in that case.
947 * This condition shouldn't be met in a non-deepening fetch
948 * (see builtin-fetch.c:quickfetch()).
950 heads
= xmalloc(nr_refs
* sizeof(*heads
));
951 for (rm
= refs
; rm
; rm
= rm
->next
)
952 heads
[nr_heads
++] = rm
;
955 rc
= transport
->fetch(transport
, nr_heads
, heads
);
960 void transport_unlock_pack(struct transport
*transport
)
962 if (transport
->pack_lockfile
) {
963 unlink_or_warn(transport
->pack_lockfile
);
964 free(transport
->pack_lockfile
);
965 transport
->pack_lockfile
= NULL
;
969 int transport_disconnect(struct transport
*transport
)
972 if (transport
->disconnect
)
973 ret
= transport
->disconnect(transport
);
979 * Strip username (and password) from an url and return
980 * it in a newly allocated string.
982 char *transport_anonymize_url(const char *url
)
984 char *anon_url
, *scheme_prefix
, *anon_part
;
985 size_t anon_len
, prefix_len
= 0;
987 anon_part
= strchr(url
, '@');
988 if (is_local(url
) || !anon_part
)
991 anon_len
= strlen(++anon_part
);
992 scheme_prefix
= strstr(url
, "://");
993 if (!scheme_prefix
) {
994 if (!strchr(anon_part
, ':'))
995 /* cannot be "me@there:/path/name" */
999 /* make sure scheme is reasonable */
1000 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1003 case '+': case '.': case '-':
1012 /* @ past the first slash does not count */
1013 cp
= strchr(scheme_prefix
+ 3, '/');
1014 if (cp
&& cp
< anon_part
)
1016 prefix_len
= scheme_prefix
- url
+ 3;
1018 anon_url
= xcalloc(1, 1 + prefix_len
+ anon_len
);
1019 memcpy(anon_url
, url
, prefix_len
);
1020 memcpy(anon_url
+ prefix_len
, anon_part
, anon_len
);
1023 return xstrdup(url
);