A console script that allows you to easily update multiple git repositories at once
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. _update_branch(repo, active, merge, rebase)
  164. branches = set(repo.heads) - {active}
  165. if branches:
  166. stasher = _Stasher(repo)
  167. try:
  168. for branch in sorted(branches, key=lambda b: b.name):
  169. _update_branch(repo, branch, merge, rebase, stasher)
  170. finally:
  171. active.checkout()
  172. stasher.restore()
  173. def _update_repository(repo, current_only=False, rebase=False, merge=False):
  174. """Update a single git repository by fetching remotes and rebasing/merging.
  175. The specific actions depend on the arguments given. We will fetch all
  176. remotes if *current_only* is ``False``, or only the remote tracked by the
  177. current branch if ``True``. By default, we will merge unless
  178. ``pull.rebase`` or ``branch.<name>.rebase`` is set in config; *rebase* will
  179. cause us to always rebase with ``--preserve-merges``, and *merge* will
  180. cause us to always merge.
  181. """
  182. print(INDENT1, BOLD + os.path.split(repo.working_dir)[1] + ":")
  183. active = repo.active_branch
  184. if current_only:
  185. ref = active.tracking_branch()
  186. if not ref:
  187. print(INDENT2, ERROR, "no remote tracked by current branch.")
  188. return
  189. remotes = [repo.remotes[ref.remote_name]]
  190. else:
  191. remotes = repo.remotes
  192. if not remotes:
  193. print(INDENT2, ERROR, "no remotes configured to pull from.")
  194. return
  195. rebase = rebase or _read_config(repo, "pull.rebase")
  196. _fetch_remotes(remotes)
  197. _update_branches(repo, active, merge, rebase)
  198. def _update_subdirectories(path, long_name, update_args):
  199. """Update all subdirectories that are git repos in a given directory."""
  200. repos = []
  201. for item in os.listdir(path):
  202. try:
  203. repo = Repo(os.path.join(path, item))
  204. except (exc.InvalidGitRepositoryError, exc.NoSuchPathError):
  205. continue
  206. repos.append(repo)
  207. suffix = "ies" if len(repos) != 1 else "y"
  208. print(long_name[0].upper() + long_name[1:],
  209. "contains {0} git repositor{1}:".format(len(repos), suffix))
  210. for repo in sorted(repos, key=lambda r: os.path.split(r.working_dir)[1]):
  211. _update_repository(repo, *update_args)
  212. def _update_directory(path, update_args, is_bookmark=False):
  213. """Update a particular directory.
  214. Determine whether the directory is a git repo on its own, a directory of
  215. git repositories, or something invalid. If the first, update the single
  216. repository; if the second, update all repositories contained within; if the
  217. third, print an error.
  218. """
  219. dir_type = "bookmark" if is_bookmark else "directory"
  220. long_name = dir_type + ' "' + BOLD + path + RESET + '"'
  221. try:
  222. repo = Repo(path)
  223. except exc.NoSuchPathError:
  224. print(ERROR, long_name, "doesn't exist!")
  225. except exc.InvalidGitRepositoryError:
  226. if os.path.isdir(path):
  227. _update_subdirectories(path, long_name, update_args)
  228. else:
  229. print(ERROR, long_name, "isn't a repository!")
  230. else:
  231. long_name = (dir_type.capitalize() + ' "' + BOLD + repo.working_dir +
  232. RESET + '"')
  233. print(long_name, "is a git repository:")
  234. _update_repository(repo, *update_args)
  235. def update_bookmarks(bookmarks, update_args):
  236. """Loop through and update all bookmarks."""
  237. if bookmarks:
  238. for path, name in bookmarks:
  239. _update_directory(path, update_args, is_bookmark=True)
  240. else:
  241. print("You don't have any bookmarks configured! Get help with 'gitup -h'.")
  242. def update_directories(paths, update_args):
  243. """Update a list of directories supplied by command arguments."""
  244. for path in paths:
  245. full_path = os.path.abspath(path)
  246. _update_directory(full_path, update_args, is_bookmark=False)