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.

143 lines
5.6 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2018 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, run_command
  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_a = parser.add_argument_group("advanced")
  31. group_m = parser.add_argument_group("miscellaneous")
  32. group_u.add_argument(
  33. 'directories_to_update', nargs="*", metavar="path", type=_decode,
  34. help="""update this repository, or all repositories it contains
  35. (if not a repo directly)""")
  36. group_u.add_argument(
  37. '-u', '--update', action="store_true", help="""update all bookmarks
  38. (default behavior when called without arguments)""")
  39. group_u.add_argument(
  40. '-t', '--depth', dest="max_depth", metavar="n", type=int, default=3,
  41. help="""max recursion depth when searching for repos in subdirectories
  42. (default: 3; use 0 for no recursion, or -1 for unlimited)""")
  43. group_u.add_argument(
  44. '-c', '--current-only', action="store_true", help="""only fetch the
  45. remote tracked by the current branch instead of all remotes""")
  46. group_u.add_argument(
  47. '-f', '--fetch-only', action="store_true",
  48. help="only fetch remotes, don't try to fast-forward any branches")
  49. group_u.add_argument(
  50. '-p', '--prune', action="store_true", help="""after fetching, delete
  51. remote-tracking branches that no longer exist on their remote""")
  52. group_b.add_argument(
  53. '-a', '--add', dest="bookmarks_to_add", nargs="+", metavar="path",
  54. type=_decode, help="add directory(s) as bookmarks")
  55. group_b.add_argument(
  56. '-d', '--delete', dest="bookmarks_to_del", nargs="+", metavar="path",
  57. type=_decode,
  58. help="delete bookmark(s) (leaves actual directories alone)")
  59. group_b.add_argument(
  60. '-l', '--list', dest="list_bookmarks", action="store_true",
  61. help="list current bookmarks")
  62. group_b.add_argument(
  63. '-n', '--clean', '--cleanup', dest="clean_bookmarks",
  64. action="store_true", help="delete any bookmarks that don't exist")
  65. group_b.add_argument(
  66. '-b', '--bookmark-file', nargs="?", metavar="path", type=_decode,
  67. help="use a specific bookmark config file (default: {0})".format(
  68. get_default_config_path()))
  69. group_a.add_argument(
  70. '-e', '--exec', '--batch', dest="command", metavar="command",
  71. help="run a shell command on all repos")
  72. group_m.add_argument(
  73. '-h', '--help', action="help", help="show this help message and exit")
  74. group_m.add_argument(
  75. '-v', '--version', action="version",
  76. version="gitup " + __version__)
  77. # TODO: deprecated arguments, for removal in v1.0:
  78. parser.add_argument(
  79. '-m', '--merge', action="store_true", help=argparse.SUPPRESS)
  80. parser.add_argument(
  81. '-r', '--rebase', action="store_true", help=argparse.SUPPRESS)
  82. color_init(autoreset=True)
  83. args = parser.parse_args()
  84. print(Style.BRIGHT + "gitup" + Style.RESET_ALL + ": the git-repo-updater")
  85. print()
  86. # TODO: remove in v1.0
  87. if args.merge or args.rebase:
  88. print(Style.BRIGHT + Fore.YELLOW + "Warning:", "--merge and --rebase "
  89. "are deprecated. Branches are only updated if they\ntrack an "
  90. "upstream branch and can be safely fast-forwarded. Use "
  91. "--fetch-only to\navoid updating any branches.\n")
  92. if args.bookmark_file:
  93. args.bookmark_file = os.path.expanduser(args.bookmark_file)
  94. acted = False
  95. if args.bookmarks_to_add:
  96. add_bookmarks(args.bookmarks_to_add, args.bookmark_file)
  97. acted = True
  98. if args.bookmarks_to_del:
  99. delete_bookmarks(args.bookmarks_to_del, args.bookmark_file)
  100. acted = True
  101. if args.list_bookmarks:
  102. list_bookmarks(args.bookmark_file)
  103. acted = True
  104. if args.clean_bookmarks:
  105. clean_bookmarks(args.bookmark_file)
  106. acted = True
  107. if args.command:
  108. if args.directories_to_update:
  109. run_command(args.directories_to_update, args)
  110. if args.update or not args.directories_to_update:
  111. run_command(get_bookmarks(args.bookmark_file), args)
  112. else:
  113. if args.directories_to_update:
  114. update_directories(args.directories_to_update, args)
  115. acted = True
  116. if args.update or not acted:
  117. update_bookmarks(get_bookmarks(args.bookmark_file), args)
  118. def run():
  119. """Thin wrapper for main() that catches KeyboardInterrupts."""
  120. try:
  121. main()
  122. except KeyboardInterrupt:
  123. print("Stopped by user.")