Example #1
0
    def _select_hunks(self, creator, file_id, work_tree_lines):
        """Provide diff hunk selection for modified text.

        If self.reporter.invert_diff is True, the diff is inverted so that
        insertions are displayed as removals and vice versa.

        :param creator: a ShelfCreator
        :param file_id: The id of the file to shelve.
        :param work_tree_lines: Line contents of the file in the working tree.
        :return: number of shelved hunks.
        """
        if self.reporter.invert_diff:
            target_lines = work_tree_lines
        else:
            target_lines = self.target_tree.get_file_lines(file_id)
        textfile.check_text_lines(work_tree_lines)
        textfile.check_text_lines(target_lines)
        parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
        final_hunks = []
        if not self.auto:
            offset = 0
            self.diff_writer.write(parsed.get_header())
            for hunk in parsed.hunks:
                self.diff_writer.write(str(hunk))
                selected = self.prompt_bool(self.reporter.vocab['hunk'],
                                            allow_editor=(self.change_editor
                                                          is not None))
                if not self.reporter.invert_diff:
                    selected = (not selected)
                if selected:
                    hunk.mod_pos += offset
                    final_hunks.append(hunk)
                else:
                    offset -= (hunk.mod_range - hunk.orig_range)
        sys.stdout.flush()
        if self.reporter.invert_diff:
            change_count = len(final_hunks)
        else:
            change_count = len(parsed.hunks) - len(final_hunks)
        patched = patches.iter_patched_from_hunks(target_lines,
                                                  final_hunks)
        lines = list(patched)
        return lines, change_count
Example #2
0
    def _select_hunks(self, creator, file_id, work_tree_lines):
        """Provide diff hunk selection for modified text.

        If self.reporter.invert_diff is True, the diff is inverted so that
        insertions are displayed as removals and vice versa.

        :param creator: a ShelfCreator
        :param file_id: The id of the file to shelve.
        :param work_tree_lines: Line contents of the file in the working tree.
        :return: number of shelved hunks.
        """
        if self.reporter.invert_diff:
            target_lines = work_tree_lines
        else:
            target_lines = self.target_tree.get_file_lines(file_id)
        textfile.check_text_lines(work_tree_lines)
        textfile.check_text_lines(target_lines)
        parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
        final_hunks = []
        if not self.auto:
            offset = 0
            self.diff_writer.write(parsed.get_header())
            for hunk in parsed.hunks:
                self.diff_writer.write(str(hunk))
                selected = self.prompt_bool(self.reporter.vocab['hunk'],
                                            allow_editor=(self.change_editor
                                                          is not None))
                if not self.reporter.invert_diff:
                    selected = (not selected)
                if selected:
                    hunk.mod_pos += offset
                    final_hunks.append(hunk)
                else:
                    offset -= (hunk.mod_range - hunk.orig_range)
        sys.stdout.flush()
        if self.reporter.invert_diff:
            change_count = len(final_hunks)
        else:
            change_count = len(parsed.hunks) - len(final_hunks)
        patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
        lines = list(patched)
        return lines, change_count
Example #3
0
 def test_iter_patched_from_hunks(self):
     """Test a few patch files, and make sure they work."""
     files = [
         ('diff-2', 'orig-2', 'mod-2'),
         ('diff-3', 'orig-3', 'mod-3'),
         ('diff-4', 'orig-4', 'mod-4'),
         ('diff-5', 'orig-5', 'mod-5'),
         ('diff-6', 'orig-6', 'mod-6'),
         ('diff-7', 'orig-7', 'mod-7'),
     ]
     for diff, orig, mod in files:
         parsed = parse_patch(self.datafile(diff))
         orig_lines = list(self.datafile(orig))
         mod_lines = list(self.datafile(mod))
         iter_patched = iter_patched_from_hunks(orig_lines, parsed.hunks)
         patched_file = IterableFile(iter_patched)
         count = 0
         for patch_line in patched_file:
             self.assertEqual(patch_line, mod_lines[count])
             count += 1
         self.assertEqual(count, len(mod_lines))
 def test_iter_patched_from_hunks(self):
     """Test a few patch files, and make sure they work."""
     files = [
         ('diff-2', 'orig-2', 'mod-2'),
         ('diff-3', 'orig-3', 'mod-3'),
         ('diff-4', 'orig-4', 'mod-4'),
         ('diff-5', 'orig-5', 'mod-5'),
         ('diff-6', 'orig-6', 'mod-6'),
         ('diff-7', 'orig-7', 'mod-7'),
     ]
     for diff, orig, mod in files:
         parsed = parse_patch(self.datafile(diff))
         orig_lines = list(self.datafile(orig))
         mod_lines = list(self.datafile(mod))
         iter_patched = iter_patched_from_hunks(orig_lines, parsed.hunks)
         patched_file = IterableFile(iter_patched)
         count = 0
         for patch_line in patched_file:
             self.assertEqual(patch_line, mod_lines[count])
             count += 1
         self.assertEqual(count, len(mod_lines))