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]",
23 static int show_all(void);
25 static inline int postfixcmp(const char *string
, const char *postfix
)
27 int len1
= strlen(string
), len2
= strlen(postfix
);
30 return strcmp(string
+ len1
- len2
, postfix
);
33 static int opt_parse_track(const struct option
*opt
, const char *arg
, int not)
35 struct string_list
*list
= opt
->value
;
37 string_list_clear(list
, 0);
39 string_list_append(arg
, list
);
43 static int fetch_remote(const char *name
)
45 const char *argv
[] = { "fetch", name
, NULL
, NULL
};
50 printf("Updating %s\n", name
);
51 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
52 return error("Could not fetch %s", name
);
56 static int add(int argc
, const char **argv
)
58 int fetch
= 0, mirror
= 0;
59 struct string_list track
= { NULL
, 0, 0 };
60 const char *master
= NULL
;
61 struct remote
*remote
;
62 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
63 const char *name
, *url
;
66 struct option options
[] = {
67 OPT_GROUP("add specific options"),
68 OPT_BOOLEAN('f', "fetch", &fetch
, "fetch the remote branches"),
69 OPT_CALLBACK('t', "track", &track
, "branch",
70 "branch(es) to track", opt_parse_track
),
71 OPT_STRING('m', "master", &master
, "branch", "master branch"),
72 OPT_BOOLEAN(0, "mirror", &mirror
, "no separate remotes"),
76 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
79 usage_with_options(builtin_remote_usage
, options
);
84 remote
= remote_get(name
);
85 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
86 remote
->fetch_refspec_nr
))
87 die("remote %s already exists.", name
);
89 strbuf_addf(&buf2
, "refs/heads/test:refs/remotes/%s/test", name
);
90 if (!valid_fetch_refspec(buf2
.buf
))
91 die("'%s' is not a valid remote name", name
);
93 strbuf_addf(&buf
, "remote.%s.url", name
);
94 if (git_config_set(buf
.buf
, url
))
98 strbuf_addf(&buf
, "remote.%s.fetch", name
);
101 string_list_append("*", &track
);
102 for (i
= 0; i
< track
.nr
; i
++) {
103 struct string_list_item
*item
= track
.items
+ i
;
106 strbuf_addch(&buf2
, '+');
108 strbuf_addf(&buf2
, "refs/%s:refs/%s",
109 item
->string
, item
->string
);
111 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
112 item
->string
, name
, item
->string
);
113 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
119 strbuf_addf(&buf
, "remote.%s.mirror", name
);
120 if (git_config_set(buf
.buf
, "true"))
124 if (fetch
&& fetch_remote(name
))
129 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
132 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
134 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
135 return error("Could not setup master '%s'", master
);
138 strbuf_release(&buf
);
139 strbuf_release(&buf2
);
140 string_list_clear(&track
, 0);
147 struct string_list merge
;
150 static struct string_list branch_list
;
152 static const char *abbrev_ref(const char *name
, const char *prefix
)
154 const char *abbrev
= skip_prefix(name
, prefix
);
159 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
161 static int config_read_branches(const char *key
, const char *value
, void *cb
)
163 if (!prefixcmp(key
, "branch.")) {
165 struct string_list_item
*item
;
166 struct branch_info
*info
;
167 enum { REMOTE
, MERGE
} type
;
170 if (!postfixcmp(key
, ".remote")) {
171 name
= xstrndup(key
, strlen(key
) - 7);
173 } else if (!postfixcmp(key
, ".merge")) {
174 name
= xstrndup(key
, strlen(key
) - 6);
179 item
= string_list_insert(name
, &branch_list
);
182 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
184 if (type
== REMOTE
) {
186 warning("more than one branch.%s", key
);
187 info
->remote
= xstrdup(value
);
189 char *space
= strchr(value
, ' ');
190 value
= abbrev_branch(value
);
193 merge
= xstrndup(value
, space
- value
);
194 string_list_append(merge
, &info
->merge
);
195 value
= abbrev_branch(space
+ 1);
196 space
= strchr(value
, ' ');
198 string_list_append(xstrdup(value
), &info
->merge
);
204 static void read_branches(void)
208 git_config(config_read_branches
, NULL
);
209 sort_string_list(&branch_list
);
213 struct remote
*remote
;
214 struct string_list
new, stale
, tracked
;
217 static int handle_one_branch(const char *refname
,
218 const unsigned char *sha1
, int flags
, void *cb_data
)
220 struct ref_states
*states
= cb_data
;
221 struct refspec refspec
;
223 memset(&refspec
, 0, sizeof(refspec
));
224 refspec
.dst
= (char *)refname
;
225 if (!remote_find_tracking(states
->remote
, &refspec
)) {
226 struct string_list_item
*item
;
227 const char *name
= abbrev_branch(refspec
.src
);
228 /* symbolic refs pointing nowhere were handled already */
229 if ((flags
& REF_ISSYMREF
) ||
230 unsorted_string_list_has_string(&states
->tracked
,
232 unsorted_string_list_has_string(&states
->new,
235 item
= string_list_append(name
, &states
->stale
);
236 item
->util
= xstrdup(refname
);
241 static int get_ref_states(const struct ref
*ref
, struct ref_states
*states
)
243 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
246 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
247 if (get_fetch_map(ref
, states
->remote
->fetch
+ i
, &tail
, 1))
248 die("Could not get fetch map for refspec %s",
249 states
->remote
->fetch_refspec
[i
]);
251 states
->new.strdup_strings
= states
->tracked
.strdup_strings
= 1;
252 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
253 struct string_list
*target
= &states
->tracked
;
254 unsigned char sha1
[20];
257 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
258 target
= &states
->new;
260 target
= &states
->tracked
;
261 if (hashcmp(sha1
, ref
->new_sha1
))
264 string_list_append(abbrev_branch(ref
->name
), target
)->util
= util
;
266 free_refs(fetch_map
);
268 for_each_ref(handle_one_branch
, states
);
269 sort_string_list(&states
->stale
);
274 struct known_remote
{
275 struct known_remote
*next
;
276 struct remote
*remote
;
279 struct known_remotes
{
280 struct remote
*to_delete
;
281 struct known_remote
*list
;
284 static int add_known_remote(struct remote
*remote
, void *cb_data
)
286 struct known_remotes
*all
= cb_data
;
287 struct known_remote
*r
;
289 if (!strcmp(all
->to_delete
->name
, remote
->name
))
292 r
= xmalloc(sizeof(*r
));
299 struct branches_for_remote
{
300 struct remote
*remote
;
301 struct string_list
*branches
, *skipped
;
302 struct known_remotes
*keep
;
305 static int add_branch_for_removal(const char *refname
,
306 const unsigned char *sha1
, int flags
, void *cb_data
)
308 struct branches_for_remote
*branches
= cb_data
;
309 struct refspec refspec
;
310 struct string_list_item
*item
;
311 struct known_remote
*kr
;
313 memset(&refspec
, 0, sizeof(refspec
));
314 refspec
.dst
= (char *)refname
;
315 if (remote_find_tracking(branches
->remote
, &refspec
))
318 /* don't delete a branch if another remote also uses it */
319 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
320 memset(&refspec
, 0, sizeof(refspec
));
321 refspec
.dst
= (char *)refname
;
322 if (!remote_find_tracking(kr
->remote
, &refspec
))
326 /* don't delete non-remote refs */
327 if (prefixcmp(refname
, "refs/remotes")) {
328 /* advise user how to delete local branches */
329 if (!prefixcmp(refname
, "refs/heads/"))
330 string_list_append(abbrev_branch(refname
),
332 /* silently skip over other non-remote refs */
336 /* make sure that symrefs are deleted */
337 if (flags
& REF_ISSYMREF
)
338 return unlink(git_path("%s", refname
));
340 item
= string_list_append(refname
, branches
->branches
);
341 item
->util
= xmalloc(20);
342 hashcpy(item
->util
, sha1
);
350 struct string_list
*remote_branches
;
353 static int read_remote_branches(const char *refname
,
354 const unsigned char *sha1
, int flags
, void *cb_data
)
356 struct rename_info
*rename
= cb_data
;
357 struct strbuf buf
= STRBUF_INIT
;
358 struct string_list_item
*item
;
360 unsigned char orig_sha1
[20];
363 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
364 if(!prefixcmp(refname
, buf
.buf
)) {
365 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
366 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
367 if (flag
& REF_ISSYMREF
)
368 item
->util
= xstrdup(symref
);
376 static int migrate_file(struct remote
*remote
)
378 struct strbuf buf
= STRBUF_INIT
;
382 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
383 for (i
= 0; i
< remote
->url_nr
; i
++)
384 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
385 return error("Could not append '%s' to '%s'",
386 remote
->url
[i
], buf
.buf
);
388 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
389 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
390 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
391 return error("Could not append '%s' to '%s'",
392 remote
->push_refspec
[i
], buf
.buf
);
394 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
395 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
396 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
397 return error("Could not append '%s' to '%s'",
398 remote
->fetch_refspec
[i
], buf
.buf
);
399 if (remote
->origin
== REMOTE_REMOTES
)
400 path
= git_path("remotes/%s", remote
->name
);
401 else if (remote
->origin
== REMOTE_BRANCHES
)
402 path
= git_path("branches/%s", remote
->name
);
403 if (path
&& unlink(path
))
404 warning("failed to remove '%s'", path
);
408 static int mv(int argc
, const char **argv
)
410 struct option options
[] = {
413 struct remote
*oldremote
, *newremote
;
414 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
415 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
416 struct rename_info rename
;
420 usage_with_options(builtin_remote_usage
, options
);
422 rename
.old
= argv
[1];
423 rename
.new = argv
[2];
424 rename
.remote_branches
= &remote_branches
;
426 oldremote
= remote_get(rename
.old
);
428 die("No such remote: %s", rename
.old
);
430 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
431 return migrate_file(oldremote
);
433 newremote
= remote_get(rename
.new);
434 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
435 die("remote %s already exists.", rename
.new);
437 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
438 if (!valid_fetch_refspec(buf
.buf
))
439 die("'%s' is not a valid remote name", rename
.new);
442 strbuf_addf(&buf
, "remote.%s", rename
.old
);
443 strbuf_addf(&buf2
, "remote.%s", rename
.new);
444 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
445 return error("Could not rename config section '%s' to '%s'",
449 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
450 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
451 return error("Could not remove config section '%s'", buf
.buf
);
452 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
456 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
457 ptr
= strstr(buf2
.buf
, rename
.old
);
459 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
460 rename
.new, strlen(rename
.new));
461 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
462 return error("Could not append '%s'", buf
.buf
);
466 for (i
= 0; i
< branch_list
.nr
; i
++) {
467 struct string_list_item
*item
= branch_list
.items
+ i
;
468 struct branch_info
*info
= item
->util
;
469 if (info
->remote
&& !strcmp(info
->remote
, rename
.old
)) {
471 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
472 if (git_config_set(buf
.buf
, rename
.new)) {
473 return error("Could not set '%s'", buf
.buf
);
479 * First remove symrefs, then rename the rest, finally create
482 for_each_ref(read_remote_branches
, &rename
);
483 for (i
= 0; i
< remote_branches
.nr
; i
++) {
484 struct string_list_item
*item
= remote_branches
.items
+ i
;
486 unsigned char sha1
[20];
488 resolve_ref(item
->string
, sha1
, 1, &flag
);
489 if (!(flag
& REF_ISSYMREF
))
491 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
492 die("deleting '%s' failed", item
->string
);
494 for (i
= 0; i
< remote_branches
.nr
; i
++) {
495 struct string_list_item
*item
= remote_branches
.items
+ i
;
500 strbuf_addstr(&buf
, item
->string
);
501 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
502 rename
.new, strlen(rename
.new));
504 strbuf_addf(&buf2
, "remote: renamed %s to %s",
505 item
->string
, buf
.buf
);
506 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
507 die("renaming '%s' failed", item
->string
);
509 for (i
= 0; i
< remote_branches
.nr
; i
++) {
510 struct string_list_item
*item
= remote_branches
.items
+ i
;
515 strbuf_addstr(&buf
, item
->string
);
516 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
517 rename
.new, strlen(rename
.new));
519 strbuf_addstr(&buf2
, item
->util
);
520 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
521 rename
.new, strlen(rename
.new));
523 strbuf_addf(&buf3
, "remote: renamed %s to %s",
524 item
->string
, buf
.buf
);
525 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
526 die("creating '%s' failed", buf
.buf
);
531 static int remove_branches(struct string_list
*branches
)
534 for (i
= 0; i
< branches
->nr
; i
++) {
535 struct string_list_item
*item
= branches
->items
+ i
;
536 const char *refname
= item
->string
;
537 unsigned char *sha1
= item
->util
;
539 if (delete_ref(refname
, sha1
, 0))
540 result
|= error("Could not remove branch %s", refname
);
545 static int rm(int argc
, const char **argv
)
547 struct option options
[] = {
550 struct remote
*remote
;
551 struct strbuf buf
= STRBUF_INIT
;
552 struct known_remotes known_remotes
= { NULL
, NULL
};
553 struct string_list branches
= { NULL
, 0, 0, 1 };
554 struct string_list skipped
= { NULL
, 0, 0, 1 };
555 struct branches_for_remote cb_data
= {
556 NULL
, &branches
, &skipped
, &known_remotes
561 usage_with_options(builtin_remote_usage
, options
);
563 remote
= remote_get(argv
[1]);
565 die("No such remote: %s", argv
[1]);
567 known_remotes
.to_delete
= remote
;
568 for_each_remote(add_known_remote
, &known_remotes
);
570 strbuf_addf(&buf
, "remote.%s", remote
->name
);
571 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
572 return error("Could not remove config section '%s'", buf
.buf
);
575 for (i
= 0; i
< branch_list
.nr
; i
++) {
576 struct string_list_item
*item
= branch_list
.items
+ i
;
577 struct branch_info
*info
= item
->util
;
578 if (info
->remote
&& !strcmp(info
->remote
, remote
->name
)) {
579 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
580 for (k
= keys
; *k
; k
++) {
582 strbuf_addf(&buf
, "branch.%s.%s",
584 if (git_config_set(buf
.buf
, NULL
)) {
585 strbuf_release(&buf
);
593 * We cannot just pass a function to for_each_ref() which deletes
594 * the branches one by one, since for_each_ref() relies on cached
595 * refs, which are invalidated when deleting a branch.
597 cb_data
.remote
= remote
;
598 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
599 strbuf_release(&buf
);
602 result
= remove_branches(&branches
);
603 string_list_clear(&branches
, 1);
606 fprintf(stderr
, skipped
.nr
== 1 ?
607 "Note: A non-remote branch was not removed; "
608 "to delete it, use:\n" :
609 "Note: Non-remote branches were not removed; "
610 "to delete them, use:\n");
611 for (i
= 0; i
< skipped
.nr
; i
++)
612 fprintf(stderr
, " git branch -d %s\n",
613 skipped
.items
[i
].string
);
615 string_list_clear(&skipped
, 0);
620 static void show_list(const char *title
, struct string_list
*list
,
621 const char *extra_arg
)
628 printf(title
, list
->nr
> 1 ? "es" : "", extra_arg
);
630 for (i
= 0; i
< list
->nr
; i
++)
631 printf(" %s\n", list
->items
[i
].string
);
634 static int get_remote_ref_states(const char *name
,
635 struct ref_states
*states
,
638 struct transport
*transport
;
639 const struct ref
*ref
;
641 states
->remote
= remote_get(name
);
643 return error("No such remote: %s", name
);
648 transport
= transport_get(NULL
, states
->remote
->url_nr
> 0 ?
649 states
->remote
->url
[0] : NULL
);
650 ref
= transport_get_remote_refs(transport
);
651 transport_disconnect(transport
);
653 get_ref_states(ref
, states
);
659 static int append_ref_to_tracked_list(const char *refname
,
660 const unsigned char *sha1
, int flags
, void *cb_data
)
662 struct ref_states
*states
= cb_data
;
663 struct refspec refspec
;
665 memset(&refspec
, 0, sizeof(refspec
));
666 refspec
.dst
= (char *)refname
;
667 if (!remote_find_tracking(states
->remote
, &refspec
))
668 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
673 static int show(int argc
, const char **argv
)
675 int no_query
= 0, result
= 0;
676 struct option options
[] = {
677 OPT_GROUP("show specific options"),
678 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
681 struct ref_states states
;
683 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
688 memset(&states
, 0, sizeof(states
));
689 for (; argc
; argc
--, argv
++) {
692 get_remote_ref_states(*argv
, &states
, !no_query
);
694 printf("* remote %s\n URL: %s\n", *argv
,
695 states
.remote
->url_nr
> 0 ?
696 states
.remote
->url
[0] : "(no URL)");
698 for (i
= 0; i
< branch_list
.nr
; i
++) {
699 struct string_list_item
*branch
= branch_list
.items
+ i
;
700 struct branch_info
*info
= branch
->util
;
703 if (!info
->merge
.nr
|| strcmp(*argv
, info
->remote
))
705 printf(" Remote branch%s merged with 'git pull' "
706 "while on branch %s\n ",
707 info
->merge
.nr
> 1 ? "es" : "",
709 for (j
= 0; j
< info
->merge
.nr
; j
++)
710 printf(" %s", info
->merge
.items
[j
].string
);
715 show_list(" New remote branch%s (next fetch "
716 "will store in remotes/%s)",
717 &states
.new, states
.remote
->name
);
718 show_list(" Stale tracking branch%s (use 'git remote "
719 "prune')", &states
.stale
, "");
723 for_each_ref(append_ref_to_tracked_list
, &states
);
724 show_list(" Tracked remote branch%s", &states
.tracked
, "");
726 if (states
.remote
->push_refspec_nr
) {
727 printf(" Local branch%s pushed with 'git push'\n",
728 states
.remote
->push_refspec_nr
> 1 ?
730 for (i
= 0; i
< states
.remote
->push_refspec_nr
; i
++) {
731 struct refspec
*spec
= states
.remote
->push
+ i
;
732 printf(" %s%s%s%s\n",
733 spec
->force
? "+" : "",
734 abbrev_branch(spec
->src
),
735 spec
->dst
? ":" : "",
736 spec
->dst
? abbrev_branch(spec
->dst
) : "");
740 /* NEEDSWORK: free remote */
741 string_list_clear(&states
.new, 0);
742 string_list_clear(&states
.stale
, 0);
743 string_list_clear(&states
.tracked
, 0);
749 static int prune(int argc
, const char **argv
)
751 int dry_run
= 0, result
= 0;
752 struct option options
[] = {
753 OPT_GROUP("prune specific options"),
754 OPT__DRY_RUN(&dry_run
),
757 struct ref_states states
;
758 const char *dangling_msg
;
760 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
763 usage_with_options(builtin_remote_usage
, options
);
765 dangling_msg
= (dry_run
766 ? " %s will become dangling!\n"
767 : " %s has become dangling!\n");
769 memset(&states
, 0, sizeof(states
));
770 for (; argc
; argc
--, argv
++) {
773 get_remote_ref_states(*argv
, &states
, 1);
775 if (states
.stale
.nr
) {
776 printf("Pruning %s\n", *argv
);
778 states
.remote
->url_nr
779 ? states
.remote
->url
[0]
783 for (i
= 0; i
< states
.stale
.nr
; i
++) {
784 const char *refname
= states
.stale
.items
[i
].util
;
787 result
|= delete_ref(refname
, NULL
, 0);
789 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
790 abbrev_ref(refname
, "refs/remotes/"));
791 warn_dangling_symref(dangling_msg
, refname
);
794 /* NEEDSWORK: free remote */
795 string_list_clear(&states
.new, 0);
796 string_list_clear(&states
.stale
, 0);
797 string_list_clear(&states
.tracked
, 0);
803 static int get_one_remote_for_update(struct remote
*remote
, void *priv
)
805 struct string_list
*list
= priv
;
806 if (!remote
->skip_default_update
)
807 string_list_append(remote
->name
, list
);
811 struct remote_group
{
813 struct string_list
*list
;
816 static int get_remote_group(const char *key
, const char *value
, void *cb
)
818 if (!prefixcmp(key
, "remotes.") &&
819 !strcmp(key
+ 8, remote_group
.name
)) {
820 /* split list by white space */
821 int space
= strcspn(value
, " \t\n");
824 string_list_append(xstrndup(value
, space
),
826 value
+= space
+ (value
[space
] != '\0');
827 space
= strcspn(value
, " \t\n");
834 static int update(int argc
, const char **argv
)
837 struct string_list list
= { NULL
, 0, 0, 0 };
838 static const char *default_argv
[] = { NULL
, "default", NULL
};
845 remote_group
.list
= &list
;
846 for (i
= 1; i
< argc
; i
++) {
847 remote_group
.name
= argv
[i
];
848 result
= git_config(get_remote_group
, NULL
);
851 if (!result
&& !list
.nr
&& argc
== 2 && !strcmp(argv
[1], "default"))
852 result
= for_each_remote(get_one_remote_for_update
, &list
);
854 for (i
= 0; i
< list
.nr
; i
++)
855 result
|= fetch_remote(list
.items
[i
].string
);
857 /* all names were strdup()ed or strndup()ed */
858 list
.strdup_strings
= 1;
859 string_list_clear(&list
, 0);
864 static int get_one_entry(struct remote
*remote
, void *priv
)
866 struct string_list
*list
= priv
;
868 if (remote
->url_nr
> 0) {
871 for (i
= 0; i
< remote
->url_nr
; i
++)
872 string_list_append(remote
->name
, list
)->util
= (void *)remote
->url
[i
];
874 string_list_append(remote
->name
, list
)->util
= NULL
;
879 static int show_all(void)
881 struct string_list list
= { NULL
, 0, 0 };
882 int result
= for_each_remote(get_one_entry
, &list
);
887 sort_string_list(&list
);
888 for (i
= 0; i
< list
.nr
; i
++) {
889 struct string_list_item
*item
= list
.items
+ i
;
891 printf("%s\t%s\n", item
->string
,
892 item
->util
? (const char *)item
->util
: "");
894 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
896 printf("%s\n", item
->string
);
903 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
905 struct option options
[] = {
906 OPT__VERBOSE(&verbose
),
911 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
912 PARSE_OPT_STOP_AT_NON_OPTION
);
916 else if (!strcmp(argv
[0], "add"))
917 result
= add(argc
, argv
);
918 else if (!strcmp(argv
[0], "rename"))
919 result
= mv(argc
, argv
);
920 else if (!strcmp(argv
[0], "rm"))
921 result
= rm(argc
, argv
);
922 else if (!strcmp(argv
[0], "show"))
923 result
= show(argc
, argv
);
924 else if (!strcmp(argv
[0], "prune"))
925 result
= prune(argc
, argv
);
926 else if (!strcmp(argv
[0], "update"))
927 result
= update(argc
, argv
);
929 error("Unknown subcommand: %s", argv
[0]);
930 usage_with_options(builtin_remote_usage
, options
);
933 return result
? 1 : 0;