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.0 KiB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2015 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. from colorama import init as color_init, Fore, Style
  8. from . import __version__
  9. from .config import (get_bookmarks, add_bookmarks, delete_bookmarks,
  10. list_bookmarks)
  11. from .update import update_bookmarks, update_directories
  12. def main():
  13. """Parse arguments and then call the appropriate function(s)."""
  14. parser = argparse.ArgumentParser(
  15. description="Easily update multiple git repositories at once.",
  16. epilog="""
  17. Both relative and absolute paths are accepted by all arguments.
  18. Direct bug reports and feature requests to:
  19. https://github.com/earwig/git-repo-updater.""",
  20. add_help=False)
  21. group_u = parser.add_argument_group("updating repositories")
  22. group_b = parser.add_argument_group("bookmarking")
  23. group_m = parser.add_argument_group("miscellaneous")
  24. group_u.add_argument(
  25. 'directories_to_update', nargs="*", metavar="path",
  26. help="""update all repositories in this directory (or the directory
  27. itself, if it is a repo)""")
  28. group_u.add_argument(
  29. '-u', '--update', action="store_true", help="""update all bookmarks
  30. (default behavior when called without arguments)""")
  31. group_u.add_argument(
  32. '-c', '--current-only', action="store_true", help="""only fetch the
  33. remote tracked by the current branch instead of all remotes""")
  34. group_u.add_argument(
  35. '-f', '--fetch-only', action="store_true",
  36. help="only fetch remotes, don't try to fast-forward any branches")
  37. group_u.add_argument(
  38. '-p', '--prune', action="store_true", help="""after fetching, delete
  39. remote-tracking branches that no longer exist on their remote""")
  40. group_b.add_argument(
  41. '-a', '--add', dest="bookmarks_to_add", nargs="+", metavar="path",
  42. help="add directory(s) as bookmarks")
  43. group_b.add_argument(
  44. '-d', '--delete', dest="bookmarks_to_del", nargs="+", metavar="path",
  45. help="delete bookmark(s) (leaves actual directories alone)")
  46. group_b.add_argument(
  47. '-l', '--list', dest="list_bookmarks", action="store_true",
  48. help="list current bookmarks")
  49. group_m.add_argument(
  50. '-h', '--help', action="help", help="show this help message and exit")
  51. group_m.add_argument(
  52. '-v', '--version', action="version",
  53. version="gitup " + __version__)
  54. # TODO: deprecated arguments, for removal in v1.0:
  55. parser.add_argument(
  56. '-m', '--merge', action="store_true", help=argparse.SUPPRESS)
  57. parser.add_argument(
  58. '-r', '--rebase', action="store_true", help=argparse.SUPPRESS)
  59. color_init(autoreset=True)
  60. args = parser.parse_args()
  61. update_args = args.current_only, args.fetch_only, args.prune
  62. print(Style.BRIGHT + "gitup" + Style.RESET_ALL + ": the git-repo-updater")
  63. print()
  64. # TODO: remove in v1.0
  65. if args.merge or args.rebase:
  66. print(Style.BRIGHT + Fore.YELLOW + "Warning:", "--merge and --rebase "
  67. "are deprecated. Branches are only updated if they\ntrack an "
  68. "upstream branch and can be safely fast-forwarded. Use "
  69. "--fetch-only to\navoid updating any branches.\n")
  70. acted = False
  71. if args.bookmarks_to_add:
  72. add_bookmarks(args.bookmarks_to_add)
  73. acted = True
  74. if args.bookmarks_to_del:
  75. delete_bookmarks(args.bookmarks_to_del)
  76. acted = True
  77. if args.list_bookmarks:
  78. list_bookmarks()
  79. acted = True
  80. if args.directories_to_update:
  81. update_directories(args.directories_to_update, update_args)
  82. acted = True
  83. if args.update or not acted:
  84. update_bookmarks(get_bookmarks(), update_args)
  85. def run():
  86. """Thin wrapper for main() that catches KeyboardInterrupts."""
  87. try:
  88. main()
  89. except KeyboardInterrupt:
  90. print("Stopped by user.")