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 set-head <name> [-a | -d | <branch>]",
16 "git remote show [-n] <name>",
17 "git remote prune [-n | --dry-run] <name>",
18 "git remote [-v | --verbose] update [group]",
22 #define GET_REF_STATES (1<<0)
23 #define GET_HEAD_NAMES (1<<1)
27 static int show_all(void);
29 static inline int postfixcmp(const char *string
, const char *postfix
)
31 int len1
= strlen(string
), len2
= strlen(postfix
);
34 return strcmp(string
+ len1
- len2
, postfix
);
37 static int opt_parse_track(const struct option
*opt
, const char *arg
, int not)
39 struct string_list
*list
= opt
->value
;
41 string_list_clear(list
, 0);
43 string_list_append(arg
, list
);
47 static int fetch_remote(const char *name
)
49 const char *argv
[] = { "fetch", name
, NULL
, NULL
};
54 printf("Updating %s\n", name
);
55 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
56 return error("Could not fetch %s", name
);
60 static int add(int argc
, const char **argv
)
62 int fetch
= 0, mirror
= 0;
63 struct string_list track
= { NULL
, 0, 0 };
64 const char *master
= NULL
;
65 struct remote
*remote
;
66 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
67 const char *name
, *url
;
70 struct option options
[] = {
71 OPT_GROUP("add specific options"),
72 OPT_BOOLEAN('f', "fetch", &fetch
, "fetch the remote branches"),
73 OPT_CALLBACK('t', "track", &track
, "branch",
74 "branch(es) to track", opt_parse_track
),
75 OPT_STRING('m', "master", &master
, "branch", "master branch"),
76 OPT_BOOLEAN(0, "mirror", &mirror
, "no separate remotes"),
80 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
83 usage_with_options(builtin_remote_usage
, options
);
88 remote
= remote_get(name
);
89 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
90 remote
->fetch_refspec_nr
))
91 die("remote %s already exists.", name
);
93 strbuf_addf(&buf2
, "refs/heads/test:refs/remotes/%s/test", name
);
94 if (!valid_fetch_refspec(buf2
.buf
))
95 die("'%s' is not a valid remote name", name
);
97 strbuf_addf(&buf
, "remote.%s.url", name
);
98 if (git_config_set(buf
.buf
, url
))
102 strbuf_addf(&buf
, "remote.%s.fetch", name
);
105 string_list_append("*", &track
);
106 for (i
= 0; i
< track
.nr
; i
++) {
107 struct string_list_item
*item
= track
.items
+ i
;
110 strbuf_addch(&buf2
, '+');
112 strbuf_addf(&buf2
, "refs/%s:refs/%s",
113 item
->string
, item
->string
);
115 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
116 item
->string
, name
, item
->string
);
117 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
123 strbuf_addf(&buf
, "remote.%s.mirror", name
);
124 if (git_config_set(buf
.buf
, "true"))
128 if (fetch
&& fetch_remote(name
))
133 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
136 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
138 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
139 return error("Could not setup master '%s'", master
);
142 strbuf_release(&buf
);
143 strbuf_release(&buf2
);
144 string_list_clear(&track
, 0);
151 struct string_list merge
;
154 static struct string_list branch_list
;
156 static const char *abbrev_ref(const char *name
, const char *prefix
)
158 const char *abbrev
= skip_prefix(name
, prefix
);
163 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
165 static int config_read_branches(const char *key
, const char *value
, void *cb
)
167 if (!prefixcmp(key
, "branch.")) {
169 struct string_list_item
*item
;
170 struct branch_info
*info
;
171 enum { REMOTE
, MERGE
} type
;
174 if (!postfixcmp(key
, ".remote")) {
175 name
= xstrndup(key
, strlen(key
) - 7);
177 } else if (!postfixcmp(key
, ".merge")) {
178 name
= xstrndup(key
, strlen(key
) - 6);
183 item
= string_list_insert(name
, &branch_list
);
186 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
188 if (type
== REMOTE
) {
189 if (info
->remote_name
)
190 warning("more than one branch.%s", key
);
191 info
->remote_name
= xstrdup(value
);
193 char *space
= strchr(value
, ' ');
194 value
= abbrev_branch(value
);
197 merge
= xstrndup(value
, space
- value
);
198 string_list_append(merge
, &info
->merge
);
199 value
= abbrev_branch(space
+ 1);
200 space
= strchr(value
, ' ');
202 string_list_append(xstrdup(value
), &info
->merge
);
208 static void read_branches(void)
212 git_config(config_read_branches
, NULL
);
216 struct remote
*remote
;
217 struct string_list
new, stale
, tracked
, heads
;
220 static int handle_one_branch(const char *refname
,
221 const unsigned char *sha1
, int flags
, void *cb_data
)
223 struct ref_states
*states
= cb_data
;
224 struct refspec refspec
;
226 memset(&refspec
, 0, sizeof(refspec
));
227 refspec
.dst
= (char *)refname
;
228 if (!remote_find_tracking(states
->remote
, &refspec
)) {
229 struct string_list_item
*item
;
230 const char *name
= abbrev_branch(refspec
.src
);
231 /* symbolic refs pointing nowhere were handled already */
232 if ((flags
& REF_ISSYMREF
) ||
233 string_list_has_string(&states
->tracked
, name
) ||
234 string_list_has_string(&states
->new, name
))
236 item
= string_list_append(name
, &states
->stale
);
237 item
->util
= xstrdup(refname
);
242 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
244 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
248 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
249 if (get_fetch_map(remote_refs
, states
->remote
->fetch
+ i
, &tail
, 1))
250 die("Could not get fetch map for refspec %s",
251 states
->remote
->fetch_refspec
[i
]);
253 states
->new.strdup_strings
= states
->tracked
.strdup_strings
= 1;
254 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
255 unsigned char sha1
[20];
256 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
257 string_list_append(abbrev_branch(ref
->name
), &states
->new);
259 string_list_append(abbrev_branch(ref
->name
), &states
->tracked
);
261 free_refs(fetch_map
);
263 sort_string_list(&states
->new);
264 sort_string_list(&states
->tracked
);
265 for_each_ref(handle_one_branch
, states
);
266 sort_string_list(&states
->stale
);
271 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
273 struct ref
*ref
, *matches
;
274 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
275 struct refspec refspec
;
279 refspec
.src
= refspec
.dst
= "refs/heads/";
280 states
->heads
.strdup_strings
= 1;
281 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
282 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
284 for(ref
= matches
; ref
; ref
= ref
->next
)
285 string_list_append(abbrev_branch(ref
->name
), &states
->heads
);
287 free_refs(fetch_map
);
293 struct known_remote
{
294 struct known_remote
*next
;
295 struct remote
*remote
;
298 struct known_remotes
{
299 struct remote
*to_delete
;
300 struct known_remote
*list
;
303 static int add_known_remote(struct remote
*remote
, void *cb_data
)
305 struct known_remotes
*all
= cb_data
;
306 struct known_remote
*r
;
308 if (!strcmp(all
->to_delete
->name
, remote
->name
))
311 r
= xmalloc(sizeof(*r
));
318 struct branches_for_remote
{
319 struct remote
*remote
;
320 struct string_list
*branches
, *skipped
;
321 struct known_remotes
*keep
;
324 static int add_branch_for_removal(const char *refname
,
325 const unsigned char *sha1
, int flags
, void *cb_data
)
327 struct branches_for_remote
*branches
= cb_data
;
328 struct refspec refspec
;
329 struct string_list_item
*item
;
330 struct known_remote
*kr
;
332 memset(&refspec
, 0, sizeof(refspec
));
333 refspec
.dst
= (char *)refname
;
334 if (remote_find_tracking(branches
->remote
, &refspec
))
337 /* don't delete a branch if another remote also uses it */
338 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
339 memset(&refspec
, 0, sizeof(refspec
));
340 refspec
.dst
= (char *)refname
;
341 if (!remote_find_tracking(kr
->remote
, &refspec
))
345 /* don't delete non-remote refs */
346 if (prefixcmp(refname
, "refs/remotes")) {
347 /* advise user how to delete local branches */
348 if (!prefixcmp(refname
, "refs/heads/"))
349 string_list_append(abbrev_branch(refname
),
351 /* silently skip over other non-remote refs */
355 /* make sure that symrefs are deleted */
356 if (flags
& REF_ISSYMREF
)
357 return unlink(git_path("%s", refname
));
359 item
= string_list_append(refname
, branches
->branches
);
360 item
->util
= xmalloc(20);
361 hashcpy(item
->util
, sha1
);
369 struct string_list
*remote_branches
;
372 static int read_remote_branches(const char *refname
,
373 const unsigned char *sha1
, int flags
, void *cb_data
)
375 struct rename_info
*rename
= cb_data
;
376 struct strbuf buf
= STRBUF_INIT
;
377 struct string_list_item
*item
;
379 unsigned char orig_sha1
[20];
382 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
383 if(!prefixcmp(refname
, buf
.buf
)) {
384 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
385 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
386 if (flag
& REF_ISSYMREF
)
387 item
->util
= xstrdup(symref
);
395 static int migrate_file(struct remote
*remote
)
397 struct strbuf buf
= STRBUF_INIT
;
401 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
402 for (i
= 0; i
< remote
->url_nr
; i
++)
403 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
404 return error("Could not append '%s' to '%s'",
405 remote
->url
[i
], buf
.buf
);
407 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
408 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
409 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
410 return error("Could not append '%s' to '%s'",
411 remote
->push_refspec
[i
], buf
.buf
);
413 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
414 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
415 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
416 return error("Could not append '%s' to '%s'",
417 remote
->fetch_refspec
[i
], buf
.buf
);
418 if (remote
->origin
== REMOTE_REMOTES
)
419 path
= git_path("remotes/%s", remote
->name
);
420 else if (remote
->origin
== REMOTE_BRANCHES
)
421 path
= git_path("branches/%s", remote
->name
);
422 if (path
&& unlink(path
))
423 warning("failed to remove '%s'", path
);
427 static int mv(int argc
, const char **argv
)
429 struct option options
[] = {
432 struct remote
*oldremote
, *newremote
;
433 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
434 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
435 struct rename_info rename
;
439 usage_with_options(builtin_remote_usage
, options
);
441 rename
.old
= argv
[1];
442 rename
.new = argv
[2];
443 rename
.remote_branches
= &remote_branches
;
445 oldremote
= remote_get(rename
.old
);
447 die("No such remote: %s", rename
.old
);
449 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
450 return migrate_file(oldremote
);
452 newremote
= remote_get(rename
.new);
453 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
454 die("remote %s already exists.", rename
.new);
456 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
457 if (!valid_fetch_refspec(buf
.buf
))
458 die("'%s' is not a valid remote name", rename
.new);
461 strbuf_addf(&buf
, "remote.%s", rename
.old
);
462 strbuf_addf(&buf2
, "remote.%s", rename
.new);
463 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
464 return error("Could not rename config section '%s' to '%s'",
468 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
469 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
470 return error("Could not remove config section '%s'", buf
.buf
);
471 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
475 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
476 ptr
= strstr(buf2
.buf
, rename
.old
);
478 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
479 rename
.new, strlen(rename
.new));
480 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
481 return error("Could not append '%s'", buf
.buf
);
485 for (i
= 0; i
< branch_list
.nr
; i
++) {
486 struct string_list_item
*item
= branch_list
.items
+ i
;
487 struct branch_info
*info
= item
->util
;
488 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old
)) {
490 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
491 if (git_config_set(buf
.buf
, rename
.new)) {
492 return error("Could not set '%s'", buf
.buf
);
498 * First remove symrefs, then rename the rest, finally create
501 for_each_ref(read_remote_branches
, &rename
);
502 for (i
= 0; i
< remote_branches
.nr
; i
++) {
503 struct string_list_item
*item
= remote_branches
.items
+ i
;
505 unsigned char sha1
[20];
508 symref
= resolve_ref(item
->string
, sha1
, 1, &flag
);
509 if (!(flag
& REF_ISSYMREF
))
511 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
512 die("deleting '%s' failed", item
->string
);
514 for (i
= 0; i
< remote_branches
.nr
; i
++) {
515 struct string_list_item
*item
= remote_branches
.items
+ i
;
520 strbuf_addstr(&buf
, item
->string
);
521 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
522 rename
.new, strlen(rename
.new));
524 strbuf_addf(&buf2
, "remote: renamed %s to %s",
525 item
->string
, buf
.buf
);
526 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
527 die("renaming '%s' failed", item
->string
);
529 for (i
= 0; i
< remote_branches
.nr
; i
++) {
530 struct string_list_item
*item
= remote_branches
.items
+ i
;
535 strbuf_addstr(&buf
, item
->string
);
536 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
537 rename
.new, strlen(rename
.new));
539 strbuf_addstr(&buf2
, item
->util
);
540 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
541 rename
.new, strlen(rename
.new));
543 strbuf_addf(&buf3
, "remote: renamed %s to %s",
544 item
->string
, buf
.buf
);
545 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
546 die("creating '%s' failed", buf
.buf
);
551 static int remove_branches(struct string_list
*branches
)
554 for (i
= 0; i
< branches
->nr
; i
++) {
555 struct string_list_item
*item
= branches
->items
+ i
;
556 const char *refname
= item
->string
;
557 unsigned char *sha1
= item
->util
;
559 if (delete_ref(refname
, sha1
, 0))
560 result
|= error("Could not remove branch %s", refname
);
565 static int rm(int argc
, const char **argv
)
567 struct option options
[] = {
570 struct remote
*remote
;
571 struct strbuf buf
= STRBUF_INIT
;
572 struct known_remotes known_remotes
= { NULL
, NULL
};
573 struct string_list branches
= { NULL
, 0, 0, 1 };
574 struct string_list skipped
= { NULL
, 0, 0, 1 };
575 struct branches_for_remote cb_data
= {
576 NULL
, &branches
, &skipped
, &known_remotes
581 usage_with_options(builtin_remote_usage
, options
);
583 remote
= remote_get(argv
[1]);
585 die("No such remote: %s", argv
[1]);
587 known_remotes
.to_delete
= remote
;
588 for_each_remote(add_known_remote
, &known_remotes
);
590 strbuf_addf(&buf
, "remote.%s", remote
->name
);
591 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
592 return error("Could not remove config section '%s'", buf
.buf
);
595 for (i
= 0; i
< branch_list
.nr
; i
++) {
596 struct string_list_item
*item
= branch_list
.items
+ i
;
597 struct branch_info
*info
= item
->util
;
598 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
599 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
600 for (k
= keys
; *k
; k
++) {
602 strbuf_addf(&buf
, "branch.%s.%s",
604 if (git_config_set(buf
.buf
, NULL
)) {
605 strbuf_release(&buf
);
613 * We cannot just pass a function to for_each_ref() which deletes
614 * the branches one by one, since for_each_ref() relies on cached
615 * refs, which are invalidated when deleting a branch.
617 cb_data
.remote
= remote
;
618 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
619 strbuf_release(&buf
);
622 result
= remove_branches(&branches
);
623 string_list_clear(&branches
, 1);
626 fprintf(stderr
, skipped
.nr
== 1 ?
627 "Note: A non-remote branch was not removed; "
628 "to delete it, use:\n" :
629 "Note: Non-remote branches were not removed; "
630 "to delete them, use:\n");
631 for (i
= 0; i
< skipped
.nr
; i
++)
632 fprintf(stderr
, " git branch -d %s\n",
633 skipped
.items
[i
].string
);
635 string_list_clear(&skipped
, 0);
640 static void show_list(const char *title
, struct string_list
*list
,
641 const char *extra_arg
)
648 printf(title
, list
->nr
> 1 ? "es" : "", extra_arg
);
650 for (i
= 0; i
< list
->nr
; i
++)
651 printf(" %s\n", list
->items
[i
].string
);
654 static void free_remote_ref_states(struct ref_states
*states
)
656 string_list_clear(&states
->new, 0);
657 string_list_clear(&states
->stale
, 0);
658 string_list_clear(&states
->tracked
, 0);
659 string_list_clear(&states
->heads
, 0);
662 static int append_ref_to_tracked_list(const char *refname
,
663 const unsigned char *sha1
, int flags
, void *cb_data
)
665 struct ref_states
*states
= cb_data
;
666 struct refspec refspec
;
668 if (flags
& REF_ISSYMREF
)
671 memset(&refspec
, 0, sizeof(refspec
));
672 refspec
.dst
= (char *)refname
;
673 if (!remote_find_tracking(states
->remote
, &refspec
))
674 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
679 static int get_remote_ref_states(const char *name
,
680 struct ref_states
*states
,
683 struct transport
*transport
;
684 const struct ref
*remote_refs
;
686 states
->remote
= remote_get(name
);
688 return error("No such remote: %s", name
);
693 transport
= transport_get(NULL
, states
->remote
->url_nr
> 0 ?
694 states
->remote
->url
[0] : NULL
);
695 remote_refs
= transport_get_remote_refs(transport
);
696 transport_disconnect(transport
);
698 if (query
& GET_REF_STATES
)
699 get_ref_states(remote_refs
, states
);
700 if (query
& GET_HEAD_NAMES
)
701 get_head_names(remote_refs
, states
);
703 for_each_ref(append_ref_to_tracked_list
, states
);
704 sort_string_list(&states
->tracked
);
710 static int show(int argc
, const char **argv
)
712 int no_query
= 0, result
= 0, query_flag
= 0;
713 struct option options
[] = {
714 OPT_GROUP("show specific options"),
715 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
718 struct ref_states states
;
720 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
726 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
);
728 memset(&states
, 0, sizeof(states
));
729 for (; argc
; argc
--, argv
++) {
732 get_remote_ref_states(*argv
, &states
, query_flag
);
734 printf("* remote %s\n URL: %s\n", *argv
,
735 states
.remote
->url_nr
> 0 ?
736 states
.remote
->url
[0] : "(no URL)");
738 printf(" HEAD branch: (not queried)\n");
739 else if (!states
.heads
.nr
)
740 printf(" HEAD branch: (unknown)\n");
741 else if (states
.heads
.nr
== 1)
742 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
744 printf(" HEAD branch (remote HEAD is ambiguous,"
745 " may be one of the following):\n");
746 for (i
= 0; i
< states
.heads
.nr
; i
++)
747 printf(" %s\n", states
.heads
.items
[i
].string
);
750 for (i
= 0; i
< branch_list
.nr
; i
++) {
751 struct string_list_item
*branch
= branch_list
.items
+ i
;
752 struct branch_info
*info
= branch
->util
;
755 if (!info
->merge
.nr
|| strcmp(*argv
, info
->remote_name
))
757 printf(" Remote branch%s merged with 'git pull' "
758 "while on branch %s\n ",
759 info
->merge
.nr
> 1 ? "es" : "",
761 for (j
= 0; j
< info
->merge
.nr
; j
++)
762 printf(" %s", info
->merge
.items
[j
].string
);
767 show_list(" New remote branch%s (next fetch "
768 "will store in remotes/%s)",
769 &states
.new, states
.remote
->name
);
770 show_list(" Stale tracking branch%s (use 'git remote "
771 "prune')", &states
.stale
, "");
774 show_list(" Tracked remote branch%s", &states
.tracked
, "");
776 if (states
.remote
->push_refspec_nr
) {
777 printf(" Local branch%s pushed with 'git push'\n",
778 states
.remote
->push_refspec_nr
> 1 ?
780 for (i
= 0; i
< states
.remote
->push_refspec_nr
; i
++) {
781 struct refspec
*spec
= states
.remote
->push
+ i
;
782 printf(" %s%s%s%s\n",
783 spec
->force
? "+" : "",
784 abbrev_branch(spec
->src
),
785 spec
->dst
? ":" : "",
786 spec
->dst
? abbrev_branch(spec
->dst
) : "");
790 free_remote_ref_states(&states
);
796 static int set_head(int argc
, const char **argv
)
798 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
799 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
800 char *head_name
= NULL
;
802 struct option options
[] = {
803 OPT_GROUP("set-head specific options"),
804 OPT_BOOLEAN('a', "auto", &opt_a
,
805 "set refs/remotes/<name>/HEAD according to remote"),
806 OPT_BOOLEAN('d', "delete", &opt_d
,
807 "delete refs/remotes/<name>/HEAD"),
810 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
812 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
814 if (!opt_a
&& !opt_d
&& argc
== 2) {
815 head_name
= xstrdup(argv
[1]);
816 } else if (opt_a
&& !opt_d
&& argc
== 1) {
817 struct ref_states states
;
818 memset(&states
, 0, sizeof(states
));
819 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
820 if (!states
.heads
.nr
)
821 result
|= error("Cannot determine remote HEAD");
822 else if (states
.heads
.nr
> 1) {
823 result
|= error("Multiple remote HEAD branches. "
824 "Please choose one explicitly with:");
825 for (i
= 0; i
< states
.heads
.nr
; i
++)
826 fprintf(stderr
, " git remote set-head %s %s\n",
827 argv
[0], states
.heads
.items
[i
].string
);
829 head_name
= xstrdup(states
.heads
.items
[0].string
);
830 free_remote_ref_states(&states
);
831 } else if (opt_d
&& !opt_a
&& argc
== 1) {
832 if (delete_ref(buf
.buf
, NULL
, REF_NODEREF
))
833 result
|= error("Could not delete %s", buf
.buf
);
835 usage_with_options(builtin_remote_usage
, options
);
838 unsigned char sha1
[20];
839 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
840 /* make sure it's valid */
841 if (!resolve_ref(buf2
.buf
, sha1
, 1, NULL
))
842 result
|= error("Not a valid ref: %s", buf2
.buf
);
843 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
844 result
|= error("Could not setup %s", buf
.buf
);
846 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
850 strbuf_release(&buf
);
851 strbuf_release(&buf2
);
855 static int prune(int argc
, const char **argv
)
857 int dry_run
= 0, result
= 0;
858 struct option options
[] = {
859 OPT_GROUP("prune specific options"),
860 OPT__DRY_RUN(&dry_run
),
863 struct ref_states states
;
864 const char *dangling_msg
;
866 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
869 usage_with_options(builtin_remote_usage
, options
);
871 dangling_msg
= (dry_run
872 ? " %s will become dangling!\n"
873 : " %s has become dangling!\n");
875 memset(&states
, 0, sizeof(states
));
876 for (; argc
; argc
--, argv
++) {
879 get_remote_ref_states(*argv
, &states
, GET_REF_STATES
);
881 if (states
.stale
.nr
) {
882 printf("Pruning %s\n", *argv
);
884 states
.remote
->url_nr
885 ? states
.remote
->url
[0]
889 for (i
= 0; i
< states
.stale
.nr
; i
++) {
890 const char *refname
= states
.stale
.items
[i
].util
;
893 result
|= delete_ref(refname
, NULL
, 0);
895 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
896 abbrev_ref(refname
, "refs/remotes/"));
897 warn_dangling_symref(dangling_msg
, refname
);
900 free_remote_ref_states(&states
);
906 static int get_one_remote_for_update(struct remote
*remote
, void *priv
)
908 struct string_list
*list
= priv
;
909 if (!remote
->skip_default_update
)
910 string_list_append(remote
->name
, list
);
914 struct remote_group
{
916 struct string_list
*list
;
919 static int get_remote_group(const char *key
, const char *value
, void *cb
)
921 if (!prefixcmp(key
, "remotes.") &&
922 !strcmp(key
+ 8, remote_group
.name
)) {
923 /* split list by white space */
924 int space
= strcspn(value
, " \t\n");
927 string_list_append(xstrndup(value
, space
),
929 value
+= space
+ (value
[space
] != '\0');
930 space
= strcspn(value
, " \t\n");
937 static int update(int argc
, const char **argv
)
940 struct string_list list
= { NULL
, 0, 0, 0 };
941 static const char *default_argv
[] = { NULL
, "default", NULL
};
948 remote_group
.list
= &list
;
949 for (i
= 1; i
< argc
; i
++) {
950 remote_group
.name
= argv
[i
];
951 result
= git_config(get_remote_group
, NULL
);
954 if (!result
&& !list
.nr
&& argc
== 2 && !strcmp(argv
[1], "default"))
955 result
= for_each_remote(get_one_remote_for_update
, &list
);
957 for (i
= 0; i
< list
.nr
; i
++)
958 result
|= fetch_remote(list
.items
[i
].string
);
960 /* all names were strdup()ed or strndup()ed */
961 list
.strdup_strings
= 1;
962 string_list_clear(&list
, 0);
967 static int get_one_entry(struct remote
*remote
, void *priv
)
969 struct string_list
*list
= priv
;
971 if (remote
->url_nr
> 0) {
974 for (i
= 0; i
< remote
->url_nr
; i
++)
975 string_list_append(remote
->name
, list
)->util
= (void *)remote
->url
[i
];
977 string_list_append(remote
->name
, list
)->util
= NULL
;
982 static int show_all(void)
984 struct string_list list
= { NULL
, 0, 0 };
985 int result
= for_each_remote(get_one_entry
, &list
);
990 sort_string_list(&list
);
991 for (i
= 0; i
< list
.nr
; i
++) {
992 struct string_list_item
*item
= list
.items
+ i
;
994 printf("%s\t%s\n", item
->string
,
995 item
->util
? (const char *)item
->util
: "");
997 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
999 printf("%s\n", item
->string
);
1006 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1008 struct option options
[] = {
1009 OPT__VERBOSE(&verbose
),
1014 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
1015 PARSE_OPT_STOP_AT_NON_OPTION
);
1018 result
= show_all();
1019 else if (!strcmp(argv
[0], "add"))
1020 result
= add(argc
, argv
);
1021 else if (!strcmp(argv
[0], "rename"))
1022 result
= mv(argc
, argv
);
1023 else if (!strcmp(argv
[0], "rm"))
1024 result
= rm(argc
, argv
);
1025 else if (!strcmp(argv
[0], "set-head"))
1026 result
= set_head(argc
, argv
);
1027 else if (!strcmp(argv
[0], "show"))
1028 result
= show(argc
, argv
);
1029 else if (!strcmp(argv
[0], "prune"))
1030 result
= prune(argc
, argv
);
1031 else if (!strcmp(argv
[0], "update"))
1032 result
= update(argc
, argv
);
1034 error("Unknown subcommand: %s", argv
[0]);
1035 usage_with_options(builtin_remote_usage
, options
);
1038 return result
? 1 : 0;