Exemplo n.º 1
0
    def setUp(self):
        self.executive = Executive()
        self.filesystem = FileSystem()
        self.original_cwd = self.filesystem.getcwd()

        # Set up fresh git repository with one commit.
        self.untracking_checkout_path = self._mkdtemp(
            suffix='-git_unittest_untracking')
        self._run(['git', 'init', self.untracking_checkout_path])
        self._chdir(self.untracking_checkout_path)
        self._write_text_file('foo_file', 'foo')
        self._run(['git', 'add', 'foo_file'])
        self._run(['git', 'commit', '-am', 'dummy commit'])
        self.untracking_git = Git(cwd=self.untracking_checkout_path,
                                  filesystem=self.filesystem,
                                  executive=self.executive)

        # Then set up a second git repo that tracks the first one.
        self.tracking_git_checkout_path = self._mkdtemp(
            suffix='-git_unittest_tracking')
        self._run([
            'git', 'clone', '--quiet', self.untracking_checkout_path,
            self.tracking_git_checkout_path
        ])
        self._chdir(self.tracking_git_checkout_path)
        self.tracking_git = Git(cwd=self.tracking_git_checkout_path,
                                filesystem=self.filesystem,
                                executive=self.executive)
Exemplo n.º 2
0
 def git(self, path=None):
     if path:
         return Git(cwd=path,
                    executive=self.executive,
                    filesystem=self.filesystem)
     if not self._git:
         self._git = Git(filesystem=self.filesystem,
                         executive=self.executive)
     return self._git
Exemplo n.º 3
0
    def fix_branch(self):
        git = Git(cwd=self._repo_root)
        status = self._get_local_change_status(git)
        if len(status) == 0:
            _log.info('No local changes.')
            return
        modified_files = {f for (s, f) in status if s != 'D'}
        deleted_files = {f for (s, f) in status if s == 'D'}

        self.update(apply_only=modified_files)
        self.move(apply_only=modified_files)
        try:
            git.commit_locally_with_message('This commit should be squashed.')
        except ScriptError:
            _log.info('move_blink_source.py modified nothing.')
    def move(self, apply_only=None):
        """Move Blink source files.

        Args:
            apply_only: If it's None, move all affected files. Otherwise,
                it should be a set of file paths and this function moves
                only the files in |apply_only|.
        """
        _log.info('Planning renaming ...')
        file_pairs = plan_blink_move(self._fs, [])

        if apply_only:
            file_pairs = [(src, dest) for (src, dest) in file_pairs
                          if 'third_party/WebKit/' + src.replace('\\', '/') in apply_only]
            print 'Update file_pairs = ', file_pairs
        _log.info('Will move %d files', len(file_pairs))

        git = Git(cwd=self._repo_root)
        files_set = self._get_checked_in_files(git)
        for i, (src, dest) in enumerate(file_pairs):
            src_from_repo = self._fs.join('third_party', 'WebKit', src)
            if src_from_repo.replace('\\', '/') not in files_set:
                _log.info('%s is not in the repository', src)
                continue
            dest_from_repo = self._fs.join('third_party', 'blink', dest)
            self._fs.maybe_make_directory(self._repo_root, 'third_party', 'blink', self._fs.dirname(dest))
            if self._options.run_git:
                git.move(src_from_repo, dest_from_repo)
                _log.info('[%d/%d] Git moved %s', i + 1, len(file_pairs), src)
            else:
                self._fs.move(self._fs.join(self._repo_root, src_from_repo),
                              self._fs.join(self._repo_root, dest_from_repo))
                _log.info('[%d/%d] Moved %s', i + 1, len(file_pairs), src)
        if apply_only:
            return

        self._update_single_file_content(
            'build/get_landmines.py',
            [('\ndef main', '  print \'The Great Blink mv for source files (crbug.com/768828)\'\n\ndef main')])

        _log.info('Run run-bindings-tests ...')
        Executive().run_command(['python',
                                 self._fs.join(get_scripts_dir(), 'run-bindings-tests'),
                                 '--reset-results'],
                                cwd=self._repo_root)

        if self._options.run_git:
            _log.info('Make a local commit ...')
            git.commit_locally_with_message("""The Great Blink mv for source files, part 2.

Move and rename files.

NOAUTOREVERT=true
Bug: 768828
""")
Exemplo n.º 5
0
    def move(self):
        _log.info('Planning renaming ...')
        file_pairs = plan_blink_move(self._fs, [])
        _log.info('Will move %d files', len(file_pairs))

        git = Git(cwd=self._repo_root)
        files_set = self._get_checked_in_files(git)
        for i, (src, dest) in enumerate(file_pairs):
            src_from_repo = self._fs.join('third_party', 'WebKit', src)
            if src_from_repo.replace('\\', '/') not in files_set:
                _log.info('%s is not in the repository', src)
                continue
            dest_from_repo = self._fs.join('third_party', 'blink', dest)
            self._fs.maybe_make_directory(self._repo_root, 'third_party',
                                          'blink', self._fs.dirname(dest))
            if self._options.run_git:
                git.move(src_from_repo, dest_from_repo)
                _log.info('[%d/%d] Git moved %s', i + 1, len(file_pairs), src)
            else:
                self._fs.move(self._fs.join(self._repo_root, src_from_repo),
                              self._fs.join(self._repo_root, dest_from_repo))
                _log.info('[%d/%d] Moved %s', i + 1, len(file_pairs), src)
        self._update_single_file_content('build/get_landmines.py', [(
            '\ndef main',
            '  print \'The Great Blink mv for source files (crbug.com/768828)\'\n\ndef main'
        )])

        _log.info('Run run-bindings-tests ...')
        Executive().run_command([
            'python',
            self._fs.join(get_scripts_dir(), 'run-bindings-tests'),
            '--reset-results'
        ],
                                cwd=self._repo_root)

        if self._options.run_git:
            _log.info('Make a local commit ...')
            git.commit_locally_with_message(
                """The Great Blink mv for source files, part 2.

Move and rename files.

Bug: 768828
""")
Exemplo n.º 6
0
 def __init__(self, host, auth_refresh_token_json=None, cwd=None):
     self._host = host
     self._auth_refresh_token_json = auth_refresh_token_json
     self._cwd = cwd
     self._git_executable_name = Git.find_executable_name(
         host.executive, host.platform)
