2 #include "parse-options.h"
5 #include "string-list.h"
7 #include "run-command.h"
10 static const char * const builtin_remote_usage
[] = {
11 "git remote [-v | --verbose]",
12 "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
13 "git remote rename <old> <new>",
14 "git remote rm <name>",
15 "git remote set-head <name> [-a | -d | <branch>]",
16 "git remote show [-n] <name>",
17 "git remote prune [-n | --dry-run] <name>",
18 "git remote [-v | --verbose] update [group]",
22 #define GET_REF_STATES (1<<0)
23 #define GET_HEAD_NAMES (1<<1)
24 #define GET_PUSH_REF_STATES (1<<2)
28 static int show_all(void);
30 static inline int postfixcmp(const char *string
, const char *postfix
)
32 int len1
= strlen(string
), len2
= strlen(postfix
);
35 return strcmp(string
+ len1
- len2
, postfix
);
38 static int opt_parse_track(const struct option
*opt
, const char *arg
, int not)
40 struct string_list
*list
= opt
->value
;
42 string_list_clear(list
, 0);
44 string_list_append(arg
, list
);
48 static int fetch_remote(const char *name
)
50 const char *argv
[] = { "fetch", name
, NULL
, NULL
};
55 printf("Updating %s\n", name
);
56 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
57 return error("Could not fetch %s", name
);
61 static int add(int argc
, const char **argv
)
63 int fetch
= 0, mirror
= 0;
64 struct string_list track
= { NULL
, 0, 0 };
65 const char *master
= NULL
;
66 struct remote
*remote
;
67 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
68 const char *name
, *url
;
71 struct option options
[] = {
72 OPT_GROUP("add specific options"),
73 OPT_BOOLEAN('f', "fetch", &fetch
, "fetch the remote branches"),
74 OPT_CALLBACK('t', "track", &track
, "branch",
75 "branch(es) to track", opt_parse_track
),
76 OPT_STRING('m', "master", &master
, "branch", "master branch"),
77 OPT_BOOLEAN(0, "mirror", &mirror
, "no separate remotes"),
81 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
84 usage_with_options(builtin_remote_usage
, options
);
89 remote
= remote_get(name
);
90 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
91 remote
->fetch_refspec_nr
))
92 die("remote %s already exists.", name
);
94 strbuf_addf(&buf2
, "refs/heads/test:refs/remotes/%s/test", name
);
95 if (!valid_fetch_refspec(buf2
.buf
))
96 die("'%s' is not a valid remote name", name
);
98 strbuf_addf(&buf
, "remote.%s.url", name
);
99 if (git_config_set(buf
.buf
, url
))
103 strbuf_addf(&buf
, "remote.%s.fetch", name
);
106 string_list_append("*", &track
);
107 for (i
= 0; i
< track
.nr
; i
++) {
108 struct string_list_item
*item
= track
.items
+ i
;
111 strbuf_addch(&buf2
, '+');
113 strbuf_addf(&buf2
, "refs/%s:refs/%s",
114 item
->string
, item
->string
);
116 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
117 item
->string
, name
, item
->string
);
118 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
124 strbuf_addf(&buf
, "remote.%s.mirror", name
);
125 if (git_config_set(buf
.buf
, "true"))
129 if (fetch
&& fetch_remote(name
))
134 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
137 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
139 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
140 return error("Could not setup master '%s'", master
);
143 strbuf_release(&buf
);
144 strbuf_release(&buf2
);
145 string_list_clear(&track
, 0);
152 struct string_list merge
;
156 static struct string_list branch_list
;
158 static const char *abbrev_ref(const char *name
, const char *prefix
)
160 const char *abbrev
= skip_prefix(name
, prefix
);
165 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
167 static int config_read_branches(const char *key
, const char *value
, void *cb
)
169 if (!prefixcmp(key
, "branch.")) {
170 const char *orig_key
= key
;
172 struct string_list_item
*item
;
173 struct branch_info
*info
;
174 enum { REMOTE
, MERGE
, REBASE
} type
;
177 if (!postfixcmp(key
, ".remote")) {
178 name
= xstrndup(key
, strlen(key
) - 7);
180 } else if (!postfixcmp(key
, ".merge")) {
181 name
= xstrndup(key
, strlen(key
) - 6);
183 } else if (!postfixcmp(key
, ".rebase")) {
184 name
= xstrndup(key
, strlen(key
) - 7);
189 item
= string_list_insert(name
, &branch_list
);
192 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
194 if (type
== REMOTE
) {
195 if (info
->remote_name
)
196 warning("more than one %s", orig_key
);
197 info
->remote_name
= xstrdup(value
);
198 } else if (type
== MERGE
) {
199 char *space
= strchr(value
, ' ');
200 value
= abbrev_branch(value
);
203 merge
= xstrndup(value
, space
- value
);
204 string_list_append(merge
, &info
->merge
);
205 value
= abbrev_branch(space
+ 1);
206 space
= strchr(value
, ' ');
208 string_list_append(xstrdup(value
), &info
->merge
);
210 info
->rebase
= git_config_bool(orig_key
, value
);
215 static void read_branches(void)
219 git_config(config_read_branches
, NULL
);
223 struct remote
*remote
;
224 struct string_list
new, stale
, tracked
, heads
, push
;
228 static int handle_one_branch(const char *refname
,
229 const unsigned char *sha1
, int flags
, void *cb_data
)
231 struct ref_states
*states
= cb_data
;
232 struct refspec refspec
;
234 memset(&refspec
, 0, sizeof(refspec
));
235 refspec
.dst
= (char *)refname
;
236 if (!remote_find_tracking(states
->remote
, &refspec
)) {
237 struct string_list_item
*item
;
238 const char *name
= abbrev_branch(refspec
.src
);
239 /* symbolic refs pointing nowhere were handled already */
240 if ((flags
& REF_ISSYMREF
) ||
241 string_list_has_string(&states
->tracked
, name
) ||
242 string_list_has_string(&states
->new, name
))
244 item
= string_list_append(name
, &states
->stale
);
245 item
->util
= xstrdup(refname
);
250 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
252 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
256 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
257 if (get_fetch_map(remote_refs
, states
->remote
->fetch
+ i
, &tail
, 1))
258 die("Could not get fetch map for refspec %s",
259 states
->remote
->fetch_refspec
[i
]);
261 states
->new.strdup_strings
= states
->tracked
.strdup_strings
= 1;
262 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
263 unsigned char sha1
[20];
264 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
265 string_list_append(abbrev_branch(ref
->name
), &states
->new);
267 string_list_append(abbrev_branch(ref
->name
), &states
->tracked
);
269 free_refs(fetch_map
);
271 sort_string_list(&states
->new);
272 sort_string_list(&states
->tracked
);
273 for_each_ref(handle_one_branch
, states
);
274 sort_string_list(&states
->stale
);
283 PUSH_STATUS_CREATE
= 0,
285 PUSH_STATUS_UPTODATE
,
286 PUSH_STATUS_FASTFORWARD
,
287 PUSH_STATUS_OUTOFDATE
,
288 PUSH_STATUS_NOTQUERIED
,
292 static int get_push_ref_states(const struct ref
*remote_refs
,
293 struct ref_states
*states
)
295 struct remote
*remote
= states
->remote
;
296 struct ref
*ref
, *local_refs
, *push_map
, **push_tail
;
300 local_refs
= get_local_heads();
301 ref
= push_map
= copy_ref_list(remote_refs
);
304 push_tail
= &ref
->next
;
306 match_refs(local_refs
, push_map
, &push_tail
, remote
->push_refspec_nr
,
307 remote
->push_refspec
, MATCH_REFS_NONE
);
309 states
->push
.strdup_strings
= 1;
310 for (ref
= push_map
; ref
; ref
= ref
->next
) {
311 struct string_list_item
*item
;
312 struct push_info
*info
;
316 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
318 item
= string_list_append(abbrev_branch(ref
->peer_ref
->name
),
320 item
->util
= xcalloc(sizeof(struct push_info
), 1);
322 info
->forced
= ref
->force
;
323 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
325 if (is_null_sha1(ref
->new_sha1
)) {
326 info
->status
= PUSH_STATUS_DELETE
;
327 } else if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
))
328 info
->status
= PUSH_STATUS_UPTODATE
;
329 else if (is_null_sha1(ref
->old_sha1
))
330 info
->status
= PUSH_STATUS_CREATE
;
331 else if (has_sha1_file(ref
->old_sha1
) &&
332 ref_newer(ref
->new_sha1
, ref
->old_sha1
))
333 info
->status
= PUSH_STATUS_FASTFORWARD
;
335 info
->status
= PUSH_STATUS_OUTOFDATE
;
337 free_refs(local_refs
);
342 static int get_push_ref_states_noquery(struct ref_states
*states
)
345 struct remote
*remote
= states
->remote
;
346 struct string_list_item
*item
;
347 struct push_info
*info
;
352 states
->push
.strdup_strings
= 1;
353 if (!remote
->push_refspec_nr
) {
354 item
= string_list_append("(matching)", &states
->push
);
355 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
356 info
->status
= PUSH_STATUS_NOTQUERIED
;
357 info
->dest
= xstrdup(item
->string
);
359 for (i
= 0; i
< remote
->push_refspec_nr
; i
++) {
360 struct refspec
*spec
= remote
->push
+ i
;
363 item
= string_list_append("(matching)", &states
->push
);
364 else if (spec
->pattern
) {
365 snprintf(buf
, (sizeof(buf
)), "%s*", spec
->src
);
366 item
= string_list_append(buf
, &states
->push
);
367 snprintf(buf
, (sizeof(buf
)), "%s*", spec
->dst
);
368 } else if (strlen(spec
->src
))
369 item
= string_list_append(spec
->src
, &states
->push
);
371 item
= string_list_append("(delete)", &states
->push
);
373 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
374 info
->forced
= spec
->force
;
375 info
->status
= PUSH_STATUS_NOTQUERIED
;
377 info
->dest
= xstrdup(buf
);
379 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
384 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
386 struct ref
*ref
, *matches
;
387 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
388 struct refspec refspec
;
392 refspec
.src
= refspec
.dst
= "refs/heads/";
393 states
->heads
.strdup_strings
= 1;
394 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
395 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
397 for(ref
= matches
; ref
; ref
= ref
->next
)
398 string_list_append(abbrev_branch(ref
->name
), &states
->heads
);
400 free_refs(fetch_map
);
406 struct known_remote
{
407 struct known_remote
*next
;
408 struct remote
*remote
;
411 struct known_remotes
{
412 struct remote
*to_delete
;
413 struct known_remote
*list
;
416 static int add_known_remote(struct remote
*remote
, void *cb_data
)
418 struct known_remotes
*all
= cb_data
;
419 struct known_remote
*r
;
421 if (!strcmp(all
->to_delete
->name
, remote
->name
))
424 r
= xmalloc(sizeof(*r
));
431 struct branches_for_remote
{
432 struct remote
*remote
;
433 struct string_list
*branches
, *skipped
;
434 struct known_remotes
*keep
;
437 static int add_branch_for_removal(const char *refname
,
438 const unsigned char *sha1
, int flags
, void *cb_data
)
440 struct branches_for_remote
*branches
= cb_data
;
441 struct refspec refspec
;
442 struct string_list_item
*item
;
443 struct known_remote
*kr
;
445 memset(&refspec
, 0, sizeof(refspec
));
446 refspec
.dst
= (char *)refname
;
447 if (remote_find_tracking(branches
->remote
, &refspec
))
450 /* don't delete a branch if another remote also uses it */
451 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
452 memset(&refspec
, 0, sizeof(refspec
));
453 refspec
.dst
= (char *)refname
;
454 if (!remote_find_tracking(kr
->remote
, &refspec
))
458 /* don't delete non-remote refs */
459 if (prefixcmp(refname
, "refs/remotes")) {
460 /* advise user how to delete local branches */
461 if (!prefixcmp(refname
, "refs/heads/"))
462 string_list_append(abbrev_branch(refname
),
464 /* silently skip over other non-remote refs */
468 /* make sure that symrefs are deleted */
469 if (flags
& REF_ISSYMREF
)
470 return unlink(git_path("%s", refname
));
472 item
= string_list_append(refname
, branches
->branches
);
473 item
->util
= xmalloc(20);
474 hashcpy(item
->util
, sha1
);
482 struct string_list
*remote_branches
;
485 static int read_remote_branches(const char *refname
,
486 const unsigned char *sha1
, int flags
, void *cb_data
)
488 struct rename_info
*rename
= cb_data
;
489 struct strbuf buf
= STRBUF_INIT
;
490 struct string_list_item
*item
;
492 unsigned char orig_sha1
[20];
495 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
496 if(!prefixcmp(refname
, buf
.buf
)) {
497 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
498 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
499 if (flag
& REF_ISSYMREF
)
500 item
->util
= xstrdup(symref
);
508 static int migrate_file(struct remote
*remote
)
510 struct strbuf buf
= STRBUF_INIT
;
514 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
515 for (i
= 0; i
< remote
->url_nr
; i
++)
516 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
517 return error("Could not append '%s' to '%s'",
518 remote
->url
[i
], buf
.buf
);
520 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
521 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
522 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
523 return error("Could not append '%s' to '%s'",
524 remote
->push_refspec
[i
], buf
.buf
);
526 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
527 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
528 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
529 return error("Could not append '%s' to '%s'",
530 remote
->fetch_refspec
[i
], buf
.buf
);
531 if (remote
->origin
== REMOTE_REMOTES
)
532 path
= git_path("remotes/%s", remote
->name
);
533 else if (remote
->origin
== REMOTE_BRANCHES
)
534 path
= git_path("branches/%s", remote
->name
);
535 if (path
&& unlink(path
))
536 warning("failed to remove '%s'", path
);
540 static int mv(int argc
, const char **argv
)
542 struct option options
[] = {
545 struct remote
*oldremote
, *newremote
;
546 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
547 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
548 struct rename_info rename
;
552 usage_with_options(builtin_remote_usage
, options
);
554 rename
.old
= argv
[1];
555 rename
.new = argv
[2];
556 rename
.remote_branches
= &remote_branches
;
558 oldremote
= remote_get(rename
.old
);
560 die("No such remote: %s", rename
.old
);
562 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
563 return migrate_file(oldremote
);
565 newremote
= remote_get(rename
.new);
566 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
567 die("remote %s already exists.", rename
.new);
569 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
570 if (!valid_fetch_refspec(buf
.buf
))
571 die("'%s' is not a valid remote name", rename
.new);
574 strbuf_addf(&buf
, "remote.%s", rename
.old
);
575 strbuf_addf(&buf2
, "remote.%s", rename
.new);
576 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
577 return error("Could not rename config section '%s' to '%s'",
581 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
582 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
583 return error("Could not remove config section '%s'", buf
.buf
);
584 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
588 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
589 ptr
= strstr(buf2
.buf
, rename
.old
);
591 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
592 rename
.new, strlen(rename
.new));
593 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
594 return error("Could not append '%s'", buf
.buf
);
598 for (i
= 0; i
< branch_list
.nr
; i
++) {
599 struct string_list_item
*item
= branch_list
.items
+ i
;
600 struct branch_info
*info
= item
->util
;
601 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old
)) {
603 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
604 if (git_config_set(buf
.buf
, rename
.new)) {
605 return error("Could not set '%s'", buf
.buf
);
611 * First remove symrefs, then rename the rest, finally create
614 for_each_ref(read_remote_branches
, &rename
);
615 for (i
= 0; i
< remote_branches
.nr
; i
++) {
616 struct string_list_item
*item
= remote_branches
.items
+ i
;
618 unsigned char sha1
[20];
621 symref
= resolve_ref(item
->string
, sha1
, 1, &flag
);
622 if (!(flag
& REF_ISSYMREF
))
624 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
625 die("deleting '%s' failed", item
->string
);
627 for (i
= 0; i
< remote_branches
.nr
; i
++) {
628 struct string_list_item
*item
= remote_branches
.items
+ i
;
633 strbuf_addstr(&buf
, item
->string
);
634 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
635 rename
.new, strlen(rename
.new));
637 strbuf_addf(&buf2
, "remote: renamed %s to %s",
638 item
->string
, buf
.buf
);
639 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
640 die("renaming '%s' failed", item
->string
);
642 for (i
= 0; i
< remote_branches
.nr
; i
++) {
643 struct string_list_item
*item
= remote_branches
.items
+ i
;
648 strbuf_addstr(&buf
, item
->string
);
649 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
650 rename
.new, strlen(rename
.new));
652 strbuf_addstr(&buf2
, item
->util
);
653 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
654 rename
.new, strlen(rename
.new));
656 strbuf_addf(&buf3
, "remote: renamed %s to %s",
657 item
->string
, buf
.buf
);
658 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
659 die("creating '%s' failed", buf
.buf
);
664 static int remove_branches(struct string_list
*branches
)
667 for (i
= 0; i
< branches
->nr
; i
++) {
668 struct string_list_item
*item
= branches
->items
+ i
;
669 const char *refname
= item
->string
;
670 unsigned char *sha1
= item
->util
;
672 if (delete_ref(refname
, sha1
, 0))
673 result
|= error("Could not remove branch %s", refname
);
678 static int rm(int argc
, const char **argv
)
680 struct option options
[] = {
683 struct remote
*remote
;
684 struct strbuf buf
= STRBUF_INIT
;
685 struct known_remotes known_remotes
= { NULL
, NULL
};
686 struct string_list branches
= { NULL
, 0, 0, 1 };
687 struct string_list skipped
= { NULL
, 0, 0, 1 };
688 struct branches_for_remote cb_data
= {
689 NULL
, &branches
, &skipped
, &known_remotes
694 usage_with_options(builtin_remote_usage
, options
);
696 remote
= remote_get(argv
[1]);
698 die("No such remote: %s", argv
[1]);
700 known_remotes
.to_delete
= remote
;
701 for_each_remote(add_known_remote
, &known_remotes
);
703 strbuf_addf(&buf
, "remote.%s", remote
->name
);
704 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
705 return error("Could not remove config section '%s'", buf
.buf
);
708 for (i
= 0; i
< branch_list
.nr
; i
++) {
709 struct string_list_item
*item
= branch_list
.items
+ i
;
710 struct branch_info
*info
= item
->util
;
711 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
712 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
713 for (k
= keys
; *k
; k
++) {
715 strbuf_addf(&buf
, "branch.%s.%s",
717 if (git_config_set(buf
.buf
, NULL
)) {
718 strbuf_release(&buf
);
726 * We cannot just pass a function to for_each_ref() which deletes
727 * the branches one by one, since for_each_ref() relies on cached
728 * refs, which are invalidated when deleting a branch.
730 cb_data
.remote
= remote
;
731 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
732 strbuf_release(&buf
);
735 result
= remove_branches(&branches
);
736 string_list_clear(&branches
, 1);
739 fprintf(stderr
, skipped
.nr
== 1 ?
740 "Note: A non-remote branch was not removed; "
741 "to delete it, use:\n" :
742 "Note: Non-remote branches were not removed; "
743 "to delete them, use:\n");
744 for (i
= 0; i
< skipped
.nr
; i
++)
745 fprintf(stderr
, " git branch -d %s\n",
746 skipped
.items
[i
].string
);
748 string_list_clear(&skipped
, 0);
753 void clear_push_info(void *util
, const char *string
)
755 struct push_info
*info
= util
;
760 static void free_remote_ref_states(struct ref_states
*states
)
762 string_list_clear(&states
->new, 0);
763 string_list_clear(&states
->stale
, 0);
764 string_list_clear(&states
->tracked
, 0);
765 string_list_clear(&states
->heads
, 0);
766 string_list_clear_func(&states
->push
, clear_push_info
);
769 static int append_ref_to_tracked_list(const char *refname
,
770 const unsigned char *sha1
, int flags
, void *cb_data
)
772 struct ref_states
*states
= cb_data
;
773 struct refspec refspec
;
775 if (flags
& REF_ISSYMREF
)
778 memset(&refspec
, 0, sizeof(refspec
));
779 refspec
.dst
= (char *)refname
;
780 if (!remote_find_tracking(states
->remote
, &refspec
))
781 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
786 static int get_remote_ref_states(const char *name
,
787 struct ref_states
*states
,
790 struct transport
*transport
;
791 const struct ref
*remote_refs
;
793 states
->remote
= remote_get(name
);
795 return error("No such remote: %s", name
);
800 transport
= transport_get(NULL
, states
->remote
->url_nr
> 0 ?
801 states
->remote
->url
[0] : NULL
);
802 remote_refs
= transport_get_remote_refs(transport
);
803 transport_disconnect(transport
);
806 if (query
& GET_REF_STATES
)
807 get_ref_states(remote_refs
, states
);
808 if (query
& GET_HEAD_NAMES
)
809 get_head_names(remote_refs
, states
);
810 if (query
& GET_PUSH_REF_STATES
)
811 get_push_ref_states(remote_refs
, states
);
813 for_each_ref(append_ref_to_tracked_list
, states
);
814 sort_string_list(&states
->tracked
);
815 get_push_ref_states_noquery(states
);
822 struct string_list
*list
;
823 struct ref_states
*states
;
828 int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
830 struct show_info
*info
= cb_data
;
831 int n
= strlen(item
->string
);
834 string_list_insert(item
->string
, info
->list
);
838 int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
840 struct show_info
*info
= cb_data
;
841 struct ref_states
*states
= info
->states
;
842 const char *name
= item
->string
;
844 if (states
->queried
) {
845 const char *fmt
= "%s";
846 const char *arg
= "";
847 if (string_list_has_string(&states
->new, name
)) {
848 fmt
= " new (next fetch will store in remotes/%s)";
849 arg
= states
->remote
->name
;
850 } else if (string_list_has_string(&states
->tracked
, name
))
852 else if (string_list_has_string(&states
->stale
, name
))
853 arg
= " stale (use 'git remote prune' to remove)";
856 printf(" %-*s", info
->width
, name
);
860 printf(" %s\n", name
);
865 int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
867 struct show_info
*show_info
= cb_data
;
868 struct ref_states
*states
= show_info
->states
;
869 struct branch_info
*branch_info
= branch_item
->util
;
870 struct string_list_item
*item
;
873 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
874 strcmp(states
->remote
->name
, branch_info
->remote_name
))
876 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
877 show_info
->width
= n
;
878 if (branch_info
->rebase
)
879 show_info
->any_rebase
= 1;
881 item
= string_list_insert(branch_item
->string
, show_info
->list
);
882 item
->util
= branch_info
;
887 int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
889 struct show_info
*show_info
= cb_data
;
890 struct branch_info
*branch_info
= item
->util
;
891 struct string_list
*merge
= &branch_info
->merge
;
895 if (branch_info
->rebase
&& branch_info
->merge
.nr
> 1) {
896 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
901 printf(" %-*s ", show_info
->width
, item
->string
);
902 if (branch_info
->rebase
) {
903 printf("rebases onto remote %s\n", merge
->items
[0].string
);
905 } else if (show_info
->any_rebase
) {
906 printf(" merges with remote %s\n", merge
->items
[0].string
);
907 also
= " and with remote";
909 printf("merges with remote %s\n", merge
->items
[0].string
);
910 also
= " and with remote";
912 for (i
= 1; i
< merge
->nr
; i
++)
913 printf(" %-*s %s %s\n", show_info
->width
, "", also
,
914 merge
->items
[i
].string
);
919 int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
921 struct show_info
*show_info
= cb_data
;
922 struct push_info
*push_info
= push_item
->util
;
923 struct string_list_item
*item
;
925 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
926 show_info
->width
= n
;
927 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
928 show_info
->width2
= n
;
929 item
= string_list_append(push_item
->string
, show_info
->list
);
930 item
->util
= push_item
->util
;
934 int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
936 struct show_info
*show_info
= cb_data
;
937 struct push_info
*push_info
= item
->util
;
938 char *src
= item
->string
, *status
= NULL
;
940 switch (push_info
->status
) {
941 case PUSH_STATUS_CREATE
:
944 case PUSH_STATUS_DELETE
:
948 case PUSH_STATUS_UPTODATE
:
949 status
= "up to date";
951 case PUSH_STATUS_FASTFORWARD
:
952 status
= "fast forwardable";
954 case PUSH_STATUS_OUTOFDATE
:
955 status
= "local out of date";
957 case PUSH_STATUS_NOTQUERIED
:
961 printf(" %-*s %s to %-*s (%s)\n", show_info
->width
, src
,
962 push_info
->forced
? "forces" : "pushes",
963 show_info
->width2
, push_info
->dest
, status
);
965 printf(" %-*s %s to %s\n", show_info
->width
, src
,
966 push_info
->forced
? "forces" : "pushes",
971 static int show(int argc
, const char **argv
)
973 int no_query
= 0, result
= 0, query_flag
= 0;
974 struct option options
[] = {
975 OPT_GROUP("show specific options"),
976 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
979 struct ref_states states
;
980 struct string_list info_list
= { NULL
, 0, 0, 0 };
981 struct show_info info
;
983 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
989 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
991 memset(&states
, 0, sizeof(states
));
992 memset(&info
, 0, sizeof(info
));
993 info
.states
= &states
;
994 info
.list
= &info_list
;
995 for (; argc
; argc
--, argv
++) {
998 get_remote_ref_states(*argv
, &states
, query_flag
);
1000 printf("* remote %s\n URL: %s\n", *argv
,
1001 states
.remote
->url_nr
> 0 ?
1002 states
.remote
->url
[0] : "(no URL)");
1004 printf(" HEAD branch: (not queried)\n");
1005 else if (!states
.heads
.nr
)
1006 printf(" HEAD branch: (unknown)\n");
1007 else if (states
.heads
.nr
== 1)
1008 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
1010 printf(" HEAD branch (remote HEAD is ambiguous,"
1011 " may be one of the following):\n");
1012 for (i
= 0; i
< states
.heads
.nr
; i
++)
1013 printf(" %s\n", states
.heads
.items
[i
].string
);
1016 /* remote branch info */
1018 for_each_string_list(add_remote_to_show_info
, &states
.new, &info
);
1019 for_each_string_list(add_remote_to_show_info
, &states
.tracked
, &info
);
1020 for_each_string_list(add_remote_to_show_info
, &states
.stale
, &info
);
1022 printf(" Remote branch%s:%s\n",
1023 info
.list
->nr
> 1 ? "es" : "",
1024 no_query
? " (status not queried)" : "");
1025 for_each_string_list(show_remote_info_item
, info
.list
, &info
);
1026 string_list_clear(info
.list
, 0);
1030 info
.any_rebase
= 0;
1031 for_each_string_list(add_local_to_show_info
, &branch_list
, &info
);
1033 printf(" Local branch%s configured for 'git pull':\n",
1034 info
.list
->nr
> 1 ? "es" : "");
1035 for_each_string_list(show_local_info_item
, info
.list
, &info
);
1036 string_list_clear(info
.list
, 0);
1039 if (states
.remote
->mirror
)
1040 printf(" Local refs will be mirrored by 'git push'\n");
1042 info
.width
= info
.width2
= 0;
1043 for_each_string_list(add_push_to_show_info
, &states
.push
, &info
);
1044 sort_string_list(info
.list
);
1046 printf(" Local ref%s configured for 'git push'%s:\n",
1047 info
.list
->nr
> 1 ? "s" : "",
1048 no_query
? " (status not queried)" : "");
1049 for_each_string_list(show_push_info_item
, info
.list
, &info
);
1050 string_list_clear(info
.list
, 0);
1052 free_remote_ref_states(&states
);
1058 static int set_head(int argc
, const char **argv
)
1060 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1061 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1062 char *head_name
= NULL
;
1064 struct option options
[] = {
1065 OPT_GROUP("set-head specific options"),
1066 OPT_BOOLEAN('a', "auto", &opt_a
,
1067 "set refs/remotes/<name>/HEAD according to remote"),
1068 OPT_BOOLEAN('d', "delete", &opt_d
,
1069 "delete refs/remotes/<name>/HEAD"),
1072 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
1074 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1076 if (!opt_a
&& !opt_d
&& argc
== 2) {
1077 head_name
= xstrdup(argv
[1]);
1078 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1079 struct ref_states states
;
1080 memset(&states
, 0, sizeof(states
));
1081 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1082 if (!states
.heads
.nr
)
1083 result
|= error("Cannot determine remote HEAD");
1084 else if (states
.heads
.nr
> 1) {
1085 result
|= error("Multiple remote HEAD branches. "
1086 "Please choose one explicitly with:");
1087 for (i
= 0; i
< states
.heads
.nr
; i
++)
1088 fprintf(stderr
, " git remote set-head %s %s\n",
1089 argv
[0], states
.heads
.items
[i
].string
);
1091 head_name
= xstrdup(states
.heads
.items
[0].string
);
1092 free_remote_ref_states(&states
);
1093 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1094 if (delete_ref(buf
.buf
, NULL
, REF_NODEREF
))
1095 result
|= error("Could not delete %s", buf
.buf
);
1097 usage_with_options(builtin_remote_usage
, options
);
1100 unsigned char sha1
[20];
1101 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1102 /* make sure it's valid */
1103 if (!resolve_ref(buf2
.buf
, sha1
, 1, NULL
))
1104 result
|= error("Not a valid ref: %s", buf2
.buf
);
1105 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1106 result
|= error("Could not setup %s", buf
.buf
);
1108 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1112 strbuf_release(&buf
);
1113 strbuf_release(&buf2
);
1117 static int prune(int argc
, const char **argv
)
1119 int dry_run
= 0, result
= 0;
1120 struct option options
[] = {
1121 OPT_GROUP("prune specific options"),
1122 OPT__DRY_RUN(&dry_run
),
1125 struct ref_states states
;
1126 const char *dangling_msg
;
1128 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
1131 usage_with_options(builtin_remote_usage
, options
);
1133 dangling_msg
= (dry_run
1134 ? " %s will become dangling!\n"
1135 : " %s has become dangling!\n");
1137 memset(&states
, 0, sizeof(states
));
1138 for (; argc
; argc
--, argv
++) {
1141 get_remote_ref_states(*argv
, &states
, GET_REF_STATES
);
1143 if (states
.stale
.nr
) {
1144 printf("Pruning %s\n", *argv
);
1146 states
.remote
->url_nr
1147 ? states
.remote
->url
[0]
1151 for (i
= 0; i
< states
.stale
.nr
; i
++) {
1152 const char *refname
= states
.stale
.items
[i
].util
;
1155 result
|= delete_ref(refname
, NULL
, 0);
1157 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
1158 abbrev_ref(refname
, "refs/remotes/"));
1159 warn_dangling_symref(dangling_msg
, refname
);
1162 free_remote_ref_states(&states
);
1168 static int get_one_remote_for_update(struct remote
*remote
, void *priv
)
1170 struct string_list
*list
= priv
;
1171 if (!remote
->skip_default_update
)
1172 string_list_append(remote
->name
, list
);
1176 struct remote_group
{
1178 struct string_list
*list
;
1181 static int get_remote_group(const char *key
, const char *value
, void *cb
)
1183 if (!prefixcmp(key
, "remotes.") &&
1184 !strcmp(key
+ 8, remote_group
.name
)) {
1185 /* split list by white space */
1186 int space
= strcspn(value
, " \t\n");
1189 string_list_append(xstrndup(value
, space
),
1191 value
+= space
+ (value
[space
] != '\0');
1192 space
= strcspn(value
, " \t\n");
1199 static int update(int argc
, const char **argv
)
1202 struct string_list list
= { NULL
, 0, 0, 0 };
1203 static const char *default_argv
[] = { NULL
, "default", NULL
};
1207 argv
= default_argv
;
1210 remote_group
.list
= &list
;
1211 for (i
= 1; i
< argc
; i
++) {
1212 remote_group
.name
= argv
[i
];
1213 result
= git_config(get_remote_group
, NULL
);
1216 if (!result
&& !list
.nr
&& argc
== 2 && !strcmp(argv
[1], "default"))
1217 result
= for_each_remote(get_one_remote_for_update
, &list
);
1219 for (i
= 0; i
< list
.nr
; i
++)
1220 result
|= fetch_remote(list
.items
[i
].string
);
1222 /* all names were strdup()ed or strndup()ed */
1223 list
.strdup_strings
= 1;
1224 string_list_clear(&list
, 0);
1229 static int get_one_entry(struct remote
*remote
, void *priv
)
1231 struct string_list
*list
= priv
;
1233 if (remote
->url_nr
> 0) {
1236 for (i
= 0; i
< remote
->url_nr
; i
++)
1237 string_list_append(remote
->name
, list
)->util
= (void *)remote
->url
[i
];
1239 string_list_append(remote
->name
, list
)->util
= NULL
;
1244 static int show_all(void)
1246 struct string_list list
= { NULL
, 0, 0 };
1247 int result
= for_each_remote(get_one_entry
, &list
);
1252 sort_string_list(&list
);
1253 for (i
= 0; i
< list
.nr
; i
++) {
1254 struct string_list_item
*item
= list
.items
+ i
;
1256 printf("%s\t%s\n", item
->string
,
1257 item
->util
? (const char *)item
->util
: "");
1259 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1261 printf("%s\n", item
->string
);
1268 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1270 struct option options
[] = {
1271 OPT__VERBOSE(&verbose
),
1276 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
1277 PARSE_OPT_STOP_AT_NON_OPTION
);
1280 result
= show_all();
1281 else if (!strcmp(argv
[0], "add"))
1282 result
= add(argc
, argv
);
1283 else if (!strcmp(argv
[0], "rename"))
1284 result
= mv(argc
, argv
);
1285 else if (!strcmp(argv
[0], "rm"))
1286 result
= rm(argc
, argv
);
1287 else if (!strcmp(argv
[0], "set-head"))
1288 result
= set_head(argc
, argv
);
1289 else if (!strcmp(argv
[0], "show"))
1290 result
= show(argc
, argv
);
1291 else if (!strcmp(argv
[0], "prune"))
1292 result
= prune(argc
, argv
);
1293 else if (!strcmp(argv
[0], "update"))
1294 result
= update(argc
, argv
);
1296 error("Unknown subcommand: %s", argv
[0]);
1297 usage_with_options(builtin_remote_usage
, options
);
1300 return result
? 1 : 0;