From a51b99d98abfa9f68c77b822c12aa69d51b60ed7 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 11 Jun 2016 17:34:08 -0400 Subject: [PATCH] Add a --cleanup flag to remove old bookmarks (closes #26). --- CHANGELOG | 3 ++- gitup/config.py | 21 +++++++++++++++++++-- gitup/script.py | 8 +++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 1f3d49b..d2a32a7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,7 +2,8 @@ v0.3.1 (unreleased): - Added a `--prune` flag to delete remote-tracking branches that no longer exist on their remote after fetching. -- Added a '--bookmark-file' option to support multiple bookmark config files. +- Added a `--bookmark-file` flag to support multiple bookmark config files. +- Added a `--cleanup` flag to remove old bookmarks that don't exist. - Cleaned up the bookmark file format, fixing a related Windows bug. The script will automatically migrate to the new one. - Fixed a bug related to Python 3 compatibility. diff --git a/gitup/config.py b/gitup/config.py index 68b6c81..195dda7 100644 --- a/gitup/config.py +++ b/gitup/config.py @@ -12,7 +12,7 @@ from colorama import Fore, Style from .migrate import run_migrations __all__ = ["get_default_config_path", "get_bookmarks", "add_bookmarks", - "delete_bookmarks", "list_bookmarks"] + "delete_bookmarks", "list_bookmarks", "clean_bookmarks"] YELLOW = Fore.YELLOW + Style.BRIGHT RED = Fore.RED + Style.BRIGHT @@ -106,10 +106,27 @@ def delete_bookmarks(paths, config_path=None): def list_bookmarks(config_path=None): """Print all of our current bookmarks.""" - bookmarks = get_bookmarks(config_path=config_path) + bookmarks = _load_config_file(config_path) if bookmarks: print(YELLOW + "Current bookmarks:") for bookmark_path in bookmarks: print(INDENT1, bookmark_path) else: print("You have no bookmarks to display.") + +def clean_bookmarks(config_path=None): + """Delete any bookmarks that don't exist.""" + bookmarks = _load_config_file(config_path) + if not bookmarks: + return + + delete = [path for path in bookmarks if not os.path.isdir(path)] + if not delete: + return + + bookmarks = [path for path in bookmarks if path not in delete] + _save_config_file(bookmarks, config_path) + + print(YELLOW + "Deleted bookmarks:") + for path in delete: + print(INDENT1, path) diff --git a/gitup/script.py b/gitup/script.py index 77740ea..514f3ad 100644 --- a/gitup/script.py +++ b/gitup/script.py @@ -13,7 +13,7 @@ from colorama import init as color_init, Fore, Style from . import __version__ from .config import (get_default_config_path, get_bookmarks, add_bookmarks, - delete_bookmarks, list_bookmarks) + delete_bookmarks, list_bookmarks, clean_bookmarks) from .update import update_bookmarks, update_directories def _decode(path): @@ -64,6 +64,9 @@ def main(): '-l', '--list', dest="list_bookmarks", action="store_true", help="list current bookmarks") group_b.add_argument( + '-e', '--clean', '--cleanup', dest="clean_bookmarks", + action="store_true", help="delete any bookmarks that don't exist") + group_b.add_argument( '-b', '--bookmark-file', nargs="?", metavar="path", type=_decode, help="use a specific bookmark config file (default: {0})".format( get_default_config_path())) @@ -107,6 +110,9 @@ def main(): if args.list_bookmarks: list_bookmarks(args.bookmark_file) acted = True + if args.clean_bookmarks: + clean_bookmarks(args.bookmark_file) + acted = True if args.directories_to_update: update_directories(args.directories_to_update, update_args) acted = True