mirror of
https://gerrit.googlesource.com/git-repo
synced 2025-07-09 08:29:02 +00:00
Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
163fdbf2fd | |||
555be54790 | |||
c5cd433daf | |||
2a3e15217a | |||
abaa7f312f | |||
7cccfb2cf0 | |||
57f43f4944 | |||
17af578d72 | |||
b1a07b8276 | |||
4e16c24981 | |||
b3d6e67196 | |||
503d66d8af | |||
679bac4bf3 | |||
384b3c5948 |
203
project.py
203
project.py
@ -32,7 +32,7 @@ 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 error import GitError, HookError, UploadError, DownloadError
|
||||
from error import ManifestInvalidRevisionError
|
||||
from error import NoManifestException
|
||||
from trace import IsTrace, Trace
|
||||
@ -544,6 +544,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,
|
||||
@ -645,7 +651,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):
|
||||
@ -1095,6 +1101,7 @@ class Project(object):
|
||||
quiet=False,
|
||||
is_new=None,
|
||||
current_branch_only=False,
|
||||
force_sync=False,
|
||||
clone_bundle=True,
|
||||
no_tags=False,
|
||||
archive=False,
|
||||
@ -1131,11 +1138,10 @@ class Project(object):
|
||||
"%s" % (tarpath, str(e)), file=sys.stderr)
|
||||
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()
|
||||
@ -1228,11 +1234,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)
|
||||
@ -1876,8 +1882,6 @@ class Project(object):
|
||||
cmd.append('--quiet')
|
||||
if not self.worktree:
|
||||
cmd.append('--update-head-ok')
|
||||
if self.manifest.IsMirror:
|
||||
cmd.append('--prune')
|
||||
cmd.append(name)
|
||||
|
||||
# If using depth then we should not get all the tags since they may
|
||||
@ -1897,7 +1901,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)
|
||||
@ -1960,8 +1964,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
|
||||
|
||||
@ -2154,52 +2165,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:
|
||||
print("Retrying clone after deleting %s" % force_sync, file=sys.stderr)
|
||||
if force_sync:
|
||||
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):
|
||||
@ -2273,6 +2309,22 @@ 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')
|
||||
|
||||
def _ReferenceGitDir(self, gitdir, dotgit, share_refs, copy_all):
|
||||
"""Update |dotgit| to reference |gitdir|, using symlinks where possible.
|
||||
|
||||
@ -2284,26 +2336,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):
|
||||
@ -2326,26 +2377,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))
|
||||
|
@ -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)
|
||||
|
@ -119,6 +119,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
|
||||
@ -174,6 +179,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")
|
||||
@ -281,6 +291,7 @@ 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,
|
||||
optimized_fetch=opt.optimized_fetch)
|
||||
@ -303,7 +314,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:
|
||||
@ -696,7 +709,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():
|
||||
|
Reference in New Issue
Block a user