2 #include "parse-options.h"
5 #include "string-list.h"
7 #include "run-command.h"
10 static const char * const builtin_remote_usage
[] = {
11 "git remote [-v | --verbose]",
12 "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
13 "git remote rename <old> <new>",
14 "git remote rm <name>",
15 "git remote set-head <name> (-a | -d | <branch>)",
16 "git remote [-v | --verbose] show [-n] <name>",
17 "git remote prune [-n | --dry-run] <name>",
18 "git remote [-v | --verbose] update [-p | --prune] [group | remote]",
19 "git remote set-url <name> <newurl> [<oldurl>]",
20 "git remote set-url --add <name> <newurl>",
21 "git remote set-url --delete <name> <url>",
25 static const char * const builtin_remote_add_usage
[] = {
26 "git remote add [<options>] <name> <url>",
30 static const char * const builtin_remote_rename_usage
[] = {
31 "git remote rename <old> <new>",
35 static const char * const builtin_remote_rm_usage
[] = {
36 "git remote rm <name>",
40 static const char * const builtin_remote_sethead_usage
[] = {
41 "git remote set-head <name> (-a | -d | <branch>])",
45 static const char * const builtin_remote_show_usage
[] = {
46 "git remote show [<options>] <name>",
50 static const char * const builtin_remote_prune_usage
[] = {
51 "git remote prune [<options>] <name>",
55 static const char * const builtin_remote_update_usage
[] = {
56 "git remote update [<options>] [<group> | <remote>]...",
60 static const char * const builtin_remote_seturl_usage
[] = {
61 "git remote set-url [--push] <name> <newurl> [<oldurl>]",
62 "git remote set-url --add <name> <newurl>",
63 "git remote set-url --delete <name> <url>",
67 #define GET_REF_STATES (1<<0)
68 #define GET_HEAD_NAMES (1<<1)
69 #define GET_PUSH_REF_STATES (1<<2)
73 static int show_all(void);
74 static int prune_remote(const char *remote
, int dry_run
);
76 static inline int postfixcmp(const char *string
, const char *postfix
)
78 int len1
= strlen(string
), len2
= strlen(postfix
);
81 return strcmp(string
+ len1
- len2
, postfix
);
84 static int opt_parse_track(const struct option
*opt
, const char *arg
, int not)
86 struct string_list
*list
= opt
->value
;
88 string_list_clear(list
, 0);
90 string_list_append(arg
, list
);
94 static int fetch_remote(const char *name
)
96 const char *argv
[] = { "fetch", name
, NULL
, NULL
};
101 printf("Updating %s\n", name
);
102 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
103 return error("Could not fetch %s", name
);
113 static int add(int argc
, const char **argv
)
115 int fetch
= 0, mirror
= 0, fetch_tags
= TAGS_DEFAULT
;
116 struct string_list track
= { NULL
, 0, 0 };
117 const char *master
= NULL
;
118 struct remote
*remote
;
119 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
120 const char *name
, *url
;
123 struct option options
[] = {
124 OPT_BOOLEAN('f', "fetch", &fetch
, "fetch the remote branches"),
125 OPT_SET_INT(0, "tags", &fetch_tags
,
126 "import all tags and associated objects when fetching",
128 OPT_SET_INT(0, NULL
, &fetch_tags
,
129 "or do not fetch any tag at all (--no-tags)", TAGS_UNSET
),
130 OPT_CALLBACK('t', "track", &track
, "branch",
131 "branch(es) to track", opt_parse_track
),
132 OPT_STRING('m', "master", &master
, "branch", "master branch"),
133 OPT_BOOLEAN(0, "mirror", &mirror
, "no separate remotes"),
137 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_add_usage
,
141 usage_with_options(builtin_remote_add_usage
, options
);
146 remote
= remote_get(name
);
147 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
148 remote
->fetch_refspec_nr
))
149 die("remote %s already exists.", name
);
151 strbuf_addf(&buf2
, "refs/heads/test:refs/remotes/%s/test", name
);
152 if (!valid_fetch_refspec(buf2
.buf
))
153 die("'%s' is not a valid remote name", name
);
155 strbuf_addf(&buf
, "remote.%s.url", name
);
156 if (git_config_set(buf
.buf
, url
))
160 strbuf_addf(&buf
, "remote.%s.fetch", name
);
163 string_list_append("*", &track
);
164 for (i
= 0; i
< track
.nr
; i
++) {
165 struct string_list_item
*item
= track
.items
+ i
;
168 strbuf_addch(&buf2
, '+');
170 strbuf_addf(&buf2
, "refs/%s:refs/%s",
171 item
->string
, item
->string
);
173 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
174 item
->string
, name
, item
->string
);
175 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
181 strbuf_addf(&buf
, "remote.%s.mirror", name
);
182 if (git_config_set(buf
.buf
, "true"))
186 if (fetch_tags
!= TAGS_DEFAULT
) {
188 strbuf_addf(&buf
, "remote.%s.tagopt", name
);
189 if (git_config_set(buf
.buf
,
190 fetch_tags
== TAGS_SET
? "--tags" : "--no-tags"))
194 if (fetch
&& fetch_remote(name
))
199 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
202 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
204 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
205 return error("Could not setup master '%s'", master
);
208 strbuf_release(&buf
);
209 strbuf_release(&buf2
);
210 string_list_clear(&track
, 0);
217 struct string_list merge
;
221 static struct string_list branch_list
;
223 static const char *abbrev_ref(const char *name
, const char *prefix
)
225 const char *abbrev
= skip_prefix(name
, prefix
);
230 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
232 static int config_read_branches(const char *key
, const char *value
, void *cb
)
234 if (!prefixcmp(key
, "branch.")) {
235 const char *orig_key
= key
;
237 struct string_list_item
*item
;
238 struct branch_info
*info
;
239 enum { REMOTE
, MERGE
, REBASE
} type
;
242 if (!postfixcmp(key
, ".remote")) {
243 name
= xstrndup(key
, strlen(key
) - 7);
245 } else if (!postfixcmp(key
, ".merge")) {
246 name
= xstrndup(key
, strlen(key
) - 6);
248 } else if (!postfixcmp(key
, ".rebase")) {
249 name
= xstrndup(key
, strlen(key
) - 7);
254 item
= string_list_insert(name
, &branch_list
);
257 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
259 if (type
== REMOTE
) {
260 if (info
->remote_name
)
261 warning("more than one %s", orig_key
);
262 info
->remote_name
= xstrdup(value
);
263 } else if (type
== MERGE
) {
264 char *space
= strchr(value
, ' ');
265 value
= abbrev_branch(value
);
268 merge
= xstrndup(value
, space
- value
);
269 string_list_append(merge
, &info
->merge
);
270 value
= abbrev_branch(space
+ 1);
271 space
= strchr(value
, ' ');
273 string_list_append(xstrdup(value
), &info
->merge
);
275 info
->rebase
= git_config_bool(orig_key
, value
);
280 static void read_branches(void)
284 git_config(config_read_branches
, NULL
);
288 struct remote
*remote
;
289 struct string_list
new, stale
, tracked
, heads
, push
;
293 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
295 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
296 struct ref
*ref
, *stale_refs
;
299 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
300 if (get_fetch_map(remote_refs
, states
->remote
->fetch
+ i
, &tail
, 1))
301 die("Could not get fetch map for refspec %s",
302 states
->remote
->fetch_refspec
[i
]);
304 states
->new.strdup_strings
= 1;
305 states
->tracked
.strdup_strings
= 1;
306 states
->stale
.strdup_strings
= 1;
307 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
308 unsigned char sha1
[20];
309 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
310 string_list_append(abbrev_branch(ref
->name
), &states
->new);
312 string_list_append(abbrev_branch(ref
->name
), &states
->tracked
);
314 stale_refs
= get_stale_heads(states
->remote
, fetch_map
);
315 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
316 struct string_list_item
*item
=
317 string_list_append(abbrev_branch(ref
->name
), &states
->stale
);
318 item
->util
= xstrdup(ref
->name
);
320 free_refs(stale_refs
);
321 free_refs(fetch_map
);
323 sort_string_list(&states
->new);
324 sort_string_list(&states
->tracked
);
325 sort_string_list(&states
->stale
);
334 PUSH_STATUS_CREATE
= 0,
336 PUSH_STATUS_UPTODATE
,
337 PUSH_STATUS_FASTFORWARD
,
338 PUSH_STATUS_OUTOFDATE
,
339 PUSH_STATUS_NOTQUERIED
,
343 static int get_push_ref_states(const struct ref
*remote_refs
,
344 struct ref_states
*states
)
346 struct remote
*remote
= states
->remote
;
347 struct ref
*ref
, *local_refs
, *push_map
;
351 local_refs
= get_local_heads();
352 push_map
= copy_ref_list(remote_refs
);
354 match_refs(local_refs
, &push_map
, remote
->push_refspec_nr
,
355 remote
->push_refspec
, MATCH_REFS_NONE
);
357 states
->push
.strdup_strings
= 1;
358 for (ref
= push_map
; ref
; ref
= ref
->next
) {
359 struct string_list_item
*item
;
360 struct push_info
*info
;
364 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
366 item
= string_list_append(abbrev_branch(ref
->peer_ref
->name
),
368 item
->util
= xcalloc(sizeof(struct push_info
), 1);
370 info
->forced
= ref
->force
;
371 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
373 if (is_null_sha1(ref
->new_sha1
)) {
374 info
->status
= PUSH_STATUS_DELETE
;
375 } else if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
))
376 info
->status
= PUSH_STATUS_UPTODATE
;
377 else if (is_null_sha1(ref
->old_sha1
))
378 info
->status
= PUSH_STATUS_CREATE
;
379 else if (has_sha1_file(ref
->old_sha1
) &&
380 ref_newer(ref
->new_sha1
, ref
->old_sha1
))
381 info
->status
= PUSH_STATUS_FASTFORWARD
;
383 info
->status
= PUSH_STATUS_OUTOFDATE
;
385 free_refs(local_refs
);
390 static int get_push_ref_states_noquery(struct ref_states
*states
)
393 struct remote
*remote
= states
->remote
;
394 struct string_list_item
*item
;
395 struct push_info
*info
;
400 states
->push
.strdup_strings
= 1;
401 if (!remote
->push_refspec_nr
) {
402 item
= string_list_append("(matching)", &states
->push
);
403 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
404 info
->status
= PUSH_STATUS_NOTQUERIED
;
405 info
->dest
= xstrdup(item
->string
);
407 for (i
= 0; i
< remote
->push_refspec_nr
; i
++) {
408 struct refspec
*spec
= remote
->push
+ i
;
410 item
= string_list_append("(matching)", &states
->push
);
411 else if (strlen(spec
->src
))
412 item
= string_list_append(spec
->src
, &states
->push
);
414 item
= string_list_append("(delete)", &states
->push
);
416 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
417 info
->forced
= spec
->force
;
418 info
->status
= PUSH_STATUS_NOTQUERIED
;
419 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
424 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
426 struct ref
*ref
, *matches
;
427 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
428 struct refspec refspec
;
432 refspec
.src
= refspec
.dst
= "refs/heads/*";
433 states
->heads
.strdup_strings
= 1;
434 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
435 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
437 for (ref
= matches
; ref
; ref
= ref
->next
)
438 string_list_append(abbrev_branch(ref
->name
), &states
->heads
);
440 free_refs(fetch_map
);
446 struct known_remote
{
447 struct known_remote
*next
;
448 struct remote
*remote
;
451 struct known_remotes
{
452 struct remote
*to_delete
;
453 struct known_remote
*list
;
456 static int add_known_remote(struct remote
*remote
, void *cb_data
)
458 struct known_remotes
*all
= cb_data
;
459 struct known_remote
*r
;
461 if (!strcmp(all
->to_delete
->name
, remote
->name
))
464 r
= xmalloc(sizeof(*r
));
471 struct branches_for_remote
{
472 struct remote
*remote
;
473 struct string_list
*branches
, *skipped
;
474 struct known_remotes
*keep
;
477 static int add_branch_for_removal(const char *refname
,
478 const unsigned char *sha1
, int flags
, void *cb_data
)
480 struct branches_for_remote
*branches
= cb_data
;
481 struct refspec refspec
;
482 struct string_list_item
*item
;
483 struct known_remote
*kr
;
485 memset(&refspec
, 0, sizeof(refspec
));
486 refspec
.dst
= (char *)refname
;
487 if (remote_find_tracking(branches
->remote
, &refspec
))
490 /* don't delete a branch if another remote also uses it */
491 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
492 memset(&refspec
, 0, sizeof(refspec
));
493 refspec
.dst
= (char *)refname
;
494 if (!remote_find_tracking(kr
->remote
, &refspec
))
498 /* don't delete non-remote refs */
499 if (prefixcmp(refname
, "refs/remotes")) {
500 /* advise user how to delete local branches */
501 if (!prefixcmp(refname
, "refs/heads/"))
502 string_list_append(abbrev_branch(refname
),
504 /* silently skip over other non-remote refs */
508 /* make sure that symrefs are deleted */
509 if (flags
& REF_ISSYMREF
)
510 return unlink(git_path("%s", refname
));
512 item
= string_list_append(refname
, branches
->branches
);
513 item
->util
= xmalloc(20);
514 hashcpy(item
->util
, sha1
);
522 struct string_list
*remote_branches
;
525 static int read_remote_branches(const char *refname
,
526 const unsigned char *sha1
, int flags
, void *cb_data
)
528 struct rename_info
*rename
= cb_data
;
529 struct strbuf buf
= STRBUF_INIT
;
530 struct string_list_item
*item
;
532 unsigned char orig_sha1
[20];
535 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
536 if (!prefixcmp(refname
, buf
.buf
)) {
537 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
538 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
539 if (flag
& REF_ISSYMREF
)
540 item
->util
= xstrdup(symref
);
548 static int migrate_file(struct remote
*remote
)
550 struct strbuf buf
= STRBUF_INIT
;
554 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
555 for (i
= 0; i
< remote
->url_nr
; i
++)
556 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
557 return error("Could not append '%s' to '%s'",
558 remote
->url
[i
], buf
.buf
);
560 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
561 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
562 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
563 return error("Could not append '%s' to '%s'",
564 remote
->push_refspec
[i
], buf
.buf
);
566 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
567 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
568 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
569 return error("Could not append '%s' to '%s'",
570 remote
->fetch_refspec
[i
], buf
.buf
);
571 if (remote
->origin
== REMOTE_REMOTES
)
572 path
= git_path("remotes/%s", remote
->name
);
573 else if (remote
->origin
== REMOTE_BRANCHES
)
574 path
= git_path("branches/%s", remote
->name
);
576 unlink_or_warn(path
);
580 static int mv(int argc
, const char **argv
)
582 struct option options
[] = {
585 struct remote
*oldremote
, *newremote
;
586 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
587 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
588 struct rename_info rename
;
592 usage_with_options(builtin_remote_rename_usage
, options
);
594 rename
.old
= argv
[1];
595 rename
.new = argv
[2];
596 rename
.remote_branches
= &remote_branches
;
598 oldremote
= remote_get(rename
.old
);
600 die("No such remote: %s", rename
.old
);
602 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
603 return migrate_file(oldremote
);
605 newremote
= remote_get(rename
.new);
606 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
607 die("remote %s already exists.", rename
.new);
609 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
610 if (!valid_fetch_refspec(buf
.buf
))
611 die("'%s' is not a valid remote name", rename
.new);
614 strbuf_addf(&buf
, "remote.%s", rename
.old
);
615 strbuf_addf(&buf2
, "remote.%s", rename
.new);
616 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
617 return error("Could not rename config section '%s' to '%s'",
621 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
622 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
623 return error("Could not remove config section '%s'", buf
.buf
);
624 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
628 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
629 ptr
= strstr(buf2
.buf
, rename
.old
);
631 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
632 rename
.new, strlen(rename
.new));
633 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
634 return error("Could not append '%s'", buf
.buf
);
638 for (i
= 0; i
< branch_list
.nr
; i
++) {
639 struct string_list_item
*item
= branch_list
.items
+ i
;
640 struct branch_info
*info
= item
->util
;
641 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old
)) {
643 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
644 if (git_config_set(buf
.buf
, rename
.new)) {
645 return error("Could not set '%s'", buf
.buf
);
651 * First remove symrefs, then rename the rest, finally create
654 for_each_ref(read_remote_branches
, &rename
);
655 for (i
= 0; i
< remote_branches
.nr
; i
++) {
656 struct string_list_item
*item
= remote_branches
.items
+ i
;
658 unsigned char sha1
[20];
660 resolve_ref(item
->string
, sha1
, 1, &flag
);
661 if (!(flag
& REF_ISSYMREF
))
663 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
664 die("deleting '%s' failed", item
->string
);
666 for (i
= 0; i
< remote_branches
.nr
; i
++) {
667 struct string_list_item
*item
= remote_branches
.items
+ i
;
672 strbuf_addstr(&buf
, item
->string
);
673 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
674 rename
.new, strlen(rename
.new));
676 strbuf_addf(&buf2
, "remote: renamed %s to %s",
677 item
->string
, buf
.buf
);
678 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
679 die("renaming '%s' failed", item
->string
);
681 for (i
= 0; i
< remote_branches
.nr
; i
++) {
682 struct string_list_item
*item
= remote_branches
.items
+ i
;
687 strbuf_addstr(&buf
, item
->string
);
688 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
689 rename
.new, strlen(rename
.new));
691 strbuf_addstr(&buf2
, item
->util
);
692 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
693 rename
.new, strlen(rename
.new));
695 strbuf_addf(&buf3
, "remote: renamed %s to %s",
696 item
->string
, buf
.buf
);
697 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
698 die("creating '%s' failed", buf
.buf
);
703 static int remove_branches(struct string_list
*branches
)
706 for (i
= 0; i
< branches
->nr
; i
++) {
707 struct string_list_item
*item
= branches
->items
+ i
;
708 const char *refname
= item
->string
;
709 unsigned char *sha1
= item
->util
;
711 if (delete_ref(refname
, sha1
, 0))
712 result
|= error("Could not remove branch %s", refname
);
717 static int rm(int argc
, const char **argv
)
719 struct option options
[] = {
722 struct remote
*remote
;
723 struct strbuf buf
= STRBUF_INIT
;
724 struct known_remotes known_remotes
= { NULL
, NULL
};
725 struct string_list branches
= { NULL
, 0, 0, 1 };
726 struct string_list skipped
= { NULL
, 0, 0, 1 };
727 struct branches_for_remote cb_data
= {
728 NULL
, &branches
, &skipped
, &known_remotes
733 usage_with_options(builtin_remote_rm_usage
, options
);
735 remote
= remote_get(argv
[1]);
737 die("No such remote: %s", argv
[1]);
739 known_remotes
.to_delete
= remote
;
740 for_each_remote(add_known_remote
, &known_remotes
);
742 strbuf_addf(&buf
, "remote.%s", remote
->name
);
743 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
744 return error("Could not remove config section '%s'", buf
.buf
);
747 for (i
= 0; i
< branch_list
.nr
; i
++) {
748 struct string_list_item
*item
= branch_list
.items
+ i
;
749 struct branch_info
*info
= item
->util
;
750 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
751 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
752 for (k
= keys
; *k
; k
++) {
754 strbuf_addf(&buf
, "branch.%s.%s",
756 if (git_config_set(buf
.buf
, NULL
)) {
757 strbuf_release(&buf
);
765 * We cannot just pass a function to for_each_ref() which deletes
766 * the branches one by one, since for_each_ref() relies on cached
767 * refs, which are invalidated when deleting a branch.
769 cb_data
.remote
= remote
;
770 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
771 strbuf_release(&buf
);
774 result
= remove_branches(&branches
);
775 string_list_clear(&branches
, 1);
778 fprintf(stderr
, skipped
.nr
== 1 ?
779 "Note: A non-remote branch was not removed; "
780 "to delete it, use:\n" :
781 "Note: Non-remote branches were not removed; "
782 "to delete them, use:\n");
783 for (i
= 0; i
< skipped
.nr
; i
++)
784 fprintf(stderr
, " git branch -d %s\n",
785 skipped
.items
[i
].string
);
787 string_list_clear(&skipped
, 0);
792 static void clear_push_info(void *util
, const char *string
)
794 struct push_info
*info
= util
;
799 static void free_remote_ref_states(struct ref_states
*states
)
801 string_list_clear(&states
->new, 0);
802 string_list_clear(&states
->stale
, 1);
803 string_list_clear(&states
->tracked
, 0);
804 string_list_clear(&states
->heads
, 0);
805 string_list_clear_func(&states
->push
, clear_push_info
);
808 static int append_ref_to_tracked_list(const char *refname
,
809 const unsigned char *sha1
, int flags
, void *cb_data
)
811 struct ref_states
*states
= cb_data
;
812 struct refspec refspec
;
814 if (flags
& REF_ISSYMREF
)
817 memset(&refspec
, 0, sizeof(refspec
));
818 refspec
.dst
= (char *)refname
;
819 if (!remote_find_tracking(states
->remote
, &refspec
))
820 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
825 static int get_remote_ref_states(const char *name
,
826 struct ref_states
*states
,
829 struct transport
*transport
;
830 const struct ref
*remote_refs
;
832 states
->remote
= remote_get(name
);
834 return error("No such remote: %s", name
);
839 transport
= transport_get(states
->remote
, states
->remote
->url_nr
> 0 ?
840 states
->remote
->url
[0] : NULL
);
841 remote_refs
= transport_get_remote_refs(transport
);
842 transport_disconnect(transport
);
845 if (query
& GET_REF_STATES
)
846 get_ref_states(remote_refs
, states
);
847 if (query
& GET_HEAD_NAMES
)
848 get_head_names(remote_refs
, states
);
849 if (query
& GET_PUSH_REF_STATES
)
850 get_push_ref_states(remote_refs
, states
);
852 for_each_ref(append_ref_to_tracked_list
, states
);
853 sort_string_list(&states
->tracked
);
854 get_push_ref_states_noquery(states
);
861 struct string_list
*list
;
862 struct ref_states
*states
;
867 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
869 struct show_info
*info
= cb_data
;
870 int n
= strlen(item
->string
);
873 string_list_insert(item
->string
, info
->list
);
877 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
879 struct show_info
*info
= cb_data
;
880 struct ref_states
*states
= info
->states
;
881 const char *name
= item
->string
;
883 if (states
->queried
) {
884 const char *fmt
= "%s";
885 const char *arg
= "";
886 if (string_list_has_string(&states
->new, name
)) {
887 fmt
= " new (next fetch will store in remotes/%s)";
888 arg
= states
->remote
->name
;
889 } else if (string_list_has_string(&states
->tracked
, name
))
891 else if (string_list_has_string(&states
->stale
, name
))
892 arg
= " stale (use 'git remote prune' to remove)";
895 printf(" %-*s", info
->width
, name
);
899 printf(" %s\n", name
);
904 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
906 struct show_info
*show_info
= cb_data
;
907 struct ref_states
*states
= show_info
->states
;
908 struct branch_info
*branch_info
= branch_item
->util
;
909 struct string_list_item
*item
;
912 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
913 strcmp(states
->remote
->name
, branch_info
->remote_name
))
915 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
916 show_info
->width
= n
;
917 if (branch_info
->rebase
)
918 show_info
->any_rebase
= 1;
920 item
= string_list_insert(branch_item
->string
, show_info
->list
);
921 item
->util
= branch_info
;
926 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
928 struct show_info
*show_info
= cb_data
;
929 struct branch_info
*branch_info
= item
->util
;
930 struct string_list
*merge
= &branch_info
->merge
;
934 if (branch_info
->rebase
&& branch_info
->merge
.nr
> 1) {
935 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
940 printf(" %-*s ", show_info
->width
, item
->string
);
941 if (branch_info
->rebase
) {
942 printf("rebases onto remote %s\n", merge
->items
[0].string
);
944 } else if (show_info
->any_rebase
) {
945 printf(" merges with remote %s\n", merge
->items
[0].string
);
946 also
= " and with remote";
948 printf("merges with remote %s\n", merge
->items
[0].string
);
949 also
= " and with remote";
951 for (i
= 1; i
< merge
->nr
; i
++)
952 printf(" %-*s %s %s\n", show_info
->width
, "", also
,
953 merge
->items
[i
].string
);
958 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
960 struct show_info
*show_info
= cb_data
;
961 struct push_info
*push_info
= push_item
->util
;
962 struct string_list_item
*item
;
964 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
965 show_info
->width
= n
;
966 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
967 show_info
->width2
= n
;
968 item
= string_list_append(push_item
->string
, show_info
->list
);
969 item
->util
= push_item
->util
;
974 * Sorting comparison for a string list that has push_info
975 * structs in its util field
977 static int cmp_string_with_push(const void *va
, const void *vb
)
979 const struct string_list_item
*a
= va
;
980 const struct string_list_item
*b
= vb
;
981 const struct push_info
*a_push
= a
->util
;
982 const struct push_info
*b_push
= b
->util
;
983 int cmp
= strcmp(a
->string
, b
->string
);
984 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
987 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
989 struct show_info
*show_info
= cb_data
;
990 struct push_info
*push_info
= item
->util
;
991 char *src
= item
->string
, *status
= NULL
;
993 switch (push_info
->status
) {
994 case PUSH_STATUS_CREATE
:
997 case PUSH_STATUS_DELETE
:
1001 case PUSH_STATUS_UPTODATE
:
1002 status
= "up to date";
1004 case PUSH_STATUS_FASTFORWARD
:
1005 status
= "fast-forwardable";
1007 case PUSH_STATUS_OUTOFDATE
:
1008 status
= "local out of date";
1010 case PUSH_STATUS_NOTQUERIED
:
1014 printf(" %-*s %s to %-*s (%s)\n", show_info
->width
, src
,
1015 push_info
->forced
? "forces" : "pushes",
1016 show_info
->width2
, push_info
->dest
, status
);
1018 printf(" %-*s %s to %s\n", show_info
->width
, src
,
1019 push_info
->forced
? "forces" : "pushes",
1024 static int show(int argc
, const char **argv
)
1026 int no_query
= 0, result
= 0, query_flag
= 0;
1027 struct option options
[] = {
1028 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
1031 struct ref_states states
;
1032 struct string_list info_list
= { NULL
, 0, 0, 0 };
1033 struct show_info info
;
1035 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_show_usage
,
1042 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1044 memset(&states
, 0, sizeof(states
));
1045 memset(&info
, 0, sizeof(info
));
1046 info
.states
= &states
;
1047 info
.list
= &info_list
;
1048 for (; argc
; argc
--, argv
++) {
1053 get_remote_ref_states(*argv
, &states
, query_flag
);
1055 printf("* remote %s\n", *argv
);
1056 printf(" Fetch URL: %s\n", states
.remote
->url_nr
> 0 ?
1057 states
.remote
->url
[0] : "(no URL)");
1058 if (states
.remote
->pushurl_nr
) {
1059 url
= states
.remote
->pushurl
;
1060 url_nr
= states
.remote
->pushurl_nr
;
1062 url
= states
.remote
->url
;
1063 url_nr
= states
.remote
->url_nr
;
1065 for (i
=0; i
< url_nr
; i
++)
1066 printf(" Push URL: %s\n", url
[i
]);
1068 printf(" Push URL: %s\n", "(no URL)");
1070 printf(" HEAD branch: (not queried)\n");
1071 else if (!states
.heads
.nr
)
1072 printf(" HEAD branch: (unknown)\n");
1073 else if (states
.heads
.nr
== 1)
1074 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
1076 printf(" HEAD branch (remote HEAD is ambiguous,"
1077 " may be one of the following):\n");
1078 for (i
= 0; i
< states
.heads
.nr
; i
++)
1079 printf(" %s\n", states
.heads
.items
[i
].string
);
1082 /* remote branch info */
1084 for_each_string_list(&states
.new, add_remote_to_show_info
, &info
);
1085 for_each_string_list(&states
.tracked
, add_remote_to_show_info
, &info
);
1086 for_each_string_list(&states
.stale
, add_remote_to_show_info
, &info
);
1088 printf(" Remote branch%s:%s\n",
1089 info
.list
->nr
> 1 ? "es" : "",
1090 no_query
? " (status not queried)" : "");
1091 for_each_string_list(info
.list
, show_remote_info_item
, &info
);
1092 string_list_clear(info
.list
, 0);
1096 info
.any_rebase
= 0;
1097 for_each_string_list(&branch_list
, add_local_to_show_info
, &info
);
1099 printf(" Local branch%s configured for 'git pull':\n",
1100 info
.list
->nr
> 1 ? "es" : "");
1101 for_each_string_list(info
.list
, show_local_info_item
, &info
);
1102 string_list_clear(info
.list
, 0);
1105 if (states
.remote
->mirror
)
1106 printf(" Local refs will be mirrored by 'git push'\n");
1108 info
.width
= info
.width2
= 0;
1109 for_each_string_list(&states
.push
, add_push_to_show_info
, &info
);
1110 qsort(info
.list
->items
, info
.list
->nr
,
1111 sizeof(*info
.list
->items
), cmp_string_with_push
);
1113 printf(" Local ref%s configured for 'git push'%s:\n",
1114 info
.list
->nr
> 1 ? "s" : "",
1115 no_query
? " (status not queried)" : "");
1116 for_each_string_list(info
.list
, show_push_info_item
, &info
);
1117 string_list_clear(info
.list
, 0);
1119 free_remote_ref_states(&states
);
1125 static int set_head(int argc
, const char **argv
)
1127 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1128 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1129 char *head_name
= NULL
;
1131 struct option options
[] = {
1132 OPT_BOOLEAN('a', "auto", &opt_a
,
1133 "set refs/remotes/<name>/HEAD according to remote"),
1134 OPT_BOOLEAN('d', "delete", &opt_d
,
1135 "delete refs/remotes/<name>/HEAD"),
1138 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_sethead_usage
,
1141 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1143 if (!opt_a
&& !opt_d
&& argc
== 2) {
1144 head_name
= xstrdup(argv
[1]);
1145 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1146 struct ref_states states
;
1147 memset(&states
, 0, sizeof(states
));
1148 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1149 if (!states
.heads
.nr
)
1150 result
|= error("Cannot determine remote HEAD");
1151 else if (states
.heads
.nr
> 1) {
1152 result
|= error("Multiple remote HEAD branches. "
1153 "Please choose one explicitly with:");
1154 for (i
= 0; i
< states
.heads
.nr
; i
++)
1155 fprintf(stderr
, " git remote set-head %s %s\n",
1156 argv
[0], states
.heads
.items
[i
].string
);
1158 head_name
= xstrdup(states
.heads
.items
[0].string
);
1159 free_remote_ref_states(&states
);
1160 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1161 if (delete_ref(buf
.buf
, NULL
, REF_NODEREF
))
1162 result
|= error("Could not delete %s", buf
.buf
);
1164 usage_with_options(builtin_remote_sethead_usage
, options
);
1167 unsigned char sha1
[20];
1168 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1169 /* make sure it's valid */
1170 if (!resolve_ref(buf2
.buf
, sha1
, 1, NULL
))
1171 result
|= error("Not a valid ref: %s", buf2
.buf
);
1172 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1173 result
|= error("Could not setup %s", buf
.buf
);
1175 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1179 strbuf_release(&buf
);
1180 strbuf_release(&buf2
);
1184 static int prune(int argc
, const char **argv
)
1186 int dry_run
= 0, result
= 0;
1187 struct option options
[] = {
1188 OPT__DRY_RUN(&dry_run
),
1192 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_prune_usage
,
1196 usage_with_options(builtin_remote_prune_usage
, options
);
1198 for (; argc
; argc
--, argv
++)
1199 result
|= prune_remote(*argv
, dry_run
);
1204 static int prune_remote(const char *remote
, int dry_run
)
1207 struct ref_states states
;
1208 const char *dangling_msg
= dry_run
1209 ? " %s will become dangling!\n"
1210 : " %s has become dangling!\n";
1212 memset(&states
, 0, sizeof(states
));
1213 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1215 if (states
.stale
.nr
) {
1216 printf("Pruning %s\n", remote
);
1218 states
.remote
->url_nr
1219 ? states
.remote
->url
[0]
1223 for (i
= 0; i
< states
.stale
.nr
; i
++) {
1224 const char *refname
= states
.stale
.items
[i
].util
;
1227 result
|= delete_ref(refname
, NULL
, 0);
1229 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
1230 abbrev_ref(refname
, "refs/remotes/"));
1231 warn_dangling_symref(stdout
, dangling_msg
, refname
);
1234 free_remote_ref_states(&states
);
1238 static int get_remote_default(const char *key
, const char *value
, void *priv
)
1240 if (strcmp(key
, "remotes.default") == 0) {
1247 static int update(int argc
, const char **argv
)
1250 struct option options
[] = {
1251 OPT_BOOLEAN('p', "prune", &prune
,
1252 "prune remotes after fetching"),
1255 const char **fetch_argv
;
1257 int default_defined
= 0;
1259 fetch_argv
= xmalloc(sizeof(char *) * (argc
+5));
1261 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_update_usage
,
1262 PARSE_OPT_KEEP_ARGV0
);
1264 fetch_argv
[fetch_argc
++] = "fetch";
1267 fetch_argv
[fetch_argc
++] = "--prune";
1269 fetch_argv
[fetch_argc
++] = "-v";
1270 fetch_argv
[fetch_argc
++] = "--multiple";
1272 fetch_argv
[fetch_argc
++] = "default";
1273 for (i
= 1; i
< argc
; i
++)
1274 fetch_argv
[fetch_argc
++] = argv
[i
];
1276 if (strcmp(fetch_argv
[fetch_argc
-1], "default") == 0) {
1277 git_config(get_remote_default
, &default_defined
);
1278 if (!default_defined
)
1279 fetch_argv
[fetch_argc
-1] = "--all";
1282 fetch_argv
[fetch_argc
] = NULL
;
1284 return run_command_v_opt(fetch_argv
, RUN_GIT_CMD
);
1287 static int set_url(int argc
, const char **argv
)
1289 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1290 int matches
= 0, negative_matches
= 0;
1291 const char *remotename
= NULL
;
1292 const char *newurl
= NULL
;
1293 const char *oldurl
= NULL
;
1294 struct remote
*remote
;
1296 const char **urlset
;
1298 struct strbuf name_buf
= STRBUF_INIT
;
1299 struct option options
[] = {
1300 OPT_BOOLEAN('\0', "push", &push_mode
,
1301 "manipulate push URLs"),
1302 OPT_BOOLEAN('\0', "add", &add_mode
,
1304 OPT_BOOLEAN('\0', "delete", &delete_mode
,
1308 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_update_usage
,
1309 PARSE_OPT_KEEP_ARGV0
);
1311 if (add_mode
&& delete_mode
)
1312 die("--add --delete doesn't make sense");
1314 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1315 usage_with_options(builtin_remote_seturl_usage
, options
);
1317 remotename
= argv
[1];
1325 if (!remote_is_configured(remotename
))
1326 die("No such remote '%s'", remotename
);
1327 remote
= remote_get(remotename
);
1330 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1331 urlset
= remote
->pushurl
;
1332 urlset_nr
= remote
->pushurl_nr
;
1334 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1335 urlset
= remote
->url
;
1336 urlset_nr
= remote
->url_nr
;
1339 /* Special cases that add new entry. */
1340 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1342 git_config_set_multivar(name_buf
.buf
, newurl
,
1345 git_config_set(name_buf
.buf
, newurl
);
1346 strbuf_release(&name_buf
);
1350 /* Old URL specified. Demand that one matches. */
1351 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1352 die("Invalid old URL pattern: %s", oldurl
);
1354 for (i
= 0; i
< urlset_nr
; i
++)
1355 if (!regexec(&old_regex
, urlset
[i
], 0, NULL
, 0))
1359 if (!delete_mode
&& !matches
)
1360 die("No such URL found: %s", oldurl
);
1361 if (delete_mode
&& !negative_matches
&& !push_mode
)
1362 die("Will not delete all non-push URLs");
1364 regfree(&old_regex
);
1367 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1369 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
, 1);
1373 static int get_one_entry(struct remote
*remote
, void *priv
)
1375 struct string_list
*list
= priv
;
1376 struct strbuf url_buf
= STRBUF_INIT
;
1380 if (remote
->url_nr
> 0) {
1381 strbuf_addf(&url_buf
, "%s (fetch)", remote
->url
[0]);
1382 string_list_append(remote
->name
, list
)->util
=
1383 strbuf_detach(&url_buf
, NULL
);
1385 string_list_append(remote
->name
, list
)->util
= NULL
;
1386 if (remote
->pushurl_nr
) {
1387 url
= remote
->pushurl
;
1388 url_nr
= remote
->pushurl_nr
;
1391 url_nr
= remote
->url_nr
;
1393 for (i
= 0; i
< url_nr
; i
++)
1395 strbuf_addf(&url_buf
, "%s (push)", url
[i
]);
1396 string_list_append(remote
->name
, list
)->util
=
1397 strbuf_detach(&url_buf
, NULL
);
1403 static int show_all(void)
1405 struct string_list list
= { NULL
, 0, 0 };
1408 list
.strdup_strings
= 1;
1409 result
= for_each_remote(get_one_entry
, &list
);
1414 sort_string_list(&list
);
1415 for (i
= 0; i
< list
.nr
; i
++) {
1416 struct string_list_item
*item
= list
.items
+ i
;
1418 printf("%s\t%s\n", item
->string
,
1419 item
->util
? (const char *)item
->util
: "");
1421 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1423 printf("%s\n", item
->string
);
1427 string_list_clear(&list
, 1);
1431 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1433 struct option options
[] = {
1434 OPT_BOOLEAN('v', "verbose", &verbose
, "be verbose; must be placed before a subcommand"),
1439 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1440 PARSE_OPT_STOP_AT_NON_OPTION
);
1443 result
= show_all();
1444 else if (!strcmp(argv
[0], "add"))
1445 result
= add(argc
, argv
);
1446 else if (!strcmp(argv
[0], "rename"))
1447 result
= mv(argc
, argv
);
1448 else if (!strcmp(argv
[0], "rm"))
1449 result
= rm(argc
, argv
);
1450 else if (!strcmp(argv
[0], "set-head"))
1451 result
= set_head(argc
, argv
);
1452 else if (!strcmp(argv
[0], "set-url"))
1453 result
= set_url(argc
, argv
);
1454 else if (!strcmp(argv
[0], "show"))
1455 result
= show(argc
, argv
);
1456 else if (!strcmp(argv
[0], "prune"))
1457 result
= prune(argc
, argv
);
1458 else if (!strcmp(argv
[0], "update"))
1459 result
= update(argc
, argv
);
1461 error("Unknown subcommand: %s", argv
[0]);
1462 usage_with_options(builtin_remote_usage
, options
);
1465 return result
? 1 : 0;