3 #include "parse-options.h"
6 #include "string-list.h"
8 #include "run-command.h"
12 #include "object-store.h"
14 #include "commit-reach.h"
17 static const char * const builtin_remote_usage
[] = {
18 "git remote [-v | --verbose]",
19 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
20 N_("git remote rename [--[no-]progress] <old> <new>"),
21 N_("git remote remove <name>"),
22 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
23 N_("git remote [-v | --verbose] show [-n] <name>"),
24 N_("git remote prune [-n | --dry-run] <name>"),
25 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
26 N_("git remote set-branches [--add] <name> <branch>..."),
27 N_("git remote get-url [--push] [--all] <name>"),
28 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
29 N_("git remote set-url --add <name> <newurl>"),
30 N_("git remote set-url --delete <name> <url>"),
34 static const char * const builtin_remote_add_usage
[] = {
35 N_("git remote add [<options>] <name> <url>"),
39 static const char * const builtin_remote_rename_usage
[] = {
40 N_("git remote rename [--[no-]progress] <old> <new>"),
44 static const char * const builtin_remote_rm_usage
[] = {
45 N_("git remote remove <name>"),
49 static const char * const builtin_remote_sethead_usage
[] = {
50 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
54 static const char * const builtin_remote_setbranches_usage
[] = {
55 N_("git remote set-branches <name> <branch>..."),
56 N_("git remote set-branches --add <name> <branch>..."),
60 static const char * const builtin_remote_show_usage
[] = {
61 N_("git remote show [<options>] <name>"),
65 static const char * const builtin_remote_prune_usage
[] = {
66 N_("git remote prune [<options>] <name>"),
70 static const char * const builtin_remote_update_usage
[] = {
71 N_("git remote update [<options>] [<group> | <remote>]..."),
75 static const char * const builtin_remote_geturl_usage
[] = {
76 N_("git remote get-url [--push] [--all] <name>"),
80 static const char * const builtin_remote_seturl_usage
[] = {
81 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
82 N_("git remote set-url --add <name> <newurl>"),
83 N_("git remote set-url --delete <name> <url>"),
87 #define GET_REF_STATES (1<<0)
88 #define GET_HEAD_NAMES (1<<1)
89 #define GET_PUSH_REF_STATES (1<<2)
93 static int fetch_remote(const char *name
)
95 const char *argv
[] = { "fetch", name
, NULL
, NULL
};
100 printf_ln(_("Updating %s"), name
);
101 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
102 return error(_("Could not fetch %s"), name
);
112 #define MIRROR_NONE 0
113 #define MIRROR_FETCH 1
114 #define MIRROR_PUSH 2
115 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
117 static void add_branch(const char *key
, const char *branchname
,
118 const char *remotename
, int mirror
, struct strbuf
*tmp
)
121 strbuf_addch(tmp
, '+');
123 strbuf_addf(tmp
, "refs/%s:refs/%s",
124 branchname
, branchname
);
126 strbuf_addf(tmp
, "refs/heads/%s:refs/remotes/%s/%s",
127 branchname
, remotename
, branchname
);
128 git_config_set_multivar(key
, tmp
->buf
, "^$", 0);
131 static const char mirror_advice
[] =
132 N_("--mirror is dangerous and deprecated; please\n"
133 "\t use --mirror=fetch or --mirror=push instead");
135 static int parse_mirror_opt(const struct option
*opt
, const char *arg
, int not)
137 unsigned *mirror
= opt
->value
;
139 *mirror
= MIRROR_NONE
;
141 warning("%s", _(mirror_advice
));
142 *mirror
= MIRROR_BOTH
;
144 else if (!strcmp(arg
, "fetch"))
145 *mirror
= MIRROR_FETCH
;
146 else if (!strcmp(arg
, "push"))
147 *mirror
= MIRROR_PUSH
;
149 return error(_("unknown mirror argument: %s"), arg
);
153 static int add(int argc
, const char **argv
, const char *prefix
)
155 int fetch
= 0, fetch_tags
= TAGS_DEFAULT
;
156 unsigned mirror
= MIRROR_NONE
;
157 struct string_list track
= STRING_LIST_INIT_NODUP
;
158 const char *master
= NULL
;
159 struct remote
*remote
;
160 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
161 const char *name
, *url
;
164 struct option options
[] = {
165 OPT_BOOL('f', "fetch", &fetch
, N_("fetch the remote branches")),
166 OPT_SET_INT(0, "tags", &fetch_tags
,
167 N_("import all tags and associated objects when fetching"),
169 OPT_SET_INT(0, NULL
, &fetch_tags
,
170 N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET
),
171 OPT_STRING_LIST('t', "track", &track
, N_("branch"),
172 N_("branch(es) to track")),
173 OPT_STRING('m', "master", &master
, N_("branch"), N_("master branch")),
174 OPT_CALLBACK_F(0, "mirror", &mirror
, "(push|fetch)",
175 N_("set up remote as a mirror to push to or fetch from"),
176 PARSE_OPT_OPTARG
| PARSE_OPT_COMP_ARG
, parse_mirror_opt
),
180 argc
= parse_options(argc
, argv
, prefix
, options
,
181 builtin_remote_add_usage
, 0);
184 usage_with_options(builtin_remote_add_usage
, options
);
186 if (mirror
&& master
)
187 die(_("specifying a master branch makes no sense with --mirror"));
188 if (mirror
&& !(mirror
& MIRROR_FETCH
) && track
.nr
)
189 die(_("specifying branches to track makes sense only with fetch mirrors"));
194 remote
= remote_get(name
);
195 if (remote_is_configured(remote
, 1)) {
196 error(_("remote %s already exists."), name
);
200 if (!valid_remote_name(name
))
201 die(_("'%s' is not a valid remote name"), name
);
203 strbuf_addf(&buf
, "remote.%s.url", name
);
204 git_config_set(buf
.buf
, url
);
206 if (!mirror
|| mirror
& MIRROR_FETCH
) {
208 strbuf_addf(&buf
, "remote.%s.fetch", name
);
210 string_list_append(&track
, "*");
211 for (i
= 0; i
< track
.nr
; i
++) {
212 add_branch(buf
.buf
, track
.items
[i
].string
,
213 name
, mirror
, &buf2
);
217 if (mirror
& MIRROR_PUSH
) {
219 strbuf_addf(&buf
, "remote.%s.mirror", name
);
220 git_config_set(buf
.buf
, "true");
223 if (fetch_tags
!= TAGS_DEFAULT
) {
225 strbuf_addf(&buf
, "remote.%s.tagOpt", name
);
226 git_config_set(buf
.buf
,
227 fetch_tags
== TAGS_SET
? "--tags" : "--no-tags");
230 if (fetch
&& fetch_remote(name
))
235 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
238 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
240 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
241 return error(_("Could not setup master '%s'"), master
);
244 strbuf_release(&buf
);
245 strbuf_release(&buf2
);
246 string_list_clear(&track
, 0);
253 struct string_list merge
;
254 enum rebase_type rebase
;
255 char *push_remote_name
;
258 static struct string_list branch_list
= STRING_LIST_INIT_NODUP
;
260 static const char *abbrev_ref(const char *name
, const char *prefix
)
262 skip_prefix(name
, prefix
, &name
);
265 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
267 static int config_read_branches(const char *key
, const char *value
,
270 const char *orig_key
= key
;
272 struct string_list_item
*item
;
273 struct branch_info
*info
;
274 enum { REMOTE
, MERGE
, REBASE
, PUSH_REMOTE
} type
;
277 if (!starts_with(key
, "branch."))
280 key
+= strlen("branch.");
281 if (strip_suffix(key
, ".remote", &key_len
))
283 else if (strip_suffix(key
, ".merge", &key_len
))
285 else if (strip_suffix(key
, ".rebase", &key_len
))
287 else if (strip_suffix(key
, ".pushremote", &key_len
))
291 name
= xmemdupz(key
, key_len
);
293 item
= string_list_insert(&branch_list
, name
);
296 item
->util
= xcalloc(1, sizeof(struct branch_info
));
300 if (info
->remote_name
)
301 warning(_("more than one %s"), orig_key
);
302 info
->remote_name
= xstrdup(value
);
305 char *space
= strchr(value
, ' ');
306 value
= abbrev_branch(value
);
309 merge
= xstrndup(value
, space
- value
);
310 string_list_append(&info
->merge
, merge
);
311 value
= abbrev_branch(space
+ 1);
312 space
= strchr(value
, ' ');
314 string_list_append(&info
->merge
, xstrdup(value
));
319 * Consider invalid values as false and check the
320 * truth value with >= REBASE_TRUE.
322 info
->rebase
= rebase_parse_value(value
);
323 if (info
->rebase
== REBASE_INVALID
)
324 warning(_("unhandled branch.%s.rebase=%s; assuming "
325 "'true'"), name
, value
);
328 if (info
->push_remote_name
)
329 warning(_("more than one %s"), orig_key
);
330 info
->push_remote_name
= xstrdup(value
);
333 BUG("unexpected type=%d", type
);
339 static void read_branches(void)
343 git_config(config_read_branches
, NULL
);
347 struct remote
*remote
;
348 struct string_list new_refs
, skipped
, stale
, tracked
, heads
, push
;
352 #define REF_STATES_INIT { \
353 .new_refs = STRING_LIST_INIT_DUP, \
354 .skipped = STRING_LIST_INIT_DUP, \
355 .stale = STRING_LIST_INIT_DUP, \
356 .tracked = STRING_LIST_INIT_DUP, \
357 .heads = STRING_LIST_INIT_DUP, \
358 .push = STRING_LIST_INIT_DUP, \
361 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
363 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
364 struct ref
*ref
, *stale_refs
;
367 for (i
= 0; i
< states
->remote
->fetch
.nr
; i
++)
368 if (get_fetch_map(remote_refs
, &states
->remote
->fetch
.items
[i
], &tail
, 1))
369 die(_("Could not get fetch map for refspec %s"),
370 states
->remote
->fetch
.raw
[i
]);
372 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
373 if (omit_name_by_refspec(ref
->name
, &states
->remote
->fetch
))
374 string_list_append(&states
->skipped
, abbrev_branch(ref
->name
));
375 else if (!ref
->peer_ref
|| !ref_exists(ref
->peer_ref
->name
))
376 string_list_append(&states
->new_refs
, abbrev_branch(ref
->name
));
378 string_list_append(&states
->tracked
, abbrev_branch(ref
->name
));
380 stale_refs
= get_stale_heads(&states
->remote
->fetch
, fetch_map
);
381 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
382 struct string_list_item
*item
=
383 string_list_append(&states
->stale
, abbrev_branch(ref
->name
));
384 item
->util
= xstrdup(ref
->name
);
386 free_refs(stale_refs
);
387 free_refs(fetch_map
);
389 string_list_sort(&states
->new_refs
);
390 string_list_sort(&states
->skipped
);
391 string_list_sort(&states
->tracked
);
392 string_list_sort(&states
->stale
);
401 PUSH_STATUS_CREATE
= 0,
403 PUSH_STATUS_UPTODATE
,
404 PUSH_STATUS_FASTFORWARD
,
405 PUSH_STATUS_OUTOFDATE
,
406 PUSH_STATUS_NOTQUERIED
410 static int get_push_ref_states(const struct ref
*remote_refs
,
411 struct ref_states
*states
)
413 struct remote
*remote
= states
->remote
;
414 struct ref
*ref
, *local_refs
, *push_map
;
418 local_refs
= get_local_heads();
419 push_map
= copy_ref_list(remote_refs
);
421 match_push_refs(local_refs
, &push_map
, &remote
->push
, MATCH_REFS_NONE
);
423 for (ref
= push_map
; ref
; ref
= ref
->next
) {
424 struct string_list_item
*item
;
425 struct push_info
*info
;
429 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
431 item
= string_list_append(&states
->push
,
432 abbrev_branch(ref
->peer_ref
->name
));
433 item
->util
= xcalloc(1, sizeof(struct push_info
));
435 info
->forced
= ref
->force
;
436 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
438 if (is_null_oid(&ref
->new_oid
)) {
439 info
->status
= PUSH_STATUS_DELETE
;
440 } else if (oideq(&ref
->old_oid
, &ref
->new_oid
))
441 info
->status
= PUSH_STATUS_UPTODATE
;
442 else if (is_null_oid(&ref
->old_oid
))
443 info
->status
= PUSH_STATUS_CREATE
;
444 else if (has_object_file(&ref
->old_oid
) &&
445 ref_newer(&ref
->new_oid
, &ref
->old_oid
))
446 info
->status
= PUSH_STATUS_FASTFORWARD
;
448 info
->status
= PUSH_STATUS_OUTOFDATE
;
450 free_refs(local_refs
);
455 static int get_push_ref_states_noquery(struct ref_states
*states
)
458 struct remote
*remote
= states
->remote
;
459 struct string_list_item
*item
;
460 struct push_info
*info
;
465 if (!remote
->push
.nr
) {
466 item
= string_list_append(&states
->push
, _("(matching)"));
467 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
468 info
->status
= PUSH_STATUS_NOTQUERIED
;
469 info
->dest
= xstrdup(item
->string
);
471 for (i
= 0; i
< remote
->push
.nr
; i
++) {
472 const struct refspec_item
*spec
= &remote
->push
.items
[i
];
474 item
= string_list_append(&states
->push
, _("(matching)"));
475 else if (strlen(spec
->src
))
476 item
= string_list_append(&states
->push
, spec
->src
);
478 item
= string_list_append(&states
->push
, _("(delete)"));
480 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
481 info
->forced
= spec
->force
;
482 info
->status
= PUSH_STATUS_NOTQUERIED
;
483 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
488 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
490 struct ref
*ref
, *matches
;
491 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
492 struct refspec_item refspec
;
494 memset(&refspec
, 0, sizeof(refspec
));
497 refspec
.src
= refspec
.dst
= "refs/heads/*";
498 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
499 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
501 for (ref
= matches
; ref
; ref
= ref
->next
)
502 string_list_append(&states
->heads
, abbrev_branch(ref
->name
));
504 free_refs(fetch_map
);
510 struct known_remote
{
511 struct known_remote
*next
;
512 struct remote
*remote
;
515 struct known_remotes
{
516 struct remote
*to_delete
;
517 struct known_remote
*list
;
520 static int add_known_remote(struct remote
*remote
, void *cb_data
)
522 struct known_remotes
*all
= cb_data
;
523 struct known_remote
*r
;
525 if (!strcmp(all
->to_delete
->name
, remote
->name
))
528 r
= xmalloc(sizeof(*r
));
535 struct branches_for_remote
{
536 struct remote
*remote
;
537 struct string_list
*branches
, *skipped
;
538 struct known_remotes
*keep
;
541 static int add_branch_for_removal(const char *refname
,
542 const struct object_id
*oid UNUSED
,
543 int flags UNUSED
, void *cb_data
)
545 struct branches_for_remote
*branches
= cb_data
;
546 struct refspec_item refspec
;
547 struct known_remote
*kr
;
549 memset(&refspec
, 0, sizeof(refspec
));
550 refspec
.dst
= (char *)refname
;
551 if (remote_find_tracking(branches
->remote
, &refspec
))
554 /* don't delete a branch if another remote also uses it */
555 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
556 memset(&refspec
, 0, sizeof(refspec
));
557 refspec
.dst
= (char *)refname
;
558 if (!remote_find_tracking(kr
->remote
, &refspec
))
562 /* don't delete non-remote-tracking refs */
563 if (!starts_with(refname
, "refs/remotes/")) {
564 /* advise user how to delete local branches */
565 if (starts_with(refname
, "refs/heads/"))
566 string_list_append(branches
->skipped
,
567 abbrev_branch(refname
));
568 /* silently skip over other non-remote refs */
572 string_list_append(branches
->branches
, refname
);
578 const char *old_name
;
579 const char *new_name
;
580 struct string_list
*remote_branches
;
584 static int read_remote_branches(const char *refname
,
585 const struct object_id
*oid UNUSED
,
586 int flags UNUSED
, void *cb_data
)
588 struct rename_info
*rename
= cb_data
;
589 struct strbuf buf
= STRBUF_INIT
;
590 struct string_list_item
*item
;
594 strbuf_addf(&buf
, "refs/remotes/%s/", rename
->old_name
);
595 if (starts_with(refname
, buf
.buf
)) {
596 item
= string_list_append(rename
->remote_branches
, refname
);
597 symref
= resolve_ref_unsafe(refname
, RESOLVE_REF_READING
,
599 if (symref
&& (flag
& REF_ISSYMREF
)) {
600 item
->util
= xstrdup(symref
);
601 rename
->symrefs_nr
++;
606 strbuf_release(&buf
);
611 static int migrate_file(struct remote
*remote
)
613 struct strbuf buf
= STRBUF_INIT
;
616 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
617 for (i
= 0; i
< remote
->url_nr
; i
++)
618 git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0);
620 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
621 for (i
= 0; i
< remote
->push
.raw_nr
; i
++)
622 git_config_set_multivar(buf
.buf
, remote
->push
.raw
[i
], "^$", 0);
624 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
625 for (i
= 0; i
< remote
->fetch
.raw_nr
; i
++)
626 git_config_set_multivar(buf
.buf
, remote
->fetch
.raw
[i
], "^$", 0);
627 if (remote
->origin
== REMOTE_REMOTES
)
628 unlink_or_warn(git_path("remotes/%s", remote
->name
));
629 else if (remote
->origin
== REMOTE_BRANCHES
)
630 unlink_or_warn(git_path("branches/%s", remote
->name
));
631 strbuf_release(&buf
);
636 struct push_default_info
638 const char *old_name
;
639 enum config_scope scope
;
640 struct strbuf origin
;
644 static int config_read_push_default(const char *key
, const char *value
,
647 struct push_default_info
* info
= cb
;
648 if (strcmp(key
, "remote.pushdefault") ||
649 !value
|| strcmp(value
, info
->old_name
))
652 info
->scope
= current_config_scope();
653 strbuf_reset(&info
->origin
);
654 strbuf_addstr(&info
->origin
, current_config_name());
655 info
->linenr
= current_config_line();
660 static void handle_push_default(const char* old_name
, const char* new_name
)
662 struct push_default_info push_default
= {
663 old_name
, CONFIG_SCOPE_UNKNOWN
, STRBUF_INIT
, -1 };
664 git_config(config_read_push_default
, &push_default
);
665 if (push_default
.scope
>= CONFIG_SCOPE_COMMAND
)
667 else if (push_default
.scope
>= CONFIG_SCOPE_LOCAL
) {
668 int result
= git_config_set_gently("remote.pushDefault",
670 if (new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
671 die(_("could not set '%s'"), "remote.pushDefault");
672 else if (!new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
673 die(_("could not unset '%s'"), "remote.pushDefault");
674 } else if (push_default
.scope
>= CONFIG_SCOPE_SYSTEM
) {
676 warning(_("The %s configuration remote.pushDefault in:\n"
678 "now names the non-existent remote '%s'"),
679 config_scope_name(push_default
.scope
),
680 push_default
.origin
.buf
, push_default
.linenr
,
686 static int mv(int argc
, const char **argv
, const char *prefix
)
688 int show_progress
= isatty(2);
689 struct option options
[] = {
690 OPT_BOOL(0, "progress", &show_progress
, N_("force progress reporting")),
693 struct remote
*oldremote
, *newremote
;
694 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
,
695 old_remote_context
= STRBUF_INIT
;
696 struct string_list remote_branches
= STRING_LIST_INIT_DUP
;
697 struct rename_info rename
;
698 int i
, refs_renamed_nr
= 0, refspec_updated
= 0;
699 struct progress
*progress
= NULL
;
701 argc
= parse_options(argc
, argv
, prefix
, options
,
702 builtin_remote_rename_usage
, 0);
705 usage_with_options(builtin_remote_rename_usage
, options
);
707 rename
.old_name
= argv
[0];
708 rename
.new_name
= argv
[1];
709 rename
.remote_branches
= &remote_branches
;
710 rename
.symrefs_nr
= 0;
712 oldremote
= remote_get(rename
.old_name
);
713 if (!remote_is_configured(oldremote
, 1)) {
714 error(_("No such remote: '%s'"), rename
.old_name
);
718 if (!strcmp(rename
.old_name
, rename
.new_name
) && oldremote
->origin
!= REMOTE_CONFIG
)
719 return migrate_file(oldremote
);
721 newremote
= remote_get(rename
.new_name
);
722 if (remote_is_configured(newremote
, 1)) {
723 error(_("remote %s already exists."), rename
.new_name
);
727 if (!valid_remote_name(rename
.new_name
))
728 die(_("'%s' is not a valid remote name"), rename
.new_name
);
730 strbuf_addf(&buf
, "remote.%s", rename
.old_name
);
731 strbuf_addf(&buf2
, "remote.%s", rename
.new_name
);
732 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
733 return error(_("Could not rename config section '%s' to '%s'"),
737 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new_name
);
738 git_config_set_multivar(buf
.buf
, NULL
, NULL
, CONFIG_FLAGS_MULTI_REPLACE
);
739 strbuf_addf(&old_remote_context
, ":refs/remotes/%s/", rename
.old_name
);
740 for (i
= 0; i
< oldremote
->fetch
.raw_nr
; i
++) {
744 strbuf_addstr(&buf2
, oldremote
->fetch
.raw
[i
]);
745 ptr
= strstr(buf2
.buf
, old_remote_context
.buf
);
749 ptr
-buf2
.buf
+ strlen(":refs/remotes/"),
750 strlen(rename
.old_name
), rename
.new_name
,
751 strlen(rename
.new_name
));
753 warning(_("Not updating non-default fetch refspec\n"
755 "\tPlease update the configuration manually if necessary."),
758 git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0);
762 for (i
= 0; i
< branch_list
.nr
; i
++) {
763 struct string_list_item
*item
= branch_list
.items
+ i
;
764 struct branch_info
*info
= item
->util
;
765 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old_name
)) {
767 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
768 git_config_set(buf
.buf
, rename
.new_name
);
770 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, rename
.old_name
)) {
772 strbuf_addf(&buf
, "branch.%s.pushRemote", item
->string
);
773 git_config_set(buf
.buf
, rename
.new_name
);
777 if (!refspec_updated
)
781 * First remove symrefs, then rename the rest, finally create
784 for_each_ref(read_remote_branches
, &rename
);
787 * Count symrefs twice, since "renaming" them is done by
788 * deleting and recreating them in two separate passes.
790 progress
= start_progress(_("Renaming remote references"),
791 rename
.remote_branches
->nr
+ rename
.symrefs_nr
);
793 for (i
= 0; i
< remote_branches
.nr
; i
++) {
794 struct string_list_item
*item
= remote_branches
.items
+ i
;
795 struct strbuf referent
= STRBUF_INIT
;
797 if (refs_read_symbolic_ref(get_main_ref_store(the_repository
), item
->string
,
800 if (delete_ref(NULL
, item
->string
, NULL
, REF_NO_DEREF
))
801 die(_("deleting '%s' failed"), item
->string
);
803 strbuf_release(&referent
);
804 display_progress(progress
, ++refs_renamed_nr
);
806 for (i
= 0; i
< remote_branches
.nr
; i
++) {
807 struct string_list_item
*item
= remote_branches
.items
+ i
;
812 strbuf_addstr(&buf
, item
->string
);
813 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
814 rename
.new_name
, strlen(rename
.new_name
));
816 strbuf_addf(&buf2
, "remote: renamed %s to %s",
817 item
->string
, buf
.buf
);
818 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
819 die(_("renaming '%s' failed"), item
->string
);
820 display_progress(progress
, ++refs_renamed_nr
);
822 for (i
= 0; i
< remote_branches
.nr
; i
++) {
823 struct string_list_item
*item
= remote_branches
.items
+ i
;
828 strbuf_addstr(&buf
, item
->string
);
829 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
830 rename
.new_name
, strlen(rename
.new_name
));
832 strbuf_addstr(&buf2
, item
->util
);
833 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old_name
),
834 rename
.new_name
, strlen(rename
.new_name
));
836 strbuf_addf(&buf3
, "remote: renamed %s to %s",
837 item
->string
, buf
.buf
);
838 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
839 die(_("creating '%s' failed"), buf
.buf
);
840 display_progress(progress
, ++refs_renamed_nr
);
842 stop_progress(&progress
);
843 string_list_clear(&remote_branches
, 1);
845 handle_push_default(rename
.old_name
, rename
.new_name
);
850 static int rm(int argc
, const char **argv
, const char *prefix
)
852 struct option options
[] = {
855 struct remote
*remote
;
856 struct strbuf buf
= STRBUF_INIT
;
857 struct known_remotes known_remotes
= { NULL
, NULL
};
858 struct string_list branches
= STRING_LIST_INIT_DUP
;
859 struct string_list skipped
= STRING_LIST_INIT_DUP
;
860 struct branches_for_remote cb_data
;
863 memset(&cb_data
, 0, sizeof(cb_data
));
864 cb_data
.branches
= &branches
;
865 cb_data
.skipped
= &skipped
;
866 cb_data
.keep
= &known_remotes
;
868 argc
= parse_options(argc
, argv
, prefix
, options
,
869 builtin_remote_rm_usage
, 0);
871 usage_with_options(builtin_remote_rm_usage
, options
);
873 remote
= remote_get(argv
[0]);
874 if (!remote_is_configured(remote
, 1)) {
875 error(_("No such remote: '%s'"), argv
[0]);
879 known_remotes
.to_delete
= remote
;
880 for_each_remote(add_known_remote
, &known_remotes
);
883 for (i
= 0; i
< branch_list
.nr
; i
++) {
884 struct string_list_item
*item
= branch_list
.items
+ i
;
885 struct branch_info
*info
= item
->util
;
886 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
887 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
888 for (k
= keys
; *k
; k
++) {
890 strbuf_addf(&buf
, "branch.%s.%s",
892 result
= git_config_set_gently(buf
.buf
, NULL
);
893 if (result
&& result
!= CONFIG_NOTHING_SET
)
894 die(_("could not unset '%s'"), buf
.buf
);
897 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, remote
->name
)) {
899 strbuf_addf(&buf
, "branch.%s.pushremote", item
->string
);
900 result
= git_config_set_gently(buf
.buf
, NULL
);
901 if (result
&& result
!= CONFIG_NOTHING_SET
)
902 die(_("could not unset '%s'"), buf
.buf
);
907 * We cannot just pass a function to for_each_ref() which deletes
908 * the branches one by one, since for_each_ref() relies on cached
909 * refs, which are invalidated when deleting a branch.
911 cb_data
.remote
= remote
;
912 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
913 strbuf_release(&buf
);
916 result
= delete_refs("remote: remove", &branches
, REF_NO_DEREF
);
917 string_list_clear(&branches
, 0);
921 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
922 "to delete it, use:",
923 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
924 "to delete them, use:",
926 for (i
= 0; i
< skipped
.nr
; i
++)
927 fprintf(stderr
, " git branch -d %s\n",
928 skipped
.items
[i
].string
);
930 string_list_clear(&skipped
, 0);
933 strbuf_addf(&buf
, "remote.%s", remote
->name
);
934 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
935 return error(_("Could not remove config section '%s'"), buf
.buf
);
937 handle_push_default(remote
->name
, NULL
);
943 static void clear_push_info(void *util
, const char *string
)
945 struct push_info
*info
= util
;
950 static void free_remote_ref_states(struct ref_states
*states
)
952 string_list_clear(&states
->new_refs
, 0);
953 string_list_clear(&states
->skipped
, 0);
954 string_list_clear(&states
->stale
, 1);
955 string_list_clear(&states
->tracked
, 0);
956 string_list_clear(&states
->heads
, 0);
957 string_list_clear_func(&states
->push
, clear_push_info
);
960 static int append_ref_to_tracked_list(const char *refname
,
961 const struct object_id
*oid UNUSED
,
962 int flags
, void *cb_data
)
964 struct ref_states
*states
= cb_data
;
965 struct refspec_item refspec
;
967 if (flags
& REF_ISSYMREF
)
970 memset(&refspec
, 0, sizeof(refspec
));
971 refspec
.dst
= (char *)refname
;
972 if (!remote_find_tracking(states
->remote
, &refspec
))
973 string_list_append(&states
->tracked
, abbrev_branch(refspec
.src
));
978 static int get_remote_ref_states(const char *name
,
979 struct ref_states
*states
,
982 states
->remote
= remote_get(name
);
984 return error(_("No such remote: '%s'"), name
);
989 struct transport
*transport
;
990 const struct ref
*remote_refs
;
992 transport
= transport_get(states
->remote
, states
->remote
->url_nr
> 0 ?
993 states
->remote
->url
[0] : NULL
);
994 remote_refs
= transport_get_remote_refs(transport
, NULL
);
997 if (query
& GET_REF_STATES
)
998 get_ref_states(remote_refs
, states
);
999 if (query
& GET_HEAD_NAMES
)
1000 get_head_names(remote_refs
, states
);
1001 if (query
& GET_PUSH_REF_STATES
)
1002 get_push_ref_states(remote_refs
, states
);
1003 transport_disconnect(transport
);
1005 for_each_ref(append_ref_to_tracked_list
, states
);
1006 string_list_sort(&states
->tracked
);
1007 get_push_ref_states_noquery(states
);
1014 struct string_list list
;
1015 struct ref_states states
;
1020 #define SHOW_INFO_INIT { \
1021 .list = STRING_LIST_INIT_DUP, \
1022 .states = REF_STATES_INIT, \
1025 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
1027 struct show_info
*info
= cb_data
;
1028 int n
= strlen(item
->string
);
1029 if (n
> info
->width
)
1031 string_list_insert(&info
->list
, item
->string
);
1035 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
1037 struct show_info
*info
= cb_data
;
1038 struct ref_states
*states
= &info
->states
;
1039 const char *name
= item
->string
;
1041 if (states
->queried
) {
1042 const char *fmt
= "%s";
1043 const char *arg
= "";
1044 if (string_list_has_string(&states
->new_refs
, name
)) {
1045 fmt
= _(" new (next fetch will store in remotes/%s)");
1046 arg
= states
->remote
->name
;
1047 } else if (string_list_has_string(&states
->tracked
, name
))
1048 arg
= _(" tracked");
1049 else if (string_list_has_string(&states
->skipped
, name
))
1050 arg
= _(" skipped");
1051 else if (string_list_has_string(&states
->stale
, name
))
1052 arg
= _(" stale (use 'git remote prune' to remove)");
1055 printf(" %-*s", info
->width
, name
);
1059 printf(" %s\n", name
);
1064 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
1066 struct show_info
*show_info
= cb_data
;
1067 struct ref_states
*states
= &show_info
->states
;
1068 struct branch_info
*branch_info
= branch_item
->util
;
1069 struct string_list_item
*item
;
1072 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
1073 strcmp(states
->remote
->name
, branch_info
->remote_name
))
1075 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
1076 show_info
->width
= n
;
1077 if (branch_info
->rebase
>= REBASE_TRUE
)
1078 show_info
->any_rebase
= 1;
1080 item
= string_list_insert(&show_info
->list
, branch_item
->string
);
1081 item
->util
= branch_info
;
1086 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
1088 struct show_info
*show_info
= cb_data
;
1089 struct branch_info
*branch_info
= item
->util
;
1090 struct string_list
*merge
= &branch_info
->merge
;
1091 int width
= show_info
->width
+ 4;
1094 if (branch_info
->rebase
>= REBASE_TRUE
&& branch_info
->merge
.nr
> 1) {
1095 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1100 printf(" %-*s ", show_info
->width
, item
->string
);
1101 if (branch_info
->rebase
>= REBASE_TRUE
) {
1103 if (branch_info
->rebase
== REBASE_INTERACTIVE
)
1104 msg
= _("rebases interactively onto remote %s");
1105 else if (branch_info
->rebase
== REBASE_MERGES
)
1106 msg
= _("rebases interactively (with merges) onto "
1109 msg
= _("rebases onto remote %s");
1110 printf_ln(msg
, merge
->items
[0].string
);
1112 } else if (show_info
->any_rebase
) {
1113 printf_ln(_(" merges with remote %s"), merge
->items
[0].string
);
1116 printf_ln(_("merges with remote %s"), merge
->items
[0].string
);
1118 for (i
= 1; i
< merge
->nr
; i
++)
1119 printf(_("%-*s and with remote %s\n"), width
, "",
1120 merge
->items
[i
].string
);
1125 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
1127 struct show_info
*show_info
= cb_data
;
1128 struct push_info
*push_info
= push_item
->util
;
1129 struct string_list_item
*item
;
1131 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
1132 show_info
->width
= n
;
1133 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
1134 show_info
->width2
= n
;
1135 item
= string_list_append(&show_info
->list
, push_item
->string
);
1136 item
->util
= push_item
->util
;
1141 * Sorting comparison for a string list that has push_info
1142 * structs in its util field
1144 static int cmp_string_with_push(const void *va
, const void *vb
)
1146 const struct string_list_item
*a
= va
;
1147 const struct string_list_item
*b
= vb
;
1148 const struct push_info
*a_push
= a
->util
;
1149 const struct push_info
*b_push
= b
->util
;
1150 int cmp
= strcmp(a
->string
, b
->string
);
1151 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
1154 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
1156 struct show_info
*show_info
= cb_data
;
1157 struct push_info
*push_info
= item
->util
;
1158 const char *src
= item
->string
, *status
= NULL
;
1160 switch (push_info
->status
) {
1161 case PUSH_STATUS_CREATE
:
1162 status
= _("create");
1164 case PUSH_STATUS_DELETE
:
1165 status
= _("delete");
1168 case PUSH_STATUS_UPTODATE
:
1169 status
= _("up to date");
1171 case PUSH_STATUS_FASTFORWARD
:
1172 status
= _("fast-forwardable");
1174 case PUSH_STATUS_OUTOFDATE
:
1175 status
= _("local out of date");
1177 case PUSH_STATUS_NOTQUERIED
:
1181 if (push_info
->forced
)
1182 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info
->width
, src
,
1183 show_info
->width2
, push_info
->dest
, status
);
1185 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info
->width
, src
,
1186 show_info
->width2
, push_info
->dest
, status
);
1188 if (push_info
->forced
)
1189 printf_ln(_(" %-*s forces to %s"), show_info
->width
, src
,
1192 printf_ln(_(" %-*s pushes to %s"), show_info
->width
, src
,
1198 static int get_one_entry(struct remote
*remote
, void *priv
)
1200 struct string_list
*list
= priv
;
1201 struct strbuf remote_info_buf
= STRBUF_INIT
;
1205 if (remote
->url_nr
> 0) {
1206 struct strbuf promisor_config
= STRBUF_INIT
;
1207 const char *partial_clone_filter
= NULL
;
1209 strbuf_addf(&promisor_config
, "remote.%s.partialclonefilter", remote
->name
);
1210 strbuf_addf(&remote_info_buf
, "%s (fetch)", remote
->url
[0]);
1211 if (!git_config_get_string_tmp(promisor_config
.buf
, &partial_clone_filter
))
1212 strbuf_addf(&remote_info_buf
, " [%s]", partial_clone_filter
);
1214 strbuf_release(&promisor_config
);
1215 string_list_append(list
, remote
->name
)->util
=
1216 strbuf_detach(&remote_info_buf
, NULL
);
1218 string_list_append(list
, remote
->name
)->util
= NULL
;
1219 if (remote
->pushurl_nr
) {
1220 url
= remote
->pushurl
;
1221 url_nr
= remote
->pushurl_nr
;
1224 url_nr
= remote
->url_nr
;
1226 for (i
= 0; i
< url_nr
; i
++)
1228 strbuf_addf(&remote_info_buf
, "%s (push)", url
[i
]);
1229 string_list_append(list
, remote
->name
)->util
=
1230 strbuf_detach(&remote_info_buf
, NULL
);
1236 static int show_all(void)
1238 struct string_list list
= STRING_LIST_INIT_DUP
;
1241 result
= for_each_remote(get_one_entry
, &list
);
1246 string_list_sort(&list
);
1247 for (i
= 0; i
< list
.nr
; i
++) {
1248 struct string_list_item
*item
= list
.items
+ i
;
1250 printf("%s\t%s\n", item
->string
,
1251 item
->util
? (const char *)item
->util
: "");
1253 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1255 printf("%s\n", item
->string
);
1259 string_list_clear(&list
, 1);
1263 static int show(int argc
, const char **argv
, const char *prefix
)
1265 int no_query
= 0, result
= 0, query_flag
= 0;
1266 struct option options
[] = {
1267 OPT_BOOL('n', NULL
, &no_query
, N_("do not query remotes")),
1270 struct show_info info
= SHOW_INFO_INIT
;
1272 argc
= parse_options(argc
, argv
, prefix
, options
,
1273 builtin_remote_show_usage
,
1280 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1282 for (; argc
; argc
--, argv
++) {
1287 get_remote_ref_states(*argv
, &info
.states
, query_flag
);
1289 printf_ln(_("* remote %s"), *argv
);
1290 printf_ln(_(" Fetch URL: %s"), info
.states
.remote
->url_nr
> 0 ?
1291 info
.states
.remote
->url
[0] : _("(no URL)"));
1292 if (info
.states
.remote
->pushurl_nr
) {
1293 url
= info
.states
.remote
->pushurl
;
1294 url_nr
= info
.states
.remote
->pushurl_nr
;
1296 url
= info
.states
.remote
->url
;
1297 url_nr
= info
.states
.remote
->url_nr
;
1299 for (i
= 0; i
< url_nr
; i
++)
1301 * TRANSLATORS: the colon ':' should align
1302 * with the one in " Fetch URL: %s"
1305 printf_ln(_(" Push URL: %s"), url
[i
]);
1307 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1309 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1310 else if (!info
.states
.heads
.nr
)
1311 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1312 else if (info
.states
.heads
.nr
== 1)
1313 printf_ln(_(" HEAD branch: %s"), info
.states
.heads
.items
[0].string
);
1315 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1316 " may be one of the following):\n"));
1317 for (i
= 0; i
< info
.states
.heads
.nr
; i
++)
1318 printf(" %s\n", info
.states
.heads
.items
[i
].string
);
1321 /* remote branch info */
1323 for_each_string_list(&info
.states
.new_refs
, add_remote_to_show_info
, &info
);
1324 for_each_string_list(&info
.states
.skipped
, add_remote_to_show_info
, &info
);
1325 for_each_string_list(&info
.states
.tracked
, add_remote_to_show_info
, &info
);
1326 for_each_string_list(&info
.states
.stale
, add_remote_to_show_info
, &info
);
1328 printf_ln(Q_(" Remote branch:%s",
1329 " Remote branches:%s",
1331 no_query
? _(" (status not queried)") : "");
1332 for_each_string_list(&info
.list
, show_remote_info_item
, &info
);
1333 string_list_clear(&info
.list
, 0);
1337 info
.any_rebase
= 0;
1338 for_each_string_list(&branch_list
, add_local_to_show_info
, &info
);
1340 printf_ln(Q_(" Local branch configured for 'git pull':",
1341 " Local branches configured for 'git pull':",
1343 for_each_string_list(&info
.list
, show_local_info_item
, &info
);
1344 string_list_clear(&info
.list
, 0);
1347 if (info
.states
.remote
->mirror
)
1348 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1350 info
.width
= info
.width2
= 0;
1351 for_each_string_list(&info
.states
.push
, add_push_to_show_info
, &info
);
1352 QSORT(info
.list
.items
, info
.list
.nr
, cmp_string_with_push
);
1354 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1355 " Local refs configured for 'git push'%s:",
1357 no_query
? _(" (status not queried)") : "");
1358 for_each_string_list(&info
.list
, show_push_info_item
, &info
);
1359 string_list_clear(&info
.list
, 0);
1361 free_remote_ref_states(&info
.states
);
1367 static int set_head(int argc
, const char **argv
, const char *prefix
)
1369 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1370 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1371 char *head_name
= NULL
;
1373 struct option options
[] = {
1374 OPT_BOOL('a', "auto", &opt_a
,
1375 N_("set refs/remotes/<name>/HEAD according to remote")),
1376 OPT_BOOL('d', "delete", &opt_d
,
1377 N_("delete refs/remotes/<name>/HEAD")),
1380 argc
= parse_options(argc
, argv
, prefix
, options
,
1381 builtin_remote_sethead_usage
, 0);
1383 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1385 if (!opt_a
&& !opt_d
&& argc
== 2) {
1386 head_name
= xstrdup(argv
[1]);
1387 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1388 struct ref_states states
= REF_STATES_INIT
;
1389 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1390 if (!states
.heads
.nr
)
1391 result
|= error(_("Cannot determine remote HEAD"));
1392 else if (states
.heads
.nr
> 1) {
1393 result
|= error(_("Multiple remote HEAD branches. "
1394 "Please choose one explicitly with:"));
1395 for (i
= 0; i
< states
.heads
.nr
; i
++)
1396 fprintf(stderr
, " git remote set-head %s %s\n",
1397 argv
[0], states
.heads
.items
[i
].string
);
1399 head_name
= xstrdup(states
.heads
.items
[0].string
);
1400 free_remote_ref_states(&states
);
1401 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1402 if (delete_ref(NULL
, buf
.buf
, NULL
, REF_NO_DEREF
))
1403 result
|= error(_("Could not delete %s"), buf
.buf
);
1405 usage_with_options(builtin_remote_sethead_usage
, options
);
1408 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1409 /* make sure it's valid */
1410 if (!ref_exists(buf2
.buf
))
1411 result
|= error(_("Not a valid ref: %s"), buf2
.buf
);
1412 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1413 result
|= error(_("Could not setup %s"), buf
.buf
);
1415 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1419 strbuf_release(&buf
);
1420 strbuf_release(&buf2
);
1424 static int prune_remote(const char *remote
, int dry_run
)
1427 struct ref_states states
= REF_STATES_INIT
;
1428 struct string_list refs_to_prune
= STRING_LIST_INIT_NODUP
;
1429 struct string_list_item
*item
;
1430 const char *dangling_msg
= dry_run
1431 ? _(" %s will become dangling!")
1432 : _(" %s has become dangling!");
1434 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1436 if (!states
.stale
.nr
) {
1437 free_remote_ref_states(&states
);
1441 printf_ln(_("Pruning %s"), remote
);
1442 printf_ln(_("URL: %s"),
1443 states
.remote
->url_nr
1444 ? states
.remote
->url
[0]
1447 for_each_string_list_item(item
, &states
.stale
)
1448 string_list_append(&refs_to_prune
, item
->util
);
1449 string_list_sort(&refs_to_prune
);
1452 result
|= delete_refs("remote: prune", &refs_to_prune
, 0);
1454 for_each_string_list_item(item
, &states
.stale
) {
1455 const char *refname
= item
->util
;
1458 printf_ln(_(" * [would prune] %s"),
1459 abbrev_ref(refname
, "refs/remotes/"));
1461 printf_ln(_(" * [pruned] %s"),
1462 abbrev_ref(refname
, "refs/remotes/"));
1465 warn_dangling_symrefs(stdout
, dangling_msg
, &refs_to_prune
);
1467 string_list_clear(&refs_to_prune
, 0);
1468 free_remote_ref_states(&states
);
1472 static int prune(int argc
, const char **argv
, const char *prefix
)
1474 int dry_run
= 0, result
= 0;
1475 struct option options
[] = {
1476 OPT__DRY_RUN(&dry_run
, N_("dry run")),
1480 argc
= parse_options(argc
, argv
, prefix
, options
,
1481 builtin_remote_prune_usage
, 0);
1484 usage_with_options(builtin_remote_prune_usage
, options
);
1486 for (; argc
; argc
--, argv
++)
1487 result
|= prune_remote(*argv
, dry_run
);
1492 static int get_remote_default(const char *key
, const char *value UNUSED
, void *priv
)
1494 if (strcmp(key
, "remotes.default") == 0) {
1501 static int update(int argc
, const char **argv
, const char *prefix
)
1504 struct option options
[] = {
1505 OPT_BOOL('p', "prune", &prune
,
1506 N_("prune remotes after fetching")),
1509 struct strvec fetch_argv
= STRVEC_INIT
;
1510 int default_defined
= 0;
1513 argc
= parse_options(argc
, argv
, prefix
, options
,
1514 builtin_remote_update_usage
,
1515 PARSE_OPT_KEEP_ARGV0
);
1517 strvec_push(&fetch_argv
, "fetch");
1520 strvec_push(&fetch_argv
, prune
? "--prune" : "--no-prune");
1522 strvec_push(&fetch_argv
, "-v");
1523 strvec_push(&fetch_argv
, "--multiple");
1525 strvec_push(&fetch_argv
, "default");
1526 for (i
= 1; i
< argc
; i
++)
1527 strvec_push(&fetch_argv
, argv
[i
]);
1529 if (strcmp(fetch_argv
.v
[fetch_argv
.nr
-1], "default") == 0) {
1530 git_config(get_remote_default
, &default_defined
);
1531 if (!default_defined
) {
1532 strvec_pop(&fetch_argv
);
1533 strvec_push(&fetch_argv
, "--all");
1537 retval
= run_command_v_opt(fetch_argv
.v
, RUN_GIT_CMD
);
1538 strvec_clear(&fetch_argv
);
1542 static int remove_all_fetch_refspecs(const char *key
)
1544 return git_config_set_multivar_gently(key
, NULL
, NULL
,
1545 CONFIG_FLAGS_MULTI_REPLACE
);
1548 static void add_branches(struct remote
*remote
, const char **branches
,
1551 const char *remotename
= remote
->name
;
1552 int mirror
= remote
->mirror
;
1553 struct strbuf refspec
= STRBUF_INIT
;
1555 for (; *branches
; branches
++)
1556 add_branch(key
, *branches
, remotename
, mirror
, &refspec
);
1558 strbuf_release(&refspec
);
1561 static int set_remote_branches(const char *remotename
, const char **branches
,
1564 struct strbuf key
= STRBUF_INIT
;
1565 struct remote
*remote
;
1567 strbuf_addf(&key
, "remote.%s.fetch", remotename
);
1569 remote
= remote_get(remotename
);
1570 if (!remote_is_configured(remote
, 1)) {
1571 error(_("No such remote '%s'"), remotename
);
1575 if (!add_mode
&& remove_all_fetch_refspecs(key
.buf
)) {
1576 strbuf_release(&key
);
1579 add_branches(remote
, branches
, key
.buf
);
1581 strbuf_release(&key
);
1585 static int set_branches(int argc
, const char **argv
, const char *prefix
)
1588 struct option options
[] = {
1589 OPT_BOOL('\0', "add", &add_mode
, N_("add branch")),
1593 argc
= parse_options(argc
, argv
, prefix
, options
,
1594 builtin_remote_setbranches_usage
, 0);
1596 error(_("no remote specified"));
1597 usage_with_options(builtin_remote_setbranches_usage
, options
);
1601 return set_remote_branches(argv
[0], argv
+ 1, add_mode
);
1604 static int get_url(int argc
, const char **argv
, const char *prefix
)
1606 int i
, push_mode
= 0, all_mode
= 0;
1607 const char *remotename
= NULL
;
1608 struct remote
*remote
;
1611 struct option options
[] = {
1612 OPT_BOOL('\0', "push", &push_mode
,
1613 N_("query push URLs rather than fetch URLs")),
1614 OPT_BOOL('\0', "all", &all_mode
,
1615 N_("return all URLs")),
1618 argc
= parse_options(argc
, argv
, prefix
, options
,
1619 builtin_remote_geturl_usage
, 0);
1622 usage_with_options(builtin_remote_geturl_usage
, options
);
1624 remotename
= argv
[0];
1626 remote
= remote_get(remotename
);
1627 if (!remote_is_configured(remote
, 1)) {
1628 error(_("No such remote '%s'"), remotename
);
1634 url
= remote
->pushurl
;
1635 url_nr
= remote
->pushurl_nr
;
1637 /* else fetch mode */
1639 /* Use the fetch URL when no push URLs were found or requested. */
1642 url_nr
= remote
->url_nr
;
1646 die(_("no URLs configured for remote '%s'"), remotename
);
1649 for (i
= 0; i
< url_nr
; i
++)
1650 printf_ln("%s", url
[i
]);
1652 printf_ln("%s", *url
);
1658 static int set_url(int argc
, const char **argv
, const char *prefix
)
1660 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1661 int matches
= 0, negative_matches
= 0;
1662 const char *remotename
= NULL
;
1663 const char *newurl
= NULL
;
1664 const char *oldurl
= NULL
;
1665 struct remote
*remote
;
1667 const char **urlset
;
1669 struct strbuf name_buf
= STRBUF_INIT
;
1670 struct option options
[] = {
1671 OPT_BOOL('\0', "push", &push_mode
,
1672 N_("manipulate push URLs")),
1673 OPT_BOOL('\0', "add", &add_mode
,
1675 OPT_BOOL('\0', "delete", &delete_mode
,
1679 argc
= parse_options(argc
, argv
, prefix
, options
,
1680 builtin_remote_seturl_usage
,
1681 PARSE_OPT_KEEP_ARGV0
);
1683 if (add_mode
&& delete_mode
)
1684 die(_("--add --delete doesn't make sense"));
1686 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1687 usage_with_options(builtin_remote_seturl_usage
, options
);
1689 remotename
= argv
[1];
1697 remote
= remote_get(remotename
);
1698 if (!remote_is_configured(remote
, 1)) {
1699 error(_("No such remote '%s'"), remotename
);
1704 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1705 urlset
= remote
->pushurl
;
1706 urlset_nr
= remote
->pushurl_nr
;
1708 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1709 urlset
= remote
->url
;
1710 urlset_nr
= remote
->url_nr
;
1713 /* Special cases that add new entry. */
1714 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1716 git_config_set_multivar(name_buf
.buf
, newurl
,
1719 git_config_set(name_buf
.buf
, newurl
);
1723 /* Old URL specified. Demand that one matches. */
1724 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1725 die(_("Invalid old URL pattern: %s"), oldurl
);
1727 for (i
= 0; i
< urlset_nr
; i
++)
1728 if (!regexec(&old_regex
, urlset
[i
], 0, NULL
, 0))
1732 if (!delete_mode
&& !matches
)
1733 die(_("No such URL found: %s"), oldurl
);
1734 if (delete_mode
&& !negative_matches
&& !push_mode
)
1735 die(_("Will not delete all non-push URLs"));
1737 regfree(&old_regex
);
1740 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1742 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
,
1743 CONFIG_FLAGS_MULTI_REPLACE
);
1745 strbuf_release(&name_buf
);
1749 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1751 parse_opt_subcommand_fn
*fn
= NULL
;
1752 struct option options
[] = {
1753 OPT__VERBOSE(&verbose
, N_("be verbose; must be placed before a subcommand")),
1754 OPT_SUBCOMMAND("add", &fn
, add
),
1755 OPT_SUBCOMMAND("rename", &fn
, mv
),
1756 OPT_SUBCOMMAND_F("rm", &fn
, rm
, PARSE_OPT_NOCOMPLETE
),
1757 OPT_SUBCOMMAND("remove", &fn
, rm
),
1758 OPT_SUBCOMMAND("set-head", &fn
, set_head
),
1759 OPT_SUBCOMMAND("set-branches", &fn
, set_branches
),
1760 OPT_SUBCOMMAND("get-url", &fn
, get_url
),
1761 OPT_SUBCOMMAND("set-url", &fn
, set_url
),
1762 OPT_SUBCOMMAND("show", &fn
, show
),
1763 OPT_SUBCOMMAND("prune", &fn
, prune
),
1764 OPT_SUBCOMMAND("update", &fn
, update
),
1768 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1769 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1772 return !!fn(argc
, argv
, prefix
);
1775 error(_("unknown subcommand: `%s'"), argv
[0]);
1776 usage_with_options(builtin_remote_usage
, options
);
1778 return !!show_all();