for_each_alternate_ref: use strbuf for path allocation
We have a string with ".../objects" pointing to the alternate object store, and overwrite bits of it to look at other paths in the (potential) git repository holding it. This works because the only path we care about is "refs", which is shorter than "objects". Using a strbuf to hold the path lets us get rid of some magic numbers, and makes it more obvious that the memory operations are safe. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Feb 8, 2017 at 15:52 UTC
5e8c968c6465d35c9047ab3ed522cb08d46386f5
1 file changed
+14
-14
transport.c
+14
-14
@@ -1214,34 +1214,34 @@ struct alternate_refs_data {
1214
static int refs_from_alternate_cb(struct alternate_object_database *e,
1215
void *data)
1216
{
1217
- char *other;
1218
- size_t len;
1217
+ struct strbuf path = STRBUF_INIT;
1218
+ size_t base_len;
1219
struct remote *remote;
1220
struct transport *transport;
1221
const struct ref *extra;
1222
struct alternate_refs_data *cb = data;
1223
1224
- other = real_pathdup(e->path);
1225
- if (!other)
1226
- return 0;
1227
- len = strlen(other);
1228
-
1229
- if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1224
+ if (!strbuf_realpath(&path, e->path, 0))
1225
+ goto out;
1226
+ if (!strbuf_strip_suffix(&path, "/objects"))
1227
goto out;
1228
+ base_len = path.len;
1229
+
1230
/* Is this a git repository with refs? */
1232
- memcpy(other + len - 8, "/refs", 6);
1233
- if (!is_directory(other))
1231
+ strbuf_addstr(&path, "/refs");
1232
+ if (!is_directory(path.buf))
1233
goto out;
1235
- other[len - 8] = '\0';
1236
- remote = remote_get(other);
1237
- transport = transport_get(remote, other);
1234
+ strbuf_setlen(&path, base_len);
1235
+
1236
+ remote = remote_get(path.buf);
1237
+ transport = transport_get(remote, path.buf);
1238
for (extra = transport_get_remote_refs(transport);
1239
extra;
1240
extra = extra->next)
1241
cb->fn(extra, cb->data);
1242
transport_disconnect(transport);
1243
out:
1244
- free(other);
1244
+ strbuf_release(&path);
1245
return 0;
1246
}
1247