Beispiel #1
0
def PrepManifestForRepo(git_repo, manifest):
    """Use this to store a local manifest in a git repo suitable for repo.

  The repo tool can only fetch manifests from git repositories. So, to use
  a local manifest file as the basis for a checkout, it must be checked into
  a local git repository.

  Common Usage:
    manifest = CreateOrFetchWondrousManifest()
    with osutils.TempDir() as manifest_git_dir:
      PrepManifestForRepo(manifest_git_dir, manifest)
      repo = RepoRepository(manifest_git_dir, repo_dir)
      repo.Sync()

  Args:
    git_repo: Path at which to create the git repository (directory created, if
              needed). If a tempdir, then cleanup is owned by the caller.
    manifest: Path to existing manifest file to copy into the new git
              repository.
  """
    if not git.IsGitRepo(git_repo):
        git.Init(git_repo)

    new_manifest = os.path.join(git_repo, constants.DEFAULT_MANIFEST)

    shutil.copyfile(manifest, new_manifest)
    git.AddPath(new_manifest)
    message = 'Local repository holding: %s' % manifest

    # Commit new manifest. allow_empty in case it's the same as last manifest.
    git.Commit(git_repo, message, allow_empty=True)
    def testGetGitGitdir_bare(self):
        git.Init(self.fake_git_dir)
        os.makedirs(os.path.join(self.fake_git_dir, 'refs', 'heads'))
        os.makedirs(os.path.join(self.fake_git_dir, 'objects'))
        config_file = os.path.join(self.fake_git_dir, 'config')
        osutils.Touch(config_file)

        ret = git.GetGitGitdir(self.fake_git_dir)
        self.assertEqual(ret, self.fake_git_dir)
    def testGetGitGitdir(self):
        git.Init(self.fake_git_dir)
        os.makedirs(os.path.join(self.fake_git_dir, '.git', 'refs', 'heads'))
        os.makedirs(os.path.join(self.fake_git_dir, '.git', 'objects'))
        other_file = os.path.join(self.fake_git_dir, 'other_file')
        osutils.Touch(other_file)

        ret = git.GetGitGitdir(self.fake_git_dir)
        self.assertEqual(ret, os.path.join(self.fake_git_dir, '.git'))
    def setUp(self):
        self.manifest_dir = os.path.join(self.tempdir, '.repo', 'manifests')

        # Initialize a repo instance here.
        local_repo = os.path.join(constants.SOURCE_ROOT, '.repo/repo/.git')

        # TODO(evanhernandez): This is a hack. Find a way to simplify this test.
        # We used to use the current checkout's manifests.git, but that caused
        # problems in production environemnts.
        remote_manifests = os.path.join(self.tempdir, 'remote',
                                        'manifests.git')
        osutils.SafeMakedirs(remote_manifests)
        git.Init(remote_manifests)
        default_manifest = os.path.join(remote_manifests, 'default.xml')
        osutils.WriteFile(
            default_manifest,
            '<?xml version="1.0" encoding="UTF-8"?><manifest></manifest>')
        git.AddPath(default_manifest)
        git.Commit(remote_manifests, 'dummy commit', allow_empty=True)
        git.CreateBranch(remote_manifests, 'default')
        git.CreateBranch(remote_manifests, 'release-R23-2913.B')
        git.CreateBranch(remote_manifests, 'release-R23-2913.B-suffix')
        git.CreateBranch(remote_manifests, 'firmware-link-')

        # Create a copy of our existing manifests.git, but rewrite it so it
        # looks like a remote manifests.git.  This is to avoid hitting the
        # network, and speeds things up in general.
        local_manifests = 'file://%s' % remote_manifests
        temp_manifests = os.path.join(self.tempdir, 'manifests.git')
        git.RunGit(self.tempdir, ['clone', '-n', '--bare', local_manifests])
        git.RunGit(temp_manifests, [
            'fetch', '-f', '-u', local_manifests,
            'refs/remotes/origin/*:refs/heads/*'
        ])
        git.RunGit(temp_manifests, ['branch', '-D', 'default'])
        cros_build_lib.run([
            'repo',
            'init',
            '-u',
            temp_manifests,
            '--repo-branch',
            'default',
            '--repo-url',
            'file://%s' % local_repo,
        ],
                           cwd=self.tempdir)

        self.active_manifest = os.path.realpath(
            os.path.join(self.tempdir, '.repo', 'manifest.xml'))
    def testDeleteStaleLocks_bare(self):
        git.Init(self.fake_git_dir)
        refs_heads = os.path.join(self.fake_git_dir, 'refs', 'heads')
        os.makedirs(refs_heads)
        objects = os.path.join(self.fake_git_dir, 'objects')
        os.makedirs(objects)
        fake_lock = os.path.join(refs_heads, 'master.lock')
        osutils.Touch(fake_lock)
        os.makedirs(self.fake_path)
        other_file = os.path.join(self.fake_path, 'other_file')
        osutils.Touch(other_file)

        git.DeleteStaleLocks(self.fake_git_dir)
        self.assertExists(refs_heads)
        self.assertExists(objects)
        self.assertExists(other_file)
        self.assertNotExists(fake_lock)
    def testInit(self):
        git.Init(self.fake_path)

        # Should have created the git repo directory, if it didn't exist.
        self.assertExists(self.fake_git_dir)
        self.assertCommandContains(['init'])