9 #include "string-list.h"
11 static struct refspec s_tag_refspec
= {
19 const struct refspec
*tag_refspec
= &s_tag_refspec
;
21 struct counted_string
{
28 struct counted_string
*instead_of
;
33 struct rewrite
**rewrite
;
38 static struct remote
**remotes
;
39 static int remotes_alloc
;
40 static int remotes_nr
;
42 static struct branch
**branches
;
43 static int branches_alloc
;
44 static int branches_nr
;
46 static struct branch
*current_branch
;
47 static const char *default_remote_name
;
48 static int explicit_default_remote_name
;
50 static struct rewrites rewrites
;
51 static struct rewrites rewrites_push
;
53 #define BUF_SIZE (2048)
54 static char buffer
[BUF_SIZE
];
56 static int valid_remote(const struct remote
*remote
)
58 return (!!remote
->url
) || (!!remote
->foreign_vcs
);
61 static const char *alias_url(const char *url
, struct rewrites
*r
)
65 struct counted_string
*longest
;
70 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
73 for (j
= 0; j
< r
->rewrite
[i
]->instead_of_nr
; j
++) {
74 if (!prefixcmp(url
, r
->rewrite
[i
]->instead_of
[j
].s
) &&
76 longest
->len
< r
->rewrite
[i
]->instead_of
[j
].len
)) {
77 longest
= &(r
->rewrite
[i
]->instead_of
[j
]);
85 ret
= xmalloc(r
->rewrite
[longest_i
]->baselen
+
86 (strlen(url
) - longest
->len
) + 1);
87 strcpy(ret
, r
->rewrite
[longest_i
]->base
);
88 strcpy(ret
+ r
->rewrite
[longest_i
]->baselen
, url
+ longest
->len
);
92 static void add_push_refspec(struct remote
*remote
, const char *ref
)
94 ALLOC_GROW(remote
->push_refspec
,
95 remote
->push_refspec_nr
+ 1,
96 remote
->push_refspec_alloc
);
97 remote
->push_refspec
[remote
->push_refspec_nr
++] = ref
;
100 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
102 ALLOC_GROW(remote
->fetch_refspec
,
103 remote
->fetch_refspec_nr
+ 1,
104 remote
->fetch_refspec_alloc
);
105 remote
->fetch_refspec
[remote
->fetch_refspec_nr
++] = ref
;
108 static void add_url(struct remote
*remote
, const char *url
)
110 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
111 remote
->url
[remote
->url_nr
++] = url
;
114 static void add_pushurl(struct remote
*remote
, const char *pushurl
)
116 ALLOC_GROW(remote
->pushurl
, remote
->pushurl_nr
+ 1, remote
->pushurl_alloc
);
117 remote
->pushurl
[remote
->pushurl_nr
++] = pushurl
;
120 static void add_pushurl_alias(struct remote
*remote
, const char *url
)
122 const char *pushurl
= alias_url(url
, &rewrites_push
);
124 add_pushurl(remote
, pushurl
);
127 static void add_url_alias(struct remote
*remote
, const char *url
)
129 add_url(remote
, alias_url(url
, &rewrites
));
130 add_pushurl_alias(remote
, url
);
133 static struct remote
*make_remote(const char *name
, int len
)
138 for (i
= 0; i
< remotes_nr
; i
++) {
139 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
140 !remotes
[i
]->name
[len
]) :
141 !strcmp(name
, remotes
[i
]->name
))
145 ret
= xcalloc(1, sizeof(struct remote
));
146 ALLOC_GROW(remotes
, remotes_nr
+ 1, remotes_alloc
);
147 remotes
[remotes_nr
++] = ret
;
149 ret
->name
= xstrndup(name
, len
);
151 ret
->name
= xstrdup(name
);
155 static void add_merge(struct branch
*branch
, const char *name
)
157 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
158 branch
->merge_alloc
);
159 branch
->merge_name
[branch
->merge_nr
++] = name
;
162 static struct branch
*make_branch(const char *name
, int len
)
168 for (i
= 0; i
< branches_nr
; i
++) {
169 if (len
? (!strncmp(name
, branches
[i
]->name
, len
) &&
170 !branches
[i
]->name
[len
]) :
171 !strcmp(name
, branches
[i
]->name
))
175 ALLOC_GROW(branches
, branches_nr
+ 1, branches_alloc
);
176 ret
= xcalloc(1, sizeof(struct branch
));
177 branches
[branches_nr
++] = ret
;
179 ret
->name
= xstrndup(name
, len
);
181 ret
->name
= xstrdup(name
);
182 refname
= xmalloc(strlen(name
) + strlen("refs/heads/") + 1);
183 strcpy(refname
, "refs/heads/");
184 strcpy(refname
+ strlen("refs/heads/"), ret
->name
);
185 ret
->refname
= refname
;
190 static struct rewrite
*make_rewrite(struct rewrites
*r
, const char *base
, int len
)
195 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
197 ? (len
== r
->rewrite
[i
]->baselen
&&
198 !strncmp(base
, r
->rewrite
[i
]->base
, len
))
199 : !strcmp(base
, r
->rewrite
[i
]->base
))
200 return r
->rewrite
[i
];
203 ALLOC_GROW(r
->rewrite
, r
->rewrite_nr
+ 1, r
->rewrite_alloc
);
204 ret
= xcalloc(1, sizeof(struct rewrite
));
205 r
->rewrite
[r
->rewrite_nr
++] = ret
;
207 ret
->base
= xstrndup(base
, len
);
211 ret
->base
= xstrdup(base
);
212 ret
->baselen
= strlen(base
);
217 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
219 ALLOC_GROW(rewrite
->instead_of
, rewrite
->instead_of_nr
+ 1, rewrite
->instead_of_alloc
);
220 rewrite
->instead_of
[rewrite
->instead_of_nr
].s
= instead_of
;
221 rewrite
->instead_of
[rewrite
->instead_of_nr
].len
= strlen(instead_of
);
222 rewrite
->instead_of_nr
++;
225 static void read_remotes_file(struct remote
*remote
)
227 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
231 remote
->origin
= REMOTE_REMOTES
;
232 while (fgets(buffer
, BUF_SIZE
, f
)) {
236 if (!prefixcmp(buffer
, "URL:")) {
239 } else if (!prefixcmp(buffer
, "Push:")) {
242 } else if (!prefixcmp(buffer
, "Pull:")) {
254 while (isspace(p
[-1]))
257 switch (value_list
) {
259 add_url_alias(remote
, xstrdup(s
));
262 add_push_refspec(remote
, xstrdup(s
));
265 add_fetch_refspec(remote
, xstrdup(s
));
272 static void read_branches_file(struct remote
*remote
)
274 const char *slash
= strchr(remote
->name
, '/');
276 struct strbuf branch
= STRBUF_INIT
;
277 int n
= slash
? slash
- remote
->name
: 1000;
278 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
284 s
= fgets(buffer
, BUF_SIZE
, f
);
292 remote
->origin
= REMOTE_BRANCHES
;
294 while (isspace(p
[-1]))
298 len
+= strlen(slash
);
299 p
= xmalloc(len
+ 1);
305 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
306 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
307 * the partial URL obtained from the branches file plus
308 * "/netdev-2.6" and does not store it in any tracking ref.
309 * #branch specifier in the file is ignored.
311 * Otherwise, the branches file would have URL and optionally
312 * #branch specified. The "master" (or specified) branch is
313 * fetched and stored in the local branch of the same name.
315 frag
= strchr(p
, '#');
318 strbuf_addf(&branch
, "refs/heads/%s", frag
);
320 strbuf_addstr(&branch
, "refs/heads/master");
322 strbuf_addf(&branch
, ":refs/heads/%s", remote
->name
);
324 strbuf_reset(&branch
);
325 strbuf_addstr(&branch
, "HEAD:");
327 add_url_alias(remote
, p
);
328 add_fetch_refspec(remote
, strbuf_detach(&branch
, NULL
));
330 * Cogito compatible push: push current HEAD to remote #branch
331 * (master if missing)
333 strbuf_init(&branch
, 0);
334 strbuf_addstr(&branch
, "HEAD");
336 strbuf_addf(&branch
, ":refs/heads/%s", frag
);
338 strbuf_addstr(&branch
, ":refs/heads/master");
339 add_push_refspec(remote
, strbuf_detach(&branch
, NULL
));
340 remote
->fetch_tags
= 1; /* always auto-follow */
343 static int handle_config(const char *key
, const char *value
, void *cb
)
347 struct remote
*remote
;
348 struct branch
*branch
;
349 if (!prefixcmp(key
, "branch.")) {
351 subkey
= strrchr(name
, '.');
354 branch
= make_branch(name
, subkey
- name
);
355 if (!strcmp(subkey
, ".remote")) {
357 return config_error_nonbool(key
);
358 branch
->remote_name
= xstrdup(value
);
359 if (branch
== current_branch
) {
360 default_remote_name
= branch
->remote_name
;
361 explicit_default_remote_name
= 1;
363 } else if (!strcmp(subkey
, ".merge")) {
365 return config_error_nonbool(key
);
366 add_merge(branch
, xstrdup(value
));
370 if (!prefixcmp(key
, "url.")) {
371 struct rewrite
*rewrite
;
373 subkey
= strrchr(name
, '.');
376 if (!strcmp(subkey
, ".insteadof")) {
377 rewrite
= make_rewrite(&rewrites
, name
, subkey
- name
);
379 return config_error_nonbool(key
);
380 add_instead_of(rewrite
, xstrdup(value
));
381 } else if (!strcmp(subkey
, ".pushinsteadof")) {
382 rewrite
= make_rewrite(&rewrites_push
, name
, subkey
- name
);
384 return config_error_nonbool(key
);
385 add_instead_of(rewrite
, xstrdup(value
));
388 if (prefixcmp(key
, "remote."))
392 warning("Config remote shorthand cannot begin with '/': %s",
396 subkey
= strrchr(name
, '.');
399 remote
= make_remote(name
, subkey
- name
);
400 remote
->origin
= REMOTE_CONFIG
;
401 if (!strcmp(subkey
, ".mirror"))
402 remote
->mirror
= git_config_bool(key
, value
);
403 else if (!strcmp(subkey
, ".skipdefaultupdate"))
404 remote
->skip_default_update
= git_config_bool(key
, value
);
405 else if (!strcmp(subkey
, ".skipfetchall"))
406 remote
->skip_default_update
= git_config_bool(key
, value
);
407 else if (!strcmp(subkey
, ".url")) {
409 if (git_config_string(&v
, key
, value
))
412 } else if (!strcmp(subkey
, ".pushurl")) {
414 if (git_config_string(&v
, key
, value
))
416 add_pushurl(remote
, v
);
417 } else if (!strcmp(subkey
, ".push")) {
419 if (git_config_string(&v
, key
, value
))
421 add_push_refspec(remote
, v
);
422 } else if (!strcmp(subkey
, ".fetch")) {
424 if (git_config_string(&v
, key
, value
))
426 add_fetch_refspec(remote
, v
);
427 } else if (!strcmp(subkey
, ".receivepack")) {
429 if (git_config_string(&v
, key
, value
))
431 if (!remote
->receivepack
)
432 remote
->receivepack
= v
;
434 error("more than one receivepack given, using the first");
435 } else if (!strcmp(subkey
, ".uploadpack")) {
437 if (git_config_string(&v
, key
, value
))
439 if (!remote
->uploadpack
)
440 remote
->uploadpack
= v
;
442 error("more than one uploadpack given, using the first");
443 } else if (!strcmp(subkey
, ".tagopt")) {
444 if (!strcmp(value
, "--no-tags"))
445 remote
->fetch_tags
= -1;
446 else if (!strcmp(value
, "--tags"))
447 remote
->fetch_tags
= 2;
448 } else if (!strcmp(subkey
, ".proxy")) {
449 return git_config_string((const char **)&remote
->http_proxy
,
451 } else if (!strcmp(subkey
, ".vcs")) {
452 return git_config_string(&remote
->foreign_vcs
, key
, value
);
457 static void alias_all_urls(void)
460 for (i
= 0; i
< remotes_nr
; i
++) {
461 int add_pushurl_aliases
;
464 for (j
= 0; j
< remotes
[i
]->pushurl_nr
; j
++) {
465 remotes
[i
]->pushurl
[j
] = alias_url(remotes
[i
]->pushurl
[j
], &rewrites
);
467 add_pushurl_aliases
= remotes
[i
]->pushurl_nr
== 0;
468 for (j
= 0; j
< remotes
[i
]->url_nr
; j
++) {
469 if (add_pushurl_aliases
)
470 add_pushurl_alias(remotes
[i
], remotes
[i
]->url
[j
]);
471 remotes
[i
]->url
[j
] = alias_url(remotes
[i
]->url
[j
], &rewrites
);
476 static void read_config(void)
478 unsigned char sha1
[20];
479 const char *head_ref
;
481 if (default_remote_name
) /* did this already */
483 default_remote_name
= xstrdup("origin");
484 current_branch
= NULL
;
485 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
486 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
487 !prefixcmp(head_ref
, "refs/heads/")) {
489 make_branch(head_ref
+ strlen("refs/heads/"), 0);
491 git_config(handle_config
, NULL
);
496 * We need to make sure the remote-tracking branches are well formed, but a
497 * wildcard refspec in "struct refspec" must have a trailing slash. We
498 * temporarily drop the trailing '/' while calling check_ref_format(),
499 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
500 * error return is Ok for a wildcard refspec.
502 static int verify_refname(char *name
, int is_glob
)
506 result
= check_ref_format(name
);
507 if (is_glob
&& result
== CHECK_REF_FORMAT_WILDCARD
)
508 result
= CHECK_REF_FORMAT_OK
;
513 * This function frees a refspec array.
514 * Warning: code paths should be checked to ensure that the src
515 * and dst pointers are always freeable pointers as well
516 * as the refspec pointer itself.
518 static void free_refspecs(struct refspec
*refspec
, int nr_refspec
)
525 for (i
= 0; i
< nr_refspec
; i
++) {
526 free(refspec
[i
].src
);
527 free(refspec
[i
].dst
);
532 static struct refspec
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
536 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
538 for (i
= 0; i
< nr_refspec
; i
++) {
541 const char *lhs
, *rhs
;
551 rhs
= strrchr(lhs
, ':');
554 * Before going on, special case ":" (or "+:") as a refspec
557 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
563 size_t rlen
= strlen(++rhs
);
564 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
565 rs
[i
].dst
= xstrndup(rhs
, rlen
);
568 llen
= (rhs
? (rhs
- lhs
- 1) : strlen(lhs
));
569 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
570 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
573 } else if (rhs
&& is_glob
) {
577 rs
[i
].pattern
= is_glob
;
578 rs
[i
].src
= xstrndup(lhs
, llen
);
583 * - empty is allowed; it means HEAD.
584 * - otherwise it must be a valid looking ref.
589 st
= verify_refname(rs
[i
].src
, is_glob
);
590 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
595 * - missing is ok, and is same as empty.
596 * - empty is ok; it means not to store.
597 * - otherwise it must be a valid looking ref.
601 } else if (!*rs
[i
].dst
) {
604 st
= verify_refname(rs
[i
].dst
, is_glob
);
605 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
611 * - empty is allowed; it means delete.
612 * - when wildcarded, it must be a valid looking ref.
613 * - otherwise, it must be an extended SHA-1, but
614 * there is no existing way to validate this.
619 st
= verify_refname(rs
[i
].src
, is_glob
);
620 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
624 ; /* anything goes, for now */
627 * - missing is allowed, but LHS then must be a
629 * - empty is not allowed.
630 * - otherwise it must be a valid looking ref.
633 st
= verify_refname(rs
[i
].src
, is_glob
);
634 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
636 } else if (!*rs
[i
].dst
) {
639 st
= verify_refname(rs
[i
].dst
, is_glob
);
640 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
)
650 * nr_refspec must be greater than zero and i must be valid
651 * since it is only possible to reach this point from within
652 * the for loop above.
654 free_refspecs(rs
, i
+1);
657 die("Invalid refspec '%s'", refspec
[i
]);
660 int valid_fetch_refspec(const char *fetch_refspec_str
)
662 struct refspec
*refspec
;
664 refspec
= parse_refspec_internal(1, &fetch_refspec_str
, 1, 1);
665 free_refspecs(refspec
, 1);
669 struct refspec
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
671 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
674 static struct refspec
*parse_push_refspec(int nr_refspec
, const char **refspec
)
676 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
679 void free_refspec(int nr_refspec
, struct refspec
*refspec
)
682 for (i
= 0; i
< nr_refspec
; i
++) {
683 free(refspec
[i
].src
);
684 free(refspec
[i
].dst
);
689 static int valid_remote_nick(const char *name
)
691 if (!name
[0] || is_dot_or_dotdot(name
))
693 return !strchr(name
, '/'); /* no slash */
696 struct remote
*remote_get(const char *name
)
705 name
= default_remote_name
;
706 name_given
= explicit_default_remote_name
;
709 ret
= make_remote(name
, 0);
710 if (valid_remote_nick(name
)) {
711 if (!valid_remote(ret
))
712 read_remotes_file(ret
);
713 if (!valid_remote(ret
))
714 read_branches_file(ret
);
716 if (name_given
&& !valid_remote(ret
))
717 add_url_alias(ret
, name
);
718 if (!valid_remote(ret
))
720 ret
->fetch
= parse_fetch_refspec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
721 ret
->push
= parse_push_refspec(ret
->push_refspec_nr
, ret
->push_refspec
);
725 int remote_is_configured(const char *name
)
730 for (i
= 0; i
< remotes_nr
; i
++)
731 if (!strcmp(name
, remotes
[i
]->name
))
736 int for_each_remote(each_remote_fn fn
, void *priv
)
740 for (i
= 0; i
< remotes_nr
&& !result
; i
++) {
741 struct remote
*r
= remotes
[i
];
745 r
->fetch
= parse_fetch_refspec(r
->fetch_refspec_nr
,
748 r
->push
= parse_push_refspec(r
->push_refspec_nr
,
750 result
= fn(r
, priv
);
755 void ref_remove_duplicates(struct ref
*ref_map
)
757 struct string_list refs
= STRING_LIST_INIT_NODUP
;
758 struct string_list_item
*item
= NULL
;
759 struct ref
*prev
= NULL
, *next
= NULL
;
760 for (; ref_map
; prev
= ref_map
, ref_map
= next
) {
761 next
= ref_map
->next
;
762 if (!ref_map
->peer_ref
)
765 item
= string_list_lookup(&refs
, ref_map
->peer_ref
->name
);
767 if (strcmp(((struct ref
*)item
->util
)->name
,
769 die("%s tracks both %s and %s",
770 ref_map
->peer_ref
->name
,
771 ((struct ref
*)item
->util
)->name
,
773 prev
->next
= ref_map
->next
;
774 free(ref_map
->peer_ref
);
776 ref_map
= prev
; /* skip this; we freed it */
780 item
= string_list_insert(&refs
, ref_map
->peer_ref
->name
);
781 item
->util
= ref_map
;
783 string_list_clear(&refs
, 0);
786 int remote_has_url(struct remote
*remote
, const char *url
)
789 for (i
= 0; i
< remote
->url_nr
; i
++) {
790 if (!strcmp(remote
->url
[i
], url
))
796 static int match_name_with_pattern(const char *key
, const char *name
,
797 const char *value
, char **result
)
799 const char *kstar
= strchr(key
, '*');
805 die("Key '%s' of pattern had no '*'", key
);
807 ksuffixlen
= strlen(kstar
+ 1);
808 namelen
= strlen(name
);
809 ret
= !strncmp(name
, key
, klen
) && namelen
>= klen
+ ksuffixlen
&&
810 !memcmp(name
+ namelen
- ksuffixlen
, kstar
+ 1, ksuffixlen
);
812 const char *vstar
= strchr(value
, '*');
816 die("Value '%s' of pattern has no '*'", value
);
817 vlen
= vstar
- value
;
818 vsuffixlen
= strlen(vstar
+ 1);
819 *result
= xmalloc(vlen
+ vsuffixlen
+
821 klen
- ksuffixlen
+ 1);
822 strncpy(*result
, value
, vlen
);
823 strncpy(*result
+ vlen
,
824 name
+ klen
, namelen
- klen
- ksuffixlen
);
825 strcpy(*result
+ vlen
+ namelen
- klen
- ksuffixlen
,
831 char *apply_refspecs(struct refspec
*refspecs
, int nr_refspec
,
836 for (i
= 0; i
< nr_refspec
; i
++) {
837 struct refspec
*refspec
= refspecs
+ i
;
838 if (refspec
->pattern
) {
839 if (match_name_with_pattern(refspec
->src
, name
,
842 } else if (!strcmp(refspec
->src
, name
))
843 return strdup(refspec
->dst
);
848 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
850 int find_src
= refspec
->src
== NULL
;
851 char *needle
, **result
;
856 return error("find_tracking: need either src or dst");
857 needle
= refspec
->dst
;
858 result
= &refspec
->src
;
860 needle
= refspec
->src
;
861 result
= &refspec
->dst
;
864 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
865 struct refspec
*fetch
= &remote
->fetch
[i
];
866 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
867 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
870 if (fetch
->pattern
) {
871 if (match_name_with_pattern(key
, needle
, value
, result
)) {
872 refspec
->force
= fetch
->force
;
875 } else if (!strcmp(needle
, key
)) {
876 *result
= xstrdup(value
);
877 refspec
->force
= fetch
->force
;
884 static struct ref
*alloc_ref_with_prefix(const char *prefix
, size_t prefixlen
,
887 size_t len
= strlen(name
);
888 struct ref
*ref
= xcalloc(1, sizeof(struct ref
) + prefixlen
+ len
+ 1);
889 memcpy(ref
->name
, prefix
, prefixlen
);
890 memcpy(ref
->name
+ prefixlen
, name
, len
);
894 struct ref
*alloc_ref(const char *name
)
896 return alloc_ref_with_prefix("", 0, name
);
899 static struct ref
*copy_ref(const struct ref
*ref
)
905 len
= strlen(ref
->name
);
906 cpy
= xmalloc(sizeof(struct ref
) + len
+ 1);
907 memcpy(cpy
, ref
, sizeof(struct ref
) + len
+ 1);
909 cpy
->symref
= ref
->symref
? xstrdup(ref
->symref
) : NULL
;
910 cpy
->remote_status
= ref
->remote_status
? xstrdup(ref
->remote_status
) : NULL
;
911 cpy
->peer_ref
= copy_ref(ref
->peer_ref
);
915 struct ref
*copy_ref_list(const struct ref
*ref
)
917 struct ref
*ret
= NULL
;
918 struct ref
**tail
= &ret
;
920 *tail
= copy_ref(ref
);
922 tail
= &((*tail
)->next
);
927 static void free_ref(struct ref
*ref
)
931 free_ref(ref
->peer_ref
);
932 free(ref
->remote_status
);
937 void free_refs(struct ref
*ref
)
947 static int count_refspec_match(const char *pattern
,
949 struct ref
**matched_ref
)
951 int patlen
= strlen(pattern
);
952 struct ref
*matched_weak
= NULL
;
953 struct ref
*matched
= NULL
;
957 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
958 char *name
= refs
->name
;
959 int namelen
= strlen(name
);
961 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
964 /* A match is "weak" if it is with refs outside
965 * heads or tags, and did not specify the pattern
966 * in full (e.g. "refs/remotes/origin/master") or at
967 * least from the toplevel (e.g. "remotes/origin/master");
968 * otherwise "git push $URL master" would result in
969 * ambiguity between remotes/origin/master and heads/master
970 * at the remote site.
972 if (namelen
!= patlen
&&
973 patlen
!= namelen
- 5 &&
974 prefixcmp(name
, "refs/heads/") &&
975 prefixcmp(name
, "refs/tags/")) {
976 /* We want to catch the case where only weak
977 * matches are found and there are multiple
978 * matches, and where more than one strong
979 * matches are found, as ambiguous. One
980 * strong match with zero or more weak matches
981 * are acceptable as a unique match.
992 *matched_ref
= matched_weak
;
996 *matched_ref
= matched
;
1001 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
1009 static struct ref
*try_explicit_object_name(const char *name
)
1011 unsigned char sha1
[20];
1015 ref
= alloc_ref("(delete)");
1016 hashclr(ref
->new_sha1
);
1019 if (get_sha1(name
, sha1
))
1021 ref
= alloc_ref(name
);
1022 hashcpy(ref
->new_sha1
, sha1
);
1026 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
1028 struct ref
*ret
= alloc_ref(name
);
1029 tail_link_ref(ret
, tail
);
1033 static char *guess_ref(const char *name
, struct ref
*peer
)
1035 struct strbuf buf
= STRBUF_INIT
;
1036 unsigned char sha1
[20];
1038 const char *r
= resolve_ref(peer
->name
, sha1
, 1, NULL
);
1042 if (!prefixcmp(r
, "refs/heads/"))
1043 strbuf_addstr(&buf
, "refs/heads/");
1044 else if (!prefixcmp(r
, "refs/tags/"))
1045 strbuf_addstr(&buf
, "refs/tags/");
1049 strbuf_addstr(&buf
, name
);
1050 return strbuf_detach(&buf
, NULL
);
1053 static int match_explicit(struct ref
*src
, struct ref
*dst
,
1054 struct ref
***dst_tail
,
1057 struct ref
*matched_src
, *matched_dst
;
1060 const char *dst_value
= rs
->dst
;
1063 if (rs
->pattern
|| rs
->matching
)
1066 matched_src
= matched_dst
= NULL
;
1067 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
1072 /* The source could be in the get_sha1() format
1073 * not a reference name. :refs/other is a
1074 * way to delete 'other' ref at the remote end.
1076 matched_src
= try_explicit_object_name(rs
->src
);
1078 return error("src refspec %s does not match any.", rs
->src
);
1082 return error("src refspec %s matches more than one.", rs
->src
);
1086 unsigned char sha1
[20];
1089 dst_value
= resolve_ref(matched_src
->name
, sha1
, 1, &flag
);
1091 ((flag
& REF_ISSYMREF
) &&
1092 prefixcmp(dst_value
, "refs/heads/")))
1093 die("%s cannot be resolved to branch.",
1097 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
1101 if (!memcmp(dst_value
, "refs/", 5))
1102 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
1103 else if ((dst_guess
= guess_ref(dst_value
, matched_src
)))
1104 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
1106 error("unable to push to unqualified destination: %s\n"
1107 "The destination refspec neither matches an "
1108 "existing ref on the remote nor\n"
1109 "begins with refs/, and we are unable to "
1110 "guess a prefix based on the source ref.",
1115 error("dst refspec %s matches more than one.",
1121 if (matched_dst
->peer_ref
)
1122 return error("dst ref %s receives from more than one src.",
1125 matched_dst
->peer_ref
= copy_src
? copy_ref(matched_src
) : matched_src
;
1126 matched_dst
->force
= rs
->force
;
1131 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
1132 struct ref
***dst_tail
, struct refspec
*rs
,
1136 for (i
= errs
= 0; i
< rs_nr
; i
++)
1137 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
[i
]);
1141 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
1143 const struct ref
*src
)
1146 int matching_refs
= -1;
1147 for (i
= 0; i
< rs_nr
; i
++) {
1148 if (rs
[i
].matching
&&
1149 (matching_refs
== -1 || rs
[i
].force
)) {
1154 if (rs
[i
].pattern
&& match_name_with_pattern(rs
[i
].src
, src
->name
,
1158 if (matching_refs
!= -1)
1159 return rs
+ matching_refs
;
1164 static struct ref
**tail_ref(struct ref
**head
)
1166 struct ref
**tail
= head
;
1168 tail
= &((*tail
)->next
);
1173 * Note. This is used only by "push"; refspec matching rules for
1174 * push and fetch are subtly different, so do not try to reuse it
1177 int match_refs(struct ref
*src
, struct ref
**dst
,
1178 int nr_refspec
, const char **refspec
, int flags
)
1181 int send_all
= flags
& MATCH_REFS_ALL
;
1182 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1184 static const char *default_refspec
[] = { ":", NULL
};
1185 struct ref
**dst_tail
= tail_ref(dst
);
1189 refspec
= default_refspec
;
1191 rs
= parse_push_refspec(nr_refspec
, (const char **) refspec
);
1192 errs
= match_explicit_refs(src
, *dst
, &dst_tail
, rs
, nr_refspec
);
1194 /* pick the remainder */
1195 for ( ; src
; src
= src
->next
) {
1196 struct ref
*dst_peer
;
1197 const struct refspec
*pat
= NULL
;
1202 pat
= check_pattern_match(rs
, nr_refspec
, src
);
1206 if (pat
->matching
) {
1208 * "matching refs"; traditionally we pushed everything
1209 * including refs outside refs/heads/ hierarchy, but
1210 * that does not make much sense these days.
1212 if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
1214 dst_name
= xstrdup(src
->name
);
1217 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
1218 if (!match_name_with_pattern(pat
->src
, src
->name
,
1219 dst_side
, &dst_name
))
1220 die("Didn't think it matches any more");
1222 dst_peer
= find_ref_by_name(*dst
, dst_name
);
1224 if (dst_peer
->peer_ref
)
1225 /* We're already sending something to this ref. */
1229 if (pat
->matching
&& !(send_all
|| send_mirror
))
1231 * Remote doesn't have it, and we have no
1232 * explicit pattern, and we don't have
1233 * --all nor --mirror.
1237 /* Create a new one and link it */
1238 dst_peer
= make_linked_ref(dst_name
, &dst_tail
);
1239 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
1241 dst_peer
->peer_ref
= copy_ref(src
);
1242 dst_peer
->force
= pat
->force
;
1251 void set_ref_status_for_push(struct ref
*remote_refs
, int send_mirror
,
1256 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1258 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
1259 else if (!send_mirror
)
1262 ref
->deletion
= is_null_sha1(ref
->new_sha1
);
1263 if (!ref
->deletion
&&
1264 !hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
1265 ref
->status
= REF_STATUS_UPTODATE
;
1269 /* This part determines what can overwrite what.
1272 * (0) you can always use --force or +A:B notation to
1273 * selectively force individual ref pairs.
1275 * (1) if the old thing does not exist, it is OK.
1277 * (2) if you do not have the old thing, you are not allowed
1278 * to overwrite it; you would not know what you are losing
1281 * (3) if both new and old are commit-ish, and new is a
1282 * descendant of old, it is OK.
1284 * (4) regardless of all of the above, removing :B is
1288 ref
->nonfastforward
=
1290 !is_null_sha1(ref
->old_sha1
) &&
1291 (!has_sha1_file(ref
->old_sha1
)
1292 || !ref_newer(ref
->new_sha1
, ref
->old_sha1
));
1294 if (ref
->nonfastforward
&& !ref
->force
&& !force_update
) {
1295 ref
->status
= REF_STATUS_REJECT_NONFASTFORWARD
;
1301 struct branch
*branch_get(const char *name
)
1306 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1307 ret
= current_branch
;
1309 ret
= make_branch(name
, 0);
1310 if (ret
&& ret
->remote_name
) {
1311 ret
->remote
= remote_get(ret
->remote_name
);
1312 if (ret
->merge_nr
) {
1314 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
1316 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1317 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1318 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1319 if (remote_find_tracking(ret
->remote
, ret
->merge
[i
])
1320 && !strcmp(ret
->remote_name
, "."))
1321 ret
->merge
[i
]->dst
= xstrdup(ret
->merge_name
[i
]);
1328 int branch_has_merge_config(struct branch
*branch
)
1330 return branch
&& !!branch
->merge
;
1333 int branch_merge_matches(struct branch
*branch
,
1335 const char *refname
)
1337 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1339 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
1342 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1343 const struct refspec
*refspec
)
1345 const struct ref
*ref
;
1346 struct ref
*ret
= NULL
;
1347 struct ref
**tail
= &ret
;
1351 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1352 if (strchr(ref
->name
, '^'))
1353 continue; /* a dereference item */
1354 if (match_name_with_pattern(refspec
->src
, ref
->name
,
1355 refspec
->dst
, &expn_name
)) {
1356 struct ref
*cpy
= copy_ref(ref
);
1358 cpy
->peer_ref
= alloc_ref(expn_name
);
1361 cpy
->peer_ref
->force
= 1;
1370 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1372 const struct ref
*ref
;
1373 for (ref
= refs
; ref
; ref
= ref
->next
) {
1374 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
1380 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1382 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1387 return copy_ref(ref
);
1390 static struct ref
*get_local_ref(const char *name
)
1392 if (!name
|| name
[0] == '\0')
1395 if (!prefixcmp(name
, "refs/"))
1396 return alloc_ref(name
);
1398 if (!prefixcmp(name
, "heads/") ||
1399 !prefixcmp(name
, "tags/") ||
1400 !prefixcmp(name
, "remotes/"))
1401 return alloc_ref_with_prefix("refs/", 5, name
);
1403 return alloc_ref_with_prefix("refs/heads/", 11, name
);
1406 int get_fetch_map(const struct ref
*remote_refs
,
1407 const struct refspec
*refspec
,
1411 struct ref
*ref_map
, **rmp
;
1413 if (refspec
->pattern
) {
1414 ref_map
= get_expanded_map(remote_refs
, refspec
);
1416 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1418 ref_map
= get_remote_ref(remote_refs
, name
);
1419 if (!missing_ok
&& !ref_map
)
1420 die("Couldn't find remote ref %s", name
);
1422 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1423 if (ref_map
->peer_ref
&& refspec
->force
)
1424 ref_map
->peer_ref
->force
= 1;
1428 for (rmp
= &ref_map
; *rmp
; ) {
1429 if ((*rmp
)->peer_ref
) {
1430 int st
= check_ref_format((*rmp
)->peer_ref
->name
+ 5);
1431 if (st
&& st
!= CHECK_REF_FORMAT_ONELEVEL
) {
1432 struct ref
*ignore
= *rmp
;
1433 error("* Ignoring funny ref '%s' locally",
1434 (*rmp
)->peer_ref
->name
);
1435 *rmp
= (*rmp
)->next
;
1436 free(ignore
->peer_ref
);
1441 rmp
= &((*rmp
)->next
);
1445 tail_link_ref(ref_map
, tail
);
1450 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
1454 for (; list
; list
= list
->next
)
1455 if (!strcmp(ref
->symref
, list
->name
)) {
1456 hashcpy(ref
->old_sha1
, list
->old_sha1
);
1462 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
1465 struct commit_list
*temp
= list
;
1466 temp
->item
->object
.flags
&= ~mark
;
1472 int ref_newer(const unsigned char *new_sha1
, const unsigned char *old_sha1
)
1475 struct commit
*old
, *new;
1476 struct commit_list
*list
, *used
;
1479 /* Both new and old must be commit-ish and new is descendant of
1480 * old. Otherwise we require --force.
1482 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
1483 if (!o
|| o
->type
!= OBJ_COMMIT
)
1485 old
= (struct commit
*) o
;
1487 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
1488 if (!o
|| o
->type
!= OBJ_COMMIT
)
1490 new = (struct commit
*) o
;
1492 if (parse_commit(new) < 0)
1496 commit_list_insert(new, &list
);
1498 new = pop_most_recent_commit(&list
, TMP_MARK
);
1499 commit_list_insert(new, &used
);
1505 unmark_and_free(list
, TMP_MARK
);
1506 unmark_and_free(used
, TMP_MARK
);
1511 * Return true if there is anything to report, otherwise false.
1513 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
)
1515 unsigned char sha1
[20];
1516 struct commit
*ours
, *theirs
;
1518 struct rev_info revs
;
1519 const char *rev_argv
[10], *base
;
1523 * Nothing to report unless we are marked to build on top of
1527 !branch
->merge
|| !branch
->merge
[0] || !branch
->merge
[0]->dst
)
1531 * If what we used to build on no longer exists, there is
1532 * nothing to report.
1534 base
= branch
->merge
[0]->dst
;
1535 if (!resolve_ref(base
, sha1
, 1, NULL
))
1537 theirs
= lookup_commit_reference(sha1
);
1541 if (!resolve_ref(branch
->refname
, sha1
, 1, NULL
))
1543 ours
= lookup_commit_reference(sha1
);
1547 /* are we the same? */
1551 /* Run "rev-list --left-right ours...theirs" internally... */
1553 rev_argv
[rev_argc
++] = NULL
;
1554 rev_argv
[rev_argc
++] = "--left-right";
1555 rev_argv
[rev_argc
++] = symmetric
;
1556 rev_argv
[rev_argc
++] = "--";
1557 rev_argv
[rev_argc
] = NULL
;
1559 strcpy(symmetric
, sha1_to_hex(ours
->object
.sha1
));
1560 strcpy(symmetric
+ 40, "...");
1561 strcpy(symmetric
+ 43, sha1_to_hex(theirs
->object
.sha1
));
1563 init_revisions(&revs
, NULL
);
1564 setup_revisions(rev_argc
, rev_argv
, &revs
, NULL
);
1565 prepare_revision_walk(&revs
);
1567 /* ... and count the commits on each side. */
1571 struct commit
*c
= get_revision(&revs
);
1574 if (c
->object
.flags
& SYMMETRIC_LEFT
)
1580 /* clear object flags smudged by the above traversal */
1581 clear_commit_marks(ours
, ALL_REV_FLAGS
);
1582 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
1587 * Return true when there is anything to report, otherwise false.
1589 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
)
1591 int num_ours
, num_theirs
;
1594 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
))
1597 base
= branch
->merge
[0]->dst
;
1598 base
= shorten_unambiguous_ref(base
, 0);
1600 strbuf_addf(sb
, "Your branch is ahead of '%s' "
1601 "by %d commit%s.\n",
1602 base
, num_ours
, (num_ours
== 1) ? "" : "s");
1604 strbuf_addf(sb
, "Your branch is behind '%s' "
1606 "and can be fast-forwarded.\n",
1607 base
, num_theirs
, (num_theirs
== 1) ? "" : "s");
1609 strbuf_addf(sb
, "Your branch and '%s' have diverged,\n"
1610 "and have %d and %d different commit(s) each, "
1612 base
, num_ours
, num_theirs
);
1616 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
1618 struct ref
***local_tail
= cb_data
;
1622 /* we already know it starts with refs/ to get here */
1623 if (check_ref_format(refname
+ 5))
1626 len
= strlen(refname
) + 1;
1627 ref
= xcalloc(1, sizeof(*ref
) + len
);
1628 hashcpy(ref
->new_sha1
, sha1
);
1629 memcpy(ref
->name
, refname
, len
);
1631 *local_tail
= &ref
->next
;
1635 struct ref
*get_local_heads(void)
1637 struct ref
*local_refs
= NULL
, **local_tail
= &local_refs
;
1638 for_each_ref(one_local_ref
, &local_tail
);
1642 struct ref
*guess_remote_head(const struct ref
*head
,
1643 const struct ref
*refs
,
1646 const struct ref
*r
;
1647 struct ref
*list
= NULL
;
1648 struct ref
**tail
= &list
;
1654 * Some transports support directly peeking at
1655 * where HEAD points; if that is the case, then
1656 * we don't have to guess.
1659 return copy_ref(find_ref_by_name(refs
, head
->symref
));
1661 /* If refs/heads/master could be right, it is. */
1663 r
= find_ref_by_name(refs
, "refs/heads/master");
1664 if (r
&& !hashcmp(r
->old_sha1
, head
->old_sha1
))
1668 /* Look for another ref that points there */
1669 for (r
= refs
; r
; r
= r
->next
) {
1670 if (r
!= head
&& !hashcmp(r
->old_sha1
, head
->old_sha1
)) {
1671 *tail
= copy_ref(r
);
1672 tail
= &((*tail
)->next
);
1681 struct stale_heads_info
{
1682 struct remote
*remote
;
1683 struct string_list
*ref_names
;
1684 struct ref
**stale_refs_tail
;
1687 static int get_stale_heads_cb(const char *refname
,
1688 const unsigned char *sha1
, int flags
, void *cb_data
)
1690 struct stale_heads_info
*info
= cb_data
;
1691 struct refspec refspec
;
1692 memset(&refspec
, 0, sizeof(refspec
));
1693 refspec
.dst
= (char *)refname
;
1694 if (!remote_find_tracking(info
->remote
, &refspec
)) {
1695 if (!((flags
& REF_ISSYMREF
) ||
1696 string_list_has_string(info
->ref_names
, refspec
.src
))) {
1697 struct ref
*ref
= make_linked_ref(refname
, &info
->stale_refs_tail
);
1698 hashcpy(ref
->new_sha1
, sha1
);
1704 struct ref
*get_stale_heads(struct remote
*remote
, struct ref
*fetch_map
)
1706 struct ref
*ref
, *stale_refs
= NULL
;
1707 struct string_list ref_names
= STRING_LIST_INIT_NODUP
;
1708 struct stale_heads_info info
;
1709 info
.remote
= remote
;
1710 info
.ref_names
= &ref_names
;
1711 info
.stale_refs_tail
= &stale_refs
;
1712 for (ref
= fetch_map
; ref
; ref
= ref
->next
)
1713 string_list_append(&ref_names
, ref
->name
);
1714 sort_string_list(&ref_names
);
1715 for_each_ref(get_stale_heads_cb
, &info
);
1716 string_list_clear(&ref_names
, 0);