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
)
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
, NULL
, options
, builtin_remote_add_usage
,
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
, void *cb
)
269 const char *orig_key
= key
;
271 struct string_list_item
*item
;
272 struct branch_info
*info
;
273 enum { REMOTE
, MERGE
, REBASE
, PUSH_REMOTE
} type
;
276 if (!starts_with(key
, "branch."))
279 key
+= strlen("branch.");
280 if (strip_suffix(key
, ".remote", &key_len
))
282 else if (strip_suffix(key
, ".merge", &key_len
))
284 else if (strip_suffix(key
, ".rebase", &key_len
))
286 else if (strip_suffix(key
, ".pushremote", &key_len
))
290 name
= xmemdupz(key
, key_len
);
292 item
= string_list_insert(&branch_list
, name
);
295 item
->util
= xcalloc(1, sizeof(struct branch_info
));
299 if (info
->remote_name
)
300 warning(_("more than one %s"), orig_key
);
301 info
->remote_name
= xstrdup(value
);
304 char *space
= strchr(value
, ' ');
305 value
= abbrev_branch(value
);
308 merge
= xstrndup(value
, space
- value
);
309 string_list_append(&info
->merge
, merge
);
310 value
= abbrev_branch(space
+ 1);
311 space
= strchr(value
, ' ');
313 string_list_append(&info
->merge
, xstrdup(value
));
318 * Consider invalid values as false and check the
319 * truth value with >= REBASE_TRUE.
321 info
->rebase
= rebase_parse_value(value
);
322 if (info
->rebase
== REBASE_INVALID
)
323 warning(_("unhandled branch.%s.rebase=%s; assuming "
324 "'true'"), name
, value
);
327 if (info
->push_remote_name
)
328 warning(_("more than one %s"), orig_key
);
329 info
->push_remote_name
= xstrdup(value
);
332 BUG("unexpected type=%d", type
);
338 static void read_branches(void)
342 git_config(config_read_branches
, NULL
);
346 struct remote
*remote
;
347 struct string_list new_refs
, stale
, tracked
, heads
, push
;
351 #define REF_STATES_INIT { \
352 .new_refs = STRING_LIST_INIT_DUP, \
353 .stale = STRING_LIST_INIT_DUP, \
354 .tracked = STRING_LIST_INIT_DUP, \
355 .heads = STRING_LIST_INIT_DUP, \
356 .push = STRING_LIST_INIT_DUP, \
359 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
361 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
362 struct ref
*ref
, *stale_refs
;
365 for (i
= 0; i
< states
->remote
->fetch
.nr
; i
++)
366 if (get_fetch_map(remote_refs
, &states
->remote
->fetch
.items
[i
], &tail
, 1))
367 die(_("Could not get fetch map for refspec %s"),
368 states
->remote
->fetch
.raw
[i
]);
370 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
371 if (!ref
->peer_ref
|| !ref_exists(ref
->peer_ref
->name
))
372 string_list_append(&states
->new_refs
, abbrev_branch(ref
->name
));
374 string_list_append(&states
->tracked
, abbrev_branch(ref
->name
));
376 stale_refs
= get_stale_heads(&states
->remote
->fetch
, fetch_map
);
377 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
378 struct string_list_item
*item
=
379 string_list_append(&states
->stale
, abbrev_branch(ref
->name
));
380 item
->util
= xstrdup(ref
->name
);
382 free_refs(stale_refs
);
383 free_refs(fetch_map
);
385 string_list_sort(&states
->new_refs
);
386 string_list_sort(&states
->tracked
);
387 string_list_sort(&states
->stale
);
396 PUSH_STATUS_CREATE
= 0,
398 PUSH_STATUS_UPTODATE
,
399 PUSH_STATUS_FASTFORWARD
,
400 PUSH_STATUS_OUTOFDATE
,
401 PUSH_STATUS_NOTQUERIED
405 static int get_push_ref_states(const struct ref
*remote_refs
,
406 struct ref_states
*states
)
408 struct remote
*remote
= states
->remote
;
409 struct ref
*ref
, *local_refs
, *push_map
;
413 local_refs
= get_local_heads();
414 push_map
= copy_ref_list(remote_refs
);
416 match_push_refs(local_refs
, &push_map
, &remote
->push
, MATCH_REFS_NONE
);
418 for (ref
= push_map
; ref
; ref
= ref
->next
) {
419 struct string_list_item
*item
;
420 struct push_info
*info
;
424 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
426 item
= string_list_append(&states
->push
,
427 abbrev_branch(ref
->peer_ref
->name
));
428 item
->util
= xcalloc(1, sizeof(struct push_info
));
430 info
->forced
= ref
->force
;
431 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
433 if (is_null_oid(&ref
->new_oid
)) {
434 info
->status
= PUSH_STATUS_DELETE
;
435 } else if (oideq(&ref
->old_oid
, &ref
->new_oid
))
436 info
->status
= PUSH_STATUS_UPTODATE
;
437 else if (is_null_oid(&ref
->old_oid
))
438 info
->status
= PUSH_STATUS_CREATE
;
439 else if (has_object_file(&ref
->old_oid
) &&
440 ref_newer(&ref
->new_oid
, &ref
->old_oid
))
441 info
->status
= PUSH_STATUS_FASTFORWARD
;
443 info
->status
= PUSH_STATUS_OUTOFDATE
;
445 free_refs(local_refs
);
450 static int get_push_ref_states_noquery(struct ref_states
*states
)
453 struct remote
*remote
= states
->remote
;
454 struct string_list_item
*item
;
455 struct push_info
*info
;
460 if (!remote
->push
.nr
) {
461 item
= string_list_append(&states
->push
, _("(matching)"));
462 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
463 info
->status
= PUSH_STATUS_NOTQUERIED
;
464 info
->dest
= xstrdup(item
->string
);
466 for (i
= 0; i
< remote
->push
.nr
; i
++) {
467 const struct refspec_item
*spec
= &remote
->push
.items
[i
];
469 item
= string_list_append(&states
->push
, _("(matching)"));
470 else if (strlen(spec
->src
))
471 item
= string_list_append(&states
->push
, spec
->src
);
473 item
= string_list_append(&states
->push
, _("(delete)"));
475 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
476 info
->forced
= spec
->force
;
477 info
->status
= PUSH_STATUS_NOTQUERIED
;
478 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
483 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
485 struct ref
*ref
, *matches
;
486 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
487 struct refspec_item refspec
;
489 memset(&refspec
, 0, sizeof(refspec
));
492 refspec
.src
= refspec
.dst
= "refs/heads/*";
493 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
494 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
496 for (ref
= matches
; ref
; ref
= ref
->next
)
497 string_list_append(&states
->heads
, abbrev_branch(ref
->name
));
499 free_refs(fetch_map
);
505 struct known_remote
{
506 struct known_remote
*next
;
507 struct remote
*remote
;
510 struct known_remotes
{
511 struct remote
*to_delete
;
512 struct known_remote
*list
;
515 static int add_known_remote(struct remote
*remote
, void *cb_data
)
517 struct known_remotes
*all
= cb_data
;
518 struct known_remote
*r
;
520 if (!strcmp(all
->to_delete
->name
, remote
->name
))
523 r
= xmalloc(sizeof(*r
));
530 struct branches_for_remote
{
531 struct remote
*remote
;
532 struct string_list
*branches
, *skipped
;
533 struct known_remotes
*keep
;
536 static int add_branch_for_removal(const char *refname
,
537 const struct object_id
*oid
, int flags
, void *cb_data
)
539 struct branches_for_remote
*branches
= cb_data
;
540 struct refspec_item refspec
;
541 struct known_remote
*kr
;
543 memset(&refspec
, 0, sizeof(refspec
));
544 refspec
.dst
= (char *)refname
;
545 if (remote_find_tracking(branches
->remote
, &refspec
))
548 /* don't delete a branch if another remote also uses it */
549 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
550 memset(&refspec
, 0, sizeof(refspec
));
551 refspec
.dst
= (char *)refname
;
552 if (!remote_find_tracking(kr
->remote
, &refspec
))
556 /* don't delete non-remote-tracking refs */
557 if (!starts_with(refname
, "refs/remotes/")) {
558 /* advise user how to delete local branches */
559 if (starts_with(refname
, "refs/heads/"))
560 string_list_append(branches
->skipped
,
561 abbrev_branch(refname
));
562 /* silently skip over other non-remote refs */
566 string_list_append(branches
->branches
, refname
);
572 const char *old_name
;
573 const char *new_name
;
574 struct string_list
*remote_branches
;
578 static int read_remote_branches(const char *refname
,
579 const struct object_id
*oid
, int flags
, void *cb_data
)
581 struct rename_info
*rename
= cb_data
;
582 struct strbuf buf
= STRBUF_INIT
;
583 struct string_list_item
*item
;
587 strbuf_addf(&buf
, "refs/remotes/%s/", rename
->old_name
);
588 if (starts_with(refname
, buf
.buf
)) {
589 item
= string_list_append(rename
->remote_branches
, refname
);
590 symref
= resolve_ref_unsafe(refname
, RESOLVE_REF_READING
,
592 if (symref
&& (flag
& REF_ISSYMREF
)) {
593 item
->util
= xstrdup(symref
);
594 rename
->symrefs_nr
++;
599 strbuf_release(&buf
);
604 static int migrate_file(struct remote
*remote
)
606 struct strbuf buf
= STRBUF_INIT
;
609 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
610 for (i
= 0; i
< remote
->url_nr
; i
++)
611 git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0);
613 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
614 for (i
= 0; i
< remote
->push
.raw_nr
; i
++)
615 git_config_set_multivar(buf
.buf
, remote
->push
.raw
[i
], "^$", 0);
617 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
618 for (i
= 0; i
< remote
->fetch
.raw_nr
; i
++)
619 git_config_set_multivar(buf
.buf
, remote
->fetch
.raw
[i
], "^$", 0);
620 if (remote
->origin
== REMOTE_REMOTES
)
621 unlink_or_warn(git_path("remotes/%s", remote
->name
));
622 else if (remote
->origin
== REMOTE_BRANCHES
)
623 unlink_or_warn(git_path("branches/%s", remote
->name
));
624 strbuf_release(&buf
);
629 struct push_default_info
631 const char *old_name
;
632 enum config_scope scope
;
633 struct strbuf origin
;
637 static int config_read_push_default(const char *key
, const char *value
,
640 struct push_default_info
* info
= cb
;
641 if (strcmp(key
, "remote.pushdefault") ||
642 !value
|| strcmp(value
, info
->old_name
))
645 info
->scope
= current_config_scope();
646 strbuf_reset(&info
->origin
);
647 strbuf_addstr(&info
->origin
, current_config_name());
648 info
->linenr
= current_config_line();
653 static void handle_push_default(const char* old_name
, const char* new_name
)
655 struct push_default_info push_default
= {
656 old_name
, CONFIG_SCOPE_UNKNOWN
, STRBUF_INIT
, -1 };
657 git_config(config_read_push_default
, &push_default
);
658 if (push_default
.scope
>= CONFIG_SCOPE_COMMAND
)
660 else if (push_default
.scope
>= CONFIG_SCOPE_LOCAL
) {
661 int result
= git_config_set_gently("remote.pushDefault",
663 if (new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
664 die(_("could not set '%s'"), "remote.pushDefault");
665 else if (!new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
666 die(_("could not unset '%s'"), "remote.pushDefault");
667 } else if (push_default
.scope
>= CONFIG_SCOPE_SYSTEM
) {
669 warning(_("The %s configuration remote.pushDefault in:\n"
671 "now names the non-existent remote '%s'"),
672 config_scope_name(push_default
.scope
),
673 push_default
.origin
.buf
, push_default
.linenr
,
679 static int mv(int argc
, const char **argv
)
681 int show_progress
= isatty(2);
682 struct option options
[] = {
683 OPT_BOOL(0, "progress", &show_progress
, N_("force progress reporting")),
686 struct remote
*oldremote
, *newremote
;
687 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
,
688 old_remote_context
= STRBUF_INIT
;
689 struct string_list remote_branches
= STRING_LIST_INIT_DUP
;
690 struct rename_info rename
;
691 int i
, refs_renamed_nr
= 0, refspec_updated
= 0;
692 struct progress
*progress
= NULL
;
694 argc
= parse_options(argc
, argv
, NULL
, options
,
695 builtin_remote_rename_usage
, 0);
698 usage_with_options(builtin_remote_rename_usage
, options
);
700 rename
.old_name
= argv
[0];
701 rename
.new_name
= argv
[1];
702 rename
.remote_branches
= &remote_branches
;
703 rename
.symrefs_nr
= 0;
705 oldremote
= remote_get(rename
.old_name
);
706 if (!remote_is_configured(oldremote
, 1)) {
707 error(_("No such remote: '%s'"), rename
.old_name
);
711 if (!strcmp(rename
.old_name
, rename
.new_name
) && oldremote
->origin
!= REMOTE_CONFIG
)
712 return migrate_file(oldremote
);
714 newremote
= remote_get(rename
.new_name
);
715 if (remote_is_configured(newremote
, 1)) {
716 error(_("remote %s already exists."), rename
.new_name
);
720 if (!valid_remote_name(rename
.new_name
))
721 die(_("'%s' is not a valid remote name"), rename
.new_name
);
723 strbuf_addf(&buf
, "remote.%s", rename
.old_name
);
724 strbuf_addf(&buf2
, "remote.%s", rename
.new_name
);
725 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
726 return error(_("Could not rename config section '%s' to '%s'"),
730 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new_name
);
731 git_config_set_multivar(buf
.buf
, NULL
, NULL
, CONFIG_FLAGS_MULTI_REPLACE
);
732 strbuf_addf(&old_remote_context
, ":refs/remotes/%s/", rename
.old_name
);
733 for (i
= 0; i
< oldremote
->fetch
.raw_nr
; i
++) {
737 strbuf_addstr(&buf2
, oldremote
->fetch
.raw
[i
]);
738 ptr
= strstr(buf2
.buf
, old_remote_context
.buf
);
742 ptr
-buf2
.buf
+ strlen(":refs/remotes/"),
743 strlen(rename
.old_name
), rename
.new_name
,
744 strlen(rename
.new_name
));
746 warning(_("Not updating non-default fetch refspec\n"
748 "\tPlease update the configuration manually if necessary."),
751 git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0);
755 for (i
= 0; i
< branch_list
.nr
; i
++) {
756 struct string_list_item
*item
= branch_list
.items
+ i
;
757 struct branch_info
*info
= item
->util
;
758 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old_name
)) {
760 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
761 git_config_set(buf
.buf
, rename
.new_name
);
763 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, rename
.old_name
)) {
765 strbuf_addf(&buf
, "branch.%s.pushRemote", item
->string
);
766 git_config_set(buf
.buf
, rename
.new_name
);
770 if (!refspec_updated
)
774 * First remove symrefs, then rename the rest, finally create
777 for_each_ref(read_remote_branches
, &rename
);
780 * Count symrefs twice, since "renaming" them is done by
781 * deleting and recreating them in two separate passes.
783 progress
= start_progress(_("Renaming remote references"),
784 rename
.remote_branches
->nr
+ rename
.symrefs_nr
);
786 for (i
= 0; i
< remote_branches
.nr
; i
++) {
787 struct string_list_item
*item
= remote_branches
.items
+ i
;
788 struct strbuf referent
= STRBUF_INIT
;
790 if (refs_read_symbolic_ref(get_main_ref_store(the_repository
), item
->string
,
793 if (delete_ref(NULL
, item
->string
, NULL
, REF_NO_DEREF
))
794 die(_("deleting '%s' failed"), item
->string
);
796 strbuf_release(&referent
);
797 display_progress(progress
, ++refs_renamed_nr
);
799 for (i
= 0; i
< remote_branches
.nr
; i
++) {
800 struct string_list_item
*item
= remote_branches
.items
+ i
;
805 strbuf_addstr(&buf
, item
->string
);
806 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
807 rename
.new_name
, strlen(rename
.new_name
));
809 strbuf_addf(&buf2
, "remote: renamed %s to %s",
810 item
->string
, buf
.buf
);
811 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
812 die(_("renaming '%s' failed"), item
->string
);
813 display_progress(progress
, ++refs_renamed_nr
);
815 for (i
= 0; i
< remote_branches
.nr
; i
++) {
816 struct string_list_item
*item
= remote_branches
.items
+ i
;
821 strbuf_addstr(&buf
, item
->string
);
822 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
823 rename
.new_name
, strlen(rename
.new_name
));
825 strbuf_addstr(&buf2
, item
->util
);
826 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old_name
),
827 rename
.new_name
, strlen(rename
.new_name
));
829 strbuf_addf(&buf3
, "remote: renamed %s to %s",
830 item
->string
, buf
.buf
);
831 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
832 die(_("creating '%s' failed"), buf
.buf
);
833 display_progress(progress
, ++refs_renamed_nr
);
835 stop_progress(&progress
);
836 string_list_clear(&remote_branches
, 1);
838 handle_push_default(rename
.old_name
, rename
.new_name
);
843 static int rm(int argc
, const char **argv
)
845 struct option options
[] = {
848 struct remote
*remote
;
849 struct strbuf buf
= STRBUF_INIT
;
850 struct known_remotes known_remotes
= { NULL
, NULL
};
851 struct string_list branches
= STRING_LIST_INIT_DUP
;
852 struct string_list skipped
= STRING_LIST_INIT_DUP
;
853 struct branches_for_remote cb_data
;
856 memset(&cb_data
, 0, sizeof(cb_data
));
857 cb_data
.branches
= &branches
;
858 cb_data
.skipped
= &skipped
;
859 cb_data
.keep
= &known_remotes
;
862 usage_with_options(builtin_remote_rm_usage
, options
);
864 remote
= remote_get(argv
[1]);
865 if (!remote_is_configured(remote
, 1)) {
866 error(_("No such remote: '%s'"), argv
[1]);
870 known_remotes
.to_delete
= remote
;
871 for_each_remote(add_known_remote
, &known_remotes
);
874 for (i
= 0; i
< branch_list
.nr
; i
++) {
875 struct string_list_item
*item
= branch_list
.items
+ i
;
876 struct branch_info
*info
= item
->util
;
877 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
878 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
879 for (k
= keys
; *k
; k
++) {
881 strbuf_addf(&buf
, "branch.%s.%s",
883 result
= git_config_set_gently(buf
.buf
, NULL
);
884 if (result
&& result
!= CONFIG_NOTHING_SET
)
885 die(_("could not unset '%s'"), buf
.buf
);
888 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, remote
->name
)) {
890 strbuf_addf(&buf
, "branch.%s.pushremote", item
->string
);
891 result
= git_config_set_gently(buf
.buf
, NULL
);
892 if (result
&& result
!= CONFIG_NOTHING_SET
)
893 die(_("could not unset '%s'"), buf
.buf
);
898 * We cannot just pass a function to for_each_ref() which deletes
899 * the branches one by one, since for_each_ref() relies on cached
900 * refs, which are invalidated when deleting a branch.
902 cb_data
.remote
= remote
;
903 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
904 strbuf_release(&buf
);
907 result
= delete_refs("remote: remove", &branches
, REF_NO_DEREF
);
908 string_list_clear(&branches
, 0);
912 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
913 "to delete it, use:",
914 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
915 "to delete them, use:",
917 for (i
= 0; i
< skipped
.nr
; i
++)
918 fprintf(stderr
, " git branch -d %s\n",
919 skipped
.items
[i
].string
);
921 string_list_clear(&skipped
, 0);
924 strbuf_addf(&buf
, "remote.%s", remote
->name
);
925 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
926 return error(_("Could not remove config section '%s'"), buf
.buf
);
928 handle_push_default(remote
->name
, NULL
);
934 static void clear_push_info(void *util
, const char *string
)
936 struct push_info
*info
= util
;
941 static void free_remote_ref_states(struct ref_states
*states
)
943 string_list_clear(&states
->new_refs
, 0);
944 string_list_clear(&states
->stale
, 1);
945 string_list_clear(&states
->tracked
, 0);
946 string_list_clear(&states
->heads
, 0);
947 string_list_clear_func(&states
->push
, clear_push_info
);
950 static int append_ref_to_tracked_list(const char *refname
,
951 const struct object_id
*oid
, int flags
, void *cb_data
)
953 struct ref_states
*states
= cb_data
;
954 struct refspec_item refspec
;
956 if (flags
& REF_ISSYMREF
)
959 memset(&refspec
, 0, sizeof(refspec
));
960 refspec
.dst
= (char *)refname
;
961 if (!remote_find_tracking(states
->remote
, &refspec
))
962 string_list_append(&states
->tracked
, abbrev_branch(refspec
.src
));
967 static int get_remote_ref_states(const char *name
,
968 struct ref_states
*states
,
971 states
->remote
= remote_get(name
);
973 return error(_("No such remote: '%s'"), name
);
978 struct transport
*transport
;
979 const struct ref
*remote_refs
;
981 transport
= transport_get(states
->remote
, states
->remote
->url_nr
> 0 ?
982 states
->remote
->url
[0] : NULL
);
983 remote_refs
= transport_get_remote_refs(transport
, NULL
);
986 if (query
& GET_REF_STATES
)
987 get_ref_states(remote_refs
, states
);
988 if (query
& GET_HEAD_NAMES
)
989 get_head_names(remote_refs
, states
);
990 if (query
& GET_PUSH_REF_STATES
)
991 get_push_ref_states(remote_refs
, states
);
992 transport_disconnect(transport
);
994 for_each_ref(append_ref_to_tracked_list
, states
);
995 string_list_sort(&states
->tracked
);
996 get_push_ref_states_noquery(states
);
1003 struct string_list list
;
1004 struct ref_states states
;
1009 #define SHOW_INFO_INIT { \
1010 .list = STRING_LIST_INIT_DUP, \
1011 .states = REF_STATES_INIT, \
1014 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
1016 struct show_info
*info
= cb_data
;
1017 int n
= strlen(item
->string
);
1018 if (n
> info
->width
)
1020 string_list_insert(&info
->list
, item
->string
);
1024 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
1026 struct show_info
*info
= cb_data
;
1027 struct ref_states
*states
= &info
->states
;
1028 const char *name
= item
->string
;
1030 if (states
->queried
) {
1031 const char *fmt
= "%s";
1032 const char *arg
= "";
1033 if (string_list_has_string(&states
->new_refs
, name
)) {
1034 fmt
= _(" new (next fetch will store in remotes/%s)");
1035 arg
= states
->remote
->name
;
1036 } else if (string_list_has_string(&states
->tracked
, name
))
1037 arg
= _(" tracked");
1038 else if (string_list_has_string(&states
->stale
, name
))
1039 arg
= _(" stale (use 'git remote prune' to remove)");
1042 printf(" %-*s", info
->width
, name
);
1046 printf(" %s\n", name
);
1051 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
1053 struct show_info
*show_info
= cb_data
;
1054 struct ref_states
*states
= &show_info
->states
;
1055 struct branch_info
*branch_info
= branch_item
->util
;
1056 struct string_list_item
*item
;
1059 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
1060 strcmp(states
->remote
->name
, branch_info
->remote_name
))
1062 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
1063 show_info
->width
= n
;
1064 if (branch_info
->rebase
>= REBASE_TRUE
)
1065 show_info
->any_rebase
= 1;
1067 item
= string_list_insert(&show_info
->list
, branch_item
->string
);
1068 item
->util
= branch_info
;
1073 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
1075 struct show_info
*show_info
= cb_data
;
1076 struct branch_info
*branch_info
= item
->util
;
1077 struct string_list
*merge
= &branch_info
->merge
;
1078 int width
= show_info
->width
+ 4;
1081 if (branch_info
->rebase
>= REBASE_TRUE
&& branch_info
->merge
.nr
> 1) {
1082 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1087 printf(" %-*s ", show_info
->width
, item
->string
);
1088 if (branch_info
->rebase
>= REBASE_TRUE
) {
1090 if (branch_info
->rebase
== REBASE_INTERACTIVE
)
1091 msg
= _("rebases interactively onto remote %s");
1092 else if (branch_info
->rebase
== REBASE_MERGES
)
1093 msg
= _("rebases interactively (with merges) onto "
1096 msg
= _("rebases onto remote %s");
1097 printf_ln(msg
, merge
->items
[0].string
);
1099 } else if (show_info
->any_rebase
) {
1100 printf_ln(_(" merges with remote %s"), merge
->items
[0].string
);
1103 printf_ln(_("merges with remote %s"), merge
->items
[0].string
);
1105 for (i
= 1; i
< merge
->nr
; i
++)
1106 printf(_("%-*s and with remote %s\n"), width
, "",
1107 merge
->items
[i
].string
);
1112 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
1114 struct show_info
*show_info
= cb_data
;
1115 struct push_info
*push_info
= push_item
->util
;
1116 struct string_list_item
*item
;
1118 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
1119 show_info
->width
= n
;
1120 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
1121 show_info
->width2
= n
;
1122 item
= string_list_append(&show_info
->list
, push_item
->string
);
1123 item
->util
= push_item
->util
;
1128 * Sorting comparison for a string list that has push_info
1129 * structs in its util field
1131 static int cmp_string_with_push(const void *va
, const void *vb
)
1133 const struct string_list_item
*a
= va
;
1134 const struct string_list_item
*b
= vb
;
1135 const struct push_info
*a_push
= a
->util
;
1136 const struct push_info
*b_push
= b
->util
;
1137 int cmp
= strcmp(a
->string
, b
->string
);
1138 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
1141 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
1143 struct show_info
*show_info
= cb_data
;
1144 struct push_info
*push_info
= item
->util
;
1145 const char *src
= item
->string
, *status
= NULL
;
1147 switch (push_info
->status
) {
1148 case PUSH_STATUS_CREATE
:
1149 status
= _("create");
1151 case PUSH_STATUS_DELETE
:
1152 status
= _("delete");
1155 case PUSH_STATUS_UPTODATE
:
1156 status
= _("up to date");
1158 case PUSH_STATUS_FASTFORWARD
:
1159 status
= _("fast-forwardable");
1161 case PUSH_STATUS_OUTOFDATE
:
1162 status
= _("local out of date");
1164 case PUSH_STATUS_NOTQUERIED
:
1168 if (push_info
->forced
)
1169 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info
->width
, src
,
1170 show_info
->width2
, push_info
->dest
, status
);
1172 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info
->width
, src
,
1173 show_info
->width2
, push_info
->dest
, status
);
1175 if (push_info
->forced
)
1176 printf_ln(_(" %-*s forces to %s"), show_info
->width
, src
,
1179 printf_ln(_(" %-*s pushes to %s"), show_info
->width
, src
,
1185 static int get_one_entry(struct remote
*remote
, void *priv
)
1187 struct string_list
*list
= priv
;
1188 struct strbuf remote_info_buf
= STRBUF_INIT
;
1192 if (remote
->url_nr
> 0) {
1193 struct strbuf promisor_config
= STRBUF_INIT
;
1194 const char *partial_clone_filter
= NULL
;
1196 strbuf_addf(&promisor_config
, "remote.%s.partialclonefilter", remote
->name
);
1197 strbuf_addf(&remote_info_buf
, "%s (fetch)", remote
->url
[0]);
1198 if (!git_config_get_string_tmp(promisor_config
.buf
, &partial_clone_filter
))
1199 strbuf_addf(&remote_info_buf
, " [%s]", partial_clone_filter
);
1201 strbuf_release(&promisor_config
);
1202 string_list_append(list
, remote
->name
)->util
=
1203 strbuf_detach(&remote_info_buf
, NULL
);
1205 string_list_append(list
, remote
->name
)->util
= NULL
;
1206 if (remote
->pushurl_nr
) {
1207 url
= remote
->pushurl
;
1208 url_nr
= remote
->pushurl_nr
;
1211 url_nr
= remote
->url_nr
;
1213 for (i
= 0; i
< url_nr
; i
++)
1215 strbuf_addf(&remote_info_buf
, "%s (push)", url
[i
]);
1216 string_list_append(list
, remote
->name
)->util
=
1217 strbuf_detach(&remote_info_buf
, NULL
);
1223 static int show_all(void)
1225 struct string_list list
= STRING_LIST_INIT_DUP
;
1228 result
= for_each_remote(get_one_entry
, &list
);
1233 string_list_sort(&list
);
1234 for (i
= 0; i
< list
.nr
; i
++) {
1235 struct string_list_item
*item
= list
.items
+ i
;
1237 printf("%s\t%s\n", item
->string
,
1238 item
->util
? (const char *)item
->util
: "");
1240 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1242 printf("%s\n", item
->string
);
1246 string_list_clear(&list
, 1);
1250 static int show(int argc
, const char **argv
)
1252 int no_query
= 0, result
= 0, query_flag
= 0;
1253 struct option options
[] = {
1254 OPT_BOOL('n', NULL
, &no_query
, N_("do not query remotes")),
1257 struct show_info info
= SHOW_INFO_INIT
;
1259 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_show_usage
,
1266 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1268 for (; argc
; argc
--, argv
++) {
1273 get_remote_ref_states(*argv
, &info
.states
, query_flag
);
1275 printf_ln(_("* remote %s"), *argv
);
1276 printf_ln(_(" Fetch URL: %s"), info
.states
.remote
->url_nr
> 0 ?
1277 info
.states
.remote
->url
[0] : _("(no URL)"));
1278 if (info
.states
.remote
->pushurl_nr
) {
1279 url
= info
.states
.remote
->pushurl
;
1280 url_nr
= info
.states
.remote
->pushurl_nr
;
1282 url
= info
.states
.remote
->url
;
1283 url_nr
= info
.states
.remote
->url_nr
;
1285 for (i
= 0; i
< url_nr
; i
++)
1287 * TRANSLATORS: the colon ':' should align
1288 * with the one in " Fetch URL: %s"
1291 printf_ln(_(" Push URL: %s"), url
[i
]);
1293 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1295 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1296 else if (!info
.states
.heads
.nr
)
1297 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1298 else if (info
.states
.heads
.nr
== 1)
1299 printf_ln(_(" HEAD branch: %s"), info
.states
.heads
.items
[0].string
);
1301 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1302 " may be one of the following):\n"));
1303 for (i
= 0; i
< info
.states
.heads
.nr
; i
++)
1304 printf(" %s\n", info
.states
.heads
.items
[i
].string
);
1307 /* remote branch info */
1309 for_each_string_list(&info
.states
.new_refs
, add_remote_to_show_info
, &info
);
1310 for_each_string_list(&info
.states
.tracked
, add_remote_to_show_info
, &info
);
1311 for_each_string_list(&info
.states
.stale
, add_remote_to_show_info
, &info
);
1313 printf_ln(Q_(" Remote branch:%s",
1314 " Remote branches:%s",
1316 no_query
? _(" (status not queried)") : "");
1317 for_each_string_list(&info
.list
, show_remote_info_item
, &info
);
1318 string_list_clear(&info
.list
, 0);
1322 info
.any_rebase
= 0;
1323 for_each_string_list(&branch_list
, add_local_to_show_info
, &info
);
1325 printf_ln(Q_(" Local branch configured for 'git pull':",
1326 " Local branches configured for 'git pull':",
1328 for_each_string_list(&info
.list
, show_local_info_item
, &info
);
1329 string_list_clear(&info
.list
, 0);
1332 if (info
.states
.remote
->mirror
)
1333 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1335 info
.width
= info
.width2
= 0;
1336 for_each_string_list(&info
.states
.push
, add_push_to_show_info
, &info
);
1337 QSORT(info
.list
.items
, info
.list
.nr
, cmp_string_with_push
);
1339 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1340 " Local refs configured for 'git push'%s:",
1342 no_query
? _(" (status not queried)") : "");
1343 for_each_string_list(&info
.list
, show_push_info_item
, &info
);
1344 string_list_clear(&info
.list
, 0);
1346 free_remote_ref_states(&info
.states
);
1352 static int set_head(int argc
, const char **argv
)
1354 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1355 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1356 char *head_name
= NULL
;
1358 struct option options
[] = {
1359 OPT_BOOL('a', "auto", &opt_a
,
1360 N_("set refs/remotes/<name>/HEAD according to remote")),
1361 OPT_BOOL('d', "delete", &opt_d
,
1362 N_("delete refs/remotes/<name>/HEAD")),
1365 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_sethead_usage
,
1368 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1370 if (!opt_a
&& !opt_d
&& argc
== 2) {
1371 head_name
= xstrdup(argv
[1]);
1372 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1373 struct ref_states states
= REF_STATES_INIT
;
1374 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1375 if (!states
.heads
.nr
)
1376 result
|= error(_("Cannot determine remote HEAD"));
1377 else if (states
.heads
.nr
> 1) {
1378 result
|= error(_("Multiple remote HEAD branches. "
1379 "Please choose one explicitly with:"));
1380 for (i
= 0; i
< states
.heads
.nr
; i
++)
1381 fprintf(stderr
, " git remote set-head %s %s\n",
1382 argv
[0], states
.heads
.items
[i
].string
);
1384 head_name
= xstrdup(states
.heads
.items
[0].string
);
1385 free_remote_ref_states(&states
);
1386 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1387 if (delete_ref(NULL
, buf
.buf
, NULL
, REF_NO_DEREF
))
1388 result
|= error(_("Could not delete %s"), buf
.buf
);
1390 usage_with_options(builtin_remote_sethead_usage
, options
);
1393 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1394 /* make sure it's valid */
1395 if (!ref_exists(buf2
.buf
))
1396 result
|= error(_("Not a valid ref: %s"), buf2
.buf
);
1397 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1398 result
|= error(_("Could not setup %s"), buf
.buf
);
1400 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1404 strbuf_release(&buf
);
1405 strbuf_release(&buf2
);
1409 static int prune_remote(const char *remote
, int dry_run
)
1412 struct ref_states states
= REF_STATES_INIT
;
1413 struct string_list refs_to_prune
= STRING_LIST_INIT_NODUP
;
1414 struct string_list_item
*item
;
1415 const char *dangling_msg
= dry_run
1416 ? _(" %s will become dangling!")
1417 : _(" %s has become dangling!");
1419 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1421 if (!states
.stale
.nr
) {
1422 free_remote_ref_states(&states
);
1426 printf_ln(_("Pruning %s"), remote
);
1427 printf_ln(_("URL: %s"),
1428 states
.remote
->url_nr
1429 ? states
.remote
->url
[0]
1432 for_each_string_list_item(item
, &states
.stale
)
1433 string_list_append(&refs_to_prune
, item
->util
);
1434 string_list_sort(&refs_to_prune
);
1437 result
|= delete_refs("remote: prune", &refs_to_prune
, 0);
1439 for_each_string_list_item(item
, &states
.stale
) {
1440 const char *refname
= item
->util
;
1443 printf_ln(_(" * [would prune] %s"),
1444 abbrev_ref(refname
, "refs/remotes/"));
1446 printf_ln(_(" * [pruned] %s"),
1447 abbrev_ref(refname
, "refs/remotes/"));
1450 warn_dangling_symrefs(stdout
, dangling_msg
, &refs_to_prune
);
1452 string_list_clear(&refs_to_prune
, 0);
1453 free_remote_ref_states(&states
);
1457 static int prune(int argc
, const char **argv
)
1459 int dry_run
= 0, result
= 0;
1460 struct option options
[] = {
1461 OPT__DRY_RUN(&dry_run
, N_("dry run")),
1465 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_prune_usage
,
1469 usage_with_options(builtin_remote_prune_usage
, options
);
1471 for (; argc
; argc
--, argv
++)
1472 result
|= prune_remote(*argv
, dry_run
);
1477 static int get_remote_default(const char *key
, const char *value
, void *priv
)
1479 if (strcmp(key
, "remotes.default") == 0) {
1486 static int update(int argc
, const char **argv
)
1489 struct option options
[] = {
1490 OPT_BOOL('p', "prune", &prune
,
1491 N_("prune remotes after fetching")),
1494 struct strvec fetch_argv
= STRVEC_INIT
;
1495 int default_defined
= 0;
1498 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_update_usage
,
1499 PARSE_OPT_KEEP_ARGV0
);
1501 strvec_push(&fetch_argv
, "fetch");
1504 strvec_push(&fetch_argv
, prune
? "--prune" : "--no-prune");
1506 strvec_push(&fetch_argv
, "-v");
1507 strvec_push(&fetch_argv
, "--multiple");
1509 strvec_push(&fetch_argv
, "default");
1510 for (i
= 1; i
< argc
; i
++)
1511 strvec_push(&fetch_argv
, argv
[i
]);
1513 if (strcmp(fetch_argv
.v
[fetch_argv
.nr
-1], "default") == 0) {
1514 git_config(get_remote_default
, &default_defined
);
1515 if (!default_defined
) {
1516 strvec_pop(&fetch_argv
);
1517 strvec_push(&fetch_argv
, "--all");
1521 retval
= run_command_v_opt(fetch_argv
.v
, RUN_GIT_CMD
);
1522 strvec_clear(&fetch_argv
);
1526 static int remove_all_fetch_refspecs(const char *key
)
1528 return git_config_set_multivar_gently(key
, NULL
, NULL
,
1529 CONFIG_FLAGS_MULTI_REPLACE
);
1532 static void add_branches(struct remote
*remote
, const char **branches
,
1535 const char *remotename
= remote
->name
;
1536 int mirror
= remote
->mirror
;
1537 struct strbuf refspec
= STRBUF_INIT
;
1539 for (; *branches
; branches
++)
1540 add_branch(key
, *branches
, remotename
, mirror
, &refspec
);
1542 strbuf_release(&refspec
);
1545 static int set_remote_branches(const char *remotename
, const char **branches
,
1548 struct strbuf key
= STRBUF_INIT
;
1549 struct remote
*remote
;
1551 strbuf_addf(&key
, "remote.%s.fetch", remotename
);
1553 remote
= remote_get(remotename
);
1554 if (!remote_is_configured(remote
, 1)) {
1555 error(_("No such remote '%s'"), remotename
);
1559 if (!add_mode
&& remove_all_fetch_refspecs(key
.buf
)) {
1560 strbuf_release(&key
);
1563 add_branches(remote
, branches
, key
.buf
);
1565 strbuf_release(&key
);
1569 static int set_branches(int argc
, const char **argv
)
1572 struct option options
[] = {
1573 OPT_BOOL('\0', "add", &add_mode
, N_("add branch")),
1577 argc
= parse_options(argc
, argv
, NULL
, options
,
1578 builtin_remote_setbranches_usage
, 0);
1580 error(_("no remote specified"));
1581 usage_with_options(builtin_remote_setbranches_usage
, options
);
1585 return set_remote_branches(argv
[0], argv
+ 1, add_mode
);
1588 static int get_url(int argc
, const char **argv
)
1590 int i
, push_mode
= 0, all_mode
= 0;
1591 const char *remotename
= NULL
;
1592 struct remote
*remote
;
1595 struct option options
[] = {
1596 OPT_BOOL('\0', "push", &push_mode
,
1597 N_("query push URLs rather than fetch URLs")),
1598 OPT_BOOL('\0', "all", &all_mode
,
1599 N_("return all URLs")),
1602 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_geturl_usage
, 0);
1605 usage_with_options(builtin_remote_geturl_usage
, options
);
1607 remotename
= argv
[0];
1609 remote
= remote_get(remotename
);
1610 if (!remote_is_configured(remote
, 1)) {
1611 error(_("No such remote '%s'"), remotename
);
1617 url
= remote
->pushurl
;
1618 url_nr
= remote
->pushurl_nr
;
1620 /* else fetch mode */
1622 /* Use the fetch URL when no push URLs were found or requested. */
1625 url_nr
= remote
->url_nr
;
1629 die(_("no URLs configured for remote '%s'"), remotename
);
1632 for (i
= 0; i
< url_nr
; i
++)
1633 printf_ln("%s", url
[i
]);
1635 printf_ln("%s", *url
);
1641 static int set_url(int argc
, const char **argv
)
1643 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1644 int matches
= 0, negative_matches
= 0;
1645 const char *remotename
= NULL
;
1646 const char *newurl
= NULL
;
1647 const char *oldurl
= NULL
;
1648 struct remote
*remote
;
1650 const char **urlset
;
1652 struct strbuf name_buf
= STRBUF_INIT
;
1653 struct option options
[] = {
1654 OPT_BOOL('\0', "push", &push_mode
,
1655 N_("manipulate push URLs")),
1656 OPT_BOOL('\0', "add", &add_mode
,
1658 OPT_BOOL('\0', "delete", &delete_mode
,
1662 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_seturl_usage
,
1663 PARSE_OPT_KEEP_ARGV0
);
1665 if (add_mode
&& delete_mode
)
1666 die(_("--add --delete doesn't make sense"));
1668 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1669 usage_with_options(builtin_remote_seturl_usage
, options
);
1671 remotename
= argv
[1];
1679 remote
= remote_get(remotename
);
1680 if (!remote_is_configured(remote
, 1)) {
1681 error(_("No such remote '%s'"), remotename
);
1686 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1687 urlset
= remote
->pushurl
;
1688 urlset_nr
= remote
->pushurl_nr
;
1690 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1691 urlset
= remote
->url
;
1692 urlset_nr
= remote
->url_nr
;
1695 /* Special cases that add new entry. */
1696 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1698 git_config_set_multivar(name_buf
.buf
, newurl
,
1701 git_config_set(name_buf
.buf
, newurl
);
1705 /* Old URL specified. Demand that one matches. */
1706 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1707 die(_("Invalid old URL pattern: %s"), oldurl
);
1709 for (i
= 0; i
< urlset_nr
; i
++)
1710 if (!regexec(&old_regex
, urlset
[i
], 0, NULL
, 0))
1714 if (!delete_mode
&& !matches
)
1715 die(_("No such URL found: %s"), oldurl
);
1716 if (delete_mode
&& !negative_matches
&& !push_mode
)
1717 die(_("Will not delete all non-push URLs"));
1719 regfree(&old_regex
);
1722 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1724 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
,
1725 CONFIG_FLAGS_MULTI_REPLACE
);
1727 strbuf_release(&name_buf
);
1731 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1733 struct option options
[] = {
1734 OPT__VERBOSE(&verbose
, N_("be verbose; must be placed before a subcommand")),
1739 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1740 PARSE_OPT_STOP_AT_NON_OPTION
);
1743 result
= show_all();
1744 else if (!strcmp(argv
[0], "add"))
1745 result
= add(argc
, argv
);
1746 else if (!strcmp(argv
[0], "rename"))
1747 result
= mv(argc
, argv
);
1748 else if (!strcmp(argv
[0], "rm") || !strcmp(argv
[0], "remove"))
1749 result
= rm(argc
, argv
);
1750 else if (!strcmp(argv
[0], "set-head"))
1751 result
= set_head(argc
, argv
);
1752 else if (!strcmp(argv
[0], "set-branches"))
1753 result
= set_branches(argc
, argv
);
1754 else if (!strcmp(argv
[0], "get-url"))
1755 result
= get_url(argc
, argv
);
1756 else if (!strcmp(argv
[0], "set-url"))
1757 result
= set_url(argc
, argv
);
1758 else if (!strcmp(argv
[0], "show"))
1759 result
= show(argc
, argv
);
1760 else if (!strcmp(argv
[0], "prune"))
1761 result
= prune(argc
, argv
);
1762 else if (!strcmp(argv
[0], "update"))
1763 result
= update(argc
, argv
);
1765 error(_("Unknown subcommand: %s"), argv
[0]);
1766 usage_with_options(builtin_remote_usage
, options
);
1769 return result
? 1 : 0;