- unsigned char sha1[20];
- char *snapshot;
-
- snapshot = xstrdup(filename);
- snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
-
- if (get_sha1(snapshot, sha1) == 0)
- return snapshot;
-
- reponame = cgit_repobasename(url);
- if (prefixcmp(snapshot, reponame) == 0) {
- snapshot += strlen(reponame);
- while (snapshot && (*snapshot == '-' || *snapshot == '_'))
- snapshot++;
+ struct object_id oid;
+ struct strbuf snapshot = STRBUF_INIT;
+ int result = 1;
+
+ strbuf_addstr(&snapshot, filename);
+ strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
+
+ if (get_oid(snapshot.buf, &oid) == 0)
+ goto out;
+
+ reponame = cgit_snapshot_prefix(repo);
+ if (starts_with(snapshot.buf, reponame)) {
+ const char *new_start = snapshot.buf;
+ new_start += strlen(reponame);
+ while (new_start && (*new_start == '-' || *new_start == '_'))
+ new_start++;
+ strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);