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
, options
, builtin_remote_usage
, 0);
85 usage_with_options(builtin_remote_usage
, options
);
90 remote
= remote_get(name
);
91 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
92 remote
->fetch_refspec_nr
))
93 die("remote %s already exists.", name
);
95 strbuf_addf(&buf2
, "refs/heads/test:refs/remotes/%s/test", name
);
96 if (!valid_fetch_refspec(buf2
.buf
))
97 die("'%s' is not a valid remote name", name
);
99 strbuf_addf(&buf
, "remote.%s.url", name
);
100 if (git_config_set(buf
.buf
, url
))
104 strbuf_addf(&buf
, "remote.%s.fetch", name
);
107 string_list_append("*", &track
);
108 for (i
= 0; i
< track
.nr
; i
++) {
109 struct string_list_item
*item
= track
.items
+ i
;
112 strbuf_addch(&buf2
, '+');
114 strbuf_addf(&buf2
, "refs/%s:refs/%s",
115 item
->string
, item
->string
);
117 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
118 item
->string
, name
, item
->string
);
119 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
125 strbuf_addf(&buf
, "remote.%s.mirror", name
);
126 if (git_config_set(buf
.buf
, "true"))
130 if (fetch
&& fetch_remote(name
))
135 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
138 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
140 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
141 return error("Could not setup master '%s'", master
);
144 strbuf_release(&buf
);
145 strbuf_release(&buf2
);
146 string_list_clear(&track
, 0);
153 struct string_list merge
;
157 static struct string_list branch_list
;
159 static const char *abbrev_ref(const char *name
, const char *prefix
)
161 const char *abbrev
= skip_prefix(name
, prefix
);
166 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
168 static int config_read_branches(const char *key
, const char *value
, void *cb
)
170 if (!prefixcmp(key
, "branch.")) {
171 const char *orig_key
= key
;
173 struct string_list_item
*item
;
174 struct branch_info
*info
;
175 enum { REMOTE
, MERGE
, REBASE
} type
;
178 if (!postfixcmp(key
, ".remote")) {
179 name
= xstrndup(key
, strlen(key
) - 7);
181 } else if (!postfixcmp(key
, ".merge")) {
182 name
= xstrndup(key
, strlen(key
) - 6);
184 } else if (!postfixcmp(key
, ".rebase")) {
185 name
= xstrndup(key
, strlen(key
) - 7);
190 item
= string_list_insert(name
, &branch_list
);
193 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
195 if (type
== REMOTE
) {
196 if (info
->remote_name
)
197 warning("more than one %s", orig_key
);
198 info
->remote_name
= xstrdup(value
);
199 } else if (type
== MERGE
) {
200 char *space
= strchr(value
, ' ');
201 value
= abbrev_branch(value
);
204 merge
= xstrndup(value
, space
- value
);
205 string_list_append(merge
, &info
->merge
);
206 value
= abbrev_branch(space
+ 1);
207 space
= strchr(value
, ' ');
209 string_list_append(xstrdup(value
), &info
->merge
);
211 info
->rebase
= git_config_bool(orig_key
, value
);
216 static void read_branches(void)
220 git_config(config_read_branches
, NULL
);
224 struct remote
*remote
;
225 struct string_list
new, stale
, tracked
, heads
, push
;
229 static int handle_one_branch(const char *refname
,
230 const unsigned char *sha1
, int flags
, void *cb_data
)
232 struct ref_states
*states
= cb_data
;
233 struct refspec refspec
;
235 memset(&refspec
, 0, sizeof(refspec
));
236 refspec
.dst
= (char *)refname
;
237 if (!remote_find_tracking(states
->remote
, &refspec
)) {
238 struct string_list_item
*item
;
239 const char *name
= abbrev_branch(refspec
.src
);
240 /* symbolic refs pointing nowhere were handled already */
241 if ((flags
& REF_ISSYMREF
) ||
242 string_list_has_string(&states
->tracked
, name
) ||
243 string_list_has_string(&states
->new, name
))
245 item
= string_list_append(name
, &states
->stale
);
246 item
->util
= xstrdup(refname
);
251 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
253 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
257 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
258 if (get_fetch_map(remote_refs
, states
->remote
->fetch
+ i
, &tail
, 1))
259 die("Could not get fetch map for refspec %s",
260 states
->remote
->fetch_refspec
[i
]);
262 states
->new.strdup_strings
= states
->tracked
.strdup_strings
= 1;
263 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
264 unsigned char sha1
[20];
265 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
266 string_list_append(abbrev_branch(ref
->name
), &states
->new);
268 string_list_append(abbrev_branch(ref
->name
), &states
->tracked
);
270 free_refs(fetch_map
);
272 sort_string_list(&states
->new);
273 sort_string_list(&states
->tracked
);
274 for_each_ref(handle_one_branch
, states
);
275 sort_string_list(&states
->stale
);
284 PUSH_STATUS_CREATE
= 0,
286 PUSH_STATUS_UPTODATE
,
287 PUSH_STATUS_FASTFORWARD
,
288 PUSH_STATUS_OUTOFDATE
,
289 PUSH_STATUS_NOTQUERIED
,
293 static int get_push_ref_states(const struct ref
*remote_refs
,
294 struct ref_states
*states
)
296 struct remote
*remote
= states
->remote
;
297 struct ref
*ref
, *local_refs
, *push_map
;
301 local_refs
= get_local_heads();
302 push_map
= copy_ref_list(remote_refs
);
304 match_refs(local_refs
, &push_map
, remote
->push_refspec_nr
,
305 remote
->push_refspec
, MATCH_REFS_NONE
);
307 states
->push
.strdup_strings
= 1;
308 for (ref
= push_map
; ref
; ref
= ref
->next
) {
309 struct string_list_item
*item
;
310 struct push_info
*info
;
314 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
316 item
= string_list_append(abbrev_branch(ref
->peer_ref
->name
),
318 item
->util
= xcalloc(sizeof(struct push_info
), 1);
320 info
->forced
= ref
->force
;
321 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
323 if (is_null_sha1(ref
->new_sha1
)) {
324 info
->status
= PUSH_STATUS_DELETE
;
325 } else if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
))
326 info
->status
= PUSH_STATUS_UPTODATE
;
327 else if (is_null_sha1(ref
->old_sha1
))
328 info
->status
= PUSH_STATUS_CREATE
;
329 else if (has_sha1_file(ref
->old_sha1
) &&
330 ref_newer(ref
->new_sha1
, ref
->old_sha1
))
331 info
->status
= PUSH_STATUS_FASTFORWARD
;
333 info
->status
= PUSH_STATUS_OUTOFDATE
;
335 free_refs(local_refs
);
340 static int get_push_ref_states_noquery(struct ref_states
*states
)
343 struct remote
*remote
= states
->remote
;
344 struct string_list_item
*item
;
345 struct push_info
*info
;
350 states
->push
.strdup_strings
= 1;
351 if (!remote
->push_refspec_nr
) {
352 item
= string_list_append("(matching)", &states
->push
);
353 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
354 info
->status
= PUSH_STATUS_NOTQUERIED
;
355 info
->dest
= xstrdup(item
->string
);
357 for (i
= 0; i
< remote
->push_refspec_nr
; i
++) {
358 struct refspec
*spec
= remote
->push
+ i
;
360 item
= string_list_append("(matching)", &states
->push
);
361 else if (strlen(spec
->src
))
362 item
= string_list_append(spec
->src
, &states
->push
);
364 item
= string_list_append("(delete)", &states
->push
);
366 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
367 info
->forced
= spec
->force
;
368 info
->status
= PUSH_STATUS_NOTQUERIED
;
369 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
374 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
376 struct ref
*ref
, *matches
;
377 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
378 struct refspec refspec
;
382 refspec
.src
= refspec
.dst
= "refs/heads/*";
383 states
->heads
.strdup_strings
= 1;
384 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
385 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
387 for(ref
= matches
; ref
; ref
= ref
->next
)
388 string_list_append(abbrev_branch(ref
->name
), &states
->heads
);
390 free_refs(fetch_map
);
396 struct known_remote
{
397 struct known_remote
*next
;
398 struct remote
*remote
;
401 struct known_remotes
{
402 struct remote
*to_delete
;
403 struct known_remote
*list
;
406 static int add_known_remote(struct remote
*remote
, void *cb_data
)
408 struct known_remotes
*all
= cb_data
;
409 struct known_remote
*r
;
411 if (!strcmp(all
->to_delete
->name
, remote
->name
))
414 r
= xmalloc(sizeof(*r
));
421 struct branches_for_remote
{
422 struct remote
*remote
;
423 struct string_list
*branches
, *skipped
;
424 struct known_remotes
*keep
;
427 static int add_branch_for_removal(const char *refname
,
428 const unsigned char *sha1
, int flags
, void *cb_data
)
430 struct branches_for_remote
*branches
= cb_data
;
431 struct refspec refspec
;
432 struct string_list_item
*item
;
433 struct known_remote
*kr
;
435 memset(&refspec
, 0, sizeof(refspec
));
436 refspec
.dst
= (char *)refname
;
437 if (remote_find_tracking(branches
->remote
, &refspec
))
440 /* don't delete a branch if another remote also uses it */
441 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
442 memset(&refspec
, 0, sizeof(refspec
));
443 refspec
.dst
= (char *)refname
;
444 if (!remote_find_tracking(kr
->remote
, &refspec
))
448 /* don't delete non-remote refs */
449 if (prefixcmp(refname
, "refs/remotes")) {
450 /* advise user how to delete local branches */
451 if (!prefixcmp(refname
, "refs/heads/"))
452 string_list_append(abbrev_branch(refname
),
454 /* silently skip over other non-remote refs */
458 /* make sure that symrefs are deleted */
459 if (flags
& REF_ISSYMREF
)
460 return unlink(git_path("%s", refname
));
462 item
= string_list_append(refname
, branches
->branches
);
463 item
->util
= xmalloc(20);
464 hashcpy(item
->util
, sha1
);
472 struct string_list
*remote_branches
;
475 static int read_remote_branches(const char *refname
,
476 const unsigned char *sha1
, int flags
, void *cb_data
)
478 struct rename_info
*rename
= cb_data
;
479 struct strbuf buf
= STRBUF_INIT
;
480 struct string_list_item
*item
;
482 unsigned char orig_sha1
[20];
485 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
486 if(!prefixcmp(refname
, buf
.buf
)) {
487 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
488 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
489 if (flag
& REF_ISSYMREF
)
490 item
->util
= xstrdup(symref
);
498 static int migrate_file(struct remote
*remote
)
500 struct strbuf buf
= STRBUF_INIT
;
504 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
505 for (i
= 0; i
< remote
->url_nr
; i
++)
506 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
507 return error("Could not append '%s' to '%s'",
508 remote
->url
[i
], buf
.buf
);
510 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
511 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
512 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
513 return error("Could not append '%s' to '%s'",
514 remote
->push_refspec
[i
], buf
.buf
);
516 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
517 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
518 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
519 return error("Could not append '%s' to '%s'",
520 remote
->fetch_refspec
[i
], buf
.buf
);
521 if (remote
->origin
== REMOTE_REMOTES
)
522 path
= git_path("remotes/%s", remote
->name
);
523 else if (remote
->origin
== REMOTE_BRANCHES
)
524 path
= git_path("branches/%s", remote
->name
);
526 unlink_or_warn(path
);
530 static int mv(int argc
, const char **argv
)
532 struct option options
[] = {
535 struct remote
*oldremote
, *newremote
;
536 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
537 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
538 struct rename_info rename
;
542 usage_with_options(builtin_remote_usage
, options
);
544 rename
.old
= argv
[1];
545 rename
.new = argv
[2];
546 rename
.remote_branches
= &remote_branches
;
548 oldremote
= remote_get(rename
.old
);
550 die("No such remote: %s", rename
.old
);
552 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
553 return migrate_file(oldremote
);
555 newremote
= remote_get(rename
.new);
556 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
557 die("remote %s already exists.", rename
.new);
559 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
560 if (!valid_fetch_refspec(buf
.buf
))
561 die("'%s' is not a valid remote name", rename
.new);
564 strbuf_addf(&buf
, "remote.%s", rename
.old
);
565 strbuf_addf(&buf2
, "remote.%s", rename
.new);
566 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
567 return error("Could not rename config section '%s' to '%s'",
571 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
572 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
573 return error("Could not remove config section '%s'", buf
.buf
);
574 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
578 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
579 ptr
= strstr(buf2
.buf
, rename
.old
);
581 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
582 rename
.new, strlen(rename
.new));
583 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
584 return error("Could not append '%s'", buf
.buf
);
588 for (i
= 0; i
< branch_list
.nr
; i
++) {
589 struct string_list_item
*item
= branch_list
.items
+ i
;
590 struct branch_info
*info
= item
->util
;
591 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old
)) {
593 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
594 if (git_config_set(buf
.buf
, rename
.new)) {
595 return error("Could not set '%s'", buf
.buf
);
601 * First remove symrefs, then rename the rest, finally create
604 for_each_ref(read_remote_branches
, &rename
);
605 for (i
= 0; i
< remote_branches
.nr
; i
++) {
606 struct string_list_item
*item
= remote_branches
.items
+ i
;
608 unsigned char sha1
[20];
610 resolve_ref(item
->string
, sha1
, 1, &flag
);
611 if (!(flag
& REF_ISSYMREF
))
613 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
614 die("deleting '%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_addf(&buf2
, "remote: renamed %s to %s",
627 item
->string
, buf
.buf
);
628 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
629 die("renaming '%s' failed", item
->string
);
631 for (i
= 0; i
< remote_branches
.nr
; i
++) {
632 struct string_list_item
*item
= remote_branches
.items
+ i
;
637 strbuf_addstr(&buf
, item
->string
);
638 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
639 rename
.new, strlen(rename
.new));
641 strbuf_addstr(&buf2
, item
->util
);
642 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
643 rename
.new, strlen(rename
.new));
645 strbuf_addf(&buf3
, "remote: renamed %s to %s",
646 item
->string
, buf
.buf
);
647 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
648 die("creating '%s' failed", buf
.buf
);
653 static int remove_branches(struct string_list
*branches
)
656 for (i
= 0; i
< branches
->nr
; i
++) {
657 struct string_list_item
*item
= branches
->items
+ i
;
658 const char *refname
= item
->string
;
659 unsigned char *sha1
= item
->util
;
661 if (delete_ref(refname
, sha1
, 0))
662 result
|= error("Could not remove branch %s", refname
);
667 static int rm(int argc
, const char **argv
)
669 struct option options
[] = {
672 struct remote
*remote
;
673 struct strbuf buf
= STRBUF_INIT
;
674 struct known_remotes known_remotes
= { NULL
, NULL
};
675 struct string_list branches
= { NULL
, 0, 0, 1 };
676 struct string_list skipped
= { NULL
, 0, 0, 1 };
677 struct branches_for_remote cb_data
= {
678 NULL
, &branches
, &skipped
, &known_remotes
683 usage_with_options(builtin_remote_usage
, options
);
685 remote
= remote_get(argv
[1]);
687 die("No such remote: %s", argv
[1]);
689 known_remotes
.to_delete
= remote
;
690 for_each_remote(add_known_remote
, &known_remotes
);
692 strbuf_addf(&buf
, "remote.%s", remote
->name
);
693 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
694 return error("Could not remove config section '%s'", buf
.buf
);
697 for (i
= 0; i
< branch_list
.nr
; i
++) {
698 struct string_list_item
*item
= branch_list
.items
+ i
;
699 struct branch_info
*info
= item
->util
;
700 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
701 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
702 for (k
= keys
; *k
; k
++) {
704 strbuf_addf(&buf
, "branch.%s.%s",
706 if (git_config_set(buf
.buf
, NULL
)) {
707 strbuf_release(&buf
);
715 * We cannot just pass a function to for_each_ref() which deletes
716 * the branches one by one, since for_each_ref() relies on cached
717 * refs, which are invalidated when deleting a branch.
719 cb_data
.remote
= remote
;
720 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
721 strbuf_release(&buf
);
724 result
= remove_branches(&branches
);
725 string_list_clear(&branches
, 1);
728 fprintf(stderr
, skipped
.nr
== 1 ?
729 "Note: A non-remote branch was not removed; "
730 "to delete it, use:\n" :
731 "Note: Non-remote branches were not removed; "
732 "to delete them, use:\n");
733 for (i
= 0; i
< skipped
.nr
; i
++)
734 fprintf(stderr
, " git branch -d %s\n",
735 skipped
.items
[i
].string
);
737 string_list_clear(&skipped
, 0);
742 void clear_push_info(void *util
, const char *string
)
744 struct push_info
*info
= util
;
749 static void free_remote_ref_states(struct ref_states
*states
)
751 string_list_clear(&states
->new, 0);
752 string_list_clear(&states
->stale
, 0);
753 string_list_clear(&states
->tracked
, 0);
754 string_list_clear(&states
->heads
, 0);
755 string_list_clear_func(&states
->push
, clear_push_info
);
758 static int append_ref_to_tracked_list(const char *refname
,
759 const unsigned char *sha1
, int flags
, void *cb_data
)
761 struct ref_states
*states
= cb_data
;
762 struct refspec refspec
;
764 if (flags
& REF_ISSYMREF
)
767 memset(&refspec
, 0, sizeof(refspec
));
768 refspec
.dst
= (char *)refname
;
769 if (!remote_find_tracking(states
->remote
, &refspec
))
770 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
775 static int get_remote_ref_states(const char *name
,
776 struct ref_states
*states
,
779 struct transport
*transport
;
780 const struct ref
*remote_refs
;
782 states
->remote
= remote_get(name
);
784 return error("No such remote: %s", name
);
789 transport
= transport_get(NULL
, states
->remote
->url_nr
> 0 ?
790 states
->remote
->url
[0] : NULL
);
791 remote_refs
= transport_get_remote_refs(transport
);
792 transport_disconnect(transport
);
795 if (query
& GET_REF_STATES
)
796 get_ref_states(remote_refs
, states
);
797 if (query
& GET_HEAD_NAMES
)
798 get_head_names(remote_refs
, states
);
799 if (query
& GET_PUSH_REF_STATES
)
800 get_push_ref_states(remote_refs
, states
);
802 for_each_ref(append_ref_to_tracked_list
, states
);
803 sort_string_list(&states
->tracked
);
804 get_push_ref_states_noquery(states
);
811 struct string_list
*list
;
812 struct ref_states
*states
;
817 int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
819 struct show_info
*info
= cb_data
;
820 int n
= strlen(item
->string
);
823 string_list_insert(item
->string
, info
->list
);
827 int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
829 struct show_info
*info
= cb_data
;
830 struct ref_states
*states
= info
->states
;
831 const char *name
= item
->string
;
833 if (states
->queried
) {
834 const char *fmt
= "%s";
835 const char *arg
= "";
836 if (string_list_has_string(&states
->new, name
)) {
837 fmt
= " new (next fetch will store in remotes/%s)";
838 arg
= states
->remote
->name
;
839 } else if (string_list_has_string(&states
->tracked
, name
))
841 else if (string_list_has_string(&states
->stale
, name
))
842 arg
= " stale (use 'git remote prune' to remove)";
845 printf(" %-*s", info
->width
, name
);
849 printf(" %s\n", name
);
854 int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
856 struct show_info
*show_info
= cb_data
;
857 struct ref_states
*states
= show_info
->states
;
858 struct branch_info
*branch_info
= branch_item
->util
;
859 struct string_list_item
*item
;
862 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
863 strcmp(states
->remote
->name
, branch_info
->remote_name
))
865 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
866 show_info
->width
= n
;
867 if (branch_info
->rebase
)
868 show_info
->any_rebase
= 1;
870 item
= string_list_insert(branch_item
->string
, show_info
->list
);
871 item
->util
= branch_info
;
876 int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
878 struct show_info
*show_info
= cb_data
;
879 struct branch_info
*branch_info
= item
->util
;
880 struct string_list
*merge
= &branch_info
->merge
;
884 if (branch_info
->rebase
&& branch_info
->merge
.nr
> 1) {
885 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
890 printf(" %-*s ", show_info
->width
, item
->string
);
891 if (branch_info
->rebase
) {
892 printf("rebases onto remote %s\n", merge
->items
[0].string
);
894 } else if (show_info
->any_rebase
) {
895 printf(" merges with remote %s\n", merge
->items
[0].string
);
896 also
= " and with remote";
898 printf("merges with remote %s\n", merge
->items
[0].string
);
899 also
= " and with remote";
901 for (i
= 1; i
< merge
->nr
; i
++)
902 printf(" %-*s %s %s\n", show_info
->width
, "", also
,
903 merge
->items
[i
].string
);
908 int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
910 struct show_info
*show_info
= cb_data
;
911 struct push_info
*push_info
= push_item
->util
;
912 struct string_list_item
*item
;
914 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
915 show_info
->width
= n
;
916 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
917 show_info
->width2
= n
;
918 item
= string_list_append(push_item
->string
, show_info
->list
);
919 item
->util
= push_item
->util
;
924 * Sorting comparison for a string list that has push_info
925 * structs in its util field
927 static int cmp_string_with_push(const void *va
, const void *vb
)
929 const struct string_list_item
*a
= va
;
930 const struct string_list_item
*b
= vb
;
931 const struct push_info
*a_push
= a
->util
;
932 const struct push_info
*b_push
= b
->util
;
933 int cmp
= strcmp(a
->string
, b
->string
);
934 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
937 int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
939 struct show_info
*show_info
= cb_data
;
940 struct push_info
*push_info
= item
->util
;
941 char *src
= item
->string
, *status
= NULL
;
943 switch (push_info
->status
) {
944 case PUSH_STATUS_CREATE
:
947 case PUSH_STATUS_DELETE
:
951 case PUSH_STATUS_UPTODATE
:
952 status
= "up to date";
954 case PUSH_STATUS_FASTFORWARD
:
955 status
= "fast forwardable";
957 case PUSH_STATUS_OUTOFDATE
:
958 status
= "local out of date";
960 case PUSH_STATUS_NOTQUERIED
:
964 printf(" %-*s %s to %-*s (%s)\n", show_info
->width
, src
,
965 push_info
->forced
? "forces" : "pushes",
966 show_info
->width2
, push_info
->dest
, status
);
968 printf(" %-*s %s to %s\n", show_info
->width
, src
,
969 push_info
->forced
? "forces" : "pushes",
974 static int show(int argc
, const char **argv
)
976 int no_query
= 0, result
= 0, query_flag
= 0;
977 struct option options
[] = {
978 OPT_GROUP("show specific options"),
979 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
982 struct ref_states states
;
983 struct string_list info_list
= { NULL
, 0, 0, 0 };
984 struct show_info info
;
986 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
992 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
994 memset(&states
, 0, sizeof(states
));
995 memset(&info
, 0, sizeof(info
));
996 info
.states
= &states
;
997 info
.list
= &info_list
;
998 for (; argc
; argc
--, argv
++) {
1001 get_remote_ref_states(*argv
, &states
, query_flag
);
1003 printf("* remote %s\n URL: %s\n", *argv
,
1004 states
.remote
->url_nr
> 0 ?
1005 states
.remote
->url
[0] : "(no URL)");
1007 printf(" HEAD branch: (not queried)\n");
1008 else if (!states
.heads
.nr
)
1009 printf(" HEAD branch: (unknown)\n");
1010 else if (states
.heads
.nr
== 1)
1011 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
1013 printf(" HEAD branch (remote HEAD is ambiguous,"
1014 " may be one of the following):\n");
1015 for (i
= 0; i
< states
.heads
.nr
; i
++)
1016 printf(" %s\n", states
.heads
.items
[i
].string
);
1019 /* remote branch info */
1021 for_each_string_list(add_remote_to_show_info
, &states
.new, &info
);
1022 for_each_string_list(add_remote_to_show_info
, &states
.tracked
, &info
);
1023 for_each_string_list(add_remote_to_show_info
, &states
.stale
, &info
);
1025 printf(" Remote branch%s:%s\n",
1026 info
.list
->nr
> 1 ? "es" : "",
1027 no_query
? " (status not queried)" : "");
1028 for_each_string_list(show_remote_info_item
, info
.list
, &info
);
1029 string_list_clear(info
.list
, 0);
1033 info
.any_rebase
= 0;
1034 for_each_string_list(add_local_to_show_info
, &branch_list
, &info
);
1036 printf(" Local branch%s configured for 'git pull':\n",
1037 info
.list
->nr
> 1 ? "es" : "");
1038 for_each_string_list(show_local_info_item
, info
.list
, &info
);
1039 string_list_clear(info
.list
, 0);
1042 if (states
.remote
->mirror
)
1043 printf(" Local refs will be mirrored by 'git push'\n");
1045 info
.width
= info
.width2
= 0;
1046 for_each_string_list(add_push_to_show_info
, &states
.push
, &info
);
1047 qsort(info
.list
->items
, info
.list
->nr
,
1048 sizeof(*info
.list
->items
), cmp_string_with_push
);
1050 printf(" Local ref%s configured for 'git push'%s:\n",
1051 info
.list
->nr
> 1 ? "s" : "",
1052 no_query
? " (status not queried)" : "");
1053 for_each_string_list(show_push_info_item
, info
.list
, &info
);
1054 string_list_clear(info
.list
, 0);
1056 free_remote_ref_states(&states
);
1062 static int set_head(int argc
, const char **argv
)
1064 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1065 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1066 char *head_name
= NULL
;
1068 struct option options
[] = {
1069 OPT_GROUP("set-head specific options"),
1070 OPT_BOOLEAN('a', "auto", &opt_a
,
1071 "set refs/remotes/<name>/HEAD according to remote"),
1072 OPT_BOOLEAN('d', "delete", &opt_d
,
1073 "delete refs/remotes/<name>/HEAD"),
1076 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
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
, options
, builtin_remote_usage
, 0);
1133 usage_with_options(builtin_remote_usage
, options
);
1135 for (; argc
; argc
--, argv
++)
1136 result
|= prune_remote(*argv
, dry_run
);
1141 static int prune_remote(const char *remote
, int dry_run
)
1144 struct ref_states states
;
1145 const char *dangling_msg
= dry_run
1146 ? " %s will become dangling!\n"
1147 : " %s has become dangling!\n";
1149 memset(&states
, 0, sizeof(states
));
1150 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1152 if (states
.stale
.nr
) {
1153 printf("Pruning %s\n", remote
);
1155 states
.remote
->url_nr
1156 ? states
.remote
->url
[0]
1160 for (i
= 0; i
< states
.stale
.nr
; i
++) {
1161 const char *refname
= states
.stale
.items
[i
].util
;
1164 result
|= delete_ref(refname
, NULL
, 0);
1166 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
1167 abbrev_ref(refname
, "refs/remotes/"));
1168 warn_dangling_symref(dangling_msg
, refname
);
1171 free_remote_ref_states(&states
);
1175 static int get_one_remote_for_update(struct remote
*remote
, void *priv
)
1177 struct string_list
*list
= priv
;
1178 if (!remote
->skip_default_update
)
1179 string_list_append(remote
->name
, list
);
1183 struct remote_group
{
1185 struct string_list
*list
;
1188 static int get_remote_group(const char *key
, const char *value
, void *num_hits
)
1190 if (!prefixcmp(key
, "remotes.") &&
1191 !strcmp(key
+ 8, remote_group
.name
)) {
1192 /* split list by white space */
1193 int space
= strcspn(value
, " \t\n");
1196 string_list_append(xstrndup(value
, space
),
1198 ++*((int *)num_hits
);
1200 value
+= space
+ (value
[space
] != '\0');
1201 space
= strcspn(value
, " \t\n");
1208 static int update(int argc
, const char **argv
)
1210 int i
, result
= 0, prune
= 0;
1211 struct string_list list
= { NULL
, 0, 0, 0 };
1212 static const char *default_argv
[] = { NULL
, "default", NULL
};
1213 struct option options
[] = {
1214 OPT_GROUP("update specific options"),
1215 OPT_BOOLEAN('p', "prune", &prune
,
1216 "prune remotes after fetching"),
1220 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
1221 PARSE_OPT_KEEP_ARGV0
);
1224 argv
= default_argv
;
1227 remote_group
.list
= &list
;
1228 for (i
= 1; i
< argc
; i
++) {
1229 int groups_found
= 0;
1230 remote_group
.name
= argv
[i
];
1231 result
= git_config(get_remote_group
, &groups_found
);
1232 if (!groups_found
&& (i
!= 1 || strcmp(argv
[1], "default"))) {
1233 struct remote
*remote
;
1234 if (!remote_is_configured(argv
[i
]))
1235 die("No such remote or remote group: %s",
1237 remote
= remote_get(argv
[i
]);
1238 string_list_append(remote
->name
, remote_group
.list
);
1242 if (!result
&& !list
.nr
&& argc
== 2 && !strcmp(argv
[1], "default"))
1243 result
= for_each_remote(get_one_remote_for_update
, &list
);
1245 for (i
= 0; i
< list
.nr
; i
++) {
1246 int err
= fetch_remote(list
.items
[i
].string
);
1249 result
|= prune_remote(list
.items
[i
].string
, 0);
1252 /* all names were strdup()ed or strndup()ed */
1253 list
.strdup_strings
= 1;
1254 string_list_clear(&list
, 0);
1259 static int get_one_entry(struct remote
*remote
, void *priv
)
1261 struct string_list
*list
= priv
;
1263 if (remote
->url_nr
> 0) {
1266 for (i
= 0; i
< remote
->url_nr
; i
++)
1267 string_list_append(remote
->name
, list
)->util
= (void *)remote
->url
[i
];
1269 string_list_append(remote
->name
, list
)->util
= NULL
;
1274 static int show_all(void)
1276 struct string_list list
= { NULL
, 0, 0 };
1277 int result
= for_each_remote(get_one_entry
, &list
);
1282 sort_string_list(&list
);
1283 for (i
= 0; i
< list
.nr
; i
++) {
1284 struct string_list_item
*item
= list
.items
+ i
;
1286 printf("%s\t%s\n", item
->string
,
1287 item
->util
? (const char *)item
->util
: "");
1289 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1291 printf("%s\n", item
->string
);
1298 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1300 struct option options
[] = {
1301 OPT__VERBOSE(&verbose
),
1306 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
1307 PARSE_OPT_STOP_AT_NON_OPTION
);
1310 result
= show_all();
1311 else if (!strcmp(argv
[0], "add"))
1312 result
= add(argc
, argv
);
1313 else if (!strcmp(argv
[0], "rename"))
1314 result
= mv(argc
, argv
);
1315 else if (!strcmp(argv
[0], "rm"))
1316 result
= rm(argc
, argv
);
1317 else if (!strcmp(argv
[0], "set-head"))
1318 result
= set_head(argc
, argv
);
1319 else if (!strcmp(argv
[0], "show"))
1320 result
= show(argc
, argv
);
1321 else if (!strcmp(argv
[0], "prune"))
1322 result
= prune(argc
, argv
);
1323 else if (!strcmp(argv
[0], "update"))
1324 result
= update(argc
, argv
);
1326 error("Unknown subcommand: %s", argv
[0]);
1327 usage_with_options(builtin_remote_usage
, options
);
1330 return result
? 1 : 0;