-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nix hash path
, and preperatory refactors
- Proper `parse` and `render` functions for `FileIngestionMethod` and `ContentAddressMethod` Including unit tests! Older methods with same names that operate on on method + algo pair (for old-style `<method>:algo`) are renamed to `*WithAlgo`.) - `nix store add` supports text hashing With functional test ensuring it matches `builtins.toFile`. - Factored-out flags for both - Move all common reusable flags to `libcmd` - They are not part of the *definition* of the CLI infra, just a usag of it. - The `libstore` flag couldn't go in `args.hh` in libutil anyways, would be awkward for it to live alone - Shuffle around `Cmd*` hierarchy so flags for deprecated commands don't end up on the new ones - Split `tests/functional/hash.sh` into `hash-path.sh` and `has-convert.sh` for performance and conceptual clarity.
- Loading branch information
1 parent
da1aae2
commit ddb5764
Showing
21 changed files
with
589 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#include "misc-store-flags.hh" | ||
|
||
namespace nix::flag | ||
{ | ||
|
||
static void hashFormatCompleter(AddCompletions & completions, size_t index, std::string_view prefix) | ||
{ | ||
for (auto & format : hashFormats) { | ||
if (hasPrefix(format, prefix)) { | ||
completions.add(format); | ||
} | ||
} | ||
} | ||
|
||
Args::Flag hashFormatWithDefault(std::string && longName, HashFormat * hf) | ||
{ | ||
assert(*hf == nix::HashFormat::SRI); | ||
return Args::Flag { | ||
.longName = std::move(longName), | ||
.description = "hash format ('base16', 'nix32', 'base64', 'sri'). Default: 'sri'", | ||
.labels = {"hash-format"}, | ||
.handler = {[hf](std::string s) { | ||
*hf = parseHashFormat(s); | ||
}}, | ||
.completer = hashFormatCompleter, | ||
}; | ||
} | ||
|
||
Args::Flag hashFormatOpt(std::string && longName, std::optional<HashFormat> * ohf) | ||
{ | ||
return Args::Flag { | ||
.longName = std::move(longName), | ||
.description = "hash format ('base16', 'nix32', 'base64', 'sri').", | ||
.labels = {"hash-format"}, | ||
.handler = {[ohf](std::string s) { | ||
*ohf = std::optional<HashFormat>{parseHashFormat(s)}; | ||
}}, | ||
.completer = hashFormatCompleter, | ||
}; | ||
} | ||
|
||
static void hashAlgoCompleter(AddCompletions & completions, size_t index, std::string_view prefix) | ||
{ | ||
for (auto & algo : hashAlgorithms) | ||
if (hasPrefix(algo, prefix)) | ||
completions.add(algo); | ||
} | ||
|
||
Args::Flag hashAlgo(std::string && longName, HashAlgorithm * ha) | ||
{ | ||
return Args::Flag { | ||
.longName = std::move(longName), | ||
.description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')", | ||
.labels = {"hash-algo"}, | ||
.handler = {[ha](std::string s) { | ||
*ha = parseHashAlgo(s); | ||
}}, | ||
.completer = hashAlgoCompleter, | ||
}; | ||
} | ||
|
||
Args::Flag hashAlgoOpt(std::string && longName, std::optional<HashAlgorithm> * oha) | ||
{ | ||
return Args::Flag { | ||
.longName = std::move(longName), | ||
.description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512'). Optional as can also be gotten from SRI hash itself.", | ||
.labels = {"hash-algo"}, | ||
.handler = {[oha](std::string s) { | ||
*oha = std::optional<HashAlgorithm>{parseHashAlgo(s)}; | ||
}}, | ||
.completer = hashAlgoCompleter, | ||
}; | ||
} | ||
|
||
Args::Flag fileIngestionMethod(FileIngestionMethod * method) | ||
{ | ||
return Args::Flag { | ||
.longName = "mode", | ||
// FIXME indentation carefully made for context, this is messed up. | ||
.description = R"( | ||
How to compute the hash of the input. | ||
One of: | ||
- `nar` (the default): Serialises the input as an archive (following the [_Nix Archive Format_](https://edolstra.github.io/pubs/phd-thesis.pdf#page=101)) and passes that to the hash function. | ||
- `flat`: Assumes that the input is a single file and directly passes it to the hash function; | ||
)", | ||
.labels = {"file-ingestion-method"}, | ||
.handler = {[method](std::string s) { | ||
*method = parseFileIngestionMethod(s); | ||
}}, | ||
}; | ||
} | ||
|
||
Args::Flag contentAddressMethod(ContentAddressMethod * method) | ||
{ | ||
return Args::Flag { | ||
.longName = "mode", | ||
// FIXME indentation carefully made for context, this is messed up. | ||
.description = R"( | ||
How to compute the content-address of the store object. | ||
One of: | ||
- `nar` (the default): Serialises the input as an archive (following the [_Nix Archive Format_](https://edolstra.github.io/pubs/phd-thesis.pdf#page=101)) and passes that to the hash function. | ||
- `flat`: Assumes that the input is a single file and directly passes it to the hash function; | ||
- `text`: Like `flat`, but used for misc things like | ||
[derivations](@docroot@/glossary.md#store-derivation) serialized in store object, | ||
[`builtins.toFile`](@docroot@/language/builtins.html#builtins-toFile). | ||
Less common. | ||
)", | ||
.labels = {"content-address-method"}, | ||
.handler = {[method](std::string s) { | ||
*method = ContentAddressMethod::parse(s); | ||
}}, | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "args.hh" | ||
#include "content-address.hh" | ||
|
||
namespace nix::flag { | ||
|
||
Args::Flag hashAlgo(std::string && longName, HashAlgorithm * ha); | ||
Args::Flag hashAlgoOpt(std::string && longName, std::optional<HashAlgorithm> * oha); | ||
Args::Flag hashFormatWithDefault(std::string && longName, HashFormat * hf); | ||
Args::Flag hashFormatOpt(std::string && longName, std::optional<HashFormat> * ohf); | ||
Args::Flag fileIngestionMethod(FileIngestionMethod * method); | ||
Args::Flag contentAddressMethod(ContentAddressMethod * method); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.