2 #include "parse-options.h"
5 #include "string-list.h"
7 #include "run-command.h"
10 static const char * const builtin_remote_usage
[] = {
11 "git remote [-v | --verbose]",
12 "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
13 "git remote rename <old> <new>",
14 "git remote rm <name>",
15 "git remote show [-n] <name>",
16 "git remote prune [-n | --dry-run] <name>",
17 "git remote [-v | --verbose] update [group]",
21 #define GET_REF_STATES (1<<0)
22 #define GET_HEAD_NAMES (1<<1)
26 static int show_all(void);
28 static inline int postfixcmp(const char *string
, const char *postfix
)
30 int len1
= strlen(string
), len2
= strlen(postfix
);
33 return strcmp(string
+ len1
- len2
, postfix
);
36 static int opt_parse_track(const struct option
*opt
, const char *arg
, int not)
38 struct string_list
*list
= opt
->value
;
40 string_list_clear(list
, 0);
42 string_list_append(arg
, list
);
46 static int fetch_remote(const char *name
)
48 const char *argv
[] = { "fetch", name
, NULL
, NULL
};
53 printf("Updating %s\n", name
);
54 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
55 return error("Could not fetch %s", name
);
59 static int add(int argc
, const char **argv
)
61 int fetch
= 0, mirror
= 0;
62 struct string_list track
= { NULL
, 0, 0 };
63 const char *master
= NULL
;
64 struct remote
*remote
;
65 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
66 const char *name
, *url
;
69 struct option options
[] = {
70 OPT_GROUP("add specific options"),
71 OPT_BOOLEAN('f', "fetch", &fetch
, "fetch the remote branches"),
72 OPT_CALLBACK('t', "track", &track
, "branch",
73 "branch(es) to track", opt_parse_track
),
74 OPT_STRING('m', "master", &master
, "branch", "master branch"),
75 OPT_BOOLEAN(0, "mirror", &mirror
, "no separate remotes"),
79 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
82 usage_with_options(builtin_remote_usage
, options
);
87 remote
= remote_get(name
);
88 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
89 remote
->fetch_refspec_nr
))
90 die("remote %s already exists.", name
);
92 strbuf_addf(&buf2
, "refs/heads/test:refs/remotes/%s/test", name
);
93 if (!valid_fetch_refspec(buf2
.buf
))
94 die("'%s' is not a valid remote name", name
);
96 strbuf_addf(&buf
, "remote.%s.url", name
);
97 if (git_config_set(buf
.buf
, url
))
101 strbuf_addf(&buf
, "remote.%s.fetch", name
);
104 string_list_append("*", &track
);
105 for (i
= 0; i
< track
.nr
; i
++) {
106 struct string_list_item
*item
= track
.items
+ i
;
109 strbuf_addch(&buf2
, '+');
111 strbuf_addf(&buf2
, "refs/%s:refs/%s",
112 item
->string
, item
->string
);
114 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
115 item
->string
, name
, item
->string
);
116 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
122 strbuf_addf(&buf
, "remote.%s.mirror", name
);
123 if (git_config_set(buf
.buf
, "true"))
127 if (fetch
&& fetch_remote(name
))
132 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
135 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
137 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
138 return error("Could not setup master '%s'", master
);
141 strbuf_release(&buf
);
142 strbuf_release(&buf2
);
143 string_list_clear(&track
, 0);
150 struct string_list merge
;
153 static struct string_list branch_list
;
155 static const char *abbrev_ref(const char *name
, const char *prefix
)
157 const char *abbrev
= skip_prefix(name
, prefix
);
162 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
164 static int config_read_branches(const char *key
, const char *value
, void *cb
)
166 if (!prefixcmp(key
, "branch.")) {
168 struct string_list_item
*item
;
169 struct branch_info
*info
;
170 enum { REMOTE
, MERGE
} type
;
173 if (!postfixcmp(key
, ".remote")) {
174 name
= xstrndup(key
, strlen(key
) - 7);
176 } else if (!postfixcmp(key
, ".merge")) {
177 name
= xstrndup(key
, strlen(key
) - 6);
182 item
= string_list_insert(name
, &branch_list
);
185 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
187 if (type
== REMOTE
) {
188 if (info
->remote_name
)
189 warning("more than one branch.%s", key
);
190 info
->remote_name
= xstrdup(value
);
192 char *space
= strchr(value
, ' ');
193 value
= abbrev_branch(value
);
196 merge
= xstrndup(value
, space
- value
);
197 string_list_append(merge
, &info
->merge
);
198 value
= abbrev_branch(space
+ 1);
199 space
= strchr(value
, ' ');
201 string_list_append(xstrdup(value
), &info
->merge
);
207 static void read_branches(void)
211 git_config(config_read_branches
, NULL
);
215 struct remote
*remote
;
216 struct string_list
new, stale
, tracked
, heads
;
219 static int handle_one_branch(const char *refname
,
220 const unsigned char *sha1
, int flags
, void *cb_data
)
222 struct ref_states
*states
= cb_data
;
223 struct refspec refspec
;
225 memset(&refspec
, 0, sizeof(refspec
));
226 refspec
.dst
= (char *)refname
;
227 if (!remote_find_tracking(states
->remote
, &refspec
)) {
228 struct string_list_item
*item
;
229 const char *name
= abbrev_branch(refspec
.src
);
230 /* symbolic refs pointing nowhere were handled already */
231 if ((flags
& REF_ISSYMREF
) ||
232 string_list_has_string(&states
->tracked
, name
) ||
233 string_list_has_string(&states
->new, name
))
235 item
= string_list_append(name
, &states
->stale
);
236 item
->util
= xstrdup(refname
);
241 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
243 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
247 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
248 if (get_fetch_map(remote_refs
, states
->remote
->fetch
+ i
, &tail
, 1))
249 die("Could not get fetch map for refspec %s",
250 states
->remote
->fetch_refspec
[i
]);
252 states
->new.strdup_strings
= states
->tracked
.strdup_strings
= 1;
253 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
254 unsigned char sha1
[20];
255 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
256 string_list_append(abbrev_branch(ref
->name
), &states
->new);
258 string_list_append(abbrev_branch(ref
->name
), &states
->tracked
);
260 free_refs(fetch_map
);
262 sort_string_list(&states
->new);
263 sort_string_list(&states
->tracked
);
264 for_each_ref(handle_one_branch
, states
);
265 sort_string_list(&states
->stale
);
270 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
272 struct ref
*ref
, *matches
;
273 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
274 struct refspec refspec
;
278 refspec
.src
= refspec
.dst
= "refs/heads/";
279 states
->heads
.strdup_strings
= 1;
280 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
281 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
283 for(ref
= matches
; ref
; ref
= ref
->next
)
284 string_list_append(abbrev_branch(ref
->name
), &states
->heads
);
286 free_refs(fetch_map
);
292 struct known_remote
{
293 struct known_remote
*next
;
294 struct remote
*remote
;
297 struct known_remotes
{
298 struct remote
*to_delete
;
299 struct known_remote
*list
;
302 static int add_known_remote(struct remote
*remote
, void *cb_data
)
304 struct known_remotes
*all
= cb_data
;
305 struct known_remote
*r
;
307 if (!strcmp(all
->to_delete
->name
, remote
->name
))
310 r
= xmalloc(sizeof(*r
));
317 struct branches_for_remote
{
318 struct remote
*remote
;
319 struct string_list
*branches
, *skipped
;
320 struct known_remotes
*keep
;
323 static int add_branch_for_removal(const char *refname
,
324 const unsigned char *sha1
, int flags
, void *cb_data
)
326 struct branches_for_remote
*branches
= cb_data
;
327 struct refspec refspec
;
328 struct string_list_item
*item
;
329 struct known_remote
*kr
;
331 memset(&refspec
, 0, sizeof(refspec
));
332 refspec
.dst
= (char *)refname
;
333 if (remote_find_tracking(branches
->remote
, &refspec
))
336 /* don't delete a branch if another remote also uses it */
337 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
338 memset(&refspec
, 0, sizeof(refspec
));
339 refspec
.dst
= (char *)refname
;
340 if (!remote_find_tracking(kr
->remote
, &refspec
))
344 /* don't delete non-remote refs */
345 if (prefixcmp(refname
, "refs/remotes")) {
346 /* advise user how to delete local branches */
347 if (!prefixcmp(refname
, "refs/heads/"))
348 string_list_append(abbrev_branch(refname
),
350 /* silently skip over other non-remote refs */
354 /* make sure that symrefs are deleted */
355 if (flags
& REF_ISSYMREF
)
356 return unlink(git_path("%s", refname
));
358 item
= string_list_append(refname
, branches
->branches
);
359 item
->util
= xmalloc(20);
360 hashcpy(item
->util
, sha1
);
368 struct string_list
*remote_branches
;
371 static int read_remote_branches(const char *refname
,
372 const unsigned char *sha1
, int flags
, void *cb_data
)
374 struct rename_info
*rename
= cb_data
;
375 struct strbuf buf
= STRBUF_INIT
;
376 struct string_list_item
*item
;
378 unsigned char orig_sha1
[20];
381 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
382 if(!prefixcmp(refname
, buf
.buf
)) {
383 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
384 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
385 if (flag
& REF_ISSYMREF
)
386 item
->util
= xstrdup(symref
);
394 static int migrate_file(struct remote
*remote
)
396 struct strbuf buf
= STRBUF_INIT
;
400 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
401 for (i
= 0; i
< remote
->url_nr
; i
++)
402 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
403 return error("Could not append '%s' to '%s'",
404 remote
->url
[i
], buf
.buf
);
406 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
407 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
408 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
409 return error("Could not append '%s' to '%s'",
410 remote
->push_refspec
[i
], buf
.buf
);
412 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
413 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
414 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
415 return error("Could not append '%s' to '%s'",
416 remote
->fetch_refspec
[i
], buf
.buf
);
417 if (remote
->origin
== REMOTE_REMOTES
)
418 path
= git_path("remotes/%s", remote
->name
);
419 else if (remote
->origin
== REMOTE_BRANCHES
)
420 path
= git_path("branches/%s", remote
->name
);
421 if (path
&& unlink(path
))
422 warning("failed to remove '%s'", path
);
426 static int mv(int argc
, const char **argv
)
428 struct option options
[] = {
431 struct remote
*oldremote
, *newremote
;
432 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
433 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
434 struct rename_info rename
;
438 usage_with_options(builtin_remote_usage
, options
);
440 rename
.old
= argv
[1];
441 rename
.new = argv
[2];
442 rename
.remote_branches
= &remote_branches
;
444 oldremote
= remote_get(rename
.old
);
446 die("No such remote: %s", rename
.old
);
448 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
449 return migrate_file(oldremote
);
451 newremote
= remote_get(rename
.new);
452 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
453 die("remote %s already exists.", rename
.new);
455 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
456 if (!valid_fetch_refspec(buf
.buf
))
457 die("'%s' is not a valid remote name", rename
.new);
460 strbuf_addf(&buf
, "remote.%s", rename
.old
);
461 strbuf_addf(&buf2
, "remote.%s", rename
.new);
462 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
463 return error("Could not rename config section '%s' to '%s'",
467 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
468 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
469 return error("Could not remove config section '%s'", buf
.buf
);
470 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
474 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
475 ptr
= strstr(buf2
.buf
, rename
.old
);
477 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
478 rename
.new, strlen(rename
.new));
479 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
480 return error("Could not append '%s'", buf
.buf
);
484 for (i
= 0; i
< branch_list
.nr
; i
++) {
485 struct string_list_item
*item
= branch_list
.items
+ i
;
486 struct branch_info
*info
= item
->util
;
487 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old
)) {
489 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
490 if (git_config_set(buf
.buf
, rename
.new)) {
491 return error("Could not set '%s'", buf
.buf
);
497 * First remove symrefs, then rename the rest, finally create
500 for_each_ref(read_remote_branches
, &rename
);
501 for (i
= 0; i
< remote_branches
.nr
; i
++) {
502 struct string_list_item
*item
= remote_branches
.items
+ i
;
504 unsigned char sha1
[20];
507 symref
= resolve_ref(item
->string
, sha1
, 1, &flag
);
508 if (!(flag
& REF_ISSYMREF
))
510 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
511 die("deleting '%s' failed", item
->string
);
513 for (i
= 0; i
< remote_branches
.nr
; i
++) {
514 struct string_list_item
*item
= remote_branches
.items
+ i
;
519 strbuf_addstr(&buf
, item
->string
);
520 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
521 rename
.new, strlen(rename
.new));
523 strbuf_addf(&buf2
, "remote: renamed %s to %s",
524 item
->string
, buf
.buf
);
525 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
526 die("renaming '%s' failed", item
->string
);
528 for (i
= 0; i
< remote_branches
.nr
; i
++) {
529 struct string_list_item
*item
= remote_branches
.items
+ i
;
534 strbuf_addstr(&buf
, item
->string
);
535 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
536 rename
.new, strlen(rename
.new));
538 strbuf_addstr(&buf2
, item
->util
);
539 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
540 rename
.new, strlen(rename
.new));
542 strbuf_addf(&buf3
, "remote: renamed %s to %s",
543 item
->string
, buf
.buf
);
544 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
545 die("creating '%s' failed", buf
.buf
);
550 static int remove_branches(struct string_list
*branches
)
553 for (i
= 0; i
< branches
->nr
; i
++) {
554 struct string_list_item
*item
= branches
->items
+ i
;
555 const char *refname
= item
->string
;
556 unsigned char *sha1
= item
->util
;
558 if (delete_ref(refname
, sha1
, 0))
559 result
|= error("Could not remove branch %s", refname
);
564 static int rm(int argc
, const char **argv
)
566 struct option options
[] = {
569 struct remote
*remote
;
570 struct strbuf buf
= STRBUF_INIT
;
571 struct known_remotes known_remotes
= { NULL
, NULL
};
572 struct string_list branches
= { NULL
, 0, 0, 1 };
573 struct string_list skipped
= { NULL
, 0, 0, 1 };
574 struct branches_for_remote cb_data
= {
575 NULL
, &branches
, &skipped
, &known_remotes
580 usage_with_options(builtin_remote_usage
, options
);
582 remote
= remote_get(argv
[1]);
584 die("No such remote: %s", argv
[1]);
586 known_remotes
.to_delete
= remote
;
587 for_each_remote(add_known_remote
, &known_remotes
);
589 strbuf_addf(&buf
, "remote.%s", remote
->name
);
590 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
591 return error("Could not remove config section '%s'", buf
.buf
);
594 for (i
= 0; i
< branch_list
.nr
; i
++) {
595 struct string_list_item
*item
= branch_list
.items
+ i
;
596 struct branch_info
*info
= item
->util
;
597 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
598 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
599 for (k
= keys
; *k
; k
++) {
601 strbuf_addf(&buf
, "branch.%s.%s",
603 if (git_config_set(buf
.buf
, NULL
)) {
604 strbuf_release(&buf
);
612 * We cannot just pass a function to for_each_ref() which deletes
613 * the branches one by one, since for_each_ref() relies on cached
614 * refs, which are invalidated when deleting a branch.
616 cb_data
.remote
= remote
;
617 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
618 strbuf_release(&buf
);
621 result
= remove_branches(&branches
);
622 string_list_clear(&branches
, 1);
625 fprintf(stderr
, skipped
.nr
== 1 ?
626 "Note: A non-remote branch was not removed; "
627 "to delete it, use:\n" :
628 "Note: Non-remote branches were not removed; "
629 "to delete them, use:\n");
630 for (i
= 0; i
< skipped
.nr
; i
++)
631 fprintf(stderr
, " git branch -d %s\n",
632 skipped
.items
[i
].string
);
634 string_list_clear(&skipped
, 0);
639 static void show_list(const char *title
, struct string_list
*list
,
640 const char *extra_arg
)
647 printf(title
, list
->nr
> 1 ? "es" : "", extra_arg
);
649 for (i
= 0; i
< list
->nr
; i
++)
650 printf(" %s\n", list
->items
[i
].string
);
653 static void free_remote_ref_states(struct ref_states
*states
)
655 string_list_clear(&states
->new, 0);
656 string_list_clear(&states
->stale
, 0);
657 string_list_clear(&states
->tracked
, 0);
658 string_list_clear(&states
->heads
, 0);
661 static int append_ref_to_tracked_list(const char *refname
,
662 const unsigned char *sha1
, int flags
, void *cb_data
)
664 struct ref_states
*states
= cb_data
;
665 struct refspec refspec
;
667 if (flags
& REF_ISSYMREF
)
670 memset(&refspec
, 0, sizeof(refspec
));
671 refspec
.dst
= (char *)refname
;
672 if (!remote_find_tracking(states
->remote
, &refspec
))
673 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
678 static int get_remote_ref_states(const char *name
,
679 struct ref_states
*states
,
682 struct transport
*transport
;
683 const struct ref
*remote_refs
;
685 states
->remote
= remote_get(name
);
687 return error("No such remote: %s", name
);
692 transport
= transport_get(NULL
, states
->remote
->url_nr
> 0 ?
693 states
->remote
->url
[0] : NULL
);
694 remote_refs
= transport_get_remote_refs(transport
);
695 transport_disconnect(transport
);
697 if (query
& GET_REF_STATES
)
698 get_ref_states(remote_refs
, states
);
699 if (query
& GET_HEAD_NAMES
)
700 get_head_names(remote_refs
, states
);
702 for_each_ref(append_ref_to_tracked_list
, states
);
703 sort_string_list(&states
->tracked
);
709 static int show(int argc
, const char **argv
)
711 int no_query
= 0, result
= 0, query_flag
= 0;
712 struct option options
[] = {
713 OPT_GROUP("show specific options"),
714 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
717 struct ref_states states
;
719 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
725 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
);
727 memset(&states
, 0, sizeof(states
));
728 for (; argc
; argc
--, argv
++) {
731 get_remote_ref_states(*argv
, &states
, query_flag
);
733 printf("* remote %s\n URL: %s\n", *argv
,
734 states
.remote
->url_nr
> 0 ?
735 states
.remote
->url
[0] : "(no URL)");
737 printf(" HEAD branch: (not queried)\n");
738 else if (!states
.heads
.nr
)
739 printf(" HEAD branch: (unknown)\n");
740 else if (states
.heads
.nr
== 1)
741 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
743 printf(" HEAD branch (remote HEAD is ambiguous,"
744 " may be one of the following):\n");
745 for (i
= 0; i
< states
.heads
.nr
; i
++)
746 printf(" %s\n", states
.heads
.items
[i
].string
);
749 for (i
= 0; i
< branch_list
.nr
; i
++) {
750 struct string_list_item
*branch
= branch_list
.items
+ i
;
751 struct branch_info
*info
= branch
->util
;
754 if (!info
->merge
.nr
|| strcmp(*argv
, info
->remote_name
))
756 printf(" Remote branch%s merged with 'git pull' "
757 "while on branch %s\n ",
758 info
->merge
.nr
> 1 ? "es" : "",
760 for (j
= 0; j
< info
->merge
.nr
; j
++)
761 printf(" %s", info
->merge
.items
[j
].string
);
766 show_list(" New remote branch%s (next fetch "
767 "will store in remotes/%s)",
768 &states
.new, states
.remote
->name
);
769 show_list(" Stale tracking branch%s (use 'git remote "
770 "prune')", &states
.stale
, "");
773 show_list(" Tracked remote branch%s", &states
.tracked
, "");
775 if (states
.remote
->push_refspec_nr
) {
776 printf(" Local branch%s pushed with 'git push'\n",
777 states
.remote
->push_refspec_nr
> 1 ?
779 for (i
= 0; i
< states
.remote
->push_refspec_nr
; i
++) {
780 struct refspec
*spec
= states
.remote
->push
+ i
;
781 printf(" %s%s%s%s\n",
782 spec
->force
? "+" : "",
783 abbrev_branch(spec
->src
),
784 spec
->dst
? ":" : "",
785 spec
->dst
? abbrev_branch(spec
->dst
) : "");
789 free_remote_ref_states(&states
);
795 static int prune(int argc
, const char **argv
)
797 int dry_run
= 0, result
= 0;
798 struct option options
[] = {
799 OPT_GROUP("prune specific options"),
800 OPT__DRY_RUN(&dry_run
),
803 struct ref_states states
;
804 const char *dangling_msg
;
806 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
809 usage_with_options(builtin_remote_usage
, options
);
811 dangling_msg
= (dry_run
812 ? " %s will become dangling!\n"
813 : " %s has become dangling!\n");
815 memset(&states
, 0, sizeof(states
));
816 for (; argc
; argc
--, argv
++) {
819 get_remote_ref_states(*argv
, &states
, GET_REF_STATES
);
821 if (states
.stale
.nr
) {
822 printf("Pruning %s\n", *argv
);
824 states
.remote
->url_nr
825 ? states
.remote
->url
[0]
829 for (i
= 0; i
< states
.stale
.nr
; i
++) {
830 const char *refname
= states
.stale
.items
[i
].util
;
833 result
|= delete_ref(refname
, NULL
, 0);
835 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
836 abbrev_ref(refname
, "refs/remotes/"));
837 warn_dangling_symref(dangling_msg
, refname
);
840 free_remote_ref_states(&states
);
846 static int get_one_remote_for_update(struct remote
*remote
, void *priv
)
848 struct string_list
*list
= priv
;
849 if (!remote
->skip_default_update
)
850 string_list_append(remote
->name
, list
);
854 struct remote_group
{
856 struct string_list
*list
;
859 static int get_remote_group(const char *key
, const char *value
, void *cb
)
861 if (!prefixcmp(key
, "remotes.") &&
862 !strcmp(key
+ 8, remote_group
.name
)) {
863 /* split list by white space */
864 int space
= strcspn(value
, " \t\n");
867 string_list_append(xstrndup(value
, space
),
869 value
+= space
+ (value
[space
] != '\0');
870 space
= strcspn(value
, " \t\n");
877 static int update(int argc
, const char **argv
)
880 struct string_list list
= { NULL
, 0, 0, 0 };
881 static const char *default_argv
[] = { NULL
, "default", NULL
};
888 remote_group
.list
= &list
;
889 for (i
= 1; i
< argc
; i
++) {
890 remote_group
.name
= argv
[i
];
891 result
= git_config(get_remote_group
, NULL
);
894 if (!result
&& !list
.nr
&& argc
== 2 && !strcmp(argv
[1], "default"))
895 result
= for_each_remote(get_one_remote_for_update
, &list
);
897 for (i
= 0; i
< list
.nr
; i
++)
898 result
|= fetch_remote(list
.items
[i
].string
);
900 /* all names were strdup()ed or strndup()ed */
901 list
.strdup_strings
= 1;
902 string_list_clear(&list
, 0);
907 static int get_one_entry(struct remote
*remote
, void *priv
)
909 struct string_list
*list
= priv
;
911 if (remote
->url_nr
> 0) {
914 for (i
= 0; i
< remote
->url_nr
; i
++)
915 string_list_append(remote
->name
, list
)->util
= (void *)remote
->url
[i
];
917 string_list_append(remote
->name
, list
)->util
= NULL
;
922 static int show_all(void)
924 struct string_list list
= { NULL
, 0, 0 };
925 int result
= for_each_remote(get_one_entry
, &list
);
930 sort_string_list(&list
);
931 for (i
= 0; i
< list
.nr
; i
++) {
932 struct string_list_item
*item
= list
.items
+ i
;
934 printf("%s\t%s\n", item
->string
,
935 item
->util
? (const char *)item
->util
: "");
937 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
939 printf("%s\n", item
->string
);
946 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
948 struct option options
[] = {
949 OPT__VERBOSE(&verbose
),
954 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
955 PARSE_OPT_STOP_AT_NON_OPTION
);
959 else if (!strcmp(argv
[0], "add"))
960 result
= add(argc
, argv
);
961 else if (!strcmp(argv
[0], "rename"))
962 result
= mv(argc
, argv
);
963 else if (!strcmp(argv
[0], "rm"))
964 result
= rm(argc
, argv
);
965 else if (!strcmp(argv
[0], "show"))
966 result
= show(argc
, argv
);
967 else if (!strcmp(argv
[0], "prune"))
968 result
= prune(argc
, argv
);
969 else if (!strcmp(argv
[0], "update"))
970 result
= update(argc
, argv
);
972 error("Unknown subcommand: %s", argv
[0]);
973 usage_with_options(builtin_remote_usage
, options
);
976 return result
? 1 : 0;