A console script that allows you to easily update multiple git repositories at once
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

update.py 11 KiB

10年前
10年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2018 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. from glob import glob
  7. import os
  8. import pipes
  9. import re
  10. import shlex
  11. from colorama import Fore, Style
  12. from git import RemoteReference as RemoteRef, Repo, exc
  13. from git.util import RemoteProgress
  14. __all__ = ["update_bookmarks", "update_directories", "run_command"]
  15. BOLD = Style.BRIGHT
  16. BLUE = Fore.BLUE + BOLD
  17. GREEN = Fore.GREEN + BOLD
  18. RED = Fore.RED + BOLD
  19. CYAN = Fore.CYAN + BOLD
  20. YELLOW = Fore.YELLOW + BOLD
  21. RESET = Style.RESET_ALL
  22. INDENT1 = " " * 3
  23. INDENT2 = " " * 7
  24. ERROR = RED + "Error:" + RESET
  25. class _ProgressMonitor(RemoteProgress):
  26. """Displays relevant output during the fetching process."""
  27. def __init__(self):
  28. super(_ProgressMonitor, self).__init__()
  29. self._started = False
  30. def update(self, op_code, cur_count, max_count=None, message=''):
  31. """Called whenever progress changes. Overrides default behavior."""
  32. if op_code & (self.COMPRESSING | self.RECEIVING):
  33. cur_count = str(int(cur_count))
  34. if max_count:
  35. max_count = str(int(max_count))
  36. if op_code & self.BEGIN:
  37. print("\b, " if self._started else " (", end="")
  38. if not self._started:
  39. self._started = True
  40. if op_code & self.END:
  41. end = ")"
  42. elif max_count:
  43. end = "\b" * (1 + len(cur_count) + len(max_count))
  44. else:
  45. end = "\b" * len(cur_count)
  46. if max_count:
  47. print("{0}/{1}".format(cur_count, max_count), end=end)
  48. else:
  49. print(str(cur_count), end=end)
  50. def _fetch_remotes(remotes, prune):
  51. """Fetch a list of remotes, displaying progress info along the way."""
  52. def _get_name(ref):
  53. """Return the local name of a remote or tag reference."""
  54. return ref.remote_head if isinstance(ref, RemoteRef) else ref.name
  55. # TODO: missing branch deleted (via --prune):
  56. info = [("NEW_HEAD", "new branch", "new branches"),
  57. ("NEW_TAG", "new tag", "new tags"),
  58. ("FAST_FORWARD", "branch update", "branch updates")]
  59. up_to_date = BLUE + "up to date" + RESET
  60. for remote in remotes:
  61. print(INDENT2, "Fetching", BOLD + remote.name, end="")
  62. if not remote.config_reader.has_option("fetch"):
  63. print(":", YELLOW + "skipped:", "no configured refspec.")
  64. continue
  65. try:
  66. results = remote.fetch(progress=_ProgressMonitor(), prune=prune)
  67. except exc.GitCommandError as err:
  68. # We should have to do this ourselves, but GitPython doesn't give
  69. # us a sensible way to get the raw stderr...
  70. msg = re.sub(r"\s+", " ", err.stderr).strip()
  71. msg = re.sub(r"^stderr: *'(fatal: *)?", "", msg).strip("'")
  72. if not msg:
  73. command = " ".join(pipes.quote(arg) for arg in err.command)
  74. msg = "{0} failed with status {1}.".format(command, err.status)
  75. elif not msg.endswith("."):
  76. msg += "."
  77. print(":", RED + "error:", msg)
  78. return
  79. except AssertionError: # Seems to be the result of a bug in GitPython
  80. # This happens when git initiates an auto-gc during fetch:
  81. print(":", RED + "error:", "something went wrong in GitPython,",
  82. "but the fetch might have been successful.")
  83. return
  84. rlist = []
  85. for attr, singular, plural in info:
  86. names = [_get_name(res.ref)
  87. for res in results if res.flags & getattr(res, attr)]
  88. if names:
  89. desc = singular if len(names) == 1 else plural
  90. colored = GREEN + desc + RESET
  91. rlist.append("{0} ({1})".format(colored, ", ".join(names)))
  92. print(":", (", ".join(rlist) if rlist else up_to_date) + ".")
  93. def _update_branch(repo, branch, is_active=False):
  94. """Update a single branch."""
  95. print(INDENT2, "Updating", BOLD + branch.name, end=": ")
  96. upstream = branch.tracking_branch()
  97. if not upstream:
  98. print(YELLOW + "skipped:", "no upstream is tracked.")
  99. return
  100. try:
  101. branch.commit
  102. except ValueError:
  103. print(YELLOW + "skipped:", "branch has no revisions.")
  104. return
  105. try:
  106. upstream.commit
  107. except ValueError:
  108. print(YELLOW + "skipped:", "upstream does not exist.")
  109. return
  110. try:
  111. base = repo.git.merge_base(branch.commit, upstream.commit)
  112. except exc.GitCommandError as err:
  113. print(YELLOW + "skipped:", "can't find merge base with upstream.")
  114. return
  115. if repo.commit(base) == upstream.commit:
  116. print(BLUE + "up to date", end=".\n")
  117. return
  118. if is_active:
  119. try:
  120. repo.git.merge(upstream.name, ff_only=True)
  121. print(GREEN + "done", end=".\n")
  122. except exc.GitCommandError as err:
  123. msg = err.stderr
  124. if "local changes" in msg and "would be overwritten" in msg:
  125. print(YELLOW + "skipped:", "uncommitted changes.")
  126. else:
  127. print(YELLOW + "skipped:", "not possible to fast-forward.")
  128. else:
  129. status = repo.git.merge_base(
  130. branch.commit, upstream.commit, is_ancestor=True,
  131. with_extended_output=True, with_exceptions=False)[0]
  132. if status != 0:
  133. print(YELLOW + "skipped:", "not possible to fast-forward.")
  134. else:
  135. repo.git.branch(branch.name, upstream.name, force=True)
  136. print(GREEN + "done", end=".\n")
  137. def _update_repository(repo, repo_name, args):
  138. """Update a single git repository by fetching remotes and rebasing/merging.
  139. The specific actions depend on the arguments given. We will fetch all
  140. remotes if *args.current_only* is ``False``, or only the remote tracked by
  141. the current branch if ``True``. If *args.fetch_only* is ``False``, we will
  142. also update all fast-forwardable branches that are tracking valid
  143. upstreams. If *args.prune* is ``True``, remote-tracking branches that no
  144. longer exist on their remote after fetching will be deleted.
  145. """
  146. print(INDENT1, BOLD + repo_name + ":")
  147. try:
  148. active = repo.active_branch
  149. except TypeError: # Happens when HEAD is detached
  150. active = None
  151. if args.current_only:
  152. if not active:
  153. print(INDENT2, ERROR,
  154. "--current-only doesn't make sense with a detached HEAD.")
  155. return
  156. ref = active.tracking_branch()
  157. if not ref:
  158. print(INDENT2, ERROR, "no remote tracked by current branch.")
  159. return
  160. remotes = [repo.remotes[ref.remote_name]]
  161. else:
  162. remotes = repo.remotes
  163. if not remotes:
  164. print(INDENT2, ERROR, "no remotes configured to fetch.")
  165. return
  166. _fetch_remotes(remotes, args.prune)
  167. if not args.fetch_only:
  168. for branch in sorted(repo.heads, key=lambda b: b.name):
  169. _update_branch(repo, branch, branch == active)
  170. def _run_command(repo, repo_name, args):
  171. """Run an arbitrary shell command on the given repository."""
  172. print(INDENT1, BOLD + repo_name + ":")
  173. cmd = shlex.split(args.command)
  174. try:
  175. out = repo.git.execute(
  176. cmd, with_extended_output=True, with_exceptions=False)
  177. except exc.GitCommandNotFound as err:
  178. print(INDENT2, ERROR, err)
  179. return
  180. for line in out[1].splitlines() + out[2].splitlines():
  181. print(INDENT2, line)
  182. def _dispatch(base_path, callback, args):
  183. """Apply a callback function on each valid repo in the given path.
  184. Determine whether the directory is a git repo on its own, a directory of
  185. git repositories, a shell glob pattern, or something invalid. If the first,
  186. apply the callback on it; if the second or third, apply the callback on all
  187. repositories contained within; if the last, print an error.
  188. The given args are passed directly to the callback function after the repo.
  189. """
  190. def _collect(paths, max_depth):
  191. """Return all valid repo paths in the given paths, recursively."""
  192. if max_depth == 0:
  193. return []
  194. valid = []
  195. for path in paths:
  196. try:
  197. Repo(path)
  198. valid.append(path)
  199. except exc.InvalidGitRepositoryError:
  200. if not os.path.isdir(path):
  201. continue
  202. children = [os.path.join(path, it) for it in os.listdir(path)]
  203. valid += _collect(children, max_depth - 1)
  204. except exc.NoSuchPathError:
  205. continue
  206. return valid
  207. def _get_basename(base, path):
  208. """Return a reasonable name for a repo path in the given base."""
  209. if path.startswith(base + os.path.sep):
  210. return path.split(base + os.path.sep, 1)[1]
  211. prefix = os.path.commonprefix([base, path])
  212. while not base.startswith(prefix + os.path.sep):
  213. old = prefix
  214. prefix = os.path.split(prefix)[0]
  215. if prefix == old:
  216. break # Prevent infinite loop, but should be almost impossible
  217. return path.split(prefix + os.path.sep, 1)[1]
  218. base = os.path.expanduser(base_path)
  219. max_depth = args.max_depth
  220. if max_depth >= 0:
  221. max_depth += 1
  222. try:
  223. Repo(base)
  224. valid = [base]
  225. except exc.NoSuchPathError:
  226. if is_comment(base):
  227. comment = get_comment(base)
  228. if comment:
  229. print(CYAN + BOLD + comment)
  230. return
  231. paths = glob(base)
  232. if not paths:
  233. print(ERROR, BOLD + base, "doesn't exist!")
  234. return
  235. valid = _collect(paths, max_depth)
  236. except exc.InvalidGitRepositoryError:
  237. if not os.path.isdir(base) or args.max_depth == 0:
  238. print(ERROR, BOLD + base, "isn't a repository!")
  239. return
  240. valid = _collect([base], max_depth)
  241. base = os.path.abspath(base)
  242. suffix = "" if len(valid) == 1 else "s"
  243. print(BOLD + base, "({0} repo{1}):".format(len(valid), suffix))
  244. valid = [os.path.abspath(path) for path in valid]
  245. paths = [(_get_basename(base, path), path) for path in valid]
  246. for name, path in sorted(paths):
  247. callback(Repo(path), name, args)
  248. def is_comment(path):
  249. """Does the line start with a # symbol?"""
  250. return path.lstrip().startswith("#")
  251. def get_comment(path):
  252. """Return the string minus the comment symbol."""
  253. return path.lstrip().lstrip("#").strip()
  254. def update_bookmarks(bookmarks, args):
  255. """Loop through and update all bookmarks."""
  256. if not bookmarks:
  257. print("You don't have any bookmarks configured! Get help with 'gitup -h'.")
  258. return
  259. for path in bookmarks:
  260. _dispatch(path, _update_repository, args)
  261. def update_directories(paths, args):
  262. """Update a list of directories supplied by command arguments."""
  263. for path in paths:
  264. _dispatch(path, _update_repository, args)
  265. def run_command(paths, args):
  266. """Run an arbitrary shell command on all repos."""
  267. for path in paths:
  268. _dispatch(path, _run_command, args)