Exemplo n.º 7
0
 def make_git(self):
     git = Git(cwd='.', executive=MockExecutive(), filesystem=MockFileSystem())
     git.read_git_config = lambda *args, **kw: 'MOCKKEY:MOCKVALUE'
     return git
Exemplo n.º 8
0
class GitTestWithRealFilesystemAndExecutive(unittest.TestCase):

    def setUp(self):
        self.executive = Executive()
        self.filesystem = FileSystem()
        self.original_cwd = self.filesystem.getcwd()

        # Set up fresh git repository with one commit.
        self.untracking_checkout_path = self._mkdtemp(suffix='-git_unittest_untracking')
        self._run(['git', 'init', self.untracking_checkout_path])
        self._chdir(self.untracking_checkout_path)
        self._write_text_file('foo_file', 'foo')
        self._run(['git', 'add', 'foo_file'])
        self._run(['git', 'commit', '-am', 'dummy commit'])
        self.untracking_git = Git(cwd=self.untracking_checkout_path, filesystem=self.filesystem, executive=self.executive)

        # Then set up a second git repo that tracks the first one.
        self.tracking_git_checkout_path = self._mkdtemp(suffix='-git_unittest_tracking')
        self._run(['git', 'clone', '--quiet', self.untracking_checkout_path, self.tracking_git_checkout_path])
        self._chdir(self.tracking_git_checkout_path)
        self.tracking_git = Git(cwd=self.tracking_git_checkout_path, filesystem=self.filesystem, executive=self.executive)

    def tearDown(self):
        self._chdir(self.original_cwd)
        self._run(['rm', '-rf', self.tracking_git_checkout_path])
        self._run(['rm', '-rf', self.untracking_checkout_path])

    def _join(self, *comps):
        return self.filesystem.join(*comps)

    def _chdir(self, path):
        self.filesystem.chdir(path)

    def _mkdir(self, path):
        assert not self.filesystem.exists(path)
        self.filesystem.maybe_make_directory(path)

    def _mkdtemp(self, **kwargs):
        return str(self.filesystem.mkdtemp(**kwargs))

    def _remove(self, path):
        self.filesystem.remove(path)

    def _run(self, *args, **kwargs):
        return self.executive.run_command(*args, **kwargs)

    def _write_text_file(self, path, contents):
        self.filesystem.write_text_file(path, contents)

    def test_add_list(self):
        self._chdir(self.untracking_checkout_path)
        git = self.untracking_git
        self._mkdir('added_dir')
        self._write_text_file('added_dir/added_file', 'new stuff')
        print self._run(['ls', 'added_dir'])
        print self._run(['pwd'])
        print self._run(['cat', 'added_dir/added_file'])
        git.add_list(['added_dir/added_file'])
        self.assertIn('added_dir/added_file', git.added_files())

    def test_delete_recursively(self):
        self._chdir(self.untracking_checkout_path)
        git = self.untracking_git
        self._mkdir('added_dir')
        self._write_text_file('added_dir/added_file', 'new stuff')
        git.add_list(['added_dir/added_file'])
        self.assertIn('added_dir/added_file', git.added_files())
        git.delete_list(['added_dir/added_file'])
        self.assertNotIn('added_dir', git.added_files())

    def test_delete_recursively_or_not(self):
        self._chdir(self.untracking_checkout_path)
        git = self.untracking_git
        self._mkdir('added_dir')
        self._write_text_file('added_dir/added_file', 'new stuff')
        self._write_text_file('added_dir/another_added_file', 'more new stuff')
        git.add_list(['added_dir/added_file', 'added_dir/another_added_file'])
        self.assertIn('added_dir/added_file', git.added_files())
        self.assertIn('added_dir/another_added_file', git.added_files())
        git.delete_list(['added_dir/added_file'])
        self.assertIn('added_dir/another_added_file', git.added_files())

    def test_exists(self):
        self._chdir(self.untracking_checkout_path)
        git = self.untracking_git
        self._chdir(git.checkout_root)
        self.assertFalse(git.exists('foo.txt'))
        self._write_text_file('foo.txt', 'some stuff')
        self.assertFalse(git.exists('foo.txt'))
        git.add_list(['foo.txt'])
        git.commit_locally_with_message('adding foo')
        self.assertTrue(git.exists('foo.txt'))
        git.delete_list(['foo.txt'])
        git.commit_locally_with_message('deleting foo')
        self.assertFalse(git.exists('foo.txt'))

    def test_move(self):
        self._chdir(self.untracking_checkout_path)
        git = self.untracking_git
        self._write_text_file('added_file', 'new stuff')
        git.add_list(['added_file'])
        git.move('added_file', 'moved_file')
        self.assertIn('moved_file', git.added_files())

    def test_move_recursive(self):
        self._chdir(self.untracking_checkout_path)
        git = self.untracking_git
        self._mkdir('added_dir')
        self._write_text_file('added_dir/added_file', 'new stuff')
        self._write_text_file('added_dir/another_added_file', 'more new stuff')
        git.add_list(['added_dir'])
        git.move('added_dir', 'moved_dir')
        self.assertIn('moved_dir/added_file', git.added_files())
        self.assertIn('moved_dir/another_added_file', git.added_files())

    def test_remote_branch_ref(self):
        # This tests a protected method. pylint: disable=protected-access
        self.assertEqual(self.tracking_git._remote_branch_ref(), 'refs/remotes/origin/master')
        self._chdir(self.untracking_checkout_path)
        self.assertRaises(ScriptError, self.untracking_git._remote_branch_ref)

    def test_create_patch(self):
        self._chdir(self.tracking_git_checkout_path)
        git = self.tracking_git
        self._write_text_file('test_file_commit1', 'contents')
        self._run(['git', 'add', 'test_file_commit1'])
        git.commit_locally_with_message('message')
        git._patch_order = lambda: ''  # pylint: disable=protected-access
        patch = git.create_patch()
        self.assertNotRegexpMatches(patch, r'Subversion Revision:')

    def test_patches_have_filenames_with_prefixes(self):
        self._chdir(self.tracking_git_checkout_path)
        git = self.tracking_git
        self._write_text_file('test_file_commit1', 'contents')
        self._run(['git', 'add', 'test_file_commit1'])
        git.commit_locally_with_message('message')

        # Even if diff.noprefix is enabled, create_patch() produces diffs with prefixes.
        self._run(['git', 'config', 'diff.noprefix', 'true'])
        git._patch_order = lambda: ''  # pylint: disable=protected-access
        patch = git.create_patch()
        self.assertRegexpMatches(patch, r'^diff --git a/test_file_commit1 b/test_file_commit1')

    def test_rename_files(self):
        self._chdir(self.tracking_git_checkout_path)
        git = self.tracking_git
        git.move('foo_file', 'bar_file')
        git.commit_locally_with_message('message')

    def test_commit_position_from_git_log(self):
        # This tests a protected method. pylint: disable=protected-access
        git_log = """
commit 624c3081c0
Author: foobarbaz1 <*****@*****.**>
Date:   Mon Sep 28 19:10:30 2015 -0700

    Test foo bar baz qux 123.

    BUG=000000

    Review URL: https://codereview.chromium.org/999999999

    Cr-Commit-Position: refs/heads/master@{#1234567}
"""
        self._chdir(self.tracking_git_checkout_path)
        git = self.tracking_git
        self.assertEqual(git._commit_position_from_git_log(git_log), 1234567)

    def test_timestamp_of_revision(self):
        # This tests a protected method. pylint: disable=protected-access
        self._chdir(self.tracking_git_checkout_path)
        git = self.tracking_git
        position_regex = git._commit_position_regex_for_timestamp()
        git.most_recent_log_matching(position_regex, git.checkout_root)
