]> arthur.barton.de Git - bup.git/blobdiff - cmd/prune-older-cmd.py
Use next(it), not it.next() and drop the helpers fallback
[bup.git] / cmd / prune-older-cmd.py
index b340e017be1e27ff2cb78d6718f04e993427f5c2..208ddf73bb8d6f69ded65bfbd99fa538a91929de 100755 (executable)
@@ -20,7 +20,7 @@ from bup.rm import bup_rm
 
 def branches(refnames=()):
     return ((name[11:], sha) for (name,sha)
-            in git.list_refs(refnames=('refs/heads/' + n for n in refnames),
+            in git.list_refs(patterns=('refs/heads/' + n for n in refnames),
                              limit_to_heads=True))
 
 def save_name(branch, utc):
@@ -32,14 +32,11 @@ def classify_saves(saves, period_start):
     The ids are binary hashes.
     """
 
-    def retain_oldest_in_region(region):
-        prev = None
-        for save in region:
-            if prev:
-                yield False, prev
-            prev = save
-        if prev:
-            yield True, prev
+    def retain_newest_in_region(region):
+        for save in region[0:1]:
+            yield True, save
+        for save in region[1:]:
+            yield False, save
 
     matches, rest = partition(lambda s: s[0] >= period_start['all'], saves)
     for save in matches:
@@ -49,12 +46,18 @@ def classify_saves(saves, period_start):
                  (period_start['monthlies'], lambda s: localtime(s[0]).tm_mon),
                  (period_start['yearlies'], lambda s: localtime(s[0]).tm_year))
 
+    # Break the decreasing utc sorted saves up into the respective
+    # period ranges (dailies, monthlies, ...).  Within each range,
+    # group the saves by the period scale (days, months, ...), and
+    # then yield a "keep" action (True, utc) for the newest save in
+    # each group, and a "drop" action (False, utc) for the rest.
     for pstart, time_region_id in tm_ranges:
         matches, rest = partition(lambda s: s[0] >= pstart, rest)
         for region_id, region_saves in groupby(matches, time_region_id):
-            for action in retain_oldest_in_region(region_saves):
+            for action in retain_newest_in_region(list(region_saves)):
                 yield action
 
+    # Finally, drop any saves older than the specified periods
     for save in rest:
         yield False, save
 
@@ -63,9 +66,9 @@ optspec = """
 bup prune-older [options...] [BRANCH...]
 --
 keep-all-for=       retain all saves within the PERIOD
-keep-dailies-for=   retain the oldest save per day within the PERIOD
-keep-monthlies-for= retain the oldest save per month within the PERIOD
-keep-yearlies-for=  retain the oldest save per year within the PERIOD
+keep-dailies-for=   retain the newest save per day within the PERIOD
+keep-monthlies-for= retain the newest save per month within the PERIOD
+keep-yearlies-for=  retain the newest save per year within the PERIOD
 wrt=                end all periods at this number of seconds since the epoch
 pretend       don't prune, just report intended actions to standard output
 gc            collect garbage after removals [1]