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 handle_one_branch(const char *refname
,
231 const unsigned char *sha1
, int flags
, void *cb_data
)
233 struct ref_states
*states
= cb_data
;
234 struct refspec refspec
;
236 memset(&refspec
, 0, sizeof(refspec
));
237 refspec
.dst
= (char *)refname
;
238 if (!remote_find_tracking(states
->remote
, &refspec
)) {
239 struct string_list_item
*item
;
240 const char *name
= abbrev_branch(refspec
.src
);
241 /* symbolic refs pointing nowhere were handled already */
242 if ((flags
& REF_ISSYMREF
) ||
243 string_list_has_string(&states
->tracked
, name
) ||
244 string_list_has_string(&states
->new, name
))
246 item
= string_list_append(name
, &states
->stale
);
247 item
->util
= xstrdup(refname
);
252 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
254 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
258 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
259 if (get_fetch_map(remote_refs
, states
->remote
->fetch
+ i
, &tail
, 1))
260 die("Could not get fetch map for refspec %s",
261 states
->remote
->fetch_refspec
[i
]);
263 states
->new.strdup_strings
= states
->tracked
.strdup_strings
= 1;
264 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
265 unsigned char sha1
[20];
266 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
267 string_list_append(abbrev_branch(ref
->name
), &states
->new);
269 string_list_append(abbrev_branch(ref
->name
), &states
->tracked
);
271 free_refs(fetch_map
);
273 sort_string_list(&states
->new);
274 sort_string_list(&states
->tracked
);
275 for_each_ref(handle_one_branch
, states
);
276 sort_string_list(&states
->stale
);
285 PUSH_STATUS_CREATE
= 0,
287 PUSH_STATUS_UPTODATE
,
288 PUSH_STATUS_FASTFORWARD
,
289 PUSH_STATUS_OUTOFDATE
,
290 PUSH_STATUS_NOTQUERIED
,
294 static int get_push_ref_states(const struct ref
*remote_refs
,
295 struct ref_states
*states
)
297 struct remote
*remote
= states
->remote
;
298 struct ref
*ref
, *local_refs
, *push_map
;
302 local_refs
= get_local_heads();
303 push_map
= copy_ref_list(remote_refs
);
305 match_refs(local_refs
, &push_map
, remote
->push_refspec_nr
,
306 remote
->push_refspec
, MATCH_REFS_NONE
);
308 states
->push
.strdup_strings
= 1;
309 for (ref
= push_map
; ref
; ref
= ref
->next
) {
310 struct string_list_item
*item
;
311 struct push_info
*info
;
315 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
317 item
= string_list_append(abbrev_branch(ref
->peer_ref
->name
),
319 item
->util
= xcalloc(sizeof(struct push_info
), 1);
321 info
->forced
= ref
->force
;
322 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
324 if (is_null_sha1(ref
->new_sha1
)) {
325 info
->status
= PUSH_STATUS_DELETE
;
326 } else if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
))
327 info
->status
= PUSH_STATUS_UPTODATE
;
328 else if (is_null_sha1(ref
->old_sha1
))
329 info
->status
= PUSH_STATUS_CREATE
;
330 else if (has_sha1_file(ref
->old_sha1
) &&
331 ref_newer(ref
->new_sha1
, ref
->old_sha1
))
332 info
->status
= PUSH_STATUS_FASTFORWARD
;
334 info
->status
= PUSH_STATUS_OUTOFDATE
;
336 free_refs(local_refs
);
341 static int get_push_ref_states_noquery(struct ref_states
*states
)
344 struct remote
*remote
= states
->remote
;
345 struct string_list_item
*item
;
346 struct push_info
*info
;
351 states
->push
.strdup_strings
= 1;
352 if (!remote
->push_refspec_nr
) {
353 item
= string_list_append("(matching)", &states
->push
);
354 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
355 info
->status
= PUSH_STATUS_NOTQUERIED
;
356 info
->dest
= xstrdup(item
->string
);
358 for (i
= 0; i
< remote
->push_refspec_nr
; i
++) {
359 struct refspec
*spec
= remote
->push
+ i
;
361 item
= string_list_append("(matching)", &states
->push
);
362 else if (strlen(spec
->src
))
363 item
= string_list_append(spec
->src
, &states
->push
);
365 item
= string_list_append("(delete)", &states
->push
);
367 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
368 info
->forced
= spec
->force
;
369 info
->status
= PUSH_STATUS_NOTQUERIED
;
370 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
375 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
377 struct ref
*ref
, *matches
;
378 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
379 struct refspec refspec
;
383 refspec
.src
= refspec
.dst
= "refs/heads/*";
384 states
->heads
.strdup_strings
= 1;
385 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
386 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
388 for(ref
= matches
; ref
; ref
= ref
->next
)
389 string_list_append(abbrev_branch(ref
->name
), &states
->heads
);
391 free_refs(fetch_map
);
397 struct known_remote
{
398 struct known_remote
*next
;
399 struct remote
*remote
;
402 struct known_remotes
{
403 struct remote
*to_delete
;
404 struct known_remote
*list
;
407 static int add_known_remote(struct remote
*remote
, void *cb_data
)
409 struct known_remotes
*all
= cb_data
;
410 struct known_remote
*r
;
412 if (!strcmp(all
->to_delete
->name
, remote
->name
))
415 r
= xmalloc(sizeof(*r
));
422 struct branches_for_remote
{
423 struct remote
*remote
;
424 struct string_list
*branches
, *skipped
;
425 struct known_remotes
*keep
;
428 static int add_branch_for_removal(const char *refname
,
429 const unsigned char *sha1
, int flags
, void *cb_data
)
431 struct branches_for_remote
*branches
= cb_data
;
432 struct refspec refspec
;
433 struct string_list_item
*item
;
434 struct known_remote
*kr
;
436 memset(&refspec
, 0, sizeof(refspec
));
437 refspec
.dst
= (char *)refname
;
438 if (remote_find_tracking(branches
->remote
, &refspec
))
441 /* don't delete a branch if another remote also uses it */
442 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
443 memset(&refspec
, 0, sizeof(refspec
));
444 refspec
.dst
= (char *)refname
;
445 if (!remote_find_tracking(kr
->remote
, &refspec
))
449 /* don't delete non-remote refs */
450 if (prefixcmp(refname
, "refs/remotes")) {
451 /* advise user how to delete local branches */
452 if (!prefixcmp(refname
, "refs/heads/"))
453 string_list_append(abbrev_branch(refname
),
455 /* silently skip over other non-remote refs */
459 /* make sure that symrefs are deleted */
460 if (flags
& REF_ISSYMREF
)
461 return unlink(git_path("%s", refname
));
463 item
= string_list_append(refname
, branches
->branches
);
464 item
->util
= xmalloc(20);
465 hashcpy(item
->util
, sha1
);
473 struct string_list
*remote_branches
;
476 static int read_remote_branches(const char *refname
,
477 const unsigned char *sha1
, int flags
, void *cb_data
)
479 struct rename_info
*rename
= cb_data
;
480 struct strbuf buf
= STRBUF_INIT
;
481 struct string_list_item
*item
;
483 unsigned char orig_sha1
[20];
486 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
487 if(!prefixcmp(refname
, buf
.buf
)) {
488 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
489 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
490 if (flag
& REF_ISSYMREF
)
491 item
->util
= xstrdup(symref
);
499 static int migrate_file(struct remote
*remote
)
501 struct strbuf buf
= STRBUF_INIT
;
505 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
506 for (i
= 0; i
< remote
->url_nr
; i
++)
507 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
508 return error("Could not append '%s' to '%s'",
509 remote
->url
[i
], buf
.buf
);
511 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
512 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
513 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
514 return error("Could not append '%s' to '%s'",
515 remote
->push_refspec
[i
], buf
.buf
);
517 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
518 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
519 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
520 return error("Could not append '%s' to '%s'",
521 remote
->fetch_refspec
[i
], buf
.buf
);
522 if (remote
->origin
== REMOTE_REMOTES
)
523 path
= git_path("remotes/%s", remote
->name
);
524 else if (remote
->origin
== REMOTE_BRANCHES
)
525 path
= git_path("branches/%s", remote
->name
);
527 unlink_or_warn(path
);
531 static int mv(int argc
, const char **argv
)
533 struct option options
[] = {
536 struct remote
*oldremote
, *newremote
;
537 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
538 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
539 struct rename_info rename
;
543 usage_with_options(builtin_remote_usage
, options
);
545 rename
.old
= argv
[1];
546 rename
.new = argv
[2];
547 rename
.remote_branches
= &remote_branches
;
549 oldremote
= remote_get(rename
.old
);
551 die("No such remote: %s", rename
.old
);
553 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
554 return migrate_file(oldremote
);
556 newremote
= remote_get(rename
.new);
557 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
558 die("remote %s already exists.", rename
.new);
560 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
561 if (!valid_fetch_refspec(buf
.buf
))
562 die("'%s' is not a valid remote name", rename
.new);
565 strbuf_addf(&buf
, "remote.%s", rename
.old
);
566 strbuf_addf(&buf2
, "remote.%s", rename
.new);
567 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
568 return error("Could not rename config section '%s' to '%s'",
572 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
573 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
574 return error("Could not remove config section '%s'", buf
.buf
);
575 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
579 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
580 ptr
= strstr(buf2
.buf
, rename
.old
);
582 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
583 rename
.new, strlen(rename
.new));
584 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
585 return error("Could not append '%s'", buf
.buf
);
589 for (i
= 0; i
< branch_list
.nr
; i
++) {
590 struct string_list_item
*item
= branch_list
.items
+ i
;
591 struct branch_info
*info
= item
->util
;
592 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old
)) {
594 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
595 if (git_config_set(buf
.buf
, rename
.new)) {
596 return error("Could not set '%s'", buf
.buf
);
602 * First remove symrefs, then rename the rest, finally create
605 for_each_ref(read_remote_branches
, &rename
);
606 for (i
= 0; i
< remote_branches
.nr
; i
++) {
607 struct string_list_item
*item
= remote_branches
.items
+ i
;
609 unsigned char sha1
[20];
611 resolve_ref(item
->string
, sha1
, 1, &flag
);
612 if (!(flag
& REF_ISSYMREF
))
614 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
615 die("deleting '%s' failed", item
->string
);
617 for (i
= 0; i
< remote_branches
.nr
; i
++) {
618 struct string_list_item
*item
= remote_branches
.items
+ i
;
623 strbuf_addstr(&buf
, item
->string
);
624 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
625 rename
.new, strlen(rename
.new));
627 strbuf_addf(&buf2
, "remote: renamed %s to %s",
628 item
->string
, buf
.buf
);
629 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
630 die("renaming '%s' failed", item
->string
);
632 for (i
= 0; i
< remote_branches
.nr
; i
++) {
633 struct string_list_item
*item
= remote_branches
.items
+ i
;
638 strbuf_addstr(&buf
, item
->string
);
639 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
640 rename
.new, strlen(rename
.new));
642 strbuf_addstr(&buf2
, item
->util
);
643 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
644 rename
.new, strlen(rename
.new));
646 strbuf_addf(&buf3
, "remote: renamed %s to %s",
647 item
->string
, buf
.buf
);
648 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
649 die("creating '%s' failed", buf
.buf
);
654 static int remove_branches(struct string_list
*branches
)
657 for (i
= 0; i
< branches
->nr
; i
++) {
658 struct string_list_item
*item
= branches
->items
+ i
;
659 const char *refname
= item
->string
;
660 unsigned char *sha1
= item
->util
;
662 if (delete_ref(refname
, sha1
, 0))
663 result
|= error("Could not remove branch %s", refname
);
668 static int rm(int argc
, const char **argv
)
670 struct option options
[] = {
673 struct remote
*remote
;
674 struct strbuf buf
= STRBUF_INIT
;
675 struct known_remotes known_remotes
= { NULL
, NULL
};
676 struct string_list branches
= { NULL
, 0, 0, 1 };
677 struct string_list skipped
= { NULL
, 0, 0, 1 };
678 struct branches_for_remote cb_data
= {
679 NULL
, &branches
, &skipped
, &known_remotes
684 usage_with_options(builtin_remote_usage
, options
);
686 remote
= remote_get(argv
[1]);
688 die("No such remote: %s", argv
[1]);
690 known_remotes
.to_delete
= remote
;
691 for_each_remote(add_known_remote
, &known_remotes
);
693 strbuf_addf(&buf
, "remote.%s", remote
->name
);
694 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
695 return error("Could not remove config section '%s'", buf
.buf
);
698 for (i
= 0; i
< branch_list
.nr
; i
++) {
699 struct string_list_item
*item
= branch_list
.items
+ i
;
700 struct branch_info
*info
= item
->util
;
701 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
702 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
703 for (k
= keys
; *k
; k
++) {
705 strbuf_addf(&buf
, "branch.%s.%s",
707 if (git_config_set(buf
.buf
, NULL
)) {
708 strbuf_release(&buf
);
716 * We cannot just pass a function to for_each_ref() which deletes
717 * the branches one by one, since for_each_ref() relies on cached
718 * refs, which are invalidated when deleting a branch.
720 cb_data
.remote
= remote
;
721 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
722 strbuf_release(&buf
);
725 result
= remove_branches(&branches
);
726 string_list_clear(&branches
, 1);
729 fprintf(stderr
, skipped
.nr
== 1 ?
730 "Note: A non-remote branch was not removed; "
731 "to delete it, use:\n" :
732 "Note: Non-remote branches were not removed; "
733 "to delete them, use:\n");
734 for (i
= 0; i
< skipped
.nr
; i
++)
735 fprintf(stderr
, " git branch -d %s\n",
736 skipped
.items
[i
].string
);
738 string_list_clear(&skipped
, 0);
743 void clear_push_info(void *util
, const char *string
)
745 struct push_info
*info
= util
;
750 static void free_remote_ref_states(struct ref_states
*states
)
752 string_list_clear(&states
->new, 0);
753 string_list_clear(&states
->stale
, 0);
754 string_list_clear(&states
->tracked
, 0);
755 string_list_clear(&states
->heads
, 0);
756 string_list_clear_func(&states
->push
, clear_push_info
);
759 static int append_ref_to_tracked_list(const char *refname
,
760 const unsigned char *sha1
, int flags
, void *cb_data
)
762 struct ref_states
*states
= cb_data
;
763 struct refspec refspec
;
765 if (flags
& REF_ISSYMREF
)
768 memset(&refspec
, 0, sizeof(refspec
));
769 refspec
.dst
= (char *)refname
;
770 if (!remote_find_tracking(states
->remote
, &refspec
))
771 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
776 static int get_remote_ref_states(const char *name
,
777 struct ref_states
*states
,
780 struct transport
*transport
;
781 const struct ref
*remote_refs
;
783 states
->remote
= remote_get(name
);
785 return error("No such remote: %s", name
);
790 transport
= transport_get(NULL
, states
->remote
->url_nr
> 0 ?
791 states
->remote
->url
[0] : NULL
);
792 remote_refs
= transport_get_remote_refs(transport
);
793 transport_disconnect(transport
);
796 if (query
& GET_REF_STATES
)
797 get_ref_states(remote_refs
, states
);
798 if (query
& GET_HEAD_NAMES
)
799 get_head_names(remote_refs
, states
);
800 if (query
& GET_PUSH_REF_STATES
)
801 get_push_ref_states(remote_refs
, states
);
803 for_each_ref(append_ref_to_tracked_list
, states
);
804 sort_string_list(&states
->tracked
);
805 get_push_ref_states_noquery(states
);
812 struct string_list
*list
;
813 struct ref_states
*states
;
818 int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
820 struct show_info
*info
= cb_data
;
821 int n
= strlen(item
->string
);
824 string_list_insert(item
->string
, info
->list
);
828 int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
830 struct show_info
*info
= cb_data
;
831 struct ref_states
*states
= info
->states
;
832 const char *name
= item
->string
;
834 if (states
->queried
) {
835 const char *fmt
= "%s";
836 const char *arg
= "";
837 if (string_list_has_string(&states
->new, name
)) {
838 fmt
= " new (next fetch will store in remotes/%s)";
839 arg
= states
->remote
->name
;
840 } else if (string_list_has_string(&states
->tracked
, name
))
842 else if (string_list_has_string(&states
->stale
, name
))
843 arg
= " stale (use 'git remote prune' to remove)";
846 printf(" %-*s", info
->width
, name
);
850 printf(" %s\n", name
);
855 int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
857 struct show_info
*show_info
= cb_data
;
858 struct ref_states
*states
= show_info
->states
;
859 struct branch_info
*branch_info
= branch_item
->util
;
860 struct string_list_item
*item
;
863 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
864 strcmp(states
->remote
->name
, branch_info
->remote_name
))
866 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
867 show_info
->width
= n
;
868 if (branch_info
->rebase
)
869 show_info
->any_rebase
= 1;
871 item
= string_list_insert(branch_item
->string
, show_info
->list
);
872 item
->util
= branch_info
;
877 int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
879 struct show_info
*show_info
= cb_data
;
880 struct branch_info
*branch_info
= item
->util
;
881 struct string_list
*merge
= &branch_info
->merge
;
885 if (branch_info
->rebase
&& branch_info
->merge
.nr
> 1) {
886 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
891 printf(" %-*s ", show_info
->width
, item
->string
);
892 if (branch_info
->rebase
) {
893 printf("rebases onto remote %s\n", merge
->items
[0].string
);
895 } else if (show_info
->any_rebase
) {
896 printf(" merges with remote %s\n", merge
->items
[0].string
);
897 also
= " and with remote";
899 printf("merges with remote %s\n", merge
->items
[0].string
);
900 also
= " and with remote";
902 for (i
= 1; i
< merge
->nr
; i
++)
903 printf(" %-*s %s %s\n", show_info
->width
, "", also
,
904 merge
->items
[i
].string
);
909 int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
911 struct show_info
*show_info
= cb_data
;
912 struct push_info
*push_info
= push_item
->util
;
913 struct string_list_item
*item
;
915 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
916 show_info
->width
= n
;
917 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
918 show_info
->width2
= n
;
919 item
= string_list_append(push_item
->string
, show_info
->list
);
920 item
->util
= push_item
->util
;
925 * Sorting comparison for a string list that has push_info
926 * structs in its util field
928 static int cmp_string_with_push(const void *va
, const void *vb
)
930 const struct string_list_item
*a
= va
;
931 const struct string_list_item
*b
= vb
;
932 const struct push_info
*a_push
= a
->util
;
933 const struct push_info
*b_push
= b
->util
;
934 int cmp
= strcmp(a
->string
, b
->string
);
935 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
938 int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
940 struct show_info
*show_info
= cb_data
;
941 struct push_info
*push_info
= item
->util
;
942 char *src
= item
->string
, *status
= NULL
;
944 switch (push_info
->status
) {
945 case PUSH_STATUS_CREATE
:
948 case PUSH_STATUS_DELETE
:
952 case PUSH_STATUS_UPTODATE
:
953 status
= "up to date";
955 case PUSH_STATUS_FASTFORWARD
:
956 status
= "fast forwardable";
958 case PUSH_STATUS_OUTOFDATE
:
959 status
= "local out of date";
961 case PUSH_STATUS_NOTQUERIED
:
965 printf(" %-*s %s to %-*s (%s)\n", show_info
->width
, src
,
966 push_info
->forced
? "forces" : "pushes",
967 show_info
->width2
, push_info
->dest
, status
);
969 printf(" %-*s %s to %s\n", show_info
->width
, src
,
970 push_info
->forced
? "forces" : "pushes",
975 static int show(int argc
, const char **argv
)
977 int no_query
= 0, result
= 0, query_flag
= 0;
978 struct option options
[] = {
979 OPT_GROUP("show specific options"),
980 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
983 struct ref_states states
;
984 struct string_list info_list
= { NULL
, 0, 0, 0 };
985 struct show_info info
;
987 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_usage
,
994 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
996 memset(&states
, 0, sizeof(states
));
997 memset(&info
, 0, sizeof(info
));
998 info
.states
= &states
;
999 info
.list
= &info_list
;
1000 for (; argc
; argc
--, argv
++) {
1003 get_remote_ref_states(*argv
, &states
, query_flag
);
1005 printf("* remote %s\n", *argv
);
1006 if (states
.remote
->url_nr
) {
1007 for (i
=0; i
< states
.remote
->url_nr
; i
++)
1008 printf(" URL: %s\n", states
.remote
->url
[i
]);
1010 printf(" URL: %s\n", "(no URL)");
1012 printf(" HEAD branch: (not queried)\n");
1013 else if (!states
.heads
.nr
)
1014 printf(" HEAD branch: (unknown)\n");
1015 else if (states
.heads
.nr
== 1)
1016 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
1018 printf(" HEAD branch (remote HEAD is ambiguous,"
1019 " may be one of the following):\n");
1020 for (i
= 0; i
< states
.heads
.nr
; i
++)
1021 printf(" %s\n", states
.heads
.items
[i
].string
);
1024 /* remote branch info */
1026 for_each_string_list(add_remote_to_show_info
, &states
.new, &info
);
1027 for_each_string_list(add_remote_to_show_info
, &states
.tracked
, &info
);
1028 for_each_string_list(add_remote_to_show_info
, &states
.stale
, &info
);
1030 printf(" Remote branch%s:%s\n",
1031 info
.list
->nr
> 1 ? "es" : "",
1032 no_query
? " (status not queried)" : "");
1033 for_each_string_list(show_remote_info_item
, info
.list
, &info
);
1034 string_list_clear(info
.list
, 0);
1038 info
.any_rebase
= 0;
1039 for_each_string_list(add_local_to_show_info
, &branch_list
, &info
);
1041 printf(" Local branch%s configured for 'git pull':\n",
1042 info
.list
->nr
> 1 ? "es" : "");
1043 for_each_string_list(show_local_info_item
, info
.list
, &info
);
1044 string_list_clear(info
.list
, 0);
1047 if (states
.remote
->mirror
)
1048 printf(" Local refs will be mirrored by 'git push'\n");
1050 info
.width
= info
.width2
= 0;
1051 for_each_string_list(add_push_to_show_info
, &states
.push
, &info
);
1052 qsort(info
.list
->items
, info
.list
->nr
,
1053 sizeof(*info
.list
->items
), cmp_string_with_push
);
1055 printf(" Local ref%s configured for 'git push'%s:\n",
1056 info
.list
->nr
> 1 ? "s" : "",
1057 no_query
? " (status not queried)" : "");
1058 for_each_string_list(show_push_info_item
, info
.list
, &info
);
1059 string_list_clear(info
.list
, 0);
1061 free_remote_ref_states(&states
);
1067 static int set_head(int argc
, const char **argv
)
1069 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1070 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1071 char *head_name
= NULL
;
1073 struct option options
[] = {
1074 OPT_GROUP("set-head specific options"),
1075 OPT_BOOLEAN('a', "auto", &opt_a
,
1076 "set refs/remotes/<name>/HEAD according to remote"),
1077 OPT_BOOLEAN('d', "delete", &opt_d
,
1078 "delete refs/remotes/<name>/HEAD"),
1081 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_usage
,
1084 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1086 if (!opt_a
&& !opt_d
&& argc
== 2) {
1087 head_name
= xstrdup(argv
[1]);
1088 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1089 struct ref_states states
;
1090 memset(&states
, 0, sizeof(states
));
1091 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1092 if (!states
.heads
.nr
)
1093 result
|= error("Cannot determine remote HEAD");
1094 else if (states
.heads
.nr
> 1) {
1095 result
|= error("Multiple remote HEAD branches. "
1096 "Please choose one explicitly with:");
1097 for (i
= 0; i
< states
.heads
.nr
; i
++)
1098 fprintf(stderr
, " git remote set-head %s %s\n",
1099 argv
[0], states
.heads
.items
[i
].string
);
1101 head_name
= xstrdup(states
.heads
.items
[0].string
);
1102 free_remote_ref_states(&states
);
1103 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1104 if (delete_ref(buf
.buf
, NULL
, REF_NODEREF
))
1105 result
|= error("Could not delete %s", buf
.buf
);
1107 usage_with_options(builtin_remote_usage
, options
);
1110 unsigned char sha1
[20];
1111 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1112 /* make sure it's valid */
1113 if (!resolve_ref(buf2
.buf
, sha1
, 1, NULL
))
1114 result
|= error("Not a valid ref: %s", buf2
.buf
);
1115 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1116 result
|= error("Could not setup %s", buf
.buf
);
1118 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1122 strbuf_release(&buf
);
1123 strbuf_release(&buf2
);
1127 static int prune(int argc
, const char **argv
)
1129 int dry_run
= 0, result
= 0;
1130 struct option options
[] = {
1131 OPT_GROUP("prune specific options"),
1132 OPT__DRY_RUN(&dry_run
),
1136 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_usage
,
1140 usage_with_options(builtin_remote_usage
, options
);
1142 for (; argc
; argc
--, argv
++)
1143 result
|= prune_remote(*argv
, dry_run
);
1148 static int prune_remote(const char *remote
, int dry_run
)
1151 struct ref_states states
;
1152 const char *dangling_msg
= dry_run
1153 ? " %s will become dangling!\n"
1154 : " %s has become dangling!\n";
1156 memset(&states
, 0, sizeof(states
));
1157 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1159 if (states
.stale
.nr
) {
1160 printf("Pruning %s\n", remote
);
1162 states
.remote
->url_nr
1163 ? states
.remote
->url
[0]
1167 for (i
= 0; i
< states
.stale
.nr
; i
++) {
1168 const char *refname
= states
.stale
.items
[i
].util
;
1171 result
|= delete_ref(refname
, NULL
, 0);
1173 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
1174 abbrev_ref(refname
, "refs/remotes/"));
1175 warn_dangling_symref(dangling_msg
, refname
);
1178 free_remote_ref_states(&states
);
1182 static int get_one_remote_for_update(struct remote
*remote
, void *priv
)
1184 struct string_list
*list
= priv
;
1185 if (!remote
->skip_default_update
)
1186 string_list_append(remote
->name
, list
);
1190 struct remote_group
{
1192 struct string_list
*list
;
1195 static int get_remote_group(const char *key
, const char *value
, void *num_hits
)
1197 if (!prefixcmp(key
, "remotes.") &&
1198 !strcmp(key
+ 8, remote_group
.name
)) {
1199 /* split list by white space */
1200 int space
= strcspn(value
, " \t\n");
1203 string_list_append(xstrndup(value
, space
),
1205 ++*((int *)num_hits
);
1207 value
+= space
+ (value
[space
] != '\0');
1208 space
= strcspn(value
, " \t\n");
1215 static int update(int argc
, const char **argv
)
1217 int i
, result
= 0, prune
= 0;
1218 struct string_list list
= { NULL
, 0, 0, 0 };
1219 static const char *default_argv
[] = { NULL
, "default", NULL
};
1220 struct option options
[] = {
1221 OPT_GROUP("update specific options"),
1222 OPT_BOOLEAN('p', "prune", &prune
,
1223 "prune remotes after fetching"),
1227 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_usage
,
1228 PARSE_OPT_KEEP_ARGV0
);
1231 argv
= default_argv
;
1234 remote_group
.list
= &list
;
1235 for (i
= 1; i
< argc
; i
++) {
1236 int groups_found
= 0;
1237 remote_group
.name
= argv
[i
];
1238 result
= git_config(get_remote_group
, &groups_found
);
1239 if (!groups_found
&& (i
!= 1 || strcmp(argv
[1], "default"))) {
1240 struct remote
*remote
;
1241 if (!remote_is_configured(argv
[i
]))
1242 die("No such remote or remote group: %s",
1244 remote
= remote_get(argv
[i
]);
1245 string_list_append(remote
->name
, remote_group
.list
);
1249 if (!result
&& !list
.nr
&& argc
== 2 && !strcmp(argv
[1], "default"))
1250 result
= for_each_remote(get_one_remote_for_update
, &list
);
1252 for (i
= 0; i
< list
.nr
; i
++) {
1253 int err
= fetch_remote(list
.items
[i
].string
);
1256 result
|= prune_remote(list
.items
[i
].string
, 0);
1259 /* all names were strdup()ed or strndup()ed */
1260 list
.strdup_strings
= 1;
1261 string_list_clear(&list
, 0);
1266 static int get_one_entry(struct remote
*remote
, void *priv
)
1268 struct string_list
*list
= priv
;
1270 if (remote
->url_nr
> 0) {
1273 for (i
= 0; i
< remote
->url_nr
; i
++)
1274 string_list_append(remote
->name
, list
)->util
= (void *)remote
->url
[i
];
1276 string_list_append(remote
->name
, list
)->util
= NULL
;
1281 static int show_all(void)
1283 struct string_list list
= { NULL
, 0, 0 };
1284 int result
= for_each_remote(get_one_entry
, &list
);
1289 sort_string_list(&list
);
1290 for (i
= 0; i
< list
.nr
; i
++) {
1291 struct string_list_item
*item
= list
.items
+ i
;
1293 printf("%s\t%s\n", item
->string
,
1294 item
->util
? (const char *)item
->util
: "");
1296 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1298 printf("%s\n", item
->string
);
1305 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1307 struct option options
[] = {
1308 OPT__VERBOSE(&verbose
),
1313 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1314 PARSE_OPT_STOP_AT_NON_OPTION
);
1317 result
= show_all();
1318 else if (!strcmp(argv
[0], "add"))
1319 result
= add(argc
, argv
);
1320 else if (!strcmp(argv
[0], "rename"))
1321 result
= mv(argc
, argv
);
1322 else if (!strcmp(argv
[0], "rm"))
1323 result
= rm(argc
, argv
);
1324 else if (!strcmp(argv
[0], "set-head"))
1325 result
= set_head(argc
, argv
);
1326 else if (!strcmp(argv
[0], "show"))
1327 result
= show(argc
, argv
);
1328 else if (!strcmp(argv
[0], "prune"))
1329 result
= prune(argc
, argv
);
1330 else if (!strcmp(argv
[0], "update"))
1331 result
= update(argc
, argv
);
1333 error("Unknown subcommand: %s", argv
[0]);
1334 usage_with_options(builtin_remote_usage
, options
);
1337 return result
? 1 : 0;