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>
Hui Wang committed
Sep 7, 2011 at 18:37 UTC
5bdf0a8468efd55cf8f13cafdb946c0ade3372fe
1 file changed
+20
-17
sha1_file.c
+20
-17
@@ -248,27 +248,30 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
248
const char *objdir = get_object_directory();
249
struct alternate_object_database *ent;
250
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;
253
254
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, '/');
257
}
263
- ent = xmalloc(sizeof(*ent) + entlen);
258
+ strbuf_add(&pathbuf, entry, len);
259
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);
275
276
ent->name = ent->base + pfxlen + 1;
277
ent->base[pfxlen + 3] = '/';