navid/veins / 96f30f16c4319a85e8ccc3c97bc84950cf53e5f9cf7db59977076b5c1f3796cf / xveins/src/refs/tag.cpp
tag.cpp
← xveins/src/refs · 12771 bytes · raw · download
#include "cli/local_commands.h"
#include "store/store.h"
#include "worktree/worktree.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits>
namespace vn {
namespace {
bool tag_is_flag(std::string_view arg) {
return !arg.empty() && arg.front() == '-';
}
bool tag_contains(std::string_view name, std::string_view needle) {
return !needle.empty() && name.find(needle) != std::string_view::npos;
}
bool valid_tag_name(std::string_view name) {
if (name.empty() || name.size() > MAX_NAME_LEN)
return false;
const auto first = static_cast<unsigned char>(name.front());
if (first == '-' || first == '/' || first == '.')
return false;
const auto last = static_cast<unsigned char>(name.back());
if (last == '/' || last == '.')
return false;
for (char raw : name) {
const auto c = static_cast<unsigned char>(raw);
if (c == 0 || c == ' ' || c < 32 || c == 127)
return false;
if (c == '~' || c == '^' || c == ':' || c == '?' || c == '*' || c == '[' || c == '\\')
return false;
}
if (tag_contains(name, "..") || tag_contains(name, "@{") || tag_contains(name, "//"))
return false;
if (name == "@")
return false;
if (name.size() >= 5 && name.substr(name.size() - 5) == ".lock")
return false;
return true;
}
std::string short_commit(const Id& id, std::size_t n = 12) {
if (n == 0)
n = 12;
n = std::min<std::size_t>(n, 64);
return hex_encode(id).substr(0, n);
}
void print_cell(std::string_view text, std::size_t width) {
std::cout << text;
for (std::size_t i = text.size(); i < width; ++i)
std::cout << ' ';
}
bool is_digit(char c) {
return c >= '0' && c <= '9';
}
std::size_t skip_leading_zeroes(std::string_view text, std::size_t begin, std::size_t end) {
while (begin < end && text[begin] == '0')
++begin;
return begin;
}
// Compare tag names as natural versions so v0.0.10 sorts after v0.0.9.
int natural_compare(std::string_view a, std::string_view b) {
std::size_t i = 0;
std::size_t j = 0;
while (i < a.size() && j < b.size()) {
if (is_digit(a[i]) && is_digit(b[j])) {
std::size_t ai = i;
std::size_t bj = j;
while (ai < a.size() && is_digit(a[ai]))
++ai;
while (bj < b.size() && is_digit(b[bj]))
++bj;
const std::size_t an = skip_leading_zeroes(a, i, ai);
const std::size_t bn = skip_leading_zeroes(b, j, bj);
const std::size_t alen = ai - an;
const std::size_t blen = bj - bn;
if (alen != blen)
return alen < blen ? -1 : 1;
for (std::size_t k = 0; k < alen; ++k) {
const auto ac = static_cast<unsigned char>(a[an + k]);
const auto bc = static_cast<unsigned char>(b[bn + k]);
if (ac != bc)
return ac < bc ? -1 : 1;
}
i = ai;
j = bj;
} else {
const auto ac = static_cast<unsigned char>(a[i]);
const auto bc = static_cast<unsigned char>(b[j]);
if (ac != bc)
return ac < bc ? -1 : 1;
++i;
++j;
}
}
if (i < a.size())
return 1;
if (j < b.size())
return -1;
return 0;
}
int tag_set(const std::vector<std::string>& args, std::size_t start) {
std::string name;
std::string rev;
bool end_flags = false;
for (std::size_t i = start; i < args.size(); ++i) {
const std::string& arg = args[i];
if (!end_flags && arg == "--") {
end_flags = true;
} else if (!end_flags && (arg == "-r" || arg == "-rev")) {
if (i + 1 >= args.size()) {
error_prefix();
std::cout << arg << " requires a revision\n";
return 1;
}
rev = args[++i];
} else if (!end_flags && tag_is_flag(arg)) {
error_prefix();
std::cout << "unknown flag: " << arg << "\n";
return 1;
} else if (name.empty()) {
name = arg;
} else if (rev.empty()) {
rev = arg;
} else {
error_prefix();
std::cout << "tag set accepts a name and one revision\n";
return 1;
}
}
if (name.empty()) {
error_prefix();
std::cout << "tag set requires a name\n";
return 1;
}
if (!valid_tag_name(name)) {
error_prefix();
std::cout << "invalid tag name: " << name << "\n";
return 1;
}
const auto root = discover_repo(".");
if (!root) {
error_prefix();
std::cout << "not a veins repository\n";
return 1;
}
const int snap = snapshot_run(*root);
if (snap != 0)
return snap;
Store store = Store::open_all(*root);
auto tx = Txn::begin(*root, store);
if (!tx) {
error_prefix();
std::cout << "cannot load repository state\n";
return 1;
}
auto target = resolve_revision(store, tx->view(), rev);
if (!target) {
error_prefix();
if (rev.empty())
std::cout << "no working-copy commit\n";
else
std::cout << "unknown revision: " << rev << "\n";
return 1;
}
bool changed = true;
if (const auto existing = find_tag(tx->view(), name)) {
auto& tag = tx->view().tags[*existing];
if (std::memcmp(tag.target.data(), target->data(), ID_LEN) == 0) {
changed = false;
} else {
tag.target = *target;
}
} else {
tx->view().tags.push_back(TagRef{name, *target});
}
if (!changed) {
info_prefix();
std::cout << "tag " << name << " is already at " << short_commit(*target) << "\n";
return 0;
}
rebuild_heads(tx->view());
const auto status = tx->finish("tag set " + name);
if (!status)
return 1;
info_prefix();
std::cout << "tag " << name << " set to " << short_commit(*target) << "\n";
return 0;
}
int tag_delete(const std::vector<std::string>& args, std::size_t start) {
std::string name;
bool end_flags = false;
for (std::size_t i = start; i < args.size(); ++i) {
const std::string& arg = args[i];
if (!end_flags && arg == "--") {
end_flags = true;
} else if (!end_flags && tag_is_flag(arg)) {
error_prefix();
std::cout << "unknown flag: " << arg << "\n";
return 1;
} else if (!name.empty()) {
error_prefix();
std::cout << "tag rm accepts exactly one name\n";
return 1;
} else {
name = arg;
}
}
if (name.empty()) {
error_prefix();
std::cout << "tag rm requires a name\n";
return 1;
}
const auto root = discover_repo(".");
if (!root) {
error_prefix();
std::cout << "not a veins repository\n";
return 1;
}
Store store = Store::open_all(*root);
auto tx = Txn::begin(*root, store);
if (!tx) {
error_prefix();
std::cout << "cannot load repository state\n";
return 1;
}
const auto existing = find_tag(tx->view(), name);
if (!existing) {
error_prefix();
std::cout << "no such tag: " << name << "\n";
return 1;
}
auto& tags = tx->view().tags;
tags.erase(tags.begin() + static_cast<std::ptrdiff_t>(*existing));
rebuild_heads(tx->view());
const auto status = tx->finish("tag delete " + name);
if (!status)
return 1;
info_prefix();
std::cout << "deleted tag " << name << "\n";
return 0;
}
int tag_list(const std::vector<std::string>& args, std::size_t start) {
bool col_titles = true;
bool end_flags = false;
for (std::size_t i = start; i < args.size(); ++i) {
const std::string& arg = args[i];
if (!end_flags && arg == "--") {
end_flags = true;
} else if (!end_flags && arg == "-no-col-titles") {
col_titles = false;
} else if (!end_flags && tag_is_flag(arg)) {
error_prefix();
std::cout << "unknown flag: " << arg << "\n";
return 1;
} else {
error_prefix();
std::cout << "tag ls takes no operands\n";
return 1;
}
}
const auto root = discover_repo(".");
if (!root) {
error_prefix();
std::cout << "not a veins repository\n";
return 1;
}
Store store = Store::open_all(*root);
auto view = current_view(*root, store);
if (!view) {
error_prefix();
std::cout << "no operation state\n";
return 1;
}
const auto wc = working_copy_id(*view);
constexpr std::size_t w_commit = 12;
constexpr std::size_t w_change = 8;
std::size_t w_name = 3;
for (const auto& tag : view->tags)
w_name = std::max(w_name, tag.name.size());
if (col_titles) {
print_cell("Tag", w_name);
std::cout << " ";
print_cell("Commit", w_commit);
std::cout << " ";
print_cell("Change", w_change);
std::cout << " Target\n";
}
for (const auto& tag : view->tags) {
print_cell(tag.name, w_name);
std::cout << " ";
print_cell(short_commit(tag.target), w_commit);
std::cout << " ";
std::string change_text;
if (auto loaded = load_commit(store, tag.target))
change_text = base32_encode(loaded->commit.change_id).substr(0, 8);
print_cell(change_text, w_change);
std::cout << " ";
if (wc && std::memcmp(wc->data(), tag.target.data(), ID_LEN) == 0)
print_colored("@", Color::Red, false, false);
std::cout << "\n";
}
if (view->tags.empty()) {
info_prefix();
std::cout << "no tags\n";
}
return 0;
}
int tag_latest(const std::vector<std::string>& args, std::size_t start) {
for (std::size_t i = start; i < args.size(); ++i) {
error_prefix();
std::cout << "tag latest takes no operands\n";
return 1;
}
const auto root = discover_repo(".");
if (!root) {
error_prefix();
std::cout << "not a veins repository\n";
return 1;
}
Store store = Store::open_all(*root);
auto view = current_view(*root, store);
if (!view) {
error_prefix();
std::cout << "no operation state\n";
return 1;
}
if (view->tags.empty()) {
error_prefix();
std::cout << "no tags\n";
return 1;
}
const TagRef* best = nullptr;
std::int64_t best_time = std::numeric_limits<std::int64_t>::min();
for (const auto& tag : view->tags) {
std::int64_t time = std::numeric_limits<std::int64_t>::min();
if (auto loaded = load_commit(store, tag.target))
time = loaded->commit.committer.seconds;
if (best == nullptr || time > best_time ||
(time == best_time && natural_compare(tag.name, best->name) > 0)) {
best = &tag;
best_time = time;
}
}
if (!best) {
error_prefix();
std::cout << "no tags\n";
return 1;
}
std::cout << best->name << "\n";
return 0;
}
void tag_usage() {
std::cout << "usage: vn tag <set|ls|rm|latest> ...\n";
std::cout << "\n";
std::cout << "manage named pointers to commits (tags)\n";
std::cout << "\n";
std::cout << "subcommands:\n";
std::cout << " set <name> [-rev <rev>] create or move a tag (default: @)\n";
std::cout << " ls [-no-col-titles] list tags\n";
std::cout << " rm <name> delete a tag\n";
std::cout << " latest print the latest tag name\n";
std::cout << "\n";
std::cout << "the latest tag is the one whose target commit has the newest\n";
std::cout << "committer timestamp; equal timestamps use natural name order\n";
}
} // namespace
int tag_run(const std::vector<std::string>& args) {
if (args.size() <= 2) {
tag_usage();
return 1;
}
const std::string& sub = args[2];
if (sub == "set")
return tag_set(args, 3);
if (sub == "ls" || sub == "list")
return tag_list(args, 3);
if (sub == "rm" || sub == "remove")
return tag_delete(args, 3);
if (sub == "latest")
return tag_latest(args, 3);
if (sub == "help" || sub == "-h") {
tag_usage();
return 0;
}
error_prefix();
std::cout << "unknown tag subcommand: " << sub << "\n";
tag_usage();
return 1;
}
} // namespace vn