Exemplo n.º 9
0
    def update(self, apply_only=None):
        """Updates contents of files affected by Blink source move.

        Args:
            apply_only: If it's None, updates all affected files. Otherwise,
                it should be a set of file paths and this function updates
                only the files in |apply_only|.
        """
        _log.info('Planning renaming ...')
        file_pairs = plan_blink_move(self._fs, [])
        _log.info('Will move %d files', len(file_pairs))

        self._create_basename_maps(file_pairs)
        dirs = self._update_file_content(apply_only)

        # Updates #includes in files in directories with updated DEPS +
        # third_party/WebKit/{Source,common,public}.
        self._append_unless_upper_dir_exists(
            dirs,
            self._fs.join(self._repo_root, 'third_party', 'WebKit', 'Source'))
        self._append_unless_upper_dir_exists(
            dirs,
            self._fs.join(self._repo_root, 'third_party', 'WebKit', 'common'))
        self._append_unless_upper_dir_exists(
            dirs,
            self._fs.join(self._repo_root, 'third_party', 'WebKit', 'public'))
        self._append_unless_upper_dir_exists(
            dirs,
            self._fs.join(self._repo_root, 'mojo', 'public', 'tools',
                          'bindings', 'generators', 'cpp_templates'))
        self._update_cpp_includes_in_directories(dirs, apply_only)

        # Content update for individual files.
        # The following is a list of tuples.
        #  Tuple: (<file path relative to repo root>, [replacement commands])
        #  Command: a callable object, or
        #           a tuple of (<original string>, <new string>).
        file_replacement_list = [
            ('DEPS', [('src/third_party/WebKit/Source/devtools',
                       'src/third_party/blink/renderer/devtools')]),
            ('WATCHLISTS',
             [('third_party/WebKit/Source', 'third_party/blink/renderer'),
              ('third_party/WebKit/public', 'third_party/blink/public')]),
            ('build/check_gn_headers_whitelist.txt',
             [('third_party/WebKit/Source', 'third_party/blink/renderer'),
              ('third_party/WebKit/public', 'third_party/blink/public'),
              self._update_basename]),
            ('chrome/browser/resources/chromeos/chromevox/tools/jsbundler.py',
             [('third_party/WebKit/Source', 'third_party/blink/renderer')]),
            ('testing/buildbot/gn_isolate_map.pyl',
             [('third_party/WebKit/Source', 'third_party/blink/renderer')]),
            ('third_party/WebKit/Source/BUILD.gn',
             [('$root_gen_dir/third_party/WebKit',
               '$root_gen_dir/third_party/blink')]),
            ('third_party/WebKit/Source/config.gni',
             [('snake_case_source_files = false',
               'snake_case_source_files = true')]),
            ('third_party/WebKit/Source/core/css/CSSProperties.json5',
             [self._update_basename]),
            ('third_party/WebKit/Source/core/css/ComputedStyleExtraFields.json5',
             [self._update_basename]),
            ('third_party/WebKit/Source/core/css/ComputedStyleFieldAliases.json5',
             [self._update_basename]),
            ('third_party/WebKit/Source/core/html/parser/create-html-entity-table',
             [self._update_basename]),
            ('third_party/WebKit/Source/core/inspector/inspector_protocol_config.json',
             [self._update_basename]),
            ('third_party/WebKit/Source/core/probe/CoreProbes.json5',
             [self._update_basename]),
            ('third_party/WebKit/Source/core/testing/InternalSettings.h',
             [('InternalSettingsGenerated.h', 'internal_settings_generated.h')
              ]),
            ('third_party/WebKit/Source/core/testing/Internals.cpp',
             [('InternalRuntimeFlags.h', 'internal_runtime_flags.h')]),
            ('third_party/WebKit/Source/platform/probe/PlatformProbes.json5',
             [self._update_basename]),
            ('third_party/WebKit/public/BUILD.gn',
             [('$root_gen_dir/third_party/WebKit',
               '$root_gen_dir/third_party/blink')]),
            ('third_party/WebKit/public/blink_resources.grd',
             [('../Source/', '../renderer/')]),
            ('third_party/blink/tools/compile_devtools_frontend.py',
             [('\'WebKit\', \'Source\'', '\'blink\', \'renderer\'')]),
            ('tools/android/eclipse/.classpath',
             [('third_party/WebKit/public', 'third_party/blink/public')]),
            ('tools/android/loading/cloud/backend/deploy.sh',
             [('third_party/WebKit/Source', 'third_party/blink/renderer')]),
            ('tools/android/loading/emulation_unittest.py',
             [('third_party/WebKit/Source', 'third_party/blink/renderer')]),
            ('tools/android/loading/options.py',
             [('third_party/WebKit/Source', 'third_party/blink/renderer')]),
            ('tools/android/loading/request_track.py',
             [('third_party/WebKit/Source', 'third_party/blink/renderer')]),
            ('tools/gritsettings/resource_ids',
             [('third_party/WebKit/public', 'third_party/blink/public'),
              ('third_party/WebKit/Source', 'third_party/blink/renderer')]),
            ('tools/metrics/actions/extract_actions.py',
             [('third_party/WebKit/Source', 'third_party/blink/renderer')]),
            ('tools/metrics/histograms/update_editor_commands.py',
             [('third_party/WebKit/Source/core/editing/EditorCommand.cpp',
               'third_party/blink/renderer/core/editing/editor_command.cc')]),
            ('tools/metrics/histograms/update_use_counter_css.py',
             [('third_party/WebKit/Source/core/frame/UseCounter.cpp',
               'third_party/blink/renderer/core/frame/use_counter.cc')]),
            ('tools/metrics/histograms/update_use_counter_feature_enum.py',
             [('third_party/WebKit/public', 'third_party/blink/public')]),
        ]
        for file_path, replacement_list in file_replacement_list:
            if not apply_only or file_path in apply_only:
                self._update_single_file_content(
                    file_path,
                    replacement_list,
                    should_write=self._options.run)

        if self._options.run:
            _log.info('Formatting updated %d files ...',
                      len(self._updated_files))
            git = Git(cwd=self._repo_root)
            # |git cl format| can't handle too many files at once.
            while len(self._updated_files) > 0:
                end_index = 100
                if end_index > len(self._updated_files):
                    end_index = len(self._updated_files)
                git.run(['cl', 'format'] + self._updated_files[:end_index])
                self._updated_files = self._updated_files[end_index:]

            if not apply_only:
                _log.info('Make a local commit ...')
                git.commit_locally_with_message(
                    """The Great Blink mv for source files, part 1.

Update file contents without moving files.

NOAUTOREVERT=true
Bug: 768828
""")
Exemplo n.º 10
0
 def make_git(self):
     git = Git(cwd='.',
               executive=MockExecutive(),
               filesystem=MockFileSystem())
     return git
Exemplo n.º 11
0
 def _create_git(self):
     return Git(cwd=self._repo_root,
                filesystem=self._fs,
                platform=self._platform)