diff options
Diffstat (limited to 'subcmds/sync.py')
| -rw-r--r-- | subcmds/sync.py | 96 |
1 files changed, 75 insertions, 21 deletions
diff --git a/subcmds/sync.py b/subcmds/sync.py index 082b254f..83c9ad36 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py | |||
| @@ -755,33 +755,87 @@ later is required to fix a server side protocol bug. | |||
| 755 | shutil.copy(os.path.join(pack_dir, fname), bak_fname + '.tmp') | 755 | shutil.copy(os.path.join(pack_dir, fname), bak_fname + '.tmp') |
| 756 | shutil.move(bak_fname + '.tmp', bak_fname) | 756 | shutil.move(bak_fname + '.tmp', bak_fname) |
| 757 | 757 | ||
| 758 | @staticmethod | ||
| 759 | def _GetPreciousObjectsState(project: Project, opt): | ||
| 760 | """Get the preciousObjects state for the project. | ||
| 761 | |||
| 762 | Args: | ||
| 763 | project (Project): the project to examine, and possibly correct. | ||
| 764 | opt (optparse.Values): options given to sync. | ||
| 765 | |||
| 766 | Returns: | ||
| 767 | Expected state of extensions.preciousObjects: | ||
| 768 | False: Should be disabled. (not present) | ||
| 769 | True: Should be enabled. | ||
| 770 | """ | ||
| 771 | if project.use_git_worktrees: | ||
| 772 | return False | ||
| 773 | projects = project.manifest.GetProjectsWithName(project.name, | ||
| 774 | all_manifests=True) | ||
| 775 | if len(projects) == 1: | ||
| 776 | return False | ||
| 777 | relpath = project.RelPath(local=opt.this_manifest_only) | ||
| 778 | if len(projects) > 1: | ||
| 779 | # Objects are potentially shared with another project. | ||
| 780 | # See the logic in Project.Sync_NetworkHalf regarding UseAlternates. | ||
| 781 | # - When False, shared projects share (via symlink) | ||
| 782 | # .repo/project-objects/{PROJECT_NAME}.git as the one-and-only objects | ||
| 783 | # directory. All objects are precious, since there is no project with a | ||
| 784 | # complete set of refs. | ||
| 785 | # - When True, shared projects share (via info/alternates) | ||
| 786 | # .repo/project-objects/{PROJECT_NAME}.git as an alternate object store, | ||
| 787 | # which is written only on the first clone of the project, and is not | ||
| 788 | # written subsequently. (When Sync_NetworkHalf sees that it exists, it | ||
| 789 | # makes sure that the alternates file points there, and uses a | ||
| 790 | # project-local .git/objects directory for all syncs going forward. | ||
| 791 | # We do not support switching between the options. The environment | ||
| 792 | # variable is present for testing and migration only. | ||
| 793 | return not project.UseAlternates | ||
| 794 | print(f'\r{relpath}: project not found in manifest.', file=sys.stderr) | ||
| 795 | return False | ||
| 796 | |||
| 797 | def _RepairPreciousObjectsState(self, project: Project, opt): | ||
| 798 | """Correct the preciousObjects state for the project. | ||
| 799 | |||
| 800 | Args: | ||
| 801 | project (Project): the project to examine, and possibly correct. | ||
| 802 | opt (optparse.Values): options given to sync. | ||
| 803 | """ | ||
| 804 | expected = self._GetPreciousObjectsState(project, opt) | ||
| 805 | actual = project.config.GetBoolean('extensions.preciousObjects') or False | ||
| 806 | relpath = project.RelPath(local = opt.this_manifest_only) | ||
| 807 | |||
| 808 | if (expected != actual and | ||
| 809 | not project.config.GetBoolean('repo.preservePreciousObjects')): | ||
| 810 | # If this is unexpected, log it and repair. | ||
| 811 | Trace(f'{relpath} expected preciousObjects={expected}, got {actual}') | ||
| 812 | if expected: | ||
| 813 | if not opt.quiet: | ||
| 814 | print('\r%s: Shared project %s found, disabling pruning.' % | ||
| 815 | (relpath, project.name)) | ||
| 816 | if git_require((2, 7, 0)): | ||
| 817 | project.EnableRepositoryExtension('preciousObjects') | ||
| 818 | else: | ||
| 819 | # This isn't perfect, but it's the best we can do with old git. | ||
| 820 | print('\r%s: WARNING: shared projects are unreliable when using ' | ||
| 821 | 'old versions of git; please upgrade to git-2.7.0+.' | ||
| 822 | % (relpath,), | ||
| 823 | file=sys.stderr) | ||
| 824 | project.config.SetString('gc.pruneExpire', 'never') | ||
| 825 | else: | ||
| 826 | if not opt.quiet: | ||
| 827 | print(f'\r{relpath}: not shared, disabling pruning.') | ||
| 828 | project.config.SetString('extensions.preciousObjects', None) | ||
| 829 | project.config.SetString('gc.pruneExpire', None) | ||
| 830 | |||
| 758 | def _GCProjects(self, projects, opt, err_event): | 831 | def _GCProjects(self, projects, opt, err_event): |
| 759 | pm = Progress('Garbage collecting', len(projects), delay=False, quiet=opt.quiet) | 832 | pm = Progress('Garbage collecting', len(projects), delay=False, quiet=opt.quiet) |
| 760 | pm.update(inc=0, msg='prescan') | 833 | pm.update(inc=0, msg='prescan') |
| 761 | 834 | ||
| 762 | tidy_dirs = {} | 835 | tidy_dirs = {} |
| 763 | for project in projects: | 836 | for project in projects: |
| 764 | # Make sure pruning never kicks in with shared projects that do not use | 837 | self._RepairPreciousObjectsState(project, opt) |
| 765 | # alternates to avoid corruption. | 838 | |
| 766 | if (not project.use_git_worktrees and | ||
| 767 | len(project.manifest.GetProjectsWithName(project.name, all_manifests=True)) > 1): | ||
| 768 | if project.UseAlternates: | ||
| 769 | # Undo logic set by previous versions of repo. | ||
| 770 | project.config.SetString('extensions.preciousObjects', None) | ||
| 771 | project.config.SetString('gc.pruneExpire', None) | ||
| 772 | else: | ||
| 773 | if not opt.quiet: | ||
| 774 | print('\r%s: Shared project %s found, disabling pruning.' % | ||
| 775 | (project.relpath, project.name)) | ||
| 776 | if git_require((2, 7, 0)): | ||
| 777 | project.EnableRepositoryExtension('preciousObjects') | ||
| 778 | else: | ||
| 779 | # This isn't perfect, but it's the best we can do with old git. | ||
| 780 | print('\r%s: WARNING: shared projects are unreliable when using old ' | ||
| 781 | 'versions of git; please upgrade to git-2.7.0+.' | ||
| 782 | % (project.relpath,), | ||
| 783 | file=sys.stderr) | ||
| 784 | project.config.SetString('gc.pruneExpire', 'never') | ||
| 785 | project.config.SetString('gc.autoDetach', 'false') | 839 | project.config.SetString('gc.autoDetach', 'false') |
| 786 | # Only call git gc once per objdir, but call pack-refs for the remainder. | 840 | # Only call git gc once per objdir, but call pack-refs for the remainder. |
| 787 | if project.objdir not in tidy_dirs: | 841 | if project.objdir not in tidy_dirs: |
