From f8c396e4ce0b2cecc63a4e6bf695c7abc005ffbd Mon Sep 17 00:00:00 2001 From: Roman Perepelitsa Date: Fri, 4 Jun 2021 09:33:30 +0200 Subject: [PATCH] drop all tags if packed-refs doesn't have a header line (https://github.com/romkatv/powerlevel10k/issues/1428) --- src/tag_db.cc | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/tag_db.cc b/src/tag_db.cc index 0e44079..28fe5f2 100644 --- a/src/tag_db.cc +++ b/src/tag_db.cc @@ -212,13 +212,24 @@ void TagDb::ParsePack() { char* p = &pack_[0]; char* e = p + pack_.size(); - if (*p == '#') { - char* eol = std::strchr(p, '\n'); - if (!eol) return; - *eol = 0; - if (!std::strstr(p, " fully-peeled") || !std::strstr(p, " sorted")) return; - p = eol + 1; - } + // Usually packed-refs starts with the following line: + // + // # pack-refs with: peeled fully-peeled sorted + // + // However, some users can produce pack-refs without this line. + // See https://github.com/romkatv/powerlevel10k/issues/1428. + // I don't know how they do it. Without the header line we cannot + // assume that refs are sorted, which isn't a big deal because we + // can just sort them. What's worse is that refs cannot be assumed + // to be fully-peeled. We don't want to peel them, so we just drop + // all tags. + if (*p != '#') return; + + char* eol = std::strchr(p, '\n'); + if (!eol) return; + *eol = 0; + if (!std::strstr(p, " fully-peeled") || !std::strstr(p, " sorted")) return; + p = eol + 1; name2id_.reserve(pack_.size() / 128); id2name_.reserve(pack_.size() / 128); @@ -249,7 +260,9 @@ void TagDb::ParsePack() { id2name_.push_back(tag); } - VERIFY(std::is_sorted(name2id_.begin(), name2id_.end(), ByName)); + if (!std::is_sorted(name2id_.begin(), name2id_.end(), ByName)) { + std::sort(name2id_.begin(), name2id_.end(), ByName); + } id2name_dirty_ = true; GlobalThreadPool()->Schedule([this] {