Beispiel #1
0
 def untracked(self):
     """Return the untracked files."""
     output = execute(["git", "ls-files", "--exclude-standard",
                       "--others"])[0]
     if not output:
         return []
     return [line for line in output.splitlines() if line]
Beispiel #2
0
    def violations(self, src_path):
        """
        Return a list of Violations recorded in `src_path`.
        """
        if not any(
                src_path.endswith(ext)
                for ext in self.driver.supported_extensions):
            return []
        if src_path not in self.violations_dict:
            if self.reports:
                self.violations_dict = self.driver.parse_reports(self.reports)
            else:
                if self.driver_tool_installed is None:
                    self.driver_tool_installed = self.driver.installed()
                if not self.driver_tool_installed:
                    raise OSError(f"{self.driver.name} is not installed")
                command = copy.deepcopy(self.driver.command)
                if self.options:
                    command.append(self.options)
                if os.path.exists(src_path):
                    command.append(src_path.encode(
                        sys.getfilesystemencoding()))

                    output = execute(command, self.driver.exit_codes)
                    if self.driver.output_stderr:
                        output = output[1]
                    else:
                        output = output[0]
                    self.violations_dict.update(
                        self.driver.parse_reports([output]))

        return self.violations_dict[src_path]
Beispiel #3
0
 def _git_root(cls):
     """
     Returns the output of `git rev-parse --show-toplevel`, which
     is the absolute path for the git project root.
     """
     command = ['git', 'rev-parse', '--show-toplevel', '--encoding=utf-8']
     git_root = execute(command)[0]
     return git_root.split('\n')[0] if git_root else u''
Beispiel #4
0
 def _git_root(cls):
     """
     Returns the output of `git rev-parse --show-toplevel`, which
     is the absolute path for the git project root.
     """
     command = ["git", "rev-parse", "--show-toplevel", "--encoding=utf-8"]
     git_root = execute(command)[0]
     return git_root.split("\n", maxsplit=1)[0] if git_root else ""
Beispiel #5
0
    def diff_unstaged(self):
        """
        Returns the output of `git diff` with no arguments, which
        is the diff for unstaged changes.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        return execute(self._default_git_args + self._default_diff_args)[0]
Beispiel #6
0
    def diff_staged(self):
        """
        Returns the output of `git diff --cached`, which
        is the diff for staged changes.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        return execute(['git', 'diff', '--cached', '--no-color', '--no-ext-diff'])[0]
Beispiel #7
0
    def diff_unstaged(self):
        """
        Returns the output of `git diff` with no arguments, which
        is the diff for unstaged changes.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        return execute(['git', '-c', 'diff.mnemonicprefix=no', 'diff',
                        '--no-color', '--no-ext-diff'])[0]
Beispiel #8
0
    def diff_staged(self):
        """
        Returns the output of `git diff --cached`, which
        is the diff for staged changes.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        return execute(self._default_git_args + self._default_diff_args +
                       ["--cached"])[0]
Beispiel #9
0
    def diff_unstaged(self):
        """
        Returns the output of `git diff` with no arguments, which
        is the diff for unstaged changes.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        return execute([
            'git', '-c', 'diff.mnemonicprefix=no', '-c', 'diff.noprefix=no',
            'diff', '--no-color', '--no-ext-diff', '-U0'
        ])[0]
Beispiel #10
0
 def _git_root(cls):
     """
     Returns the output of `git rev-parse --show-toplevel`, which
     is the absolute path for the git project root.
     """
     command = ['git', 'rev-parse', '--show-toplevel', '--encoding=utf-8']
     try:
         git_root = execute(command)[0]
     except CommandError as e:
         warnings.warn(str(e))
         return ''
     return git_root.split('\n')[0] if git_root else ''
Beispiel #11
0
    def diff_committed(self, compare_branch="origin/master"):
        """
        Returns the output of `git diff` for committed
        changes not yet in origin/master.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        diff_range = "{branch}{notation}HEAD".format(
            branch=compare_branch, notation=self._range_notation)
        return execute(self._default_git_args + self._default_diff_args +
                       [diff_range])[0]
Beispiel #12
0
    def diff_committed(self, compare_branch='origin/master'):
        """
        Returns the output of `git diff` for committed
        changes not yet in origin/master.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        return execute([
            'git', '-c', 'diff.mnemonicprefix=no', '-c', 'diff.noprefix=no',
            'diff', '{branch}...HEAD'.format(branch=compare_branch),
            '--no-color', '--no-ext-diff'
        ])[0]
Beispiel #13
0
    def diff_committed(self, compare_branch='origin/master'):
        """
        Returns the output of `git diff` for committed
        changes not yet in origin/master.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        return execute([
            'git', 'diff',
            "{branch}...HEAD".format(branch=compare_branch),
            '--no-color',
            '--no-ext-diff'
        ])[0]
Beispiel #14
0
 def violations(self, src_path):
     """
     Return a list of Violations recorded in `src_path`.
     """
     if not any(src_path.endswith(ext) for ext in self.driver.supported_extensions):
         return []
     if src_path not in self.violations_dict:
         if not self.reports:
             if self.driver_tool_installed is None:
                 self.driver_tool_installed = self.driver.installed()
             if not self.driver_tool_installed:
                 raise EnvironmentError("{0} is not installed".format(self.driver.name))
             command = copy.deepcopy(self.driver.command)
             if self.options:
                 command.append(self.options)
             command.append(src_path.encode(sys.getfilesystemencoding()))
             output, _ = execute(command)
             self.reports = [output]
         self.violations_dict = self.driver.parse_reports(self.reports)
     return self.violations_dict[src_path]
Beispiel #15
0
    def diff_committed(self, compare_branch="origin/main"):
        """
        Returns the output of `git diff` for committed
        changes not yet in origin/main.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        diff_range = "{branch}{notation}HEAD".format(
            branch=compare_branch, notation=self._range_notation)
        try:
            return execute(self._default_git_args + self._default_diff_args +
                           [diff_range])[0]
        except CommandError as e:
            if "unknown revision" in str(e):
                raise ValueError(
                    dedent(f"""
                        Could not find the branch to compare to. Does '{compare_branch}' exist?
                        the `--compare-branch` argument allows you to set a different branch.
                    """))
            raise
Beispiel #16
0
    def diff_unstaged(self):
        """
        Returns the output of `git diff` with no arguments, which
        is the diff for unstaged changes.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        return execute(
            [
                "git",
                "-c",
                "diff.mnemonicprefix=no",
                "-c",
                "diff.noprefix=no",
                "diff",
                "--no-color",
                "--no-ext-diff",
                "-U0",
            ]
        )[0]
Beispiel #17
0
    def diff_committed(self, compare_branch="origin/master"):
        """
        Returns the output of `git diff` for committed
        changes not yet in origin/master.

        Raises a `GitDiffError` if `git diff` outputs anything
        to stderr.
        """
        return execute(
            [
                "git",
                "-c",
                "diff.mnemonicprefix=no",
                "-c",
                "diff.noprefix=no",
                "diff",
                "{branch}{notation}HEAD".format(
                    branch=compare_branch, notation=self._range_notation
                ),
                "--no-color",
                "--no-ext-diff",
                "-U0",
            ]
        )[0]