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 [-p | --prune] [group]",
22 #define GET_REF_STATES (1<<0)
23 #define GET_HEAD_NAMES (1<<1)
24 #define GET_PUSH_REF_STATES (1<<2)
28 static int show_all(void);
29 static int prune_remote(const char *remote
, int dry_run
);
31 static inline int postfixcmp(const char *string
, const char *postfix
)
33 int len1
= strlen(string
), len2
= strlen(postfix
);
36 return strcmp(string
+ len1
- len2
, postfix
);
39 static int opt_parse_track(const struct option
*opt
, const char *arg
, int not)
41 struct string_list
*list
= opt
->value
;
43 string_list_clear(list
, 0);
45 string_list_append(arg
, list
);
49 static int fetch_remote(const char *name
)
51 const char *argv
[] = { "fetch", name
, NULL
, NULL
};
56 printf("Updating %s\n", name
);
57 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
58 return error("Could not fetch %s", name
);
62 static int add(int argc
, const char **argv
)
64 int fetch
= 0, mirror
= 0;
65 struct string_list track
= { NULL
, 0, 0 };
66 const char *master
= NULL
;
67 struct remote
*remote
;
68 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
69 const char *name
, *url
;
72 struct option options
[] = {
73 OPT_GROUP("add specific options"),
74 OPT_BOOLEAN('f', "fetch", &fetch
, "fetch the remote branches"),
75 OPT_CALLBACK('t', "track", &track
, "branch",
76 "branch(es) to track", opt_parse_track
),
77 OPT_STRING('m', "master", &master
, "branch", "master branch"),
78 OPT_BOOLEAN(0, "mirror", &mirror
, "no separate remotes"),
82 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_usage
,
86 usage_with_options(builtin_remote_usage
, options
);
91 remote
= remote_get(name
);
92 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
93 remote
->fetch_refspec_nr
))
94 die("remote %s already exists.", name
);
96 strbuf_addf(&buf2
, "refs/heads/test:refs/remotes/%s/test", name
);
97 if (!valid_fetch_refspec(buf2
.buf
))
98 die("'%s' is not a valid remote name", name
);
100 strbuf_addf(&buf
, "remote.%s.url", name
);
101 if (git_config_set(buf
.buf
, url
))
105 strbuf_addf(&buf
, "remote.%s.fetch", name
);
108 string_list_append("*", &track
);
109 for (i
= 0; i
< track
.nr
; i
++) {
110 struct string_list_item
*item
= track
.items
+ i
;
113 strbuf_addch(&buf2
, '+');
115 strbuf_addf(&buf2
, "refs/%s:refs/%s",
116 item
->string
, item
->string
);
118 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
119 item
->string
, name
, item
->string
);
120 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
126 strbuf_addf(&buf
, "remote.%s.mirror", name
);
127 if (git_config_set(buf
.buf
, "true"))
131 if (fetch
&& fetch_remote(name
))
136 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
139 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
141 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
142 return error("Could not setup master '%s'", master
);
145 strbuf_release(&buf
);
146 strbuf_release(&buf2
);
147 string_list_clear(&track
, 0);
154 struct string_list merge
;
158 static struct string_list branch_list
;
160 static const char *abbrev_ref(const char *name
, const char *prefix
)
162 const char *abbrev
= skip_prefix(name
, prefix
);
167 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
169 static int config_read_branches(const char *key
, const char *value
, void *cb
)
171 if (!prefixcmp(key
, "branch.")) {
172 const char *orig_key
= key
;
174 struct string_list_item
*item
;
175 struct branch_info
*info
;
176 enum { REMOTE
, MERGE
, REBASE
} type
;
179 if (!postfixcmp(key
, ".remote")) {
180 name
= xstrndup(key
, strlen(key
) - 7);
182 } else if (!postfixcmp(key
, ".merge")) {
183 name
= xstrndup(key
, strlen(key
) - 6);
185 } else if (!postfixcmp(key
, ".rebase")) {
186 name
= xstrndup(key
, strlen(key
) - 7);
191 item
= string_list_insert(name
, &branch_list
);
194 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
196 if (type
== REMOTE
) {
197 if (info
->remote_name
)
198 warning("more than one %s", orig_key
);
199 info
->remote_name
= xstrdup(value
);
200 } else if (type
== MERGE
) {
201 char *space
= strchr(value
, ' ');
202 value
= abbrev_branch(value
);
205 merge
= xstrndup(value
, space
- value
);
206 string_list_append(merge
, &info
->merge
);
207 value
= abbrev_branch(space
+ 1);
208 space
= strchr(value
, ' ');
210 string_list_append(xstrdup(value
), &info
->merge
);
212 info
->rebase
= git_config_bool(orig_key
, value
);
217 static void read_branches(void)
221 git_config(config_read_branches
, NULL
);
225 struct remote
*remote
;
226 struct string_list
new, stale
, tracked
, heads
, push
;
230 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
232 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
233 struct ref
*ref
, *stale_refs
;
236 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
237 if (get_fetch_map(remote_refs
, states
->remote
->fetch
+ i
, &tail
, 1))
238 die("Could not get fetch map for refspec %s",
239 states
->remote
->fetch_refspec
[i
]);
241 states
->new.strdup_strings
= states
->tracked
.strdup_strings
= 1;
242 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
243 unsigned char sha1
[20];
244 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
245 string_list_append(abbrev_branch(ref
->name
), &states
->new);
247 string_list_append(abbrev_branch(ref
->name
), &states
->tracked
);
249 stale_refs
= get_stale_heads(states
->remote
, fetch_map
);
250 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
251 struct string_list_item
*item
=
252 string_list_append(abbrev_branch(ref
->name
), &states
->stale
);
253 item
->util
= xstrdup(ref
->name
);
255 free_refs(stale_refs
);
256 free_refs(fetch_map
);
258 sort_string_list(&states
->new);
259 sort_string_list(&states
->tracked
);
260 sort_string_list(&states
->stale
);
269 PUSH_STATUS_CREATE
= 0,
271 PUSH_STATUS_UPTODATE
,
272 PUSH_STATUS_FASTFORWARD
,
273 PUSH_STATUS_OUTOFDATE
,
274 PUSH_STATUS_NOTQUERIED
,
278 static int get_push_ref_states(const struct ref
*remote_refs
,
279 struct ref_states
*states
)
281 struct remote
*remote
= states
->remote
;
282 struct ref
*ref
, *local_refs
, *push_map
;
286 local_refs
= get_local_heads();
287 push_map
= copy_ref_list(remote_refs
);
289 match_refs(local_refs
, &push_map
, remote
->push_refspec_nr
,
290 remote
->push_refspec
, MATCH_REFS_NONE
);
292 states
->push
.strdup_strings
= 1;
293 for (ref
= push_map
; ref
; ref
= ref
->next
) {
294 struct string_list_item
*item
;
295 struct push_info
*info
;
299 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
301 item
= string_list_append(abbrev_branch(ref
->peer_ref
->name
),
303 item
->util
= xcalloc(sizeof(struct push_info
), 1);
305 info
->forced
= ref
->force
;
306 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
308 if (is_null_sha1(ref
->new_sha1
)) {
309 info
->status
= PUSH_STATUS_DELETE
;
310 } else if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
))
311 info
->status
= PUSH_STATUS_UPTODATE
;
312 else if (is_null_sha1(ref
->old_sha1
))
313 info
->status
= PUSH_STATUS_CREATE
;
314 else if (has_sha1_file(ref
->old_sha1
) &&
315 ref_newer(ref
->new_sha1
, ref
->old_sha1
))
316 info
->status
= PUSH_STATUS_FASTFORWARD
;
318 info
->status
= PUSH_STATUS_OUTOFDATE
;
320 free_refs(local_refs
);
325 static int get_push_ref_states_noquery(struct ref_states
*states
)
328 struct remote
*remote
= states
->remote
;
329 struct string_list_item
*item
;
330 struct push_info
*info
;
335 states
->push
.strdup_strings
= 1;
336 if (!remote
->push_refspec_nr
) {
337 item
= string_list_append("(matching)", &states
->push
);
338 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
339 info
->status
= PUSH_STATUS_NOTQUERIED
;
340 info
->dest
= xstrdup(item
->string
);
342 for (i
= 0; i
< remote
->push_refspec_nr
; i
++) {
343 struct refspec
*spec
= remote
->push
+ i
;
345 item
= string_list_append("(matching)", &states
->push
);
346 else if (strlen(spec
->src
))
347 item
= string_list_append(spec
->src
, &states
->push
);
349 item
= string_list_append("(delete)", &states
->push
);
351 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
352 info
->forced
= spec
->force
;
353 info
->status
= PUSH_STATUS_NOTQUERIED
;
354 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
359 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
361 struct ref
*ref
, *matches
;
362 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
363 struct refspec refspec
;
367 refspec
.src
= refspec
.dst
= "refs/heads/*";
368 states
->heads
.strdup_strings
= 1;
369 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
370 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
372 for (ref
= matches
; ref
; ref
= ref
->next
)
373 string_list_append(abbrev_branch(ref
->name
), &states
->heads
);
375 free_refs(fetch_map
);
381 struct known_remote
{
382 struct known_remote
*next
;
383 struct remote
*remote
;
386 struct known_remotes
{
387 struct remote
*to_delete
;
388 struct known_remote
*list
;
391 static int add_known_remote(struct remote
*remote
, void *cb_data
)
393 struct known_remotes
*all
= cb_data
;
394 struct known_remote
*r
;
396 if (!strcmp(all
->to_delete
->name
, remote
->name
))
399 r
= xmalloc(sizeof(*r
));
406 struct branches_for_remote
{
407 struct remote
*remote
;
408 struct string_list
*branches
, *skipped
;
409 struct known_remotes
*keep
;
412 static int add_branch_for_removal(const char *refname
,
413 const unsigned char *sha1
, int flags
, void *cb_data
)
415 struct branches_for_remote
*branches
= cb_data
;
416 struct refspec refspec
;
417 struct string_list_item
*item
;
418 struct known_remote
*kr
;
420 memset(&refspec
, 0, sizeof(refspec
));
421 refspec
.dst
= (char *)refname
;
422 if (remote_find_tracking(branches
->remote
, &refspec
))
425 /* don't delete a branch if another remote also uses it */
426 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
427 memset(&refspec
, 0, sizeof(refspec
));
428 refspec
.dst
= (char *)refname
;
429 if (!remote_find_tracking(kr
->remote
, &refspec
))
433 /* don't delete non-remote refs */
434 if (prefixcmp(refname
, "refs/remotes")) {
435 /* advise user how to delete local branches */
436 if (!prefixcmp(refname
, "refs/heads/"))
437 string_list_append(abbrev_branch(refname
),
439 /* silently skip over other non-remote refs */
443 /* make sure that symrefs are deleted */
444 if (flags
& REF_ISSYMREF
)
445 return unlink(git_path("%s", refname
));
447 item
= string_list_append(refname
, branches
->branches
);
448 item
->util
= xmalloc(20);
449 hashcpy(item
->util
, sha1
);
457 struct string_list
*remote_branches
;
460 static int read_remote_branches(const char *refname
,
461 const unsigned char *sha1
, int flags
, void *cb_data
)
463 struct rename_info
*rename
= cb_data
;
464 struct strbuf buf
= STRBUF_INIT
;
465 struct string_list_item
*item
;
467 unsigned char orig_sha1
[20];
470 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
471 if (!prefixcmp(refname
, buf
.buf
)) {
472 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
473 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
474 if (flag
& REF_ISSYMREF
)
475 item
->util
= xstrdup(symref
);
483 static int migrate_file(struct remote
*remote
)
485 struct strbuf buf
= STRBUF_INIT
;
489 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
490 for (i
= 0; i
< remote
->url_nr
; i
++)
491 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
492 return error("Could not append '%s' to '%s'",
493 remote
->url
[i
], buf
.buf
);
495 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
496 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
497 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
498 return error("Could not append '%s' to '%s'",
499 remote
->push_refspec
[i
], buf
.buf
);
501 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
502 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
503 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
504 return error("Could not append '%s' to '%s'",
505 remote
->fetch_refspec
[i
], buf
.buf
);
506 if (remote
->origin
== REMOTE_REMOTES
)
507 path
= git_path("remotes/%s", remote
->name
);
508 else if (remote
->origin
== REMOTE_BRANCHES
)
509 path
= git_path("branches/%s", remote
->name
);
511 unlink_or_warn(path
);
515 static int mv(int argc
, const char **argv
)
517 struct option options
[] = {
520 struct remote
*oldremote
, *newremote
;
521 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
522 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
523 struct rename_info rename
;
527 usage_with_options(builtin_remote_usage
, options
);
529 rename
.old
= argv
[1];
530 rename
.new = argv
[2];
531 rename
.remote_branches
= &remote_branches
;
533 oldremote
= remote_get(rename
.old
);
535 die("No such remote: %s", rename
.old
);
537 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
538 return migrate_file(oldremote
);
540 newremote
= remote_get(rename
.new);
541 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
542 die("remote %s already exists.", rename
.new);
544 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
545 if (!valid_fetch_refspec(buf
.buf
))
546 die("'%s' is not a valid remote name", rename
.new);
549 strbuf_addf(&buf
, "remote.%s", rename
.old
);
550 strbuf_addf(&buf2
, "remote.%s", rename
.new);
551 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
552 return error("Could not rename config section '%s' to '%s'",
556 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
557 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
558 return error("Could not remove config section '%s'", buf
.buf
);
559 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
563 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
564 ptr
= strstr(buf2
.buf
, rename
.old
);
566 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
567 rename
.new, strlen(rename
.new));
568 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
569 return error("Could not append '%s'", buf
.buf
);
573 for (i
= 0; i
< branch_list
.nr
; i
++) {
574 struct string_list_item
*item
= branch_list
.items
+ i
;
575 struct branch_info
*info
= item
->util
;
576 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old
)) {
578 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
579 if (git_config_set(buf
.buf
, rename
.new)) {
580 return error("Could not set '%s'", buf
.buf
);
586 * First remove symrefs, then rename the rest, finally create
589 for_each_ref(read_remote_branches
, &rename
);
590 for (i
= 0; i
< remote_branches
.nr
; i
++) {
591 struct string_list_item
*item
= remote_branches
.items
+ i
;
593 unsigned char sha1
[20];
595 resolve_ref(item
->string
, sha1
, 1, &flag
);
596 if (!(flag
& REF_ISSYMREF
))
598 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
599 die("deleting '%s' failed", item
->string
);
601 for (i
= 0; i
< remote_branches
.nr
; i
++) {
602 struct string_list_item
*item
= remote_branches
.items
+ i
;
607 strbuf_addstr(&buf
, item
->string
);
608 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
609 rename
.new, strlen(rename
.new));
611 strbuf_addf(&buf2
, "remote: renamed %s to %s",
612 item
->string
, buf
.buf
);
613 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
614 die("renaming '%s' failed", item
->string
);
616 for (i
= 0; i
< remote_branches
.nr
; i
++) {
617 struct string_list_item
*item
= remote_branches
.items
+ i
;
622 strbuf_addstr(&buf
, item
->string
);
623 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
624 rename
.new, strlen(rename
.new));
626 strbuf_addstr(&buf2
, item
->util
);
627 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
628 rename
.new, strlen(rename
.new));
630 strbuf_addf(&buf3
, "remote: renamed %s to %s",
631 item
->string
, buf
.buf
);
632 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
633 die("creating '%s' failed", buf
.buf
);
638 static int remove_branches(struct string_list
*branches
)
641 for (i
= 0; i
< branches
->nr
; i
++) {
642 struct string_list_item
*item
= branches
->items
+ i
;
643 const char *refname
= item
->string
;
644 unsigned char *sha1
= item
->util
;
646 if (delete_ref(refname
, sha1
, 0))
647 result
|= error("Could not remove branch %s", refname
);
652 static int rm(int argc
, const char **argv
)
654 struct option options
[] = {
657 struct remote
*remote
;
658 struct strbuf buf
= STRBUF_INIT
;
659 struct known_remotes known_remotes
= { NULL
, NULL
};
660 struct string_list branches
= { NULL
, 0, 0, 1 };
661 struct string_list skipped
= { NULL
, 0, 0, 1 };
662 struct branches_for_remote cb_data
= {
663 NULL
, &branches
, &skipped
, &known_remotes
668 usage_with_options(builtin_remote_usage
, options
);
670 remote
= remote_get(argv
[1]);
672 die("No such remote: %s", argv
[1]);
674 known_remotes
.to_delete
= remote
;
675 for_each_remote(add_known_remote
, &known_remotes
);
677 strbuf_addf(&buf
, "remote.%s", remote
->name
);
678 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
679 return error("Could not remove config section '%s'", buf
.buf
);
682 for (i
= 0; i
< branch_list
.nr
; i
++) {
683 struct string_list_item
*item
= branch_list
.items
+ i
;
684 struct branch_info
*info
= item
->util
;
685 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
686 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
687 for (k
= keys
; *k
; k
++) {
689 strbuf_addf(&buf
, "branch.%s.%s",
691 if (git_config_set(buf
.buf
, NULL
)) {
692 strbuf_release(&buf
);
700 * We cannot just pass a function to for_each_ref() which deletes
701 * the branches one by one, since for_each_ref() relies on cached
702 * refs, which are invalidated when deleting a branch.
704 cb_data
.remote
= remote
;
705 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
706 strbuf_release(&buf
);
709 result
= remove_branches(&branches
);
710 string_list_clear(&branches
, 1);
713 fprintf(stderr
, skipped
.nr
== 1 ?
714 "Note: A non-remote branch was not removed; "
715 "to delete it, use:\n" :
716 "Note: Non-remote branches were not removed; "
717 "to delete them, use:\n");
718 for (i
= 0; i
< skipped
.nr
; i
++)
719 fprintf(stderr
, " git branch -d %s\n",
720 skipped
.items
[i
].string
);
722 string_list_clear(&skipped
, 0);
727 static void clear_push_info(void *util
, const char *string
)
729 struct push_info
*info
= util
;
734 static void free_remote_ref_states(struct ref_states
*states
)
736 string_list_clear(&states
->new, 0);
737 string_list_clear(&states
->stale
, 0);
738 string_list_clear(&states
->tracked
, 0);
739 string_list_clear(&states
->heads
, 0);
740 string_list_clear_func(&states
->push
, clear_push_info
);
743 static int append_ref_to_tracked_list(const char *refname
,
744 const unsigned char *sha1
, int flags
, void *cb_data
)
746 struct ref_states
*states
= cb_data
;
747 struct refspec refspec
;
749 if (flags
& REF_ISSYMREF
)
752 memset(&refspec
, 0, sizeof(refspec
));
753 refspec
.dst
= (char *)refname
;
754 if (!remote_find_tracking(states
->remote
, &refspec
))
755 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
760 static int get_remote_ref_states(const char *name
,
761 struct ref_states
*states
,
764 struct transport
*transport
;
765 const struct ref
*remote_refs
;
767 states
->remote
= remote_get(name
);
769 return error("No such remote: %s", name
);
774 transport
= transport_get(states
->remote
, states
->remote
->url_nr
> 0 ?
775 states
->remote
->url
[0] : NULL
);
776 remote_refs
= transport_get_remote_refs(transport
);
777 transport_disconnect(transport
);
780 if (query
& GET_REF_STATES
)
781 get_ref_states(remote_refs
, states
);
782 if (query
& GET_HEAD_NAMES
)
783 get_head_names(remote_refs
, states
);
784 if (query
& GET_PUSH_REF_STATES
)
785 get_push_ref_states(remote_refs
, states
);
787 for_each_ref(append_ref_to_tracked_list
, states
);
788 sort_string_list(&states
->tracked
);
789 get_push_ref_states_noquery(states
);
796 struct string_list
*list
;
797 struct ref_states
*states
;
802 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
804 struct show_info
*info
= cb_data
;
805 int n
= strlen(item
->string
);
808 string_list_insert(item
->string
, info
->list
);
812 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
814 struct show_info
*info
= cb_data
;
815 struct ref_states
*states
= info
->states
;
816 const char *name
= item
->string
;
818 if (states
->queried
) {
819 const char *fmt
= "%s";
820 const char *arg
= "";
821 if (string_list_has_string(&states
->new, name
)) {
822 fmt
= " new (next fetch will store in remotes/%s)";
823 arg
= states
->remote
->name
;
824 } else if (string_list_has_string(&states
->tracked
, name
))
826 else if (string_list_has_string(&states
->stale
, name
))
827 arg
= " stale (use 'git remote prune' to remove)";
830 printf(" %-*s", info
->width
, name
);
834 printf(" %s\n", name
);
839 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
841 struct show_info
*show_info
= cb_data
;
842 struct ref_states
*states
= show_info
->states
;
843 struct branch_info
*branch_info
= branch_item
->util
;
844 struct string_list_item
*item
;
847 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
848 strcmp(states
->remote
->name
, branch_info
->remote_name
))
850 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
851 show_info
->width
= n
;
852 if (branch_info
->rebase
)
853 show_info
->any_rebase
= 1;
855 item
= string_list_insert(branch_item
->string
, show_info
->list
);
856 item
->util
= branch_info
;
861 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
863 struct show_info
*show_info
= cb_data
;
864 struct branch_info
*branch_info
= item
->util
;
865 struct string_list
*merge
= &branch_info
->merge
;
869 if (branch_info
->rebase
&& branch_info
->merge
.nr
> 1) {
870 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
875 printf(" %-*s ", show_info
->width
, item
->string
);
876 if (branch_info
->rebase
) {
877 printf("rebases onto remote %s\n", merge
->items
[0].string
);
879 } else if (show_info
->any_rebase
) {
880 printf(" merges with remote %s\n", merge
->items
[0].string
);
881 also
= " and with remote";
883 printf("merges with remote %s\n", merge
->items
[0].string
);
884 also
= " and with remote";
886 for (i
= 1; i
< merge
->nr
; i
++)
887 printf(" %-*s %s %s\n", show_info
->width
, "", also
,
888 merge
->items
[i
].string
);
893 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
895 struct show_info
*show_info
= cb_data
;
896 struct push_info
*push_info
= push_item
->util
;
897 struct string_list_item
*item
;
899 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
900 show_info
->width
= n
;
901 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
902 show_info
->width2
= n
;
903 item
= string_list_append(push_item
->string
, show_info
->list
);
904 item
->util
= push_item
->util
;
909 * Sorting comparison for a string list that has push_info
910 * structs in its util field
912 static int cmp_string_with_push(const void *va
, const void *vb
)
914 const struct string_list_item
*a
= va
;
915 const struct string_list_item
*b
= vb
;
916 const struct push_info
*a_push
= a
->util
;
917 const struct push_info
*b_push
= b
->util
;
918 int cmp
= strcmp(a
->string
, b
->string
);
919 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
922 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
924 struct show_info
*show_info
= cb_data
;
925 struct push_info
*push_info
= item
->util
;
926 char *src
= item
->string
, *status
= NULL
;
928 switch (push_info
->status
) {
929 case PUSH_STATUS_CREATE
:
932 case PUSH_STATUS_DELETE
:
936 case PUSH_STATUS_UPTODATE
:
937 status
= "up to date";
939 case PUSH_STATUS_FASTFORWARD
:
940 status
= "fast forwardable";
942 case PUSH_STATUS_OUTOFDATE
:
943 status
= "local out of date";
945 case PUSH_STATUS_NOTQUERIED
:
949 printf(" %-*s %s to %-*s (%s)\n", show_info
->width
, src
,
950 push_info
->forced
? "forces" : "pushes",
951 show_info
->width2
, push_info
->dest
, status
);
953 printf(" %-*s %s to %s\n", show_info
->width
, src
,
954 push_info
->forced
? "forces" : "pushes",
959 static int show(int argc
, const char **argv
)
961 int no_query
= 0, result
= 0, query_flag
= 0;
962 struct option options
[] = {
963 OPT_GROUP("show specific options"),
964 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
967 struct ref_states states
;
968 struct string_list info_list
= { NULL
, 0, 0, 0 };
969 struct show_info info
;
971 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_usage
,
978 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
980 memset(&states
, 0, sizeof(states
));
981 memset(&info
, 0, sizeof(info
));
982 info
.states
= &states
;
983 info
.list
= &info_list
;
984 for (; argc
; argc
--, argv
++) {
989 get_remote_ref_states(*argv
, &states
, query_flag
);
991 printf("* remote %s\n", *argv
);
992 printf(" Fetch URL: %s\n", states
.remote
->url_nr
> 0 ?
993 states
.remote
->url
[0] : "(no URL)");
994 if (states
.remote
->pushurl_nr
) {
995 url
= states
.remote
->pushurl
;
996 url_nr
= states
.remote
->pushurl_nr
;
998 url
= states
.remote
->url
;
999 url_nr
= states
.remote
->url_nr
;
1001 for (i
=0; i
< url_nr
; i
++)
1002 printf(" Push URL: %s\n", url
[i
]);
1004 printf(" Push URL: %s\n", "(no URL)");
1006 printf(" HEAD branch: (not queried)\n");
1007 else if (!states
.heads
.nr
)
1008 printf(" HEAD branch: (unknown)\n");
1009 else if (states
.heads
.nr
== 1)
1010 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
1012 printf(" HEAD branch (remote HEAD is ambiguous,"
1013 " may be one of the following):\n");
1014 for (i
= 0; i
< states
.heads
.nr
; i
++)
1015 printf(" %s\n", states
.heads
.items
[i
].string
);
1018 /* remote branch info */
1020 for_each_string_list(add_remote_to_show_info
, &states
.new, &info
);
1021 for_each_string_list(add_remote_to_show_info
, &states
.tracked
, &info
);
1022 for_each_string_list(add_remote_to_show_info
, &states
.stale
, &info
);
1024 printf(" Remote branch%s:%s\n",
1025 info
.list
->nr
> 1 ? "es" : "",
1026 no_query
? " (status not queried)" : "");
1027 for_each_string_list(show_remote_info_item
, info
.list
, &info
);
1028 string_list_clear(info
.list
, 0);
1032 info
.any_rebase
= 0;
1033 for_each_string_list(add_local_to_show_info
, &branch_list
, &info
);
1035 printf(" Local branch%s configured for 'git pull':\n",
1036 info
.list
->nr
> 1 ? "es" : "");
1037 for_each_string_list(show_local_info_item
, info
.list
, &info
);
1038 string_list_clear(info
.list
, 0);
1041 if (states
.remote
->mirror
)
1042 printf(" Local refs will be mirrored by 'git push'\n");
1044 info
.width
= info
.width2
= 0;
1045 for_each_string_list(add_push_to_show_info
, &states
.push
, &info
);
1046 qsort(info
.list
->items
, info
.list
->nr
,
1047 sizeof(*info
.list
->items
), cmp_string_with_push
);
1049 printf(" Local ref%s configured for 'git push'%s:\n",
1050 info
.list
->nr
> 1 ? "s" : "",
1051 no_query
? " (status not queried)" : "");
1052 for_each_string_list(show_push_info_item
, info
.list
, &info
);
1053 string_list_clear(info
.list
, 0);
1055 free_remote_ref_states(&states
);
1061 static int set_head(int argc
, const char **argv
)
1063 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1064 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1065 char *head_name
= NULL
;
1067 struct option options
[] = {
1068 OPT_GROUP("set-head specific options"),
1069 OPT_BOOLEAN('a', "auto", &opt_a
,
1070 "set refs/remotes/<name>/HEAD according to remote"),
1071 OPT_BOOLEAN('d', "delete", &opt_d
,
1072 "delete refs/remotes/<name>/HEAD"),
1075 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_usage
,
1078 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1080 if (!opt_a
&& !opt_d
&& argc
== 2) {
1081 head_name
= xstrdup(argv
[1]);
1082 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1083 struct ref_states states
;
1084 memset(&states
, 0, sizeof(states
));
1085 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1086 if (!states
.heads
.nr
)
1087 result
|= error("Cannot determine remote HEAD");
1088 else if (states
.heads
.nr
> 1) {
1089 result
|= error("Multiple remote HEAD branches. "
1090 "Please choose one explicitly with:");
1091 for (i
= 0; i
< states
.heads
.nr
; i
++)
1092 fprintf(stderr
, " git remote set-head %s %s\n",
1093 argv
[0], states
.heads
.items
[i
].string
);
1095 head_name
= xstrdup(states
.heads
.items
[0].string
);
1096 free_remote_ref_states(&states
);
1097 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1098 if (delete_ref(buf
.buf
, NULL
, REF_NODEREF
))
1099 result
|= error("Could not delete %s", buf
.buf
);
1101 usage_with_options(builtin_remote_usage
, options
);
1104 unsigned char sha1
[20];
1105 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1106 /* make sure it's valid */
1107 if (!resolve_ref(buf2
.buf
, sha1
, 1, NULL
))
1108 result
|= error("Not a valid ref: %s", buf2
.buf
);
1109 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1110 result
|= error("Could not setup %s", buf
.buf
);
1112 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1116 strbuf_release(&buf
);
1117 strbuf_release(&buf2
);
1121 static int prune(int argc
, const char **argv
)
1123 int dry_run
= 0, result
= 0;
1124 struct option options
[] = {
1125 OPT_GROUP("prune specific options"),
1126 OPT__DRY_RUN(&dry_run
),
1130 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_usage
,
1134 usage_with_options(builtin_remote_usage
, options
);
1136 for (; argc
; argc
--, argv
++)
1137 result
|= prune_remote(*argv
, dry_run
);
1142 static int prune_remote(const char *remote
, int dry_run
)
1145 struct ref_states states
;
1146 const char *dangling_msg
= dry_run
1147 ? " %s will become dangling!\n"
1148 : " %s has become dangling!\n";
1150 memset(&states
, 0, sizeof(states
));
1151 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1153 if (states
.stale
.nr
) {
1154 printf("Pruning %s\n", remote
);
1156 states
.remote
->url_nr
1157 ? states
.remote
->url
[0]
1161 for (i
= 0; i
< states
.stale
.nr
; i
++) {
1162 const char *refname
= states
.stale
.items
[i
].util
;
1165 result
|= delete_ref(refname
, NULL
, 0);
1167 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
1168 abbrev_ref(refname
, "refs/remotes/"));
1169 warn_dangling_symref(stdout
, dangling_msg
, refname
);
1172 free_remote_ref_states(&states
);
1176 static int get_one_remote_for_update(struct remote
*remote
, void *priv
)
1178 struct string_list
*list
= priv
;
1179 if (!remote
->skip_default_update
)
1180 string_list_append(remote
->name
, list
);
1184 static struct remote_group
{
1186 struct string_list
*list
;
1189 static int get_remote_group(const char *key
, const char *value
, void *num_hits
)
1191 if (!prefixcmp(key
, "remotes.") &&
1192 !strcmp(key
+ 8, remote_group
.name
)) {
1193 /* split list by white space */
1194 int space
= strcspn(value
, " \t\n");
1197 string_list_append(xstrndup(value
, space
),
1199 ++*((int *)num_hits
);
1201 value
+= space
+ (value
[space
] != '\0');
1202 space
= strcspn(value
, " \t\n");
1209 static int update(int argc
, const char **argv
)
1211 int i
, result
= 0, prune
= 0;
1212 struct string_list list
= { NULL
, 0, 0, 0 };
1213 static const char *default_argv
[] = { NULL
, "default", NULL
};
1214 struct option options
[] = {
1215 OPT_GROUP("update specific options"),
1216 OPT_BOOLEAN('p', "prune", &prune
,
1217 "prune remotes after fetching"),
1221 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_usage
,
1222 PARSE_OPT_KEEP_ARGV0
);
1225 argv
= default_argv
;
1228 remote_group
.list
= &list
;
1229 for (i
= 1; i
< argc
; i
++) {
1230 int groups_found
= 0;
1231 remote_group
.name
= argv
[i
];
1232 result
= git_config(get_remote_group
, &groups_found
);
1233 if (!groups_found
&& (i
!= 1 || strcmp(argv
[1], "default"))) {
1234 struct remote
*remote
;
1235 if (!remote_is_configured(argv
[i
]))
1236 die("No such remote or remote group: %s",
1238 remote
= remote_get(argv
[i
]);
1239 string_list_append(remote
->name
, remote_group
.list
);
1243 if (!result
&& !list
.nr
&& argc
== 2 && !strcmp(argv
[1], "default"))
1244 result
= for_each_remote(get_one_remote_for_update
, &list
);
1246 for (i
= 0; i
< list
.nr
; i
++) {
1247 int err
= fetch_remote(list
.items
[i
].string
);
1250 result
|= prune_remote(list
.items
[i
].string
, 0);
1253 /* all names were strdup()ed or strndup()ed */
1254 list
.strdup_strings
= 1;
1255 string_list_clear(&list
, 0);
1260 static int get_one_entry(struct remote
*remote
, void *priv
)
1262 struct string_list
*list
= priv
;
1263 struct strbuf url_buf
= STRBUF_INIT
;
1267 if (remote
->url_nr
> 0) {
1268 strbuf_addf(&url_buf
, "%s (fetch)", remote
->url
[0]);
1269 string_list_append(remote
->name
, list
)->util
=
1270 strbuf_detach(&url_buf
, NULL
);
1272 string_list_append(remote
->name
, list
)->util
= NULL
;
1273 if (remote
->pushurl_nr
) {
1274 url
= remote
->pushurl
;
1275 url_nr
= remote
->pushurl_nr
;
1278 url_nr
= remote
->url_nr
;
1280 for (i
= 0; i
< url_nr
; i
++)
1282 strbuf_addf(&url_buf
, "%s (push)", url
[i
]);
1283 string_list_append(remote
->name
, list
)->util
=
1284 strbuf_detach(&url_buf
, NULL
);
1290 static int show_all(void)
1292 struct string_list list
= { NULL
, 0, 0 };
1295 list
.strdup_strings
= 1;
1296 result
= for_each_remote(get_one_entry
, &list
);
1301 sort_string_list(&list
);
1302 for (i
= 0; i
< list
.nr
; i
++) {
1303 struct string_list_item
*item
= list
.items
+ i
;
1305 printf("%s\t%s\n", item
->string
,
1306 item
->util
? (const char *)item
->util
: "");
1308 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1310 printf("%s\n", item
->string
);
1314 string_list_clear(&list
, 1);
1318 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1320 struct option options
[] = {
1321 OPT__VERBOSE(&verbose
),
1326 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1327 PARSE_OPT_STOP_AT_NON_OPTION
);
1330 result
= show_all();
1331 else if (!strcmp(argv
[0], "add"))
1332 result
= add(argc
, argv
);
1333 else if (!strcmp(argv
[0], "rename"))
1334 result
= mv(argc
, argv
);
1335 else if (!strcmp(argv
[0], "rm"))
1336 result
= rm(argc
, argv
);
1337 else if (!strcmp(argv
[0], "set-head"))
1338 result
= set_head(argc
, argv
);
1339 else if (!strcmp(argv
[0], "show"))
1340 result
= show(argc
, argv
);
1341 else if (!strcmp(argv
[0], "prune"))
1342 result
= prune(argc
, argv
);
1343 else if (!strcmp(argv
[0], "update"))
1344 result
= update(argc
, argv
);
1346 error("Unknown subcommand: %s", argv
[0]);
1347 usage_with_options(builtin_remote_usage
, options
);
1350 return result
? 1 : 0;