4 #include "parse-options.h"
8 #include "string-list.h"
10 #include "run-command.h"
14 #include "object-store-ll.h"
16 #include "commit-reach.h"
19 static const char * const builtin_remote_usage
[] = {
20 "git remote [-v | --verbose]",
21 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
22 N_("git remote rename [--[no-]progress] <old> <new>"),
23 N_("git remote remove <name>"),
24 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
25 N_("git remote [-v | --verbose] show [-n] <name>"),
26 N_("git remote prune [-n | --dry-run] <name>"),
27 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
28 N_("git remote set-branches [--add] <name> <branch>..."),
29 N_("git remote get-url [--push] [--all] <name>"),
30 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
31 N_("git remote set-url --add <name> <newurl>"),
32 N_("git remote set-url --delete <name> <url>"),
36 static const char * const builtin_remote_add_usage
[] = {
37 N_("git remote add [<options>] <name> <url>"),
41 static const char * const builtin_remote_rename_usage
[] = {
42 N_("git remote rename [--[no-]progress] <old> <new>"),
46 static const char * const builtin_remote_rm_usage
[] = {
47 N_("git remote remove <name>"),
51 static const char * const builtin_remote_sethead_usage
[] = {
52 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
56 static const char * const builtin_remote_setbranches_usage
[] = {
57 N_("git remote set-branches <name> <branch>..."),
58 N_("git remote set-branches --add <name> <branch>..."),
62 static const char * const builtin_remote_show_usage
[] = {
63 N_("git remote show [<options>] <name>"),
67 static const char * const builtin_remote_prune_usage
[] = {
68 N_("git remote prune [<options>] <name>"),
72 static const char * const builtin_remote_update_usage
[] = {
73 N_("git remote update [<options>] [<group> | <remote>]..."),
77 static const char * const builtin_remote_geturl_usage
[] = {
78 N_("git remote get-url [--push] [--all] <name>"),
82 static const char * const builtin_remote_seturl_usage
[] = {
83 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
84 N_("git remote set-url --add <name> <newurl>"),
85 N_("git remote set-url --delete <name> <url>"),
89 #define GET_REF_STATES (1<<0)
90 #define GET_HEAD_NAMES (1<<1)
91 #define GET_PUSH_REF_STATES (1<<2)
95 static int fetch_remote(const char *name
)
97 struct child_process cmd
= CHILD_PROCESS_INIT
;
99 strvec_push(&cmd
.args
, "fetch");
101 strvec_push(&cmd
.args
, "-v");
102 strvec_push(&cmd
.args
, name
);
104 printf_ln(_("Updating %s"), name
);
105 if (run_command(&cmd
))
106 return error(_("Could not fetch %s"), name
);
116 #define MIRROR_NONE 0
117 #define MIRROR_FETCH 1
118 #define MIRROR_PUSH 2
119 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
121 static void add_branch(const char *key
, const char *branchname
,
122 const char *remotename
, int mirror
, struct strbuf
*tmp
)
125 strbuf_addch(tmp
, '+');
127 strbuf_addf(tmp
, "refs/%s:refs/%s",
128 branchname
, branchname
);
130 strbuf_addf(tmp
, "refs/heads/%s:refs/remotes/%s/%s",
131 branchname
, remotename
, branchname
);
132 git_config_set_multivar(key
, tmp
->buf
, "^$", 0);
135 static const char mirror_advice
[] =
136 N_("--mirror is dangerous and deprecated; please\n"
137 "\t use --mirror=fetch or --mirror=push instead");
139 static int parse_mirror_opt(const struct option
*opt
, const char *arg
, int not)
141 unsigned *mirror
= opt
->value
;
143 *mirror
= MIRROR_NONE
;
145 warning("%s", _(mirror_advice
));
146 *mirror
= MIRROR_BOTH
;
148 else if (!strcmp(arg
, "fetch"))
149 *mirror
= MIRROR_FETCH
;
150 else if (!strcmp(arg
, "push"))
151 *mirror
= MIRROR_PUSH
;
153 return error(_("unknown --mirror argument: %s"), arg
);
157 static int add(int argc
, const char **argv
, const char *prefix
)
159 int fetch
= 0, fetch_tags
= TAGS_DEFAULT
;
160 unsigned mirror
= MIRROR_NONE
;
161 struct string_list track
= STRING_LIST_INIT_NODUP
;
162 const char *master
= NULL
;
163 struct remote
*remote
;
164 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
165 const char *name
, *url
;
168 struct option options
[] = {
169 OPT_BOOL('f', "fetch", &fetch
, N_("fetch the remote branches")),
170 OPT_SET_INT(0, "tags", &fetch_tags
,
171 N_("import all tags and associated objects when fetching\n"
172 "or do not fetch any tag at all (--no-tags)"),
174 OPT_STRING_LIST('t', "track", &track
, N_("branch"),
175 N_("branch(es) to track")),
176 OPT_STRING('m', "master", &master
, N_("branch"), N_("master branch")),
177 OPT_CALLBACK_F(0, "mirror", &mirror
, "(push|fetch)",
178 N_("set up remote as a mirror to push to or fetch from"),
179 PARSE_OPT_OPTARG
| PARSE_OPT_COMP_ARG
, parse_mirror_opt
),
183 argc
= parse_options(argc
, argv
, prefix
, options
,
184 builtin_remote_add_usage
, 0);
187 usage_with_options(builtin_remote_add_usage
, options
);
189 if (mirror
&& master
)
190 die(_("specifying a master branch makes no sense with --mirror"));
191 if (mirror
&& !(mirror
& MIRROR_FETCH
) && track
.nr
)
192 die(_("specifying branches to track makes sense only with fetch mirrors"));
197 remote
= remote_get(name
);
198 if (remote_is_configured(remote
, 1)) {
199 error(_("remote %s already exists."), name
);
203 if (!valid_remote_name(name
))
204 die(_("'%s' is not a valid remote name"), name
);
206 strbuf_addf(&buf
, "remote.%s.url", name
);
207 git_config_set(buf
.buf
, url
);
209 if (!mirror
|| mirror
& MIRROR_FETCH
) {
211 strbuf_addf(&buf
, "remote.%s.fetch", name
);
213 string_list_append(&track
, "*");
214 for (i
= 0; i
< track
.nr
; i
++) {
215 add_branch(buf
.buf
, track
.items
[i
].string
,
216 name
, mirror
, &buf2
);
220 if (mirror
& MIRROR_PUSH
) {
222 strbuf_addf(&buf
, "remote.%s.mirror", name
);
223 git_config_set(buf
.buf
, "true");
226 if (fetch_tags
!= TAGS_DEFAULT
) {
228 strbuf_addf(&buf
, "remote.%s.tagOpt", name
);
229 git_config_set(buf
.buf
,
230 fetch_tags
== TAGS_SET
? "--tags" : "--no-tags");
233 if (fetch
&& fetch_remote(name
))
238 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
241 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
243 if (refs_update_symref(get_main_ref_store(the_repository
), buf
.buf
, buf2
.buf
, "remote add"))
244 return error(_("Could not setup master '%s'"), master
);
247 strbuf_release(&buf
);
248 strbuf_release(&buf2
);
249 string_list_clear(&track
, 0);
256 struct string_list merge
;
257 enum rebase_type rebase
;
258 char *push_remote_name
;
261 static struct string_list branch_list
= STRING_LIST_INIT_DUP
;
263 static const char *abbrev_ref(const char *name
, const char *prefix
)
265 skip_prefix(name
, prefix
, &name
);
268 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
270 static int config_read_branches(const char *key
, const char *value
,
271 const struct config_context
*ctx UNUSED
,
274 const char *orig_key
= key
;
276 struct string_list_item
*item
;
277 struct branch_info
*info
;
278 enum { REMOTE
, MERGE
, REBASE
, PUSH_REMOTE
} type
;
281 if (!starts_with(key
, "branch."))
284 key
+= strlen("branch.");
285 if (strip_suffix(key
, ".remote", &key_len
))
287 else if (strip_suffix(key
, ".merge", &key_len
))
289 else if (strip_suffix(key
, ".rebase", &key_len
))
291 else if (strip_suffix(key
, ".pushremote", &key_len
))
296 name
= xmemdupz(key
, key_len
);
297 item
= string_list_insert(&branch_list
, name
);
300 item
->util
= xcalloc(1, sizeof(struct branch_info
));
304 if (info
->remote_name
)
305 warning(_("more than one %s"), orig_key
);
306 info
->remote_name
= xstrdup(value
);
309 char *space
= strchr(value
, ' ');
310 value
= abbrev_branch(value
);
313 merge
= xstrndup(value
, space
- value
);
314 string_list_append(&info
->merge
, merge
);
315 value
= abbrev_branch(space
+ 1);
316 space
= strchr(value
, ' ');
318 string_list_append(&info
->merge
, xstrdup(value
));
323 * Consider invalid values as false and check the
324 * truth value with >= REBASE_TRUE.
326 info
->rebase
= rebase_parse_value(value
);
327 if (info
->rebase
== REBASE_INVALID
)
328 warning(_("unhandled branch.%s.rebase=%s; assuming "
329 "'true'"), name
, value
);
332 if (info
->push_remote_name
)
333 warning(_("more than one %s"), orig_key
);
334 info
->push_remote_name
= xstrdup(value
);
337 BUG("unexpected type=%d", type
);
344 static void read_branches(void)
348 git_config(config_read_branches
, NULL
);
352 struct remote
*remote
;
353 struct string_list new_refs
, skipped
, stale
, tracked
, heads
, push
;
357 #define REF_STATES_INIT { \
358 .new_refs = STRING_LIST_INIT_DUP, \
359 .skipped = STRING_LIST_INIT_DUP, \
360 .stale = STRING_LIST_INIT_DUP, \
361 .tracked = STRING_LIST_INIT_DUP, \
362 .heads = STRING_LIST_INIT_DUP, \
363 .push = STRING_LIST_INIT_DUP, \
366 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
368 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
369 struct ref
*ref
, *stale_refs
;
372 for (i
= 0; i
< states
->remote
->fetch
.nr
; i
++)
373 if (get_fetch_map(remote_refs
, &states
->remote
->fetch
.items
[i
], &tail
, 1))
374 die(_("Could not get fetch map for refspec %s"),
375 states
->remote
->fetch
.raw
[i
]);
377 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
378 if (omit_name_by_refspec(ref
->name
, &states
->remote
->fetch
))
379 string_list_append(&states
->skipped
, abbrev_branch(ref
->name
));
380 else if (!ref
->peer_ref
|| !refs_ref_exists(get_main_ref_store(the_repository
), ref
->peer_ref
->name
))
381 string_list_append(&states
->new_refs
, abbrev_branch(ref
->name
));
383 string_list_append(&states
->tracked
, abbrev_branch(ref
->name
));
385 stale_refs
= get_stale_heads(&states
->remote
->fetch
, fetch_map
);
386 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
387 struct string_list_item
*item
=
388 string_list_append(&states
->stale
, abbrev_branch(ref
->name
));
389 item
->util
= xstrdup(ref
->name
);
391 free_refs(stale_refs
);
392 free_refs(fetch_map
);
394 string_list_sort(&states
->new_refs
);
395 string_list_sort(&states
->skipped
);
396 string_list_sort(&states
->tracked
);
397 string_list_sort(&states
->stale
);
406 PUSH_STATUS_CREATE
= 0,
408 PUSH_STATUS_UPTODATE
,
409 PUSH_STATUS_FASTFORWARD
,
410 PUSH_STATUS_OUTOFDATE
,
411 PUSH_STATUS_NOTQUERIED
415 static int get_push_ref_states(const struct ref
*remote_refs
,
416 struct ref_states
*states
)
418 struct remote
*remote
= states
->remote
;
419 struct ref
*ref
, *local_refs
, *push_map
;
423 local_refs
= get_local_heads();
424 push_map
= copy_ref_list(remote_refs
);
426 match_push_refs(local_refs
, &push_map
, &remote
->push
, MATCH_REFS_NONE
);
428 for (ref
= push_map
; ref
; ref
= ref
->next
) {
429 struct string_list_item
*item
;
430 struct push_info
*info
;
434 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
436 item
= string_list_append(&states
->push
,
437 abbrev_branch(ref
->peer_ref
->name
));
438 item
->util
= xcalloc(1, sizeof(struct push_info
));
440 info
->forced
= ref
->force
;
441 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
443 if (is_null_oid(&ref
->new_oid
)) {
444 info
->status
= PUSH_STATUS_DELETE
;
445 } else if (oideq(&ref
->old_oid
, &ref
->new_oid
))
446 info
->status
= PUSH_STATUS_UPTODATE
;
447 else if (is_null_oid(&ref
->old_oid
))
448 info
->status
= PUSH_STATUS_CREATE
;
449 else if (repo_has_object_file(the_repository
, &ref
->old_oid
) &&
450 ref_newer(&ref
->new_oid
, &ref
->old_oid
))
451 info
->status
= PUSH_STATUS_FASTFORWARD
;
453 info
->status
= PUSH_STATUS_OUTOFDATE
;
455 free_refs(local_refs
);
460 static int get_push_ref_states_noquery(struct ref_states
*states
)
463 struct remote
*remote
= states
->remote
;
464 struct string_list_item
*item
;
465 struct push_info
*info
;
470 if (!remote
->push
.nr
) {
471 item
= string_list_append(&states
->push
, _("(matching)"));
472 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
473 info
->status
= PUSH_STATUS_NOTQUERIED
;
474 info
->dest
= xstrdup(item
->string
);
476 for (i
= 0; i
< remote
->push
.nr
; i
++) {
477 const struct refspec_item
*spec
= &remote
->push
.items
[i
];
479 item
= string_list_append(&states
->push
, _("(matching)"));
480 else if (strlen(spec
->src
))
481 item
= string_list_append(&states
->push
, spec
->src
);
483 item
= string_list_append(&states
->push
, _("(delete)"));
485 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
486 info
->forced
= spec
->force
;
487 info
->status
= PUSH_STATUS_NOTQUERIED
;
488 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
493 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
495 struct ref
*ref
, *matches
;
496 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
497 struct refspec_item refspec
= {
500 .src
= (char *) "refs/heads/*",
501 .dst
= (char *) "refs/heads/*",
504 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
505 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
507 for (ref
= matches
; ref
; ref
= ref
->next
)
508 string_list_append(&states
->heads
, abbrev_branch(ref
->name
));
510 free_refs(fetch_map
);
515 struct known_remote
{
516 struct known_remote
*next
;
517 struct remote
*remote
;
520 struct known_remotes
{
521 struct remote
*to_delete
;
522 struct known_remote
*list
;
525 static int add_known_remote(struct remote
*remote
, void *cb_data
)
527 struct known_remotes
*all
= cb_data
;
528 struct known_remote
*r
;
530 if (!strcmp(all
->to_delete
->name
, remote
->name
))
533 r
= xmalloc(sizeof(*r
));
540 struct branches_for_remote
{
541 struct remote
*remote
;
542 struct string_list
*branches
, *skipped
;
543 struct known_remotes
*keep
;
546 static int add_branch_for_removal(const char *refname
,
547 const char *referent UNUSED
,
548 const struct object_id
*oid UNUSED
,
549 int flags UNUSED
, void *cb_data
)
551 struct branches_for_remote
*branches
= cb_data
;
552 struct refspec_item refspec
;
553 struct known_remote
*kr
;
555 memset(&refspec
, 0, sizeof(refspec
));
556 refspec
.dst
= (char *)refname
;
557 if (remote_find_tracking(branches
->remote
, &refspec
))
561 /* don't delete a branch if another remote also uses it */
562 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
563 memset(&refspec
, 0, sizeof(refspec
));
564 refspec
.dst
= (char *)refname
;
565 if (!remote_find_tracking(kr
->remote
, &refspec
)) {
571 /* don't delete non-remote-tracking refs */
572 if (!starts_with(refname
, "refs/remotes/")) {
573 /* advise user how to delete local branches */
574 if (starts_with(refname
, "refs/heads/"))
575 string_list_append(branches
->skipped
,
576 abbrev_branch(refname
));
577 /* silently skip over other non-remote refs */
581 string_list_append(branches
->branches
, refname
);
587 const char *old_name
;
588 const char *new_name
;
589 struct string_list
*remote_branches
;
593 static int read_remote_branches(const char *refname
, const char *referent UNUSED
,
594 const struct object_id
*oid UNUSED
,
595 int flags UNUSED
, void *cb_data
)
597 struct rename_info
*rename
= cb_data
;
598 struct strbuf buf
= STRBUF_INIT
;
599 struct string_list_item
*item
;
603 strbuf_addf(&buf
, "refs/remotes/%s/", rename
->old_name
);
604 if (starts_with(refname
, buf
.buf
)) {
605 item
= string_list_append(rename
->remote_branches
, refname
);
606 symref
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
607 refname
, RESOLVE_REF_READING
,
609 if (symref
&& (flag
& REF_ISSYMREF
)) {
610 item
->util
= xstrdup(symref
);
611 rename
->symrefs_nr
++;
616 strbuf_release(&buf
);
621 static int migrate_file(struct remote
*remote
)
623 struct strbuf buf
= STRBUF_INIT
;
626 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
627 for (i
= 0; i
< remote
->url
.nr
; i
++)
628 git_config_set_multivar(buf
.buf
, remote
->url
.v
[i
], "^$", 0);
630 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
631 for (i
= 0; i
< remote
->push
.raw_nr
; i
++)
632 git_config_set_multivar(buf
.buf
, remote
->push
.raw
[i
], "^$", 0);
634 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
635 for (i
= 0; i
< remote
->fetch
.raw_nr
; i
++)
636 git_config_set_multivar(buf
.buf
, remote
->fetch
.raw
[i
], "^$", 0);
637 if (remote
->origin
== REMOTE_REMOTES
)
638 unlink_or_warn(git_path("remotes/%s", remote
->name
));
639 else if (remote
->origin
== REMOTE_BRANCHES
)
640 unlink_or_warn(git_path("branches/%s", remote
->name
));
641 strbuf_release(&buf
);
646 struct push_default_info
648 const char *old_name
;
649 enum config_scope scope
;
650 struct strbuf origin
;
654 static int config_read_push_default(const char *key
, const char *value
,
655 const struct config_context
*ctx
, void *cb
)
657 const struct key_value_info
*kvi
= ctx
->kvi
;
659 struct push_default_info
* info
= cb
;
660 if (strcmp(key
, "remote.pushdefault") ||
661 !value
|| strcmp(value
, info
->old_name
))
664 info
->scope
= kvi
->scope
;
665 strbuf_reset(&info
->origin
);
666 strbuf_addstr(&info
->origin
, config_origin_type_name(kvi
->origin_type
));
667 info
->linenr
= kvi
->linenr
;
672 static void handle_push_default(const char* old_name
, const char* new_name
)
674 struct push_default_info push_default
= {
675 .old_name
= old_name
,
676 .scope
= CONFIG_SCOPE_UNKNOWN
,
677 .origin
= STRBUF_INIT
,
680 git_config(config_read_push_default
, &push_default
);
681 if (push_default
.scope
>= CONFIG_SCOPE_COMMAND
)
683 else if (push_default
.scope
>= CONFIG_SCOPE_LOCAL
) {
684 int result
= git_config_set_gently("remote.pushDefault",
686 if (new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
687 die(_("could not set '%s'"), "remote.pushDefault");
688 else if (!new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
689 die(_("could not unset '%s'"), "remote.pushDefault");
690 } else if (push_default
.scope
>= CONFIG_SCOPE_SYSTEM
) {
692 warning(_("The %s configuration remote.pushDefault in:\n"
694 "now names the non-existent remote '%s'"),
695 config_scope_name(push_default
.scope
),
696 push_default
.origin
.buf
, push_default
.linenr
,
700 strbuf_release(&push_default
.origin
);
704 static int mv(int argc
, const char **argv
, const char *prefix
)
706 int show_progress
= isatty(2);
707 struct option options
[] = {
708 OPT_BOOL(0, "progress", &show_progress
, N_("force progress reporting")),
711 struct remote
*oldremote
, *newremote
;
712 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
,
713 old_remote_context
= STRBUF_INIT
;
714 struct string_list remote_branches
= STRING_LIST_INIT_DUP
;
715 struct rename_info rename
;
716 int i
, refs_renamed_nr
= 0, refspec_updated
= 0;
717 struct progress
*progress
= NULL
;
719 argc
= parse_options(argc
, argv
, prefix
, options
,
720 builtin_remote_rename_usage
, 0);
723 usage_with_options(builtin_remote_rename_usage
, options
);
725 rename
.old_name
= argv
[0];
726 rename
.new_name
= argv
[1];
727 rename
.remote_branches
= &remote_branches
;
728 rename
.symrefs_nr
= 0;
730 oldremote
= remote_get(rename
.old_name
);
731 if (!remote_is_configured(oldremote
, 1)) {
732 error(_("No such remote: '%s'"), rename
.old_name
);
736 if (!strcmp(rename
.old_name
, rename
.new_name
) && oldremote
->origin
!= REMOTE_CONFIG
)
737 return migrate_file(oldremote
);
739 newremote
= remote_get(rename
.new_name
);
740 if (remote_is_configured(newremote
, 1)) {
741 error(_("remote %s already exists."), rename
.new_name
);
745 if (!valid_remote_name(rename
.new_name
))
746 die(_("'%s' is not a valid remote name"), rename
.new_name
);
748 strbuf_addf(&buf
, "remote.%s", rename
.old_name
);
749 strbuf_addf(&buf2
, "remote.%s", rename
.new_name
);
750 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
751 return error(_("Could not rename config section '%s' to '%s'"),
754 if (oldremote
->fetch
.raw_nr
) {
756 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new_name
);
757 git_config_set_multivar(buf
.buf
, NULL
, NULL
, CONFIG_FLAGS_MULTI_REPLACE
);
758 strbuf_addf(&old_remote_context
, ":refs/remotes/%s/", rename
.old_name
);
759 for (i
= 0; i
< oldremote
->fetch
.raw_nr
; i
++) {
763 strbuf_addstr(&buf2
, oldremote
->fetch
.raw
[i
]);
764 ptr
= strstr(buf2
.buf
, old_remote_context
.buf
);
768 ptr
-buf2
.buf
+ strlen(":refs/remotes/"),
769 strlen(rename
.old_name
), rename
.new_name
,
770 strlen(rename
.new_name
));
772 warning(_("Not updating non-default fetch refspec\n"
774 "\tPlease update the configuration manually if necessary."),
777 git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0);
782 for (i
= 0; i
< branch_list
.nr
; i
++) {
783 struct string_list_item
*item
= branch_list
.items
+ i
;
784 struct branch_info
*info
= item
->util
;
785 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old_name
)) {
787 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
788 git_config_set(buf
.buf
, rename
.new_name
);
790 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, rename
.old_name
)) {
792 strbuf_addf(&buf
, "branch.%s.pushRemote", item
->string
);
793 git_config_set(buf
.buf
, rename
.new_name
);
797 if (!refspec_updated
)
801 * First remove symrefs, then rename the rest, finally create
804 refs_for_each_ref(get_main_ref_store(the_repository
),
805 read_remote_branches
, &rename
);
808 * Count symrefs twice, since "renaming" them is done by
809 * deleting and recreating them in two separate passes.
811 progress
= start_progress(_("Renaming remote references"),
812 rename
.remote_branches
->nr
+ rename
.symrefs_nr
);
814 for (i
= 0; i
< remote_branches
.nr
; i
++) {
815 struct string_list_item
*item
= remote_branches
.items
+ i
;
816 struct strbuf referent
= STRBUF_INIT
;
818 if (refs_read_symbolic_ref(get_main_ref_store(the_repository
), item
->string
,
821 if (refs_delete_ref(get_main_ref_store(the_repository
), NULL
, item
->string
, NULL
, REF_NO_DEREF
))
822 die(_("deleting '%s' failed"), item
->string
);
824 strbuf_release(&referent
);
825 display_progress(progress
, ++refs_renamed_nr
);
827 for (i
= 0; i
< remote_branches
.nr
; i
++) {
828 struct string_list_item
*item
= remote_branches
.items
+ i
;
833 strbuf_addstr(&buf
, item
->string
);
834 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
835 rename
.new_name
, strlen(rename
.new_name
));
837 strbuf_addf(&buf2
, "remote: renamed %s to %s",
838 item
->string
, buf
.buf
);
839 if (refs_rename_ref(get_main_ref_store(the_repository
), item
->string
, buf
.buf
, buf2
.buf
))
840 die(_("renaming '%s' failed"), item
->string
);
841 display_progress(progress
, ++refs_renamed_nr
);
843 for (i
= 0; i
< remote_branches
.nr
; i
++) {
844 struct string_list_item
*item
= remote_branches
.items
+ i
;
849 strbuf_addstr(&buf
, item
->string
);
850 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
851 rename
.new_name
, strlen(rename
.new_name
));
853 strbuf_addstr(&buf2
, item
->util
);
854 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old_name
),
855 rename
.new_name
, strlen(rename
.new_name
));
857 strbuf_addf(&buf3
, "remote: renamed %s to %s",
858 item
->string
, buf
.buf
);
859 if (refs_update_symref(get_main_ref_store(the_repository
), buf
.buf
, buf2
.buf
, buf3
.buf
))
860 die(_("creating '%s' failed"), buf
.buf
);
861 display_progress(progress
, ++refs_renamed_nr
);
863 stop_progress(&progress
);
865 handle_push_default(rename
.old_name
, rename
.new_name
);
868 string_list_clear(&remote_branches
, 1);
869 strbuf_release(&old_remote_context
);
870 strbuf_release(&buf
);
871 strbuf_release(&buf2
);
872 strbuf_release(&buf3
);
876 static int rm(int argc
, const char **argv
, const char *prefix
)
878 struct option options
[] = {
881 struct remote
*remote
;
882 struct strbuf buf
= STRBUF_INIT
;
883 struct known_remotes known_remotes
= { NULL
, NULL
};
884 struct string_list branches
= STRING_LIST_INIT_DUP
;
885 struct string_list skipped
= STRING_LIST_INIT_DUP
;
886 struct branches_for_remote cb_data
;
889 memset(&cb_data
, 0, sizeof(cb_data
));
890 cb_data
.branches
= &branches
;
891 cb_data
.skipped
= &skipped
;
892 cb_data
.keep
= &known_remotes
;
894 argc
= parse_options(argc
, argv
, prefix
, options
,
895 builtin_remote_rm_usage
, 0);
897 usage_with_options(builtin_remote_rm_usage
, options
);
899 remote
= remote_get(argv
[0]);
900 if (!remote_is_configured(remote
, 1)) {
901 error(_("No such remote: '%s'"), argv
[0]);
905 known_remotes
.to_delete
= remote
;
906 for_each_remote(add_known_remote
, &known_remotes
);
909 for (i
= 0; i
< branch_list
.nr
; i
++) {
910 struct string_list_item
*item
= branch_list
.items
+ i
;
911 struct branch_info
*info
= item
->util
;
912 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
913 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
914 for (k
= keys
; *k
; k
++) {
916 strbuf_addf(&buf
, "branch.%s.%s",
918 result
= git_config_set_gently(buf
.buf
, NULL
);
919 if (result
&& result
!= CONFIG_NOTHING_SET
)
920 die(_("could not unset '%s'"), buf
.buf
);
923 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, remote
->name
)) {
925 strbuf_addf(&buf
, "branch.%s.pushremote", item
->string
);
926 result
= git_config_set_gently(buf
.buf
, NULL
);
927 if (result
&& result
!= CONFIG_NOTHING_SET
)
928 die(_("could not unset '%s'"), buf
.buf
);
933 * We cannot just pass a function to for_each_ref() which deletes
934 * the branches one by one, since for_each_ref() relies on cached
935 * refs, which are invalidated when deleting a branch.
937 cb_data
.remote
= remote
;
938 result
= refs_for_each_ref(get_main_ref_store(the_repository
),
939 add_branch_for_removal
, &cb_data
);
940 strbuf_release(&buf
);
943 result
= refs_delete_refs(get_main_ref_store(the_repository
),
944 "remote: remove", &branches
,
946 string_list_clear(&branches
, 0);
950 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
951 "to delete it, use:",
952 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
953 "to delete them, use:",
955 for (i
= 0; i
< skipped
.nr
; i
++)
956 fprintf(stderr
, " git branch -d %s\n",
957 skipped
.items
[i
].string
);
959 string_list_clear(&skipped
, 0);
962 strbuf_addf(&buf
, "remote.%s", remote
->name
);
963 if (git_config_rename_section(buf
.buf
, NULL
) < 1) {
964 result
= error(_("Could not remove config section '%s'"), buf
.buf
);
968 handle_push_default(remote
->name
, NULL
);
972 for (struct known_remote
*r
= known_remotes
.list
; r
;) {
973 struct known_remote
*next
= r
->next
;
977 strbuf_release(&buf
);
981 static void clear_push_info(void *util
, const char *string UNUSED
)
983 struct push_info
*info
= util
;
988 static void free_remote_ref_states(struct ref_states
*states
)
990 string_list_clear(&states
->new_refs
, 0);
991 string_list_clear(&states
->skipped
, 0);
992 string_list_clear(&states
->stale
, 1);
993 string_list_clear(&states
->tracked
, 0);
994 string_list_clear(&states
->heads
, 0);
995 string_list_clear_func(&states
->push
, clear_push_info
);
998 static int append_ref_to_tracked_list(const char *refname
,
999 const char *referent UNUSED
,
1000 const struct object_id
*oid UNUSED
,
1001 int flags
, void *cb_data
)
1003 struct ref_states
*states
= cb_data
;
1004 struct refspec_item refspec
;
1006 if (flags
& REF_ISSYMREF
)
1009 memset(&refspec
, 0, sizeof(refspec
));
1010 refspec
.dst
= (char *)refname
;
1011 if (!remote_find_tracking(states
->remote
, &refspec
)) {
1012 string_list_append(&states
->tracked
, abbrev_branch(refspec
.src
));
1019 static int get_remote_ref_states(const char *name
,
1020 struct ref_states
*states
,
1023 states
->remote
= remote_get(name
);
1024 if (!states
->remote
)
1025 return error(_("No such remote: '%s'"), name
);
1030 struct transport
*transport
;
1031 const struct ref
*remote_refs
;
1033 transport
= transport_get(states
->remote
, states
->remote
->url
.v
[0]);
1034 remote_refs
= transport_get_remote_refs(transport
, NULL
);
1036 states
->queried
= 1;
1037 if (query
& GET_REF_STATES
)
1038 get_ref_states(remote_refs
, states
);
1039 if (query
& GET_HEAD_NAMES
)
1040 get_head_names(remote_refs
, states
);
1041 if (query
& GET_PUSH_REF_STATES
)
1042 get_push_ref_states(remote_refs
, states
);
1043 transport_disconnect(transport
);
1045 refs_for_each_ref(get_main_ref_store(the_repository
),
1046 append_ref_to_tracked_list
, states
);
1047 string_list_sort(&states
->tracked
);
1048 get_push_ref_states_noquery(states
);
1055 struct string_list list
;
1056 struct ref_states states
;
1061 #define SHOW_INFO_INIT { \
1062 .list = STRING_LIST_INIT_DUP, \
1063 .states = REF_STATES_INIT, \
1066 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
1068 struct show_info
*info
= cb_data
;
1069 int n
= strlen(item
->string
);
1070 if (n
> info
->width
)
1072 string_list_insert(&info
->list
, item
->string
);
1076 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
1078 struct show_info
*info
= cb_data
;
1079 struct ref_states
*states
= &info
->states
;
1080 const char *name
= item
->string
;
1082 if (states
->queried
) {
1083 const char *fmt
= "%s";
1084 const char *arg
= "";
1085 if (string_list_has_string(&states
->new_refs
, name
)) {
1086 fmt
= _(" new (next fetch will store in remotes/%s)");
1087 arg
= states
->remote
->name
;
1088 } else if (string_list_has_string(&states
->tracked
, name
))
1089 arg
= _(" tracked");
1090 else if (string_list_has_string(&states
->skipped
, name
))
1091 arg
= _(" skipped");
1092 else if (string_list_has_string(&states
->stale
, name
))
1093 arg
= _(" stale (use 'git remote prune' to remove)");
1096 printf(" %-*s", info
->width
, name
);
1100 printf(" %s\n", name
);
1105 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
1107 struct show_info
*show_info
= cb_data
;
1108 struct ref_states
*states
= &show_info
->states
;
1109 struct branch_info
*branch_info
= branch_item
->util
;
1110 struct string_list_item
*item
;
1113 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
1114 strcmp(states
->remote
->name
, branch_info
->remote_name
))
1116 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
1117 show_info
->width
= n
;
1118 if (branch_info
->rebase
>= REBASE_TRUE
)
1119 show_info
->any_rebase
= 1;
1121 item
= string_list_insert(&show_info
->list
, branch_item
->string
);
1122 item
->util
= branch_info
;
1127 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
1129 struct show_info
*show_info
= cb_data
;
1130 struct branch_info
*branch_info
= item
->util
;
1131 struct string_list
*merge
= &branch_info
->merge
;
1132 int width
= show_info
->width
+ 4;
1135 if (branch_info
->rebase
>= REBASE_TRUE
&& branch_info
->merge
.nr
> 1) {
1136 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1141 printf(" %-*s ", show_info
->width
, item
->string
);
1142 if (branch_info
->rebase
>= REBASE_TRUE
) {
1144 if (branch_info
->rebase
== REBASE_INTERACTIVE
)
1145 msg
= _("rebases interactively onto remote %s");
1146 else if (branch_info
->rebase
== REBASE_MERGES
)
1147 msg
= _("rebases interactively (with merges) onto "
1150 msg
= _("rebases onto remote %s");
1151 printf_ln(msg
, merge
->items
[0].string
);
1153 } else if (show_info
->any_rebase
) {
1154 printf_ln(_(" merges with remote %s"), merge
->items
[0].string
);
1157 printf_ln(_("merges with remote %s"), merge
->items
[0].string
);
1159 for (i
= 1; i
< merge
->nr
; i
++)
1160 printf(_("%-*s and with remote %s\n"), width
, "",
1161 merge
->items
[i
].string
);
1166 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
1168 struct show_info
*show_info
= cb_data
;
1169 struct push_info
*push_info
= push_item
->util
;
1170 struct string_list_item
*item
;
1172 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
1173 show_info
->width
= n
;
1174 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
1175 show_info
->width2
= n
;
1176 item
= string_list_append(&show_info
->list
, push_item
->string
);
1177 item
->util
= push_item
->util
;
1182 * Sorting comparison for a string list that has push_info
1183 * structs in its util field
1185 static int cmp_string_with_push(const void *va
, const void *vb
)
1187 const struct string_list_item
*a
= va
;
1188 const struct string_list_item
*b
= vb
;
1189 const struct push_info
*a_push
= a
->util
;
1190 const struct push_info
*b_push
= b
->util
;
1191 int cmp
= strcmp(a
->string
, b
->string
);
1192 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
1195 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
1197 struct show_info
*show_info
= cb_data
;
1198 struct push_info
*push_info
= item
->util
;
1199 const char *src
= item
->string
, *status
= NULL
;
1201 switch (push_info
->status
) {
1202 case PUSH_STATUS_CREATE
:
1203 status
= _("create");
1205 case PUSH_STATUS_DELETE
:
1206 status
= _("delete");
1209 case PUSH_STATUS_UPTODATE
:
1210 status
= _("up to date");
1212 case PUSH_STATUS_FASTFORWARD
:
1213 status
= _("fast-forwardable");
1215 case PUSH_STATUS_OUTOFDATE
:
1216 status
= _("local out of date");
1218 case PUSH_STATUS_NOTQUERIED
:
1222 if (push_info
->forced
)
1223 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info
->width
, src
,
1224 show_info
->width2
, push_info
->dest
, status
);
1226 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info
->width
, src
,
1227 show_info
->width2
, push_info
->dest
, status
);
1229 if (push_info
->forced
)
1230 printf_ln(_(" %-*s forces to %s"), show_info
->width
, src
,
1233 printf_ln(_(" %-*s pushes to %s"), show_info
->width
, src
,
1239 static int get_one_entry(struct remote
*remote
, void *priv
)
1241 struct string_list
*list
= priv
;
1242 struct strbuf remote_info_buf
= STRBUF_INIT
;
1246 if (remote
->url
.nr
> 0) {
1247 struct strbuf promisor_config
= STRBUF_INIT
;
1248 const char *partial_clone_filter
= NULL
;
1250 strbuf_addf(&promisor_config
, "remote.%s.partialclonefilter", remote
->name
);
1251 strbuf_addf(&remote_info_buf
, "%s (fetch)", remote
->url
.v
[0]);
1252 if (!git_config_get_string_tmp(promisor_config
.buf
, &partial_clone_filter
))
1253 strbuf_addf(&remote_info_buf
, " [%s]", partial_clone_filter
);
1255 strbuf_release(&promisor_config
);
1256 string_list_append(list
, remote
->name
)->util
=
1257 strbuf_detach(&remote_info_buf
, NULL
);
1259 string_list_append(list
, remote
->name
)->util
= NULL
;
1260 url
= push_url_of_remote(remote
);
1261 for (i
= 0; i
< url
->nr
; i
++)
1263 strbuf_addf(&remote_info_buf
, "%s (push)", url
->v
[i
]);
1264 string_list_append(list
, remote
->name
)->util
=
1265 strbuf_detach(&remote_info_buf
, NULL
);
1271 static int show_all(void)
1273 struct string_list list
= STRING_LIST_INIT_DUP
;
1276 result
= for_each_remote(get_one_entry
, &list
);
1281 string_list_sort(&list
);
1282 for (i
= 0; i
< list
.nr
; i
++) {
1283 struct string_list_item
*item
= list
.items
+ i
;
1285 printf("%s\t%s\n", item
->string
,
1286 item
->util
? (const char *)item
->util
: "");
1288 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1290 printf("%s\n", item
->string
);
1294 string_list_clear(&list
, 1);
1298 static int show(int argc
, const char **argv
, const char *prefix
)
1300 int no_query
= 0, result
= 0, query_flag
= 0;
1301 struct option options
[] = {
1302 OPT_BOOL('n', NULL
, &no_query
, N_("do not query remotes")),
1305 struct show_info info
= SHOW_INFO_INIT
;
1307 argc
= parse_options(argc
, argv
, prefix
, options
,
1308 builtin_remote_show_usage
,
1315 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1317 for (; argc
; argc
--, argv
++) {
1321 get_remote_ref_states(*argv
, &info
.states
, query_flag
);
1323 printf_ln(_("* remote %s"), *argv
);
1324 printf_ln(_(" Fetch URL: %s"), info
.states
.remote
->url
.v
[0]);
1325 url
= push_url_of_remote(info
.states
.remote
);
1326 for (i
= 0; i
< url
->nr
; i
++)
1328 * TRANSLATORS: the colon ':' should align
1329 * with the one in " Fetch URL: %s"
1332 printf_ln(_(" Push URL: %s"), url
->v
[i
]);
1334 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1336 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1337 else if (!info
.states
.heads
.nr
)
1338 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1339 else if (info
.states
.heads
.nr
== 1)
1340 printf_ln(_(" HEAD branch: %s"), info
.states
.heads
.items
[0].string
);
1342 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1343 " may be one of the following):\n"));
1344 for (i
= 0; i
< info
.states
.heads
.nr
; i
++)
1345 printf(" %s\n", info
.states
.heads
.items
[i
].string
);
1348 /* remote branch info */
1350 for_each_string_list(&info
.states
.new_refs
, add_remote_to_show_info
, &info
);
1351 for_each_string_list(&info
.states
.skipped
, add_remote_to_show_info
, &info
);
1352 for_each_string_list(&info
.states
.tracked
, add_remote_to_show_info
, &info
);
1353 for_each_string_list(&info
.states
.stale
, add_remote_to_show_info
, &info
);
1355 printf_ln(Q_(" Remote branch:%s",
1356 " Remote branches:%s",
1358 no_query
? _(" (status not queried)") : "");
1359 for_each_string_list(&info
.list
, show_remote_info_item
, &info
);
1360 string_list_clear(&info
.list
, 0);
1364 info
.any_rebase
= 0;
1365 for_each_string_list(&branch_list
, add_local_to_show_info
, &info
);
1367 printf_ln(Q_(" Local branch configured for 'git pull':",
1368 " Local branches configured for 'git pull':",
1370 for_each_string_list(&info
.list
, show_local_info_item
, &info
);
1371 string_list_clear(&info
.list
, 0);
1374 if (info
.states
.remote
->mirror
)
1375 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1377 info
.width
= info
.width2
= 0;
1378 for_each_string_list(&info
.states
.push
, add_push_to_show_info
, &info
);
1379 QSORT(info
.list
.items
, info
.list
.nr
, cmp_string_with_push
);
1381 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1382 " Local refs configured for 'git push'%s:",
1384 no_query
? _(" (status not queried)") : "");
1385 for_each_string_list(&info
.list
, show_push_info_item
, &info
);
1386 string_list_clear(&info
.list
, 0);
1388 free_remote_ref_states(&info
.states
);
1394 static int set_head(int argc
, const char **argv
, const char *prefix
)
1396 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1397 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1398 char *head_name
= NULL
;
1400 struct option options
[] = {
1401 OPT_BOOL('a', "auto", &opt_a
,
1402 N_("set refs/remotes/<name>/HEAD according to remote")),
1403 OPT_BOOL('d', "delete", &opt_d
,
1404 N_("delete refs/remotes/<name>/HEAD")),
1407 argc
= parse_options(argc
, argv
, prefix
, options
,
1408 builtin_remote_sethead_usage
, 0);
1410 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1412 if (!opt_a
&& !opt_d
&& argc
== 2) {
1413 head_name
= xstrdup(argv
[1]);
1414 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1415 struct ref_states states
= REF_STATES_INIT
;
1416 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1417 if (!states
.heads
.nr
)
1418 result
|= error(_("Cannot determine remote HEAD"));
1419 else if (states
.heads
.nr
> 1) {
1420 result
|= error(_("Multiple remote HEAD branches. "
1421 "Please choose one explicitly with:"));
1422 for (i
= 0; i
< states
.heads
.nr
; i
++)
1423 fprintf(stderr
, " git remote set-head %s %s\n",
1424 argv
[0], states
.heads
.items
[i
].string
);
1426 head_name
= xstrdup(states
.heads
.items
[0].string
);
1427 free_remote_ref_states(&states
);
1428 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1429 if (refs_delete_ref(get_main_ref_store(the_repository
), NULL
, buf
.buf
, NULL
, REF_NO_DEREF
))
1430 result
|= error(_("Could not delete %s"), buf
.buf
);
1432 usage_with_options(builtin_remote_sethead_usage
, options
);
1435 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1436 /* make sure it's valid */
1437 if (!refs_ref_exists(get_main_ref_store(the_repository
), buf2
.buf
))
1438 result
|= error(_("Not a valid ref: %s"), buf2
.buf
);
1439 else if (refs_update_symref(get_main_ref_store(the_repository
), buf
.buf
, buf2
.buf
, "remote set-head"))
1440 result
|= error(_("Could not setup %s"), buf
.buf
);
1442 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1446 strbuf_release(&buf
);
1447 strbuf_release(&buf2
);
1451 static int prune_remote(const char *remote
, int dry_run
)
1454 struct ref_states states
= REF_STATES_INIT
;
1455 struct string_list refs_to_prune
= STRING_LIST_INIT_NODUP
;
1456 struct string_list_item
*item
;
1457 const char *dangling_msg
= dry_run
1458 ? _(" %s will become dangling!")
1459 : _(" %s has become dangling!");
1461 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1463 if (!states
.stale
.nr
) {
1464 free_remote_ref_states(&states
);
1468 printf_ln(_("Pruning %s"), remote
);
1469 printf_ln(_("URL: %s"), states
.remote
->url
.v
[0]);
1471 for_each_string_list_item(item
, &states
.stale
)
1472 string_list_append(&refs_to_prune
, item
->util
);
1473 string_list_sort(&refs_to_prune
);
1476 result
|= refs_delete_refs(get_main_ref_store(the_repository
),
1477 "remote: prune", &refs_to_prune
, 0);
1479 for_each_string_list_item(item
, &states
.stale
) {
1480 const char *refname
= item
->util
;
1483 printf_ln(_(" * [would prune] %s"),
1484 abbrev_ref(refname
, "refs/remotes/"));
1486 printf_ln(_(" * [pruned] %s"),
1487 abbrev_ref(refname
, "refs/remotes/"));
1490 refs_warn_dangling_symrefs(get_main_ref_store(the_repository
),
1491 stdout
, dangling_msg
, &refs_to_prune
);
1493 string_list_clear(&refs_to_prune
, 0);
1494 free_remote_ref_states(&states
);
1498 static int prune(int argc
, const char **argv
, const char *prefix
)
1500 int dry_run
= 0, result
= 0;
1501 struct option options
[] = {
1502 OPT__DRY_RUN(&dry_run
, N_("dry run")),
1506 argc
= parse_options(argc
, argv
, prefix
, options
,
1507 builtin_remote_prune_usage
, 0);
1510 usage_with_options(builtin_remote_prune_usage
, options
);
1512 for (; argc
; argc
--, argv
++)
1513 result
|= prune_remote(*argv
, dry_run
);
1518 static int get_remote_default(const char *key
, const char *value UNUSED
,
1519 const struct config_context
*ctx UNUSED
,
1522 if (strcmp(key
, "remotes.default") == 0) {
1529 static int update(int argc
, const char **argv
, const char *prefix
)
1532 struct option options
[] = {
1533 OPT_BOOL('p', "prune", &prune
,
1534 N_("prune remotes after fetching")),
1537 struct child_process cmd
= CHILD_PROCESS_INIT
;
1538 int default_defined
= 0;
1540 argc
= parse_options(argc
, argv
, prefix
, options
,
1541 builtin_remote_update_usage
,
1542 PARSE_OPT_KEEP_ARGV0
);
1544 strvec_push(&cmd
.args
, "fetch");
1547 strvec_push(&cmd
.args
, prune
? "--prune" : "--no-prune");
1549 strvec_push(&cmd
.args
, "-v");
1550 strvec_push(&cmd
.args
, "--multiple");
1552 strvec_push(&cmd
.args
, "default");
1553 for (i
= 1; i
< argc
; i
++)
1554 strvec_push(&cmd
.args
, argv
[i
]);
1556 if (strcmp(cmd
.args
.v
[cmd
.args
.nr
-1], "default") == 0) {
1557 git_config(get_remote_default
, &default_defined
);
1558 if (!default_defined
) {
1559 strvec_pop(&cmd
.args
);
1560 strvec_push(&cmd
.args
, "--all");
1565 return run_command(&cmd
);
1568 static int remove_all_fetch_refspecs(const char *key
)
1570 return git_config_set_multivar_gently(key
, NULL
, NULL
,
1571 CONFIG_FLAGS_MULTI_REPLACE
);
1574 static void add_branches(struct remote
*remote
, const char **branches
,
1577 const char *remotename
= remote
->name
;
1578 int mirror
= remote
->mirror
;
1579 struct strbuf refspec
= STRBUF_INIT
;
1581 for (; *branches
; branches
++)
1582 add_branch(key
, *branches
, remotename
, mirror
, &refspec
);
1584 strbuf_release(&refspec
);
1587 static int set_remote_branches(const char *remotename
, const char **branches
,
1590 struct strbuf key
= STRBUF_INIT
;
1591 struct remote
*remote
;
1593 strbuf_addf(&key
, "remote.%s.fetch", remotename
);
1595 remote
= remote_get(remotename
);
1596 if (!remote_is_configured(remote
, 1)) {
1597 error(_("No such remote '%s'"), remotename
);
1601 if (!add_mode
&& remove_all_fetch_refspecs(key
.buf
)) {
1602 strbuf_release(&key
);
1605 add_branches(remote
, branches
, key
.buf
);
1607 strbuf_release(&key
);
1611 static int set_branches(int argc
, const char **argv
, const char *prefix
)
1614 struct option options
[] = {
1615 OPT_BOOL('\0', "add", &add_mode
, N_("add branch")),
1619 argc
= parse_options(argc
, argv
, prefix
, options
,
1620 builtin_remote_setbranches_usage
, 0);
1622 error(_("no remote specified"));
1623 usage_with_options(builtin_remote_setbranches_usage
, options
);
1627 return set_remote_branches(argv
[0], argv
+ 1, add_mode
);
1630 static int get_url(int argc
, const char **argv
, const char *prefix
)
1632 int i
, push_mode
= 0, all_mode
= 0;
1633 const char *remotename
= NULL
;
1634 struct remote
*remote
;
1636 struct option options
[] = {
1637 OPT_BOOL('\0', "push", &push_mode
,
1638 N_("query push URLs rather than fetch URLs")),
1639 OPT_BOOL('\0', "all", &all_mode
,
1640 N_("return all URLs")),
1643 argc
= parse_options(argc
, argv
, prefix
, options
,
1644 builtin_remote_geturl_usage
, 0);
1647 usage_with_options(builtin_remote_geturl_usage
, options
);
1649 remotename
= argv
[0];
1651 remote
= remote_get(remotename
);
1652 if (!remote_is_configured(remote
, 1)) {
1653 error(_("No such remote '%s'"), remotename
);
1657 url
= push_mode
? push_url_of_remote(remote
) : &remote
->url
;
1660 for (i
= 0; i
< url
->nr
; i
++)
1661 printf_ln("%s", url
->v
[i
]);
1663 printf_ln("%s", url
->v
[0]);
1669 static int set_url(int argc
, const char **argv
, const char *prefix
)
1671 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1672 int matches
= 0, negative_matches
= 0;
1673 const char *remotename
= NULL
;
1674 const char *newurl
= NULL
;
1675 const char *oldurl
= NULL
;
1676 struct remote
*remote
;
1678 struct strvec
*urlset
;
1679 struct strbuf name_buf
= STRBUF_INIT
;
1680 struct option options
[] = {
1681 OPT_BOOL('\0', "push", &push_mode
,
1682 N_("manipulate push URLs")),
1683 OPT_BOOL('\0', "add", &add_mode
,
1685 OPT_BOOL('\0', "delete", &delete_mode
,
1689 argc
= parse_options(argc
, argv
, prefix
, options
,
1690 builtin_remote_seturl_usage
,
1691 PARSE_OPT_KEEP_ARGV0
);
1693 if (add_mode
&& delete_mode
)
1694 die(_("--add --delete doesn't make sense"));
1696 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1697 usage_with_options(builtin_remote_seturl_usage
, options
);
1699 remotename
= argv
[1];
1707 remote
= remote_get(remotename
);
1708 if (!remote_is_configured(remote
, 1)) {
1709 error(_("No such remote '%s'"), remotename
);
1714 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1715 urlset
= &remote
->pushurl
;
1717 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1718 urlset
= &remote
->url
;
1721 /* Special cases that add new entry. */
1722 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1724 git_config_set_multivar(name_buf
.buf
, newurl
,
1727 git_config_set(name_buf
.buf
, newurl
);
1731 /* Old URL specified. Demand that one matches. */
1732 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1733 die(_("Invalid old URL pattern: %s"), oldurl
);
1735 for (i
= 0; i
< urlset
->nr
; i
++)
1736 if (!regexec(&old_regex
, urlset
->v
[i
], 0, NULL
, 0))
1740 if (!delete_mode
&& !matches
)
1741 die(_("No such URL found: %s"), oldurl
);
1742 if (delete_mode
&& !negative_matches
&& !push_mode
)
1743 die(_("Will not delete all non-push URLs"));
1745 regfree(&old_regex
);
1748 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1750 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
,
1751 CONFIG_FLAGS_MULTI_REPLACE
);
1753 strbuf_release(&name_buf
);
1757 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1759 parse_opt_subcommand_fn
*fn
= NULL
;
1760 struct option options
[] = {
1761 OPT__VERBOSE(&verbose
, N_("be verbose; must be placed before a subcommand")),
1762 OPT_SUBCOMMAND("add", &fn
, add
),
1763 OPT_SUBCOMMAND("rename", &fn
, mv
),
1764 OPT_SUBCOMMAND_F("rm", &fn
, rm
, PARSE_OPT_NOCOMPLETE
),
1765 OPT_SUBCOMMAND("remove", &fn
, rm
),
1766 OPT_SUBCOMMAND("set-head", &fn
, set_head
),
1767 OPT_SUBCOMMAND("set-branches", &fn
, set_branches
),
1768 OPT_SUBCOMMAND("get-url", &fn
, get_url
),
1769 OPT_SUBCOMMAND("set-url", &fn
, set_url
),
1770 OPT_SUBCOMMAND("show", &fn
, show
),
1771 OPT_SUBCOMMAND("prune", &fn
, prune
),
1772 OPT_SUBCOMMAND("update", &fn
, update
),
1776 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1777 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1780 return !!fn(argc
, argv
, prefix
);
1783 error(_("unknown subcommand: `%s'"), argv
[0]);
1784 usage_with_options(builtin_remote_usage
, options
);
1786 return !!show_all();