Skip to content

Commit 5bdf0a8

Browse files
Hui Wanggitster
authored andcommitted
sha1_file: normalize alt_odb path before comparing and storing
When it needs to compare and add an alt object path to the alt_odb_list, we normalize this path first since comparing normalized path is easy to get correct result. Use strbuf to replace some string operations, since it is cleaner and safer. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Hui Wang <Hui.Wang@windriver.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f696543 commit 5bdf0a8

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

sha1_file.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,27 +248,30 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
248248
const char *objdir = get_object_directory();
249249
struct alternate_object_database *ent;
250250
struct alternate_object_database *alt;
251-
/* 43 = 40-byte + 2 '/' + terminating NUL */
252-
int pfxlen = len;
253-
int entlen = pfxlen + 43;
254-
int base_len = -1;
251+
int pfxlen, entlen;
252+
struct strbuf pathbuf = STRBUF_INIT;
255253

256254
if (!is_absolute_path(entry) && relative_base) {
257-
/* Relative alt-odb */
258-
if (base_len < 0)
259-
base_len = strlen(relative_base) + 1;
260-
entlen += base_len;
261-
pfxlen += base_len;
255+
strbuf_addstr(&pathbuf, real_path(relative_base));
256+
strbuf_addch(&pathbuf, '/');
262257
}
263-
ent = xmalloc(sizeof(*ent) + entlen);
258+
strbuf_add(&pathbuf, entry, len);
264259

265-
if (!is_absolute_path(entry) && relative_base) {
266-
memcpy(ent->base, relative_base, base_len - 1);
267-
ent->base[base_len - 1] = '/';
268-
memcpy(ent->base + base_len, entry, len);
269-
}
270-
else
271-
memcpy(ent->base, entry, pfxlen);
260+
normalize_path_copy(pathbuf.buf, pathbuf.buf);
261+
262+
pfxlen = strlen(pathbuf.buf);
263+
264+
/*
265+
* The trailing slash after the directory name is given by
266+
* this function at the end. Remove duplicates.
267+
*/
268+
while (pfxlen && pathbuf.buf[pfxlen-1] == '/')
269+
pfxlen -= 1;
270+
271+
entlen = pfxlen + 43; /* '/' + 2 hex + '/' + 38 hex + NUL */
272+
ent = xmalloc(sizeof(*ent) + entlen);
273+
memcpy(ent->base, pathbuf.buf, pfxlen);
274+
strbuf_release(&pathbuf);
272275

273276
ent->name = ent->base + pfxlen + 1;
274277
ent->base[pfxlen + 3] = '/';

0 commit comments

Comments
 (0)