From 9d7fb62db6e8ee6da7f8dbc5f5509271dc12f2ba Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 5 Feb 2020 14:48:49 +0100 Subject: Add option --commit-lock-file --- src/libexpr/flake/flake.cc | 19 +++++++++++++------ src/libexpr/flake/flake.hh | 3 +++ 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'src/libexpr') diff --git a/src/libexpr/flake/flake.cc b/src/libexpr/flake/flake.cc index a0f41e6b9..5f64b76d9 100644 --- a/src/libexpr/flake/flake.cc +++ b/src/libexpr/flake/flake.cc @@ -321,15 +321,15 @@ static std::string diffLockFiles(const LockedInputs & oldLocks, const LockedInpu while (i != oldFlat.end() || j != newFlat.end()) { if (j != newFlat.end() && (i == oldFlat.end() || i->first > j->first)) { - res += fmt(" added '%s': '%s'\n", concatStringsSep("/", j->first), j->second->lockedRef); + res += fmt("* Added '%s': '%s'\n", concatStringsSep("/", j->first), j->second->lockedRef); ++j; } else if (i != oldFlat.end() && (j == newFlat.end() || i->first < j->first)) { - res += fmt(" removed '%s'\n", concatStringsSep("/", i->first)); + res += fmt("* Removed '%s'\n", concatStringsSep("/", i->first)); ++i; } else { if (!(i->second->lockedRef == j->second->lockedRef)) { assert(i->second->lockedRef.to_string() != j->second->lockedRef.to_string()); - res += fmt(" updated '%s': '%s' -> '%s'\n", + res += fmt("* Updated '%s': '%s' -> '%s'\n", concatStringsSep("/", i->first), i->second->lockedRef, j->second->lockedRef); @@ -558,8 +558,10 @@ LockedFlake lockFlake( /* Check whether we need to / can write the new lock file. */ if (!(newLockFile == oldLockFile)) { + auto diff = diffLockFiles(oldLockFile, newLockFile); + if (!(oldLockFile == LockFile())) - printInfo("inputs of flake '%s' changed:\n%s", topRef, chomp(diffLockFiles(oldLockFile, newLockFile))); + printInfo("inputs of flake '%s' changed:\n%s", topRef, chomp(diff)); if (lockFlags.writeLockFile) { if (auto sourcePath = topRef.input->getSourcePath()) { @@ -572,7 +574,9 @@ LockedFlake lockFlake( auto path = *sourcePath + (topRef.subdir == "" ? "" : "/" + topRef.subdir) + "/flake.lock"; - if (pathExists(path)) + bool lockFileExists = pathExists(path); + + if (lockFileExists) warn("updating lock file '%s'", path); else warn("creating lock file '%s'", path); @@ -580,7 +584,10 @@ LockedFlake lockFlake( newLockFile.write(path); topRef.input->markChangedFile( - (topRef.subdir == "" ? "" : topRef.subdir + "/") + "flake.lock"); + (topRef.subdir == "" ? "" : topRef.subdir + "/") + "flake.lock", + lockFlags.commitLockFile + ? std::optional(fmt("flake.lock: %s\n\nFlake input changes:\n\n%s", lockFileExists ? "Update" : "Add", diff)) + : std::nullopt); /* Rewriting the lockfile changed the top-level repo, so we should re-read it. FIXME: we could diff --git a/src/libexpr/flake/flake.hh b/src/libexpr/flake/flake.hh index 4eb36236c..7c71f3383 100644 --- a/src/libexpr/flake/flake.hh +++ b/src/libexpr/flake/flake.hh @@ -78,6 +78,9 @@ struct LockFlags allowed. */ bool allowMutable = true; + /* Whether to commit changes to flake.lock. */ + bool commitLockFile = false; + /* Flake inputs to be overriden. */ std::map inputOverrides; -- cgit v1.2.3