Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: ensure_dependencies.py

Issue 6068640302497792: Issue 2311 - Track remote Git branches when required (Closed)
Left Patch Set: Make Git.pull track all remote branches Created May 6, 2015, 11:02 a.m.
Right Patch Set: Simplify regexp some more Created May 6, 2015, 3:08 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # coding: utf-8 2 # coding: utf-8
3 3
4 # This Source Code Form is subject to the terms of the Mozilla Public 4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this 5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 7
8 import sys 8 import sys
9 import os 9 import os
10 import posixpath 10 import posixpath
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 source = source.rstrip("/") 90 source = source.rstrip("/")
91 if not source.endswith(".git"): 91 if not source.endswith(".git"):
92 source += ".git" 92 source += ".git"
93 subprocess.check_call(["git", "clone", "--quiet", source, target]) 93 subprocess.check_call(["git", "clone", "--quiet", source, target])
94 94
95 def get_revision_id(self, repo, rev="HEAD"): 95 def get_revision_id(self, repo, rev="HEAD"):
96 command = ["git", "rev-parse", "--revs-only", rev + '^{commit}'] 96 command = ["git", "rev-parse", "--revs-only", rev + '^{commit}']
97 return subprocess.check_output(command, cwd=repo).strip() 97 return subprocess.check_output(command, cwd=repo).strip()
98 98
99 def pull(self, repo): 99 def pull(self, repo):
100 # First perform a fetch so we have a list of remote branch names 100 # Fetch tracked branches, new tags and the list of available remote branches
101 subprocess.check_call(["git", "fetch", "--quiet"], cwd=repo) 101 subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=re po)
102 # Next we need to ensure all remote branches are tracked 102 # Next we need to ensure all remote branches are tracked
103 newly_tracked = False
103 remotes = subprocess.check_output(["git", "branch", "--remotes"], cwd=repo) 104 remotes = subprocess.check_output(["git", "branch", "--remotes"], cwd=repo)
104 for match in re.finditer(r"origin/(\S+)", remotes): 105 for match in re.finditer(r"^\s*(origin/(\S+))$", remotes, re.M):
105 remote, local = match.group(0), match.group(1) 106 remote, local = match.groups()
106 if local not in ["HEAD", "master"]: 107 with open(os.devnull, "wb") as devnull:
107 with open(os.devnull, "w") as devnull: 108 if subprocess.call(["git", "branch", "--track", local, remote],
108 subprocess.call(["git", "branch", "--track", local, remote], 109 cwd=repo, stdout=devnull, stderr=devnull) == 0:
109 cwd=repo, stdout=devnull, stderr=devnull) 110 newly_tracked = True
110 # Finally we need to actually fetch all the remote branches and tags 111 # Finally fetch any newly tracked remote branches
111 subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=re po) 112 if newly_tracked:
113 subprocess.check_call(["git", "fetch", "--quiet", "origin"], cwd=repo)
112 114
113 def update(self, repo, rev): 115 def update(self, repo, rev):
114 subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo) 116 subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo)
115 117
116 def ignore(self, target, repo): 118 def ignore(self, target, repo):
117 module = os.path.relpath(target, repo) 119 module = os.path.relpath(target, repo)
118 exclude_file = os.path.join(repo, ".git", "info", "exclude") 120 exclude_file = os.path.join(repo, ".git", "info", "exclude")
119 _ensure_line_exists(exclude_file, module) 121 _ensure_line_exists(exclude_file, module)
120 122
121 def postprocess_url(self, url): 123 def postprocess_url(self, url):
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 if spec: 173 if spec:
172 result[key] = spec 174 result[key] = spec
173 return result 175 return result
174 except IOError, e: 176 except IOError, e:
175 if e.errno != errno.ENOENT: 177 if e.errno != errno.ENOENT:
176 raise 178 raise
177 return None 179 return None
178 180
179 def safe_join(path, subpath): 181 def safe_join(path, subpath):
180 # This has been inspired by Flask's safe_join() function 182 # This has been inspired by Flask's safe_join() function
181 forbidden = set([os.sep, os.altsep]) - set([posixpath.sep, None]) 183 forbidden = {os.sep, os.altsep} - {posixpath.sep, None}
182 if any(sep in subpath for sep in forbidden): 184 if any(sep in subpath for sep in forbidden):
183 raise Exception("Illegal directory separator in dependency path %s" % subpat h) 185 raise Exception("Illegal directory separator in dependency path %s" % subpat h)
184 186
185 normpath = posixpath.normpath(subpath) 187 normpath = posixpath.normpath(subpath)
186 if posixpath.isabs(normpath): 188 if posixpath.isabs(normpath):
187 raise Exception("Dependency path %s cannot be absolute" % subpath) 189 raise Exception("Dependency path %s cannot be absolute" % subpath)
188 if normpath == posixpath.pardir or normpath.startswith(posixpath.pardir + posi xpath.sep): 190 if normpath == posixpath.pardir or normpath.startswith(posixpath.pardir + posi xpath.sep):
189 raise Exception("Dependency path %s has to be inside the repository" % subpa th) 191 raise Exception("Dependency path %s has to be inside the repository" % subpa th)
190 return os.path.join(path, *normpath.split(posixpath.sep)) 192 return os.path.join(path, *normpath.split(posixpath.sep))
191 193
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 repo_types[type].update(target, resolved_revision) 253 repo_types[type].update(target, resolved_revision)
252 254
253 def resolve_deps(repodir, level=0, self_update=True, overrideroots=None, skipdep endencies=set()): 255 def resolve_deps(repodir, level=0, self_update=True, overrideroots=None, skipdep endencies=set()):
254 config = read_deps(repodir) 256 config = read_deps(repodir)
255 if config is None: 257 if config is None:
256 if level == 0: 258 if level == 0:
257 logging.warning("No dependencies file in directory %s, nothing to do...\n% s" % (repodir, USAGE)) 259 logging.warning("No dependencies file in directory %s, nothing to do...\n% s" % (repodir, USAGE))
258 return 260 return
259 if level >= 10: 261 if level >= 10:
260 logging.warning("Too much subrepository nesting, ignoring %s" % repo) 262 logging.warning("Too much subrepository nesting, ignoring %s" % repo)
263 return
261 264
262 if overrideroots is not None: 265 if overrideroots is not None:
263 config["_root"] = overrideroots 266 config["_root"] = overrideroots
264 267
265 for dir, revisions in config.iteritems(): 268 for dir, revisions in config.iteritems():
266 if dir.startswith("_") or revisions["_source"] in skipdependencies: 269 if dir.startswith("_") or revisions["_source"] in skipdependencies:
267 continue 270 continue
268 target = safe_join(repodir, dir) 271 target = safe_join(repodir, dir)
269 ensure_repo(repodir, target, config.get("_root", {}), revisions["_source"]) 272 ensure_repo(repodir, target, config.get("_root", {}), revisions["_source"])
270 update_repo(target, revisions) 273 update_repo(target, revisions)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 args = parser.parse_args() 317 args = parser.parse_args()
315 318
316 if args.quiet: 319 if args.quiet:
317 logging.disable(logging.INFO) 320 logging.disable(logging.INFO)
318 321
319 repos = args.repos 322 repos = args.repos
320 if not len(repos): 323 if not len(repos):
321 repos = [os.path.dirname(__file__)] 324 repos = [os.path.dirname(__file__)]
322 for repo in repos: 325 for repo in repos:
323 resolve_deps(repo) 326 resolve_deps(repo)
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld