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.

122 lines
4.7 KiB

  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)
  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. '-b', '--bookmark-file', nargs="?", metavar="path", type=_decode,
  59. help="use a specific bookmark config file (default: {0})".format(
  60. get_default_config_path()))
  61. group_m.add_argument(
  62. '-h', '--help', action="help", help="show this help message and exit")
  63. group_m.add_argument(
  64. '-v', '--version', action="version",
  65. version="gitup " + __version__)
  66. # TODO: deprecated arguments, for removal in v1.0:
  67. parser.add_argument(
  68. '-m', '--merge', action="store_true", help=argparse.SUPPRESS)
  69. parser.add_argument(
  70. '-r', '--rebase', action="store_true", help=argparse.SUPPRESS)
  71. color_init(autoreset=True)
  72. args = parser.parse_args()
  73. update_args = args.current_only, args.fetch_only, args.prune
  74. print(Style.BRIGHT + "gitup" + Style.RESET_ALL + ": the git-repo-updater")
  75. print()
  76. # TODO: remove in v1.0
  77. if args.merge or args.rebase:
  78. print(Style.BRIGHT + Fore.YELLOW + "Warning:", "--merge and --rebase "
  79. "are deprecated. Branches are only updated if they\ntrack an "
  80. "upstream branch and can be safely fast-forwarded. Use "
  81. "--fetch-only to\navoid updating any branches.\n")
  82. if args.bookmark_file:
  83. args.bookmark_file = os.path.expanduser(args.bookmark_file)
  84. acted = False
  85. if args.bookmarks_to_add:
  86. add_bookmarks(args.bookmarks_to_add, args.bookmark_file)
  87. acted = True
  88. if args.bookmarks_to_del:
  89. delete_bookmarks(args.bookmarks_to_del, args.bookmark_file)
  90. acted = True
  91. if args.list_bookmarks:
  92. list_bookmarks(args.bookmark_file)
  93. acted = True
  94. if args.directories_to_update:
  95. update_directories(args.directories_to_update, update_args)
  96. acted = True
  97. if args.update or not acted:
  98. update_bookmarks(get_bookmarks(args.bookmark_file), update_args)
  99. def run():
  100. """Thin wrapper for main() that catches KeyboardInterrupts."""
  101. try:
  102. main()
  103. except KeyboardInterrupt:
  104. print("Stopped by user.")