A console script that allows you to easily update multiple git repositories at once
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

118 lignes
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. # See the LICENSE file for details.
  5. from __future__ import print_function
  6. import os
  7. from colorama import Fore, Style
  8. from git import Repo, exc
  9. __all__ = ["update_bookmarks", "update_directories"]
  10. BOLD = Style.BRIGHT
  11. RED = Fore.RED + BOLD
  12. GREEN = Fore.GREEN + BOLD
  13. BLUE = Fore.BLUE + BOLD
  14. RESET = Style.RESET_ALL
  15. INDENT1 = " " * 3
  16. INDENT2 = " " * 7
  17. def _update_repository(repo):
  18. """Update a single git repository by pulling from the remote."""
  19. print(INDENT1, BOLD + os.path.split(repo.working_dir)[1] + ":")
  20. try:
  21. # Check if there is anything to pull, but don't do it yet:
  22. dry_fetch = _exec_shell("git fetch --dry-run")
  23. except subprocess.CalledProcessError:
  24. print(INDENT2, RED + "Error:" + RESET, "cannot fetch;",
  25. "do you have a remote repository configured correctly?")
  26. return
  27. try:
  28. last_commit = _exec_shell("git log -n 1 --pretty=\"%ar\"")
  29. except subprocess.CalledProcessError:
  30. last_commit = "never" # Couldn't get a log, so no commits
  31. if not dry_fetch: # No new changes to pull
  32. print(INDENT2, BLUE + "No new changes." + RESET,
  33. "Last commit was {0}.".format(last_commit))
  34. else: # Stuff has happened!
  35. print(INDENT2, "There are new changes upstream...")
  36. status = _exec_shell("git status")
  37. if status.endswith("nothing to commit, working directory clean"):
  38. print(INDENT2, GREEN + "Pulling new changes...")
  39. result = _exec_shell("git pull")
  40. if last_commit == "never":
  41. print(INDENT2, "The following changes have been made:")
  42. else:
  43. print(INDENT2, "The following changes have been made since",
  44. last_commit + ":")
  45. print(result)
  46. else:
  47. print(INDENT2, RED + "Warning:" + RESET,
  48. "you have uncommitted changes in this repository!")
  49. print(INDENT2, "Ignoring.")
  50. def _update_subdirectories(path, long_name):
  51. """Update all subdirectories that are git repos in a given directory."""
  52. repos = []
  53. for item in os.listdir(path):
  54. try:
  55. repo = Repo(os.path.join(path, item))
  56. except (exc.InvalidGitRepositoryError, exc.NoSuchPathError):
  57. continue
  58. repos.append(repo)
  59. suffix = "ies" if len(repos) != 1 else "y"
  60. print(long_name[0].upper() + long_name[1:],
  61. "contains {0} git repositor{1}:".format(len(repos), suffix))
  62. for repo in sorted(repos):
  63. _update_repository(repo)
  64. def _update_directory(path, is_bookmark=False):
  65. """Update a particular directory.
  66. Determine whether the directory is a git repo on its own, a directory of
  67. git repositories, or something invalid. If the first, update the single
  68. repository; if the second, update all repositories contained within; if the
  69. third, print an error.
  70. """
  71. dir_type = "bookmark" if is_bookmark else "directory"
  72. long_name = dir_type + ' "' + BOLD + path + RESET + '"'
  73. try:
  74. repo = Repo(path)
  75. except exc.NoSuchPathError:
  76. print(RED + "Error:" + RESET, long_name, "doesn't exist!")
  77. except exc.InvalidGitRepositoryError:
  78. if os.path.isdir(path):
  79. _update_subdirectories(path, long_name)
  80. else:
  81. print(RED + "Error:" + RESET, long_name, "isn't a repository!")
  82. else:
  83. long_name = (dir_type.capitalize() + ' "' + BOLD + repo.working_dir +
  84. RESET + '"')
  85. print(long_name, "is a git repository:")
  86. _update_repository(repo)
  87. def update_bookmarks(bookmarks):
  88. """Loop through and update all bookmarks."""
  89. if bookmarks:
  90. for path, name in bookmarks:
  91. _update_directory(path, is_bookmark=True)
  92. else:
  93. print("You don't have any bookmarks configured! Get help with 'gitup -h'.")
  94. def update_directories(paths):
  95. """Update a list of directories supplied by command arguments."""
  96. for path in paths:
  97. _update_directory(os.path.abspath(path), is_bookmark=False)