Skip to content

Commit f2ef607

Browse files
jaysoffiangitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e2d41c6 commit f2ef607

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

builtin-remote.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -227,32 +227,10 @@ struct ref_states {
227227
int queried;
228228
};
229229

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-
252230
static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
253231
{
254232
struct ref *fetch_map = NULL, **tail = &fetch_map;
255-
struct ref *ref;
233+
struct ref *ref, *stale_refs;
256234
int i;
257235

258236
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
268246
else
269247
string_list_append(abbrev_branch(ref->name), &states->tracked);
270248
}
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);
271256
free_refs(fetch_map);
272257

273258
sort_string_list(&states->new);
274259
sort_string_list(&states->tracked);
275-
for_each_ref(handle_one_branch, states);
276260
sort_string_list(&states->stale);
277261

278262
return 0;

remote.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "revision.h"
77
#include "dir.h"
88
#include "tag.h"
9+
#include "string-list.h"
910

1011
static struct refspec s_tag_refspec = {
1112
0,
@@ -1587,3 +1588,42 @@ struct ref *guess_remote_head(const struct ref *head,
15871588

15881589
return list;
15891590
}
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,7 @@ struct ref *guess_remote_head(const struct ref *head,
154154
const struct ref *refs,
155155
int all);
156156

157+
/* Return refs which no longer exist on remote */
158+
struct ref *get_stale_heads(struct remote *remote, struct ref *fetch_map);
159+
157160
#endif

0 commit comments

Comments
 (0)