4 #include "parse-options.h"
7 #include "string-list.h"
9 #include "run-command.h"
13 #include "object-store.h"
15 #include "commit-reach.h"
18 static const char * const builtin_remote_usage
[] = {
19 "git remote [-v | --verbose]",
20 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
21 N_("git remote rename [--[no-]progress] <old> <new>"),
22 N_("git remote remove <name>"),
23 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
24 N_("git remote [-v | --verbose] show [-n] <name>"),
25 N_("git remote prune [-n | --dry-run] <name>"),
26 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
27 N_("git remote set-branches [--add] <name> <branch>..."),
28 N_("git remote get-url [--push] [--all] <name>"),
29 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
30 N_("git remote set-url --add <name> <newurl>"),
31 N_("git remote set-url --delete <name> <url>"),
35 static const char * const builtin_remote_add_usage
[] = {
36 N_("git remote add [<options>] <name> <url>"),
40 static const char * const builtin_remote_rename_usage
[] = {
41 N_("git remote rename [--[no-]progress] <old> <new>"),
45 static const char * const builtin_remote_rm_usage
[] = {
46 N_("git remote remove <name>"),
50 static const char * const builtin_remote_sethead_usage
[] = {
51 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
55 static const char * const builtin_remote_setbranches_usage
[] = {
56 N_("git remote set-branches <name> <branch>..."),
57 N_("git remote set-branches --add <name> <branch>..."),
61 static const char * const builtin_remote_show_usage
[] = {
62 N_("git remote show [<options>] <name>"),
66 static const char * const builtin_remote_prune_usage
[] = {
67 N_("git remote prune [<options>] <name>"),
71 static const char * const builtin_remote_update_usage
[] = {
72 N_("git remote update [<options>] [<group> | <remote>]..."),
76 static const char * const builtin_remote_geturl_usage
[] = {
77 N_("git remote get-url [--push] [--all] <name>"),
81 static const char * const builtin_remote_seturl_usage
[] = {
82 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
83 N_("git remote set-url --add <name> <newurl>"),
84 N_("git remote set-url --delete <name> <url>"),
88 #define GET_REF_STATES (1<<0)
89 #define GET_HEAD_NAMES (1<<1)
90 #define GET_PUSH_REF_STATES (1<<2)
94 static int fetch_remote(const char *name
)
96 struct child_process cmd
= CHILD_PROCESS_INIT
;
98 strvec_push(&cmd
.args
, "fetch");
100 strvec_push(&cmd
.args
, "-v");
101 strvec_push(&cmd
.args
, name
);
103 printf_ln(_("Updating %s"), name
);
104 if (run_command(&cmd
))
105 return error(_("Could not fetch %s"), name
);
115 #define MIRROR_NONE 0
116 #define MIRROR_FETCH 1
117 #define MIRROR_PUSH 2
118 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
120 static void add_branch(const char *key
, const char *branchname
,
121 const char *remotename
, int mirror
, struct strbuf
*tmp
)
124 strbuf_addch(tmp
, '+');
126 strbuf_addf(tmp
, "refs/%s:refs/%s",
127 branchname
, branchname
);
129 strbuf_addf(tmp
, "refs/heads/%s:refs/remotes/%s/%s",
130 branchname
, remotename
, branchname
);
131 git_config_set_multivar(key
, tmp
->buf
, "^$", 0);
134 static const char mirror_advice
[] =
135 N_("--mirror is dangerous and deprecated; please\n"
136 "\t use --mirror=fetch or --mirror=push instead");
138 static int parse_mirror_opt(const struct option
*opt
, const char *arg
, int not)
140 unsigned *mirror
= opt
->value
;
142 *mirror
= MIRROR_NONE
;
144 warning("%s", _(mirror_advice
));
145 *mirror
= MIRROR_BOTH
;
147 else if (!strcmp(arg
, "fetch"))
148 *mirror
= MIRROR_FETCH
;
149 else if (!strcmp(arg
, "push"))
150 *mirror
= MIRROR_PUSH
;
152 return error(_("unknown mirror argument: %s"), arg
);
156 static int add(int argc
, const char **argv
, const char *prefix
)
158 int fetch
= 0, fetch_tags
= TAGS_DEFAULT
;
159 unsigned mirror
= MIRROR_NONE
;
160 struct string_list track
= STRING_LIST_INIT_NODUP
;
161 const char *master
= NULL
;
162 struct remote
*remote
;
163 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
164 const char *name
, *url
;
167 struct option options
[] = {
168 OPT_BOOL('f', "fetch", &fetch
, N_("fetch the remote branches")),
169 OPT_SET_INT(0, "tags", &fetch_tags
,
170 N_("import all tags and associated objects when fetching"),
172 OPT_SET_INT(0, NULL
, &fetch_tags
,
173 N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET
),
174 OPT_STRING_LIST('t', "track", &track
, N_("branch"),
175 N_("branch(es) to track")),
176 OPT_STRING('m', "master", &master
, N_("branch"), N_("master branch")),
177 OPT_CALLBACK_F(0, "mirror", &mirror
, "(push|fetch)",
178 N_("set up remote as a mirror to push to or fetch from"),
179 PARSE_OPT_OPTARG
| PARSE_OPT_COMP_ARG
, parse_mirror_opt
),
183 argc
= parse_options(argc
, argv
, prefix
, options
,
184 builtin_remote_add_usage
, 0);
187 usage_with_options(builtin_remote_add_usage
, options
);
189 if (mirror
&& master
)
190 die(_("specifying a master branch makes no sense with --mirror"));
191 if (mirror
&& !(mirror
& MIRROR_FETCH
) && track
.nr
)
192 die(_("specifying branches to track makes sense only with fetch mirrors"));
197 remote
= remote_get(name
);
198 if (remote_is_configured(remote
, 1)) {
199 error(_("remote %s already exists."), name
);
203 if (!valid_remote_name(name
))
204 die(_("'%s' is not a valid remote name"), name
);
206 strbuf_addf(&buf
, "remote.%s.url", name
);
207 git_config_set(buf
.buf
, url
);
209 if (!mirror
|| mirror
& MIRROR_FETCH
) {
211 strbuf_addf(&buf
, "remote.%s.fetch", name
);
213 string_list_append(&track
, "*");
214 for (i
= 0; i
< track
.nr
; i
++) {
215 add_branch(buf
.buf
, track
.items
[i
].string
,
216 name
, mirror
, &buf2
);
220 if (mirror
& MIRROR_PUSH
) {
222 strbuf_addf(&buf
, "remote.%s.mirror", name
);
223 git_config_set(buf
.buf
, "true");
226 if (fetch_tags
!= TAGS_DEFAULT
) {
228 strbuf_addf(&buf
, "remote.%s.tagOpt", name
);
229 git_config_set(buf
.buf
,
230 fetch_tags
== TAGS_SET
? "--tags" : "--no-tags");
233 if (fetch
&& fetch_remote(name
))
238 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
241 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
243 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
244 return error(_("Could not setup master '%s'"), master
);
247 strbuf_release(&buf
);
248 strbuf_release(&buf2
);
249 string_list_clear(&track
, 0);
256 struct string_list merge
;
257 enum rebase_type rebase
;
258 char *push_remote_name
;
261 static struct string_list branch_list
= STRING_LIST_INIT_NODUP
;
263 static const char *abbrev_ref(const char *name
, const char *prefix
)
265 skip_prefix(name
, prefix
, &name
);
268 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
270 static int config_read_branches(const char *key
, const char *value
,
273 const char *orig_key
= key
;
275 struct string_list_item
*item
;
276 struct branch_info
*info
;
277 enum { REMOTE
, MERGE
, REBASE
, PUSH_REMOTE
} type
;
280 if (!starts_with(key
, "branch."))
283 key
+= strlen("branch.");
284 if (strip_suffix(key
, ".remote", &key_len
))
286 else if (strip_suffix(key
, ".merge", &key_len
))
288 else if (strip_suffix(key
, ".rebase", &key_len
))
290 else if (strip_suffix(key
, ".pushremote", &key_len
))
294 name
= xmemdupz(key
, key_len
);
296 item
= string_list_insert(&branch_list
, name
);
299 item
->util
= xcalloc(1, sizeof(struct branch_info
));
303 if (info
->remote_name
)
304 warning(_("more than one %s"), orig_key
);
305 info
->remote_name
= xstrdup(value
);
308 char *space
= strchr(value
, ' ');
309 value
= abbrev_branch(value
);
312 merge
= xstrndup(value
, space
- value
);
313 string_list_append(&info
->merge
, merge
);
314 value
= abbrev_branch(space
+ 1);
315 space
= strchr(value
, ' ');
317 string_list_append(&info
->merge
, xstrdup(value
));
322 * Consider invalid values as false and check the
323 * truth value with >= REBASE_TRUE.
325 info
->rebase
= rebase_parse_value(value
);
326 if (info
->rebase
== REBASE_INVALID
)
327 warning(_("unhandled branch.%s.rebase=%s; assuming "
328 "'true'"), name
, value
);
331 if (info
->push_remote_name
)
332 warning(_("more than one %s"), orig_key
);
333 info
->push_remote_name
= xstrdup(value
);
336 BUG("unexpected type=%d", type
);
342 static void read_branches(void)
346 git_config(config_read_branches
, NULL
);
350 struct remote
*remote
;
351 struct string_list new_refs
, skipped
, stale
, tracked
, heads
, push
;
355 #define REF_STATES_INIT { \
356 .new_refs = STRING_LIST_INIT_DUP, \
357 .skipped = STRING_LIST_INIT_DUP, \
358 .stale = STRING_LIST_INIT_DUP, \
359 .tracked = STRING_LIST_INIT_DUP, \
360 .heads = STRING_LIST_INIT_DUP, \
361 .push = STRING_LIST_INIT_DUP, \
364 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
366 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
367 struct ref
*ref
, *stale_refs
;
370 for (i
= 0; i
< states
->remote
->fetch
.nr
; i
++)
371 if (get_fetch_map(remote_refs
, &states
->remote
->fetch
.items
[i
], &tail
, 1))
372 die(_("Could not get fetch map for refspec %s"),
373 states
->remote
->fetch
.raw
[i
]);
375 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
376 if (omit_name_by_refspec(ref
->name
, &states
->remote
->fetch
))
377 string_list_append(&states
->skipped
, abbrev_branch(ref
->name
));
378 else if (!ref
->peer_ref
|| !ref_exists(ref
->peer_ref
->name
))
379 string_list_append(&states
->new_refs
, abbrev_branch(ref
->name
));
381 string_list_append(&states
->tracked
, abbrev_branch(ref
->name
));
383 stale_refs
= get_stale_heads(&states
->remote
->fetch
, fetch_map
);
384 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
385 struct string_list_item
*item
=
386 string_list_append(&states
->stale
, abbrev_branch(ref
->name
));
387 item
->util
= xstrdup(ref
->name
);
389 free_refs(stale_refs
);
390 free_refs(fetch_map
);
392 string_list_sort(&states
->new_refs
);
393 string_list_sort(&states
->skipped
);
394 string_list_sort(&states
->tracked
);
395 string_list_sort(&states
->stale
);
404 PUSH_STATUS_CREATE
= 0,
406 PUSH_STATUS_UPTODATE
,
407 PUSH_STATUS_FASTFORWARD
,
408 PUSH_STATUS_OUTOFDATE
,
409 PUSH_STATUS_NOTQUERIED
413 static int get_push_ref_states(const struct ref
*remote_refs
,
414 struct ref_states
*states
)
416 struct remote
*remote
= states
->remote
;
417 struct ref
*ref
, *local_refs
, *push_map
;
421 local_refs
= get_local_heads();
422 push_map
= copy_ref_list(remote_refs
);
424 match_push_refs(local_refs
, &push_map
, &remote
->push
, MATCH_REFS_NONE
);
426 for (ref
= push_map
; ref
; ref
= ref
->next
) {
427 struct string_list_item
*item
;
428 struct push_info
*info
;
432 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
434 item
= string_list_append(&states
->push
,
435 abbrev_branch(ref
->peer_ref
->name
));
436 item
->util
= xcalloc(1, sizeof(struct push_info
));
438 info
->forced
= ref
->force
;
439 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
441 if (is_null_oid(&ref
->new_oid
)) {
442 info
->status
= PUSH_STATUS_DELETE
;
443 } else if (oideq(&ref
->old_oid
, &ref
->new_oid
))
444 info
->status
= PUSH_STATUS_UPTODATE
;
445 else if (is_null_oid(&ref
->old_oid
))
446 info
->status
= PUSH_STATUS_CREATE
;
447 else if (repo_has_object_file(the_repository
, &ref
->old_oid
) &&
448 ref_newer(&ref
->new_oid
, &ref
->old_oid
))
449 info
->status
= PUSH_STATUS_FASTFORWARD
;
451 info
->status
= PUSH_STATUS_OUTOFDATE
;
453 free_refs(local_refs
);
458 static int get_push_ref_states_noquery(struct ref_states
*states
)
461 struct remote
*remote
= states
->remote
;
462 struct string_list_item
*item
;
463 struct push_info
*info
;
468 if (!remote
->push
.nr
) {
469 item
= string_list_append(&states
->push
, _("(matching)"));
470 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
471 info
->status
= PUSH_STATUS_NOTQUERIED
;
472 info
->dest
= xstrdup(item
->string
);
474 for (i
= 0; i
< remote
->push
.nr
; i
++) {
475 const struct refspec_item
*spec
= &remote
->push
.items
[i
];
477 item
= string_list_append(&states
->push
, _("(matching)"));
478 else if (strlen(spec
->src
))
479 item
= string_list_append(&states
->push
, spec
->src
);
481 item
= string_list_append(&states
->push
, _("(delete)"));
483 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
484 info
->forced
= spec
->force
;
485 info
->status
= PUSH_STATUS_NOTQUERIED
;
486 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
491 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
493 struct ref
*ref
, *matches
;
494 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
495 struct refspec_item refspec
;
497 memset(&refspec
, 0, sizeof(refspec
));
500 refspec
.src
= refspec
.dst
= "refs/heads/*";
501 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
502 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
504 for (ref
= matches
; ref
; ref
= ref
->next
)
505 string_list_append(&states
->heads
, abbrev_branch(ref
->name
));
507 free_refs(fetch_map
);
513 struct known_remote
{
514 struct known_remote
*next
;
515 struct remote
*remote
;
518 struct known_remotes
{
519 struct remote
*to_delete
;
520 struct known_remote
*list
;
523 static int add_known_remote(struct remote
*remote
, void *cb_data
)
525 struct known_remotes
*all
= cb_data
;
526 struct known_remote
*r
;
528 if (!strcmp(all
->to_delete
->name
, remote
->name
))
531 r
= xmalloc(sizeof(*r
));
538 struct branches_for_remote
{
539 struct remote
*remote
;
540 struct string_list
*branches
, *skipped
;
541 struct known_remotes
*keep
;
544 static int add_branch_for_removal(const char *refname
,
545 const struct object_id
*oid UNUSED
,
546 int flags UNUSED
, void *cb_data
)
548 struct branches_for_remote
*branches
= cb_data
;
549 struct refspec_item refspec
;
550 struct known_remote
*kr
;
552 memset(&refspec
, 0, sizeof(refspec
));
553 refspec
.dst
= (char *)refname
;
554 if (remote_find_tracking(branches
->remote
, &refspec
))
557 /* don't delete a branch if another remote also uses it */
558 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
559 memset(&refspec
, 0, sizeof(refspec
));
560 refspec
.dst
= (char *)refname
;
561 if (!remote_find_tracking(kr
->remote
, &refspec
))
565 /* don't delete non-remote-tracking refs */
566 if (!starts_with(refname
, "refs/remotes/")) {
567 /* advise user how to delete local branches */
568 if (starts_with(refname
, "refs/heads/"))
569 string_list_append(branches
->skipped
,
570 abbrev_branch(refname
));
571 /* silently skip over other non-remote refs */
575 string_list_append(branches
->branches
, refname
);
581 const char *old_name
;
582 const char *new_name
;
583 struct string_list
*remote_branches
;
587 static int read_remote_branches(const char *refname
,
588 const struct object_id
*oid UNUSED
,
589 int flags UNUSED
, void *cb_data
)
591 struct rename_info
*rename
= cb_data
;
592 struct strbuf buf
= STRBUF_INIT
;
593 struct string_list_item
*item
;
597 strbuf_addf(&buf
, "refs/remotes/%s/", rename
->old_name
);
598 if (starts_with(refname
, buf
.buf
)) {
599 item
= string_list_append(rename
->remote_branches
, refname
);
600 symref
= resolve_ref_unsafe(refname
, RESOLVE_REF_READING
,
602 if (symref
&& (flag
& REF_ISSYMREF
)) {
603 item
->util
= xstrdup(symref
);
604 rename
->symrefs_nr
++;
609 strbuf_release(&buf
);
614 static int migrate_file(struct remote
*remote
)
616 struct strbuf buf
= STRBUF_INIT
;
619 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
620 for (i
= 0; i
< remote
->url_nr
; i
++)
621 git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0);
623 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
624 for (i
= 0; i
< remote
->push
.raw_nr
; i
++)
625 git_config_set_multivar(buf
.buf
, remote
->push
.raw
[i
], "^$", 0);
627 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
628 for (i
= 0; i
< remote
->fetch
.raw_nr
; i
++)
629 git_config_set_multivar(buf
.buf
, remote
->fetch
.raw
[i
], "^$", 0);
630 if (remote
->origin
== REMOTE_REMOTES
)
631 unlink_or_warn(git_path("remotes/%s", remote
->name
));
632 else if (remote
->origin
== REMOTE_BRANCHES
)
633 unlink_or_warn(git_path("branches/%s", remote
->name
));
634 strbuf_release(&buf
);
639 struct push_default_info
641 const char *old_name
;
642 enum config_scope scope
;
643 struct strbuf origin
;
647 static int config_read_push_default(const char *key
, const char *value
,
650 struct push_default_info
* info
= cb
;
651 if (strcmp(key
, "remote.pushdefault") ||
652 !value
|| strcmp(value
, info
->old_name
))
655 info
->scope
= current_config_scope();
656 strbuf_reset(&info
->origin
);
657 strbuf_addstr(&info
->origin
, current_config_name());
658 info
->linenr
= current_config_line();
663 static void handle_push_default(const char* old_name
, const char* new_name
)
665 struct push_default_info push_default
= {
666 old_name
, CONFIG_SCOPE_UNKNOWN
, STRBUF_INIT
, -1 };
667 git_config(config_read_push_default
, &push_default
);
668 if (push_default
.scope
>= CONFIG_SCOPE_COMMAND
)
670 else if (push_default
.scope
>= CONFIG_SCOPE_LOCAL
) {
671 int result
= git_config_set_gently("remote.pushDefault",
673 if (new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
674 die(_("could not set '%s'"), "remote.pushDefault");
675 else if (!new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
676 die(_("could not unset '%s'"), "remote.pushDefault");
677 } else if (push_default
.scope
>= CONFIG_SCOPE_SYSTEM
) {
679 warning(_("The %s configuration remote.pushDefault in:\n"
681 "now names the non-existent remote '%s'"),
682 config_scope_name(push_default
.scope
),
683 push_default
.origin
.buf
, push_default
.linenr
,
689 static int mv(int argc
, const char **argv
, const char *prefix
)
691 int show_progress
= isatty(2);
692 struct option options
[] = {
693 OPT_BOOL(0, "progress", &show_progress
, N_("force progress reporting")),
696 struct remote
*oldremote
, *newremote
;
697 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
,
698 old_remote_context
= STRBUF_INIT
;
699 struct string_list remote_branches
= STRING_LIST_INIT_DUP
;
700 struct rename_info rename
;
701 int i
, refs_renamed_nr
= 0, refspec_updated
= 0;
702 struct progress
*progress
= NULL
;
704 argc
= parse_options(argc
, argv
, prefix
, options
,
705 builtin_remote_rename_usage
, 0);
708 usage_with_options(builtin_remote_rename_usage
, options
);
710 rename
.old_name
= argv
[0];
711 rename
.new_name
= argv
[1];
712 rename
.remote_branches
= &remote_branches
;
713 rename
.symrefs_nr
= 0;
715 oldremote
= remote_get(rename
.old_name
);
716 if (!remote_is_configured(oldremote
, 1)) {
717 error(_("No such remote: '%s'"), rename
.old_name
);
721 if (!strcmp(rename
.old_name
, rename
.new_name
) && oldremote
->origin
!= REMOTE_CONFIG
)
722 return migrate_file(oldremote
);
724 newremote
= remote_get(rename
.new_name
);
725 if (remote_is_configured(newremote
, 1)) {
726 error(_("remote %s already exists."), rename
.new_name
);
730 if (!valid_remote_name(rename
.new_name
))
731 die(_("'%s' is not a valid remote name"), rename
.new_name
);
733 strbuf_addf(&buf
, "remote.%s", rename
.old_name
);
734 strbuf_addf(&buf2
, "remote.%s", rename
.new_name
);
735 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
736 return error(_("Could not rename config section '%s' to '%s'"),
739 if (oldremote
->fetch
.raw_nr
) {
741 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new_name
);
742 git_config_set_multivar(buf
.buf
, NULL
, NULL
, CONFIG_FLAGS_MULTI_REPLACE
);
743 strbuf_addf(&old_remote_context
, ":refs/remotes/%s/", rename
.old_name
);
744 for (i
= 0; i
< oldremote
->fetch
.raw_nr
; i
++) {
748 strbuf_addstr(&buf2
, oldremote
->fetch
.raw
[i
]);
749 ptr
= strstr(buf2
.buf
, old_remote_context
.buf
);
753 ptr
-buf2
.buf
+ strlen(":refs/remotes/"),
754 strlen(rename
.old_name
), rename
.new_name
,
755 strlen(rename
.new_name
));
757 warning(_("Not updating non-default fetch refspec\n"
759 "\tPlease update the configuration manually if necessary."),
762 git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0);
767 for (i
= 0; i
< branch_list
.nr
; i
++) {
768 struct string_list_item
*item
= branch_list
.items
+ i
;
769 struct branch_info
*info
= item
->util
;
770 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old_name
)) {
772 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
773 git_config_set(buf
.buf
, rename
.new_name
);
775 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, rename
.old_name
)) {
777 strbuf_addf(&buf
, "branch.%s.pushRemote", item
->string
);
778 git_config_set(buf
.buf
, rename
.new_name
);
782 if (!refspec_updated
)
786 * First remove symrefs, then rename the rest, finally create
789 for_each_ref(read_remote_branches
, &rename
);
792 * Count symrefs twice, since "renaming" them is done by
793 * deleting and recreating them in two separate passes.
795 progress
= start_progress(_("Renaming remote references"),
796 rename
.remote_branches
->nr
+ rename
.symrefs_nr
);
798 for (i
= 0; i
< remote_branches
.nr
; i
++) {
799 struct string_list_item
*item
= remote_branches
.items
+ i
;
800 struct strbuf referent
= STRBUF_INIT
;
802 if (refs_read_symbolic_ref(get_main_ref_store(the_repository
), item
->string
,
805 if (delete_ref(NULL
, item
->string
, NULL
, REF_NO_DEREF
))
806 die(_("deleting '%s' failed"), item
->string
);
808 strbuf_release(&referent
);
809 display_progress(progress
, ++refs_renamed_nr
);
811 for (i
= 0; i
< remote_branches
.nr
; i
++) {
812 struct string_list_item
*item
= remote_branches
.items
+ i
;
817 strbuf_addstr(&buf
, item
->string
);
818 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
819 rename
.new_name
, strlen(rename
.new_name
));
821 strbuf_addf(&buf2
, "remote: renamed %s to %s",
822 item
->string
, buf
.buf
);
823 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
824 die(_("renaming '%s' failed"), item
->string
);
825 display_progress(progress
, ++refs_renamed_nr
);
827 for (i
= 0; i
< remote_branches
.nr
; i
++) {
828 struct string_list_item
*item
= remote_branches
.items
+ i
;
833 strbuf_addstr(&buf
, item
->string
);
834 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
835 rename
.new_name
, strlen(rename
.new_name
));
837 strbuf_addstr(&buf2
, item
->util
);
838 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old_name
),
839 rename
.new_name
, strlen(rename
.new_name
));
841 strbuf_addf(&buf3
, "remote: renamed %s to %s",
842 item
->string
, buf
.buf
);
843 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
844 die(_("creating '%s' failed"), buf
.buf
);
845 display_progress(progress
, ++refs_renamed_nr
);
847 stop_progress(&progress
);
848 string_list_clear(&remote_branches
, 1);
850 handle_push_default(rename
.old_name
, rename
.new_name
);
855 static int rm(int argc
, const char **argv
, const char *prefix
)
857 struct option options
[] = {
860 struct remote
*remote
;
861 struct strbuf buf
= STRBUF_INIT
;
862 struct known_remotes known_remotes
= { NULL
, NULL
};
863 struct string_list branches
= STRING_LIST_INIT_DUP
;
864 struct string_list skipped
= STRING_LIST_INIT_DUP
;
865 struct branches_for_remote cb_data
;
868 memset(&cb_data
, 0, sizeof(cb_data
));
869 cb_data
.branches
= &branches
;
870 cb_data
.skipped
= &skipped
;
871 cb_data
.keep
= &known_remotes
;
873 argc
= parse_options(argc
, argv
, prefix
, options
,
874 builtin_remote_rm_usage
, 0);
876 usage_with_options(builtin_remote_rm_usage
, options
);
878 remote
= remote_get(argv
[0]);
879 if (!remote_is_configured(remote
, 1)) {
880 error(_("No such remote: '%s'"), argv
[0]);
884 known_remotes
.to_delete
= remote
;
885 for_each_remote(add_known_remote
, &known_remotes
);
888 for (i
= 0; i
< branch_list
.nr
; i
++) {
889 struct string_list_item
*item
= branch_list
.items
+ i
;
890 struct branch_info
*info
= item
->util
;
891 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
892 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
893 for (k
= keys
; *k
; k
++) {
895 strbuf_addf(&buf
, "branch.%s.%s",
897 result
= git_config_set_gently(buf
.buf
, NULL
);
898 if (result
&& result
!= CONFIG_NOTHING_SET
)
899 die(_("could not unset '%s'"), buf
.buf
);
902 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, remote
->name
)) {
904 strbuf_addf(&buf
, "branch.%s.pushremote", item
->string
);
905 result
= git_config_set_gently(buf
.buf
, NULL
);
906 if (result
&& result
!= CONFIG_NOTHING_SET
)
907 die(_("could not unset '%s'"), buf
.buf
);
912 * We cannot just pass a function to for_each_ref() which deletes
913 * the branches one by one, since for_each_ref() relies on cached
914 * refs, which are invalidated when deleting a branch.
916 cb_data
.remote
= remote
;
917 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
918 strbuf_release(&buf
);
921 result
= delete_refs("remote: remove", &branches
, REF_NO_DEREF
);
922 string_list_clear(&branches
, 0);
926 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
927 "to delete it, use:",
928 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
929 "to delete them, use:",
931 for (i
= 0; i
< skipped
.nr
; i
++)
932 fprintf(stderr
, " git branch -d %s\n",
933 skipped
.items
[i
].string
);
935 string_list_clear(&skipped
, 0);
938 strbuf_addf(&buf
, "remote.%s", remote
->name
);
939 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
940 return error(_("Could not remove config section '%s'"), buf
.buf
);
942 handle_push_default(remote
->name
, NULL
);
948 static void clear_push_info(void *util
, const char *string UNUSED
)
950 struct push_info
*info
= util
;
955 static void free_remote_ref_states(struct ref_states
*states
)
957 string_list_clear(&states
->new_refs
, 0);
958 string_list_clear(&states
->skipped
, 0);
959 string_list_clear(&states
->stale
, 1);
960 string_list_clear(&states
->tracked
, 0);
961 string_list_clear(&states
->heads
, 0);
962 string_list_clear_func(&states
->push
, clear_push_info
);
965 static int append_ref_to_tracked_list(const char *refname
,
966 const struct object_id
*oid UNUSED
,
967 int flags
, void *cb_data
)
969 struct ref_states
*states
= cb_data
;
970 struct refspec_item refspec
;
972 if (flags
& REF_ISSYMREF
)
975 memset(&refspec
, 0, sizeof(refspec
));
976 refspec
.dst
= (char *)refname
;
977 if (!remote_find_tracking(states
->remote
, &refspec
))
978 string_list_append(&states
->tracked
, abbrev_branch(refspec
.src
));
983 static int get_remote_ref_states(const char *name
,
984 struct ref_states
*states
,
987 states
->remote
= remote_get(name
);
989 return error(_("No such remote: '%s'"), name
);
994 struct transport
*transport
;
995 const struct ref
*remote_refs
;
997 transport
= transport_get(states
->remote
, states
->remote
->url_nr
> 0 ?
998 states
->remote
->url
[0] : NULL
);
999 remote_refs
= transport_get_remote_refs(transport
, NULL
);
1001 states
->queried
= 1;
1002 if (query
& GET_REF_STATES
)
1003 get_ref_states(remote_refs
, states
);
1004 if (query
& GET_HEAD_NAMES
)
1005 get_head_names(remote_refs
, states
);
1006 if (query
& GET_PUSH_REF_STATES
)
1007 get_push_ref_states(remote_refs
, states
);
1008 transport_disconnect(transport
);
1010 for_each_ref(append_ref_to_tracked_list
, states
);
1011 string_list_sort(&states
->tracked
);
1012 get_push_ref_states_noquery(states
);
1019 struct string_list list
;
1020 struct ref_states states
;
1025 #define SHOW_INFO_INIT { \
1026 .list = STRING_LIST_INIT_DUP, \
1027 .states = REF_STATES_INIT, \
1030 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
1032 struct show_info
*info
= cb_data
;
1033 int n
= strlen(item
->string
);
1034 if (n
> info
->width
)
1036 string_list_insert(&info
->list
, item
->string
);
1040 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
1042 struct show_info
*info
= cb_data
;
1043 struct ref_states
*states
= &info
->states
;
1044 const char *name
= item
->string
;
1046 if (states
->queried
) {
1047 const char *fmt
= "%s";
1048 const char *arg
= "";
1049 if (string_list_has_string(&states
->new_refs
, name
)) {
1050 fmt
= _(" new (next fetch will store in remotes/%s)");
1051 arg
= states
->remote
->name
;
1052 } else if (string_list_has_string(&states
->tracked
, name
))
1053 arg
= _(" tracked");
1054 else if (string_list_has_string(&states
->skipped
, name
))
1055 arg
= _(" skipped");
1056 else if (string_list_has_string(&states
->stale
, name
))
1057 arg
= _(" stale (use 'git remote prune' to remove)");
1060 printf(" %-*s", info
->width
, name
);
1064 printf(" %s\n", name
);
1069 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
1071 struct show_info
*show_info
= cb_data
;
1072 struct ref_states
*states
= &show_info
->states
;
1073 struct branch_info
*branch_info
= branch_item
->util
;
1074 struct string_list_item
*item
;
1077 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
1078 strcmp(states
->remote
->name
, branch_info
->remote_name
))
1080 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
1081 show_info
->width
= n
;
1082 if (branch_info
->rebase
>= REBASE_TRUE
)
1083 show_info
->any_rebase
= 1;
1085 item
= string_list_insert(&show_info
->list
, branch_item
->string
);
1086 item
->util
= branch_info
;
1091 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
1093 struct show_info
*show_info
= cb_data
;
1094 struct branch_info
*branch_info
= item
->util
;
1095 struct string_list
*merge
= &branch_info
->merge
;
1096 int width
= show_info
->width
+ 4;
1099 if (branch_info
->rebase
>= REBASE_TRUE
&& branch_info
->merge
.nr
> 1) {
1100 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1105 printf(" %-*s ", show_info
->width
, item
->string
);
1106 if (branch_info
->rebase
>= REBASE_TRUE
) {
1108 if (branch_info
->rebase
== REBASE_INTERACTIVE
)
1109 msg
= _("rebases interactively onto remote %s");
1110 else if (branch_info
->rebase
== REBASE_MERGES
)
1111 msg
= _("rebases interactively (with merges) onto "
1114 msg
= _("rebases onto remote %s");
1115 printf_ln(msg
, merge
->items
[0].string
);
1117 } else if (show_info
->any_rebase
) {
1118 printf_ln(_(" merges with remote %s"), merge
->items
[0].string
);
1121 printf_ln(_("merges with remote %s"), merge
->items
[0].string
);
1123 for (i
= 1; i
< merge
->nr
; i
++)
1124 printf(_("%-*s and with remote %s\n"), width
, "",
1125 merge
->items
[i
].string
);
1130 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
1132 struct show_info
*show_info
= cb_data
;
1133 struct push_info
*push_info
= push_item
->util
;
1134 struct string_list_item
*item
;
1136 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
1137 show_info
->width
= n
;
1138 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
1139 show_info
->width2
= n
;
1140 item
= string_list_append(&show_info
->list
, push_item
->string
);
1141 item
->util
= push_item
->util
;
1146 * Sorting comparison for a string list that has push_info
1147 * structs in its util field
1149 static int cmp_string_with_push(const void *va
, const void *vb
)
1151 const struct string_list_item
*a
= va
;
1152 const struct string_list_item
*b
= vb
;
1153 const struct push_info
*a_push
= a
->util
;
1154 const struct push_info
*b_push
= b
->util
;
1155 int cmp
= strcmp(a
->string
, b
->string
);
1156 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
1159 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
1161 struct show_info
*show_info
= cb_data
;
1162 struct push_info
*push_info
= item
->util
;
1163 const char *src
= item
->string
, *status
= NULL
;
1165 switch (push_info
->status
) {
1166 case PUSH_STATUS_CREATE
:
1167 status
= _("create");
1169 case PUSH_STATUS_DELETE
:
1170 status
= _("delete");
1173 case PUSH_STATUS_UPTODATE
:
1174 status
= _("up to date");
1176 case PUSH_STATUS_FASTFORWARD
:
1177 status
= _("fast-forwardable");
1179 case PUSH_STATUS_OUTOFDATE
:
1180 status
= _("local out of date");
1182 case PUSH_STATUS_NOTQUERIED
:
1186 if (push_info
->forced
)
1187 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info
->width
, src
,
1188 show_info
->width2
, push_info
->dest
, status
);
1190 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info
->width
, src
,
1191 show_info
->width2
, push_info
->dest
, status
);
1193 if (push_info
->forced
)
1194 printf_ln(_(" %-*s forces to %s"), show_info
->width
, src
,
1197 printf_ln(_(" %-*s pushes to %s"), show_info
->width
, src
,
1203 static int get_one_entry(struct remote
*remote
, void *priv
)
1205 struct string_list
*list
= priv
;
1206 struct strbuf remote_info_buf
= STRBUF_INIT
;
1210 if (remote
->url_nr
> 0) {
1211 struct strbuf promisor_config
= STRBUF_INIT
;
1212 const char *partial_clone_filter
= NULL
;
1214 strbuf_addf(&promisor_config
, "remote.%s.partialclonefilter", remote
->name
);
1215 strbuf_addf(&remote_info_buf
, "%s (fetch)", remote
->url
[0]);
1216 if (!git_config_get_string_tmp(promisor_config
.buf
, &partial_clone_filter
))
1217 strbuf_addf(&remote_info_buf
, " [%s]", partial_clone_filter
);
1219 strbuf_release(&promisor_config
);
1220 string_list_append(list
, remote
->name
)->util
=
1221 strbuf_detach(&remote_info_buf
, NULL
);
1223 string_list_append(list
, remote
->name
)->util
= NULL
;
1224 if (remote
->pushurl_nr
) {
1225 url
= remote
->pushurl
;
1226 url_nr
= remote
->pushurl_nr
;
1229 url_nr
= remote
->url_nr
;
1231 for (i
= 0; i
< url_nr
; i
++)
1233 strbuf_addf(&remote_info_buf
, "%s (push)", url
[i
]);
1234 string_list_append(list
, remote
->name
)->util
=
1235 strbuf_detach(&remote_info_buf
, NULL
);
1241 static int show_all(void)
1243 struct string_list list
= STRING_LIST_INIT_DUP
;
1246 result
= for_each_remote(get_one_entry
, &list
);
1251 string_list_sort(&list
);
1252 for (i
= 0; i
< list
.nr
; i
++) {
1253 struct string_list_item
*item
= list
.items
+ i
;
1255 printf("%s\t%s\n", item
->string
,
1256 item
->util
? (const char *)item
->util
: "");
1258 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1260 printf("%s\n", item
->string
);
1264 string_list_clear(&list
, 1);
1268 static int show(int argc
, const char **argv
, const char *prefix
)
1270 int no_query
= 0, result
= 0, query_flag
= 0;
1271 struct option options
[] = {
1272 OPT_BOOL('n', NULL
, &no_query
, N_("do not query remotes")),
1275 struct show_info info
= SHOW_INFO_INIT
;
1277 argc
= parse_options(argc
, argv
, prefix
, options
,
1278 builtin_remote_show_usage
,
1285 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1287 for (; argc
; argc
--, argv
++) {
1292 get_remote_ref_states(*argv
, &info
.states
, query_flag
);
1294 printf_ln(_("* remote %s"), *argv
);
1295 printf_ln(_(" Fetch URL: %s"), info
.states
.remote
->url_nr
> 0 ?
1296 info
.states
.remote
->url
[0] : _("(no URL)"));
1297 if (info
.states
.remote
->pushurl_nr
) {
1298 url
= info
.states
.remote
->pushurl
;
1299 url_nr
= info
.states
.remote
->pushurl_nr
;
1301 url
= info
.states
.remote
->url
;
1302 url_nr
= info
.states
.remote
->url_nr
;
1304 for (i
= 0; i
< url_nr
; i
++)
1306 * TRANSLATORS: the colon ':' should align
1307 * with the one in " Fetch URL: %s"
1310 printf_ln(_(" Push URL: %s"), url
[i
]);
1312 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1314 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1315 else if (!info
.states
.heads
.nr
)
1316 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1317 else if (info
.states
.heads
.nr
== 1)
1318 printf_ln(_(" HEAD branch: %s"), info
.states
.heads
.items
[0].string
);
1320 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1321 " may be one of the following):\n"));
1322 for (i
= 0; i
< info
.states
.heads
.nr
; i
++)
1323 printf(" %s\n", info
.states
.heads
.items
[i
].string
);
1326 /* remote branch info */
1328 for_each_string_list(&info
.states
.new_refs
, add_remote_to_show_info
, &info
);
1329 for_each_string_list(&info
.states
.skipped
, add_remote_to_show_info
, &info
);
1330 for_each_string_list(&info
.states
.tracked
, add_remote_to_show_info
, &info
);
1331 for_each_string_list(&info
.states
.stale
, add_remote_to_show_info
, &info
);
1333 printf_ln(Q_(" Remote branch:%s",
1334 " Remote branches:%s",
1336 no_query
? _(" (status not queried)") : "");
1337 for_each_string_list(&info
.list
, show_remote_info_item
, &info
);
1338 string_list_clear(&info
.list
, 0);
1342 info
.any_rebase
= 0;
1343 for_each_string_list(&branch_list
, add_local_to_show_info
, &info
);
1345 printf_ln(Q_(" Local branch configured for 'git pull':",
1346 " Local branches configured for 'git pull':",
1348 for_each_string_list(&info
.list
, show_local_info_item
, &info
);
1349 string_list_clear(&info
.list
, 0);
1352 if (info
.states
.remote
->mirror
)
1353 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1355 info
.width
= info
.width2
= 0;
1356 for_each_string_list(&info
.states
.push
, add_push_to_show_info
, &info
);
1357 QSORT(info
.list
.items
, info
.list
.nr
, cmp_string_with_push
);
1359 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1360 " Local refs configured for 'git push'%s:",
1362 no_query
? _(" (status not queried)") : "");
1363 for_each_string_list(&info
.list
, show_push_info_item
, &info
);
1364 string_list_clear(&info
.list
, 0);
1366 free_remote_ref_states(&info
.states
);
1372 static int set_head(int argc
, const char **argv
, const char *prefix
)
1374 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1375 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1376 char *head_name
= NULL
;
1378 struct option options
[] = {
1379 OPT_BOOL('a', "auto", &opt_a
,
1380 N_("set refs/remotes/<name>/HEAD according to remote")),
1381 OPT_BOOL('d', "delete", &opt_d
,
1382 N_("delete refs/remotes/<name>/HEAD")),
1385 argc
= parse_options(argc
, argv
, prefix
, options
,
1386 builtin_remote_sethead_usage
, 0);
1388 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1390 if (!opt_a
&& !opt_d
&& argc
== 2) {
1391 head_name
= xstrdup(argv
[1]);
1392 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1393 struct ref_states states
= REF_STATES_INIT
;
1394 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1395 if (!states
.heads
.nr
)
1396 result
|= error(_("Cannot determine remote HEAD"));
1397 else if (states
.heads
.nr
> 1) {
1398 result
|= error(_("Multiple remote HEAD branches. "
1399 "Please choose one explicitly with:"));
1400 for (i
= 0; i
< states
.heads
.nr
; i
++)
1401 fprintf(stderr
, " git remote set-head %s %s\n",
1402 argv
[0], states
.heads
.items
[i
].string
);
1404 head_name
= xstrdup(states
.heads
.items
[0].string
);
1405 free_remote_ref_states(&states
);
1406 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1407 if (delete_ref(NULL
, buf
.buf
, NULL
, REF_NO_DEREF
))
1408 result
|= error(_("Could not delete %s"), buf
.buf
);
1410 usage_with_options(builtin_remote_sethead_usage
, options
);
1413 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1414 /* make sure it's valid */
1415 if (!ref_exists(buf2
.buf
))
1416 result
|= error(_("Not a valid ref: %s"), buf2
.buf
);
1417 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1418 result
|= error(_("Could not setup %s"), buf
.buf
);
1420 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1424 strbuf_release(&buf
);
1425 strbuf_release(&buf2
);
1429 static int prune_remote(const char *remote
, int dry_run
)
1432 struct ref_states states
= REF_STATES_INIT
;
1433 struct string_list refs_to_prune
= STRING_LIST_INIT_NODUP
;
1434 struct string_list_item
*item
;
1435 const char *dangling_msg
= dry_run
1436 ? _(" %s will become dangling!")
1437 : _(" %s has become dangling!");
1439 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1441 if (!states
.stale
.nr
) {
1442 free_remote_ref_states(&states
);
1446 printf_ln(_("Pruning %s"), remote
);
1447 printf_ln(_("URL: %s"),
1448 states
.remote
->url_nr
1449 ? states
.remote
->url
[0]
1452 for_each_string_list_item(item
, &states
.stale
)
1453 string_list_append(&refs_to_prune
, item
->util
);
1454 string_list_sort(&refs_to_prune
);
1457 result
|= delete_refs("remote: prune", &refs_to_prune
, 0);
1459 for_each_string_list_item(item
, &states
.stale
) {
1460 const char *refname
= item
->util
;
1463 printf_ln(_(" * [would prune] %s"),
1464 abbrev_ref(refname
, "refs/remotes/"));
1466 printf_ln(_(" * [pruned] %s"),
1467 abbrev_ref(refname
, "refs/remotes/"));
1470 warn_dangling_symrefs(stdout
, dangling_msg
, &refs_to_prune
);
1472 string_list_clear(&refs_to_prune
, 0);
1473 free_remote_ref_states(&states
);
1477 static int prune(int argc
, const char **argv
, const char *prefix
)
1479 int dry_run
= 0, result
= 0;
1480 struct option options
[] = {
1481 OPT__DRY_RUN(&dry_run
, N_("dry run")),
1485 argc
= parse_options(argc
, argv
, prefix
, options
,
1486 builtin_remote_prune_usage
, 0);
1489 usage_with_options(builtin_remote_prune_usage
, options
);
1491 for (; argc
; argc
--, argv
++)
1492 result
|= prune_remote(*argv
, dry_run
);
1497 static int get_remote_default(const char *key
, const char *value UNUSED
, void *priv
)
1499 if (strcmp(key
, "remotes.default") == 0) {
1506 static int update(int argc
, const char **argv
, const char *prefix
)
1509 struct option options
[] = {
1510 OPT_BOOL('p', "prune", &prune
,
1511 N_("prune remotes after fetching")),
1514 struct child_process cmd
= CHILD_PROCESS_INIT
;
1515 int default_defined
= 0;
1517 argc
= parse_options(argc
, argv
, prefix
, options
,
1518 builtin_remote_update_usage
,
1519 PARSE_OPT_KEEP_ARGV0
);
1521 strvec_push(&cmd
.args
, "fetch");
1524 strvec_push(&cmd
.args
, prune
? "--prune" : "--no-prune");
1526 strvec_push(&cmd
.args
, "-v");
1527 strvec_push(&cmd
.args
, "--multiple");
1529 strvec_push(&cmd
.args
, "default");
1530 for (i
= 1; i
< argc
; i
++)
1531 strvec_push(&cmd
.args
, argv
[i
]);
1533 if (strcmp(cmd
.args
.v
[cmd
.args
.nr
-1], "default") == 0) {
1534 git_config(get_remote_default
, &default_defined
);
1535 if (!default_defined
) {
1536 strvec_pop(&cmd
.args
);
1537 strvec_push(&cmd
.args
, "--all");
1542 return run_command(&cmd
);
1545 static int remove_all_fetch_refspecs(const char *key
)
1547 return git_config_set_multivar_gently(key
, NULL
, NULL
,
1548 CONFIG_FLAGS_MULTI_REPLACE
);
1551 static void add_branches(struct remote
*remote
, const char **branches
,
1554 const char *remotename
= remote
->name
;
1555 int mirror
= remote
->mirror
;
1556 struct strbuf refspec
= STRBUF_INIT
;
1558 for (; *branches
; branches
++)
1559 add_branch(key
, *branches
, remotename
, mirror
, &refspec
);
1561 strbuf_release(&refspec
);
1564 static int set_remote_branches(const char *remotename
, const char **branches
,
1567 struct strbuf key
= STRBUF_INIT
;
1568 struct remote
*remote
;
1570 strbuf_addf(&key
, "remote.%s.fetch", remotename
);
1572 remote
= remote_get(remotename
);
1573 if (!remote_is_configured(remote
, 1)) {
1574 error(_("No such remote '%s'"), remotename
);
1578 if (!add_mode
&& remove_all_fetch_refspecs(key
.buf
)) {
1579 strbuf_release(&key
);
1582 add_branches(remote
, branches
, key
.buf
);
1584 strbuf_release(&key
);
1588 static int set_branches(int argc
, const char **argv
, const char *prefix
)
1591 struct option options
[] = {
1592 OPT_BOOL('\0', "add", &add_mode
, N_("add branch")),
1596 argc
= parse_options(argc
, argv
, prefix
, options
,
1597 builtin_remote_setbranches_usage
, 0);
1599 error(_("no remote specified"));
1600 usage_with_options(builtin_remote_setbranches_usage
, options
);
1604 return set_remote_branches(argv
[0], argv
+ 1, add_mode
);
1607 static int get_url(int argc
, const char **argv
, const char *prefix
)
1609 int i
, push_mode
= 0, all_mode
= 0;
1610 const char *remotename
= NULL
;
1611 struct remote
*remote
;
1614 struct option options
[] = {
1615 OPT_BOOL('\0', "push", &push_mode
,
1616 N_("query push URLs rather than fetch URLs")),
1617 OPT_BOOL('\0', "all", &all_mode
,
1618 N_("return all URLs")),
1621 argc
= parse_options(argc
, argv
, prefix
, options
,
1622 builtin_remote_geturl_usage
, 0);
1625 usage_with_options(builtin_remote_geturl_usage
, options
);
1627 remotename
= argv
[0];
1629 remote
= remote_get(remotename
);
1630 if (!remote_is_configured(remote
, 1)) {
1631 error(_("No such remote '%s'"), remotename
);
1637 url
= remote
->pushurl
;
1638 url_nr
= remote
->pushurl_nr
;
1640 /* else fetch mode */
1642 /* Use the fetch URL when no push URLs were found or requested. */
1645 url_nr
= remote
->url_nr
;
1649 die(_("no URLs configured for remote '%s'"), remotename
);
1652 for (i
= 0; i
< url_nr
; i
++)
1653 printf_ln("%s", url
[i
]);
1655 printf_ln("%s", *url
);
1661 static int set_url(int argc
, const char **argv
, const char *prefix
)
1663 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1664 int matches
= 0, negative_matches
= 0;
1665 const char *remotename
= NULL
;
1666 const char *newurl
= NULL
;
1667 const char *oldurl
= NULL
;
1668 struct remote
*remote
;
1670 const char **urlset
;
1672 struct strbuf name_buf
= STRBUF_INIT
;
1673 struct option options
[] = {
1674 OPT_BOOL('\0', "push", &push_mode
,
1675 N_("manipulate push URLs")),
1676 OPT_BOOL('\0', "add", &add_mode
,
1678 OPT_BOOL('\0', "delete", &delete_mode
,
1682 argc
= parse_options(argc
, argv
, prefix
, options
,
1683 builtin_remote_seturl_usage
,
1684 PARSE_OPT_KEEP_ARGV0
);
1686 if (add_mode
&& delete_mode
)
1687 die(_("--add --delete doesn't make sense"));
1689 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1690 usage_with_options(builtin_remote_seturl_usage
, options
);
1692 remotename
= argv
[1];
1700 remote
= remote_get(remotename
);
1701 if (!remote_is_configured(remote
, 1)) {
1702 error(_("No such remote '%s'"), remotename
);
1707 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1708 urlset
= remote
->pushurl
;
1709 urlset_nr
= remote
->pushurl_nr
;
1711 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1712 urlset
= remote
->url
;
1713 urlset_nr
= remote
->url_nr
;
1716 /* Special cases that add new entry. */
1717 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1719 git_config_set_multivar(name_buf
.buf
, newurl
,
1722 git_config_set(name_buf
.buf
, newurl
);
1726 /* Old URL specified. Demand that one matches. */
1727 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1728 die(_("Invalid old URL pattern: %s"), oldurl
);
1730 for (i
= 0; i
< urlset_nr
; i
++)
1731 if (!regexec(&old_regex
, urlset
[i
], 0, NULL
, 0))
1735 if (!delete_mode
&& !matches
)
1736 die(_("No such URL found: %s"), oldurl
);
1737 if (delete_mode
&& !negative_matches
&& !push_mode
)
1738 die(_("Will not delete all non-push URLs"));
1740 regfree(&old_regex
);
1743 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1745 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
,
1746 CONFIG_FLAGS_MULTI_REPLACE
);
1748 strbuf_release(&name_buf
);
1752 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1754 parse_opt_subcommand_fn
*fn
= NULL
;
1755 struct option options
[] = {
1756 OPT__VERBOSE(&verbose
, N_("be verbose; must be placed before a subcommand")),
1757 OPT_SUBCOMMAND("add", &fn
, add
),
1758 OPT_SUBCOMMAND("rename", &fn
, mv
),
1759 OPT_SUBCOMMAND_F("rm", &fn
, rm
, PARSE_OPT_NOCOMPLETE
),
1760 OPT_SUBCOMMAND("remove", &fn
, rm
),
1761 OPT_SUBCOMMAND("set-head", &fn
, set_head
),
1762 OPT_SUBCOMMAND("set-branches", &fn
, set_branches
),
1763 OPT_SUBCOMMAND("get-url", &fn
, get_url
),
1764 OPT_SUBCOMMAND("set-url", &fn
, set_url
),
1765 OPT_SUBCOMMAND("show", &fn
, show
),
1766 OPT_SUBCOMMAND("prune", &fn
, prune
),
1767 OPT_SUBCOMMAND("update", &fn
, update
),
1771 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1772 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1775 return !!fn(argc
, argv
, prefix
);
1778 error(_("unknown subcommand: `%s'"), argv
[0]);
1779 usage_with_options(builtin_remote_usage
, options
);
1781 return !!show_all();