A VCS repository archival tool
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

121 rader
5.2 KiB

  1. import codearchiver.core
  2. import codearchiver.subprocess
  3. import datetime
  4. import functools
  5. import hashlib
  6. import logging
  7. import os.path
  8. import shutil
  9. import subprocess
  10. _logger = logging.getLogger(__name__)
  11. class GitIndex(codearchiver.core.Index):
  12. fields = (
  13. codearchiver.core.IndexField(key = 'Based on bundle', required = False, repeatable = True),
  14. codearchiver.core.IndexField(key = 'Ref', required = True, repeatable = True),
  15. codearchiver.core.IndexField(key = 'Root commit', required = True, repeatable = True),
  16. codearchiver.core.IndexField(key = 'Commit', required = False, repeatable = True),
  17. )
  18. class Git(codearchiver.core.Module):
  19. name = 'git'
  20. @staticmethod
  21. def matches(inputUrl):
  22. return inputUrl.url.endswith('.git')
  23. def __init__(self, *args, extraBranches = {}, **kwargs):
  24. super().__init__(*args, **kwargs)
  25. self._extraBranches = extraBranches
  26. def process(self):
  27. directory = self._url.rsplit('/', 1)[1]
  28. if os.path.exists(directory):
  29. _logger.fatal(f'{directory!r} already exists')
  30. raise FileExistsError(f'{directory!r} already exists')
  31. startTime = datetime.datetime.utcnow()
  32. if self._id is None:
  33. self._id = f'git_{self._url.replace("/", "_")}_{startTime:%Y%m%dT%H%M%SZ}'
  34. bundle = f'{self._id}.bundle'
  35. if os.path.exists(bundle):
  36. _logger.fatal(f'{bundle!r} already exists')
  37. raise FileExistsError(f'{bundle!r} already exists')
  38. _logger.info(f'Cloning {self._url} into {directory}')
  39. codearchiver.subprocess.run_with_log(['git', 'clone', '--verbose', '--progress', '--mirror', self._url, directory], env = {**os.environ, 'GIT_TERMINAL_PROMPT': '0'})
  40. if self._extraBranches:
  41. for branch, commit in self._extraBranches.items():
  42. _logger.info(f'Fetching commit {commit} as {branch}')
  43. r, _, _ = codearchiver.subprocess.run_with_log(['git', 'fetch', '--verbose', '--progress', 'origin', commit], cwd = directory, check = False)
  44. if r == 0:
  45. r2, _, _ = codearchiver.subprocess.run_with_log(['git', 'update-ref', f'refs/codearchiver/{branch}', commit, ''], cwd = directory, check = False)
  46. if r2 != 0:
  47. _logger.error(f'Failed to update-ref refs/codearchiver/{branch} to {commit}')
  48. else:
  49. _logger.error(f'Failed to fetch {commit}')
  50. # This leaves over a FETCH_HEAD file, but git-bundle does not care about that, so it can safely be ignored.
  51. _logger.info(f'Collecting repository metadata for index')
  52. _, refs, _ = codearchiver.subprocess.run_with_log(['git', 'show-ref'], cwd = directory)
  53. _, commits, _ = codearchiver.subprocess.run_with_log(['git', 'log', '--reflog', '--all', '--format=format:%H% P'], cwd = directory)
  54. commits = list(map(functools.partial(str.split, sep = ' '), commits.splitlines()))
  55. rootCommits = [c[0] for c in commits if len(c) == 1]
  56. # Check whether there are relevant prior bundles to create an incremental one
  57. # Collect their commits shared with this clone (else `git bundle` complains about 'bad object')
  58. commitSet = set(c[0] for c in commits) # For fast lookup
  59. oldCommits = {} # dict to keep the order reasonable
  60. basedOnBundles = {} # ditto
  61. if self._storage:
  62. for oldBundle in self._storage.search_indices([('Root commit', c) for c in rootCommits]):
  63. if not oldBundle.startswith('git_'): #TODO Is there a more generic and elegant approach?
  64. continue
  65. _logger.info(f'Previous bundle: {oldBundle!r}')
  66. with self._storage.open_index(oldBundle) as fp:
  67. idx = GitIndex.deserialise(fp)
  68. for key, value in idx:
  69. if key == 'Commit' and value in commitSet:
  70. oldCommits[value] = True
  71. basedOnBundles[oldBundle] = True
  72. _logger.info(f'Bundling into {bundle}')
  73. status , _, stderr = codearchiver.subprocess.run_with_log(['git', 'bundle', 'create', '--progress', f'../{bundle}', '--stdin', '--reflog', '--all'], cwd = directory, input = ''.join(f'^{commit}\n' for commit in oldCommits).encode('ascii'), check = False)
  74. if status == 128 and stderr == 'fatal: Refusing to create empty bundle.\n':
  75. # Manually write an empty bundle instead
  76. # Cf. Documentation/technical/bundle-format.txt and Documentation/technical/pack-format.txt in git's repository for details on the formats
  77. _logger.info('Writing empty bundle directly instead')
  78. with open(bundle, 'wb') as fp:
  79. fp.write(b'# v2 git bundle\n') # bundle signature
  80. fp.write(b'\n') # bundle end of prerequisites and refs
  81. packdata = b'PACK' # pack signature
  82. packdata += b'\0\0\0\x02' # pack version
  83. packdata += b'\0\0\0\0' # pack number of objects
  84. fp.write(packdata)
  85. fp.write(hashlib.sha1(packdata).digest()) # pack checksum trailer
  86. elif status != 0:
  87. raise RuntimeError(f'git bundle creation returned with non-zero exit status {status}.')
  88. _logger.info(f'Removing clone')
  89. shutil.rmtree(directory)
  90. index = GitIndex()
  91. for oldBundle in basedOnBundles:
  92. index.append('Based on bundle', oldBundle)
  93. for line in refs.splitlines():
  94. index.append('Ref', line)
  95. for commitHash, *parents in commits:
  96. if commitHash not in oldCommits:
  97. index.append('Commit', commitHash)
  98. if not parents:
  99. index.append('Root commit', commitHash)
  100. return codearchiver.core.Result(id = self._id, files = [(bundle, index)])
  101. def __repr__(self):
  102. return f'{type(self).__module__}.{type(self).__name__}({self._inputUrl!r}, extraBranches = {self._extraBranches!r})'