A console script that allows you to easily update multiple git repositories at once
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

script.py 4.9 KiB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. # Released under the terms of the MIT License. See LICENSE for details.
  5. from __future__ import print_function
  6. import argparse
  7. import os
  8. import sys
  9. from colorama import init as color_init, Fore, Style
  10. from . import __version__
  11. from .config import (get_default_config_path, get_bookmarks, add_bookmarks,
  12. delete_bookmarks, list_bookmarks, clean_bookmarks)
  13. from .update import update_bookmarks, update_directories
  14. def _decode(path):
  15. """Decode the given string using the system's filesystem encoding."""
  16. if sys.version_info.major > 2:
  17. return path
  18. return path.decode(sys.getfilesystemencoding())
  19. def main():
  20. """Parse arguments and then call the appropriate function(s)."""
  21. parser = argparse.ArgumentParser(
  22. description="Easily update multiple git repositories at once.",
  23. epilog="""
  24. Both relative and absolute paths are accepted by all arguments.
  25. Direct bug reports and feature requests to
  26. https://github.com/earwig/git-repo-updater.""",
  27. add_help=False)
  28. group_u = parser.add_argument_group("updating repositories")
  29. group_b = parser.add_argument_group("bookmarking")
  30. group_m = parser.add_argument_group("miscellaneous")
  31. group_u.add_argument(
  32. 'directories_to_update', nargs="*", metavar="path", type=_decode,
  33. help="""update all repositories in this directory (or the directory
  34. itself, if it is a repo)""")
  35. group_u.add_argument(
  36. '-u', '--update', action="store_true", help="""update all bookmarks
  37. (default behavior when called without arguments)""")
  38. group_u.add_argument(
  39. '-c', '--current-only', action="store_true", help="""only fetch the
  40. remote tracked by the current branch instead of all remotes""")
  41. group_u.add_argument(
  42. '-f', '--fetch-only', action="store_true",
  43. help="only fetch remotes, don't try to fast-forward any branches")
  44. group_u.add_argument(
  45. '-p', '--prune', action="store_true", help="""after fetching, delete
  46. remote-tracking branches that no longer exist on their remote""")
  47. group_b.add_argument(
  48. '-a', '--add', dest="bookmarks_to_add", nargs="+", metavar="path",
  49. type=_decode, help="add directory(s) as bookmarks")
  50. group_b.add_argument(
  51. '-d', '--delete', dest="bookmarks_to_del", nargs="+", metavar="path",
  52. type=_decode,
  53. help="delete bookmark(s) (leaves actual directories alone)")
  54. group_b.add_argument(
  55. '-l', '--list', dest="list_bookmarks", action="store_true",
  56. help="list current bookmarks")
  57. group_b.add_argument(
  58. '-e', '--clean', '--cleanup', dest="clean_bookmarks",
  59. action="store_true", help="delete any bookmarks that don't exist")
  60. group_b.add_argument(
  61. '-b', '--bookmark-file', nargs="?", metavar="path", type=_decode,
  62. help="use a specific bookmark config file (default: {0})".format(
  63. get_default_config_path()))
  64. group_m.add_argument(
  65. '-h', '--help', action="help", help="show this help message and exit")
  66. group_m.add_argument(
  67. '-v', '--version', action="version",
  68. version="gitup " + __version__)
  69. # TODO: deprecated arguments, for removal in v1.0:
  70. parser.add_argument(
  71. '-m', '--merge', action="store_true", help=argparse.SUPPRESS)
  72. parser.add_argument(
  73. '-r', '--rebase', action="store_true", help=argparse.SUPPRESS)
  74. color_init(autoreset=True)
  75. args = parser.parse_args()
  76. update_args = args.current_only, args.fetch_only, args.prune
  77. print(Style.BRIGHT + "gitup" + Style.RESET_ALL + ": the git-repo-updater")
  78. print()
  79. # TODO: remove in v1.0
  80. if args.merge or args.rebase:
  81. print(Style.BRIGHT + Fore.YELLOW + "Warning:", "--merge and --rebase "
  82. "are deprecated. Branches are only updated if they\ntrack an "
  83. "upstream branch and can be safely fast-forwarded. Use "
  84. "--fetch-only to\navoid updating any branches.\n")
  85. if args.bookmark_file:
  86. args.bookmark_file = os.path.expanduser(args.bookmark_file)
  87. acted = False
  88. if args.bookmarks_to_add:
  89. add_bookmarks(args.bookmarks_to_add, args.bookmark_file)
  90. acted = True
  91. if args.bookmarks_to_del:
  92. delete_bookmarks(args.bookmarks_to_del, args.bookmark_file)
  93. acted = True
  94. if args.list_bookmarks:
  95. list_bookmarks(args.bookmark_file)
  96. acted = True
  97. if args.clean_bookmarks:
  98. clean_bookmarks(args.bookmark_file)
  99. acted = True
  100. if args.directories_to_update:
  101. update_directories(args.directories_to_update, update_args)
  102. acted = True
  103. if args.update or not acted:
  104. update_bookmarks(get_bookmarks(args.bookmark_file), update_args)
  105. def run():
  106. """Thin wrapper for main() that catches KeyboardInterrupts."""
  107. try:
  108. main()
  109. except KeyboardInterrupt:
  110. print("Stopped by user.")