navid/veins / commit
a56780e67a9f
Navid M <navidmomtahen@gmail.com> · 2026-11-28
parents: 685e42af51e0
Browse files at this commit · Download source
--- a/docs/SPECIFICATION_V1.adoc+++ b/docs/SPECIFICATION_V1.adoc@@ -46,7 +46,7 @@ === Naming -The product and file format are named *veins*. The command-line program is named `vn` (`vn.exe` on Windows). All command examples in this document use `vn`. The name `veins` is used for the repository directory (`.veins/`), configuration and attribute files (`.vnexclude`, `.veinsattributes`), the object-ID domain separator, and reserved header keys (`veins-change-id`, `veins-sig`). Conforming implementations of the format are not required to ship a program called `vn`.+The product and file format are named *veins*. The command-line program is named `vn` (`vn.exe` on Windows). All command examples in this document use `vn`. The name `veins` is used for the repository directory (`.veins/`), configuration and attribute files (`.vnexclude`, `.vnattribs`, `.vnmergedata`), the object-ID domain separator, and reserved header keys (`veins-change-id`, `veins-sig`). Conforming implementations of the format are not required to ship a program called `vn`. === Conventions @@ -514,7 +514,7 @@ * veins MUST keep a stat cache (path, size, mtime in nanoseconds, inode, content ID) in `.veins/working_copy/`. A file whose stat data is unchanged MUST NOT be re-read. A file whose mtime is within a "racy" window (less than 2 seconds old relative to the cache write) MUST be re-verified by content. * New or changed files are chunked and hashed in parallel. Files are only read once. * Ignore rules come from `.vnexclude` (gitignore syntax), which `vn ex` appends to. If `ignore.git = true` (default), `.gitignore` files are also honored.-* File modes: only the owner execute bit is tracked. Line endings are not translated; bytes on disk are bytes in the store.+* File modes: only the owner execute bit is tracked. Files assigned an `eol:*` attribute in `.vnattribs` are normalized to LF in the store and materialized with the requested ending on checkout. * Exact renames (a deleted path and an added path with the same content ID) are recorded automatically in the snapshot commit's `renames`. With `rename.autodetect = similar` (default), added/deleted pairs with similarity of at least 60% are also recorded. `rename.autodetect = off` records only renames made with `vn mv`. === Untracking paths@@ -663,7 +663,7 @@ === Path policies -A tracked `.veinsattributes` file assigns attributes to paths, with one `glob attr=value` rule per line (later rules override earlier ones).+A tracked `.vnmergedata` file assigns merge behavior to paths, with one `glob attr=value` rule per line (later rules override earlier ones). `.veinsattributes` is not read. [cols="2,3",options="header"] |===@@ -672,8 +672,31 @@ | `merge=text` | Default for non-binary files. | `merge=binary` | Never merge content. Conflicts resolve only through the algebra. | `merge=regenerate` | On conflict, take `ours`, record no conflict, and mark the path as *needing regeneration*.-| `generated=true` | Informational (hides from review diffs by default).+|===++A tracked `.vnattribs` file classifies paths for generation, binary detection, and line-ending normalization. Rules use `glob attr...` syntax; later matching rules override individual attributes.++[cols="2,3",options="header"]+|===+| Attribute | Meaning++| `gen` / `-gen` | Mark or unmark a path as generated. Generated files are excluded from `vn inf` totals unless `-inc-gen` is passed.+| `txt` / `bin` | Force text or binary classification.+| `eol:lf`, `eol:crlf`, `eol:cr` | Normalize a text file to LF in the store and write the requested ending on checkout.+| `-eol` | Clear an inherited EOL policy. |===++Example:++[source]+----+*.c txt eol:lf+*.cmd txt eol:crlf+*.ssa gen+**/generated/* gen+*.bin bin+*.exe bin+---- For `merge=regenerate`, `vn status` lists paths that need regeneration. If the local config defines `regenerate.<glob> = <command>`, `vn regenerate` runs that command and snapshots the result. A commit that contains a path still marked as needing regeneration triggers a warning. Commands are only read from local config (`.veins/config`), never from tracked files. @@ -1082,6 +1105,7 @@ | `vn clone <url> [-blobs head\|none\|all]` | Clone via the native protocol. | `vn status` | Working-copy changes, conflicts, files needing regeneration. | `vn log [-rev <revset>] [-lN \| -fN] [-no-col-titles]` | Show history, newest first, flagging conflicted and divergent changes.+| `vn inf [-inc-gen]` | Show repository, language and author statistics; generated paths are excluded unless `-inc-gen` is passed. | `vn show <rev>`, `vn diff [-rev <rev>]` | Display a commit or a diff. | `vn new [<rev>...]`, `vn describe [-message <message>]`, `vn commit -message <message>` | Create and describe changes. `vn commit` does nothing when the working copy has no changes. | `vn mv` | Move tracked files, recording renames.--- a/docs/SPECIFICATION_V1.html+++ b/docs/SPECIFICATION_V1.html@@ -670,7 +670,7 @@ <div class="sect2"> <h3 id="_naming">1.4. Naming</h3> <div class="paragraph">-<p>The product and file format are named <strong>veins</strong>. The command-line program is named <code>vn</code> (<code>vn.exe</code> on Windows). All command examples in this document use <code>vn</code>. The name <code>veins</code> is used for the repository directory (<code>.veins/</code>), configuration and attribute files (<code>.vnexclude</code>, <code>.veinsattributes</code>), the object-ID domain separator, and reserved header keys (<code>veins-change-id</code>, <code>veins-sig</code>). Conforming implementations of the format are not required to ship a program called <code>vn</code>.</p>+<p>The product and file format are named <strong>veins</strong>. The command-line program is named <code>vn</code> (<code>vn.exe</code> on Windows). All command examples in this document use <code>vn</code>. The name <code>veins</code> is used for the repository directory (<code>.veins/</code>), configuration and attribute files (<code>.vnexclude</code>, <code>.vnattribs</code>, <code>.vnmergedata</code>), the object-ID domain separator, and reserved header keys (<code>veins-change-id</code>, <code>veins-sig</code>). Conforming implementations of the format are not required to ship a program called <code>vn</code>.</p> </div> </div> <div class="sect2">@@ -1644,7 +1644,7 @@ <p>Ignore rules come from <code>.vnexclude</code> (gitignore syntax), which <code>vn ex</code> appends to. If <code>ignore.git = true</code> (default), <code>.gitignore</code> files are also honored.</p> </li> <li>-<p>File modes: only the owner execute bit is tracked. Line endings are not translated; bytes on disk are bytes in the store.</p>+<p>File modes: only the owner execute bit is tracked. Files assigned an <code>eol:*</code> attribute in <code>.vnattribs</code> are normalized to LF in the store and materialized with the requested ending on checkout.</p> </li> <li> <p>Exact renames (a deleted path and an added path with the same content ID) are recorded automatically in the snapshot commit’s <code>renames</code>. With <code>rename.autodetect = similar</code> (default), added/deleted pairs with similarity of at least 60% are also recorded. <code>rename.autodetect = off</code> records only renames made with <code>vn mv</code>.</p>@@ -1969,7 +1969,7 @@ <div class="sect2"> <h3 id="_path_policies">7.11. Path policies</h3> <div class="paragraph">-<p>A tracked <code>.veinsattributes</code> file assigns attributes to paths, with one <code>glob attr=value</code> rule per line (later rules override earlier ones).</p>+<p>A tracked <code>.vnmergedata</code> file assigns merge behavior to paths, with one <code>glob attr=value</code> rule per line (later rules override earlier ones). <code>.veinsattributes</code> is not read. A tracked <code>.vnattribs</code> file classifies paths for generation, binary detection, and line-ending normalization using <code>glob attr...</code> rules; generated paths are excluded from <code>vn inf</code> unless <code>-inc-gen</code> is passed.</p> </div> <table class="tableblock frame-all grid-all stretch"> <colgroup>--- a/xveins/CMakeLists.txt+++ b/xveins/CMakeLists.txt@@ -129,6 +129,7 @@ src/store/store.cpp src/store/oplog.cpp src/worktree/worktree.cpp+ src/worktree/attributes.cpp src/worktree/pathspec.cpp src/worktree/remove.cpp src/worktree/exclude.cpp--- a/xveins/src/cli/cli.cpp+++ b/xveins/src/cli/cli.cpp@@ -6,14 +6,17 @@ #include "worktree/pathspec.h" #include "protocol/promisor.h" #include "store/store.h"+#include "worktree/attributes.h" #include "worktree/worktree.h" #include <algorithm>+#include <atomic> #include <cstring> #include <iostream> #include <map> #include <set> #include <sstream>+#include <thread> #include <unordered_map> namespace vn {@@ -1917,20 +1920,6 @@ return count; } -void add_ext_stat(std::vector<ExtStat>& stats, std::string_view path, std::uint64_t lines,- std::uint64_t size) {- const std::string ext = ext_of(path);- for (auto& stat : stats) {- if (stat.ext == ext) {- ++stat.files;- stat.lines += lines;- stat.bytes += size;- return;- }- }- stats.push_back(ExtStat{ext, 1, lines, size});-}- std::uint64_t dir_size(const fs::path& path, std::size_t depth) { if (depth > 64) return 0;@@ -1949,16 +1938,20 @@ return total; } +std::string size_text(std::uint64_t n) {+ if (n >= 1073741824)+ return std::to_string(n / 1073741824) + "." ++ std::to_string(((n % 1073741824) * 10) / 1073741824) + " GiB";+ if (n >= 1048576)+ return std::to_string(n / 1048576) + "." + std::to_string(((n % 1048576) * 10) / 1048576) ++ " MiB";+ if (n >= 1024)+ return std::to_string(n / 1024) + "." + std::to_string(((n % 1024) * 10) / 1024) + " KiB";+ return std::to_string(n) + " B";+}+ void print_size(std::uint64_t n) {- if (n >= 1073741824) {- std::cout << n / 1073741824 << "." << ((n % 1073741824) * 10) / 1073741824 << " GiB\n";- } else if (n >= 1048576) {- std::cout << n / 1048576 << "." << ((n % 1048576) * 10) / 1048576 << " MiB\n";- } else if (n >= 1024) {- std::cout << n / 1024 << "." << ((n % 1024) * 10) / 1024 << " KiB\n";- } else {- std::cout << n << " B\n";- }+ std::cout << size_text(n) << "\n"; } void show_date(std::string_view label, std::int64_t seconds) {@@ -1967,7 +1960,23 @@ std::cout << stamp.substr(0, 10) << "\n"; } -int run_inf() {+int run_inf(const std::vector<std::string>& args) {+ bool include_generated = false;+ bool end_flags = false;+ for (std::size_t i = 2; i < args.size(); ++i) {+ const std::string& arg = args[i];+ if (!end_flags && arg == "--")+ end_flags = true;+ else if (!end_flags && (arg == "-inc-gen" || arg == "-include-generated" ||+ arg == "--include-generated"))+ include_generated = true;+ else if (!end_flags && is_flag(arg)) {+ error_prefix();+ std::cout << "unknown flag: " << arg << "\n";+ return 1;+ }+ }+ const auto root = open_repo(); if (!root) return 1;@@ -1982,27 +1991,94 @@ wc_tree = loaded->commit.root_tree; } + struct FileJob {+ const FlatEntry* entry = nullptr;+ PathAttributes attributes;+ };+ struct FileResult {+ std::string ext;+ std::uint64_t lines = 0;+ std::uint64_t bytes = 0;+ };+ std::vector<ExtStat> exts; std::vector<AuthorStat> authors; std::uint64_t total_files = 0; std::uint64_t total_lines = 0;+ std::uint64_t generated_files = 0;+ std::uint64_t generated_lines = 0;+ std::uint64_t generated_bytes = 0; if (wc_tree) {- for (const auto& entry : flatten_tree(store, *wc_tree)) {+ const AttributeRules attributes = AttributeRules::load(*root);+ const auto entries = flatten_tree(store, *wc_tree);+ std::vector<FileJob> jobs;+ jobs.reserve(entries.size());+ for (const auto& entry : entries) { if (entry.kind != KIND_FILE && entry.kind != KIND_EXECUTABLE) continue;- ++total_files;- auto data = read_blob(store, entry.target);- if (!data) {- add_ext_stat(exts, entry.path, 0, 0);- continue;+ jobs.push_back(FileJob{&entry, attributes.lookup(entry.path)});+ }++ std::vector<FileResult> results(jobs.size());+ std::atomic<std::size_t> next{0};+ const auto worker = [&]() {+ for (;;) {+ const std::size_t index = next.fetch_add(1);+ if (index >= jobs.size())+ return;+ const auto& job = jobs[index];+ FileResult result;+ result.ext = ext_of(job.entry->path);+ if (auto data = read_blob(store, job.entry->target)) {+ result.bytes = data->size();+ const bool binary =+ job.attributes.text == AttributeText::Binary ||+ (job.attributes.text == AttributeText::Auto &&+ looks_binary(bytes_of(*data)));+ if (!binary)+ result.lines = count_nonblank(bytes_of(*data));+ }+ results[index] = std::move(result); }- const std::uint64_t size = data->size();- std::uint64_t lines = 0;- if (!looks_binary(bytes_of(*data)))- lines = count_nonblank(bytes_of(*data));- total_lines += lines;- add_ext_stat(exts, entry.path, lines, size);+ };++ std::size_t workers = std::min<std::size_t>(available_cpu_count(), jobs.size());+ workers = std::min<std::size_t>(workers, 16);+ if (workers <= 1) {+ worker();+ } else {+ store.preload();+ std::vector<std::thread> threads;+ threads.reserve(workers);+ for (std::size_t w = 0; w < workers; ++w)+ threads.emplace_back(worker);+ for (auto& thread : threads)+ thread.join(); }++ std::map<std::string, ExtStat> by_ext;+ for (std::size_t i = 0; i < jobs.size(); ++i) {+ const auto& job = jobs[i];+ const auto& result = results[i];+ if (job.attributes.generated) {+ ++generated_files;+ generated_lines += result.lines;+ generated_bytes += result.bytes;+ if (!include_generated)+ continue;+ }+ ++total_files;+ total_lines += result.lines;+ auto& stat = by_ext[result.ext];+ stat.ext = result.ext;+ ++stat.files;+ stat.lines += result.lines;+ stat.bytes += result.bytes;+ }+ for (auto& [ext, stat] : by_ext) {+ (void)ext;+ exts.push_back(std::move(stat));+ } } std::uint64_t commits = 0;@@ -2060,6 +2136,14 @@ } print_left("Size", 14, Color::Default, true); print_size(dir_size(veins_dir(*root), 0));+ if (generated_files > 0) {+ print_left("Generated", 14, Color::Default, true);+ std::cout << generated_files << " files " << generated_lines << " lines "+ << size_text(generated_bytes);+ if (!include_generated)+ std::cout << " (excluded; use -inc-gen)";+ std::cout << "\n";+ } std::cout << "\n"; print_bold("Languages", Color::BrightCyan, false);@@ -2199,10 +2283,12 @@ } void show_inf() {- std::cout << "usage: vn inf\n\n";+ std::cout << "usage: vn inf [-inc-gen]\n\n"; std::cout << "show a repository overview with a language breakdown by file " "extension,\n";- std::cout << "line-of-code shares, author commit shares, and repository metadata\n";+ std::cout << "line-of-code shares, author commit shares, and repository metadata\n\n";+ std::cout << "options:\n";+ std::cout << " -inc-gen include files marked generated in .vnattribs\n"; } void show_log() {@@ -2591,7 +2677,7 @@ if (cmd == "ls") return run_ls(args); if (cmd == "inf")- return run_inf();+ return run_inf(args); if (cmd == "log") return run_log(args); if (cmd == "show")--- a/xveins/src/merge/merge.cpp+++ b/xveins/src/merge/merge.cpp@@ -871,7 +871,7 @@ } bool merge_binary(const fs::path& repo, std::string_view path) {- auto data = read_file_opt(repo / ".veinsattributes");+ auto data = read_file_opt(repo / ".vnmergedata"); if (!data) return false; const std::string_view text(reinterpret_cast<const char*>(data->data()), data->size());--- /dev/null+++ b/xveins/src/worktree/attributes.cpp@@ -0,0 +1,173 @@+#include "worktree/attributes.h"++#include <algorithm>+#include <cstring>++namespace vn {++namespace {++std::string_view trim(std::string_view text) {+ while (!text.empty() && (text.front() == ' ' || text.front() == '\t' || text.front() == '\r' ||+ text.front() == '\n'))+ text.remove_prefix(1);+ while (!text.empty() && (text.back() == ' ' || text.back() == '\t' || text.back() == '\r' ||+ text.back() == '\n'))+ text.remove_suffix(1);+ return text;+}++std::string_view next_token(std::string_view text, std::size_t& position) {+ while (position < text.size() && (text[position] == ' ' || text[position] == '\t'))+ ++position;+ const std::size_t start = position;+ while (position < text.size() && text[position] != ' ' && text[position] != '\t')+ ++position;+ return text.substr(start, position - start);+}++std::string_view basename(std::string_view path) {+ const auto slash = path.rfind('/');+ return slash == std::string_view::npos ? path : path.substr(slash + 1);+}++bool rule_matches(const std::string& pattern, bool has_slash, std::string_view path) {+ if (glob_match(pattern, path))+ return true;+ return !has_slash && glob_match(pattern, basename(path));+}++AttributeEol parse_eol(std::string_view value) {+ if (value == "lf")+ return AttributeEol::Lf;+ if (value == "crlf")+ return AttributeEol::Crlf;+ if (value == "cr")+ return AttributeEol::Cr;+ return AttributeEol::None;+}++} // namespace++AttributeRules AttributeRules::load(const fs::path& root) {+ AttributeRules rules;+ const fs::path path = root / ".vnattribs";+ if (const auto info = stat_path(path))+ rules.mtime_ns_ = static_cast<std::int64_t>(info->mtime_ns);+ const auto data = read_file_opt(path);+ if (!data)+ return rules;+ const std::string_view text(reinterpret_cast<const char*>(data->data()), data->size());+ std::size_t start = 0;+ while (start <= text.size()) {+ const std::size_t end = text.find('\n', start);+ const std::size_t stop = end == std::string_view::npos ? text.size() : end;+ const std::string_view line = trim(text.substr(start, stop - start));+ start = stop == text.size() ? text.size() + 1 : stop + 1;+ if (line.empty() || line.front() == '#')+ continue;+ std::size_t position = 0;+ const std::string_view pattern = next_token(line, position);+ if (pattern.empty())+ continue;+ Rule rule;+ rule.pattern = std::string(pattern);+ rule.has_slash = pattern.find('/') != std::string_view::npos;+ while (position < line.size()) {+ const std::string_view token = next_token(line, position);+ if (token.empty())+ break;+ if (token == "gen") {+ rule.generated_set = true;+ rule.generated = true;+ } else if (token == "-gen") {+ rule.generated_set = true;+ rule.generated = false;+ } else if (token == "txt") {+ rule.text_set = true;+ rule.text = AttributeText::Text;+ } else if (token == "bin") {+ rule.text_set = true;+ rule.text = AttributeText::Binary;+ } else if (token == "-txt" || token == "-bin") {+ rule.text_set = true;+ rule.text = AttributeText::Auto;+ } else if (token == "-eol") {+ rule.eol_set = true;+ rule.eol = AttributeEol::None;+ } else if (token.starts_with("eol:")) {+ const auto value = token.substr(4);+ if (value == "lf" || value == "crlf" || value == "cr") {+ rule.eol_set = true;+ rule.eol = parse_eol(value);+ }+ }+ }+ rules.rules_.push_back(std::move(rule));+ }+ return rules;+}++PathAttributes AttributeRules::lookup(std::string_view path) const {+ PathAttributes result;+ for (const auto& rule : rules_) {+ if (!rule_matches(rule.pattern, rule.has_slash, path))+ continue;+ if (rule.generated_set)+ result.generated = rule.generated;+ if (rule.text_set)+ result.text = rule.text;+ if (rule.eol_set)+ result.eol = rule.eol;+ }+ return result;+}++bool AttributeRules::changed_since(std::int64_t cache_written_ms) const {+ if (cache_written_ms <= 0)+ return true;+ if (mtime_ns_ == 0)+ return false;+ return mtime_ns_ > cache_written_ms * 1'000'000;+}++std::vector<std::uint8_t> normalize_eol(Bytes data, AttributeEol eol) {+ std::vector<std::uint8_t> out;+ out.reserve(data.size());+ std::uint8_t first = '\n';+ std::uint8_t second = 0;+ switch (eol) {+ case AttributeEol::Lf:+ first = '\n';+ break;+ case AttributeEol::Crlf:+ first = '\r';+ second = '\n';+ break;+ case AttributeEol::Cr:+ first = '\r';+ break;+ case AttributeEol::None:+ out.insert(out.end(), data.begin(), data.end());+ return out;+ }+ for (std::size_t i = 0; i < data.size(); ++i) {+ const std::uint8_t byte = data[i];+ if (byte == '\r') {+ if (i + 1 < data.size() && data[i + 1] == '\n')+ ++i;+ out.push_back(first);+ if (second != 0)+ out.push_back(second);+ } else if (byte == '\n') {+ out.push_back(first);+ if (second != 0)+ out.push_back(second);+ } else {+ out.push_back(byte);+ }+ }+ return out;+}++} // namespace vn--- /dev/null+++ b/xveins/src/worktree/attributes.h@@ -0,0 +1,56 @@+#pragma once++#include "core/common.h"++#include <cstdint>+#include <string>+#include <string_view>+#include <vector>++namespace vn {++enum class AttributeEol : std::uint8_t {+ None = 0,+ Lf = 1,+ Crlf = 2,+ Cr = 3,+};++enum class AttributeText : std::uint8_t {+ Auto = 0,+ Text = 1,+ Binary = 2,+};++struct PathAttributes {+ bool generated = false;+ AttributeText text = AttributeText::Auto;+ AttributeEol eol = AttributeEol::None;+};++class AttributeRules {+public:+ static AttributeRules load(const fs::path& root);++ PathAttributes lookup(std::string_view path) const;+ bool changed_since(std::int64_t cache_written_ms) const;++private:+ struct Rule {+ std::string pattern;+ bool has_slash = false;+ bool generated_set = false;+ bool generated = false;+ bool text_set = false;+ AttributeText text = AttributeText::Auto;+ bool eol_set = false;+ AttributeEol eol = AttributeEol::None;+ };++ std::vector<Rule> rules_;+ std::int64_t mtime_ns_ = 0;+};++std::vector<std::uint8_t> normalize_eol(Bytes data, AttributeEol eol);++} // namespace vn--- a/xveins/src/worktree/worktree.cpp+++ b/xveins/src/worktree/worktree.cpp@@ -1,6 +1,7 @@ #include "worktree/worktree.h" #include "merge/merge.h"+#include "worktree/attributes.h" #include <algorithm> #include <atomic>@@ -187,8 +188,9 @@ std::optional<Id> build_dir(Txn& txn, const fs::path& root, std::string_view base, std::optional<Id> old_tree_id, const IgnoreRules& base_rules,- const Cache& cache, std::vector<CacheEntry>& new_cache,- std::int64_t now_ms, BuildState& state, std::size_t depth) {+ const AttributeRules& attributes, bool cache_valid, const Cache& cache,+ std::vector<CacheEntry>& new_cache, std::int64_t now_ms,+ BuildState& state, std::size_t depth) { if (depth > 4096) { state.failed = true; return std::nullopt;@@ -262,8 +264,8 @@ if (old_entry.kind == KIND_SUBTREE) child_old = old_entry.target; }- auto child = build_dir(txn, root, rel, child_old, local, cache, new_cache, now_ms,- state, depth + 1);+ auto child = build_dir(txn, root, rel, child_old, local, attributes, cache_valid, cache,+ new_cache, now_ms, state, depth + 1); if (state.failed) return std::nullopt; if (child)@@ -281,9 +283,11 @@ } else if (info->kind == 1) { const bool executable = info->executable; std::optional<Id> target;- if (const auto* cached = cache_lookup(cache, rel)) {- if (cache_matches(*cached, *info, executable, now_ms, cache.written_ms)) {- target = cached->target;+ if (cache_valid) {+ if (const auto* cached = cache_lookup(cache, rel)) {+ if (cache_matches(*cached, *info, executable, now_ms, cache.written_ms)) {+ target = cached->target;+ } } } if (!target) {@@ -292,7 +296,14 @@ state.failed = true; return std::nullopt; }- target = add_blob_data(txn, bytes_of(*data));+ const auto path_attributes = attributes.lookup(rel);+ if (path_attributes.eol != AttributeEol::None &&+ path_attributes.text != AttributeText::Binary) {+ const auto normalized = normalize_eol(bytes_of(*data), path_attributes.eol);+ target = add_blob_data(txn, bytes_of(normalized));+ } else {+ target = add_blob_data(txn, bytes_of(*data));+ } } new_cache.push_back(CacheEntry{rel, info->size, info->mtime_ns, info->inode, info->kind, executable, *target});@@ -434,7 +445,7 @@ } bool write_entry(Store& store, const fs::path& root, const WalkEntry& entry,- std::string& error_path) {+ const AttributeRules* attributes, std::string& error_path) { if (entry.kind == KIND_SYMLINK) { auto data = read_blob(store, entry.target); if (!data)@@ -444,6 +455,15 @@ auto data = read_blob(store, entry.target); if (!data) return false;+ if (attributes) {+ const auto path_attributes = attributes->lookup(entry.path);+ if (path_attributes.eol != AttributeEol::None &&+ path_attributes.text != AttributeText::Binary) {+ const auto normalized = normalize_eol(bytes_of(*data), path_attributes.eol);+ return write_bytes_to_path(root, entry.path, bytes_of(normalized),+ entry.kind == KIND_EXECUTABLE, error_path);+ }+ } return write_bytes_to_path(root, entry.path, bytes_of(*data), entry.kind == KIND_EXECUTABLE, error_path); }@@ -523,7 +543,7 @@ } bool write_tree_side(Store& store, const fs::path& root, const Id& tree_id, std::string_view prefix,- std::string& error_path) {+ const AttributeRules* attributes, std::string& error_path) { const auto entries = flatten_tree(store, tree_id); for (const auto& entry : entries) { if (entry.kind == KIND_CONFLICT)@@ -534,7 +554,8 @@ if (!path.empty()) path.push_back('/'); path += entry.path;- if (!write_entry(store, root, WalkEntry{path, entry.kind, entry.target}, error_path))+ if (!write_entry(store, root, WalkEntry{path, entry.kind, entry.target}, attributes,+ error_path)) return false; } return true;@@ -541,7 +562,8 @@ } bool materialize_value(Store& store, const fs::path& root, std::string_view path,- const ConflictValue& value, std::string& error_path) {+ const ConflictValue& value, const AttributeRules* attributes,+ std::string& error_path) { error_path = std::string(path); if (value.kind == 0) { remove_tracked_path(root, path);@@ -551,14 +573,15 @@ return false; if (value.kind == KIND_SUBTREE) { remove_tracked_path(root, path);- return write_tree_side(store, root, *value.target, path, error_path);+ return write_tree_side(store, root, *value.target, path, attributes, error_path); } return write_entry(store, root, WalkEntry{std::string(path), value.kind, *value.target},- error_path);+ attributes, error_path); } bool materialize_conflict(Store& store, const fs::path& root, std::string_view path,- const Id& target, std::string& error_path) {+ const Id& target, const AttributeRules* attributes,+ std::string& error_path) { auto conflict = load_conflict(store, target); if (!conflict) { error_path = std::string(path);@@ -569,7 +592,7 @@ std::string suffix = i == 0 ? ".base" : ".base" + std::to_string(i + 1); std::string full(path); full += suffix;- if (!materialize_value(store, root, full, conflict->removes[i], error_path))+ if (!materialize_value(store, root, full, conflict->removes[i], attributes, error_path)) ok = false; } for (std::size_t i = 0; i < conflict->adds.size(); ++i) {@@ -582,11 +605,11 @@ suffix = ".theirs" + std::to_string(i + 1); std::string full(path); full += suffix;- if (!materialize_value(store, root, full, conflict->adds[i], error_path))+ if (!materialize_value(store, root, full, conflict->adds[i], attributes, error_path)) ok = false; } if (!conflict->adds.empty()) {- if (!materialize_value(store, root, path, conflict->adds.front(), error_path))+ if (!materialize_value(store, root, path, conflict->adds.front(), attributes, error_path)) ok = false; } else { remove_tracked_path(root, path);@@ -596,6 +619,7 @@ int materialize_tree(Store& store, const fs::path& root, const std::vector<FlatEntry>& old_entries, const Id& new_root, Progress* progress) {+ const AttributeRules attributes = AttributeRules::load(root); auto checked_entries = flatten_tree_checked(store, new_root); if (!checked_entries) { error_prefix();@@ -695,7 +719,7 @@ continue; } std::string local_error;- if (!write_entry(store, root, entry, local_error)) {+ if (!write_entry(store, root, entry, &attributes, local_error)) { std::lock_guard<std::mutex> lock(error_mutex); if (!failed.exchange(true)) error_path = std::move(local_error);@@ -717,7 +741,8 @@ } } for (const auto& conflict : conflicts) {- if (!materialize_conflict(store, root, conflict.path, conflict.target, error_path)) {+ if (!materialize_conflict(store, root, conflict.path, conflict.target, &attributes,+ error_path)) { if (progress) progress->clear(); error_prefix();@@ -790,10 +815,13 @@ IgnoreRules rules = ignore_empty(); ignore_load_root(rules, root); const Cache cache = load_cache(root);+ const AttributeRules attributes = AttributeRules::load(root);+ const bool cache_valid = !attributes.changed_since(cache.written_ms); std::vector<CacheEntry> new_cache; const std::int64_t now_ms = unix_now_millis(); BuildState state;- auto built = build_dir(*tx, root, "", old_tree, rules, cache, new_cache, now_ms, state, 0);+ auto built = build_dir(*tx, root, "", old_tree, rules, attributes, cache_valid, cache, new_cache,+ now_ms, state, 0); std::optional<Id> new_root; if (!state.failed && built) {--- a/xveins/tests/integration_tests.cpp+++ b/xveins/tests/integration_tests.cpp@@ -606,6 +606,41 @@ t.expect_eq(f.read("target.txt"), "edited:target.txt\n"); f.vn({"cfg", "-unset", "editor"}); #endif+}++void worktree_attributes_eol(test::Context& t) {+ VnFixture f;+ f.init();+ f.write(".vnattribs", "*.txt txt eol:lf\n*.cmd txt eol:crlf\n");+ f.write("a.txt", "a\r\nb\r\n");+ f.write("b.cmd", "x\ny\n");+ f.commit("base");+ const std::string base = f.commit_for_message("base");++ fs::remove(f.repo / "a.txt");+ fs::remove(f.repo / "b.cmd");+ f.vn({"co", base});+ t.expect_eq(f.read("a.txt"), "a\nb\n");+ t.expect_eq(f.read("b.cmd"), "x\r\ny\r\n");+}++void worktree_inf_generated(test::Context& t) {+ VnFixture f;+ f.init();+ f.write(".vnattribs", "*.ssa gen\n*.c txt eol:lf\n");+ f.write("main.c", "one\ntwo\n");+ f.write("out.ssa", "one\ntwo\nthree\n");+ f.commit("base");++ const std::string default_output = f.vn({"inf"}).out;+ t.expect_contains(default_output, "Generated");+ t.expect_contains(default_output, "1 files");+ t.expect_contains(default_output, "excluded; use -inc-gen");+ t.expect_not_contains(default_output, ".ssa");++ const std::string included_output = f.vn({"inf", "-inc-gen"}).out;+ t.expect_contains(included_output, ".ssa");+ t.expect_not_contains(included_output, "excluded; use -inc-gen"); } void history_log_is_topological(test::Context& t) {@@ -1266,6 +1301,8 @@ {"exclude_and_remove", worktree_exclude_and_remove}, {"ignore_rules", worktree_ignore_rules}, {"checkout_removes_later_files", worktree_checkout_removes_later_files},+ {"attributes_eol", worktree_attributes_eol},+ {"inf_generated", worktree_inf_generated}, {"editor", worktree_editor}, }}, {"history",--- a/xveins/tests/unit_tests.cpp+++ b/xveins/tests/unit_tests.cpp@@ -2,8 +2,9 @@ #include "core/common.h" #include "object/object.h"-#include "worktree/pathspec.h" #include "store/store.h"+#include "worktree/attributes.h"+#include "worktree/pathspec.h" #include <algorithm> #include <cstring>@@ -436,6 +437,51 @@ t.expect(!valid_path("/a")); t.expect(!valid_path("a//b")); t.expect(!valid_path("a/../b"));+}++void test_attributes(test::Context& t) {+ const TempDir temp;+ const std::string rules_text =+ "*.c txt eol:lf\n"+ "*.cmd txt eol:crlf\n"+ "*.ssa gen\n"+ "**/generated/* gen\n"+ "*.bin gen bin\n"+ "special.bin -gen\n";+ t.expect(write_file(temp.path / ".vnattribs", bytes_of(rules_text)));+ const AttributeRules rules = AttributeRules::load(temp.path);++ const auto source = rules.lookup("src/main.c");+ t.expect(source.text == AttributeText::Text);+ t.expect(source.eol == AttributeEol::Lf);+ t.expect(!source.generated);++ const auto script = rules.lookup("scripts/build.cmd");+ t.expect(script.text == AttributeText::Text);+ t.expect(script.eol == AttributeEol::Crlf);++ t.expect(rules.lookup("build/out.ssa").generated);+ t.expect(rules.lookup("pkg/generated/table.h").generated);++ const auto binary = rules.lookup("assets/blob.bin");+ t.expect(binary.generated);+ t.expect(binary.text == AttributeText::Binary);++ const auto overridden = rules.lookup("special.bin");+ t.expect(!overridden.generated);+ t.expect(overridden.text == AttributeText::Binary);++ const auto plain = rules.lookup("notes.txt");+ t.expect(plain.text == AttributeText::Auto);+ t.expect(plain.eol == AttributeEol::None);+ t.expect(!plain.generated);++ const auto lf = normalize_eol(bytes_of("a\r\nb\rc\nd"), AttributeEol::Lf);+ t.expect_eq(to_string(bytes_of(lf)), "a\nb\nc\nd");+ const auto crlf = normalize_eol(bytes_of("a\nb\rc\r\n"), AttributeEol::Crlf);+ t.expect_eq(to_string(bytes_of(crlf)), "a\r\nb\r\nc\r\n");+ const auto cr = normalize_eol(bytes_of("a\nb\r\nc\r"), AttributeEol::Cr);+ t.expect_eq(to_string(bytes_of(cr)), "a\rb\rc\r"); } void test_file_helpers(test::Context& t) {@@ -481,6 +527,7 @@ {"order_log_nodes", test_order_log_nodes}, {"pathspecs", test_pathspecs}, {"ignore_rules", test_ignore_rules},+ {"attributes", test_attributes}, {"file_helpers", test_file_helpers}, }}, };