From 87b32bab05ff91981c8847d66cd5502feb44f3b5 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sat, 28 Mar 2020 23:22:10 +0000 Subject: Use `enum struct` and drop prefixes This does a few enums; the rest will be gotten in subsequent commits. --- src/nix/hash.cc | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'src/nix/hash.cc') diff --git a/src/nix/hash.cc b/src/nix/hash.cc index 0cc523f50..deced3d11 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -11,18 +11,18 @@ struct CmdHash : Command { enum Mode { mFile, mPath }; Mode mode; - Base base = SRI; + Base base = Base::SRI; bool truncate = false; - HashType ht = htSHA256; + HashType ht = HashType::SHA256; std::vector paths; std::optional modulus; CmdHash(Mode mode) : mode(mode) { - mkFlag(0, "sri", "print hash in SRI format", &base, SRI); - mkFlag(0, "base64", "print hash in base-64", &base, Base64); - mkFlag(0, "base32", "print hash in base-32 (Nix-specific)", &base, Base32); - mkFlag(0, "base16", "print hash in base-16", &base, Base16); + mkFlag(0, "sri", "print hash in Base::SRI format", &base, Base::SRI); + mkFlag(0, "base64", "print hash in base-64", &base, Base::Base64); + mkFlag(0, "base32", "print hash in base-32 (Nix-specific)", &base, Base::Base32); + mkFlag(0, "base16", "print hash in base-16", &base, Base::Base16); mkFlag() .longName("type") .mkHashTypeFlag(&ht); @@ -61,7 +61,7 @@ struct CmdHash : Command Hash h = hashSink->finish().first; if (truncate && h.hashSize > 20) h = compressHash(h, 20); std::cout << format("%1%\n") % - h.to_string(base, base == SRI); + h.to_string(base, base == Base::SRI); } } }; @@ -72,7 +72,7 @@ static RegisterCommand r2("hash-path", [](){ return make_ref(CmdHash::m struct CmdToBase : Command { Base base; - HashType ht = htUnknown; + HashType ht = HashType::Unknown; std::vector args; CmdToBase(Base base) : base(base) @@ -86,28 +86,28 @@ struct CmdToBase : Command std::string description() override { return fmt("convert a hash to %s representation", - base == Base16 ? "base-16" : - base == Base32 ? "base-32" : - base == Base64 ? "base-64" : - "SRI"); + base == Base::Base16 ? "base-16" : + base == Base::Base32 ? "base-32" : + base == Base::Base64 ? "base-64" : + "Base::SRI"); } void run() override { for (auto s : args) - std::cout << fmt("%s\n", Hash(s, ht).to_string(base, base == SRI)); + std::cout << fmt("%s\n", Hash(s, ht).to_string(base, base == Base::SRI)); } }; -static RegisterCommand r3("to-base16", [](){ return make_ref(Base16); }); -static RegisterCommand r4("to-base32", [](){ return make_ref(Base32); }); -static RegisterCommand r5("to-base64", [](){ return make_ref(Base64); }); -static RegisterCommand r6("to-sri", [](){ return make_ref(SRI); }); +static RegisterCommand r3("to-base16", [](){ return make_ref(Base::Base16); }); +static RegisterCommand r4("to-base32", [](){ return make_ref(Base::Base32); }); +static RegisterCommand r5("to-base64", [](){ return make_ref(Base::Base64); }); +static RegisterCommand r6("to-sri", [](){ return make_ref(Base::SRI); }); /* Legacy nix-hash command. */ static int compatNixHash(int argc, char * * argv) { - HashType ht = htMD5; + HashType ht = HashType::MD5; bool flat = false; bool base32 = false; bool truncate = false; @@ -125,7 +125,7 @@ static int compatNixHash(int argc, char * * argv) else if (*arg == "--type") { string s = getArg(*arg, arg, end); ht = parseHashType(s); - if (ht == htUnknown) + if (ht == HashType::Unknown) throw UsageError(format("unknown hash type '%1%'") % s); } else if (*arg == "--to-base16") op = opTo16; @@ -140,14 +140,14 @@ static int compatNixHash(int argc, char * * argv) if (op == opHash) { CmdHash cmd(flat ? CmdHash::mFile : CmdHash::mPath); cmd.ht = ht; - cmd.base = base32 ? Base32 : Base16; + cmd.base = base32 ? Base::Base32 : Base::Base16; cmd.truncate = truncate; cmd.paths = ss; cmd.run(); } else { - CmdToBase cmd(op == opTo32 ? Base32 : Base16); + CmdToBase cmd(op == opTo32 ? Base::Base32 : Base::Base16); cmd.args = ss; cmd.ht = ht; cmd.run(); -- cgit v1.2.3 From 0e9438b6d381a87946ddda8d4bdd06707f9b0a48 Mon Sep 17 00:00:00 2001 From: Carlo Nucera Date: Mon, 1 Jun 2020 17:32:27 -0400 Subject: Create new file-hash files --- src/nix/hash.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'src/nix/hash.cc') diff --git a/src/nix/hash.cc b/src/nix/hash.cc index 3362ffd0d..ec23b77bc 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -1,5 +1,6 @@ #include "command.hh" #include "hash.hh" +#include "file-hash.hh" #include "legacy.hh" #include "shared.hh" #include "references.hh" -- cgit v1.2.3 From 450dcf2c1b60a36f5ffeab2411805287d122bcdd Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 2 Jun 2020 15:52:13 +0000 Subject: Remove `HashType::Unknown` Instead, `Hash` uses `std::optional`. In the future, we may also make `Hash` itself require a known hash type, encoraging people to use `std::optional` instead. --- src/nix/hash.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/nix/hash.cc') diff --git a/src/nix/hash.cc b/src/nix/hash.cc index 3362ffd0d..4980cd198 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -79,7 +79,7 @@ static RegisterCommand r2("hash-path", [](){ return make_ref(FileIngest struct CmdToBase : Command { Base base; - HashType ht = HashType::Unknown; + HashType ht; std::vector args; CmdToBase(Base base) : base(base) @@ -132,8 +132,6 @@ static int compatNixHash(int argc, char * * argv) else if (*arg == "--type") { string s = getArg(*arg, arg, end); ht = parseHashType(s); - if (ht == HashType::Unknown) - throw UsageError(format("unknown hash type '%1%'") % s); } else if (*arg == "--to-base16") op = opTo16; else if (*arg == "--to-base32") op = opTo32; -- cgit v1.2.3 From c502119fd3b9673e966d5c34ec42cbe18baa17b9 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 2 Jun 2020 18:05:26 +0000 Subject: to-base supports parsing SRI hashes, so make type flag optional --- src/nix/hash.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nix/hash.cc') diff --git a/src/nix/hash.cc b/src/nix/hash.cc index 4980cd198..0e24bbaed 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -79,12 +79,12 @@ static RegisterCommand r2("hash-path", [](){ return make_ref(FileIngest struct CmdToBase : Command { Base base; - HashType ht; + std::optional ht; std::vector args; CmdToBase(Base base) : base(base) { - addFlag(Flag::mkHashTypeFlag("type", &ht)); + addFlag(Flag::mkHashTypeFlag("type", &*ht)); expectArgs("strings", &args); } -- cgit v1.2.3 From c664e68b87a3e9e41c4471276886da71793b2d85 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 2 Jun 2020 18:25:32 +0000 Subject: Fix to-base --type handler to correctly set std::optional flag Now that we have a separate flag function, also describe why it is optional. --- src/nix/hash.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nix/hash.cc') diff --git a/src/nix/hash.cc b/src/nix/hash.cc index 0e24bbaed..d1b5cca72 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -84,7 +84,7 @@ struct CmdToBase : Command CmdToBase(Base base) : base(base) { - addFlag(Flag::mkHashTypeFlag("type", &*ht)); + addFlag(Flag::mkHashTypeOptFlag("type", &ht)); expectArgs("strings", &args); } -- cgit v1.2.3 From fd2eb41e6433e72516ae149949b8b0050305293d Mon Sep 17 00:00:00 2001 From: Carlo Nucera Date: Tue, 2 Jun 2020 15:44:58 -0400 Subject: Move file-hash to content-address --- src/nix/hash.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nix/hash.cc') diff --git a/src/nix/hash.cc b/src/nix/hash.cc index ec23b77bc..26bbcef1c 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -1,6 +1,6 @@ #include "command.hh" #include "hash.hh" -#include "file-hash.hh" +#include "content-address.hh" #include "legacy.hh" #include "shared.hh" #include "references.hh" -- cgit v1.2.3 From 15abb2aa2ba7de06a86e05511f81633616e17d87 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 18 Jun 2020 22:09:22 +0000 Subject: Revert the `enum struct` change Not a regular git revert as there have been many merges and things. --- src/nix/hash.cc | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'src/nix/hash.cc') diff --git a/src/nix/hash.cc b/src/nix/hash.cc index d1b5cca72..f435192fc 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -10,18 +10,18 @@ using namespace nix; struct CmdHash : Command { FileIngestionMethod mode; - Base base = Base::SRI; + Base base = SRI; bool truncate = false; - HashType ht = HashType::SHA256; + HashType ht = htSHA256; std::vector paths; std::optional modulus; CmdHash(FileIngestionMethod mode) : mode(mode) { - mkFlag(0, "sri", "print hash in Base::SRI format", &base, Base::SRI); - mkFlag(0, "base64", "print hash in base-64", &base, Base::Base64); - mkFlag(0, "base32", "print hash in base-32 (Nix-specific)", &base, Base::Base32); - mkFlag(0, "base16", "print hash in base-16", &base, Base::Base16); + mkFlag(0, "sri", "print hash in SRI format", &base, SRI); + mkFlag(0, "base64", "print hash in base-64", &base, Base64); + mkFlag(0, "base32", "print hash in base-32 (Nix-specific)", &base, Base32); + mkFlag(0, "base16", "print hash in base-16", &base, Base16); addFlag(Flag::mkHashTypeFlag("type", &ht)); #if 0 mkFlag() @@ -68,7 +68,7 @@ struct CmdHash : Command Hash h = hashSink->finish().first; if (truncate && h.hashSize > 20) h = compressHash(h, 20); - logger->stdout(h.to_string(base, base == Base::SRI)); + logger->stdout(h.to_string(base, base == SRI)); } } }; @@ -91,10 +91,10 @@ struct CmdToBase : Command std::string description() override { return fmt("convert a hash to %s representation", - base == Base::Base16 ? "base-16" : - base == Base::Base32 ? "base-32" : - base == Base::Base64 ? "base-64" : - "Base::SRI"); + base == Base16 ? "base-16" : + base == Base32 ? "base-32" : + base == Base64 ? "base-64" : + "SRI"); } Category category() override { return catUtility; } @@ -102,19 +102,19 @@ struct CmdToBase : Command void run() override { for (auto s : args) - logger->stdout(Hash(s, ht).to_string(base, base == Base::SRI)); + logger->stdout(Hash(s, ht).to_string(base, base == SRI)); } }; -static RegisterCommand r3("to-base16", [](){ return make_ref(Base::Base16); }); -static RegisterCommand r4("to-base32", [](){ return make_ref(Base::Base32); }); -static RegisterCommand r5("to-base64", [](){ return make_ref(Base::Base64); }); -static RegisterCommand r6("to-sri", [](){ return make_ref(Base::SRI); }); +static RegisterCommand r3("to-base16", [](){ return make_ref(Base16); }); +static RegisterCommand r4("to-base32", [](){ return make_ref(Base32); }); +static RegisterCommand r5("to-base64", [](){ return make_ref(Base64); }); +static RegisterCommand r6("to-sri", [](){ return make_ref(SRI); }); /* Legacy nix-hash command. */ static int compatNixHash(int argc, char * * argv) { - HashType ht = HashType::MD5; + HashType ht = htMD5; bool flat = false; bool base32 = false; bool truncate = false; @@ -145,14 +145,14 @@ static int compatNixHash(int argc, char * * argv) if (op == opHash) { CmdHash cmd(flat ? FileIngestionMethod::Flat : FileIngestionMethod::Recursive); cmd.ht = ht; - cmd.base = base32 ? Base::Base32 : Base::Base16; + cmd.base = base32 ? Base32 : Base16; cmd.truncate = truncate; cmd.paths = ss; cmd.run(); } else { - CmdToBase cmd(op == opTo32 ? Base::Base32 : Base::Base16); + CmdToBase cmd(op == opTo32 ? Base32 : Base16); cmd.args = ss; cmd.ht = ht; cmd.run(); -- cgit v1.2.3