A console script that allows you to easily update multiple git repositories at once
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

289 rader
10 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 RemoteReference as RemoteRef, Repo, exc
  9. from git.util import RemoteProgress
  10. __all__ = ["update_bookmarks", "update_directories"]
  11. BOLD = Style.BRIGHT
  12. BLUE = Fore.BLUE + BOLD
  13. GREEN = Fore.GREEN + BOLD
  14. RED = Fore.RED + BOLD
  15. YELLOW = Fore.YELLOW + BOLD
  16. RESET = Style.RESET_ALL
  17. INDENT1 = " " * 3
  18. INDENT2 = " " * 7
  19. ERROR = RED + "Error:" + RESET
  20. class _ProgressMonitor(RemoteProgress):
  21. """Displays relevant output during the fetching process."""
  22. def __init__(self):
  23. super(_ProgressMonitor, self).__init__()
  24. self._started = False
  25. def update(self, op_code, cur_count, max_count=None, message=''):
  26. """Called whenever progress changes. Overrides default behavior."""
  27. if op_code & (self.COMPRESSING | self.RECEIVING):
  28. if op_code & self.BEGIN:
  29. print("\b, " if self._started else " (", end="")
  30. if not self._started:
  31. self._started = True
  32. if op_code & self.END:
  33. end = ")"
  34. else:
  35. end = "\b" * (1 + len(cur_count) + len(max_count))
  36. print("{0}/{1}".format(cur_count, max_count), end=end)
  37. class _Stasher(object):
  38. """Manages the stash state of a given repository."""
  39. def __init__(self, repo):
  40. self._repo = repo
  41. self._clean = self._stashed = False
  42. def clean(self):
  43. """Ensure the working directory is clean, so we can do checkouts."""
  44. if not self._clean:
  45. res = self._repo.git.stash("--all")
  46. self._clean = True
  47. if res != "No local changes to save":
  48. self._stashed = True
  49. def restore(self):
  50. """Restore the pre-stash state."""
  51. if self._stashed:
  52. self._repo.git.stash("pop", "--index")
  53. def _read_config(repo, attr):
  54. """Read an attribute from git config."""
  55. try:
  56. return repo.git.config("--get", attr)
  57. except exc.GitCommandError:
  58. return None
  59. def _fetch_remotes(remotes):
  60. """Fetch a list of remotes, displaying progress info along the way."""
  61. def _get_name(ref):
  62. """Return the local name of a remote or tag reference."""
  63. return ref.remote_head if isinstance(ref, RemoteRef) else ref.name
  64. info = [("NEW_HEAD", "new branch", "new branches"),
  65. ("NEW_TAG", "new tag", "new tags"),
  66. ("FAST_FORWARD", "branch update", "branch updates")]
  67. up_to_date = BLUE + "up to date" + RESET
  68. for remote in remotes:
  69. print(INDENT2, "Fetching", BOLD + remote.name, end="")
  70. try:
  71. results = remote.fetch(progress=_ProgressMonitor())
  72. except exc.GitCommandError as err:
  73. msg = err.command[0].replace("Error when fetching: ", "")
  74. if not msg.endswith("."):
  75. msg += "."
  76. print(":", RED + "error:", msg)
  77. return
  78. except AssertionError: # Seems to be the result of a bug in GitPython
  79. # This happens when git initiates an auto-gc during fetch:
  80. print(":", RED + "error:", "something went wrong in GitPython,",
  81. "but the fetch might have been successful.")
  82. return
  83. rlist = []
  84. for attr, singular, plural in info:
  85. names = [_get_name(res.ref)
  86. for res in results if res.flags & getattr(res, attr)]
  87. if names:
  88. desc = singular if len(names) == 1 else plural
  89. colored = GREEN + desc + RESET
  90. rlist.append("{0} ({1})".format(colored, ", ".join(names)))
  91. print(":", (", ".join(rlist) if rlist else up_to_date) + ".")
  92. def _is_up_to_date(repo, branch, upstream):
  93. """Return whether *branch* is up-to-date with its *upstream*."""
  94. base = repo.git.merge_base(branch.commit, upstream.commit)
  95. return repo.commit(base) == upstream.commit
  96. def _rebase(repo, name):
  97. """Rebase the current HEAD of *repo* onto the branch *name*."""
  98. print(GREEN + "rebasing...", end="")
  99. try:
  100. res = repo.git.rebase(name, "--preserve-merges")
  101. except exc.GitCommandError as err:
  102. msg = err.stderr.replace("\n", " ").strip()
  103. if not msg.endswith("."):
  104. msg += "."
  105. if "unstaged changes" in msg:
  106. print(RED + " error:", "unstaged changes.")
  107. elif "uncommitted changes" in msg:
  108. print(RED + " error:", "uncommitted changes.")
  109. else:
  110. try:
  111. repo.git.rebase("--abort")
  112. except exc.GitCommandError:
  113. pass
  114. print(RED + " error:", msg if msg else "rebase conflict.",
  115. "Aborted.")
  116. else:
  117. print("\b" * 6 + " " * 6 + "\b" * 6 + GREEN + "ed", end=".\n")
  118. def _merge(repo, name):
  119. """Merge the branch *name* into the current HEAD of *repo*."""
  120. print(GREEN + "merging...", end="")
  121. try:
  122. repo.git.merge(name)
  123. except exc.GitCommandError as err:
  124. msg = err.stderr.replace("\n", " ").strip()
  125. if not msg.endswith("."):
  126. msg += "."
  127. if "local changes" in msg and "would be overwritten" in msg:
  128. print(RED + " error:", "uncommitted changes.")
  129. else:
  130. try:
  131. repo.git.merge("--abort")
  132. except exc.GitCommandError:
  133. pass
  134. print(RED + " error:", msg if msg else "merge conflict.",
  135. "Aborted.")
  136. else:
  137. print("\b" * 6 + " " * 6 + "\b" * 6 + GREEN + "ed", end=".\n")
  138. def _update_branch(repo, branch, merge, rebase, stasher=None):
  139. """Update a single branch."""
  140. print(INDENT2, "Updating", BOLD + branch.name, end=": ")
  141. upstream = branch.tracking_branch()
  142. if not upstream:
  143. print(YELLOW + "skipped:", "no upstream is tracked.")
  144. return
  145. try:
  146. branch.commit, upstream.commit
  147. except ValueError:
  148. print(YELLOW + "skipped:", "branch has no revisions.")
  149. return
  150. if _is_up_to_date(repo, branch, upstream):
  151. print(BLUE + "up to date", end=".\n")
  152. return
  153. if stasher:
  154. stasher.clean()
  155. branch.checkout()
  156. config_attr = "branch.{0}.rebase".format(branch.name)
  157. if not merge and (rebase or _read_config(repo, config_attr)):
  158. _rebase(repo, upstream.name)
  159. else:
  160. _merge(repo, upstream.name)
  161. def _update_branches(repo, active, merge, rebase):
  162. """Update a list of branches."""
  163. if active:
  164. _update_branch(repo, active, merge, rebase)
  165. branches = set(repo.heads) - {active}
  166. if branches:
  167. stasher = _Stasher(repo)
  168. try:
  169. for branch in sorted(branches, key=lambda b: b.name):
  170. _update_branch(repo, branch, merge, rebase, stasher)
  171. finally:
  172. if active:
  173. active.checkout()
  174. stasher.restore()
  175. def _update_repository(repo, current_only=False, rebase=False, merge=False):
  176. """Update a single git repository by fetching remotes and rebasing/merging.
  177. The specific actions depend on the arguments given. We will fetch all
  178. remotes if *current_only* is ``False``, or only the remote tracked by the
  179. current branch if ``True``. By default, we will merge unless
  180. ``pull.rebase`` or ``branch.<name>.rebase`` is set in config; *rebase* will
  181. cause us to always rebase with ``--preserve-merges``, and *merge* will
  182. cause us to always merge.
  183. """
  184. print(INDENT1, BOLD + os.path.split(repo.working_dir)[1] + ":")
  185. try:
  186. active = repo.active_branch
  187. except TypeError: # Happens when HEAD is detached
  188. active = None
  189. if current_only:
  190. ref = active.tracking_branch() if active else None
  191. if not ref:
  192. print(INDENT2, ERROR, "no remote tracked by current branch.")
  193. return
  194. remotes = [repo.remotes[ref.remote_name]]
  195. else:
  196. remotes = repo.remotes
  197. if not remotes:
  198. print(INDENT2, ERROR, "no remotes configured to pull from.")
  199. return
  200. rebase = rebase or _read_config(repo, "pull.rebase")
  201. _fetch_remotes(remotes)
  202. _update_branches(repo, active, merge, rebase)
  203. def _update_subdirectories(path, long_name, update_args):
  204. """Update all subdirectories that are git repos in a given directory."""
  205. repos = []
  206. for item in os.listdir(path):
  207. try:
  208. repo = Repo(os.path.join(path, item))
  209. except (exc.InvalidGitRepositoryError, exc.NoSuchPathError):
  210. continue
  211. repos.append(repo)
  212. suffix = "ies" if len(repos) != 1 else "y"
  213. print(long_name[0].upper() + long_name[1:],
  214. "contains {0} git repositor{1}:".format(len(repos), suffix))
  215. for repo in sorted(repos, key=lambda r: os.path.split(r.working_dir)[1]):
  216. _update_repository(repo, *update_args)
  217. def _update_directory(path, update_args, is_bookmark=False):
  218. """Update a particular directory.
  219. Determine whether the directory is a git repo on its own, a directory of
  220. git repositories, or something invalid. If the first, update the single
  221. repository; if the second, update all repositories contained within; if the
  222. third, print an error.
  223. """
  224. dir_type = "bookmark" if is_bookmark else "directory"
  225. long_name = dir_type + ' "' + BOLD + path + RESET + '"'
  226. try:
  227. repo = Repo(path)
  228. except exc.NoSuchPathError:
  229. print(ERROR, long_name, "doesn't exist!")
  230. except exc.InvalidGitRepositoryError:
  231. if os.path.isdir(path):
  232. _update_subdirectories(path, long_name, update_args)
  233. else:
  234. print(ERROR, long_name, "isn't a repository!")
  235. else:
  236. long_name = (dir_type.capitalize() + ' "' + BOLD + repo.working_dir +
  237. RESET + '"')
  238. print(long_name, "is a git repository:")
  239. _update_repository(repo, *update_args)
  240. def update_bookmarks(bookmarks, update_args):
  241. """Loop through and update all bookmarks."""
  242. if bookmarks:
  243. for path, name in bookmarks:
  244. _update_directory(path, update_args, is_bookmark=True)
  245. else:
  246. print("You don't have any bookmarks configured! Get help with 'gitup -h'.")
  247. def update_directories(paths, update_args):
  248. """Update a list of directories supplied by command arguments."""
  249. for path in paths:
  250. full_path = os.path.abspath(path)
  251. _update_directory(full_path, update_args, is_bookmark=False)