From 5572986afa19001836a1f9490b1c7a61698aef25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Sun, 22 Mar 2026 09:18:48 +0000 Subject: [PATCH] Prefer main when getting cached ref MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrien Gallouët --- common/hf-cache.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/common/hf-cache.cpp b/common/hf-cache.cpp index 51425d0c4d..4bde42cf86 100644 --- a/common/hf-cache.cpp +++ b/common/hf-cache.cpp @@ -249,16 +249,25 @@ static std::string get_cached_ref(const fs::path & repo_path) { if (!fs::is_directory(refs_path)) { return {}; } + std::string fallback; + for (const auto & entry : fs::directory_iterator(refs_path)) { - if (entry.is_regular_file()) { - std::ifstream f(entry.path()); - std::string commit; - if (f && std::getline(f, commit) && !commit.empty()) { - return commit; - } + if (!entry.is_regular_file()) { + continue; + } + std::ifstream f(entry.path()); + std::string commit; + if (!f || !std::getline(f, commit) || commit.empty()) { + continue; + } + if (entry.path().filename() == "main") { + return commit; + } + if (fallback.empty()) { + fallback = commit; } } - return {}; + return fallback; } hf_files get_cached_files(const std::string & repo_id) {