Skip to content

Commit 6f3bd50

Browse files
committed
Use git_blob_filter instead of git_blob_rawcontent
1 parent cf0df26 commit 6f3bd50

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/libfetchers/git-utils.cc

Lines changed: 47 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,51 @@ 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 for the final return
713+
git_buf filtered = GIT_BUF_INIT;
714+
git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
715+
/*
716+
if (git_object_type(state->root.get()) == GIT_OBJECT_COMMIT) {
717+
auto commit = peelObject<Commit>(state->root.get(), GIT_OBJECT_COMMIT);
718+
opts.attr_commit_id = *git_commit_id(commit.get());
719+
}
720+
*/
721+
/*
722+
git_oid oid;
723+
724+
int error = git_oid_fromstr(&oid, state->repo->getWorkdirRef()->c_str());
725+
if (error != 0) {
726+
const git_error *e = git_error_last();
727+
std::string errorMsg = e ? e->message : "Unknown error";
728+
git_buf_dispose(&filtered);
729+
throw std::runtime_error("Failed to filter blob: " + errorMsg + " " + state->repo->getWorkdirRef()->c_str());
730+
731+
}
732+
*/
733+
734+
git_oid oid;
735+
736+
if (git_oid_fromstr(&oid, state->gitRev.c_str()))
737+
throw Error("cannot convert '%s' to a Git OID", state->gitRev.c_str());
738+
739+
opts.attr_commit_id = oid;
740+
opts.flags = GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT;
741+
742+
int error = git_blob_filter(&filtered, blob.get(), path.rel_c_str(), &opts);
743+
if (error != 0) {
744+
const git_error *e = git_error_last();
745+
std::string errorMsg = e ? e->message : "Unknown error";
746+
git_buf_dispose(&filtered);
747+
throw std::runtime_error("Failed to filter blob: " + errorMsg);
748+
}
749+
std::string result(filtered.ptr, filtered.size);
750+
git_buf_dispose(&filtered);
751+
752+
if (strcmp(path.c_str(), "/buildconf.bat")) {
753+
printInfo("readBlob(%s) ==> %s => %s", path, result.find("\r") != std::string::npos ? "yes" : "no", opts.attr_commit_id);
754+
}
755+
756+
return result;
711757
}
712758

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

0 commit comments

Comments
 (0)