4 #include "parse-options.h"
8 #include "string-list.h"
10 #include "run-command.h"
14 #include "object-store-ll.h"
16 #include "commit-reach.h"
19 static const char * const builtin_remote_usage
[] = {
20 "git remote [-v | --verbose]",
21 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
22 N_("git remote rename [--[no-]progress] <old> <new>"),
23 N_("git remote remove <name>"),
24 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
25 N_("git remote [-v | --verbose] show [-n] <name>"),
26 N_("git remote prune [-n | --dry-run] <name>"),
27 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
28 N_("git remote set-branches [--add] <name> <branch>..."),
29 N_("git remote get-url [--push] [--all] <name>"),
30 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
31 N_("git remote set-url --add <name> <newurl>"),
32 N_("git remote set-url --delete <name> <url>"),
36 static const char * const builtin_remote_add_usage
[] = {
37 N_("git remote add [<options>] <name> <url>"),
41 static const char * const builtin_remote_rename_usage
[] = {
42 N_("git remote rename [--[no-]progress] <old> <new>"),
46 static const char * const builtin_remote_rm_usage
[] = {
47 N_("git remote remove <name>"),
51 static const char * const builtin_remote_sethead_usage
[] = {
52 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
56 static const char * const builtin_remote_setbranches_usage
[] = {
57 N_("git remote set-branches <name> <branch>..."),
58 N_("git remote set-branches --add <name> <branch>..."),
62 static const char * const builtin_remote_show_usage
[] = {
63 N_("git remote show [<options>] <name>"),
67 static const char * const builtin_remote_prune_usage
[] = {
68 N_("git remote prune [<options>] <name>"),
72 static const char * const builtin_remote_update_usage
[] = {
73 N_("git remote update [<options>] [<group> | <remote>]..."),
77 static const char * const builtin_remote_geturl_usage
[] = {
78 N_("git remote get-url [--push] [--all] <name>"),
82 static const char * const builtin_remote_seturl_usage
[] = {
83 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
84 N_("git remote set-url --add <name> <newurl>"),
85 N_("git remote set-url --delete <name> <url>"),
89 #define GET_REF_STATES (1<<0)
90 #define GET_HEAD_NAMES (1<<1)
91 #define GET_PUSH_REF_STATES (1<<2)
95 static int fetch_remote(const char *name
)
97 struct child_process cmd
= CHILD_PROCESS_INIT
;
99 strvec_push(&cmd
.args
, "fetch");
101 strvec_push(&cmd
.args
, "-v");
102 strvec_push(&cmd
.args
, name
);
104 printf_ln(_("Updating %s"), name
);
105 if (run_command(&cmd
))
106 return error(_("Could not fetch %s"), name
);
116 #define MIRROR_NONE 0
117 #define MIRROR_FETCH 1
118 #define MIRROR_PUSH 2
119 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
121 static void add_branch(const char *key
, const char *branchname
,
122 const char *remotename
, int mirror
, struct strbuf
*tmp
)
125 strbuf_addch(tmp
, '+');
127 strbuf_addf(tmp
, "refs/%s:refs/%s",
128 branchname
, branchname
);
130 strbuf_addf(tmp
, "refs/heads/%s:refs/remotes/%s/%s",
131 branchname
, remotename
, branchname
);
132 git_config_set_multivar(key
, tmp
->buf
, "^$", 0);
135 static const char mirror_advice
[] =
136 N_("--mirror is dangerous and deprecated; please\n"
137 "\t use --mirror=fetch or --mirror=push instead");
139 static int parse_mirror_opt(const struct option
*opt
, const char *arg
, int not)
141 unsigned *mirror
= opt
->value
;
143 *mirror
= MIRROR_NONE
;
145 warning("%s", _(mirror_advice
));
146 *mirror
= MIRROR_BOTH
;
148 else if (!strcmp(arg
, "fetch"))
149 *mirror
= MIRROR_FETCH
;
150 else if (!strcmp(arg
, "push"))
151 *mirror
= MIRROR_PUSH
;
153 return error(_("unknown mirror argument: %s"), arg
);
157 static int add(int argc
, const char **argv
, const char *prefix
)
159 int fetch
= 0, fetch_tags
= TAGS_DEFAULT
;
160 unsigned mirror
= MIRROR_NONE
;
161 struct string_list track
= STRING_LIST_INIT_NODUP
;
162 const char *master
= NULL
;
163 struct remote
*remote
;
164 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
165 const char *name
, *url
;
168 struct option options
[] = {
169 OPT_BOOL('f', "fetch", &fetch
, N_("fetch the remote branches")),
170 OPT_SET_INT(0, "tags", &fetch_tags
,
171 N_("import all tags and associated objects when fetching"),
173 OPT_SET_INT(0, NULL
, &fetch_tags
,
174 N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET
),
175 OPT_STRING_LIST('t', "track", &track
, N_("branch"),
176 N_("branch(es) to track")),
177 OPT_STRING('m', "master", &master
, N_("branch"), N_("master branch")),
178 OPT_CALLBACK_F(0, "mirror", &mirror
, "(push|fetch)",
179 N_("set up remote as a mirror to push to or fetch from"),
180 PARSE_OPT_OPTARG
| PARSE_OPT_COMP_ARG
, parse_mirror_opt
),
184 argc
= parse_options(argc
, argv
, prefix
, options
,
185 builtin_remote_add_usage
, 0);
188 usage_with_options(builtin_remote_add_usage
, options
);
190 if (mirror
&& master
)
191 die(_("specifying a master branch makes no sense with --mirror"));
192 if (mirror
&& !(mirror
& MIRROR_FETCH
) && track
.nr
)
193 die(_("specifying branches to track makes sense only with fetch mirrors"));
198 remote
= remote_get(name
);
199 if (remote_is_configured(remote
, 1)) {
200 error(_("remote %s already exists."), name
);
204 if (!valid_remote_name(name
))
205 die(_("'%s' is not a valid remote name"), name
);
207 strbuf_addf(&buf
, "remote.%s.url", name
);
208 git_config_set(buf
.buf
, url
);
210 if (!mirror
|| mirror
& MIRROR_FETCH
) {
212 strbuf_addf(&buf
, "remote.%s.fetch", name
);
214 string_list_append(&track
, "*");
215 for (i
= 0; i
< track
.nr
; i
++) {
216 add_branch(buf
.buf
, track
.items
[i
].string
,
217 name
, mirror
, &buf2
);
221 if (mirror
& MIRROR_PUSH
) {
223 strbuf_addf(&buf
, "remote.%s.mirror", name
);
224 git_config_set(buf
.buf
, "true");
227 if (fetch_tags
!= TAGS_DEFAULT
) {
229 strbuf_addf(&buf
, "remote.%s.tagOpt", name
);
230 git_config_set(buf
.buf
,
231 fetch_tags
== TAGS_SET
? "--tags" : "--no-tags");
234 if (fetch
&& fetch_remote(name
))
239 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
242 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
244 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
245 return error(_("Could not setup master '%s'"), master
);
248 strbuf_release(&buf
);
249 strbuf_release(&buf2
);
250 string_list_clear(&track
, 0);
257 struct string_list merge
;
258 enum rebase_type rebase
;
259 char *push_remote_name
;
262 static struct string_list branch_list
= STRING_LIST_INIT_NODUP
;
264 static const char *abbrev_ref(const char *name
, const char *prefix
)
266 skip_prefix(name
, prefix
, &name
);
269 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
271 static int config_read_branches(const char *key
, const char *value
,
274 const char *orig_key
= key
;
276 struct string_list_item
*item
;
277 struct branch_info
*info
;
278 enum { REMOTE
, MERGE
, REBASE
, PUSH_REMOTE
} type
;
281 if (!starts_with(key
, "branch."))
284 key
+= strlen("branch.");
285 if (strip_suffix(key
, ".remote", &key_len
))
287 else if (strip_suffix(key
, ".merge", &key_len
))
289 else if (strip_suffix(key
, ".rebase", &key_len
))
291 else if (strip_suffix(key
, ".pushremote", &key_len
))
295 name
= xmemdupz(key
, key_len
);
297 item
= string_list_insert(&branch_list
, name
);
300 item
->util
= xcalloc(1, sizeof(struct branch_info
));
304 if (info
->remote_name
)
305 warning(_("more than one %s"), orig_key
);
306 info
->remote_name
= xstrdup(value
);
309 char *space
= strchr(value
, ' ');
310 value
= abbrev_branch(value
);
313 merge
= xstrndup(value
, space
- value
);
314 string_list_append(&info
->merge
, merge
);
315 value
= abbrev_branch(space
+ 1);
316 space
= strchr(value
, ' ');
318 string_list_append(&info
->merge
, xstrdup(value
));
323 * Consider invalid values as false and check the
324 * truth value with >= REBASE_TRUE.
326 info
->rebase
= rebase_parse_value(value
);
327 if (info
->rebase
== REBASE_INVALID
)
328 warning(_("unhandled branch.%s.rebase=%s; assuming "
329 "'true'"), name
, value
);
332 if (info
->push_remote_name
)
333 warning(_("more than one %s"), orig_key
);
334 info
->push_remote_name
= xstrdup(value
);
337 BUG("unexpected type=%d", type
);
343 static void read_branches(void)
347 git_config(config_read_branches
, NULL
);
351 struct remote
*remote
;
352 struct string_list new_refs
, skipped
, stale
, tracked
, heads
, push
;
356 #define REF_STATES_INIT { \
357 .new_refs = STRING_LIST_INIT_DUP, \
358 .skipped = STRING_LIST_INIT_DUP, \
359 .stale = STRING_LIST_INIT_DUP, \
360 .tracked = STRING_LIST_INIT_DUP, \
361 .heads = STRING_LIST_INIT_DUP, \
362 .push = STRING_LIST_INIT_DUP, \
365 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
367 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
368 struct ref
*ref
, *stale_refs
;
371 for (i
= 0; i
< states
->remote
->fetch
.nr
; i
++)
372 if (get_fetch_map(remote_refs
, &states
->remote
->fetch
.items
[i
], &tail
, 1))
373 die(_("Could not get fetch map for refspec %s"),
374 states
->remote
->fetch
.raw
[i
]);
376 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
377 if (omit_name_by_refspec(ref
->name
, &states
->remote
->fetch
))
378 string_list_append(&states
->skipped
, abbrev_branch(ref
->name
));
379 else if (!ref
->peer_ref
|| !ref_exists(ref
->peer_ref
->name
))
380 string_list_append(&states
->new_refs
, abbrev_branch(ref
->name
));
382 string_list_append(&states
->tracked
, abbrev_branch(ref
->name
));
384 stale_refs
= get_stale_heads(&states
->remote
->fetch
, fetch_map
);
385 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
386 struct string_list_item
*item
=
387 string_list_append(&states
->stale
, abbrev_branch(ref
->name
));
388 item
->util
= xstrdup(ref
->name
);
390 free_refs(stale_refs
);
391 free_refs(fetch_map
);
393 string_list_sort(&states
->new_refs
);
394 string_list_sort(&states
->skipped
);
395 string_list_sort(&states
->tracked
);
396 string_list_sort(&states
->stale
);
405 PUSH_STATUS_CREATE
= 0,
407 PUSH_STATUS_UPTODATE
,
408 PUSH_STATUS_FASTFORWARD
,
409 PUSH_STATUS_OUTOFDATE
,
410 PUSH_STATUS_NOTQUERIED
414 static int get_push_ref_states(const struct ref
*remote_refs
,
415 struct ref_states
*states
)
417 struct remote
*remote
= states
->remote
;
418 struct ref
*ref
, *local_refs
, *push_map
;
422 local_refs
= get_local_heads();
423 push_map
= copy_ref_list(remote_refs
);
425 match_push_refs(local_refs
, &push_map
, &remote
->push
, MATCH_REFS_NONE
);
427 for (ref
= push_map
; ref
; ref
= ref
->next
) {
428 struct string_list_item
*item
;
429 struct push_info
*info
;
433 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
435 item
= string_list_append(&states
->push
,
436 abbrev_branch(ref
->peer_ref
->name
));
437 item
->util
= xcalloc(1, sizeof(struct push_info
));
439 info
->forced
= ref
->force
;
440 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
442 if (is_null_oid(&ref
->new_oid
)) {
443 info
->status
= PUSH_STATUS_DELETE
;
444 } else if (oideq(&ref
->old_oid
, &ref
->new_oid
))
445 info
->status
= PUSH_STATUS_UPTODATE
;
446 else if (is_null_oid(&ref
->old_oid
))
447 info
->status
= PUSH_STATUS_CREATE
;
448 else if (repo_has_object_file(the_repository
, &ref
->old_oid
) &&
449 ref_newer(&ref
->new_oid
, &ref
->old_oid
))
450 info
->status
= PUSH_STATUS_FASTFORWARD
;
452 info
->status
= PUSH_STATUS_OUTOFDATE
;
454 free_refs(local_refs
);
459 static int get_push_ref_states_noquery(struct ref_states
*states
)
462 struct remote
*remote
= states
->remote
;
463 struct string_list_item
*item
;
464 struct push_info
*info
;
469 if (!remote
->push
.nr
) {
470 item
= string_list_append(&states
->push
, _("(matching)"));
471 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
472 info
->status
= PUSH_STATUS_NOTQUERIED
;
473 info
->dest
= xstrdup(item
->string
);
475 for (i
= 0; i
< remote
->push
.nr
; i
++) {
476 const struct refspec_item
*spec
= &remote
->push
.items
[i
];
478 item
= string_list_append(&states
->push
, _("(matching)"));
479 else if (strlen(spec
->src
))
480 item
= string_list_append(&states
->push
, spec
->src
);
482 item
= string_list_append(&states
->push
, _("(delete)"));
484 info
= item
->util
= xcalloc(1, sizeof(struct push_info
));
485 info
->forced
= spec
->force
;
486 info
->status
= PUSH_STATUS_NOTQUERIED
;
487 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
492 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
494 struct ref
*ref
, *matches
;
495 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
496 struct refspec_item refspec
;
498 memset(&refspec
, 0, sizeof(refspec
));
501 refspec
.src
= refspec
.dst
= "refs/heads/*";
502 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
503 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
505 for (ref
= matches
; ref
; ref
= ref
->next
)
506 string_list_append(&states
->heads
, abbrev_branch(ref
->name
));
508 free_refs(fetch_map
);
514 struct known_remote
{
515 struct known_remote
*next
;
516 struct remote
*remote
;
519 struct known_remotes
{
520 struct remote
*to_delete
;
521 struct known_remote
*list
;
524 static int add_known_remote(struct remote
*remote
, void *cb_data
)
526 struct known_remotes
*all
= cb_data
;
527 struct known_remote
*r
;
529 if (!strcmp(all
->to_delete
->name
, remote
->name
))
532 r
= xmalloc(sizeof(*r
));
539 struct branches_for_remote
{
540 struct remote
*remote
;
541 struct string_list
*branches
, *skipped
;
542 struct known_remotes
*keep
;
545 static int add_branch_for_removal(const char *refname
,
546 const struct object_id
*oid UNUSED
,
547 int flags UNUSED
, void *cb_data
)
549 struct branches_for_remote
*branches
= cb_data
;
550 struct refspec_item refspec
;
551 struct known_remote
*kr
;
553 memset(&refspec
, 0, sizeof(refspec
));
554 refspec
.dst
= (char *)refname
;
555 if (remote_find_tracking(branches
->remote
, &refspec
))
558 /* don't delete a branch if another remote also uses it */
559 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
560 memset(&refspec
, 0, sizeof(refspec
));
561 refspec
.dst
= (char *)refname
;
562 if (!remote_find_tracking(kr
->remote
, &refspec
))
566 /* don't delete non-remote-tracking refs */
567 if (!starts_with(refname
, "refs/remotes/")) {
568 /* advise user how to delete local branches */
569 if (starts_with(refname
, "refs/heads/"))
570 string_list_append(branches
->skipped
,
571 abbrev_branch(refname
));
572 /* silently skip over other non-remote refs */
576 string_list_append(branches
->branches
, refname
);
582 const char *old_name
;
583 const char *new_name
;
584 struct string_list
*remote_branches
;
588 static int read_remote_branches(const char *refname
,
589 const struct object_id
*oid UNUSED
,
590 int flags UNUSED
, void *cb_data
)
592 struct rename_info
*rename
= cb_data
;
593 struct strbuf buf
= STRBUF_INIT
;
594 struct string_list_item
*item
;
598 strbuf_addf(&buf
, "refs/remotes/%s/", rename
->old_name
);
599 if (starts_with(refname
, buf
.buf
)) {
600 item
= string_list_append(rename
->remote_branches
, refname
);
601 symref
= resolve_ref_unsafe(refname
, RESOLVE_REF_READING
,
603 if (symref
&& (flag
& REF_ISSYMREF
)) {
604 item
->util
= xstrdup(symref
);
605 rename
->symrefs_nr
++;
610 strbuf_release(&buf
);
615 static int migrate_file(struct remote
*remote
)
617 struct strbuf buf
= STRBUF_INIT
;
620 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
621 for (i
= 0; i
< remote
->url_nr
; i
++)
622 git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0);
624 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
625 for (i
= 0; i
< remote
->push
.raw_nr
; i
++)
626 git_config_set_multivar(buf
.buf
, remote
->push
.raw
[i
], "^$", 0);
628 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
629 for (i
= 0; i
< remote
->fetch
.raw_nr
; i
++)
630 git_config_set_multivar(buf
.buf
, remote
->fetch
.raw
[i
], "^$", 0);
631 if (remote
->origin
== REMOTE_REMOTES
)
632 unlink_or_warn(git_path("remotes/%s", remote
->name
));
633 else if (remote
->origin
== REMOTE_BRANCHES
)
634 unlink_or_warn(git_path("branches/%s", remote
->name
));
635 strbuf_release(&buf
);
640 struct push_default_info
642 const char *old_name
;
643 enum config_scope scope
;
644 struct strbuf origin
;
648 static int config_read_push_default(const char *key
, const char *value
,
651 struct push_default_info
* info
= cb
;
652 if (strcmp(key
, "remote.pushdefault") ||
653 !value
|| strcmp(value
, info
->old_name
))
656 info
->scope
= current_config_scope();
657 strbuf_reset(&info
->origin
);
658 strbuf_addstr(&info
->origin
, current_config_name());
659 info
->linenr
= current_config_line();
664 static void handle_push_default(const char* old_name
, const char* new_name
)
666 struct push_default_info push_default
= {
667 old_name
, CONFIG_SCOPE_UNKNOWN
, STRBUF_INIT
, -1 };
668 git_config(config_read_push_default
, &push_default
);
669 if (push_default
.scope
>= CONFIG_SCOPE_COMMAND
)
671 else if (push_default
.scope
>= CONFIG_SCOPE_LOCAL
) {
672 int result
= git_config_set_gently("remote.pushDefault",
674 if (new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
675 die(_("could not set '%s'"), "remote.pushDefault");
676 else if (!new_name
&& result
&& result
!= CONFIG_NOTHING_SET
)
677 die(_("could not unset '%s'"), "remote.pushDefault");
678 } else if (push_default
.scope
>= CONFIG_SCOPE_SYSTEM
) {
680 warning(_("The %s configuration remote.pushDefault in:\n"
682 "now names the non-existent remote '%s'"),
683 config_scope_name(push_default
.scope
),
684 push_default
.origin
.buf
, push_default
.linenr
,
690 static int mv(int argc
, const char **argv
, const char *prefix
)
692 int show_progress
= isatty(2);
693 struct option options
[] = {
694 OPT_BOOL(0, "progress", &show_progress
, N_("force progress reporting")),
697 struct remote
*oldremote
, *newremote
;
698 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
,
699 old_remote_context
= STRBUF_INIT
;
700 struct string_list remote_branches
= STRING_LIST_INIT_DUP
;
701 struct rename_info rename
;
702 int i
, refs_renamed_nr
= 0, refspec_updated
= 0;
703 struct progress
*progress
= NULL
;
705 argc
= parse_options(argc
, argv
, prefix
, options
,
706 builtin_remote_rename_usage
, 0);
709 usage_with_options(builtin_remote_rename_usage
, options
);
711 rename
.old_name
= argv
[0];
712 rename
.new_name
= argv
[1];
713 rename
.remote_branches
= &remote_branches
;
714 rename
.symrefs_nr
= 0;
716 oldremote
= remote_get(rename
.old_name
);
717 if (!remote_is_configured(oldremote
, 1)) {
718 error(_("No such remote: '%s'"), rename
.old_name
);
722 if (!strcmp(rename
.old_name
, rename
.new_name
) && oldremote
->origin
!= REMOTE_CONFIG
)
723 return migrate_file(oldremote
);
725 newremote
= remote_get(rename
.new_name
);
726 if (remote_is_configured(newremote
, 1)) {
727 error(_("remote %s already exists."), rename
.new_name
);
731 if (!valid_remote_name(rename
.new_name
))
732 die(_("'%s' is not a valid remote name"), rename
.new_name
);
734 strbuf_addf(&buf
, "remote.%s", rename
.old_name
);
735 strbuf_addf(&buf2
, "remote.%s", rename
.new_name
);
736 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
737 return error(_("Could not rename config section '%s' to '%s'"),
740 if (oldremote
->fetch
.raw_nr
) {
742 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new_name
);
743 git_config_set_multivar(buf
.buf
, NULL
, NULL
, CONFIG_FLAGS_MULTI_REPLACE
);
744 strbuf_addf(&old_remote_context
, ":refs/remotes/%s/", rename
.old_name
);
745 for (i
= 0; i
< oldremote
->fetch
.raw_nr
; i
++) {
749 strbuf_addstr(&buf2
, oldremote
->fetch
.raw
[i
]);
750 ptr
= strstr(buf2
.buf
, old_remote_context
.buf
);
754 ptr
-buf2
.buf
+ strlen(":refs/remotes/"),
755 strlen(rename
.old_name
), rename
.new_name
,
756 strlen(rename
.new_name
));
758 warning(_("Not updating non-default fetch refspec\n"
760 "\tPlease update the configuration manually if necessary."),
763 git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0);
768 for (i
= 0; i
< branch_list
.nr
; i
++) {
769 struct string_list_item
*item
= branch_list
.items
+ i
;
770 struct branch_info
*info
= item
->util
;
771 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old_name
)) {
773 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
774 git_config_set(buf
.buf
, rename
.new_name
);
776 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, rename
.old_name
)) {
778 strbuf_addf(&buf
, "branch.%s.pushRemote", item
->string
);
779 git_config_set(buf
.buf
, rename
.new_name
);
783 if (!refspec_updated
)
787 * First remove symrefs, then rename the rest, finally create
790 for_each_ref(read_remote_branches
, &rename
);
793 * Count symrefs twice, since "renaming" them is done by
794 * deleting and recreating them in two separate passes.
796 progress
= start_progress(_("Renaming remote references"),
797 rename
.remote_branches
->nr
+ rename
.symrefs_nr
);
799 for (i
= 0; i
< remote_branches
.nr
; i
++) {
800 struct string_list_item
*item
= remote_branches
.items
+ i
;
801 struct strbuf referent
= STRBUF_INIT
;
803 if (refs_read_symbolic_ref(get_main_ref_store(the_repository
), item
->string
,
806 if (delete_ref(NULL
, item
->string
, NULL
, REF_NO_DEREF
))
807 die(_("deleting '%s' failed"), item
->string
);
809 strbuf_release(&referent
);
810 display_progress(progress
, ++refs_renamed_nr
);
812 for (i
= 0; i
< remote_branches
.nr
; i
++) {
813 struct string_list_item
*item
= remote_branches
.items
+ i
;
818 strbuf_addstr(&buf
, item
->string
);
819 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
820 rename
.new_name
, strlen(rename
.new_name
));
822 strbuf_addf(&buf2
, "remote: renamed %s to %s",
823 item
->string
, buf
.buf
);
824 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
825 die(_("renaming '%s' failed"), item
->string
);
826 display_progress(progress
, ++refs_renamed_nr
);
828 for (i
= 0; i
< remote_branches
.nr
; i
++) {
829 struct string_list_item
*item
= remote_branches
.items
+ i
;
834 strbuf_addstr(&buf
, item
->string
);
835 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old_name
),
836 rename
.new_name
, strlen(rename
.new_name
));
838 strbuf_addstr(&buf2
, item
->util
);
839 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old_name
),
840 rename
.new_name
, strlen(rename
.new_name
));
842 strbuf_addf(&buf3
, "remote: renamed %s to %s",
843 item
->string
, buf
.buf
);
844 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
845 die(_("creating '%s' failed"), buf
.buf
);
846 display_progress(progress
, ++refs_renamed_nr
);
848 stop_progress(&progress
);
849 string_list_clear(&remote_branches
, 1);
851 handle_push_default(rename
.old_name
, rename
.new_name
);
856 static int rm(int argc
, const char **argv
, const char *prefix
)
858 struct option options
[] = {
861 struct remote
*remote
;
862 struct strbuf buf
= STRBUF_INIT
;
863 struct known_remotes known_remotes
= { NULL
, NULL
};
864 struct string_list branches
= STRING_LIST_INIT_DUP
;
865 struct string_list skipped
= STRING_LIST_INIT_DUP
;
866 struct branches_for_remote cb_data
;
869 memset(&cb_data
, 0, sizeof(cb_data
));
870 cb_data
.branches
= &branches
;
871 cb_data
.skipped
= &skipped
;
872 cb_data
.keep
= &known_remotes
;
874 argc
= parse_options(argc
, argv
, prefix
, options
,
875 builtin_remote_rm_usage
, 0);
877 usage_with_options(builtin_remote_rm_usage
, options
);
879 remote
= remote_get(argv
[0]);
880 if (!remote_is_configured(remote
, 1)) {
881 error(_("No such remote: '%s'"), argv
[0]);
885 known_remotes
.to_delete
= remote
;
886 for_each_remote(add_known_remote
, &known_remotes
);
889 for (i
= 0; i
< branch_list
.nr
; i
++) {
890 struct string_list_item
*item
= branch_list
.items
+ i
;
891 struct branch_info
*info
= item
->util
;
892 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
893 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
894 for (k
= keys
; *k
; k
++) {
896 strbuf_addf(&buf
, "branch.%s.%s",
898 result
= git_config_set_gently(buf
.buf
, NULL
);
899 if (result
&& result
!= CONFIG_NOTHING_SET
)
900 die(_("could not unset '%s'"), buf
.buf
);
903 if (info
->push_remote_name
&& !strcmp(info
->push_remote_name
, remote
->name
)) {
905 strbuf_addf(&buf
, "branch.%s.pushremote", item
->string
);
906 result
= git_config_set_gently(buf
.buf
, NULL
);
907 if (result
&& result
!= CONFIG_NOTHING_SET
)
908 die(_("could not unset '%s'"), buf
.buf
);
913 * We cannot just pass a function to for_each_ref() which deletes
914 * the branches one by one, since for_each_ref() relies on cached
915 * refs, which are invalidated when deleting a branch.
917 cb_data
.remote
= remote
;
918 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
919 strbuf_release(&buf
);
922 result
= delete_refs("remote: remove", &branches
, REF_NO_DEREF
);
923 string_list_clear(&branches
, 0);
927 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
928 "to delete it, use:",
929 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
930 "to delete them, use:",
932 for (i
= 0; i
< skipped
.nr
; i
++)
933 fprintf(stderr
, " git branch -d %s\n",
934 skipped
.items
[i
].string
);
936 string_list_clear(&skipped
, 0);
939 strbuf_addf(&buf
, "remote.%s", remote
->name
);
940 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
941 return error(_("Could not remove config section '%s'"), buf
.buf
);
943 handle_push_default(remote
->name
, NULL
);
949 static void clear_push_info(void *util
, const char *string UNUSED
)
951 struct push_info
*info
= util
;
956 static void free_remote_ref_states(struct ref_states
*states
)
958 string_list_clear(&states
->new_refs
, 0);
959 string_list_clear(&states
->skipped
, 0);
960 string_list_clear(&states
->stale
, 1);
961 string_list_clear(&states
->tracked
, 0);
962 string_list_clear(&states
->heads
, 0);
963 string_list_clear_func(&states
->push
, clear_push_info
);
966 static int append_ref_to_tracked_list(const char *refname
,
967 const struct object_id
*oid UNUSED
,
968 int flags
, void *cb_data
)
970 struct ref_states
*states
= cb_data
;
971 struct refspec_item refspec
;
973 if (flags
& REF_ISSYMREF
)
976 memset(&refspec
, 0, sizeof(refspec
));
977 refspec
.dst
= (char *)refname
;
978 if (!remote_find_tracking(states
->remote
, &refspec
))
979 string_list_append(&states
->tracked
, abbrev_branch(refspec
.src
));
984 static int get_remote_ref_states(const char *name
,
985 struct ref_states
*states
,
988 states
->remote
= remote_get(name
);
990 return error(_("No such remote: '%s'"), name
);
995 struct transport
*transport
;
996 const struct ref
*remote_refs
;
998 transport
= transport_get(states
->remote
, states
->remote
->url_nr
> 0 ?
999 states
->remote
->url
[0] : NULL
);
1000 remote_refs
= transport_get_remote_refs(transport
, NULL
);
1002 states
->queried
= 1;
1003 if (query
& GET_REF_STATES
)
1004 get_ref_states(remote_refs
, states
);
1005 if (query
& GET_HEAD_NAMES
)
1006 get_head_names(remote_refs
, states
);
1007 if (query
& GET_PUSH_REF_STATES
)
1008 get_push_ref_states(remote_refs
, states
);
1009 transport_disconnect(transport
);
1011 for_each_ref(append_ref_to_tracked_list
, states
);
1012 string_list_sort(&states
->tracked
);
1013 get_push_ref_states_noquery(states
);
1020 struct string_list list
;
1021 struct ref_states states
;
1026 #define SHOW_INFO_INIT { \
1027 .list = STRING_LIST_INIT_DUP, \
1028 .states = REF_STATES_INIT, \
1031 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
1033 struct show_info
*info
= cb_data
;
1034 int n
= strlen(item
->string
);
1035 if (n
> info
->width
)
1037 string_list_insert(&info
->list
, item
->string
);
1041 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
1043 struct show_info
*info
= cb_data
;
1044 struct ref_states
*states
= &info
->states
;
1045 const char *name
= item
->string
;
1047 if (states
->queried
) {
1048 const char *fmt
= "%s";
1049 const char *arg
= "";
1050 if (string_list_has_string(&states
->new_refs
, name
)) {
1051 fmt
= _(" new (next fetch will store in remotes/%s)");
1052 arg
= states
->remote
->name
;
1053 } else if (string_list_has_string(&states
->tracked
, name
))
1054 arg
= _(" tracked");
1055 else if (string_list_has_string(&states
->skipped
, name
))
1056 arg
= _(" skipped");
1057 else if (string_list_has_string(&states
->stale
, name
))
1058 arg
= _(" stale (use 'git remote prune' to remove)");
1061 printf(" %-*s", info
->width
, name
);
1065 printf(" %s\n", name
);
1070 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
1072 struct show_info
*show_info
= cb_data
;
1073 struct ref_states
*states
= &show_info
->states
;
1074 struct branch_info
*branch_info
= branch_item
->util
;
1075 struct string_list_item
*item
;
1078 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
1079 strcmp(states
->remote
->name
, branch_info
->remote_name
))
1081 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
1082 show_info
->width
= n
;
1083 if (branch_info
->rebase
>= REBASE_TRUE
)
1084 show_info
->any_rebase
= 1;
1086 item
= string_list_insert(&show_info
->list
, branch_item
->string
);
1087 item
->util
= branch_info
;
1092 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
1094 struct show_info
*show_info
= cb_data
;
1095 struct branch_info
*branch_info
= item
->util
;
1096 struct string_list
*merge
= &branch_info
->merge
;
1097 int width
= show_info
->width
+ 4;
1100 if (branch_info
->rebase
>= REBASE_TRUE
&& branch_info
->merge
.nr
> 1) {
1101 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1106 printf(" %-*s ", show_info
->width
, item
->string
);
1107 if (branch_info
->rebase
>= REBASE_TRUE
) {
1109 if (branch_info
->rebase
== REBASE_INTERACTIVE
)
1110 msg
= _("rebases interactively onto remote %s");
1111 else if (branch_info
->rebase
== REBASE_MERGES
)
1112 msg
= _("rebases interactively (with merges) onto "
1115 msg
= _("rebases onto remote %s");
1116 printf_ln(msg
, merge
->items
[0].string
);
1118 } else if (show_info
->any_rebase
) {
1119 printf_ln(_(" merges with remote %s"), merge
->items
[0].string
);
1122 printf_ln(_("merges with remote %s"), merge
->items
[0].string
);
1124 for (i
= 1; i
< merge
->nr
; i
++)
1125 printf(_("%-*s and with remote %s\n"), width
, "",
1126 merge
->items
[i
].string
);
1131 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
1133 struct show_info
*show_info
= cb_data
;
1134 struct push_info
*push_info
= push_item
->util
;
1135 struct string_list_item
*item
;
1137 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
1138 show_info
->width
= n
;
1139 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
1140 show_info
->width2
= n
;
1141 item
= string_list_append(&show_info
->list
, push_item
->string
);
1142 item
->util
= push_item
->util
;
1147 * Sorting comparison for a string list that has push_info
1148 * structs in its util field
1150 static int cmp_string_with_push(const void *va
, const void *vb
)
1152 const struct string_list_item
*a
= va
;
1153 const struct string_list_item
*b
= vb
;
1154 const struct push_info
*a_push
= a
->util
;
1155 const struct push_info
*b_push
= b
->util
;
1156 int cmp
= strcmp(a
->string
, b
->string
);
1157 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
1160 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
1162 struct show_info
*show_info
= cb_data
;
1163 struct push_info
*push_info
= item
->util
;
1164 const char *src
= item
->string
, *status
= NULL
;
1166 switch (push_info
->status
) {
1167 case PUSH_STATUS_CREATE
:
1168 status
= _("create");
1170 case PUSH_STATUS_DELETE
:
1171 status
= _("delete");
1174 case PUSH_STATUS_UPTODATE
:
1175 status
= _("up to date");
1177 case PUSH_STATUS_FASTFORWARD
:
1178 status
= _("fast-forwardable");
1180 case PUSH_STATUS_OUTOFDATE
:
1181 status
= _("local out of date");
1183 case PUSH_STATUS_NOTQUERIED
:
1187 if (push_info
->forced
)
1188 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info
->width
, src
,
1189 show_info
->width2
, push_info
->dest
, status
);
1191 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info
->width
, src
,
1192 show_info
->width2
, push_info
->dest
, status
);
1194 if (push_info
->forced
)
1195 printf_ln(_(" %-*s forces to %s"), show_info
->width
, src
,
1198 printf_ln(_(" %-*s pushes to %s"), show_info
->width
, src
,
1204 static int get_one_entry(struct remote
*remote
, void *priv
)
1206 struct string_list
*list
= priv
;
1207 struct strbuf remote_info_buf
= STRBUF_INIT
;
1211 if (remote
->url_nr
> 0) {
1212 struct strbuf promisor_config
= STRBUF_INIT
;
1213 const char *partial_clone_filter
= NULL
;
1215 strbuf_addf(&promisor_config
, "remote.%s.partialclonefilter", remote
->name
);
1216 strbuf_addf(&remote_info_buf
, "%s (fetch)", remote
->url
[0]);
1217 if (!git_config_get_string_tmp(promisor_config
.buf
, &partial_clone_filter
))
1218 strbuf_addf(&remote_info_buf
, " [%s]", partial_clone_filter
);
1220 strbuf_release(&promisor_config
);
1221 string_list_append(list
, remote
->name
)->util
=
1222 strbuf_detach(&remote_info_buf
, NULL
);
1224 string_list_append(list
, remote
->name
)->util
= NULL
;
1225 if (remote
->pushurl_nr
) {
1226 url
= remote
->pushurl
;
1227 url_nr
= remote
->pushurl_nr
;
1230 url_nr
= remote
->url_nr
;
1232 for (i
= 0; i
< url_nr
; i
++)
1234 strbuf_addf(&remote_info_buf
, "%s (push)", url
[i
]);
1235 string_list_append(list
, remote
->name
)->util
=
1236 strbuf_detach(&remote_info_buf
, NULL
);
1242 static int show_all(void)
1244 struct string_list list
= STRING_LIST_INIT_DUP
;
1247 result
= for_each_remote(get_one_entry
, &list
);
1252 string_list_sort(&list
);
1253 for (i
= 0; i
< list
.nr
; i
++) {
1254 struct string_list_item
*item
= list
.items
+ i
;
1256 printf("%s\t%s\n", item
->string
,
1257 item
->util
? (const char *)item
->util
: "");
1259 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1261 printf("%s\n", item
->string
);
1265 string_list_clear(&list
, 1);
1269 static int show(int argc
, const char **argv
, const char *prefix
)
1271 int no_query
= 0, result
= 0, query_flag
= 0;
1272 struct option options
[] = {
1273 OPT_BOOL('n', NULL
, &no_query
, N_("do not query remotes")),
1276 struct show_info info
= SHOW_INFO_INIT
;
1278 argc
= parse_options(argc
, argv
, prefix
, options
,
1279 builtin_remote_show_usage
,
1286 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1288 for (; argc
; argc
--, argv
++) {
1293 get_remote_ref_states(*argv
, &info
.states
, query_flag
);
1295 printf_ln(_("* remote %s"), *argv
);
1296 printf_ln(_(" Fetch URL: %s"), info
.states
.remote
->url_nr
> 0 ?
1297 info
.states
.remote
->url
[0] : _("(no URL)"));
1298 if (info
.states
.remote
->pushurl_nr
) {
1299 url
= info
.states
.remote
->pushurl
;
1300 url_nr
= info
.states
.remote
->pushurl_nr
;
1302 url
= info
.states
.remote
->url
;
1303 url_nr
= info
.states
.remote
->url_nr
;
1305 for (i
= 0; i
< url_nr
; i
++)
1307 * TRANSLATORS: the colon ':' should align
1308 * with the one in " Fetch URL: %s"
1311 printf_ln(_(" Push URL: %s"), url
[i
]);
1313 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1315 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1316 else if (!info
.states
.heads
.nr
)
1317 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1318 else if (info
.states
.heads
.nr
== 1)
1319 printf_ln(_(" HEAD branch: %s"), info
.states
.heads
.items
[0].string
);
1321 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1322 " may be one of the following):\n"));
1323 for (i
= 0; i
< info
.states
.heads
.nr
; i
++)
1324 printf(" %s\n", info
.states
.heads
.items
[i
].string
);
1327 /* remote branch info */
1329 for_each_string_list(&info
.states
.new_refs
, add_remote_to_show_info
, &info
);
1330 for_each_string_list(&info
.states
.skipped
, add_remote_to_show_info
, &info
);
1331 for_each_string_list(&info
.states
.tracked
, add_remote_to_show_info
, &info
);
1332 for_each_string_list(&info
.states
.stale
, add_remote_to_show_info
, &info
);
1334 printf_ln(Q_(" Remote branch:%s",
1335 " Remote branches:%s",
1337 no_query
? _(" (status not queried)") : "");
1338 for_each_string_list(&info
.list
, show_remote_info_item
, &info
);
1339 string_list_clear(&info
.list
, 0);
1343 info
.any_rebase
= 0;
1344 for_each_string_list(&branch_list
, add_local_to_show_info
, &info
);
1346 printf_ln(Q_(" Local branch configured for 'git pull':",
1347 " Local branches configured for 'git pull':",
1349 for_each_string_list(&info
.list
, show_local_info_item
, &info
);
1350 string_list_clear(&info
.list
, 0);
1353 if (info
.states
.remote
->mirror
)
1354 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1356 info
.width
= info
.width2
= 0;
1357 for_each_string_list(&info
.states
.push
, add_push_to_show_info
, &info
);
1358 QSORT(info
.list
.items
, info
.list
.nr
, cmp_string_with_push
);
1360 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1361 " Local refs configured for 'git push'%s:",
1363 no_query
? _(" (status not queried)") : "");
1364 for_each_string_list(&info
.list
, show_push_info_item
, &info
);
1365 string_list_clear(&info
.list
, 0);
1367 free_remote_ref_states(&info
.states
);
1373 static int set_head(int argc
, const char **argv
, const char *prefix
)
1375 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1376 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1377 char *head_name
= NULL
;
1379 struct option options
[] = {
1380 OPT_BOOL('a', "auto", &opt_a
,
1381 N_("set refs/remotes/<name>/HEAD according to remote")),
1382 OPT_BOOL('d', "delete", &opt_d
,
1383 N_("delete refs/remotes/<name>/HEAD")),
1386 argc
= parse_options(argc
, argv
, prefix
, options
,
1387 builtin_remote_sethead_usage
, 0);
1389 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1391 if (!opt_a
&& !opt_d
&& argc
== 2) {
1392 head_name
= xstrdup(argv
[1]);
1393 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1394 struct ref_states states
= REF_STATES_INIT
;
1395 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1396 if (!states
.heads
.nr
)
1397 result
|= error(_("Cannot determine remote HEAD"));
1398 else if (states
.heads
.nr
> 1) {
1399 result
|= error(_("Multiple remote HEAD branches. "
1400 "Please choose one explicitly with:"));
1401 for (i
= 0; i
< states
.heads
.nr
; i
++)
1402 fprintf(stderr
, " git remote set-head %s %s\n",
1403 argv
[0], states
.heads
.items
[i
].string
);
1405 head_name
= xstrdup(states
.heads
.items
[0].string
);
1406 free_remote_ref_states(&states
);
1407 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1408 if (delete_ref(NULL
, buf
.buf
, NULL
, REF_NO_DEREF
))
1409 result
|= error(_("Could not delete %s"), buf
.buf
);
1411 usage_with_options(builtin_remote_sethead_usage
, options
);
1414 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1415 /* make sure it's valid */
1416 if (!ref_exists(buf2
.buf
))
1417 result
|= error(_("Not a valid ref: %s"), buf2
.buf
);
1418 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1419 result
|= error(_("Could not setup %s"), buf
.buf
);
1421 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1425 strbuf_release(&buf
);
1426 strbuf_release(&buf2
);
1430 static int prune_remote(const char *remote
, int dry_run
)
1433 struct ref_states states
= REF_STATES_INIT
;
1434 struct string_list refs_to_prune
= STRING_LIST_INIT_NODUP
;
1435 struct string_list_item
*item
;
1436 const char *dangling_msg
= dry_run
1437 ? _(" %s will become dangling!")
1438 : _(" %s has become dangling!");
1440 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1442 if (!states
.stale
.nr
) {
1443 free_remote_ref_states(&states
);
1447 printf_ln(_("Pruning %s"), remote
);
1448 printf_ln(_("URL: %s"),
1449 states
.remote
->url_nr
1450 ? states
.remote
->url
[0]
1453 for_each_string_list_item(item
, &states
.stale
)
1454 string_list_append(&refs_to_prune
, item
->util
);
1455 string_list_sort(&refs_to_prune
);
1458 result
|= delete_refs("remote: prune", &refs_to_prune
, 0);
1460 for_each_string_list_item(item
, &states
.stale
) {
1461 const char *refname
= item
->util
;
1464 printf_ln(_(" * [would prune] %s"),
1465 abbrev_ref(refname
, "refs/remotes/"));
1467 printf_ln(_(" * [pruned] %s"),
1468 abbrev_ref(refname
, "refs/remotes/"));
1471 warn_dangling_symrefs(stdout
, dangling_msg
, &refs_to_prune
);
1473 string_list_clear(&refs_to_prune
, 0);
1474 free_remote_ref_states(&states
);
1478 static int prune(int argc
, const char **argv
, const char *prefix
)
1480 int dry_run
= 0, result
= 0;
1481 struct option options
[] = {
1482 OPT__DRY_RUN(&dry_run
, N_("dry run")),
1486 argc
= parse_options(argc
, argv
, prefix
, options
,
1487 builtin_remote_prune_usage
, 0);
1490 usage_with_options(builtin_remote_prune_usage
, options
);
1492 for (; argc
; argc
--, argv
++)
1493 result
|= prune_remote(*argv
, dry_run
);
1498 static int get_remote_default(const char *key
, const char *value UNUSED
, void *priv
)
1500 if (strcmp(key
, "remotes.default") == 0) {
1507 static int update(int argc
, const char **argv
, const char *prefix
)
1510 struct option options
[] = {
1511 OPT_BOOL('p', "prune", &prune
,
1512 N_("prune remotes after fetching")),
1515 struct child_process cmd
= CHILD_PROCESS_INIT
;
1516 int default_defined
= 0;
1518 argc
= parse_options(argc
, argv
, prefix
, options
,
1519 builtin_remote_update_usage
,
1520 PARSE_OPT_KEEP_ARGV0
);
1522 strvec_push(&cmd
.args
, "fetch");
1525 strvec_push(&cmd
.args
, prune
? "--prune" : "--no-prune");
1527 strvec_push(&cmd
.args
, "-v");
1528 strvec_push(&cmd
.args
, "--multiple");
1530 strvec_push(&cmd
.args
, "default");
1531 for (i
= 1; i
< argc
; i
++)
1532 strvec_push(&cmd
.args
, argv
[i
]);
1534 if (strcmp(cmd
.args
.v
[cmd
.args
.nr
-1], "default") == 0) {
1535 git_config(get_remote_default
, &default_defined
);
1536 if (!default_defined
) {
1537 strvec_pop(&cmd
.args
);
1538 strvec_push(&cmd
.args
, "--all");
1543 return run_command(&cmd
);
1546 static int remove_all_fetch_refspecs(const char *key
)
1548 return git_config_set_multivar_gently(key
, NULL
, NULL
,
1549 CONFIG_FLAGS_MULTI_REPLACE
);
1552 static void add_branches(struct remote
*remote
, const char **branches
,
1555 const char *remotename
= remote
->name
;
1556 int mirror
= remote
->mirror
;
1557 struct strbuf refspec
= STRBUF_INIT
;
1559 for (; *branches
; branches
++)
1560 add_branch(key
, *branches
, remotename
, mirror
, &refspec
);
1562 strbuf_release(&refspec
);
1565 static int set_remote_branches(const char *remotename
, const char **branches
,
1568 struct strbuf key
= STRBUF_INIT
;
1569 struct remote
*remote
;
1571 strbuf_addf(&key
, "remote.%s.fetch", remotename
);
1573 remote
= remote_get(remotename
);
1574 if (!remote_is_configured(remote
, 1)) {
1575 error(_("No such remote '%s'"), remotename
);
1579 if (!add_mode
&& remove_all_fetch_refspecs(key
.buf
)) {
1580 strbuf_release(&key
);
1583 add_branches(remote
, branches
, key
.buf
);
1585 strbuf_release(&key
);
1589 static int set_branches(int argc
, const char **argv
, const char *prefix
)
1592 struct option options
[] = {
1593 OPT_BOOL('\0', "add", &add_mode
, N_("add branch")),
1597 argc
= parse_options(argc
, argv
, prefix
, options
,
1598 builtin_remote_setbranches_usage
, 0);
1600 error(_("no remote specified"));
1601 usage_with_options(builtin_remote_setbranches_usage
, options
);
1605 return set_remote_branches(argv
[0], argv
+ 1, add_mode
);
1608 static int get_url(int argc
, const char **argv
, const char *prefix
)
1610 int i
, push_mode
= 0, all_mode
= 0;
1611 const char *remotename
= NULL
;
1612 struct remote
*remote
;
1615 struct option options
[] = {
1616 OPT_BOOL('\0', "push", &push_mode
,
1617 N_("query push URLs rather than fetch URLs")),
1618 OPT_BOOL('\0', "all", &all_mode
,
1619 N_("return all URLs")),
1622 argc
= parse_options(argc
, argv
, prefix
, options
,
1623 builtin_remote_geturl_usage
, 0);
1626 usage_with_options(builtin_remote_geturl_usage
, options
);
1628 remotename
= argv
[0];
1630 remote
= remote_get(remotename
);
1631 if (!remote_is_configured(remote
, 1)) {
1632 error(_("No such remote '%s'"), remotename
);
1638 url
= remote
->pushurl
;
1639 url_nr
= remote
->pushurl_nr
;
1641 /* else fetch mode */
1643 /* Use the fetch URL when no push URLs were found or requested. */
1646 url_nr
= remote
->url_nr
;
1650 die(_("no URLs configured for remote '%s'"), remotename
);
1653 for (i
= 0; i
< url_nr
; i
++)
1654 printf_ln("%s", url
[i
]);
1656 printf_ln("%s", *url
);
1662 static int set_url(int argc
, const char **argv
, const char *prefix
)
1664 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1665 int matches
= 0, negative_matches
= 0;
1666 const char *remotename
= NULL
;
1667 const char *newurl
= NULL
;
1668 const char *oldurl
= NULL
;
1669 struct remote
*remote
;
1671 const char **urlset
;
1673 struct strbuf name_buf
= STRBUF_INIT
;
1674 struct option options
[] = {
1675 OPT_BOOL('\0', "push", &push_mode
,
1676 N_("manipulate push URLs")),
1677 OPT_BOOL('\0', "add", &add_mode
,
1679 OPT_BOOL('\0', "delete", &delete_mode
,
1683 argc
= parse_options(argc
, argv
, prefix
, options
,
1684 builtin_remote_seturl_usage
,
1685 PARSE_OPT_KEEP_ARGV0
);
1687 if (add_mode
&& delete_mode
)
1688 die(_("--add --delete doesn't make sense"));
1690 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1691 usage_with_options(builtin_remote_seturl_usage
, options
);
1693 remotename
= argv
[1];
1701 remote
= remote_get(remotename
);
1702 if (!remote_is_configured(remote
, 1)) {
1703 error(_("No such remote '%s'"), remotename
);
1708 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1709 urlset
= remote
->pushurl
;
1710 urlset_nr
= remote
->pushurl_nr
;
1712 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1713 urlset
= remote
->url
;
1714 urlset_nr
= remote
->url_nr
;
1717 /* Special cases that add new entry. */
1718 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1720 git_config_set_multivar(name_buf
.buf
, newurl
,
1723 git_config_set(name_buf
.buf
, newurl
);
1727 /* Old URL specified. Demand that one matches. */
1728 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1729 die(_("Invalid old URL pattern: %s"), oldurl
);
1731 for (i
= 0; i
< urlset_nr
; i
++)
1732 if (!regexec(&old_regex
, urlset
[i
], 0, NULL
, 0))
1736 if (!delete_mode
&& !matches
)
1737 die(_("No such URL found: %s"), oldurl
);
1738 if (delete_mode
&& !negative_matches
&& !push_mode
)
1739 die(_("Will not delete all non-push URLs"));
1741 regfree(&old_regex
);
1744 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1746 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
,
1747 CONFIG_FLAGS_MULTI_REPLACE
);
1749 strbuf_release(&name_buf
);
1753 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1755 parse_opt_subcommand_fn
*fn
= NULL
;
1756 struct option options
[] = {
1757 OPT__VERBOSE(&verbose
, N_("be verbose; must be placed before a subcommand")),
1758 OPT_SUBCOMMAND("add", &fn
, add
),
1759 OPT_SUBCOMMAND("rename", &fn
, mv
),
1760 OPT_SUBCOMMAND_F("rm", &fn
, rm
, PARSE_OPT_NOCOMPLETE
),
1761 OPT_SUBCOMMAND("remove", &fn
, rm
),
1762 OPT_SUBCOMMAND("set-head", &fn
, set_head
),
1763 OPT_SUBCOMMAND("set-branches", &fn
, set_branches
),
1764 OPT_SUBCOMMAND("get-url", &fn
, get_url
),
1765 OPT_SUBCOMMAND("set-url", &fn
, set_url
),
1766 OPT_SUBCOMMAND("show", &fn
, show
),
1767 OPT_SUBCOMMAND("prune", &fn
, prune
),
1768 OPT_SUBCOMMAND("update", &fn
, update
),
1772 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1773 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1776 return !!fn(argc
, argv
, prefix
);
1779 error(_("unknown subcommand: `%s'"), argv
[0]);
1780 usage_with_options(builtin_remote_usage
, options
);
1782 return !!show_all();