Skip to content

Commit 29babbe

Browse files
pcloudsgitster
authored andcommitted
refs: move submodule slash stripping code to get_submodule_ref_store
This is a better place that will benefit all submodule callers instead of just resolve_gitlink_ref() Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2c616c1 commit 29babbe

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

refs.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,25 +1503,10 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
15031503
int resolve_gitlink_ref(const char *submodule, const char *refname,
15041504
unsigned char *sha1)
15051505
{
1506-
size_t len = strlen(submodule);
15071506
struct ref_store *refs;
15081507
int flags;
15091508

1510-
while (len && is_dir_sep(submodule[len - 1]))
1511-
len--;
1512-
1513-
if (!len)
1514-
return -1;
1515-
1516-
if (submodule[len]) {
1517-
/* We need to strip off one or more trailing slashes */
1518-
char *stripped = xmemdupz(submodule, len);
1519-
1520-
refs = get_submodule_ref_store(stripped);
1521-
free(stripped);
1522-
} else {
1523-
refs = get_submodule_ref_store(submodule);
1524-
}
1509+
refs = get_submodule_ref_store(submodule);
15251510

15261511
if (!refs)
15271512
return -1;
@@ -1636,6 +1621,16 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
16361621
{
16371622
struct strbuf submodule_sb = STRBUF_INIT;
16381623
struct ref_store *refs;
1624+
char *to_free = NULL;
1625+
size_t len;
1626+
1627+
if (submodule) {
1628+
len = strlen(submodule);
1629+
while (len && is_dir_sep(submodule[len - 1]))
1630+
len--;
1631+
if (!len)
1632+
return NULL;
1633+
}
16391634

16401635
if (!submodule || !*submodule) {
16411636
/*
@@ -1645,6 +1640,10 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
16451640
return get_main_ref_store();
16461641
}
16471642

1643+
if (submodule[len])
1644+
/* We need to strip off one or more trailing slashes */
1645+
submodule = to_free = xmemdupz(submodule, len);
1646+
16481647
refs = lookup_ref_store_map(&submodule_ref_stores, submodule);
16491648
if (refs)
16501649
goto done;
@@ -1664,6 +1663,8 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
16641663

16651664
done:
16661665
strbuf_release(&submodule_sb);
1666+
free(to_free);
1667+
16671668
return refs;
16681669
}
16691670

0 commit comments

Comments
 (0)