Skip to content

Commit 14a7b65

Browse files
committed
Use git_blob_filter instead of git_blob_rawcontent
1 parent cf0df26 commit 14a7b65

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/libfetchers/git-utils.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ struct GitSourceAccessor : SourceAccessor
668668
struct State
669669
{
670670
ref<GitRepoImpl> repo;
671+
std::string gitRev;
671672
Object root;
672673
std::optional<lfs::Fetch> lfsFetch = std::nullopt;
673674
};
@@ -678,6 +679,7 @@ struct GitSourceAccessor : SourceAccessor
678679
: state_{
679680
State {
680681
.repo = repo_,
682+
.gitRev = rev.gitRev(),
681683
.root = peelToTreeOrBlob(lookupObject(*repo_, hashToOID(rev)).get()),
682684
.lfsFetch = smudgeLfs ? std::make_optional(lfs::Fetch(*repo_, hashToOID(rev))) : std::nullopt,
683685
}
@@ -707,7 +709,28 @@ struct GitSourceAccessor : SourceAccessor
707709
}
708710
}
709711

710-
return std::string((const char *) git_blob_rawcontent(blob.get()), git_blob_rawsize(blob.get()));
712+
// Apply git filters including CRLF conversion
713+
git_buf filtered = GIT_BUF_INIT;
714+
git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
715+
716+
git_oid oid;
717+
if (git_oid_fromstr(&oid, state->gitRev.c_str()))
718+
throw Error("cannot convert '%s' to a Git OID", state->gitRev.c_str());
719+
720+
opts.attr_commit_id = oid;
721+
opts.flags = GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT;
722+
723+
int error = git_blob_filter(&filtered, blob.get(), path.rel_c_str(), &opts);
724+
if (error != 0) {
725+
const git_error *e = git_error_last();
726+
std::string errorMsg = e ? e->message : "Unknown error";
727+
git_buf_dispose(&filtered);
728+
throw std::runtime_error("Failed to filter blob: " + errorMsg);
729+
}
730+
std::string result(filtered.ptr, filtered.size);
731+
git_buf_dispose(&filtered);
732+
733+
return result;
711734
}
712735

713736
std::string readFile(const CanonPath & path) override

0 commit comments

Comments
 (0)