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_NODUP
;
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
))
295 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
);
343 static void read_branches(void)
347 git_config(config_read_branches
, NULL
);
351 struct remote
*remote
;
352 struct string_list new_refs
, skipped
, stale
, tracked
, heads
, push
;
356 #define REF_STATES_INIT { \
357 .new_refs = STRING_LIST_INIT_DUP, \
358 .skipped = STRING_LIST_INIT_DUP, \
359 .stale = STRING_LIST_INIT_DUP, \
360 .tracked = STRING_LIST_INIT_DUP, \
361 .heads = STRING_LIST_INIT_DUP, \
362 .push = STRING_LIST_INIT_DUP, \
365 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
367 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
368 struct ref
*ref
, *stale_refs
;
371 for (i
= 0; i
< states
->remote
->fetch
.nr
; i
++)
372 if (get_fetch_map(remote_refs
, &states
->remote
->fetch
.items
[i
], &tail
, 1))
373 die(_("Could not get fetch map for refspec %s"),
374 states
->remote
->fetch
.raw
[i
]);
376 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
377 if (omit_name_by_refspec(ref
->name
, &states
->remote
->fetch
))
378 string_list_append(&states
->skipped
, abbrev_branch(ref
->name
));
379 else if (!ref
->peer_ref
|| !refs_ref_exists(get_main_ref_store(the_repository
), ref
->peer_ref
->name
))
380 string_list_append(&states
->new_refs
, abbrev_branch(ref
->name
));
382 string_list_append(&states
->tracked
, abbrev_branch(ref
->name
));
384 stale_refs
= get_stale_heads(&states
->remote
->fetch
, fetch_map
);
385 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
386 struct string_list_item
*item
=
387 string_list_append(&states
->stale
, abbrev_branch(ref
->name
));
388 item
->util
= xstrdup(ref
->name
);
390 free_refs(stale_refs
);
391 free_refs(fetch_map
);
393 string_list_sort(&states
->new_refs
);
394 string_list_sort(&states
->skipped
);
395 string_list_sort(&states
->tracked
);
396 string_list_sort(&states
->stale
);
405 PUSH_STATUS_CREATE
= 0,
407 PUSH_STATUS_UPTODATE
,
408 PUSH_STATUS_FASTFORWARD
,
409 PUSH_STATUS_OUTOFDATE
,
410 PUSH_STATUS_NOTQUERIED
414 static int get_push_ref_states(const struct ref
*remote_refs
,
415 struct ref_states
*states
)
417 struct remote
*remote
= states
->remote
;
418 struct ref
*ref
, *local_refs
, *push_map
;
422 local_refs
= get_local_heads();
423 push_map
= copy_ref_list(remote_refs
);
425 match_push_refs(local_refs
, &push_map
, &remote
->push
, MATCH_REFS_NONE
);
427 for (ref
= push_map
; ref
; ref
= ref
->next
) {
428 struct string_list_item
*item
;
429 struct push_info
*info
;
433 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
435 item
= string_list_append(&states
->push
,
436 abbrev_branch(ref
->peer_ref
->name
));
437 item
->util
= xcalloc(1, sizeof(struct push_info
));
439 info
->forced
= ref
->force
;
440 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
442 if (is_null_oid(&ref
->new_oid
)) {
443 info
->status
= PUSH_STATUS_DELETE
;
444 } else if (oideq(&ref
->old_oid
, &ref
->new_oid
))
445 info
->status
= PUSH_STATUS_UPTODATE
;
446 else if (is_null_oid(&ref
->old_oid
))
447 info
->status
= PUSH_STATUS_CREATE
;
448 else if (repo_has_object_file(the_repository
, &ref
->old_oid
) &&
449 ref_newer(&ref
->new_oid
, &ref
->old_oid
))
450 info
->status
= PUSH_STATUS_FASTFORWARD
;
452 info
->status
= PUSH_STATUS_OUTOFDATE
;
454 free_refs(local_refs
);
459 static int get_push_ref_states_noquery(struct ref_states
*states
)
462 struct remote
*remote
= states
->remote
;
463 struct string_list_item
*item
;
464 struct push_info
*info
;
469 if (!remote
->push
.nr
) {
470 item
= string_list_append(&states
->push
, _("(matching)"));
471 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
472 info
->status
= PUSH_STATUS_NOTQUERIED
;
473 info
->dest
= xstrdup(item
->string
);
475 for (i
= 0; i
< remote
->push
.nr
; i
++) {
476 const struct refspec_item
*spec
= &remote
->push
.items
[i
];
478 item
= string_list_append(&states
->push
, _("(matching)"));
479 else if (strlen(spec
->src
))
480 item
= string_list_append(&states
->push
, spec
->src
);
482 item
= string_list_append(&states
->push
, _("(delete)"));
484 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
485 info
->forced
= spec
->force
;
486 info
->status
= PUSH_STATUS_NOTQUERIED
;
487 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
492 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
494 struct ref
*ref
, *matches
;
495 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
496 struct refspec_item refspec
;
498 memset(&refspec
, 0, sizeof(refspec
));
501 refspec
.src
= refspec
.dst
= "refs/heads/*";
502 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
503 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
505 for (ref
= matches
; ref
; ref
= ref
->next
)
506 string_list_append(&states
->heads
, abbrev_branch(ref
->name
));
508 free_refs(fetch_map
);
514 struct known_remote
{
515 struct known_remote
*next
;
516 struct remote
*remote
;
519 struct known_remotes
{
520 struct remote
*to_delete
;
521 struct known_remote
*list
;
524 static int add_known_remote(struct remote
*remote
, void *cb_data
)
526 struct known_remotes
*all
= cb_data
;
527 struct known_remote
*r
;
529 if (!strcmp(all
->to_delete
->name
, remote
->name
))
532 r
= xmalloc(sizeof(*r
));
539 struct branches_for_remote
{
540 struct remote
*remote
;
541 struct string_list
*branches
, *skipped
;
542 struct known_remotes
*keep
;
545 static int add_branch_for_removal(const char *refname
,
546 const struct object_id
*oid UNUSED
,
547 int flags UNUSED
, void *cb_data
)
549 struct branches_for_remote
*branches
= cb_data
;
550 struct refspec_item refspec
;
551 struct known_remote
*kr
;
553 memset(&refspec
, 0, sizeof(refspec
));
554 refspec
.dst
= (char *)refname
;
555 if (remote_find_tracking(branches
->remote
, &refspec
))
558 /* don't delete a branch if another remote also uses it */
559 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
560 memset(&refspec
, 0, sizeof(refspec
));
561 refspec
.dst
= (char *)refname
;
562 if (!remote_find_tracking(kr
->remote
, &refspec
))
566 /* don't delete non-remote-tracking refs */
567 if (!starts_with(refname
, "refs/remotes/")) {
568 /* advise user how to delete local branches */
569 if (starts_with(refname
, "refs/heads/"))
570 string_list_append(branches
->skipped
,
571 abbrev_branch(refname
));
572 /* silently skip over other non-remote refs */
576 string_list_append(branches
->branches
, refname
);
582 const char *old_name
;
583 const char *new_name
;
584 struct string_list
*remote_branches
;
588 static int read_remote_branches(const char *refname
,
589 const struct object_id
*oid UNUSED
,
590 int flags UNUSED
, void *cb_data
)
592 struct rename_info
*rename
= cb_data
;
593 struct strbuf buf
= STRBUF_INIT
;
594 struct string_list_item
*item
;
598 strbuf_addf(&buf
, "refs/remotes/%s/", rename
->old_name
);
599 if (starts_with(refname
, buf
.buf
)) {
600 item
= string_list_append(rename
->remote_branches
, refname
);
601 symref
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
602 refname
, RESOLVE_REF_READING
,
604 if (symref
&& (flag
& REF_ISSYMREF
)) {
605 item
->util
= xstrdup(symref
);
606 rename
->symrefs_nr
++;
611 strbuf_release(&buf
);
616 static int migrate_file(struct remote
*remote
)
618 struct strbuf buf
= STRBUF_INIT
;
621 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
622 for (i
= 0; i
< remote
->url_nr
; i
++)
623 git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0);
625 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
626 for (i
= 0; i
< remote
->push
.raw_nr
; i
++)
627 git_config_set_multivar(buf
.buf
, remote
->push
.raw
[i
], "^$", 0);
629 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
630 for (i
= 0; i
< remote
->fetch
.raw_nr
; i
++)
631 git_config_set_multivar(buf
.buf
, remote
->fetch
.raw
[i
], "^$", 0);
632 if (remote
->origin
== REMOTE_REMOTES
)
633 unlink_or_warn(git_path("remotes/%s", remote
->name
));
634 else if (remote
->origin
== REMOTE_BRANCHES
)
635 unlink_or_warn(git_path("branches/%s", remote
->name
));
636 strbuf_release(&buf
);
641 struct push_default_info
643 const char *old_name
;
644 enum config_scope scope
;
645 struct strbuf origin
;
649 static int config_read_push_default(const char *key
, const char *value
,
650 const struct config_context
*ctx
, void *cb
)
652 const struct key_value_info
*kvi
= ctx
->kvi
;
654 struct push_default_info
* info
= cb
;
655 if (strcmp(key
, "remote.pushdefault") ||
656 !value
|| strcmp(value
, info
->old_name
))
659 info
->scope
= kvi
->scope
;
660 strbuf_reset(&info
->origin
);
661 strbuf_addstr(&info
->origin
, config_origin_type_name(kvi
->origin_type
));
662 info
->linenr
= kvi
->linenr
;
667 static void handle_push_default(const char* old_name
, const char* new_name
)
669 struct push_default_info push_default
= {
670 old_name
, CONFIG_SCOPE_UNKNOWN
, STRBUF_INIT
, -1 };
671 git_config(config_read_push_default
, &push_default
);
672 if (push_default
.scope
>= CONFIG_SCOPE_COMMAND
)
674 else if (push_default
.scope
>= CONFIG_SCOPE_LOCAL
) {
675 int result
= git_config_set_gently("remote.pushDefault",
677 if (new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
678 die(_("could not set '%s'"), "remote.pushDefault");
679 else if (!new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
680 die(_("could not unset '%s'"), "remote.pushDefault");
681 } else if (push_default
.scope
>= CONFIG_SCOPE_SYSTEM
) {
683 warning(_("The %s configuration remote.pushDefault in:\n"
685 "now names the non-existent remote '%s'"),
686 config_scope_name(push_default
.scope
),
687 push_default
.origin
.buf
, push_default
.linenr
,
693 static int mv(int argc
, const char **argv
, const char *prefix
)
695 int show_progress
= isatty(2);
696 struct option options
[] = {
697 OPT_BOOL(0, "progress", &show_progress
, N_("force progress reporting")),
700 struct remote
*oldremote
, *newremote
;
701 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
,
702 old_remote_context
= STRBUF_INIT
;
703 struct string_list remote_branches
= STRING_LIST_INIT_DUP
;
704 struct rename_info rename
;
705 int i
, refs_renamed_nr
= 0, refspec_updated
= 0;
706 struct progress
*progress
= NULL
;
708 argc
= parse_options(argc
, argv
, prefix
, options
,
709 builtin_remote_rename_usage
, 0);
712 usage_with_options(builtin_remote_rename_usage
, options
);
714 rename
.old_name
= argv
[0];
715 rename
.new_name
= argv
[1];
716 rename
.remote_branches
= &remote_branches
;
717 rename
.symrefs_nr
= 0;
719 oldremote
= remote_get(rename
.old_name
);
720 if (!remote_is_configured(oldremote
, 1)) {
721 error(_("No such remote: '%s'"), rename
.old_name
);
725 if (!strcmp(rename
.old_name
, rename
.new_name
) && oldremote
->origin
!= REMOTE_CONFIG
)
726 return migrate_file(oldremote
);
728 newremote
= remote_get(rename
.new_name
);
729 if (remote_is_configured(newremote
, 1)) {
730 error(_("remote %s already exists."), rename
.new_name
);
734 if (!valid_remote_name(rename
.new_name
))
735 die(_("'%s' is not a valid remote name"), rename
.new_name
);
737 strbuf_addf(&buf
, "remote.%s", rename
.old_name
);
738 strbuf_addf(&buf2
, "remote.%s", rename
.new_name
);
739 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
740 return error(_("Could not rename config section '%s' to '%s'"),
743 if (oldremote
->fetch
.raw_nr
) {
745 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new_name
);
746 git_config_set_multivar(buf
.buf
, NULL
, NULL
, CONFIG_FLAGS_MULTI_REPLACE
);
747 strbuf_addf(&old_remote_context
, ":refs/remotes/%s/", rename
.old_name
);
748 for (i
= 0; i
< oldremote
->fetch
.raw_nr
; i
++) {
752 strbuf_addstr(&buf2
, oldremote
->fetch
.raw
[i
]);
753 ptr
= strstr(buf2
.buf
, old_remote_context
.buf
);
757 ptr
-buf2
.buf
+ strlen(":refs/remotes/"),
758 strlen(rename
.old_name
), rename
.new_name
,
759 strlen(rename
.new_name
));
761 warning(_("Not updating non-default fetch refspec\n"
763 "\tPlease update the configuration manually if necessary."),
766 git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0);
771 for (i
= 0; i
< branch_list
.nr
; i
++) {
772 struct string_list_item
*item
= branch_list
.items
+ i
;
773 struct branch_info
*info
= item
->util
;
774 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old_name
)) {
776 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
777 git_config_set(buf
.buf
, rename
.new_name
);
779 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, rename
.old_name
)) {
781 strbuf_addf(&buf
, "branch.%s.pushRemote", item
->string
);
782 git_config_set(buf
.buf
, rename
.new_name
);
786 if (!refspec_updated
)
790 * First remove symrefs, then rename the rest, finally create
793 refs_for_each_ref(get_main_ref_store(the_repository
),
794 read_remote_branches
, &rename
);
797 * Count symrefs twice, since "renaming" them is done by
798 * deleting and recreating them in two separate passes.
800 progress
= start_progress(_("Renaming remote references"),
801 rename
.remote_branches
->nr
+ rename
.symrefs_nr
);
803 for (i
= 0; i
< remote_branches
.nr
; i
++) {
804 struct string_list_item
*item
= remote_branches
.items
+ i
;
805 struct strbuf referent
= STRBUF_INIT
;
807 if (refs_read_symbolic_ref(get_main_ref_store(the_repository
), item
->string
,
810 if (refs_delete_ref(get_main_ref_store(the_repository
), NULL
, item
->string
, NULL
, REF_NO_DEREF
))
811 die(_("deleting '%s' failed"), item
->string
);
813 strbuf_release(&referent
);
814 display_progress(progress
, ++refs_renamed_nr
);
816 for (i
= 0; i
< remote_branches
.nr
; i
++) {
817 struct string_list_item
*item
= remote_branches
.items
+ i
;
822 strbuf_addstr(&buf
, item
->string
);
823 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
824 rename
.new_name
, strlen(rename
.new_name
));
826 strbuf_addf(&buf2
, "remote: renamed %s to %s",
827 item
->string
, buf
.buf
);
828 if (refs_rename_ref(get_main_ref_store(the_repository
), item
->string
, buf
.buf
, buf2
.buf
))
829 die(_("renaming '%s' failed"), item
->string
);
830 display_progress(progress
, ++refs_renamed_nr
);
832 for (i
= 0; i
< remote_branches
.nr
; i
++) {
833 struct string_list_item
*item
= remote_branches
.items
+ i
;
838 strbuf_addstr(&buf
, item
->string
);
839 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
840 rename
.new_name
, strlen(rename
.new_name
));
842 strbuf_addstr(&buf2
, item
->util
);
843 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old_name
),
844 rename
.new_name
, strlen(rename
.new_name
));
846 strbuf_addf(&buf3
, "remote: renamed %s to %s",
847 item
->string
, buf
.buf
);
848 if (refs_update_symref(get_main_ref_store(the_repository
), buf
.buf
, buf2
.buf
, buf3
.buf
))
849 die(_("creating '%s' failed"), buf
.buf
);
850 display_progress(progress
, ++refs_renamed_nr
);
852 stop_progress(&progress
);
853 string_list_clear(&remote_branches
, 1);
855 handle_push_default(rename
.old_name
, rename
.new_name
);
860 static int rm(int argc
, const char **argv
, const char *prefix
)
862 struct option options
[] = {
865 struct remote
*remote
;
866 struct strbuf buf
= STRBUF_INIT
;
867 struct known_remotes known_remotes
= { NULL
, NULL
};
868 struct string_list branches
= STRING_LIST_INIT_DUP
;
869 struct string_list skipped
= STRING_LIST_INIT_DUP
;
870 struct branches_for_remote cb_data
;
873 memset(&cb_data
, 0, sizeof(cb_data
));
874 cb_data
.branches
= &branches
;
875 cb_data
.skipped
= &skipped
;
876 cb_data
.keep
= &known_remotes
;
878 argc
= parse_options(argc
, argv
, prefix
, options
,
879 builtin_remote_rm_usage
, 0);
881 usage_with_options(builtin_remote_rm_usage
, options
);
883 remote
= remote_get(argv
[0]);
884 if (!remote_is_configured(remote
, 1)) {
885 error(_("No such remote: '%s'"), argv
[0]);
889 known_remotes
.to_delete
= remote
;
890 for_each_remote(add_known_remote
, &known_remotes
);
893 for (i
= 0; i
< branch_list
.nr
; i
++) {
894 struct string_list_item
*item
= branch_list
.items
+ i
;
895 struct branch_info
*info
= item
->util
;
896 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
897 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
898 for (k
= keys
; *k
; k
++) {
900 strbuf_addf(&buf
, "branch.%s.%s",
902 result
= git_config_set_gently(buf
.buf
, NULL
);
903 if (result
&& result
!= CONFIG_NOTHING_SET
)
904 die(_("could not unset '%s'"), buf
.buf
);
907 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, remote
->name
)) {
909 strbuf_addf(&buf
, "branch.%s.pushremote", item
->string
);
910 result
= git_config_set_gently(buf
.buf
, NULL
);
911 if (result
&& result
!= CONFIG_NOTHING_SET
)
912 die(_("could not unset '%s'"), buf
.buf
);
917 * We cannot just pass a function to for_each_ref() which deletes
918 * the branches one by one, since for_each_ref() relies on cached
919 * refs, which are invalidated when deleting a branch.
921 cb_data
.remote
= remote
;
922 result
= refs_for_each_ref(get_main_ref_store(the_repository
),
923 add_branch_for_removal
, &cb_data
);
924 strbuf_release(&buf
);
927 result
= refs_delete_refs(get_main_ref_store(the_repository
),
928 "remote: remove", &branches
,
930 string_list_clear(&branches
, 0);
934 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
935 "to delete it, use:",
936 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
937 "to delete them, use:",
939 for (i
= 0; i
< skipped
.nr
; i
++)
940 fprintf(stderr
, " git branch -d %s\n",
941 skipped
.items
[i
].string
);
943 string_list_clear(&skipped
, 0);
946 strbuf_addf(&buf
, "remote.%s", remote
->name
);
947 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
948 return error(_("Could not remove config section '%s'"), buf
.buf
);
950 handle_push_default(remote
->name
, NULL
);
956 static void clear_push_info(void *util
, const char *string UNUSED
)
958 struct push_info
*info
= util
;
963 static void free_remote_ref_states(struct ref_states
*states
)
965 string_list_clear(&states
->new_refs
, 0);
966 string_list_clear(&states
->skipped
, 0);
967 string_list_clear(&states
->stale
, 1);
968 string_list_clear(&states
->tracked
, 0);
969 string_list_clear(&states
->heads
, 0);
970 string_list_clear_func(&states
->push
, clear_push_info
);
973 static int append_ref_to_tracked_list(const char *refname
,
974 const struct object_id
*oid UNUSED
,
975 int flags
, void *cb_data
)
977 struct ref_states
*states
= cb_data
;
978 struct refspec_item refspec
;
980 if (flags
& REF_ISSYMREF
)
983 memset(&refspec
, 0, sizeof(refspec
));
984 refspec
.dst
= (char *)refname
;
985 if (!remote_find_tracking(states
->remote
, &refspec
))
986 string_list_append(&states
->tracked
, abbrev_branch(refspec
.src
));
991 static int get_remote_ref_states(const char *name
,
992 struct ref_states
*states
,
995 states
->remote
= remote_get(name
);
997 return error(_("No such remote: '%s'"), name
);
1002 struct transport
*transport
;
1003 const struct ref
*remote_refs
;
1005 transport
= transport_get(states
->remote
, states
->remote
->url_nr
> 0 ?
1006 states
->remote
->url
[0] : NULL
);
1007 remote_refs
= transport_get_remote_refs(transport
, NULL
);
1009 states
->queried
= 1;
1010 if (query
& GET_REF_STATES
)
1011 get_ref_states(remote_refs
, states
);
1012 if (query
& GET_HEAD_NAMES
)
1013 get_head_names(remote_refs
, states
);
1014 if (query
& GET_PUSH_REF_STATES
)
1015 get_push_ref_states(remote_refs
, states
);
1016 transport_disconnect(transport
);
1018 refs_for_each_ref(get_main_ref_store(the_repository
),
1019 append_ref_to_tracked_list
, states
);
1020 string_list_sort(&states
->tracked
);
1021 get_push_ref_states_noquery(states
);
1028 struct string_list list
;
1029 struct ref_states states
;
1034 #define SHOW_INFO_INIT { \
1035 .list = STRING_LIST_INIT_DUP, \
1036 .states = REF_STATES_INIT, \
1039 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
1041 struct show_info
*info
= cb_data
;
1042 int n
= strlen(item
->string
);
1043 if (n
> info
->width
)
1045 string_list_insert(&info
->list
, item
->string
);
1049 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
1051 struct show_info
*info
= cb_data
;
1052 struct ref_states
*states
= &info
->states
;
1053 const char *name
= item
->string
;
1055 if (states
->queried
) {
1056 const char *fmt
= "%s";
1057 const char *arg
= "";
1058 if (string_list_has_string(&states
->new_refs
, name
)) {
1059 fmt
= _(" new (next fetch will store in remotes/%s)");
1060 arg
= states
->remote
->name
;
1061 } else if (string_list_has_string(&states
->tracked
, name
))
1062 arg
= _(" tracked");
1063 else if (string_list_has_string(&states
->skipped
, name
))
1064 arg
= _(" skipped");
1065 else if (string_list_has_string(&states
->stale
, name
))
1066 arg
= _(" stale (use 'git remote prune' to remove)");
1069 printf(" %-*s", info
->width
, name
);
1073 printf(" %s\n", name
);
1078 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
1080 struct show_info
*show_info
= cb_data
;
1081 struct ref_states
*states
= &show_info
->states
;
1082 struct branch_info
*branch_info
= branch_item
->util
;
1083 struct string_list_item
*item
;
1086 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
1087 strcmp(states
->remote
->name
, branch_info
->remote_name
))
1089 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
1090 show_info
->width
= n
;
1091 if (branch_info
->rebase
>= REBASE_TRUE
)
1092 show_info
->any_rebase
= 1;
1094 item
= string_list_insert(&show_info
->list
, branch_item
->string
);
1095 item
->util
= branch_info
;
1100 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
1102 struct show_info
*show_info
= cb_data
;
1103 struct branch_info
*branch_info
= item
->util
;
1104 struct string_list
*merge
= &branch_info
->merge
;
1105 int width
= show_info
->width
+ 4;
1108 if (branch_info
->rebase
>= REBASE_TRUE
&& branch_info
->merge
.nr
> 1) {
1109 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1114 printf(" %-*s ", show_info
->width
, item
->string
);
1115 if (branch_info
->rebase
>= REBASE_TRUE
) {
1117 if (branch_info
->rebase
== REBASE_INTERACTIVE
)
1118 msg
= _("rebases interactively onto remote %s");
1119 else if (branch_info
->rebase
== REBASE_MERGES
)
1120 msg
= _("rebases interactively (with merges) onto "
1123 msg
= _("rebases onto remote %s");
1124 printf_ln(msg
, merge
->items
[0].string
);
1126 } else if (show_info
->any_rebase
) {
1127 printf_ln(_(" merges with remote %s"), merge
->items
[0].string
);
1130 printf_ln(_("merges with remote %s"), merge
->items
[0].string
);
1132 for (i
= 1; i
< merge
->nr
; i
++)
1133 printf(_("%-*s and with remote %s\n"), width
, "",
1134 merge
->items
[i
].string
);
1139 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
1141 struct show_info
*show_info
= cb_data
;
1142 struct push_info
*push_info
= push_item
->util
;
1143 struct string_list_item
*item
;
1145 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
1146 show_info
->width
= n
;
1147 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
1148 show_info
->width2
= n
;
1149 item
= string_list_append(&show_info
->list
, push_item
->string
);
1150 item
->util
= push_item
->util
;
1155 * Sorting comparison for a string list that has push_info
1156 * structs in its util field
1158 static int cmp_string_with_push(const void *va
, const void *vb
)
1160 const struct string_list_item
*a
= va
;
1161 const struct string_list_item
*b
= vb
;
1162 const struct push_info
*a_push
= a
->util
;
1163 const struct push_info
*b_push
= b
->util
;
1164 int cmp
= strcmp(a
->string
, b
->string
);
1165 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
1168 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
1170 struct show_info
*show_info
= cb_data
;
1171 struct push_info
*push_info
= item
->util
;
1172 const char *src
= item
->string
, *status
= NULL
;
1174 switch (push_info
->status
) {
1175 case PUSH_STATUS_CREATE
:
1176 status
= _("create");
1178 case PUSH_STATUS_DELETE
:
1179 status
= _("delete");
1182 case PUSH_STATUS_UPTODATE
:
1183 status
= _("up to date");
1185 case PUSH_STATUS_FASTFORWARD
:
1186 status
= _("fast-forwardable");
1188 case PUSH_STATUS_OUTOFDATE
:
1189 status
= _("local out of date");
1191 case PUSH_STATUS_NOTQUERIED
:
1195 if (push_info
->forced
)
1196 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info
->width
, src
,
1197 show_info
->width2
, push_info
->dest
, status
);
1199 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info
->width
, src
,
1200 show_info
->width2
, push_info
->dest
, status
);
1202 if (push_info
->forced
)
1203 printf_ln(_(" %-*s forces to %s"), show_info
->width
, src
,
1206 printf_ln(_(" %-*s pushes to %s"), show_info
->width
, src
,
1212 static int get_one_entry(struct remote
*remote
, void *priv
)
1214 struct string_list
*list
= priv
;
1215 struct strbuf remote_info_buf
= STRBUF_INIT
;
1219 if (remote
->url_nr
> 0) {
1220 struct strbuf promisor_config
= STRBUF_INIT
;
1221 const char *partial_clone_filter
= NULL
;
1223 strbuf_addf(&promisor_config
, "remote.%s.partialclonefilter", remote
->name
);
1224 strbuf_addf(&remote_info_buf
, "%s (fetch)", remote
->url
[0]);
1225 if (!git_config_get_string_tmp(promisor_config
.buf
, &partial_clone_filter
))
1226 strbuf_addf(&remote_info_buf
, " [%s]", partial_clone_filter
);
1228 strbuf_release(&promisor_config
);
1229 string_list_append(list
, remote
->name
)->util
=
1230 strbuf_detach(&remote_info_buf
, NULL
);
1232 string_list_append(list
, remote
->name
)->util
= NULL
;
1233 if (remote
->pushurl_nr
) {
1234 url
= remote
->pushurl
;
1235 url_nr
= remote
->pushurl_nr
;
1238 url_nr
= remote
->url_nr
;
1240 for (i
= 0; i
< url_nr
; i
++)
1242 strbuf_addf(&remote_info_buf
, "%s (push)", url
[i
]);
1243 string_list_append(list
, remote
->name
)->util
=
1244 strbuf_detach(&remote_info_buf
, NULL
);
1250 static int show_all(void)
1252 struct string_list list
= STRING_LIST_INIT_DUP
;
1255 result
= for_each_remote(get_one_entry
, &list
);
1260 string_list_sort(&list
);
1261 for (i
= 0; i
< list
.nr
; i
++) {
1262 struct string_list_item
*item
= list
.items
+ i
;
1264 printf("%s\t%s\n", item
->string
,
1265 item
->util
? (const char *)item
->util
: "");
1267 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1269 printf("%s\n", item
->string
);
1273 string_list_clear(&list
, 1);
1277 static int show(int argc
, const char **argv
, const char *prefix
)
1279 int no_query
= 0, result
= 0, query_flag
= 0;
1280 struct option options
[] = {
1281 OPT_BOOL('n', NULL
, &no_query
, N_("do not query remotes")),
1284 struct show_info info
= SHOW_INFO_INIT
;
1286 argc
= parse_options(argc
, argv
, prefix
, options
,
1287 builtin_remote_show_usage
,
1294 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1296 for (; argc
; argc
--, argv
++) {
1301 get_remote_ref_states(*argv
, &info
.states
, query_flag
);
1303 printf_ln(_("* remote %s"), *argv
);
1304 printf_ln(_(" Fetch URL: %s"), info
.states
.remote
->url_nr
> 0 ?
1305 info
.states
.remote
->url
[0] : _("(no URL)"));
1306 if (info
.states
.remote
->pushurl_nr
) {
1307 url
= info
.states
.remote
->pushurl
;
1308 url_nr
= info
.states
.remote
->pushurl_nr
;
1310 url
= info
.states
.remote
->url
;
1311 url_nr
= info
.states
.remote
->url_nr
;
1313 for (i
= 0; i
< url_nr
; i
++)
1315 * TRANSLATORS: the colon ':' should align
1316 * with the one in " Fetch URL: %s"
1319 printf_ln(_(" Push URL: %s"), url
[i
]);
1321 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1323 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1324 else if (!info
.states
.heads
.nr
)
1325 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1326 else if (info
.states
.heads
.nr
== 1)
1327 printf_ln(_(" HEAD branch: %s"), info
.states
.heads
.items
[0].string
);
1329 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1330 " may be one of the following):\n"));
1331 for (i
= 0; i
< info
.states
.heads
.nr
; i
++)
1332 printf(" %s\n", info
.states
.heads
.items
[i
].string
);
1335 /* remote branch info */
1337 for_each_string_list(&info
.states
.new_refs
, add_remote_to_show_info
, &info
);
1338 for_each_string_list(&info
.states
.skipped
, add_remote_to_show_info
, &info
);
1339 for_each_string_list(&info
.states
.tracked
, add_remote_to_show_info
, &info
);
1340 for_each_string_list(&info
.states
.stale
, add_remote_to_show_info
, &info
);
1342 printf_ln(Q_(" Remote branch:%s",
1343 " Remote branches:%s",
1345 no_query
? _(" (status not queried)") : "");
1346 for_each_string_list(&info
.list
, show_remote_info_item
, &info
);
1347 string_list_clear(&info
.list
, 0);
1351 info
.any_rebase
= 0;
1352 for_each_string_list(&branch_list
, add_local_to_show_info
, &info
);
1354 printf_ln(Q_(" Local branch configured for 'git pull':",
1355 " Local branches configured for 'git pull':",
1357 for_each_string_list(&info
.list
, show_local_info_item
, &info
);
1358 string_list_clear(&info
.list
, 0);
1361 if (info
.states
.remote
->mirror
)
1362 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1364 info
.width
= info
.width2
= 0;
1365 for_each_string_list(&info
.states
.push
, add_push_to_show_info
, &info
);
1366 QSORT(info
.list
.items
, info
.list
.nr
, cmp_string_with_push
);
1368 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1369 " Local refs configured for 'git push'%s:",
1371 no_query
? _(" (status not queried)") : "");
1372 for_each_string_list(&info
.list
, show_push_info_item
, &info
);
1373 string_list_clear(&info
.list
, 0);
1375 free_remote_ref_states(&info
.states
);
1381 static int set_head(int argc
, const char **argv
, const char *prefix
)
1383 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1384 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1385 char *head_name
= NULL
;
1387 struct option options
[] = {
1388 OPT_BOOL('a', "auto", &opt_a
,
1389 N_("set refs/remotes/<name>/HEAD according to remote")),
1390 OPT_BOOL('d', "delete", &opt_d
,
1391 N_("delete refs/remotes/<name>/HEAD")),
1394 argc
= parse_options(argc
, argv
, prefix
, options
,
1395 builtin_remote_sethead_usage
, 0);
1397 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1399 if (!opt_a
&& !opt_d
&& argc
== 2) {
1400 head_name
= xstrdup(argv
[1]);
1401 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1402 struct ref_states states
= REF_STATES_INIT
;
1403 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1404 if (!states
.heads
.nr
)
1405 result
|= error(_("Cannot determine remote HEAD"));
1406 else if (states
.heads
.nr
> 1) {
1407 result
|= error(_("Multiple remote HEAD branches. "
1408 "Please choose one explicitly with:"));
1409 for (i
= 0; i
< states
.heads
.nr
; i
++)
1410 fprintf(stderr
, " git remote set-head %s %s\n",
1411 argv
[0], states
.heads
.items
[i
].string
);
1413 head_name
= xstrdup(states
.heads
.items
[0].string
);
1414 free_remote_ref_states(&states
);
1415 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1416 if (refs_delete_ref(get_main_ref_store(the_repository
), NULL
, buf
.buf
, NULL
, REF_NO_DEREF
))
1417 result
|= error(_("Could not delete %s"), buf
.buf
);
1419 usage_with_options(builtin_remote_sethead_usage
, options
);
1422 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1423 /* make sure it's valid */
1424 if (!refs_ref_exists(get_main_ref_store(the_repository
), buf2
.buf
))
1425 result
|= error(_("Not a valid ref: %s"), buf2
.buf
);
1426 else if (refs_update_symref(get_main_ref_store(the_repository
), buf
.buf
, buf2
.buf
, "remote set-head"))
1427 result
|= error(_("Could not setup %s"), buf
.buf
);
1429 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1433 strbuf_release(&buf
);
1434 strbuf_release(&buf2
);
1438 static int prune_remote(const char *remote
, int dry_run
)
1441 struct ref_states states
= REF_STATES_INIT
;
1442 struct string_list refs_to_prune
= STRING_LIST_INIT_NODUP
;
1443 struct string_list_item
*item
;
1444 const char *dangling_msg
= dry_run
1445 ? _(" %s will become dangling!")
1446 : _(" %s has become dangling!");
1448 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1450 if (!states
.stale
.nr
) {
1451 free_remote_ref_states(&states
);
1455 printf_ln(_("Pruning %s"), remote
);
1456 printf_ln(_("URL: %s"),
1457 states
.remote
->url_nr
1458 ? states
.remote
->url
[0]
1461 for_each_string_list_item(item
, &states
.stale
)
1462 string_list_append(&refs_to_prune
, item
->util
);
1463 string_list_sort(&refs_to_prune
);
1466 result
|= refs_delete_refs(get_main_ref_store(the_repository
),
1467 "remote: prune", &refs_to_prune
, 0);
1469 for_each_string_list_item(item
, &states
.stale
) {
1470 const char *refname
= item
->util
;
1473 printf_ln(_(" * [would prune] %s"),
1474 abbrev_ref(refname
, "refs/remotes/"));
1476 printf_ln(_(" * [pruned] %s"),
1477 abbrev_ref(refname
, "refs/remotes/"));
1480 warn_dangling_symrefs(stdout
, dangling_msg
, &refs_to_prune
);
1482 string_list_clear(&refs_to_prune
, 0);
1483 free_remote_ref_states(&states
);
1487 static int prune(int argc
, const char **argv
, const char *prefix
)
1489 int dry_run
= 0, result
= 0;
1490 struct option options
[] = {
1491 OPT__DRY_RUN(&dry_run
, N_("dry run")),
1495 argc
= parse_options(argc
, argv
, prefix
, options
,
1496 builtin_remote_prune_usage
, 0);
1499 usage_with_options(builtin_remote_prune_usage
, options
);
1501 for (; argc
; argc
--, argv
++)
1502 result
|= prune_remote(*argv
, dry_run
);
1507 static int get_remote_default(const char *key
, const char *value UNUSED
,
1508 const struct config_context
*ctx UNUSED
,
1511 if (strcmp(key
, "remotes.default") == 0) {
1518 static int update(int argc
, const char **argv
, const char *prefix
)
1521 struct option options
[] = {
1522 OPT_BOOL('p', "prune", &prune
,
1523 N_("prune remotes after fetching")),
1526 struct child_process cmd
= CHILD_PROCESS_INIT
;
1527 int default_defined
= 0;
1529 argc
= parse_options(argc
, argv
, prefix
, options
,
1530 builtin_remote_update_usage
,
1531 PARSE_OPT_KEEP_ARGV0
);
1533 strvec_push(&cmd
.args
, "fetch");
1536 strvec_push(&cmd
.args
, prune
? "--prune" : "--no-prune");
1538 strvec_push(&cmd
.args
, "-v");
1539 strvec_push(&cmd
.args
, "--multiple");
1541 strvec_push(&cmd
.args
, "default");
1542 for (i
= 1; i
< argc
; i
++)
1543 strvec_push(&cmd
.args
, argv
[i
]);
1545 if (strcmp(cmd
.args
.v
[cmd
.args
.nr
-1], "default") == 0) {
1546 git_config(get_remote_default
, &default_defined
);
1547 if (!default_defined
) {
1548 strvec_pop(&cmd
.args
);
1549 strvec_push(&cmd
.args
, "--all");
1554 return run_command(&cmd
);
1557 static int remove_all_fetch_refspecs(const char *key
)
1559 return git_config_set_multivar_gently(key
, NULL
, NULL
,
1560 CONFIG_FLAGS_MULTI_REPLACE
);
1563 static void add_branches(struct remote
*remote
, const char **branches
,
1566 const char *remotename
= remote
->name
;
1567 int mirror
= remote
->mirror
;
1568 struct strbuf refspec
= STRBUF_INIT
;
1570 for (; *branches
; branches
++)
1571 add_branch(key
, *branches
, remotename
, mirror
, &refspec
);
1573 strbuf_release(&refspec
);
1576 static int set_remote_branches(const char *remotename
, const char **branches
,
1579 struct strbuf key
= STRBUF_INIT
;
1580 struct remote
*remote
;
1582 strbuf_addf(&key
, "remote.%s.fetch", remotename
);
1584 remote
= remote_get(remotename
);
1585 if (!remote_is_configured(remote
, 1)) {
1586 error(_("No such remote '%s'"), remotename
);
1590 if (!add_mode
&& remove_all_fetch_refspecs(key
.buf
)) {
1591 strbuf_release(&key
);
1594 add_branches(remote
, branches
, key
.buf
);
1596 strbuf_release(&key
);
1600 static int set_branches(int argc
, const char **argv
, const char *prefix
)
1603 struct option options
[] = {
1604 OPT_BOOL('\0', "add", &add_mode
, N_("add branch")),
1608 argc
= parse_options(argc
, argv
, prefix
, options
,
1609 builtin_remote_setbranches_usage
, 0);
1611 error(_("no remote specified"));
1612 usage_with_options(builtin_remote_setbranches_usage
, options
);
1616 return set_remote_branches(argv
[0], argv
+ 1, add_mode
);
1619 static int get_url(int argc
, const char **argv
, const char *prefix
)
1621 int i
, push_mode
= 0, all_mode
= 0;
1622 const char *remotename
= NULL
;
1623 struct remote
*remote
;
1626 struct option options
[] = {
1627 OPT_BOOL('\0', "push", &push_mode
,
1628 N_("query push URLs rather than fetch URLs")),
1629 OPT_BOOL('\0', "all", &all_mode
,
1630 N_("return all URLs")),
1633 argc
= parse_options(argc
, argv
, prefix
, options
,
1634 builtin_remote_geturl_usage
, 0);
1637 usage_with_options(builtin_remote_geturl_usage
, options
);
1639 remotename
= argv
[0];
1641 remote
= remote_get(remotename
);
1642 if (!remote_is_configured(remote
, 1)) {
1643 error(_("No such remote '%s'"), remotename
);
1649 url
= remote
->pushurl
;
1650 url_nr
= remote
->pushurl_nr
;
1652 /* else fetch mode */
1654 /* Use the fetch URL when no push URLs were found or requested. */
1657 url_nr
= remote
->url_nr
;
1661 die(_("no URLs configured for remote '%s'"), remotename
);
1664 for (i
= 0; i
< url_nr
; i
++)
1665 printf_ln("%s", url
[i
]);
1667 printf_ln("%s", *url
);
1673 static int set_url(int argc
, const char **argv
, const char *prefix
)
1675 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1676 int matches
= 0, negative_matches
= 0;
1677 const char *remotename
= NULL
;
1678 const char *newurl
= NULL
;
1679 const char *oldurl
= NULL
;
1680 struct remote
*remote
;
1682 const char **urlset
;
1684 struct strbuf name_buf
= STRBUF_INIT
;
1685 struct option options
[] = {
1686 OPT_BOOL('\0', "push", &push_mode
,
1687 N_("manipulate push URLs")),
1688 OPT_BOOL('\0', "add", &add_mode
,
1690 OPT_BOOL('\0', "delete", &delete_mode
,
1694 argc
= parse_options(argc
, argv
, prefix
, options
,
1695 builtin_remote_seturl_usage
,
1696 PARSE_OPT_KEEP_ARGV0
);
1698 if (add_mode
&& delete_mode
)
1699 die(_("--add --delete doesn't make sense"));
1701 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1702 usage_with_options(builtin_remote_seturl_usage
, options
);
1704 remotename
= argv
[1];
1712 remote
= remote_get(remotename
);
1713 if (!remote_is_configured(remote
, 1)) {
1714 error(_("No such remote '%s'"), remotename
);
1719 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1720 urlset
= remote
->pushurl
;
1721 urlset_nr
= remote
->pushurl_nr
;
1723 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1724 urlset
= remote
->url
;
1725 urlset_nr
= remote
->url_nr
;
1728 /* Special cases that add new entry. */
1729 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1731 git_config_set_multivar(name_buf
.buf
, newurl
,
1734 git_config_set(name_buf
.buf
, newurl
);
1738 /* Old URL specified. Demand that one matches. */
1739 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1740 die(_("Invalid old URL pattern: %s"), oldurl
);
1742 for (i
= 0; i
< urlset_nr
; i
++)
1743 if (!regexec(&old_regex
, urlset
[i
], 0, NULL
, 0))
1747 if (!delete_mode
&& !matches
)
1748 die(_("No such URL found: %s"), oldurl
);
1749 if (delete_mode
&& !negative_matches
&& !push_mode
)
1750 die(_("Will not delete all non-push URLs"));
1752 regfree(&old_regex
);
1755 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1757 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
,
1758 CONFIG_FLAGS_MULTI_REPLACE
);
1760 strbuf_release(&name_buf
);
1764 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1766 parse_opt_subcommand_fn
*fn
= NULL
;
1767 struct option options
[] = {
1768 OPT__VERBOSE(&verbose
, N_("be verbose; must be placed before a subcommand")),
1769 OPT_SUBCOMMAND("add", &fn
, add
),
1770 OPT_SUBCOMMAND("rename", &fn
, mv
),
1771 OPT_SUBCOMMAND_F("rm", &fn
, rm
, PARSE_OPT_NOCOMPLETE
),
1772 OPT_SUBCOMMAND("remove", &fn
, rm
),
1773 OPT_SUBCOMMAND("set-head", &fn
, set_head
),
1774 OPT_SUBCOMMAND("set-branches", &fn
, set_branches
),
1775 OPT_SUBCOMMAND("get-url", &fn
, get_url
),
1776 OPT_SUBCOMMAND("set-url", &fn
, set_url
),
1777 OPT_SUBCOMMAND("show", &fn
, show
),
1778 OPT_SUBCOMMAND("prune", &fn
, prune
),
1779 OPT_SUBCOMMAND("update", &fn
, update
),
1783 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1784 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1787 return !!fn(argc
, argv
, prefix
);
1790 error(_("unknown subcommand: `%s'"), argv
[0]);
1791 usage_with_options(builtin_remote_usage
, options
);
1793 return !!show_all();