mirror of
https://gerrit.googlesource.com/git-repo
synced 2026-04-19 19:04:53 +00:00
Compare commits
72 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba72d8301e | ||
|
|
9ff2ece6ab | ||
|
|
2487cb7b2c | ||
|
|
8ce5041596 | ||
|
|
f7a51898d3 | ||
|
|
b9a1b73425 | ||
|
|
dc2545cad6 | ||
|
|
f33929d014 | ||
|
|
3010e5ba64 | ||
|
|
ba7bc738c1 | ||
|
|
f4599a2a3d | ||
|
|
022a1d4e6e | ||
|
|
41d1baac31 | ||
|
|
46496d8761 | ||
|
|
7c9263bce0 | ||
|
|
dab9e99f0f | ||
|
|
c5f15bf7c0 | ||
|
|
6d35d676db | ||
|
|
0745bb2657 | ||
|
|
25857b8988 | ||
|
|
bdb5271de3 | ||
|
|
884092225d | ||
|
|
5d0c3a614e | ||
|
|
1efc2b4a01 | ||
|
|
2635c0e3b6 | ||
|
|
43322283dc | ||
|
|
f9b7683a3b | ||
|
|
eeab6860f1 | ||
|
|
7e59de2bcc | ||
|
|
163fdbf2fd | ||
|
|
555be54790 | ||
|
|
c5cd433daf | ||
|
|
2a3e15217a | ||
|
|
0369a069ad | ||
|
|
abaa7f312f | ||
|
|
7cccfb2cf0 | ||
|
|
57f43f4944 | ||
|
|
17af578d72 | ||
|
|
b1a07b8276 | ||
|
|
4e16c24981 | ||
|
|
b3d6e67196 | ||
|
|
503d66d8af | ||
|
|
679bac4bf3 | ||
|
|
97836cf09f | ||
|
|
80e3a37ab5 | ||
|
|
bb4a1b5274 | ||
|
|
551dfecea9 | ||
|
|
6944cdb8d1 | ||
|
|
59b417493e | ||
|
|
30d13eea86 | ||
|
|
727cc3e324 | ||
|
|
c5ceeb1625 | ||
|
|
db75704bfc | ||
|
|
87ea5913f2 | ||
|
|
185307d1dd | ||
|
|
c116f94261 | ||
|
|
7993f3cdda | ||
|
|
b1d1fd778d | ||
|
|
be4456cf24 | ||
|
|
cf738ed4a1 | ||
|
|
6cfc68e1e6 | ||
|
|
4c426ef1d4 | ||
|
|
472ce9f5fa | ||
|
|
0184dcc510 | ||
|
|
c4b301f988 | ||
|
|
31a7be561e | ||
|
|
384b3c5948 | ||
|
|
35de228f33 | ||
|
|
ace097c36e | ||
|
|
b155354034 | ||
|
|
382582728e | ||
|
|
b4d43b9f66 |
27
command.py
27
command.py
@@ -106,13 +106,13 @@ class Command(object):
|
||||
def _UpdatePathToProjectMap(self, project):
|
||||
self._by_path[project.worktree] = project
|
||||
|
||||
def _GetProjectByPath(self, path):
|
||||
def _GetProjectByPath(self, manifest, path):
|
||||
project = None
|
||||
if os.path.exists(path):
|
||||
oldpath = None
|
||||
while path \
|
||||
and path != oldpath \
|
||||
and path != self.manifest.topdir:
|
||||
and path != manifest.topdir:
|
||||
try:
|
||||
project = self._by_path[path]
|
||||
break
|
||||
@@ -126,15 +126,19 @@ class Command(object):
|
||||
pass
|
||||
return project
|
||||
|
||||
def GetProjects(self, args, missing_ok=False, submodules_ok=False):
|
||||
def GetProjects(self, args, manifest=None, groups='', missing_ok=False,
|
||||
submodules_ok=False):
|
||||
"""A list of projects that match the arguments.
|
||||
"""
|
||||
all_projects_list = self.manifest.projects
|
||||
if not manifest:
|
||||
manifest = self.manifest
|
||||
all_projects_list = manifest.projects
|
||||
result = []
|
||||
|
||||
mp = self.manifest.manifestProject
|
||||
mp = manifest.manifestProject
|
||||
|
||||
groups = mp.config.GetString('manifest.groups')
|
||||
if not groups:
|
||||
groups = mp.config.GetString('manifest.groups')
|
||||
if not groups:
|
||||
groups = 'default,platform-' + platform.system().lower()
|
||||
groups = [x for x in re.split(r'[,\s]+', groups) if x]
|
||||
@@ -154,11 +158,11 @@ class Command(object):
|
||||
self._ResetPathToProjectMap(all_projects_list)
|
||||
|
||||
for arg in args:
|
||||
projects = self.manifest.GetProjectsWithName(arg)
|
||||
projects = manifest.GetProjectsWithName(arg)
|
||||
|
||||
if not projects:
|
||||
path = os.path.abspath(arg).replace('\\', '/')
|
||||
project = self._GetProjectByPath(path)
|
||||
project = self._GetProjectByPath(manifest, path)
|
||||
|
||||
# If it's not a derived project, update path->project mapping and
|
||||
# search again, as arg might actually point to a derived subproject.
|
||||
@@ -169,7 +173,7 @@ class Command(object):
|
||||
self._UpdatePathToProjectMap(subproject)
|
||||
search_again = True
|
||||
if search_again:
|
||||
project = self._GetProjectByPath(path) or project
|
||||
project = self._GetProjectByPath(manifest, path) or project
|
||||
|
||||
if project:
|
||||
projects = [project]
|
||||
@@ -226,3 +230,8 @@ class MirrorSafeCommand(object):
|
||||
"""Command permits itself to run within a mirror,
|
||||
and does not require a working directory.
|
||||
"""
|
||||
|
||||
class RequiresGitcCommand(object):
|
||||
"""Command that requires GITC to be available, but does
|
||||
not require the local client to be a GITC client.
|
||||
"""
|
||||
|
||||
4
error.py
4
error.py
@@ -80,7 +80,7 @@ class NoSuchProjectError(Exception):
|
||||
self.name = name
|
||||
|
||||
def __str__(self):
|
||||
if self.Name is None:
|
||||
if self.name is None:
|
||||
return 'in current directory'
|
||||
return self.name
|
||||
|
||||
@@ -93,7 +93,7 @@ class InvalidProjectGroupsError(Exception):
|
||||
self.name = name
|
||||
|
||||
def __str__(self):
|
||||
if self.Name is None:
|
||||
if self.name is None:
|
||||
return 'in current directory'
|
||||
return self.name
|
||||
|
||||
|
||||
@@ -92,7 +92,10 @@ class _GitCall(object):
|
||||
def version(self):
|
||||
p = GitCommand(None, ['--version'], capture_stdout=True)
|
||||
if p.Wait() == 0:
|
||||
return p.stdout.decode('utf-8')
|
||||
if hasattr(p.stdout, 'decode'):
|
||||
return p.stdout.decode('utf-8')
|
||||
else:
|
||||
return p.stdout
|
||||
return None
|
||||
|
||||
def version_tuple(self):
|
||||
@@ -263,6 +266,8 @@ class GitCommand(object):
|
||||
if not buf:
|
||||
s_in.remove(s)
|
||||
continue
|
||||
if not hasattr(buf, 'encode'):
|
||||
buf = buf.decode()
|
||||
if s.std_name == 'stdout':
|
||||
self.stdout += buf
|
||||
else:
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import contextlib
|
||||
import errno
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
@@ -280,7 +282,7 @@ class GitConfig(object):
|
||||
finally:
|
||||
fd.close()
|
||||
except (IOError, TypeError):
|
||||
if os.path.exists(self.json):
|
||||
if os.path.exists(self._json):
|
||||
os.remove(self._json)
|
||||
|
||||
def _ReadGit(self):
|
||||
@@ -502,6 +504,43 @@ def GetSchemeFromUrl(url):
|
||||
return m.group(1)
|
||||
return None
|
||||
|
||||
@contextlib.contextmanager
|
||||
def GetUrlCookieFile(url, quiet):
|
||||
if url.startswith('persistent-'):
|
||||
try:
|
||||
p = subprocess.Popen(
|
||||
['git-remote-persistent-https', '-print_config', url],
|
||||
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
try:
|
||||
cookieprefix = 'http.cookiefile='
|
||||
proxyprefix = 'http.proxy='
|
||||
cookiefile = None
|
||||
proxy = None
|
||||
for line in p.stdout:
|
||||
line = line.strip()
|
||||
if line.startswith(cookieprefix):
|
||||
cookiefile = line[len(cookieprefix):]
|
||||
if line.startswith(proxyprefix):
|
||||
proxy = line[len(proxyprefix):]
|
||||
# Leave subprocess open, as cookie file may be transient.
|
||||
if cookiefile or proxy:
|
||||
yield cookiefile, proxy
|
||||
return
|
||||
finally:
|
||||
p.stdin.close()
|
||||
if p.wait():
|
||||
err_msg = p.stderr.read()
|
||||
if ' -print_config' in err_msg:
|
||||
pass # Persistent proxy doesn't support -print_config.
|
||||
elif not quiet:
|
||||
print(err_msg, file=sys.stderr)
|
||||
except OSError as e:
|
||||
if e.errno == errno.ENOENT:
|
||||
pass # No persistent proxy.
|
||||
raise
|
||||
yield GitConfig.ForUser().GetString('http.cookiefile'), None
|
||||
|
||||
def _preconnect(url):
|
||||
m = URI_ALL.match(url)
|
||||
if m:
|
||||
|
||||
101
gitc_utils.py
Normal file
101
gitc_utils.py
Normal file
@@ -0,0 +1,101 @@
|
||||
#
|
||||
# Copyright (C) 2015 The Android Open Source Project
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import print_function
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
import git_command
|
||||
import git_config
|
||||
import wrapper
|
||||
|
||||
|
||||
GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
|
||||
NUM_BATCH_RETRIEVE_REVISIONID = 300
|
||||
|
||||
def get_gitc_manifest_dir():
|
||||
return wrapper.Wrapper().get_gitc_manifest_dir()
|
||||
|
||||
def parse_clientdir(gitc_fs_path):
|
||||
"""Parse a path in the GITC FS and return its client name.
|
||||
|
||||
@param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR.
|
||||
|
||||
@returns: The GITC client name
|
||||
"""
|
||||
if (gitc_fs_path == GITC_FS_ROOT_DIR or
|
||||
not gitc_fs_path.startswith(GITC_FS_ROOT_DIR)):
|
||||
return None
|
||||
return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
|
||||
|
||||
def _set_project_revisions(projects):
|
||||
"""Sets the revisionExpr for a list of projects.
|
||||
|
||||
Because of the limit of open file descriptors allowed, length of projects
|
||||
should not be overly large. Recommend calling this function multiple times
|
||||
with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
|
||||
|
||||
@param projects: List of project objects to set the revionExpr for.
|
||||
"""
|
||||
# Retrieve the commit id for each project based off of it's current
|
||||
# revisionExpr and it is not already a commit id.
|
||||
project_gitcmds = [(
|
||||
project, git_command.GitCommand(None,
|
||||
['ls-remote',
|
||||
project.remote.url,
|
||||
project.revisionExpr],
|
||||
capture_stdout=True, cwd='/tmp'))
|
||||
for project in projects if not git_config.IsId(project.revisionExpr)]
|
||||
for proj, gitcmd in project_gitcmds:
|
||||
if gitcmd.Wait():
|
||||
print('FATAL: Failed to retrieve revisionExpr for %s' % proj)
|
||||
sys.exit(1)
|
||||
proj.revisionExpr = gitcmd.stdout.split('\t')[0]
|
||||
|
||||
def generate_gitc_manifest(client_dir, manifest, projects=None):
|
||||
"""Generate a manifest for shafsd to use for this GITC client.
|
||||
|
||||
@param client_dir: GITC client directory to install the .manifest file in.
|
||||
@param manifest: XmlManifest object representing the repo manifest.
|
||||
@param projects: List of projects we want to update, this must be a sublist
|
||||
of manifest.projects to work properly. If not provided,
|
||||
manifest.projects is used.
|
||||
"""
|
||||
print('Generating GITC Manifest by fetching revision SHAs for each '
|
||||
'project.')
|
||||
if projects is None:
|
||||
projects = manifest.projects
|
||||
index = 0
|
||||
while index < len(projects):
|
||||
_set_project_revisions(
|
||||
projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
|
||||
index += NUM_BATCH_RETRIEVE_REVISIONID
|
||||
# Save the manifest.
|
||||
save_manifest(manifest, client_dir=client_dir)
|
||||
|
||||
def save_manifest(manifest, client_dir=None):
|
||||
"""Save the manifest file in the client_dir.
|
||||
|
||||
@param client_dir: Client directory to save the manifest in.
|
||||
@param manifest: Manifest object to save.
|
||||
"""
|
||||
if not client_dir:
|
||||
client_dir = manifest.gitc_client_dir
|
||||
with open(os.path.join(client_dir, '.manifest'), 'w') as f:
|
||||
manifest.Save(f)
|
||||
# TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
|
||||
# Give the GITC filesystem time to register the manifest changes.
|
||||
time.sleep(3)
|
||||
22
main.py
22
main.py
@@ -42,15 +42,18 @@ from git_command import git, GitCommand
|
||||
from git_config import init_ssh, close_ssh
|
||||
from command import InteractiveCommand
|
||||
from command import MirrorSafeCommand
|
||||
from command import RequiresGitcCommand
|
||||
from subcmds.version import Version
|
||||
from editor import Editor
|
||||
from error import DownloadError
|
||||
from error import InvalidProjectGroupsError
|
||||
from error import ManifestInvalidRevisionError
|
||||
from error import ManifestParseError
|
||||
from error import NoManifestException
|
||||
from error import NoSuchProjectError
|
||||
from error import RepoChangedException
|
||||
from manifest_xml import XmlManifest
|
||||
import gitc_utils
|
||||
from manifest_xml import GitcManifest, XmlManifest
|
||||
from pager import RunPager
|
||||
from wrapper import WrapperPath, Wrapper
|
||||
|
||||
@@ -128,6 +131,12 @@ class _Repo(object):
|
||||
|
||||
cmd.repodir = self.repodir
|
||||
cmd.manifest = XmlManifest(cmd.repodir)
|
||||
cmd.gitc_manifest = None
|
||||
gitc_client_name = gitc_utils.parse_clientdir(os.getcwd())
|
||||
if gitc_client_name:
|
||||
cmd.gitc_manifest = GitcManifest(cmd.repodir, gitc_client_name)
|
||||
cmd.manifest.isGitcClient = True
|
||||
|
||||
Editor.globalConfig = cmd.manifest.globalConfig
|
||||
|
||||
if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
|
||||
@@ -135,6 +144,11 @@ class _Repo(object):
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if isinstance(cmd, RequiresGitcCommand) and not gitc_utils.get_gitc_manifest_dir():
|
||||
print("fatal: '%s' requires GITC to be available" % name,
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
|
||||
try:
|
||||
copts, cargs = cmd.OptionParser.parse_args(argv)
|
||||
copts = cmd.ReadEnvironmentOptions(copts)
|
||||
@@ -173,6 +187,12 @@ class _Repo(object):
|
||||
else:
|
||||
print('error: no project in current directory', file=sys.stderr)
|
||||
result = 1
|
||||
except InvalidProjectGroupsError as e:
|
||||
if e.name:
|
||||
print('error: project group must be enabled for project %s' % e.name, file=sys.stderr)
|
||||
else:
|
||||
print('error: project group must be enabled for the project in the current directory', file=sys.stderr)
|
||||
result = 1
|
||||
finally:
|
||||
elapsed = time.time() - start
|
||||
hours, remainder = divmod(elapsed, 3600)
|
||||
|
||||
@@ -29,6 +29,7 @@ else:
|
||||
urllib = imp.new_module('urllib')
|
||||
urllib.parse = urlparse
|
||||
|
||||
import gitc_utils
|
||||
from git_config import GitConfig
|
||||
from git_refs import R_HEADS, HEAD
|
||||
from project import RemoteSpec, Project, MetaProject
|
||||
@@ -112,6 +113,7 @@ class XmlManifest(object):
|
||||
self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
|
||||
self.globalConfig = GitConfig.ForUser()
|
||||
self.localManifestWarning = False
|
||||
self.isGitcClient = False
|
||||
|
||||
self.repoProject = MetaProject(self, 'repo',
|
||||
gitdir = os.path.join(repodir, 'repo/.git'),
|
||||
@@ -202,6 +204,9 @@ class XmlManifest(object):
|
||||
if d.revisionExpr:
|
||||
have_default = True
|
||||
e.setAttribute('revision', d.revisionExpr)
|
||||
if d.destBranchExpr:
|
||||
have_default = True
|
||||
e.setAttribute('dest-branch', d.destBranchExpr)
|
||||
if d.sync_j > 1:
|
||||
have_default = True
|
||||
e.setAttribute('sync-j', '%d' % d.sync_j)
|
||||
@@ -253,11 +258,13 @@ class XmlManifest(object):
|
||||
else:
|
||||
value = p.work_git.rev_parse(HEAD + '^0')
|
||||
e.setAttribute('revision', value)
|
||||
if peg_rev_upstream and value != p.revisionExpr:
|
||||
# Only save the origin if the origin is not a sha1, and the default
|
||||
# isn't our value, and the if the default doesn't already have that
|
||||
# covered.
|
||||
e.setAttribute('upstream', p.revisionExpr)
|
||||
if peg_rev_upstream:
|
||||
if p.upstream:
|
||||
e.setAttribute('upstream', p.upstream)
|
||||
elif value != p.revisionExpr:
|
||||
# Only save the origin if the origin is not a sha1, and the default
|
||||
# isn't our value
|
||||
e.setAttribute('upstream', p.revisionExpr)
|
||||
else:
|
||||
revision = self.remotes[remoteName].revision or d.revisionExpr
|
||||
if not revision or revision != p.revisionExpr:
|
||||
@@ -265,6 +272,9 @@ class XmlManifest(object):
|
||||
if p.upstream and p.upstream != p.revisionExpr:
|
||||
e.setAttribute('upstream', p.upstream)
|
||||
|
||||
if p.dest_branch and p.dest_branch != d.destBranchExpr:
|
||||
e.setAttribute('dest-branch', p.dest_branch)
|
||||
|
||||
for c in p.copyfiles:
|
||||
ce = doc.createElement('copyfile')
|
||||
ce.setAttribute('src', c.src)
|
||||
@@ -295,6 +305,11 @@ class XmlManifest(object):
|
||||
if p.sync_s:
|
||||
e.setAttribute('sync-s', 'true')
|
||||
|
||||
if p.clone_depth:
|
||||
e.setAttribute('clone-depth', str(p.clone_depth))
|
||||
|
||||
self._output_manifest_project_extras(p, e)
|
||||
|
||||
if p.subprojects:
|
||||
subprojects = set(subp.name for subp in p.subprojects)
|
||||
output_projects(p, e, list(sorted(subprojects)))
|
||||
@@ -312,6 +327,10 @@ class XmlManifest(object):
|
||||
|
||||
doc.writexml(fd, '', ' ', '\n', 'UTF-8')
|
||||
|
||||
def _output_manifest_project_extras(self, p, e):
|
||||
"""Manifests can modify e if they support extra project attributes."""
|
||||
pass
|
||||
|
||||
@property
|
||||
def paths(self):
|
||||
self._Load()
|
||||
@@ -701,7 +720,7 @@ class XmlManifest(object):
|
||||
def _UnjoinName(self, parent_name, name):
|
||||
return os.path.relpath(name, parent_name)
|
||||
|
||||
def _ParseProject(self, node, parent = None):
|
||||
def _ParseProject(self, node, parent = None, **extra_proj_attrs):
|
||||
"""
|
||||
reads a <project> element from the manifest file
|
||||
"""
|
||||
@@ -796,7 +815,8 @@ class XmlManifest(object):
|
||||
clone_depth = clone_depth,
|
||||
upstream = upstream,
|
||||
parent = parent,
|
||||
dest_branch = dest_branch)
|
||||
dest_branch = dest_branch,
|
||||
**extra_proj_attrs)
|
||||
|
||||
for n in node.childNodes:
|
||||
if n.nodeName == 'copyfile':
|
||||
@@ -927,3 +947,26 @@ class XmlManifest(object):
|
||||
diff['added'].append(toProjects[proj])
|
||||
|
||||
return diff
|
||||
|
||||
|
||||
class GitcManifest(XmlManifest):
|
||||
|
||||
def __init__(self, repodir, gitc_client_name):
|
||||
"""Initialize the GitcManifest object."""
|
||||
super(GitcManifest, self).__init__(repodir)
|
||||
self.isGitcClient = True
|
||||
self.gitc_client_name = gitc_client_name
|
||||
self.gitc_client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
|
||||
gitc_client_name)
|
||||
self.manifestFile = os.path.join(self.gitc_client_dir, '.manifest')
|
||||
|
||||
def _ParseProject(self, node, parent = None):
|
||||
"""Override _ParseProject and add support for GITC specific attributes."""
|
||||
return super(GitcManifest, self)._ParseProject(
|
||||
node, parent=parent, old_revision=node.getAttribute('old-revision'))
|
||||
|
||||
def _output_manifest_project_extras(self, p, e):
|
||||
"""Output GITC Specific Project attributes"""
|
||||
if p.old_revision:
|
||||
e.setAttribute('old-revision', str(p.old_revision))
|
||||
|
||||
|
||||
372
project.py
372
project.py
@@ -13,9 +13,9 @@
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import print_function
|
||||
import contextlib
|
||||
import errno
|
||||
import filecmp
|
||||
import glob
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
@@ -30,8 +30,8 @@ import traceback
|
||||
|
||||
from color import Coloring
|
||||
from git_command import GitCommand, git_require
|
||||
from git_config import GitConfig, IsId, GetSchemeFromUrl, ID_RE
|
||||
from error import GitError, HookError, UploadError
|
||||
from git_config import GitConfig, IsId, GetSchemeFromUrl, GetUrlCookieFile, ID_RE
|
||||
from error import GitError, HookError, UploadError, DownloadError
|
||||
from error import ManifestInvalidRevisionError
|
||||
from error import NoManifestException
|
||||
from trace import IsTrace, Trace
|
||||
@@ -63,6 +63,10 @@ def _error(fmt, *args):
|
||||
msg = fmt % args
|
||||
print('error: %s' % msg, file=sys.stderr)
|
||||
|
||||
def _warn(fmt, *args):
|
||||
msg = fmt % args
|
||||
print('warn: %s' % msg, file=sys.stderr)
|
||||
|
||||
def not_rev(r):
|
||||
return '^' + r
|
||||
|
||||
@@ -233,28 +237,60 @@ class _CopyFile(object):
|
||||
_error('Cannot copy file %s to %s', src, dest)
|
||||
|
||||
class _LinkFile(object):
|
||||
def __init__(self, src, dest, abssrc, absdest):
|
||||
def __init__(self, git_worktree, src, dest, relsrc, absdest):
|
||||
self.git_worktree = git_worktree
|
||||
self.src = src
|
||||
self.dest = dest
|
||||
self.abs_src = abssrc
|
||||
self.src_rel_to_dest = relsrc
|
||||
self.abs_dest = absdest
|
||||
|
||||
def _Link(self):
|
||||
src = self.abs_src
|
||||
dest = self.abs_dest
|
||||
def __linkIt(self, relSrc, absDest):
|
||||
# link file if it does not exist or is out of date
|
||||
if not os.path.islink(dest) or os.readlink(dest) != src:
|
||||
if not os.path.islink(absDest) or (os.readlink(absDest) != relSrc):
|
||||
try:
|
||||
# remove existing file first, since it might be read-only
|
||||
if os.path.exists(dest):
|
||||
os.remove(dest)
|
||||
if os.path.exists(absDest):
|
||||
os.remove(absDest)
|
||||
else:
|
||||
dest_dir = os.path.dirname(dest)
|
||||
dest_dir = os.path.dirname(absDest)
|
||||
if not os.path.isdir(dest_dir):
|
||||
os.makedirs(dest_dir)
|
||||
os.symlink(src, dest)
|
||||
os.symlink(relSrc, absDest)
|
||||
except IOError:
|
||||
_error('Cannot link file %s to %s', src, dest)
|
||||
_error('Cannot link file %s to %s', relSrc, absDest)
|
||||
|
||||
def _Link(self):
|
||||
"""Link the self.rel_src_to_dest and self.abs_dest. Handles wild cards
|
||||
on the src linking all of the files in the source in to the destination
|
||||
directory.
|
||||
"""
|
||||
# We use the absSrc to handle the situation where the current directory
|
||||
# is not the root of the repo
|
||||
absSrc = os.path.join(self.git_worktree, self.src)
|
||||
if os.path.exists(absSrc):
|
||||
# Entity exists so just a simple one to one link operation
|
||||
self.__linkIt(self.src_rel_to_dest, self.abs_dest)
|
||||
else:
|
||||
# Entity doesn't exist assume there is a wild card
|
||||
absDestDir = self.abs_dest
|
||||
if os.path.exists(absDestDir) and not os.path.isdir(absDestDir):
|
||||
_error('Link error: src with wildcard, %s must be a directory',
|
||||
absDestDir)
|
||||
else:
|
||||
absSrcFiles = glob.glob(absSrc)
|
||||
for absSrcFile in absSrcFiles:
|
||||
# Create a releative path from source dir to destination dir
|
||||
absSrcDir = os.path.dirname(absSrcFile)
|
||||
relSrcDir = os.path.relpath(absSrcDir, absDestDir)
|
||||
|
||||
# Get the source file name
|
||||
srcFile = os.path.basename(absSrcFile)
|
||||
|
||||
# Now form the final full paths to srcFile. They will be
|
||||
# absolute for the desintaiton and relative for the srouce.
|
||||
absDest = os.path.join(absDestDir, srcFile)
|
||||
relSrc = os.path.join(relSrcDir, srcFile)
|
||||
self.__linkIt(relSrc, absDest)
|
||||
|
||||
class RemoteSpec(object):
|
||||
def __init__(self,
|
||||
@@ -511,6 +547,12 @@ class RepoHook(object):
|
||||
|
||||
|
||||
class Project(object):
|
||||
# These objects can be shared between several working trees.
|
||||
shareable_files = ['description', 'info']
|
||||
shareable_dirs = ['hooks', 'objects', 'rr-cache', 'svn']
|
||||
# These objects can only be used by a single working tree.
|
||||
working_tree_files = ['config', 'packed-refs', 'shallow']
|
||||
working_tree_dirs = ['logs', 'refs']
|
||||
def __init__(self,
|
||||
manifest,
|
||||
name,
|
||||
@@ -529,7 +571,9 @@ class Project(object):
|
||||
upstream=None,
|
||||
parent=None,
|
||||
is_derived=False,
|
||||
dest_branch=None):
|
||||
dest_branch=None,
|
||||
optimized_fetch=False,
|
||||
old_revision=None):
|
||||
"""Init a Project object.
|
||||
|
||||
Args:
|
||||
@@ -551,6 +595,9 @@ class Project(object):
|
||||
is_derived: False if the project was explicitly defined in the manifest;
|
||||
True if the project is a discovered submodule.
|
||||
dest_branch: The branch to which to push changes for review by default.
|
||||
optimized_fetch: If True, when a project is set to a sha1 revision, only
|
||||
fetch from the remote if the sha1 is not present locally.
|
||||
old_revision: saved git commit id for open GITC projects.
|
||||
"""
|
||||
self.manifest = manifest
|
||||
self.name = name
|
||||
@@ -579,6 +626,7 @@ class Project(object):
|
||||
self.upstream = upstream
|
||||
self.parent = parent
|
||||
self.is_derived = is_derived
|
||||
self.optimized_fetch = optimized_fetch
|
||||
self.subprojects = []
|
||||
|
||||
self.snapshots = {}
|
||||
@@ -597,6 +645,7 @@ class Project(object):
|
||||
self.bare_ref = GitRefs(gitdir)
|
||||
self.bare_objdir = self._GitGetByExec(self, bare=True, gitdir=objdir)
|
||||
self.dest_branch = dest_branch
|
||||
self.old_revision = old_revision
|
||||
|
||||
# This will be filled in if a project is later identified to be the
|
||||
# project containing repo hooks.
|
||||
@@ -608,7 +657,7 @@ class Project(object):
|
||||
|
||||
@property
|
||||
def Exists(self):
|
||||
return os.path.isdir(self.gitdir)
|
||||
return os.path.isdir(self.gitdir) and os.path.isdir(self.objdir)
|
||||
|
||||
@property
|
||||
def CurrentBranch(self):
|
||||
@@ -1050,24 +1099,24 @@ class Project(object):
|
||||
tar.extractall(path=path)
|
||||
return True
|
||||
except (IOError, tarfile.TarError) as e:
|
||||
print("error: Cannot extract archive %s: "
|
||||
"%s" % (tarpath, str(e)), file=sys.stderr)
|
||||
_error("Cannot extract archive %s: %s", tarpath, str(e))
|
||||
return False
|
||||
|
||||
def Sync_NetworkHalf(self,
|
||||
quiet=False,
|
||||
is_new=None,
|
||||
current_branch_only=False,
|
||||
force_sync=False,
|
||||
clone_bundle=True,
|
||||
no_tags=False,
|
||||
archive=False):
|
||||
archive=False,
|
||||
optimized_fetch=False):
|
||||
"""Perform only the network IO portion of the sync process.
|
||||
Local working directory/branch state is not affected.
|
||||
"""
|
||||
if archive and not isinstance(self, MetaProject):
|
||||
if self.remote.url.startswith(('http://', 'https://')):
|
||||
print("error: %s: Cannot fetch archives from http/https "
|
||||
"remotes." % self.name, file=sys.stderr)
|
||||
_error("%s: Cannot fetch archives from http/https remotes.", self.name)
|
||||
return False
|
||||
|
||||
name = self.relpath.replace('\\', '/')
|
||||
@@ -1078,7 +1127,7 @@ class Project(object):
|
||||
try:
|
||||
self._FetchArchive(tarpath, cwd=topdir)
|
||||
except GitError as e:
|
||||
print('error: %s' % str(e), file=sys.stderr)
|
||||
_error('%s', e)
|
||||
return False
|
||||
|
||||
# From now on, we only need absolute tarpath
|
||||
@@ -1089,15 +1138,13 @@ class Project(object):
|
||||
try:
|
||||
os.remove(tarpath)
|
||||
except OSError as e:
|
||||
print("warn: Cannot remove archive %s: "
|
||||
"%s" % (tarpath, str(e)), file=sys.stderr)
|
||||
_warn("Cannot remove archive %s: %s", tarpath, str(e))
|
||||
self._CopyAndLinkFiles()
|
||||
return True
|
||||
|
||||
if is_new is None:
|
||||
is_new = not self.Exists
|
||||
if is_new:
|
||||
self._InitGitDir()
|
||||
self._InitGitDir(force_sync=force_sync)
|
||||
else:
|
||||
self._UpdateHooks()
|
||||
self._InitRemote()
|
||||
@@ -1129,8 +1176,9 @@ class Project(object):
|
||||
elif self.manifest.default.sync_c:
|
||||
current_branch_only = True
|
||||
|
||||
has_sha1 = ID_RE.match(self.revisionExpr) and self._CheckForSha1()
|
||||
if (not has_sha1 #Need to fetch since we don't already have this revision
|
||||
need_to_fetch = not (optimized_fetch and \
|
||||
(ID_RE.match(self.revisionExpr) and self._CheckForSha1()))
|
||||
if (need_to_fetch
|
||||
and not self._RemoteFetch(initial=is_new, quiet=quiet, alt_dir=alt_dir,
|
||||
current_branch_only=current_branch_only,
|
||||
no_tags=no_tags)):
|
||||
@@ -1150,6 +1198,8 @@ class Project(object):
|
||||
self._InitHooks()
|
||||
|
||||
def _CopyAndLinkFiles(self):
|
||||
if self.manifest.isGitcClient:
|
||||
return
|
||||
for copyfile in self.copyfiles:
|
||||
copyfile._Copy()
|
||||
for linkfile in self.linkfiles:
|
||||
@@ -1189,11 +1239,11 @@ class Project(object):
|
||||
'revision %s in %s not found' % (self.revisionExpr,
|
||||
self.name))
|
||||
|
||||
def Sync_LocalHalf(self, syncbuf):
|
||||
def Sync_LocalHalf(self, syncbuf, force_sync=False):
|
||||
"""Perform only the local IO portion of the sync process.
|
||||
Network access is not required.
|
||||
"""
|
||||
self._InitWorkTree()
|
||||
self._InitWorkTree(force_sync=force_sync)
|
||||
all_refs = self.bare_ref.all
|
||||
self.CleanPublishedCache(all_refs)
|
||||
revid = self.GetRevisionId(all_refs)
|
||||
@@ -1353,9 +1403,10 @@ class Project(object):
|
||||
|
||||
def AddLinkFile(self, src, dest, absdest):
|
||||
# dest should already be an absolute path, but src is project relative
|
||||
# make src an absolute path
|
||||
abssrc = os.path.join(self.worktree, src)
|
||||
self.linkfiles.append(_LinkFile(src, dest, abssrc, absdest))
|
||||
# make src relative path to dest
|
||||
absdestdir = os.path.dirname(absdest)
|
||||
relsrc = os.path.relpath(os.path.join(self.worktree, src), absdestdir)
|
||||
self.linkfiles.append(_LinkFile(self.worktree, src, dest, relsrc, absdest))
|
||||
|
||||
def AddAnnotation(self, name, value, keep):
|
||||
self.annotations.append(_Annotation(name, value, keep))
|
||||
@@ -1379,9 +1430,11 @@ class Project(object):
|
||||
|
||||
## Branch Management ##
|
||||
|
||||
def StartBranch(self, name):
|
||||
def StartBranch(self, name, branch_merge=''):
|
||||
"""Create a new branch off the manifest's revision.
|
||||
"""
|
||||
if not branch_merge:
|
||||
branch_merge = self.revisionExpr
|
||||
head = self.work_git.GetHead()
|
||||
if head == (R_HEADS + name):
|
||||
return True
|
||||
@@ -1395,9 +1448,9 @@ class Project(object):
|
||||
|
||||
branch = self.GetBranch(name)
|
||||
branch.remote = self.GetRemote(self.remote.name)
|
||||
branch.merge = self.revisionExpr
|
||||
if not branch.merge.startswith('refs/'):
|
||||
branch.merge = R_HEADS + self.revisionExpr
|
||||
branch.merge = branch_merge
|
||||
if not branch.merge.startswith('refs/') and not ID_RE.match(branch_merge):
|
||||
branch.merge = R_HEADS + branch_merge
|
||||
revid = self.GetRevisionId(all_refs)
|
||||
|
||||
if head.startswith(R_HEADS):
|
||||
@@ -1405,7 +1458,6 @@ class Project(object):
|
||||
head = all_refs[head]
|
||||
except KeyError:
|
||||
head = None
|
||||
|
||||
if revid and head and revid == head:
|
||||
ref = os.path.join(self.gitdir, R_HEADS + name)
|
||||
try:
|
||||
@@ -1831,6 +1883,13 @@ class Project(object):
|
||||
|
||||
if depth:
|
||||
cmd.append('--depth=%s' % depth)
|
||||
else:
|
||||
# If this repo has shallow objects, then we don't know which refs have
|
||||
# shallow objects or not. Tell git to unshallow all fetched refs. Don't
|
||||
# do this with projects that don't have shallow objects, since it is less
|
||||
# efficient.
|
||||
if os.path.exists(os.path.join(self.gitdir, 'shallow')):
|
||||
cmd.append('--depth=2147483647')
|
||||
|
||||
if quiet:
|
||||
cmd.append('--quiet')
|
||||
@@ -1855,7 +1914,7 @@ class Project(object):
|
||||
|
||||
if not self.manifest.IsMirror:
|
||||
branch = self.revisionExpr
|
||||
if is_sha1 and depth:
|
||||
if is_sha1 and depth and git_require((1, 8, 3)):
|
||||
# Shallow checkout of a specific commit, fetch from that commit and not
|
||||
# the heads only as the commit might be deeper in the history.
|
||||
spec.append(branch)
|
||||
@@ -1868,16 +1927,6 @@ class Project(object):
|
||||
spec.append(str((u'+%s:' % branch) + remote.ToLocal(branch)))
|
||||
cmd.extend(spec)
|
||||
|
||||
shallowfetch = self.config.GetString('repo.shallowfetch')
|
||||
if shallowfetch and shallowfetch != ' '.join(spec):
|
||||
GitCommand(self, ['fetch', '--depth=2147483647', name]
|
||||
+ shallowfetch.split(),
|
||||
bare=True, ssh_proxy=ssh_proxy).Wait()
|
||||
if depth:
|
||||
self.config.SetString('repo.shallowfetch', ' '.join(spec))
|
||||
else:
|
||||
self.config.SetString('repo.shallowfetch', None)
|
||||
|
||||
ok = False
|
||||
for _i in range(2):
|
||||
gitcmd = GitCommand(self, cmd, bare=True, ssh_proxy=ssh_proxy)
|
||||
@@ -1900,6 +1949,9 @@ class Project(object):
|
||||
# mode, we just tried sync'ing from the upstream field; it doesn't exist, thus
|
||||
# abort the optimization attempt and do a full sync.
|
||||
break
|
||||
elif ret < 0:
|
||||
# Git died with a signal, exit immediately
|
||||
break
|
||||
time.sleep(random.randint(30, 45))
|
||||
|
||||
if initial:
|
||||
@@ -1915,8 +1967,15 @@ class Project(object):
|
||||
# got what we wanted, else trigger a second run of all
|
||||
# refs.
|
||||
if not self._CheckForSha1():
|
||||
return self._RemoteFetch(name=name, current_branch_only=False,
|
||||
initial=False, quiet=quiet, alt_dir=alt_dir)
|
||||
if not depth:
|
||||
# Avoid infinite recursion when depth is True (since depth implies
|
||||
# current_branch_only)
|
||||
return self._RemoteFetch(name=name, current_branch_only=False,
|
||||
initial=False, quiet=quiet, alt_dir=alt_dir)
|
||||
if self.clone_depth:
|
||||
self.clone_depth = None
|
||||
return self._RemoteFetch(name=name, current_branch_only=current_branch_only,
|
||||
initial=False, quiet=quiet, alt_dir=alt_dir)
|
||||
|
||||
return ok
|
||||
|
||||
@@ -1977,7 +2036,7 @@ class Project(object):
|
||||
os.remove(tmpPath)
|
||||
if 'http_proxy' in os.environ and 'darwin' == sys.platform:
|
||||
cmd += ['--proxy', os.environ['http_proxy']]
|
||||
with self._GetBundleCookieFile(srcUrl, quiet) as cookiefile:
|
||||
with GetUrlCookieFile(srcUrl, quiet) as (cookiefile, proxy):
|
||||
if cookiefile:
|
||||
cmd += ['--cookie', cookiefile, '--cookie-jar', cookiefile]
|
||||
if srcUrl.startswith('persistent-'):
|
||||
@@ -2025,40 +2084,6 @@ class Project(object):
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
@contextlib.contextmanager
|
||||
def _GetBundleCookieFile(self, url, quiet):
|
||||
if url.startswith('persistent-'):
|
||||
try:
|
||||
p = subprocess.Popen(
|
||||
['git-remote-persistent-https', '-print_config', url],
|
||||
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
try:
|
||||
prefix = 'http.cookiefile='
|
||||
cookiefile = None
|
||||
for line in p.stdout:
|
||||
line = line.strip()
|
||||
if line.startswith(prefix):
|
||||
cookiefile = line[len(prefix):]
|
||||
break
|
||||
# Leave subprocess open, as cookie file may be transient.
|
||||
if cookiefile:
|
||||
yield cookiefile
|
||||
return
|
||||
finally:
|
||||
p.stdin.close()
|
||||
if p.wait():
|
||||
err_msg = p.stderr.read()
|
||||
if ' -print_config' in err_msg:
|
||||
pass # Persistent proxy doesn't support -print_config.
|
||||
elif not quiet:
|
||||
print(err_msg, file=sys.stderr)
|
||||
except OSError as e:
|
||||
if e.errno == errno.ENOENT:
|
||||
pass # No persistent proxy.
|
||||
raise
|
||||
yield GitConfig.ForUser().GetString('http.cookiefile')
|
||||
|
||||
def _Checkout(self, rev, quiet=False):
|
||||
cmd = ['checkout']
|
||||
if quiet:
|
||||
@@ -2109,52 +2134,77 @@ class Project(object):
|
||||
if GitCommand(self, cmd).Wait() != 0:
|
||||
raise GitError('%s merge %s ' % (self.name, head))
|
||||
|
||||
def _InitGitDir(self, mirror_git=None):
|
||||
if not os.path.exists(self.gitdir):
|
||||
|
||||
def _InitGitDir(self, mirror_git=None, force_sync=False):
|
||||
init_git_dir = not os.path.exists(self.gitdir)
|
||||
init_obj_dir = not os.path.exists(self.objdir)
|
||||
try:
|
||||
# Initialize the bare repository, which contains all of the objects.
|
||||
if not os.path.exists(self.objdir):
|
||||
if init_obj_dir:
|
||||
os.makedirs(self.objdir)
|
||||
self.bare_objdir.init()
|
||||
|
||||
# If we have a separate directory to hold refs, initialize it as well.
|
||||
if self.objdir != self.gitdir:
|
||||
os.makedirs(self.gitdir)
|
||||
self._ReferenceGitDir(self.objdir, self.gitdir, share_refs=False,
|
||||
copy_all=True)
|
||||
if init_git_dir:
|
||||
os.makedirs(self.gitdir)
|
||||
|
||||
mp = self.manifest.manifestProject
|
||||
ref_dir = mp.config.GetString('repo.reference') or ''
|
||||
if init_obj_dir or init_git_dir:
|
||||
self._ReferenceGitDir(self.objdir, self.gitdir, share_refs=False,
|
||||
copy_all=True)
|
||||
try:
|
||||
self._CheckDirReference(self.objdir, self.gitdir, share_refs=False)
|
||||
except GitError as e:
|
||||
if force_sync:
|
||||
print("Retrying clone after deleting %s" % self.gitdir, file=sys.stderr)
|
||||
try:
|
||||
shutil.rmtree(os.path.realpath(self.gitdir))
|
||||
if self.worktree and os.path.exists(
|
||||
os.path.realpath(self.worktree)):
|
||||
shutil.rmtree(os.path.realpath(self.worktree))
|
||||
return self._InitGitDir(mirror_git=mirror_git, force_sync=False)
|
||||
except:
|
||||
raise e
|
||||
raise e
|
||||
|
||||
if ref_dir or mirror_git:
|
||||
if not mirror_git:
|
||||
mirror_git = os.path.join(ref_dir, self.name + '.git')
|
||||
repo_git = os.path.join(ref_dir, '.repo', 'projects',
|
||||
self.relpath + '.git')
|
||||
if init_git_dir:
|
||||
mp = self.manifest.manifestProject
|
||||
ref_dir = mp.config.GetString('repo.reference') or ''
|
||||
|
||||
if os.path.exists(mirror_git):
|
||||
ref_dir = mirror_git
|
||||
if ref_dir or mirror_git:
|
||||
if not mirror_git:
|
||||
mirror_git = os.path.join(ref_dir, self.name + '.git')
|
||||
repo_git = os.path.join(ref_dir, '.repo', 'projects',
|
||||
self.relpath + '.git')
|
||||
|
||||
elif os.path.exists(repo_git):
|
||||
ref_dir = repo_git
|
||||
if os.path.exists(mirror_git):
|
||||
ref_dir = mirror_git
|
||||
|
||||
elif os.path.exists(repo_git):
|
||||
ref_dir = repo_git
|
||||
|
||||
else:
|
||||
ref_dir = None
|
||||
|
||||
if ref_dir:
|
||||
_lwrite(os.path.join(self.gitdir, 'objects/info/alternates'),
|
||||
os.path.join(ref_dir, 'objects') + '\n')
|
||||
|
||||
self._UpdateHooks()
|
||||
|
||||
m = self.manifest.manifestProject.config
|
||||
for key in ['user.name', 'user.email']:
|
||||
if m.Has(key, include_defaults=False):
|
||||
self.config.SetString(key, m.GetString(key))
|
||||
if self.manifest.IsMirror:
|
||||
self.config.SetString('core.bare', 'true')
|
||||
else:
|
||||
ref_dir = None
|
||||
|
||||
if ref_dir:
|
||||
_lwrite(os.path.join(self.gitdir, 'objects/info/alternates'),
|
||||
os.path.join(ref_dir, 'objects') + '\n')
|
||||
|
||||
self._UpdateHooks()
|
||||
|
||||
m = self.manifest.manifestProject.config
|
||||
for key in ['user.name', 'user.email']:
|
||||
if m.Has(key, include_defaults=False):
|
||||
self.config.SetString(key, m.GetString(key))
|
||||
if self.manifest.IsMirror:
|
||||
self.config.SetString('core.bare', 'true')
|
||||
else:
|
||||
self.config.SetString('core.bare', None)
|
||||
self.config.SetString('core.bare', None)
|
||||
except Exception:
|
||||
if init_obj_dir and os.path.exists(self.objdir):
|
||||
shutil.rmtree(self.objdir)
|
||||
if init_git_dir and os.path.exists(self.gitdir):
|
||||
shutil.rmtree(self.gitdir)
|
||||
raise
|
||||
|
||||
def _UpdateHooks(self):
|
||||
if os.path.exists(self.gitdir):
|
||||
@@ -2183,7 +2233,7 @@ class Project(object):
|
||||
if filecmp.cmp(stock_hook, dst, shallow=False):
|
||||
os.remove(dst)
|
||||
else:
|
||||
_error("%s: Not replacing %s hook", self.relpath, name)
|
||||
_warn("%s: Not replacing locally modified %s hook", self.relpath, name)
|
||||
continue
|
||||
try:
|
||||
os.symlink(os.path.relpath(stock_hook, os.path.dirname(dst)), dst)
|
||||
@@ -2228,6 +2278,25 @@ class Project(object):
|
||||
msg = 'manifest set to %s' % self.revisionExpr
|
||||
self.bare_git.symbolic_ref('-m', msg, ref, dst)
|
||||
|
||||
def _CheckDirReference(self, srcdir, destdir, share_refs):
|
||||
symlink_files = self.shareable_files
|
||||
symlink_dirs = self.shareable_dirs
|
||||
if share_refs:
|
||||
symlink_files += self.working_tree_files
|
||||
symlink_dirs += self.working_tree_dirs
|
||||
to_symlink = symlink_files + symlink_dirs
|
||||
for name in set(to_symlink):
|
||||
dst = os.path.realpath(os.path.join(destdir, name))
|
||||
if os.path.lexists(dst):
|
||||
src = os.path.realpath(os.path.join(srcdir, name))
|
||||
# Fail if the links are pointing to the wrong place
|
||||
if src != dst:
|
||||
raise GitError('--force-sync not enabled; cannot overwrite a local '
|
||||
'work tree. If you\'re comfortable with the '
|
||||
'possibility of losing the work tree\'s git metadata,'
|
||||
' use `repo sync --force-sync {0}` to '
|
||||
'proceed.'.format(self.relpath))
|
||||
|
||||
def _ReferenceGitDir(self, gitdir, dotgit, share_refs, copy_all):
|
||||
"""Update |dotgit| to reference |gitdir|, using symlinks where possible.
|
||||
|
||||
@@ -2239,26 +2308,25 @@ class Project(object):
|
||||
copy_all: If true, copy all remaining files from |gitdir| -> |dotgit|.
|
||||
This saves you the effort of initializing |dotgit| yourself.
|
||||
"""
|
||||
# These objects can be shared between several working trees.
|
||||
symlink_files = ['description', 'info']
|
||||
symlink_dirs = ['hooks', 'objects', 'rr-cache', 'svn']
|
||||
symlink_files = self.shareable_files
|
||||
symlink_dirs = self.shareable_dirs
|
||||
if share_refs:
|
||||
# These objects can only be used by a single working tree.
|
||||
symlink_files += ['config', 'packed-refs', 'shallow']
|
||||
symlink_dirs += ['logs', 'refs']
|
||||
symlink_files += self.working_tree_files
|
||||
symlink_dirs += self.working_tree_dirs
|
||||
to_symlink = symlink_files + symlink_dirs
|
||||
|
||||
to_copy = []
|
||||
if copy_all:
|
||||
to_copy = os.listdir(gitdir)
|
||||
|
||||
dotgit = os.path.realpath(dotgit)
|
||||
for name in set(to_copy).union(to_symlink):
|
||||
try:
|
||||
src = os.path.realpath(os.path.join(gitdir, name))
|
||||
dst = os.path.realpath(os.path.join(dotgit, name))
|
||||
dst = os.path.join(dotgit, name)
|
||||
|
||||
if os.path.lexists(dst) and not os.path.islink(dst):
|
||||
raise GitError('cannot overwrite a local work tree')
|
||||
if os.path.lexists(dst):
|
||||
continue
|
||||
|
||||
# If the source dir doesn't exist, create an empty dir.
|
||||
if name in symlink_dirs and not os.path.lexists(src):
|
||||
@@ -2281,26 +2349,44 @@ class Project(object):
|
||||
shutil.copy(src, dst)
|
||||
except OSError as e:
|
||||
if e.errno == errno.EPERM:
|
||||
raise GitError('filesystem must support symlinks')
|
||||
raise DownloadError('filesystem must support symlinks')
|
||||
else:
|
||||
raise
|
||||
|
||||
def _InitWorkTree(self):
|
||||
def _InitWorkTree(self, force_sync=False):
|
||||
dotgit = os.path.join(self.worktree, '.git')
|
||||
if not os.path.exists(dotgit):
|
||||
os.makedirs(dotgit)
|
||||
self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True,
|
||||
copy_all=False)
|
||||
init_dotgit = not os.path.exists(dotgit)
|
||||
try:
|
||||
if init_dotgit:
|
||||
os.makedirs(dotgit)
|
||||
self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True,
|
||||
copy_all=False)
|
||||
|
||||
_lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId())
|
||||
try:
|
||||
self._CheckDirReference(self.gitdir, dotgit, share_refs=True)
|
||||
except GitError as e:
|
||||
if force_sync:
|
||||
try:
|
||||
shutil.rmtree(dotgit)
|
||||
return self._InitWorkTree(force_sync=False)
|
||||
except:
|
||||
raise e
|
||||
raise e
|
||||
|
||||
cmd = ['read-tree', '--reset', '-u']
|
||||
cmd.append('-v')
|
||||
cmd.append(HEAD)
|
||||
if GitCommand(self, cmd).Wait() != 0:
|
||||
raise GitError("cannot initialize work tree")
|
||||
if init_dotgit:
|
||||
_lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId())
|
||||
|
||||
self._CopyAndLinkFiles()
|
||||
cmd = ['read-tree', '--reset', '-u']
|
||||
cmd.append('-v')
|
||||
cmd.append(HEAD)
|
||||
if GitCommand(self, cmd).Wait() != 0:
|
||||
raise GitError("cannot initialize work tree")
|
||||
|
||||
self._CopyAndLinkFiles()
|
||||
except Exception:
|
||||
if init_dotgit:
|
||||
shutil.rmtree(dotgit)
|
||||
raise
|
||||
|
||||
def _gitdir_path(self, path):
|
||||
return os.path.realpath(os.path.join(self.gitdir, path))
|
||||
|
||||
68
repo
68
repo
@@ -108,6 +108,7 @@ S_repo = 'repo' # special repo repository
|
||||
S_manifests = 'manifests' # special manifest repository
|
||||
REPO_MAIN = S_repo + '/main.py' # main script
|
||||
MIN_PYTHON_VERSION = (2, 6) # minimum supported python version
|
||||
GITC_CONFIG_FILE = '/gitc/.config'
|
||||
|
||||
|
||||
import errno
|
||||
@@ -212,14 +213,41 @@ group.add_option('--config-name',
|
||||
dest='config_name', action="store_true", default=False,
|
||||
help='Always prompt for name/e-mail')
|
||||
|
||||
def _GitcInitOptions(init_optparse):
|
||||
init_optparse.set_usage("repo gitc-init -u url -c client [options]")
|
||||
g = init_optparse.add_option_group('GITC options')
|
||||
g.add_option('-f', '--manifest-file',
|
||||
dest='manifest_file',
|
||||
help='Optional manifest file to use for this GITC client.')
|
||||
g.add_option('-c', '--gitc-client',
|
||||
dest='gitc_client',
|
||||
help='The name for the new gitc_client instance.')
|
||||
|
||||
_gitc_manifest_dir = None
|
||||
def get_gitc_manifest_dir():
|
||||
global _gitc_manifest_dir
|
||||
if _gitc_manifest_dir is None:
|
||||
_gitc_manifest_dir = ''
|
||||
try:
|
||||
with open(GITC_CONFIG_FILE, 'r') as gitc_config:
|
||||
for line in gitc_config:
|
||||
match = re.match('gitc_dir=(?P<gitc_manifest_dir>.*)', line)
|
||||
if match:
|
||||
_gitc_manifest_dir = match.group('gitc_manifest_dir')
|
||||
except IOError:
|
||||
pass
|
||||
return _gitc_manifest_dir
|
||||
|
||||
class CloneFailure(Exception):
|
||||
"""Indicate the remote clone of repo itself failed.
|
||||
"""
|
||||
|
||||
|
||||
def _Init(args):
|
||||
def _Init(args, gitc_init=False):
|
||||
"""Installs repo by cloning it over the network.
|
||||
"""
|
||||
if gitc_init:
|
||||
_GitcInitOptions(init_optparse)
|
||||
opt, args = init_optparse.parse_args(args)
|
||||
if args:
|
||||
init_optparse.print_usage()
|
||||
@@ -242,6 +270,23 @@ def _Init(args):
|
||||
raise CloneFailure()
|
||||
|
||||
try:
|
||||
if gitc_init:
|
||||
gitc_manifest_dir = get_gitc_manifest_dir()
|
||||
if not gitc_manifest_dir:
|
||||
_print('fatal: GITC filesystem is not available. Exiting...',
|
||||
file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if not opt.gitc_client:
|
||||
_print('fatal: GITC client (-c) is required.', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
client_dir = os.path.join(gitc_manifest_dir, opt.gitc_client)
|
||||
if not os.path.exists(client_dir):
|
||||
os.makedirs(client_dir)
|
||||
os.chdir(client_dir)
|
||||
if os.path.exists(repodir):
|
||||
# This GITC Client has already initialized repo so continue.
|
||||
return
|
||||
|
||||
os.mkdir(repodir)
|
||||
except OSError as e:
|
||||
if e.errno != errno.EEXIST:
|
||||
@@ -640,6 +685,10 @@ def _ParseArguments(args):
|
||||
|
||||
|
||||
def _Usage():
|
||||
gitc_usage = ""
|
||||
if get_gitc_manifest_dir():
|
||||
gitc_usage = " gitc-init Initialize a GITC Client.\n"
|
||||
|
||||
_print(
|
||||
"""usage: repo COMMAND [ARGS]
|
||||
|
||||
@@ -648,7 +697,8 @@ repo is not yet installed. Use "repo init" to install it here.
|
||||
The most commonly used repo commands are:
|
||||
|
||||
init Install repo in the current working directory
|
||||
help Display detailed help on a command
|
||||
""" + gitc_usage +
|
||||
""" help Display detailed help on a command
|
||||
|
||||
For access to the full online help, install repo ("repo init").
|
||||
""", file=sys.stderr)
|
||||
@@ -660,6 +710,10 @@ def _Help(args):
|
||||
if args[0] == 'init':
|
||||
init_optparse.print_help()
|
||||
sys.exit(0)
|
||||
elif args[0] == 'gitc-init':
|
||||
_GitcInitOptions(init_optparse)
|
||||
init_optparse.print_help()
|
||||
sys.exit(0)
|
||||
else:
|
||||
_print("error: '%s' is not a bootstrap command.\n"
|
||||
' For access to online help, install repo ("repo init").'
|
||||
@@ -725,6 +779,12 @@ def main(orig_args):
|
||||
wrapper_path = os.path.abspath(__file__)
|
||||
my_main, my_git = _RunSelf(wrapper_path)
|
||||
|
||||
cwd = os.getcwd()
|
||||
if get_gitc_manifest_dir() and cwd.startswith(get_gitc_manifest_dir()):
|
||||
_print('error: repo cannot be used in the GITC local manifest directory.'
|
||||
'\nIf you want to work on this GITC client please rerun this '
|
||||
'command from the corresponding client under /gitc/', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if not repo_main:
|
||||
if opt.help:
|
||||
_Usage()
|
||||
@@ -732,11 +792,11 @@ def main(orig_args):
|
||||
_Help(args)
|
||||
if not cmd:
|
||||
_NotInstalled()
|
||||
if cmd == 'init':
|
||||
if cmd == 'init' or cmd == 'gitc-init':
|
||||
if my_git:
|
||||
_SetDefaultsTo(my_git)
|
||||
try:
|
||||
_Init(args)
|
||||
_Init(args, gitc_init=(cmd == 'gitc-init'))
|
||||
except CloneFailure:
|
||||
shutil.rmtree(os.path.join(repodir, S_repo), ignore_errors=True)
|
||||
sys.exit(1)
|
||||
|
||||
@@ -76,6 +76,7 @@ change id will be added.
|
||||
capture_stdout = True,
|
||||
capture_stderr = True)
|
||||
p.stdin.write(new_msg)
|
||||
p.stdin.close()
|
||||
if p.Wait() != 0:
|
||||
print("error: Failed to update commit message", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
@@ -20,6 +20,7 @@ import multiprocessing
|
||||
import re
|
||||
import os
|
||||
import select
|
||||
import signal
|
||||
import sys
|
||||
import subprocess
|
||||
|
||||
@@ -119,6 +120,9 @@ without iterating through the remaining projects.
|
||||
p.add_option('-r', '--regex',
|
||||
dest='regex', action='store_true',
|
||||
help="Execute the command only on projects matching regex or wildcard expression")
|
||||
p.add_option('-g', '--groups',
|
||||
dest='groups',
|
||||
help="Execute the command only on projects matching the specified groups")
|
||||
p.add_option('-c', '--command',
|
||||
help='Command (and arguments) to execute',
|
||||
dest='command',
|
||||
@@ -150,11 +154,15 @@ without iterating through the remaining projects.
|
||||
attributes that we need.
|
||||
|
||||
"""
|
||||
if not self.manifest.IsMirror:
|
||||
lrev = project.GetRevisionId()
|
||||
else:
|
||||
lrev = None
|
||||
return {
|
||||
'name': project.name,
|
||||
'relpath': project.relpath,
|
||||
'remote_name': project.remote.name,
|
||||
'lrev': project.GetRevisionId(),
|
||||
'lrev': lrev,
|
||||
'rrev': project.revisionExpr,
|
||||
'annotations': dict((a.name, a.value) for a in project.annotations),
|
||||
'gitdir': project.gitdir,
|
||||
@@ -200,21 +208,26 @@ without iterating through the remaining projects.
|
||||
mirror = self.manifest.IsMirror
|
||||
rc = 0
|
||||
|
||||
smart_sync_manifest_name = "smart_sync_override.xml"
|
||||
smart_sync_manifest_path = os.path.join(
|
||||
self.manifest.manifestProject.worktree, smart_sync_manifest_name)
|
||||
|
||||
if os.path.isfile(smart_sync_manifest_path):
|
||||
self.manifest.Override(smart_sync_manifest_path)
|
||||
|
||||
if not opt.regex:
|
||||
projects = self.GetProjects(args)
|
||||
projects = self.GetProjects(args, groups=opt.groups)
|
||||
else:
|
||||
projects = self.FindProjects(args)
|
||||
|
||||
os.environ['REPO_COUNT'] = str(len(projects))
|
||||
|
||||
pool = multiprocessing.Pool(opt.jobs)
|
||||
pool = multiprocessing.Pool(opt.jobs, InitWorker)
|
||||
try:
|
||||
config = self.manifest.manifestProject.config
|
||||
results_it = pool.imap(
|
||||
DoWorkWrapper,
|
||||
([mirror, opt, cmd, shell, cnt, config, self._SerializeProject(p)]
|
||||
for cnt, p in enumerate(projects))
|
||||
)
|
||||
self.ProjectArgs(projects, mirror, opt, cmd, shell, config))
|
||||
pool.close()
|
||||
for r in results_it:
|
||||
rc = rc or r
|
||||
@@ -236,12 +249,28 @@ without iterating through the remaining projects.
|
||||
if rc != 0:
|
||||
sys.exit(rc)
|
||||
|
||||
def ProjectArgs(self, projects, mirror, opt, cmd, shell, config):
|
||||
for cnt, p in enumerate(projects):
|
||||
try:
|
||||
project = self._SerializeProject(p)
|
||||
except Exception as e:
|
||||
print('Project list error: %r' % e,
|
||||
file=sys.stderr)
|
||||
return
|
||||
except KeyboardInterrupt:
|
||||
print('Project list interrupted',
|
||||
file=sys.stderr)
|
||||
return
|
||||
yield [mirror, opt, cmd, shell, cnt, config, project]
|
||||
|
||||
class WorkerKeyboardInterrupt(Exception):
|
||||
""" Keyboard interrupt exception for worker processes. """
|
||||
pass
|
||||
|
||||
|
||||
def InitWorker():
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
|
||||
def DoWorkWrapper(args):
|
||||
""" A wrapper around the DoWork() method.
|
||||
|
||||
@@ -263,7 +292,9 @@ def DoWork(project, mirror, opt, cmd, shell, cnt, config):
|
||||
def setenv(name, val):
|
||||
if val is None:
|
||||
val = ''
|
||||
env[name] = val.encode()
|
||||
if hasattr(val, 'encode'):
|
||||
val = val.encode()
|
||||
env[name] = val
|
||||
|
||||
setenv('REPO_PROJECT', project['name'])
|
||||
setenv('REPO_PATH', project['relpath'])
|
||||
|
||||
82
subcmds/gitc_init.py
Normal file
82
subcmds/gitc_init.py
Normal file
@@ -0,0 +1,82 @@
|
||||
#
|
||||
# Copyright (C) 2015 The Android Open Source Project
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import print_function
|
||||
import os
|
||||
import sys
|
||||
|
||||
import gitc_utils
|
||||
from command import RequiresGitcCommand
|
||||
from subcmds import init
|
||||
|
||||
|
||||
class GitcInit(init.Init, RequiresGitcCommand):
|
||||
common = True
|
||||
helpSummary = "Initialize a GITC Client."
|
||||
helpUsage = """
|
||||
%prog [options] [client name]
|
||||
"""
|
||||
helpDescription = """
|
||||
The '%prog' command is ran to initialize a new GITC client for use
|
||||
with the GITC file system.
|
||||
|
||||
This command will setup the client directory, initialize repo, just
|
||||
like repo init does, and then downloads the manifest collection
|
||||
and installs in in the .repo/directory of the GITC client.
|
||||
|
||||
Once this is done, a GITC manifest is generated by pulling the HEAD
|
||||
SHA for each project and generates the properly formatted XML file
|
||||
and installs it as .manifest in the GITC client directory.
|
||||
|
||||
The -c argument is required to specify the GITC client name.
|
||||
|
||||
The optional -f argument can be used to specify the manifest file to
|
||||
use for this GITC client.
|
||||
"""
|
||||
|
||||
def _Options(self, p):
|
||||
super(GitcInit, self)._Options(p)
|
||||
g = p.add_option_group('GITC options')
|
||||
g.add_option('-f', '--manifest-file',
|
||||
dest='manifest_file',
|
||||
help='Optional manifest file to use for this GITC client.')
|
||||
g.add_option('-c', '--gitc-client',
|
||||
dest='gitc_client',
|
||||
help='The name for the new gitc_client instance.')
|
||||
|
||||
def Execute(self, opt, args):
|
||||
if not opt.gitc_client:
|
||||
print('fatal: gitc client (-c) is required', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
self.client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
|
||||
opt.gitc_client)
|
||||
if not os.path.exists(gitc_utils.get_gitc_manifest_dir()):
|
||||
os.makedirs(gitc_utils.get_gitc_manifest_dir())
|
||||
if not os.path.exists(self.client_dir):
|
||||
os.mkdir(self.client_dir)
|
||||
super(GitcInit, self).Execute(opt, args)
|
||||
|
||||
for name, remote in self.manifest.remotes.iteritems():
|
||||
remote.fetchUrl = remote.resolvedFetchUrl
|
||||
|
||||
if opt.manifest_file:
|
||||
if not os.path.exists(opt.manifest_file):
|
||||
print('fatal: Specified manifest file %s does not exist.' %
|
||||
opt.manifest_file)
|
||||
sys.exit(1)
|
||||
self.manifest.Override(opt.manifest_file)
|
||||
gitc_utils.generate_gitc_manifest(self.client_dir, self.manifest)
|
||||
print('Please run `cd %s` to view your GITC client.' %
|
||||
os.path.join(gitc_utils.GITC_FS_ROOT_DIR, opt.gitc_client))
|
||||
@@ -19,7 +19,8 @@ import sys
|
||||
from formatter import AbstractFormatter, DumbWriter
|
||||
|
||||
from color import Coloring
|
||||
from command import PagedCommand, MirrorSafeCommand
|
||||
from command import PagedCommand, MirrorSafeCommand, RequiresGitcCommand
|
||||
import gitc_utils
|
||||
|
||||
class Help(PagedCommand, MirrorSafeCommand):
|
||||
common = False
|
||||
@@ -54,9 +55,17 @@ Displays detailed usage information about a command.
|
||||
def _PrintCommonCommands(self):
|
||||
print('usage: repo COMMAND [ARGS]')
|
||||
print('The most commonly used repo commands are:')
|
||||
|
||||
def gitc_supported(cmd):
|
||||
if not isinstance(cmd, RequiresGitcCommand):
|
||||
return True
|
||||
if gitc_utils.get_gitc_manifest_dir():
|
||||
return True
|
||||
return False
|
||||
|
||||
commandNames = list(sorted([name
|
||||
for name, command in self.commands.items()
|
||||
if command.common]))
|
||||
if command.common and gitc_supported(command)]))
|
||||
|
||||
maxlen = 0
|
||||
for name in commandNames:
|
||||
|
||||
@@ -59,7 +59,8 @@ class Info(PagedCommand):
|
||||
or 'all,-notdefault')
|
||||
|
||||
self.heading("Manifest branch: ")
|
||||
self.headtext(self.manifest.default.revisionExpr)
|
||||
if self.manifest.default.revisionExpr:
|
||||
self.headtext(self.manifest.default.revisionExpr)
|
||||
self.out.nl()
|
||||
self.heading("Manifest merge branch: ")
|
||||
self.headtext(mergeBranch)
|
||||
|
||||
@@ -27,7 +27,7 @@ else:
|
||||
import imp
|
||||
import urlparse
|
||||
urllib = imp.new_module('urllib')
|
||||
urllib.parse = urlparse.urlparse
|
||||
urllib.parse = urlparse
|
||||
|
||||
from color import Coloring
|
||||
from command import InteractiveCommand, MirrorSafeCommand
|
||||
@@ -153,7 +153,7 @@ to update the working directory files.
|
||||
# server where this git is located, so let's save that here.
|
||||
mirrored_manifest_git = None
|
||||
if opt.reference:
|
||||
manifest_git_path = urllib.parse(opt.manifest_url).path[1:]
|
||||
manifest_git_path = urllib.parse.urlparse(opt.manifest_url).path[1:]
|
||||
mirrored_manifest_git = os.path.join(opt.reference, manifest_git_path)
|
||||
if not mirrored_manifest_git.endswith(".git"):
|
||||
mirrored_manifest_git += ".git"
|
||||
|
||||
@@ -35,6 +35,9 @@ This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
|
||||
p.add_option('-r', '--regex',
|
||||
dest='regex', action='store_true',
|
||||
help="Filter the project list based on regex or wildcard matching of strings")
|
||||
p.add_option('-g', '--groups',
|
||||
dest='groups',
|
||||
help="Filter the project list based on the groups the project is in")
|
||||
p.add_option('-f', '--fullpath',
|
||||
dest='fullpath', action='store_true',
|
||||
help="Display the full work tree path instead of the relative path")
|
||||
@@ -62,7 +65,7 @@ This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
|
||||
sys.exit(1)
|
||||
|
||||
if not opt.regex:
|
||||
projects = self.GetProjects(args)
|
||||
projects = self.GetProjects(args, groups=opt.groups)
|
||||
else:
|
||||
projects = self.FindProjects(args)
|
||||
|
||||
|
||||
@@ -14,11 +14,15 @@
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import print_function
|
||||
import os
|
||||
import sys
|
||||
|
||||
from command import Command
|
||||
from git_config import IsId
|
||||
from git_command import git
|
||||
import gitc_utils
|
||||
from progress import Progress
|
||||
from project import SyncBuffer
|
||||
|
||||
class Start(Command):
|
||||
common = True
|
||||
@@ -53,20 +57,50 @@ revision specified in the manifest.
|
||||
print("error: at least one project must be specified", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
all_projects = self.GetProjects(projects)
|
||||
proj_name_to_gitc_proj_dict = {}
|
||||
if self.gitc_manifest:
|
||||
all_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
|
||||
missing_ok=True)
|
||||
for project in all_projects:
|
||||
if project.old_revision:
|
||||
project.already_synced = True
|
||||
else:
|
||||
project.already_synced = False
|
||||
project.old_revision = project.revisionExpr
|
||||
proj_name_to_gitc_proj_dict[project.name] = project
|
||||
project.revisionExpr = None
|
||||
# Save the GITC manifest.
|
||||
gitc_utils.save_manifest(self.gitc_manifest)
|
||||
|
||||
all_projects = self.GetProjects(projects,
|
||||
missing_ok=bool(self.gitc_manifest))
|
||||
pm = Progress('Starting %s' % nb, len(all_projects))
|
||||
for project in all_projects:
|
||||
pm.update()
|
||||
if self.gitc_manifest:
|
||||
gitc_project = proj_name_to_gitc_proj_dict[project.name]
|
||||
# Sync projects that have already been opened.
|
||||
if not gitc_project.already_synced:
|
||||
proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir,
|
||||
project.relpath)
|
||||
project.worktree = proj_localdir
|
||||
if not os.path.exists(proj_localdir):
|
||||
os.makedirs(proj_localdir)
|
||||
project.Sync_NetworkHalf()
|
||||
sync_buf = SyncBuffer(self.manifest.manifestProject.config)
|
||||
project.Sync_LocalHalf(sync_buf)
|
||||
project.revisionExpr = gitc_project.old_revision
|
||||
|
||||
# If the current revision is a specific SHA1 then we can't push back
|
||||
# to it; so substitute with dest_branch if defined, or with manifest
|
||||
# default revision instead.
|
||||
branch_merge = ''
|
||||
if IsId(project.revisionExpr):
|
||||
if project.dest_branch:
|
||||
project.revisionExpr = project.dest_branch
|
||||
branch_merge = project.dest_branch
|
||||
else:
|
||||
project.revisionExpr = self.manifest.default.revisionExpr
|
||||
if not project.StartBranch(nb):
|
||||
branch_merge = self.manifest.default.revisionExpr
|
||||
if not project.StartBranch(nb, branch_merge=branch_merge):
|
||||
err.append(project)
|
||||
pm.end()
|
||||
|
||||
|
||||
206
subcmds/sync.py
206
subcmds/sync.py
@@ -23,18 +23,26 @@ import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
from pyversion import is_python3
|
||||
if is_python3():
|
||||
import http.cookiejar as cookielib
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
import xmlrpc.client
|
||||
else:
|
||||
import cookielib
|
||||
import imp
|
||||
import urllib2
|
||||
import urlparse
|
||||
import xmlrpclib
|
||||
urllib = imp.new_module('urllib')
|
||||
urllib.error = urllib2
|
||||
urllib.parse = urlparse
|
||||
urllib.request = urllib2
|
||||
xmlrpc = imp.new_module('xmlrpc')
|
||||
xmlrpc.client = xmlrpclib
|
||||
|
||||
@@ -57,7 +65,9 @@ except ImportError:
|
||||
multiprocessing = None
|
||||
|
||||
from git_command import GIT, git_require
|
||||
from git_config import GetUrlCookieFile
|
||||
from git_refs import R_HEADS, HEAD
|
||||
import gitc_utils
|
||||
from project import Project
|
||||
from project import RemoteSpec
|
||||
from command import Command, MirrorSafeCommand
|
||||
@@ -119,6 +129,11 @@ credentials.
|
||||
The -f/--force-broken option can be used to proceed with syncing
|
||||
other projects if a project sync fails.
|
||||
|
||||
The --force-sync option can be used to overwrite existing git
|
||||
directories if they have previously been linked to a different
|
||||
object direcotry. WARNING: This may cause data to be lost since
|
||||
refs may be removed when overwriting.
|
||||
|
||||
The --no-clone-bundle option disables any attempt to use
|
||||
$URL/clone.bundle to bootstrap a new Git repository from a
|
||||
resumeable bundle file on a content delivery network. This
|
||||
@@ -131,6 +146,10 @@ of a project from server.
|
||||
The -c/--current-branch option can be used to only fetch objects that
|
||||
are on the branch specified by a project's revision.
|
||||
|
||||
The --optimized-fetch option can be used to only fetch projects that
|
||||
are fixed to a sha1 revision if the sha1 revision does not already
|
||||
exist locally.
|
||||
|
||||
SSH Connections
|
||||
---------------
|
||||
|
||||
@@ -170,6 +189,11 @@ later is required to fix a server side protocol bug.
|
||||
p.add_option('-f', '--force-broken',
|
||||
dest='force_broken', action='store_true',
|
||||
help="continue sync even if a project fails to sync")
|
||||
p.add_option('--force-sync',
|
||||
dest='force_sync', action='store_true',
|
||||
help="overwrite an existing git directory if it needs to "
|
||||
"point to a different object directory. WARNING: this "
|
||||
"may cause loss of data")
|
||||
p.add_option('-l', '--local-only',
|
||||
dest='local_only', action='store_true',
|
||||
help="only update working tree, don't fetch")
|
||||
@@ -206,6 +230,9 @@ later is required to fix a server side protocol bug.
|
||||
p.add_option('--no-tags',
|
||||
dest='no_tags', action='store_true',
|
||||
help="don't fetch tags")
|
||||
p.add_option('--optimized-fetch',
|
||||
dest='optimized_fetch', action='store_true',
|
||||
help='only fetch projects fixed to sha1 if revision does not exist locally')
|
||||
if show_smart:
|
||||
p.add_option('-s', '--smart-sync',
|
||||
dest='smart_sync', action='store_true',
|
||||
@@ -274,8 +301,10 @@ later is required to fix a server side protocol bug.
|
||||
success = project.Sync_NetworkHalf(
|
||||
quiet=opt.quiet,
|
||||
current_branch_only=opt.current_branch_only,
|
||||
force_sync=opt.force_sync,
|
||||
clone_bundle=not opt.no_clone_bundle,
|
||||
no_tags=opt.no_tags, archive=self.manifest.IsArchive)
|
||||
no_tags=opt.no_tags, archive=self.manifest.IsArchive,
|
||||
optimized_fetch=opt.optimized_fetch)
|
||||
self._fetch_times.Set(project, time.time() - start)
|
||||
|
||||
# Lock around all the rest of the code, since printing, updating a set
|
||||
@@ -295,7 +324,9 @@ later is required to fix a server side protocol bug.
|
||||
pm.update()
|
||||
except _FetchError:
|
||||
err_event.set()
|
||||
except:
|
||||
except Exception as e:
|
||||
print('error: Cannot fetch %s (%s: %s)' \
|
||||
% (project.name, type(e).__name__, str(e)), file=sys.stderr)
|
||||
err_event.set()
|
||||
raise
|
||||
finally:
|
||||
@@ -509,6 +540,9 @@ later is required to fix a server side protocol bug.
|
||||
self.manifest.Override(opt.manifest_name)
|
||||
|
||||
manifest_name = opt.manifest_name
|
||||
smart_sync_manifest_name = "smart_sync_override.xml"
|
||||
smart_sync_manifest_path = os.path.join(
|
||||
self.manifest.manifestProject.worktree, smart_sync_manifest_name)
|
||||
|
||||
if opt.smart_sync or opt.smart_tag:
|
||||
if not self.manifest.manifest_server:
|
||||
@@ -530,19 +564,18 @@ later is required to fix a server side protocol bug.
|
||||
try:
|
||||
info = netrc.netrc()
|
||||
except IOError:
|
||||
print('.netrc file does not exist or could not be opened',
|
||||
file=sys.stderr)
|
||||
# .netrc file does not exist or could not be opened
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
parse_result = urllib.parse.urlparse(manifest_server)
|
||||
if parse_result.hostname:
|
||||
username, _account, password = \
|
||||
info.authenticators(parse_result.hostname)
|
||||
except TypeError:
|
||||
# TypeError is raised when the given hostname is not present
|
||||
# in the .netrc file.
|
||||
print('No credentials found for %s in .netrc'
|
||||
% parse_result.hostname, file=sys.stderr)
|
||||
auth = info.authenticators(parse_result.hostname)
|
||||
if auth:
|
||||
username, _account, password = auth
|
||||
else:
|
||||
print('No credentials found for %s in .netrc'
|
||||
% parse_result.hostname, file=sys.stderr)
|
||||
except netrc.NetrcParseError as e:
|
||||
print('Error parsing .netrc file: %s' % e, file=sys.stderr)
|
||||
|
||||
@@ -551,8 +584,12 @@ later is required to fix a server side protocol bug.
|
||||
(username, password),
|
||||
1)
|
||||
|
||||
transport = PersistentTransport(manifest_server)
|
||||
if manifest_server.startswith('persistent-'):
|
||||
manifest_server = manifest_server[len('persistent-'):]
|
||||
|
||||
try:
|
||||
server = xmlrpc.client.Server(manifest_server)
|
||||
server = xmlrpc.client.Server(manifest_server, transport=transport)
|
||||
if opt.smart_sync:
|
||||
p = self.manifest.manifestProject
|
||||
b = p.GetBranch(p.CurrentBranch)
|
||||
@@ -575,17 +612,16 @@ later is required to fix a server side protocol bug.
|
||||
[success, manifest_str] = server.GetManifest(opt.smart_tag)
|
||||
|
||||
if success:
|
||||
manifest_name = "smart_sync_override.xml"
|
||||
manifest_path = os.path.join(self.manifest.manifestProject.worktree,
|
||||
manifest_name)
|
||||
manifest_name = smart_sync_manifest_name
|
||||
try:
|
||||
f = open(manifest_path, 'w')
|
||||
f = open(smart_sync_manifest_path, 'w')
|
||||
try:
|
||||
f.write(manifest_str)
|
||||
finally:
|
||||
f.close()
|
||||
except IOError:
|
||||
print('error: cannot write manifest to %s' % manifest_path,
|
||||
except IOError as e:
|
||||
print('error: cannot write manifest to %s:\n%s'
|
||||
% (smart_sync_manifest_path, e),
|
||||
file=sys.stderr)
|
||||
sys.exit(1)
|
||||
self._ReloadManifest(manifest_name)
|
||||
@@ -602,6 +638,13 @@ later is required to fix a server side protocol bug.
|
||||
% (self.manifest.manifest_server, e.errcode, e.errmsg),
|
||||
file=sys.stderr)
|
||||
sys.exit(1)
|
||||
else: # Not smart sync or smart tag mode
|
||||
if os.path.isfile(smart_sync_manifest_path):
|
||||
try:
|
||||
os.remove(smart_sync_manifest_path)
|
||||
except OSError as e:
|
||||
print('error: failed to remove existing smart sync override manifest: %s' %
|
||||
e, file=sys.stderr)
|
||||
|
||||
rp = self.manifest.repoProject
|
||||
rp.PreSync()
|
||||
@@ -615,7 +658,8 @@ later is required to fix a server side protocol bug.
|
||||
if not opt.local_only:
|
||||
mp.Sync_NetworkHalf(quiet=opt.quiet,
|
||||
current_branch_only=opt.current_branch_only,
|
||||
no_tags=opt.no_tags)
|
||||
no_tags=opt.no_tags,
|
||||
optimized_fetch=opt.optimized_fetch)
|
||||
|
||||
if mp.HasChanges:
|
||||
syncbuf = SyncBuffer(mp.config)
|
||||
@@ -625,6 +669,35 @@ later is required to fix a server side protocol bug.
|
||||
self._ReloadManifest(manifest_name)
|
||||
if opt.jobs is None:
|
||||
self.jobs = self.manifest.default.sync_j
|
||||
|
||||
# TODO (sbasi) - Add support for manifest changes, aka projects
|
||||
# have been added or deleted from the manifest.
|
||||
if self.gitc_manifest:
|
||||
gitc_manifest_projects = self.GetProjects(args,
|
||||
manifest=self.gitc_manifest,
|
||||
missing_ok=True)
|
||||
gitc_projects = []
|
||||
opened_projects = []
|
||||
for project in gitc_manifest_projects:
|
||||
if not project.old_revision:
|
||||
gitc_projects.append(project)
|
||||
else:
|
||||
opened_projects.append(project)
|
||||
|
||||
if gitc_projects and not opt.local_only:
|
||||
print('Updating GITC client: %s' % self.gitc_manifest.gitc_client_name)
|
||||
gitc_utils.generate_gitc_manifest(self.gitc_manifest.gitc_client_dir,
|
||||
self.gitc_manifest,
|
||||
gitc_projects)
|
||||
print('GITC client successfully synced.')
|
||||
|
||||
# The opened projects need to be synced as normal, therefore we
|
||||
# generate a new args list to represent the opened projects.
|
||||
args = []
|
||||
for proj in opened_projects:
|
||||
args.append(os.path.relpath(proj.worktree, os.getcwd()))
|
||||
if not args:
|
||||
return
|
||||
all_projects = self.GetProjects(args,
|
||||
missing_ok=True,
|
||||
submodules_ok=opt.fetch_submodules)
|
||||
@@ -678,7 +751,7 @@ later is required to fix a server side protocol bug.
|
||||
for project in all_projects:
|
||||
pm.update()
|
||||
if project.worktree:
|
||||
project.Sync_LocalHalf(syncbuf)
|
||||
project.Sync_LocalHalf(syncbuf, force_sync=opt.force_sync)
|
||||
pm.end()
|
||||
print(file=sys.stderr)
|
||||
if not syncbuf.Finish():
|
||||
@@ -819,3 +892,96 @@ class _FetchTimes(object):
|
||||
os.remove(self._path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
# This is a replacement for xmlrpc.client.Transport using urllib2
|
||||
# and supporting persistent-http[s]. It cannot change hosts from
|
||||
# request to request like the normal transport, the real url
|
||||
# is passed during initialization.
|
||||
class PersistentTransport(xmlrpc.client.Transport):
|
||||
def __init__(self, orig_host):
|
||||
self.orig_host = orig_host
|
||||
|
||||
def request(self, host, handler, request_body, verbose=False):
|
||||
with GetUrlCookieFile(self.orig_host, not verbose) as (cookiefile, proxy):
|
||||
# Python doesn't understand cookies with the #HttpOnly_ prefix
|
||||
# Since we're only using them for HTTP, copy the file temporarily,
|
||||
# stripping those prefixes away.
|
||||
if cookiefile:
|
||||
tmpcookiefile = tempfile.NamedTemporaryFile()
|
||||
try:
|
||||
with open(cookiefile) as f:
|
||||
for line in f:
|
||||
if line.startswith("#HttpOnly_"):
|
||||
line = line[len("#HttpOnly_"):]
|
||||
tmpcookiefile.write(line)
|
||||
tmpcookiefile.flush()
|
||||
|
||||
cookiejar = cookielib.MozillaCookieJar(tmpcookiefile.name)
|
||||
cookiejar.load()
|
||||
finally:
|
||||
tmpcookiefile.close()
|
||||
else:
|
||||
cookiejar = cookielib.CookieJar()
|
||||
|
||||
proxyhandler = urllib.request.ProxyHandler
|
||||
if proxy:
|
||||
proxyhandler = urllib.request.ProxyHandler({
|
||||
"http": proxy,
|
||||
"https": proxy })
|
||||
|
||||
opener = urllib.request.build_opener(
|
||||
urllib.request.HTTPCookieProcessor(cookiejar),
|
||||
proxyhandler)
|
||||
|
||||
url = urllib.parse.urljoin(self.orig_host, handler)
|
||||
parse_results = urllib.parse.urlparse(url)
|
||||
|
||||
scheme = parse_results.scheme
|
||||
if scheme == 'persistent-http':
|
||||
scheme = 'http'
|
||||
if scheme == 'persistent-https':
|
||||
# If we're proxying through persistent-https, use http. The
|
||||
# proxy itself will do the https.
|
||||
if proxy:
|
||||
scheme = 'http'
|
||||
else:
|
||||
scheme = 'https'
|
||||
|
||||
# Parse out any authentication information using the base class
|
||||
host, extra_headers, _ = self.get_host_info(parse_results.netloc)
|
||||
|
||||
url = urllib.parse.urlunparse((
|
||||
scheme,
|
||||
host,
|
||||
parse_results.path,
|
||||
parse_results.params,
|
||||
parse_results.query,
|
||||
parse_results.fragment))
|
||||
|
||||
request = urllib.request.Request(url, request_body)
|
||||
if extra_headers is not None:
|
||||
for (name, header) in extra_headers:
|
||||
request.add_header(name, header)
|
||||
request.add_header('Content-Type', 'text/xml')
|
||||
try:
|
||||
response = opener.open(request)
|
||||
except urllib.error.HTTPError as e:
|
||||
if e.code == 501:
|
||||
# We may have been redirected through a login process
|
||||
# but our POST turned into a GET. Retry.
|
||||
response = opener.open(request)
|
||||
else:
|
||||
raise
|
||||
|
||||
p, u = xmlrpc.client.getparser()
|
||||
while 1:
|
||||
data = response.read(1024)
|
||||
if not data:
|
||||
break
|
||||
p.feed(data)
|
||||
p.close()
|
||||
return u.close()
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user