aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2020-02-05 14:48:49 +0100
committerEelco Dolstra <edolstra@gmail.com>2020-02-05 14:48:49 +0100
commit9d7fb62db6e8ee6da7f8dbc5f5509271dc12f2ba (patch)
tree300a38d1bb67fb80b297b3d05e8e1425547fd2f8 /src/libexpr
parente2213d77a22a1d3d5d17167eb2760352760405e8 (diff)
Add option --commit-lock-file
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/flake/flake.cc19
-rw-r--r--src/libexpr/flake/flake.hh3
2 files changed, 16 insertions, 6 deletions
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<std::string>(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<InputPath, FlakeRef> inputOverrides;