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
, **push_tail
;
301 local_refs
= get_local_heads();
302 ref
= push_map
= copy_ref_list(remote_refs
);
305 push_tail
= &ref
->next
;
307 match_refs(local_refs
, push_map
, &push_tail
, remote
->push_refspec_nr
,
308 remote
->push_refspec
, MATCH_REFS_NONE
);
310 states
->push
.strdup_strings
= 1;
311 for (ref
= push_map
; ref
; ref
= ref
->next
) {
312 struct string_list_item
*item
;
313 struct push_info
*info
;
317 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
319 item
= string_list_append(abbrev_branch(ref
->peer_ref
->name
),
321 item
->util
= xcalloc(sizeof(struct push_info
), 1);
323 info
->forced
= ref
->force
;
324 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
326 if (is_null_sha1(ref
->new_sha1
)) {
327 info
->status
= PUSH_STATUS_DELETE
;
328 } else if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
))
329 info
->status
= PUSH_STATUS_UPTODATE
;
330 else if (is_null_sha1(ref
->old_sha1
))
331 info
->status
= PUSH_STATUS_CREATE
;
332 else if (has_sha1_file(ref
->old_sha1
) &&
333 ref_newer(ref
->new_sha1
, ref
->old_sha1
))
334 info
->status
= PUSH_STATUS_FASTFORWARD
;
336 info
->status
= PUSH_STATUS_OUTOFDATE
;
338 free_refs(local_refs
);
343 static int get_push_ref_states_noquery(struct ref_states
*states
)
346 struct remote
*remote
= states
->remote
;
347 struct string_list_item
*item
;
348 struct push_info
*info
;
353 states
->push
.strdup_strings
= 1;
354 if (!remote
->push_refspec_nr
) {
355 item
= string_list_append("(matching)", &states
->push
);
356 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
357 info
->status
= PUSH_STATUS_NOTQUERIED
;
358 info
->dest
= xstrdup(item
->string
);
360 for (i
= 0; i
< remote
->push_refspec_nr
; i
++) {
361 struct refspec
*spec
= remote
->push
+ i
;
363 item
= string_list_append("(matching)", &states
->push
);
364 else if (strlen(spec
->src
))
365 item
= string_list_append(spec
->src
, &states
->push
);
367 item
= string_list_append("(delete)", &states
->push
);
369 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
370 info
->forced
= spec
->force
;
371 info
->status
= PUSH_STATUS_NOTQUERIED
;
372 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
377 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
379 struct ref
*ref
, *matches
;
380 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
381 struct refspec refspec
;
385 refspec
.src
= refspec
.dst
= "refs/heads/*";
386 states
->heads
.strdup_strings
= 1;
387 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
388 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
390 for(ref
= matches
; ref
; ref
= ref
->next
)
391 string_list_append(abbrev_branch(ref
->name
), &states
->heads
);
393 free_refs(fetch_map
);
399 struct known_remote
{
400 struct known_remote
*next
;
401 struct remote
*remote
;
404 struct known_remotes
{
405 struct remote
*to_delete
;
406 struct known_remote
*list
;
409 static int add_known_remote(struct remote
*remote
, void *cb_data
)
411 struct known_remotes
*all
= cb_data
;
412 struct known_remote
*r
;
414 if (!strcmp(all
->to_delete
->name
, remote
->name
))
417 r
= xmalloc(sizeof(*r
));
424 struct branches_for_remote
{
425 struct remote
*remote
;
426 struct string_list
*branches
, *skipped
;
427 struct known_remotes
*keep
;
430 static int add_branch_for_removal(const char *refname
,
431 const unsigned char *sha1
, int flags
, void *cb_data
)
433 struct branches_for_remote
*branches
= cb_data
;
434 struct refspec refspec
;
435 struct string_list_item
*item
;
436 struct known_remote
*kr
;
438 memset(&refspec
, 0, sizeof(refspec
));
439 refspec
.dst
= (char *)refname
;
440 if (remote_find_tracking(branches
->remote
, &refspec
))
443 /* don't delete a branch if another remote also uses it */
444 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
445 memset(&refspec
, 0, sizeof(refspec
));
446 refspec
.dst
= (char *)refname
;
447 if (!remote_find_tracking(kr
->remote
, &refspec
))
451 /* don't delete non-remote refs */
452 if (prefixcmp(refname
, "refs/remotes")) {
453 /* advise user how to delete local branches */
454 if (!prefixcmp(refname
, "refs/heads/"))
455 string_list_append(abbrev_branch(refname
),
457 /* silently skip over other non-remote refs */
461 /* make sure that symrefs are deleted */
462 if (flags
& REF_ISSYMREF
)
463 return unlink(git_path("%s", refname
));
465 item
= string_list_append(refname
, branches
->branches
);
466 item
->util
= xmalloc(20);
467 hashcpy(item
->util
, sha1
);
475 struct string_list
*remote_branches
;
478 static int read_remote_branches(const char *refname
,
479 const unsigned char *sha1
, int flags
, void *cb_data
)
481 struct rename_info
*rename
= cb_data
;
482 struct strbuf buf
= STRBUF_INIT
;
483 struct string_list_item
*item
;
485 unsigned char orig_sha1
[20];
488 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
489 if(!prefixcmp(refname
, buf
.buf
)) {
490 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
491 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
492 if (flag
& REF_ISSYMREF
)
493 item
->util
= xstrdup(symref
);
501 static int migrate_file(struct remote
*remote
)
503 struct strbuf buf
= STRBUF_INIT
;
507 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
508 for (i
= 0; i
< remote
->url_nr
; i
++)
509 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
510 return error("Could not append '%s' to '%s'",
511 remote
->url
[i
], buf
.buf
);
513 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
514 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
515 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
516 return error("Could not append '%s' to '%s'",
517 remote
->push_refspec
[i
], buf
.buf
);
519 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
520 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
521 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
522 return error("Could not append '%s' to '%s'",
523 remote
->fetch_refspec
[i
], buf
.buf
);
524 if (remote
->origin
== REMOTE_REMOTES
)
525 path
= git_path("remotes/%s", remote
->name
);
526 else if (remote
->origin
== REMOTE_BRANCHES
)
527 path
= git_path("branches/%s", remote
->name
);
528 if (path
&& unlink(path
))
529 warning("failed to remove '%s'", path
);
533 static int mv(int argc
, const char **argv
)
535 struct option options
[] = {
538 struct remote
*oldremote
, *newremote
;
539 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
540 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
541 struct rename_info rename
;
545 usage_with_options(builtin_remote_usage
, options
);
547 rename
.old
= argv
[1];
548 rename
.new = argv
[2];
549 rename
.remote_branches
= &remote_branches
;
551 oldremote
= remote_get(rename
.old
);
553 die("No such remote: %s", rename
.old
);
555 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
556 return migrate_file(oldremote
);
558 newremote
= remote_get(rename
.new);
559 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
560 die("remote %s already exists.", rename
.new);
562 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
563 if (!valid_fetch_refspec(buf
.buf
))
564 die("'%s' is not a valid remote name", rename
.new);
567 strbuf_addf(&buf
, "remote.%s", rename
.old
);
568 strbuf_addf(&buf2
, "remote.%s", rename
.new);
569 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
570 return error("Could not rename config section '%s' to '%s'",
574 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
575 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
576 return error("Could not remove config section '%s'", buf
.buf
);
577 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
581 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
582 ptr
= strstr(buf2
.buf
, rename
.old
);
584 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
585 rename
.new, strlen(rename
.new));
586 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
587 return error("Could not append '%s'", buf
.buf
);
591 for (i
= 0; i
< branch_list
.nr
; i
++) {
592 struct string_list_item
*item
= branch_list
.items
+ i
;
593 struct branch_info
*info
= item
->util
;
594 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old
)) {
596 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
597 if (git_config_set(buf
.buf
, rename
.new)) {
598 return error("Could not set '%s'", buf
.buf
);
604 * First remove symrefs, then rename the rest, finally create
607 for_each_ref(read_remote_branches
, &rename
);
608 for (i
= 0; i
< remote_branches
.nr
; i
++) {
609 struct string_list_item
*item
= remote_branches
.items
+ i
;
611 unsigned char sha1
[20];
613 resolve_ref(item
->string
, sha1
, 1, &flag
);
614 if (!(flag
& REF_ISSYMREF
))
616 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
617 die("deleting '%s' failed", item
->string
);
619 for (i
= 0; i
< remote_branches
.nr
; i
++) {
620 struct string_list_item
*item
= remote_branches
.items
+ i
;
625 strbuf_addstr(&buf
, item
->string
);
626 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
627 rename
.new, strlen(rename
.new));
629 strbuf_addf(&buf2
, "remote: renamed %s to %s",
630 item
->string
, buf
.buf
);
631 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
632 die("renaming '%s' failed", item
->string
);
634 for (i
= 0; i
< remote_branches
.nr
; i
++) {
635 struct string_list_item
*item
= remote_branches
.items
+ i
;
640 strbuf_addstr(&buf
, item
->string
);
641 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
642 rename
.new, strlen(rename
.new));
644 strbuf_addstr(&buf2
, item
->util
);
645 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
646 rename
.new, strlen(rename
.new));
648 strbuf_addf(&buf3
, "remote: renamed %s to %s",
649 item
->string
, buf
.buf
);
650 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
651 die("creating '%s' failed", buf
.buf
);
656 static int remove_branches(struct string_list
*branches
)
659 for (i
= 0; i
< branches
->nr
; i
++) {
660 struct string_list_item
*item
= branches
->items
+ i
;
661 const char *refname
= item
->string
;
662 unsigned char *sha1
= item
->util
;
664 if (delete_ref(refname
, sha1
, 0))
665 result
|= error("Could not remove branch %s", refname
);
670 static int rm(int argc
, const char **argv
)
672 struct option options
[] = {
675 struct remote
*remote
;
676 struct strbuf buf
= STRBUF_INIT
;
677 struct known_remotes known_remotes
= { NULL
, NULL
};
678 struct string_list branches
= { NULL
, 0, 0, 1 };
679 struct string_list skipped
= { NULL
, 0, 0, 1 };
680 struct branches_for_remote cb_data
= {
681 NULL
, &branches
, &skipped
, &known_remotes
686 usage_with_options(builtin_remote_usage
, options
);
688 remote
= remote_get(argv
[1]);
690 die("No such remote: %s", argv
[1]);
692 known_remotes
.to_delete
= remote
;
693 for_each_remote(add_known_remote
, &known_remotes
);
695 strbuf_addf(&buf
, "remote.%s", remote
->name
);
696 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
697 return error("Could not remove config section '%s'", buf
.buf
);
700 for (i
= 0; i
< branch_list
.nr
; i
++) {
701 struct string_list_item
*item
= branch_list
.items
+ i
;
702 struct branch_info
*info
= item
->util
;
703 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
704 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
705 for (k
= keys
; *k
; k
++) {
707 strbuf_addf(&buf
, "branch.%s.%s",
709 if (git_config_set(buf
.buf
, NULL
)) {
710 strbuf_release(&buf
);
718 * We cannot just pass a function to for_each_ref() which deletes
719 * the branches one by one, since for_each_ref() relies on cached
720 * refs, which are invalidated when deleting a branch.
722 cb_data
.remote
= remote
;
723 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
724 strbuf_release(&buf
);
727 result
= remove_branches(&branches
);
728 string_list_clear(&branches
, 1);
731 fprintf(stderr
, skipped
.nr
== 1 ?
732 "Note: A non-remote branch was not removed; "
733 "to delete it, use:\n" :
734 "Note: Non-remote branches were not removed; "
735 "to delete them, use:\n");
736 for (i
= 0; i
< skipped
.nr
; i
++)
737 fprintf(stderr
, " git branch -d %s\n",
738 skipped
.items
[i
].string
);
740 string_list_clear(&skipped
, 0);
745 void clear_push_info(void *util
, const char *string
)
747 struct push_info
*info
= util
;
752 static void free_remote_ref_states(struct ref_states
*states
)
754 string_list_clear(&states
->new, 0);
755 string_list_clear(&states
->stale
, 0);
756 string_list_clear(&states
->tracked
, 0);
757 string_list_clear(&states
->heads
, 0);
758 string_list_clear_func(&states
->push
, clear_push_info
);
761 static int append_ref_to_tracked_list(const char *refname
,
762 const unsigned char *sha1
, int flags
, void *cb_data
)
764 struct ref_states
*states
= cb_data
;
765 struct refspec refspec
;
767 if (flags
& REF_ISSYMREF
)
770 memset(&refspec
, 0, sizeof(refspec
));
771 refspec
.dst
= (char *)refname
;
772 if (!remote_find_tracking(states
->remote
, &refspec
))
773 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
778 static int get_remote_ref_states(const char *name
,
779 struct ref_states
*states
,
782 struct transport
*transport
;
783 const struct ref
*remote_refs
;
785 states
->remote
= remote_get(name
);
787 return error("No such remote: %s", name
);
792 transport
= transport_get(NULL
, states
->remote
->url_nr
> 0 ?
793 states
->remote
->url
[0] : NULL
);
794 remote_refs
= transport_get_remote_refs(transport
);
795 transport_disconnect(transport
);
798 if (query
& GET_REF_STATES
)
799 get_ref_states(remote_refs
, states
);
800 if (query
& GET_HEAD_NAMES
)
801 get_head_names(remote_refs
, states
);
802 if (query
& GET_PUSH_REF_STATES
)
803 get_push_ref_states(remote_refs
, states
);
805 for_each_ref(append_ref_to_tracked_list
, states
);
806 sort_string_list(&states
->tracked
);
807 get_push_ref_states_noquery(states
);
814 struct string_list
*list
;
815 struct ref_states
*states
;
820 int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
822 struct show_info
*info
= cb_data
;
823 int n
= strlen(item
->string
);
826 string_list_insert(item
->string
, info
->list
);
830 int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
832 struct show_info
*info
= cb_data
;
833 struct ref_states
*states
= info
->states
;
834 const char *name
= item
->string
;
836 if (states
->queried
) {
837 const char *fmt
= "%s";
838 const char *arg
= "";
839 if (string_list_has_string(&states
->new, name
)) {
840 fmt
= " new (next fetch will store in remotes/%s)";
841 arg
= states
->remote
->name
;
842 } else if (string_list_has_string(&states
->tracked
, name
))
844 else if (string_list_has_string(&states
->stale
, name
))
845 arg
= " stale (use 'git remote prune' to remove)";
848 printf(" %-*s", info
->width
, name
);
852 printf(" %s\n", name
);
857 int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
859 struct show_info
*show_info
= cb_data
;
860 struct ref_states
*states
= show_info
->states
;
861 struct branch_info
*branch_info
= branch_item
->util
;
862 struct string_list_item
*item
;
865 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
866 strcmp(states
->remote
->name
, branch_info
->remote_name
))
868 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
869 show_info
->width
= n
;
870 if (branch_info
->rebase
)
871 show_info
->any_rebase
= 1;
873 item
= string_list_insert(branch_item
->string
, show_info
->list
);
874 item
->util
= branch_info
;
879 int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
881 struct show_info
*show_info
= cb_data
;
882 struct branch_info
*branch_info
= item
->util
;
883 struct string_list
*merge
= &branch_info
->merge
;
887 if (branch_info
->rebase
&& branch_info
->merge
.nr
> 1) {
888 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
893 printf(" %-*s ", show_info
->width
, item
->string
);
894 if (branch_info
->rebase
) {
895 printf("rebases onto remote %s\n", merge
->items
[0].string
);
897 } else if (show_info
->any_rebase
) {
898 printf(" merges with remote %s\n", merge
->items
[0].string
);
899 also
= " and with remote";
901 printf("merges with remote %s\n", merge
->items
[0].string
);
902 also
= " and with remote";
904 for (i
= 1; i
< merge
->nr
; i
++)
905 printf(" %-*s %s %s\n", show_info
->width
, "", also
,
906 merge
->items
[i
].string
);
911 int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
913 struct show_info
*show_info
= cb_data
;
914 struct push_info
*push_info
= push_item
->util
;
915 struct string_list_item
*item
;
917 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
918 show_info
->width
= n
;
919 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
920 show_info
->width2
= n
;
921 item
= string_list_append(push_item
->string
, show_info
->list
);
922 item
->util
= push_item
->util
;
927 * Sorting comparison for a string list that has push_info
928 * structs in its util field
930 static int cmp_string_with_push(const void *va
, const void *vb
)
932 const struct string_list_item
*a
= va
;
933 const struct string_list_item
*b
= vb
;
934 const struct push_info
*a_push
= a
->util
;
935 const struct push_info
*b_push
= b
->util
;
936 int cmp
= strcmp(a
->string
, b
->string
);
937 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
940 int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
942 struct show_info
*show_info
= cb_data
;
943 struct push_info
*push_info
= item
->util
;
944 char *src
= item
->string
, *status
= NULL
;
946 switch (push_info
->status
) {
947 case PUSH_STATUS_CREATE
:
950 case PUSH_STATUS_DELETE
:
954 case PUSH_STATUS_UPTODATE
:
955 status
= "up to date";
957 case PUSH_STATUS_FASTFORWARD
:
958 status
= "fast forwardable";
960 case PUSH_STATUS_OUTOFDATE
:
961 status
= "local out of date";
963 case PUSH_STATUS_NOTQUERIED
:
967 printf(" %-*s %s to %-*s (%s)\n", show_info
->width
, src
,
968 push_info
->forced
? "forces" : "pushes",
969 show_info
->width2
, push_info
->dest
, status
);
971 printf(" %-*s %s to %s\n", show_info
->width
, src
,
972 push_info
->forced
? "forces" : "pushes",
977 static int show(int argc
, const char **argv
)
979 int no_query
= 0, result
= 0, query_flag
= 0;
980 struct option options
[] = {
981 OPT_GROUP("show specific options"),
982 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
985 struct ref_states states
;
986 struct string_list info_list
= { NULL
, 0, 0, 0 };
987 struct show_info info
;
989 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
995 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
997 memset(&states
, 0, sizeof(states
));
998 memset(&info
, 0, sizeof(info
));
999 info
.states
= &states
;
1000 info
.list
= &info_list
;
1001 for (; argc
; argc
--, argv
++) {
1004 get_remote_ref_states(*argv
, &states
, query_flag
);
1006 printf("* remote %s\n URL: %s\n", *argv
,
1007 states
.remote
->url_nr
> 0 ?
1008 states
.remote
->url
[0] : "(no URL)");
1010 printf(" HEAD branch: (not queried)\n");
1011 else if (!states
.heads
.nr
)
1012 printf(" HEAD branch: (unknown)\n");
1013 else if (states
.heads
.nr
== 1)
1014 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
1016 printf(" HEAD branch (remote HEAD is ambiguous,"
1017 " may be one of the following):\n");
1018 for (i
= 0; i
< states
.heads
.nr
; i
++)
1019 printf(" %s\n", states
.heads
.items
[i
].string
);
1022 /* remote branch info */
1024 for_each_string_list(add_remote_to_show_info
, &states
.new, &info
);
1025 for_each_string_list(add_remote_to_show_info
, &states
.tracked
, &info
);
1026 for_each_string_list(add_remote_to_show_info
, &states
.stale
, &info
);
1028 printf(" Remote branch%s:%s\n",
1029 info
.list
->nr
> 1 ? "es" : "",
1030 no_query
? " (status not queried)" : "");
1031 for_each_string_list(show_remote_info_item
, info
.list
, &info
);
1032 string_list_clear(info
.list
, 0);
1036 info
.any_rebase
= 0;
1037 for_each_string_list(add_local_to_show_info
, &branch_list
, &info
);
1039 printf(" Local branch%s configured for 'git pull':\n",
1040 info
.list
->nr
> 1 ? "es" : "");
1041 for_each_string_list(show_local_info_item
, info
.list
, &info
);
1042 string_list_clear(info
.list
, 0);
1045 if (states
.remote
->mirror
)
1046 printf(" Local refs will be mirrored by 'git push'\n");
1048 info
.width
= info
.width2
= 0;
1049 for_each_string_list(add_push_to_show_info
, &states
.push
, &info
);
1050 qsort(info
.list
->items
, info
.list
->nr
,
1051 sizeof(*info
.list
->items
), cmp_string_with_push
);
1053 printf(" Local ref%s configured for 'git push'%s:\n",
1054 info
.list
->nr
> 1 ? "s" : "",
1055 no_query
? " (status not queried)" : "");
1056 for_each_string_list(show_push_info_item
, info
.list
, &info
);
1057 string_list_clear(info
.list
, 0);
1059 free_remote_ref_states(&states
);
1065 static int set_head(int argc
, const char **argv
)
1067 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1068 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1069 char *head_name
= NULL
;
1071 struct option options
[] = {
1072 OPT_GROUP("set-head specific options"),
1073 OPT_BOOLEAN('a', "auto", &opt_a
,
1074 "set refs/remotes/<name>/HEAD according to remote"),
1075 OPT_BOOLEAN('d', "delete", &opt_d
,
1076 "delete refs/remotes/<name>/HEAD"),
1079 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
1081 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1083 if (!opt_a
&& !opt_d
&& argc
== 2) {
1084 head_name
= xstrdup(argv
[1]);
1085 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1086 struct ref_states states
;
1087 memset(&states
, 0, sizeof(states
));
1088 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1089 if (!states
.heads
.nr
)
1090 result
|= error("Cannot determine remote HEAD");
1091 else if (states
.heads
.nr
> 1) {
1092 result
|= error("Multiple remote HEAD branches. "
1093 "Please choose one explicitly with:");
1094 for (i
= 0; i
< states
.heads
.nr
; i
++)
1095 fprintf(stderr
, " git remote set-head %s %s\n",
1096 argv
[0], states
.heads
.items
[i
].string
);
1098 head_name
= xstrdup(states
.heads
.items
[0].string
);
1099 free_remote_ref_states(&states
);
1100 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1101 if (delete_ref(buf
.buf
, NULL
, REF_NODEREF
))
1102 result
|= error("Could not delete %s", buf
.buf
);
1104 usage_with_options(builtin_remote_usage
, options
);
1107 unsigned char sha1
[20];
1108 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1109 /* make sure it's valid */
1110 if (!resolve_ref(buf2
.buf
, sha1
, 1, NULL
))
1111 result
|= error("Not a valid ref: %s", buf2
.buf
);
1112 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1113 result
|= error("Could not setup %s", buf
.buf
);
1115 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1119 strbuf_release(&buf
);
1120 strbuf_release(&buf2
);
1124 static int prune(int argc
, const char **argv
)
1126 int dry_run
= 0, result
= 0;
1127 struct option options
[] = {
1128 OPT_GROUP("prune specific options"),
1129 OPT__DRY_RUN(&dry_run
),
1133 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
1136 usage_with_options(builtin_remote_usage
, options
);
1138 for (; argc
; argc
--, argv
++)
1139 result
|= prune_remote(*argv
, dry_run
);
1144 static int prune_remote(const char *remote
, int dry_run
)
1147 struct ref_states states
;
1148 const char *dangling_msg
= dry_run
1149 ? " %s will become dangling!\n"
1150 : " %s has become dangling!\n";
1152 memset(&states
, 0, sizeof(states
));
1153 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1155 if (states
.stale
.nr
) {
1156 printf("Pruning %s\n", remote
);
1158 states
.remote
->url_nr
1159 ? states
.remote
->url
[0]
1163 for (i
= 0; i
< states
.stale
.nr
; i
++) {
1164 const char *refname
= states
.stale
.items
[i
].util
;
1167 result
|= delete_ref(refname
, NULL
, 0);
1169 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
1170 abbrev_ref(refname
, "refs/remotes/"));
1171 warn_dangling_symref(dangling_msg
, refname
);
1174 free_remote_ref_states(&states
);
1178 static int get_one_remote_for_update(struct remote
*remote
, void *priv
)
1180 struct string_list
*list
= priv
;
1181 if (!remote
->skip_default_update
)
1182 string_list_append(remote
->name
, list
);
1186 struct remote_group
{
1188 struct string_list
*list
;
1191 static int get_remote_group(const char *key
, const char *value
, void *num_hits
)
1193 if (!prefixcmp(key
, "remotes.") &&
1194 !strcmp(key
+ 8, remote_group
.name
)) {
1195 /* split list by white space */
1196 int space
= strcspn(value
, " \t\n");
1199 string_list_append(xstrndup(value
, space
),
1201 ++*((int *)num_hits
);
1203 value
+= space
+ (value
[space
] != '\0');
1204 space
= strcspn(value
, " \t\n");
1211 static int update(int argc
, const char **argv
)
1213 int i
, result
= 0, prune
= 0;
1214 struct string_list list
= { NULL
, 0, 0, 0 };
1215 static const char *default_argv
[] = { NULL
, "default", NULL
};
1216 struct option options
[] = {
1217 OPT_GROUP("update specific options"),
1218 OPT_BOOLEAN('p', "prune", &prune
,
1219 "prune remotes after fecthing"),
1223 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
1224 PARSE_OPT_KEEP_ARGV0
);
1227 argv
= default_argv
;
1230 remote_group
.list
= &list
;
1231 for (i
= 1; i
< argc
; i
++) {
1232 int groups_found
= 0;
1233 remote_group
.name
= argv
[i
];
1234 result
= git_config(get_remote_group
, &groups_found
);
1235 if (!groups_found
&& (i
!= 1 || strcmp(argv
[1], "default"))) {
1236 struct remote
*remote
;
1237 if (!remote_is_configured(argv
[i
]))
1238 die("No such remote or remote group: %s",
1240 remote
= remote_get(argv
[i
]);
1241 string_list_append(remote
->name
, remote_group
.list
);
1245 if (!result
&& !list
.nr
&& argc
== 2 && !strcmp(argv
[1], "default"))
1246 result
= for_each_remote(get_one_remote_for_update
, &list
);
1248 for (i
= 0; i
< list
.nr
; i
++) {
1249 int err
= fetch_remote(list
.items
[i
].string
);
1252 result
|= prune_remote(list
.items
[i
].string
, 0);
1255 /* all names were strdup()ed or strndup()ed */
1256 list
.strdup_strings
= 1;
1257 string_list_clear(&list
, 0);
1262 static int get_one_entry(struct remote
*remote
, void *priv
)
1264 struct string_list
*list
= priv
;
1266 if (remote
->url_nr
> 0) {
1269 for (i
= 0; i
< remote
->url_nr
; i
++)
1270 string_list_append(remote
->name
, list
)->util
= (void *)remote
->url
[i
];
1272 string_list_append(remote
->name
, list
)->util
= NULL
;
1277 static int show_all(void)
1279 struct string_list list
= { NULL
, 0, 0 };
1280 int result
= for_each_remote(get_one_entry
, &list
);
1285 sort_string_list(&list
);
1286 for (i
= 0; i
< list
.nr
; i
++) {
1287 struct string_list_item
*item
= list
.items
+ i
;
1289 printf("%s\t%s\n", item
->string
,
1290 item
->util
? (const char *)item
->util
: "");
1292 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1294 printf("%s\n", item
->string
);
1301 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1303 struct option options
[] = {
1304 OPT__VERBOSE(&verbose
),
1309 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
1310 PARSE_OPT_STOP_AT_NON_OPTION
);
1313 result
= show_all();
1314 else if (!strcmp(argv
[0], "add"))
1315 result
= add(argc
, argv
);
1316 else if (!strcmp(argv
[0], "rename"))
1317 result
= mv(argc
, argv
);
1318 else if (!strcmp(argv
[0], "rm"))
1319 result
= rm(argc
, argv
);
1320 else if (!strcmp(argv
[0], "set-head"))
1321 result
= set_head(argc
, argv
);
1322 else if (!strcmp(argv
[0], "show"))
1323 result
= show(argc
, argv
);
1324 else if (!strcmp(argv
[0], "prune"))
1325 result
= prune(argc
, argv
);
1326 else if (!strcmp(argv
[0], "update"))
1327 result
= update(argc
, argv
);
1329 error("Unknown subcommand: %s", argv
[0]);
1330 usage_with_options(builtin_remote_usage
, options
);
1333 return result
? 1 : 0;