@@ -163,8 +163,11 @@ struct CmdToBase : Command
163163 HashFormat hashFormat;
164164 std::optional<HashAlgorithm> hashAlgo;
165165 std::vector<std::string> args;
166+ bool legacyCli;
166167
167- CmdToBase (HashFormat hashFormat) : hashFormat(hashFormat)
168+ CmdToBase (HashFormat hashFormat, bool legacyCli = false )
169+ : hashFormat(hashFormat)
170+ , legacyCli(legacyCli)
168171 {
169172 addFlag (flag::hashAlgoOpt (" type" , &hashAlgo));
170173 expectArgs (" strings" , &args);
@@ -181,7 +184,8 @@ struct CmdToBase : Command
181184
182185 void run () override
183186 {
184- warn (" The old format conversion sub commands of `nix hash` were deprecated in favor of `nix hash convert`." );
187+ if (!legacyCli)
188+ warn (" The old format conversion subcommands of `nix hash` were deprecated in favor of `nix hash convert`." );
185189 for (const auto & s : args)
186190 logger->cout (Hash::parseAny (s, hashAlgo).to_string (hashFormat, hashFormat == HashFormat::SRI));
187191 }
@@ -222,11 +226,18 @@ struct CmdHashConvert : Command
222226 Category category () override { return catUtility; }
223227
224228 void run () override {
225- for (const auto & s: hashStrings) {
226- Hash h = Hash::parseAny (s, algo);
227- if (from && h.to_string (*from, from == HashFormat::SRI) != s) {
229+ for (const auto & s : hashStrings) {
230+ Hash h =
231+ from == HashFormat::SRI
232+ ? Hash::parseSRI (s)
233+ : Hash::parseAny (s, algo);
234+ if (from
235+ && from != HashFormat::SRI
236+ && h.to_string (*from, false ) !=
237+ (from == HashFormat::Base16 ? toLower (s) : s))
238+ {
228239 auto from_as_string = printHashFormat (*from);
229- throw BadHash (" input hash '%s' does not have the expected format '--from %s'" , s, from_as_string);
240+ throw BadHash (" input hash '%s' does not have the expected format for '--from %s'" , s, from_as_string);
230241 }
231242 logger->cout (h.to_string (to, to == HashFormat::SRI));
232243 }
@@ -321,7 +332,7 @@ static int compatNixHash(int argc, char * * argv)
321332 }
322333
323334 else {
324- CmdToBase cmd (hashFormat);
335+ CmdToBase cmd (hashFormat, true );
325336 cmd.args = ss;
326337 if (hashAlgo.has_value ()) cmd.hashAlgo = hashAlgo;
327338 cmd.run ();
0 commit comments