remote: refactor some logic into get_stale_heads()
Move the logic in builtin-remote.c which determines which local heads are stale to remote.c so it can be used by other builtins. Signed-off-by: Jay Soffian <jaysoffian@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jay Soffian committed
Nov 10, 2009 at 00:03 UTC
f2ef6075c9d248523bf658d82065b46d892b5601
3 files changed
+51
-24
builtin-remote.c
+8
-24
@@ -227,32 +227,10 @@ struct ref_states {
227
int queried;
228
};
229
230
-static int handle_one_branch(const char *refname,
231
- const unsigned char *sha1, int flags, void *cb_data)
232
-{
233
- struct ref_states *states = cb_data;
234
- struct refspec refspec;
235
-
236
- memset(&refspec, 0, sizeof(refspec));
237
- refspec.dst = (char *)refname;
238
- if (!remote_find_tracking(states->remote, &refspec)) {
239
- struct string_list_item *item;
240
- const char *name = abbrev_branch(refspec.src);
241
- /* symbolic refs pointing nowhere were handled already */
242
- if ((flags & REF_ISSYMREF) ||
243
- string_list_has_string(&states->tracked, name) ||
244
- string_list_has_string(&states->new, name))
245
- return 0;
246
- item = string_list_append(name, &states->stale);
247
- item->util = xstrdup(refname);
248
- }
249
- return 0;
250
-}
251
-
230
static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
231
{
232
struct ref *fetch_map = NULL, **tail = &fetch_map;
255
- struct ref *ref;
233
+ struct ref *ref, *stale_refs;
234
int i;
235
236
for (i = 0; i < states->remote->fetch_refspec_nr; i++)
@@ -268,11 +246,17 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
246
else
247
string_list_append(abbrev_branch(ref->name), &states->tracked);
248
}
249
+ stale_refs = get_stale_heads(states->remote, fetch_map);
250
+ for (ref = stale_refs; ref; ref = ref->next) {
251
+ struct string_list_item *item =
252
+ string_list_append(abbrev_branch(ref->name), &states->stale);
253
+ item->util = xstrdup(ref->name);
254
+ }
255
+ free_refs(stale_refs);
256
free_refs(fetch_map);
257
258
sort_string_list(&states->new);
259
sort_string_list(&states->tracked);
275
- for_each_ref(handle_one_branch, states);
260
sort_string_list(&states->stale);
261
262
return 0;
remote.c
+40
@@ -6,6 +6,7 @@
6
#include "revision.h"
7
#include "dir.h"
8
#include "tag.h"
9
+#include "string-list.h"
10
11
static struct refspec s_tag_refspec = {
12
0,
@@ -1587,3 +1588,42 @@ struct ref *guess_remote_head(const struct ref *head,
1588
1589
return list;
1590
}
1591
+
1592
+struct stale_heads_info {
1593
+ struct remote *remote;
1594
+ struct string_list *ref_names;
1595
+ struct ref **stale_refs_tail;
1596
+};
1597
+
1598
+static int get_stale_heads_cb(const char *refname,
1599
+ const unsigned char *sha1, int flags, void *cb_data)
1600
+{
1601
+ struct stale_heads_info *info = cb_data;
1602
+ struct refspec refspec;
1603
+ memset(&refspec, 0, sizeof(refspec));
1604
+ refspec.dst = (char *)refname;
1605
+ if (!remote_find_tracking(info->remote, &refspec)) {
1606
+ if (!((flags & REF_ISSYMREF) ||
1607
+ string_list_has_string(info->ref_names, refspec.src))) {
1608
+ struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
1609
+ hashcpy(ref->new_sha1, sha1);
1610
+ }
1611
+ }
1612
+ return 0;
1613
+}
1614
+
1615
+struct ref *get_stale_heads(struct remote *remote, struct ref *fetch_map)
1616
+{
1617
+ struct ref *ref, *stale_refs = NULL;
1618
+ struct string_list ref_names = { NULL, 0, 0, 0 };
1619
+ struct stale_heads_info info;
1620
+ info.remote = remote;
1621
+ info.ref_names = &ref_names;
1622
+ info.stale_refs_tail = &stale_refs;
1623
+ for (ref = fetch_map; ref; ref = ref->next)
1624
+ string_list_append(ref->name, &ref_names);
1625
+ sort_string_list(&ref_names);
1626
+ for_each_ref(get_stale_heads_cb, &info);
1627
+ string_list_clear(&ref_names, 0);
1628
+ return stale_refs;
1629
+}
remote.h
+3
@@ -154,4 +154,7 @@ struct ref *guess_remote_head(const struct ref *head,
154
const struct ref *refs,
155
int all);
156
157
+/* Return refs which no longer exist on remote */
158
+struct ref *get_stale_heads(struct remote *remote, struct ref *fetch_map);
159
+
160
#endif