| 228 | } |
| 229 | |
| 230 | static std::string get_repo_commit(const std::string & repo_id, |
| 231 | const std::string & token) { |
| 232 | try { |
| 233 | auto endpoint = common_get_model_endpoint(); |
| 234 | auto json = api_get(endpoint + "api/models/" + repo_id + "/refs", token); |
| 235 | |
| 236 | if (!json.is_object() || |
| 237 | !json.contains("branches") || !json["branches"].is_array()) { |
| 238 | LOG_WRN("%s: missing 'branches' for '%s'\n", __func__, repo_id.c_str()); |
| 239 | return {}; |
| 240 | } |
| 241 | |
| 242 | fs::path refs_path = get_repo_path(repo_id) / "refs"; |
| 243 | std::string name; |
| 244 | std::string commit; |
| 245 | |
| 246 | for (const auto & branch : json["branches"]) { |
| 247 | if (!branch.is_object() || |
| 248 | !branch.contains("name") || !branch["name"].is_string() || |
| 249 | !branch.contains("targetCommit") || !branch["targetCommit"].is_string()) { |
| 250 | continue; |
| 251 | } |
| 252 | std::string _name = branch["name"].get<std::string>(); |
| 253 | std::string _commit = branch["targetCommit"].get<std::string>(); |
| 254 | |
| 255 | if (!is_valid_subpath(refs_path, _name)) { |
| 256 | LOG_WRN("%s: skip invalid branch: %s\n", __func__, _name.c_str()); |
| 257 | continue; |
| 258 | } |
| 259 | if (!is_valid_commit(_commit)) { |
| 260 | LOG_WRN("%s: skip invalid commit: %s\n", __func__, _commit.c_str()); |
| 261 | continue; |
| 262 | } |
| 263 | |
| 264 | if (_name == "main") { |
| 265 | name = _name; |
| 266 | commit = _commit; |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | if (name.empty() || commit.empty()) { |
| 271 | name = _name; |
| 272 | commit = _commit; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (name.empty() || commit.empty()) { |
| 277 | LOG_WRN("%s: no valid branch for '%s'\n", __func__, repo_id.c_str()); |
| 278 | return {}; |
| 279 | } |
| 280 | |
| 281 | safe_write_file(refs_path / name, commit); |
| 282 | return commit; |
| 283 | |
| 284 | } catch (const nl::json::exception & e) { |
| 285 | LOG_ERR("%s: JSON error: %s\n", __func__, e.what()); |
| 286 | } catch (const std::exception & e) { |
| 287 | LOG_ERR("%s: error: %s\n", __func__, e.what()); |
no test coverage detected