1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-22 18:29:36 +01:00

chore: improve comments and factored out variables when moving iterator

This commit is contained in:
nenikitov 2025-09-25 12:05:45 -04:00
parent d9c62f3f17
commit f2587b713b
4 changed files with 18 additions and 13 deletions

View file

@ -94,7 +94,7 @@ generations of each profile, do
$ nix-collect-garbage -d
```
## Keep most-recent by time (number fo days) and trim by amount
## Keep most-recent by time (number of days) and trim by amount
This command will delete generations older than a week if possible, while keeping an amount of generations between `10` and `20`.

View file

@ -135,17 +135,17 @@ void deleteGenerations(const Path & profile, const std::set<GenerationNumber> &
*
* Examples:
* - All parameters are nullopt
* No generations are deleted
* - keepMax is 5
* No generations are deleted
* No generations are deleted.
* - keepMin is 5
* No generations are deleted, only keepMax and olderThan delete generations.
* - keepMax is 10
* 10 generations after the current one are kept, the rest is deleted.
* 10 most recent generations after the current one are kept, the rest is deleted.
* - olderThan is 2025-09-16
* Generations older than 2025-09-16 are deleted
* Generations older than 2025-09-16 are deleted.
* - olderThan is 2025-09-16, keepMin is 5, keepMax is 10 -
* Will try to delete generations older than 2025-09-16.
* If there are more than 10 generations to be kept, continues to delete old generations until there is 10.
* If there are less than 5 generations to be kept, preserves older generations until there is 5.
* If there are more than 10 generations to be kept, continues to delete old generations until there are 10.
* If there are less than 5 generations to be kept, preserves the most recent of generations to be deleted until there are 5.
*
* @param profile The profile, specified by its name and location combined into a path, whose generations we want to
* delete.

View file

@ -166,7 +166,7 @@ static inline void iterDropUntil(Generations & gens, auto && i, auto && cond)
void deleteGenerationsFilter(const Path & profile, std::optional<time_t> olderThan, std::optional<GenerationNumber> keepMin, std::optional<GenerationNumber> keepMax, bool dryRun)
{
if (keepMin.has_value() && keepMax.has_value() && *keepMin > *keepMax)
throw Error("Keep min cannot be greater than keep max");
throw Error("--keep-min cannot be greater than --keep-max");
PathLocks lock;
lockProfile(lock, profile);
@ -197,13 +197,18 @@ void deleteGenerationsFilter(const Path & profile, std::optional<time_t> olderTh
iterDropUntil(gens, older, [&](auto & g) { return g.creationTime < *olderThan; });
}
// Find first generation to delete by clamping
// Find first generation to delete by clamping between keepMin and keepMax
auto toDelete = older;
for (int i = std::distance(gens.rbegin(), older) - std::distance(gens.rbegin(), end); i > 0; --i)
auto clampBackward = std::distance(gens.rbegin(), older) - std::distance(gens.rbegin(), end);
for (int i = clampBackward; i > 0; --i)
--toDelete;
for (int i = std::distance(gens.rbegin(), start) - std::distance(gens.rbegin(), older); i > 0; --i)
auto clampForward = std::distance(gens.rbegin(), start) - std::distance(gens.rbegin(), older);
for (int i = clampForward; i > 0; --i)
++toDelete;
// Delete
for (; toDelete != gens.rend(); ++toDelete)
deleteGeneration2(profile, toDelete->number, dryRun);
}

View file

@ -83,7 +83,7 @@ static int main_nix_collect_garbage(int argc, char ** argv)
deleteOlderThan = std::optional<time_t> { parseOlderThanTimeSpec(getArg(*arg, arg, end)) };
} else if (*arg == "--keep-min")
keepMin = std::optional<GenerationNumber> { std::max(getIntArg<GenerationNumber>(*arg, arg, end, false), (GenerationNumber) 1) };
else if (*arg == "--keep-max"){
else if (*arg == "--keep-max") {
removeOld = true;
keepMax = std::optional<GenerationNumber> { std::max(getIntArg<GenerationNumber>(*arg, arg, end, false), (GenerationNumber) 1) };
}