1 #define USE_THE_REPOSITORY_VARIABLE
5 #include "parse-options.h"
9 #include "string-list.h"
11 #include "run-command.h"
15 #include "object-store-ll.h"
17 #include "commit-reach.h"
20 static const char * const builtin_remote_usage
[] = {
21 "git remote [-v | --verbose]",
22 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
23 N_("git remote rename [--[no-]progress] <old> <new>"),
24 N_("git remote remove <name>"),
25 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
26 N_("git remote [-v | --verbose] show [-n] <name>"),
27 N_("git remote prune [-n | --dry-run] <name>"),
28 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
29 N_("git remote set-branches [--add] <name> <branch>..."),
30 N_("git remote get-url [--push] [--all] <name>"),
31 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
32 N_("git remote set-url --add <name> <newurl>"),
33 N_("git remote set-url --delete <name> <url>"),
37 static const char * const builtin_remote_add_usage
[] = {
38 N_("git remote add [<options>] <name> <url>"),
42 static const char * const builtin_remote_rename_usage
[] = {
43 N_("git remote rename [--[no-]progress] <old> <new>"),
47 static const char * const builtin_remote_rm_usage
[] = {
48 N_("git remote remove <name>"),
52 static const char * const builtin_remote_sethead_usage
[] = {
53 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
57 static const char * const builtin_remote_setbranches_usage
[] = {
58 N_("git remote set-branches <name> <branch>..."),
59 N_("git remote set-branches --add <name> <branch>..."),
63 static const char * const builtin_remote_show_usage
[] = {
64 N_("git remote show [<options>] <name>"),
68 static const char * const builtin_remote_prune_usage
[] = {
69 N_("git remote prune [<options>] <name>"),
73 static const char * const builtin_remote_update_usage
[] = {
74 N_("git remote update [<options>] [<group> | <remote>]..."),
78 static const char * const builtin_remote_geturl_usage
[] = {
79 N_("git remote get-url [--push] [--all] <name>"),
83 static const char * const builtin_remote_seturl_usage
[] = {
84 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
85 N_("git remote set-url --add <name> <newurl>"),
86 N_("git remote set-url --delete <name> <url>"),
90 #define GET_REF_STATES (1<<0)
91 #define GET_HEAD_NAMES (1<<1)
92 #define GET_PUSH_REF_STATES (1<<2)
96 static int fetch_remote(const char *name
)
98 struct child_process cmd
= CHILD_PROCESS_INIT
;
100 strvec_push(&cmd
.args
, "fetch");
102 strvec_push(&cmd
.args
, "-v");
103 strvec_push(&cmd
.args
, name
);
105 printf_ln(_("Updating %s"), name
);
106 if (run_command(&cmd
))
107 return error(_("Could not fetch %s"), name
);
117 #define MIRROR_NONE 0
118 #define MIRROR_FETCH 1
119 #define MIRROR_PUSH 2
120 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
122 static void add_branch(const char *key
, const char *branchname
,
123 const char *remotename
, int mirror
, struct strbuf
*tmp
)
126 strbuf_addch(tmp
, '+');
128 strbuf_addf(tmp
, "refs/%s:refs/%s",
129 branchname
, branchname
);
131 strbuf_addf(tmp
, "refs/heads/%s:refs/remotes/%s/%s",
132 branchname
, remotename
, branchname
);
133 git_config_set_multivar(key
, tmp
->buf
, "^$", 0);
136 static const char mirror_advice
[] =
137 N_("--mirror is dangerous and deprecated; please\n"
138 "\t use --mirror=fetch or --mirror=push instead");
140 static int parse_mirror_opt(const struct option
*opt
, const char *arg
, int not)
142 unsigned *mirror
= opt
->value
;
144 *mirror
= MIRROR_NONE
;
146 warning("%s", _(mirror_advice
));
147 *mirror
= MIRROR_BOTH
;
149 else if (!strcmp(arg
, "fetch"))
150 *mirror
= MIRROR_FETCH
;
151 else if (!strcmp(arg
, "push"))
152 *mirror
= MIRROR_PUSH
;
154 return error(_("unknown --mirror argument: %s"), arg
);
158 static int add(int argc
, const char **argv
, const char *prefix
)
160 int fetch
= 0, fetch_tags
= TAGS_DEFAULT
;
161 unsigned mirror
= MIRROR_NONE
;
162 struct string_list track
= STRING_LIST_INIT_NODUP
;
163 const char *master
= NULL
;
164 struct remote
*remote
;
165 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
166 const char *name
, *url
;
170 struct option options
[] = {
171 OPT_BOOL('f', "fetch", &fetch
, N_("fetch the remote branches")),
172 OPT_SET_INT(0, "tags", &fetch_tags
,
173 N_("import all tags and associated objects when fetching\n"
174 "or do not fetch any tag at all (--no-tags)"),
176 OPT_STRING_LIST('t', "track", &track
, N_("branch"),
177 N_("branch(es) to track")),
178 OPT_STRING('m', "master", &master
, N_("branch"), N_("master branch")),
179 OPT_CALLBACK_F(0, "mirror", &mirror
, "(push|fetch)",
180 N_("set up remote as a mirror to push to or fetch from"),
181 PARSE_OPT_OPTARG
| PARSE_OPT_COMP_ARG
, parse_mirror_opt
),
185 argc
= parse_options(argc
, argv
, prefix
, options
,
186 builtin_remote_add_usage
, 0);
189 usage_with_options(builtin_remote_add_usage
, options
);
191 if (mirror
&& master
)
192 die(_("specifying a master branch makes no sense with --mirror"));
193 if (mirror
&& !(mirror
& MIRROR_FETCH
) && track
.nr
)
194 die(_("specifying branches to track makes sense only with fetch mirrors"));
199 remote
= remote_get(name
);
200 if (remote_is_configured(remote
, 1)) {
201 error(_("remote %s already exists."), name
);
205 if (!valid_remote_name(name
))
206 die(_("'%s' is not a valid remote name"), name
);
208 strbuf_addf(&buf
, "remote.%s.url", name
);
209 git_config_set(buf
.buf
, url
);
211 if (!mirror
|| mirror
& MIRROR_FETCH
) {
213 strbuf_addf(&buf
, "remote.%s.fetch", name
);
215 string_list_append(&track
, "*");
216 for (i
= 0; i
< track
.nr
; i
++) {
217 add_branch(buf
.buf
, track
.items
[i
].string
,
218 name
, mirror
, &buf2
);
222 if (mirror
& MIRROR_PUSH
) {
224 strbuf_addf(&buf
, "remote.%s.mirror", name
);
225 git_config_set(buf
.buf
, "true");
228 if (fetch_tags
!= TAGS_DEFAULT
) {
230 strbuf_addf(&buf
, "remote.%s.tagOpt", name
);
231 git_config_set(buf
.buf
,
232 fetch_tags
== TAGS_SET
? "--tags" : "--no-tags");
235 if (fetch
&& fetch_remote(name
)) {
242 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
245 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
247 if (refs_update_symref(get_main_ref_store(the_repository
), buf
.buf
, buf2
.buf
, "remote add"))
248 result
= error(_("Could not setup master '%s'"), master
);
252 strbuf_release(&buf
);
253 strbuf_release(&buf2
);
254 string_list_clear(&track
, 0);
261 struct string_list merge
;
262 enum rebase_type rebase
;
263 char *push_remote_name
;
266 static struct string_list branch_list
= STRING_LIST_INIT_DUP
;
268 static const char *abbrev_ref(const char *name
, const char *prefix
)
270 skip_prefix(name
, prefix
, &name
);
273 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
275 static int config_read_branches(const char *key
, const char *value
,
276 const struct config_context
*ctx UNUSED
,
279 const char *orig_key
= key
;
281 struct string_list_item
*item
;
282 struct branch_info
*info
;
283 enum { REMOTE
, MERGE
, REBASE
, PUSH_REMOTE
} type
;
286 if (!starts_with(key
, "branch."))
289 key
+= strlen("branch.");
290 if (strip_suffix(key
, ".remote", &key_len
))
292 else if (strip_suffix(key
, ".merge", &key_len
))
294 else if (strip_suffix(key
, ".rebase", &key_len
))
296 else if (strip_suffix(key
, ".pushremote", &key_len
))
301 name
= xmemdupz(key
, key_len
);
302 item
= string_list_insert(&branch_list
, name
);
305 item
->util
= xcalloc(1, sizeof(struct branch_info
));
309 if (info
->remote_name
)
310 warning(_("more than one %s"), orig_key
);
311 info
->remote_name
= xstrdup(value
);
314 char *space
= strchr(value
, ' ');
315 value
= abbrev_branch(value
);
318 merge
= xstrndup(value
, space
- value
);
319 string_list_append(&info
->merge
, merge
);
320 value
= abbrev_branch(space
+ 1);
321 space
= strchr(value
, ' ');
323 string_list_append(&info
->merge
, xstrdup(value
));
328 * Consider invalid values as false and check the
329 * truth value with >= REBASE_TRUE.
331 info
->rebase
= rebase_parse_value(value
);
332 if (info
->rebase
== REBASE_INVALID
)
333 warning(_("unhandled branch.%s.rebase=%s; assuming "
334 "'true'"), name
, value
);
337 if (info
->push_remote_name
)
338 warning(_("more than one %s"), orig_key
);
339 info
->push_remote_name
= xstrdup(value
);
342 BUG("unexpected type=%d", type
);
349 static void read_branches(void)
353 git_config(config_read_branches
, NULL
);
357 struct remote
*remote
;
358 struct string_list new_refs
, skipped
, stale
, tracked
, heads
, push
;
362 #define REF_STATES_INIT { \
363 .new_refs = STRING_LIST_INIT_DUP, \
364 .skipped = STRING_LIST_INIT_DUP, \
365 .stale = STRING_LIST_INIT_DUP, \
366 .tracked = STRING_LIST_INIT_DUP, \
367 .heads = STRING_LIST_INIT_DUP, \
368 .push = STRING_LIST_INIT_DUP, \
371 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
373 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
374 struct ref
*ref
, *stale_refs
;
377 for (i
= 0; i
< states
->remote
->fetch
.nr
; i
++)
378 if (get_fetch_map(remote_refs
, &states
->remote
->fetch
.items
[i
], &tail
, 1))
379 die(_("Could not get fetch map for refspec %s"),
380 states
->remote
->fetch
.raw
[i
]);
382 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
383 if (omit_name_by_refspec(ref
->name
, &states
->remote
->fetch
))
384 string_list_append(&states
->skipped
, abbrev_branch(ref
->name
));
385 else if (!ref
->peer_ref
|| !refs_ref_exists(get_main_ref_store(the_repository
), ref
->peer_ref
->name
))
386 string_list_append(&states
->new_refs
, abbrev_branch(ref
->name
));
388 string_list_append(&states
->tracked
, abbrev_branch(ref
->name
));
390 stale_refs
= get_stale_heads(&states
->remote
->fetch
, fetch_map
);
391 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
392 struct string_list_item
*item
=
393 string_list_append(&states
->stale
, abbrev_branch(ref
->name
));
394 item
->util
= xstrdup(ref
->name
);
396 free_refs(stale_refs
);
397 free_refs(fetch_map
);
399 string_list_sort(&states
->new_refs
);
400 string_list_sort(&states
->skipped
);
401 string_list_sort(&states
->tracked
);
402 string_list_sort(&states
->stale
);
411 PUSH_STATUS_CREATE
= 0,
413 PUSH_STATUS_UPTODATE
,
414 PUSH_STATUS_FASTFORWARD
,
415 PUSH_STATUS_OUTOFDATE
,
416 PUSH_STATUS_NOTQUERIED
420 static int get_push_ref_states(const struct ref
*remote_refs
,
421 struct ref_states
*states
)
423 struct remote
*remote
= states
->remote
;
424 struct ref
*ref
, *local_refs
, *push_map
;
428 local_refs
= get_local_heads();
429 push_map
= copy_ref_list(remote_refs
);
431 match_push_refs(local_refs
, &push_map
, &remote
->push
, MATCH_REFS_NONE
);
433 for (ref
= push_map
; ref
; ref
= ref
->next
) {
434 struct string_list_item
*item
;
435 struct push_info
*info
;
439 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
441 item
= string_list_append(&states
->push
,
442 abbrev_branch(ref
->peer_ref
->name
));
443 item
->util
= xcalloc(1, sizeof(struct push_info
));
445 info
->forced
= ref
->force
;
446 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
448 if (is_null_oid(&ref
->new_oid
)) {
449 info
->status
= PUSH_STATUS_DELETE
;
450 } else if (oideq(&ref
->old_oid
, &ref
->new_oid
))
451 info
->status
= PUSH_STATUS_UPTODATE
;
452 else if (is_null_oid(&ref
->old_oid
))
453 info
->status
= PUSH_STATUS_CREATE
;
454 else if (repo_has_object_file(the_repository
, &ref
->old_oid
) &&
455 ref_newer(&ref
->new_oid
, &ref
->old_oid
))
456 info
->status
= PUSH_STATUS_FASTFORWARD
;
458 info
->status
= PUSH_STATUS_OUTOFDATE
;
460 free_refs(local_refs
);
465 static int get_push_ref_states_noquery(struct ref_states
*states
)
468 struct remote
*remote
= states
->remote
;
469 struct string_list_item
*item
;
470 struct push_info
*info
;
475 if (!remote
->push
.nr
) {
476 item
= string_list_append(&states
->push
, _("(matching)"));
477 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
478 info
->status
= PUSH_STATUS_NOTQUERIED
;
479 info
->dest
= xstrdup(item
->string
);
481 for (i
= 0; i
< remote
->push
.nr
; i
++) {
482 const struct refspec_item
*spec
= &remote
->push
.items
[i
];
484 item
= string_list_append(&states
->push
, _("(matching)"));
485 else if (strlen(spec
->src
))
486 item
= string_list_append(&states
->push
, spec
->src
);
488 item
= string_list_append(&states
->push
, _("(delete)"));
490 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
491 info
->forced
= spec
->force
;
492 info
->status
= PUSH_STATUS_NOTQUERIED
;
493 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
498 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
500 struct ref
*ref
, *matches
;
501 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
502 struct refspec_item refspec
= {
505 .src
= (char *) "refs/heads/*",
506 .dst
= (char *) "refs/heads/*",
509 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
510 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
512 for (ref
= matches
; ref
; ref
= ref
->next
)
513 string_list_append(&states
->heads
, abbrev_branch(ref
->name
));
515 free_refs(fetch_map
);
520 struct known_remote
{
521 struct known_remote
*next
;
522 struct remote
*remote
;
525 struct known_remotes
{
526 struct remote
*to_delete
;
527 struct known_remote
*list
;
530 static int add_known_remote(struct remote
*remote
, void *cb_data
)
532 struct known_remotes
*all
= cb_data
;
533 struct known_remote
*r
;
535 if (!strcmp(all
->to_delete
->name
, remote
->name
))
538 r
= xmalloc(sizeof(*r
));
545 struct branches_for_remote
{
546 struct remote
*remote
;
547 struct string_list
*branches
, *skipped
;
548 struct known_remotes
*keep
;
551 static int add_branch_for_removal(const char *refname
,
552 const char *referent UNUSED
,
553 const struct object_id
*oid UNUSED
,
554 int flags UNUSED
, void *cb_data
)
556 struct branches_for_remote
*branches
= cb_data
;
557 struct refspec_item refspec
;
558 struct known_remote
*kr
;
560 memset(&refspec
, 0, sizeof(refspec
));
561 refspec
.dst
= (char *)refname
;
562 if (remote_find_tracking(branches
->remote
, &refspec
))
566 /* don't delete a branch if another remote also uses it */
567 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
568 memset(&refspec
, 0, sizeof(refspec
));
569 refspec
.dst
= (char *)refname
;
570 if (!remote_find_tracking(kr
->remote
, &refspec
)) {
576 /* don't delete non-remote-tracking refs */
577 if (!starts_with(refname
, "refs/remotes/")) {
578 /* advise user how to delete local branches */
579 if (starts_with(refname
, "refs/heads/"))
580 string_list_append(branches
->skipped
,
581 abbrev_branch(refname
));
582 /* silently skip over other non-remote refs */
586 string_list_append(branches
->branches
, refname
);
592 const char *old_name
;
593 const char *new_name
;
594 struct string_list
*remote_branches
;
598 static int read_remote_branches(const char *refname
, const char *referent UNUSED
,
599 const struct object_id
*oid UNUSED
,
600 int flags UNUSED
, void *cb_data
)
602 struct rename_info
*rename
= cb_data
;
603 struct strbuf buf
= STRBUF_INIT
;
604 struct string_list_item
*item
;
608 strbuf_addf(&buf
, "refs/remotes/%s/", rename
->old_name
);
609 if (starts_with(refname
, buf
.buf
)) {
610 item
= string_list_append(rename
->remote_branches
, refname
);
611 symref
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
612 refname
, RESOLVE_REF_READING
,
614 if (symref
&& (flag
& REF_ISSYMREF
)) {
615 item
->util
= xstrdup(symref
);
616 rename
->symrefs_nr
++;
621 strbuf_release(&buf
);
626 static int migrate_file(struct remote
*remote
)
628 struct strbuf buf
= STRBUF_INIT
;
631 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
632 for (i
= 0; i
< remote
->url
.nr
; i
++)
633 git_config_set_multivar(buf
.buf
, remote
->url
.v
[i
], "^$", 0);
635 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
636 for (i
= 0; i
< remote
->push
.raw_nr
; i
++)
637 git_config_set_multivar(buf
.buf
, remote
->push
.raw
[i
], "^$", 0);
639 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
640 for (i
= 0; i
< remote
->fetch
.raw_nr
; i
++)
641 git_config_set_multivar(buf
.buf
, remote
->fetch
.raw
[i
], "^$", 0);
642 if (remote
->origin
== REMOTE_REMOTES
)
643 unlink_or_warn(git_path("remotes/%s", remote
->name
));
644 else if (remote
->origin
== REMOTE_BRANCHES
)
645 unlink_or_warn(git_path("branches/%s", remote
->name
));
646 strbuf_release(&buf
);
651 struct push_default_info
653 const char *old_name
;
654 enum config_scope scope
;
655 struct strbuf origin
;
659 static int config_read_push_default(const char *key
, const char *value
,
660 const struct config_context
*ctx
, void *cb
)
662 const struct key_value_info
*kvi
= ctx
->kvi
;
664 struct push_default_info
* info
= cb
;
665 if (strcmp(key
, "remote.pushdefault") ||
666 !value
|| strcmp(value
, info
->old_name
))
669 info
->scope
= kvi
->scope
;
670 strbuf_reset(&info
->origin
);
671 strbuf_addstr(&info
->origin
, config_origin_type_name(kvi
->origin_type
));
672 info
->linenr
= kvi
->linenr
;
677 static void handle_push_default(const char* old_name
, const char* new_name
)
679 struct push_default_info push_default
= {
680 .old_name
= old_name
,
681 .scope
= CONFIG_SCOPE_UNKNOWN
,
682 .origin
= STRBUF_INIT
,
685 git_config(config_read_push_default
, &push_default
);
686 if (push_default
.scope
>= CONFIG_SCOPE_COMMAND
)
688 else if (push_default
.scope
>= CONFIG_SCOPE_LOCAL
) {
689 int result
= git_config_set_gently("remote.pushDefault",
691 if (new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
692 die(_("could not set '%s'"), "remote.pushDefault");
693 else if (!new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
694 die(_("could not unset '%s'"), "remote.pushDefault");
695 } else if (push_default
.scope
>= CONFIG_SCOPE_SYSTEM
) {
697 warning(_("The %s configuration remote.pushDefault in:\n"
699 "now names the non-existent remote '%s'"),
700 config_scope_name(push_default
.scope
),
701 push_default
.origin
.buf
, push_default
.linenr
,
705 strbuf_release(&push_default
.origin
);
709 static int mv(int argc
, const char **argv
, const char *prefix
)
711 int show_progress
= isatty(2);
712 struct option options
[] = {
713 OPT_BOOL(0, "progress", &show_progress
, N_("force progress reporting")),
716 struct remote
*oldremote
, *newremote
;
717 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
,
718 old_remote_context
= STRBUF_INIT
;
719 struct string_list remote_branches
= STRING_LIST_INIT_DUP
;
720 struct rename_info rename
;
721 int i
, refs_renamed_nr
= 0, refspec_updated
= 0;
722 struct progress
*progress
= NULL
;
725 argc
= parse_options(argc
, argv
, prefix
, options
,
726 builtin_remote_rename_usage
, 0);
729 usage_with_options(builtin_remote_rename_usage
, options
);
731 rename
.old_name
= argv
[0];
732 rename
.new_name
= argv
[1];
733 rename
.remote_branches
= &remote_branches
;
734 rename
.symrefs_nr
= 0;
736 oldremote
= remote_get(rename
.old_name
);
737 if (!remote_is_configured(oldremote
, 1)) {
738 error(_("No such remote: '%s'"), rename
.old_name
);
742 if (!strcmp(rename
.old_name
, rename
.new_name
) && oldremote
->origin
!= REMOTE_CONFIG
)
743 return migrate_file(oldremote
);
745 newremote
= remote_get(rename
.new_name
);
746 if (remote_is_configured(newremote
, 1)) {
747 error(_("remote %s already exists."), rename
.new_name
);
751 if (!valid_remote_name(rename
.new_name
))
752 die(_("'%s' is not a valid remote name"), rename
.new_name
);
754 strbuf_addf(&buf
, "remote.%s", rename
.old_name
);
755 strbuf_addf(&buf2
, "remote.%s", rename
.new_name
);
756 if (repo_config_rename_section(the_repository
, buf
.buf
, buf2
.buf
) < 1) {
757 result
= error(_("Could not rename config section '%s' to '%s'"),
762 if (oldremote
->fetch
.raw_nr
) {
764 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new_name
);
765 git_config_set_multivar(buf
.buf
, NULL
, NULL
, CONFIG_FLAGS_MULTI_REPLACE
);
766 strbuf_addf(&old_remote_context
, ":refs/remotes/%s/", rename
.old_name
);
767 for (i
= 0; i
< oldremote
->fetch
.raw_nr
; i
++) {
771 strbuf_addstr(&buf2
, oldremote
->fetch
.raw
[i
]);
772 ptr
= strstr(buf2
.buf
, old_remote_context
.buf
);
776 ptr
-buf2
.buf
+ strlen(":refs/remotes/"),
777 strlen(rename
.old_name
), rename
.new_name
,
778 strlen(rename
.new_name
));
780 warning(_("Not updating non-default fetch refspec\n"
782 "\tPlease update the configuration manually if necessary."),
785 git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0);
790 for (i
= 0; i
< branch_list
.nr
; i
++) {
791 struct string_list_item
*item
= branch_list
.items
+ i
;
792 struct branch_info
*info
= item
->util
;
793 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old_name
)) {
795 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
796 git_config_set(buf
.buf
, rename
.new_name
);
798 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, rename
.old_name
)) {
800 strbuf_addf(&buf
, "branch.%s.pushRemote", item
->string
);
801 git_config_set(buf
.buf
, rename
.new_name
);
805 if (!refspec_updated
)
809 * First remove symrefs, then rename the rest, finally create
812 refs_for_each_ref(get_main_ref_store(the_repository
),
813 read_remote_branches
, &rename
);
816 * Count symrefs twice, since "renaming" them is done by
817 * deleting and recreating them in two separate passes.
819 progress
= start_progress(_("Renaming remote references"),
820 rename
.remote_branches
->nr
+ rename
.symrefs_nr
);
822 for (i
= 0; i
< remote_branches
.nr
; i
++) {
823 struct string_list_item
*item
= remote_branches
.items
+ i
;
824 struct strbuf referent
= STRBUF_INIT
;
826 if (refs_read_symbolic_ref(get_main_ref_store(the_repository
), item
->string
,
829 if (refs_delete_ref(get_main_ref_store(the_repository
), NULL
, item
->string
, NULL
, REF_NO_DEREF
))
830 die(_("deleting '%s' failed"), item
->string
);
832 strbuf_release(&referent
);
833 display_progress(progress
, ++refs_renamed_nr
);
835 for (i
= 0; i
< remote_branches
.nr
; i
++) {
836 struct string_list_item
*item
= remote_branches
.items
+ i
;
841 strbuf_addstr(&buf
, item
->string
);
842 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
843 rename
.new_name
, strlen(rename
.new_name
));
845 strbuf_addf(&buf2
, "remote: renamed %s to %s",
846 item
->string
, buf
.buf
);
847 if (refs_rename_ref(get_main_ref_store(the_repository
), item
->string
, buf
.buf
, buf2
.buf
))
848 die(_("renaming '%s' failed"), item
->string
);
849 display_progress(progress
, ++refs_renamed_nr
);
851 for (i
= 0; i
< remote_branches
.nr
; i
++) {
852 struct string_list_item
*item
= remote_branches
.items
+ i
;
857 strbuf_addstr(&buf
, item
->string
);
858 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
859 rename
.new_name
, strlen(rename
.new_name
));
861 strbuf_addstr(&buf2
, item
->util
);
862 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old_name
),
863 rename
.new_name
, strlen(rename
.new_name
));
865 strbuf_addf(&buf3
, "remote: renamed %s to %s",
866 item
->string
, buf
.buf
);
867 if (refs_update_symref(get_main_ref_store(the_repository
), buf
.buf
, buf2
.buf
, buf3
.buf
))
868 die(_("creating '%s' failed"), buf
.buf
);
869 display_progress(progress
, ++refs_renamed_nr
);
871 stop_progress(&progress
);
873 handle_push_default(rename
.old_name
, rename
.new_name
);
876 string_list_clear(&remote_branches
, 1);
877 strbuf_release(&old_remote_context
);
878 strbuf_release(&buf
);
879 strbuf_release(&buf2
);
880 strbuf_release(&buf3
);
884 static int rm(int argc
, const char **argv
, const char *prefix
)
886 struct option options
[] = {
889 struct remote
*remote
;
890 struct strbuf buf
= STRBUF_INIT
;
891 struct known_remotes known_remotes
= { NULL
, NULL
};
892 struct string_list branches
= STRING_LIST_INIT_DUP
;
893 struct string_list skipped
= STRING_LIST_INIT_DUP
;
894 struct branches_for_remote cb_data
;
897 memset(&cb_data
, 0, sizeof(cb_data
));
898 cb_data
.branches
= &branches
;
899 cb_data
.skipped
= &skipped
;
900 cb_data
.keep
= &known_remotes
;
902 argc
= parse_options(argc
, argv
, prefix
, options
,
903 builtin_remote_rm_usage
, 0);
905 usage_with_options(builtin_remote_rm_usage
, options
);
907 remote
= remote_get(argv
[0]);
908 if (!remote_is_configured(remote
, 1)) {
909 error(_("No such remote: '%s'"), argv
[0]);
913 known_remotes
.to_delete
= remote
;
914 for_each_remote(add_known_remote
, &known_remotes
);
917 for (i
= 0; i
< branch_list
.nr
; i
++) {
918 struct string_list_item
*item
= branch_list
.items
+ i
;
919 struct branch_info
*info
= item
->util
;
920 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
921 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
922 for (k
= keys
; *k
; k
++) {
924 strbuf_addf(&buf
, "branch.%s.%s",
926 result
= git_config_set_gently(buf
.buf
, NULL
);
927 if (result
&& result
!= CONFIG_NOTHING_SET
)
928 die(_("could not unset '%s'"), buf
.buf
);
931 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, remote
->name
)) {
933 strbuf_addf(&buf
, "branch.%s.pushremote", item
->string
);
934 result
= git_config_set_gently(buf
.buf
, NULL
);
935 if (result
&& result
!= CONFIG_NOTHING_SET
)
936 die(_("could not unset '%s'"), buf
.buf
);
941 * We cannot just pass a function to for_each_ref() which deletes
942 * the branches one by one, since for_each_ref() relies on cached
943 * refs, which are invalidated when deleting a branch.
945 cb_data
.remote
= remote
;
946 result
= refs_for_each_ref(get_main_ref_store(the_repository
),
947 add_branch_for_removal
, &cb_data
);
948 strbuf_release(&buf
);
951 result
= refs_delete_refs(get_main_ref_store(the_repository
),
952 "remote: remove", &branches
,
954 string_list_clear(&branches
, 0);
958 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
959 "to delete it, use:",
960 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
961 "to delete them, use:",
963 for (i
= 0; i
< skipped
.nr
; i
++)
964 fprintf(stderr
, " git branch -d %s\n",
965 skipped
.items
[i
].string
);
967 string_list_clear(&skipped
, 0);
970 strbuf_addf(&buf
, "remote.%s", remote
->name
);
971 if (repo_config_rename_section(the_repository
, buf
.buf
, NULL
) < 1) {
972 result
= error(_("Could not remove config section '%s'"), buf
.buf
);
976 handle_push_default(remote
->name
, NULL
);
980 for (struct known_remote
*r
= known_remotes
.list
; r
;) {
981 struct known_remote
*next
= r
->next
;
985 strbuf_release(&buf
);
989 static void clear_push_info(void *util
, const char *string UNUSED
)
991 struct push_info
*info
= util
;
996 static void free_remote_ref_states(struct ref_states
*states
)
998 string_list_clear(&states
->new_refs
, 0);
999 string_list_clear(&states
->skipped
, 0);
1000 string_list_clear(&states
->stale
, 1);
1001 string_list_clear(&states
->tracked
, 0);
1002 string_list_clear(&states
->heads
, 0);
1003 string_list_clear_func(&states
->push
, clear_push_info
);
1006 static int append_ref_to_tracked_list(const char *refname
,
1007 const char *referent UNUSED
,
1008 const struct object_id
*oid UNUSED
,
1009 int flags
, void *cb_data
)
1011 struct ref_states
*states
= cb_data
;
1012 struct refspec_item refspec
;
1014 if (flags
& REF_ISSYMREF
)
1017 memset(&refspec
, 0, sizeof(refspec
));
1018 refspec
.dst
= (char *)refname
;
1019 if (!remote_find_tracking(states
->remote
, &refspec
)) {
1020 string_list_append(&states
->tracked
, abbrev_branch(refspec
.src
));
1027 static int get_remote_ref_states(const char *name
,
1028 struct ref_states
*states
,
1031 states
->remote
= remote_get(name
);
1032 if (!states
->remote
)
1033 return error(_("No such remote: '%s'"), name
);
1038 struct transport
*transport
;
1039 const struct ref
*remote_refs
;
1041 transport
= transport_get(states
->remote
, states
->remote
->url
.v
[0]);
1042 remote_refs
= transport_get_remote_refs(transport
, NULL
);
1044 states
->queried
= 1;
1045 if (query
& GET_REF_STATES
)
1046 get_ref_states(remote_refs
, states
);
1047 if (query
& GET_HEAD_NAMES
)
1048 get_head_names(remote_refs
, states
);
1049 if (query
& GET_PUSH_REF_STATES
)
1050 get_push_ref_states(remote_refs
, states
);
1051 transport_disconnect(transport
);
1053 refs_for_each_ref(get_main_ref_store(the_repository
),
1054 append_ref_to_tracked_list
, states
);
1055 string_list_sort(&states
->tracked
);
1056 get_push_ref_states_noquery(states
);
1063 struct string_list list
;
1064 struct ref_states states
;
1069 #define SHOW_INFO_INIT { \
1070 .list = STRING_LIST_INIT_DUP, \
1071 .states = REF_STATES_INIT, \
1074 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
1076 struct show_info
*info
= cb_data
;
1077 int n
= strlen(item
->string
);
1078 if (n
> info
->width
)
1080 string_list_insert(&info
->list
, item
->string
);
1084 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
1086 struct show_info
*info
= cb_data
;
1087 struct ref_states
*states
= &info
->states
;
1088 const char *name
= item
->string
;
1090 if (states
->queried
) {
1091 const char *fmt
= "%s";
1092 const char *arg
= "";
1093 if (string_list_has_string(&states
->new_refs
, name
)) {
1094 fmt
= _(" new (next fetch will store in remotes/%s)");
1095 arg
= states
->remote
->name
;
1096 } else if (string_list_has_string(&states
->tracked
, name
))
1097 arg
= _(" tracked");
1098 else if (string_list_has_string(&states
->skipped
, name
))
1099 arg
= _(" skipped");
1100 else if (string_list_has_string(&states
->stale
, name
))
1101 arg
= _(" stale (use 'git remote prune' to remove)");
1104 printf(" %-*s", info
->width
, name
);
1108 printf(" %s\n", name
);
1113 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
1115 struct show_info
*show_info
= cb_data
;
1116 struct ref_states
*states
= &show_info
->states
;
1117 struct branch_info
*branch_info
= branch_item
->util
;
1118 struct string_list_item
*item
;
1121 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
1122 strcmp(states
->remote
->name
, branch_info
->remote_name
))
1124 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
1125 show_info
->width
= n
;
1126 if (branch_info
->rebase
>= REBASE_TRUE
)
1127 show_info
->any_rebase
= 1;
1129 item
= string_list_insert(&show_info
->list
, branch_item
->string
);
1130 item
->util
= branch_info
;
1135 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
1137 struct show_info
*show_info
= cb_data
;
1138 struct branch_info
*branch_info
= item
->util
;
1139 struct string_list
*merge
= &branch_info
->merge
;
1140 int width
= show_info
->width
+ 4;
1143 if (branch_info
->rebase
>= REBASE_TRUE
&& branch_info
->merge
.nr
> 1) {
1144 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1149 printf(" %-*s ", show_info
->width
, item
->string
);
1150 if (branch_info
->rebase
>= REBASE_TRUE
) {
1152 if (branch_info
->rebase
== REBASE_INTERACTIVE
)
1153 msg
= _("rebases interactively onto remote %s");
1154 else if (branch_info
->rebase
== REBASE_MERGES
)
1155 msg
= _("rebases interactively (with merges) onto "
1158 msg
= _("rebases onto remote %s");
1159 printf_ln(msg
, merge
->items
[0].string
);
1161 } else if (show_info
->any_rebase
) {
1162 printf_ln(_(" merges with remote %s"), merge
->items
[0].string
);
1165 printf_ln(_("merges with remote %s"), merge
->items
[0].string
);
1167 for (i
= 1; i
< merge
->nr
; i
++)
1168 printf(_("%-*s and with remote %s\n"), width
, "",
1169 merge
->items
[i
].string
);
1174 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
1176 struct show_info
*show_info
= cb_data
;
1177 struct push_info
*push_info
= push_item
->util
;
1178 struct string_list_item
*item
;
1180 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
1181 show_info
->width
= n
;
1182 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
1183 show_info
->width2
= n
;
1184 item
= string_list_append(&show_info
->list
, push_item
->string
);
1185 item
->util
= push_item
->util
;
1190 * Sorting comparison for a string list that has push_info
1191 * structs in its util field
1193 static int cmp_string_with_push(const void *va
, const void *vb
)
1195 const struct string_list_item
*a
= va
;
1196 const struct string_list_item
*b
= vb
;
1197 const struct push_info
*a_push
= a
->util
;
1198 const struct push_info
*b_push
= b
->util
;
1199 int cmp
= strcmp(a
->string
, b
->string
);
1200 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
1203 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
1205 struct show_info
*show_info
= cb_data
;
1206 struct push_info
*push_info
= item
->util
;
1207 const char *src
= item
->string
, *status
= NULL
;
1209 switch (push_info
->status
) {
1210 case PUSH_STATUS_CREATE
:
1211 status
= _("create");
1213 case PUSH_STATUS_DELETE
:
1214 status
= _("delete");
1217 case PUSH_STATUS_UPTODATE
:
1218 status
= _("up to date");
1220 case PUSH_STATUS_FASTFORWARD
:
1221 status
= _("fast-forwardable");
1223 case PUSH_STATUS_OUTOFDATE
:
1224 status
= _("local out of date");
1226 case PUSH_STATUS_NOTQUERIED
:
1230 if (push_info
->forced
)
1231 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info
->width
, src
,
1232 show_info
->width2
, push_info
->dest
, status
);
1234 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info
->width
, src
,
1235 show_info
->width2
, push_info
->dest
, status
);
1237 if (push_info
->forced
)
1238 printf_ln(_(" %-*s forces to %s"), show_info
->width
, src
,
1241 printf_ln(_(" %-*s pushes to %s"), show_info
->width
, src
,
1247 static int get_one_entry(struct remote
*remote
, void *priv
)
1249 struct string_list
*list
= priv
;
1250 struct strbuf remote_info_buf
= STRBUF_INIT
;
1254 if (remote
->url
.nr
> 0) {
1255 struct strbuf promisor_config
= STRBUF_INIT
;
1256 const char *partial_clone_filter
= NULL
;
1258 strbuf_addf(&promisor_config
, "remote.%s.partialclonefilter", remote
->name
);
1259 strbuf_addf(&remote_info_buf
, "%s (fetch)", remote
->url
.v
[0]);
1260 if (!git_config_get_string_tmp(promisor_config
.buf
, &partial_clone_filter
))
1261 strbuf_addf(&remote_info_buf
, " [%s]", partial_clone_filter
);
1263 strbuf_release(&promisor_config
);
1264 string_list_append(list
, remote
->name
)->util
=
1265 strbuf_detach(&remote_info_buf
, NULL
);
1267 string_list_append(list
, remote
->name
)->util
= NULL
;
1268 url
= push_url_of_remote(remote
);
1269 for (i
= 0; i
< url
->nr
; i
++)
1271 strbuf_addf(&remote_info_buf
, "%s (push)", url
->v
[i
]);
1272 string_list_append(list
, remote
->name
)->util
=
1273 strbuf_detach(&remote_info_buf
, NULL
);
1279 static int show_all(void)
1281 struct string_list list
= STRING_LIST_INIT_DUP
;
1284 result
= for_each_remote(get_one_entry
, &list
);
1289 string_list_sort(&list
);
1290 for (i
= 0; i
< list
.nr
; i
++) {
1291 struct string_list_item
*item
= list
.items
+ i
;
1293 printf("%s\t%s\n", item
->string
,
1294 item
->util
? (const char *)item
->util
: "");
1296 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1298 printf("%s\n", item
->string
);
1302 string_list_clear(&list
, 1);
1306 static int show(int argc
, const char **argv
, const char *prefix
)
1308 int no_query
= 0, result
= 0, query_flag
= 0;
1309 struct option options
[] = {
1310 OPT_BOOL('n', NULL
, &no_query
, N_("do not query remotes")),
1313 struct show_info info
= SHOW_INFO_INIT
;
1315 argc
= parse_options(argc
, argv
, prefix
, options
,
1316 builtin_remote_show_usage
,
1323 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1325 for (; argc
; argc
--, argv
++) {
1329 get_remote_ref_states(*argv
, &info
.states
, query_flag
);
1331 printf_ln(_("* remote %s"), *argv
);
1332 printf_ln(_(" Fetch URL: %s"), info
.states
.remote
->url
.v
[0]);
1333 url
= push_url_of_remote(info
.states
.remote
);
1334 for (i
= 0; i
< url
->nr
; i
++)
1336 * TRANSLATORS: the colon ':' should align
1337 * with the one in " Fetch URL: %s"
1340 printf_ln(_(" Push URL: %s"), url
->v
[i
]);
1342 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1344 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1345 else if (!info
.states
.heads
.nr
)
1346 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1347 else if (info
.states
.heads
.nr
== 1)
1348 printf_ln(_(" HEAD branch: %s"), info
.states
.heads
.items
[0].string
);
1350 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1351 " may be one of the following):\n"));
1352 for (i
= 0; i
< info
.states
.heads
.nr
; i
++)
1353 printf(" %s\n", info
.states
.heads
.items
[i
].string
);
1356 /* remote branch info */
1358 for_each_string_list(&info
.states
.new_refs
, add_remote_to_show_info
, &info
);
1359 for_each_string_list(&info
.states
.skipped
, add_remote_to_show_info
, &info
);
1360 for_each_string_list(&info
.states
.tracked
, add_remote_to_show_info
, &info
);
1361 for_each_string_list(&info
.states
.stale
, add_remote_to_show_info
, &info
);
1363 printf_ln(Q_(" Remote branch:%s",
1364 " Remote branches:%s",
1366 no_query
? _(" (status not queried)") : "");
1367 for_each_string_list(&info
.list
, show_remote_info_item
, &info
);
1368 string_list_clear(&info
.list
, 0);
1372 info
.any_rebase
= 0;
1373 for_each_string_list(&branch_list
, add_local_to_show_info
, &info
);
1375 printf_ln(Q_(" Local branch configured for 'git pull':",
1376 " Local branches configured for 'git pull':",
1378 for_each_string_list(&info
.list
, show_local_info_item
, &info
);
1379 string_list_clear(&info
.list
, 0);
1382 if (info
.states
.remote
->mirror
)
1383 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1385 info
.width
= info
.width2
= 0;
1386 for_each_string_list(&info
.states
.push
, add_push_to_show_info
, &info
);
1387 QSORT(info
.list
.items
, info
.list
.nr
, cmp_string_with_push
);
1389 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1390 " Local refs configured for 'git push'%s:",
1392 no_query
? _(" (status not queried)") : "");
1393 for_each_string_list(&info
.list
, show_push_info_item
, &info
);
1394 string_list_clear(&info
.list
, 0);
1396 free_remote_ref_states(&info
.states
);
1402 static int set_head(int argc
, const char **argv
, const char *prefix
)
1404 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1405 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1406 char *head_name
= NULL
;
1408 struct option options
[] = {
1409 OPT_BOOL('a', "auto", &opt_a
,
1410 N_("set refs/remotes/<name>/HEAD according to remote")),
1411 OPT_BOOL('d', "delete", &opt_d
,
1412 N_("delete refs/remotes/<name>/HEAD")),
1415 argc
= parse_options(argc
, argv
, prefix
, options
,
1416 builtin_remote_sethead_usage
, 0);
1418 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1420 if (!opt_a
&& !opt_d
&& argc
== 2) {
1421 head_name
= xstrdup(argv
[1]);
1422 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1423 struct ref_states states
= REF_STATES_INIT
;
1424 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1425 if (!states
.heads
.nr
)
1426 result
|= error(_("Cannot determine remote HEAD"));
1427 else if (states
.heads
.nr
> 1) {
1428 result
|= error(_("Multiple remote HEAD branches. "
1429 "Please choose one explicitly with:"));
1430 for (i
= 0; i
< states
.heads
.nr
; i
++)
1431 fprintf(stderr
, " git remote set-head %s %s\n",
1432 argv
[0], states
.heads
.items
[i
].string
);
1434 head_name
= xstrdup(states
.heads
.items
[0].string
);
1435 free_remote_ref_states(&states
);
1436 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1437 if (refs_delete_ref(get_main_ref_store(the_repository
), NULL
, buf
.buf
, NULL
, REF_NO_DEREF
))
1438 result
|= error(_("Could not delete %s"), buf
.buf
);
1440 usage_with_options(builtin_remote_sethead_usage
, options
);
1443 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1444 /* make sure it's valid */
1445 if (!refs_ref_exists(get_main_ref_store(the_repository
), buf2
.buf
))
1446 result
|= error(_("Not a valid ref: %s"), buf2
.buf
);
1447 else if (refs_update_symref(get_main_ref_store(the_repository
), buf
.buf
, buf2
.buf
, "remote set-head"))
1448 result
|= error(_("Could not setup %s"), buf
.buf
);
1450 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1454 strbuf_release(&buf
);
1455 strbuf_release(&buf2
);
1459 static int prune_remote(const char *remote
, int dry_run
)
1462 struct ref_states states
= REF_STATES_INIT
;
1463 struct string_list refs_to_prune
= STRING_LIST_INIT_NODUP
;
1464 struct string_list_item
*item
;
1465 const char *dangling_msg
= dry_run
1466 ? _(" %s will become dangling!")
1467 : _(" %s has become dangling!");
1469 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1471 if (!states
.stale
.nr
) {
1472 free_remote_ref_states(&states
);
1476 printf_ln(_("Pruning %s"), remote
);
1477 printf_ln(_("URL: %s"), states
.remote
->url
.v
[0]);
1479 for_each_string_list_item(item
, &states
.stale
)
1480 string_list_append(&refs_to_prune
, item
->util
);
1481 string_list_sort(&refs_to_prune
);
1484 result
|= refs_delete_refs(get_main_ref_store(the_repository
),
1485 "remote: prune", &refs_to_prune
, 0);
1487 for_each_string_list_item(item
, &states
.stale
) {
1488 const char *refname
= item
->util
;
1491 printf_ln(_(" * [would prune] %s"),
1492 abbrev_ref(refname
, "refs/remotes/"));
1494 printf_ln(_(" * [pruned] %s"),
1495 abbrev_ref(refname
, "refs/remotes/"));
1498 refs_warn_dangling_symrefs(get_main_ref_store(the_repository
),
1499 stdout
, dangling_msg
, &refs_to_prune
);
1501 string_list_clear(&refs_to_prune
, 0);
1502 free_remote_ref_states(&states
);
1506 static int prune(int argc
, const char **argv
, const char *prefix
)
1508 int dry_run
= 0, result
= 0;
1509 struct option options
[] = {
1510 OPT__DRY_RUN(&dry_run
, N_("dry run")),
1514 argc
= parse_options(argc
, argv
, prefix
, options
,
1515 builtin_remote_prune_usage
, 0);
1518 usage_with_options(builtin_remote_prune_usage
, options
);
1520 for (; argc
; argc
--, argv
++)
1521 result
|= prune_remote(*argv
, dry_run
);
1526 static int get_remote_default(const char *key
, const char *value UNUSED
,
1527 const struct config_context
*ctx UNUSED
,
1530 if (strcmp(key
, "remotes.default") == 0) {
1537 static int update(int argc
, const char **argv
, const char *prefix
)
1540 struct option options
[] = {
1541 OPT_BOOL('p', "prune", &prune
,
1542 N_("prune remotes after fetching")),
1545 struct child_process cmd
= CHILD_PROCESS_INIT
;
1546 int default_defined
= 0;
1548 argc
= parse_options(argc
, argv
, prefix
, options
,
1549 builtin_remote_update_usage
,
1550 PARSE_OPT_KEEP_ARGV0
);
1552 strvec_push(&cmd
.args
, "fetch");
1555 strvec_push(&cmd
.args
, prune
? "--prune" : "--no-prune");
1557 strvec_push(&cmd
.args
, "-v");
1558 strvec_push(&cmd
.args
, "--multiple");
1560 strvec_push(&cmd
.args
, "default");
1561 for (i
= 1; i
< argc
; i
++)
1562 strvec_push(&cmd
.args
, argv
[i
]);
1564 if (strcmp(cmd
.args
.v
[cmd
.args
.nr
-1], "default") == 0) {
1565 git_config(get_remote_default
, &default_defined
);
1566 if (!default_defined
) {
1567 strvec_pop(&cmd
.args
);
1568 strvec_push(&cmd
.args
, "--all");
1573 return run_command(&cmd
);
1576 static int remove_all_fetch_refspecs(const char *key
)
1578 return git_config_set_multivar_gently(key
, NULL
, NULL
,
1579 CONFIG_FLAGS_MULTI_REPLACE
);
1582 static void add_branches(struct remote
*remote
, const char **branches
,
1585 const char *remotename
= remote
->name
;
1586 int mirror
= remote
->mirror
;
1587 struct strbuf refspec
= STRBUF_INIT
;
1589 for (; *branches
; branches
++)
1590 add_branch(key
, *branches
, remotename
, mirror
, &refspec
);
1592 strbuf_release(&refspec
);
1595 static int set_remote_branches(const char *remotename
, const char **branches
,
1598 struct strbuf key
= STRBUF_INIT
;
1599 struct remote
*remote
;
1601 strbuf_addf(&key
, "remote.%s.fetch", remotename
);
1603 remote
= remote_get(remotename
);
1604 if (!remote_is_configured(remote
, 1)) {
1605 error(_("No such remote '%s'"), remotename
);
1609 if (!add_mode
&& remove_all_fetch_refspecs(key
.buf
)) {
1610 strbuf_release(&key
);
1613 add_branches(remote
, branches
, key
.buf
);
1615 strbuf_release(&key
);
1619 static int set_branches(int argc
, const char **argv
, const char *prefix
)
1622 struct option options
[] = {
1623 OPT_BOOL('\0', "add", &add_mode
, N_("add branch")),
1627 argc
= parse_options(argc
, argv
, prefix
, options
,
1628 builtin_remote_setbranches_usage
, 0);
1630 error(_("no remote specified"));
1631 usage_with_options(builtin_remote_setbranches_usage
, options
);
1635 return set_remote_branches(argv
[0], argv
+ 1, add_mode
);
1638 static int get_url(int argc
, const char **argv
, const char *prefix
)
1640 int i
, push_mode
= 0, all_mode
= 0;
1641 const char *remotename
= NULL
;
1642 struct remote
*remote
;
1644 struct option options
[] = {
1645 OPT_BOOL('\0', "push", &push_mode
,
1646 N_("query push URLs rather than fetch URLs")),
1647 OPT_BOOL('\0', "all", &all_mode
,
1648 N_("return all URLs")),
1651 argc
= parse_options(argc
, argv
, prefix
, options
,
1652 builtin_remote_geturl_usage
, 0);
1655 usage_with_options(builtin_remote_geturl_usage
, options
);
1657 remotename
= argv
[0];
1659 remote
= remote_get(remotename
);
1660 if (!remote_is_configured(remote
, 1)) {
1661 error(_("No such remote '%s'"), remotename
);
1665 url
= push_mode
? push_url_of_remote(remote
) : &remote
->url
;
1668 for (i
= 0; i
< url
->nr
; i
++)
1669 printf_ln("%s", url
->v
[i
]);
1671 printf_ln("%s", url
->v
[0]);
1677 static int set_url(int argc
, const char **argv
, const char *prefix
)
1679 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1680 int matches
= 0, negative_matches
= 0;
1681 const char *remotename
= NULL
;
1682 const char *newurl
= NULL
;
1683 const char *oldurl
= NULL
;
1684 struct remote
*remote
;
1686 struct strvec
*urlset
;
1687 struct strbuf name_buf
= STRBUF_INIT
;
1688 struct option options
[] = {
1689 OPT_BOOL('\0', "push", &push_mode
,
1690 N_("manipulate push URLs")),
1691 OPT_BOOL('\0', "add", &add_mode
,
1693 OPT_BOOL('\0', "delete", &delete_mode
,
1697 argc
= parse_options(argc
, argv
, prefix
, options
,
1698 builtin_remote_seturl_usage
,
1699 PARSE_OPT_KEEP_ARGV0
);
1701 if (add_mode
&& delete_mode
)
1702 die(_("--add --delete doesn't make sense"));
1704 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1705 usage_with_options(builtin_remote_seturl_usage
, options
);
1707 remotename
= argv
[1];
1715 remote
= remote_get(remotename
);
1716 if (!remote_is_configured(remote
, 1)) {
1717 error(_("No such remote '%s'"), remotename
);
1722 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1723 urlset
= &remote
->pushurl
;
1725 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1726 urlset
= &remote
->url
;
1729 /* Special cases that add new entry. */
1730 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1732 git_config_set_multivar(name_buf
.buf
, newurl
,
1735 git_config_set(name_buf
.buf
, newurl
);
1739 /* Old URL specified. Demand that one matches. */
1740 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1741 die(_("Invalid old URL pattern: %s"), oldurl
);
1743 for (i
= 0; i
< urlset
->nr
; i
++)
1744 if (!regexec(&old_regex
, urlset
->v
[i
], 0, NULL
, 0))
1748 if (!delete_mode
&& !matches
)
1749 die(_("No such URL found: %s"), oldurl
);
1750 if (delete_mode
&& !negative_matches
&& !push_mode
)
1751 die(_("Will not delete all non-push URLs"));
1753 regfree(&old_regex
);
1756 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1758 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
,
1759 CONFIG_FLAGS_MULTI_REPLACE
);
1761 strbuf_release(&name_buf
);
1765 int cmd_remote(int argc
,
1768 struct repository
*repo UNUSED
)
1770 parse_opt_subcommand_fn
*fn
= NULL
;
1771 struct option options
[] = {
1772 OPT__VERBOSE(&verbose
, N_("be verbose; must be placed before a subcommand")),
1773 OPT_SUBCOMMAND("add", &fn
, add
),
1774 OPT_SUBCOMMAND("rename", &fn
, mv
),
1775 OPT_SUBCOMMAND_F("rm", &fn
, rm
, PARSE_OPT_NOCOMPLETE
),
1776 OPT_SUBCOMMAND("remove", &fn
, rm
),
1777 OPT_SUBCOMMAND("set-head", &fn
, set_head
),
1778 OPT_SUBCOMMAND("set-branches", &fn
, set_branches
),
1779 OPT_SUBCOMMAND("get-url", &fn
, get_url
),
1780 OPT_SUBCOMMAND("set-url", &fn
, set_url
),
1781 OPT_SUBCOMMAND("show", &fn
, show
),
1782 OPT_SUBCOMMAND("prune", &fn
, prune
),
1783 OPT_SUBCOMMAND("update", &fn
, update
),
1787 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1788 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1791 return !!fn(argc
, argv
, prefix
);
1794 error(_("unknown subcommand: `%s'"), argv
[0]);
1795 usage_with_options(builtin_remote_usage
, options
);
1797 return !!show_all();