Example #1
0
    def process_diff_selection(self,
                               selected,
                               offset,
                               selection,
                               apply_to_worktree=False):
        """Processes a diff selection and applies changes to git."""
        if selection:
            # qt destroys \r\n and makes it \n with no way of going back.
            # boo!  we work around that here.
            # I think this was win32-specific.  We might want to do
            # this on win32 only (TODO verify)
            if selection not in self.fwd_diff:
                special_selection = selection.replace('\n', '\r\n')
                if special_selection in self.fwd_diff:
                    selection = special_selection
                else:
                    return 0, ''
            start = self.fwd_diff.index(selection)
            end = start + len(selection)
            self.set_diffs_to_range(start, end)
        else:
            self.set_diff_to_offset(offset)
            selected = False

        output = ''
        status = 0
        # Process diff selection only
        if selected:
            encoding = self.config.file_encoding(self.filename)
            for idx in self.selected:
                contents = self.diff_subset(idx, start, end)
                if not contents:
                    continue
                tmpfile = utils.tmp_filename('selection')
                utils.write(tmpfile, contents, encoding=encoding)
                if apply_to_worktree:
                    stat, out = self.model.apply_diff_to_worktree(tmpfile)
                    output += out
                    status = max(status, stat)
                else:
                    stat, out = self.model.apply_diff(tmpfile)
                    output += out
                    status = max(status, stat)
                os.unlink(tmpfile)
        # Process a complete hunk
        else:
            for idx, diff in enumerate(self.diff_sel):
                tmpfile = utils.tmp_filename('patch%02d' % idx)
                if not self.write_diff(tmpfile, idx):
                    continue
                if apply_to_worktree:
                    stat, out = self.model.apply_diff_to_worktree(tmpfile)
                    output += out
                    status = max(status, stat)
                else:
                    stat, out = self.model.apply_diff(tmpfile)
                    output += out
                    status = max(status, stat)
                os.unlink(tmpfile)
        return status, output
Example #2
0
    def process_diff_selection(self, selected, offset, selection,
                               apply_to_worktree=False):
        """Processes a diff selection and applies changes to git."""
        if selection:
            # qt destroys \r\n and makes it \n with no way of going back.
            # boo!  we work around that here.
            # I think this was win32-specific.  We might want to do
            # this on win32 only (TODO verify)
            if selection not in self.fwd_diff:
                special_selection = selection.replace('\n', '\r\n')
                if special_selection in self.fwd_diff:
                    selection = special_selection
                else:
                    return 0, ''
            start = self.fwd_diff.index(selection)
            end = start + len(selection)
            self.set_diffs_to_range(start, end)
        else:
            self.set_diff_to_offset(offset)
            selected = False

        output = ''
        status = 0
        # Process diff selection only
        if selected:
            encoding = self.config.file_encoding(self.filename)
            for idx in self.selected:
                contents = self.diff_subset(idx, start, end)
                if not contents:
                    continue
                tmpfile = utils.tmp_filename('selection')
                utils.write(tmpfile, contents, encoding=encoding)
                if apply_to_worktree:
                    stat, out = self.model.apply_diff_to_worktree(tmpfile)
                    output += out
                    status = max(status, stat)
                else:
                    stat, out = self.model.apply_diff(tmpfile)
                    output += out
                    status = max(status, stat)
                os.unlink(tmpfile)
        # Process a complete hunk
        else:
            for idx, diff in enumerate(self.diff_sel):
                tmpfile = utils.tmp_filename('patch%02d' % idx)
                if not self.write_diff(tmpfile,idx):
                    continue
                if apply_to_worktree:
                    stat, out = self.model.apply_diff_to_worktree(tmpfile)
                    output += out
                    status = max(status, stat)
                else:
                    stat, out = self.model.apply_diff(tmpfile)
                    output += out
                    status = max(status, stat)
                os.unlink(tmpfile)
        return status, output
Example #3
0
    def test_tmp_filename_gives_good_file(self):
        first = utils.tmp_filename('test')
        second = utils.tmp_filename('test')

        self.assertFalse(core.exists(first))
        self.assertFalse(core.exists(second))

        self.assertNotEqual(first, second)
        self.assertTrue(os.path.basename(first).startswith('git-cola-test'))
        self.assertTrue(os.path.basename(second).startswith('git-cola-test'))
Example #4
0
    def test_tmp_filename_gives_good_file(self):
        first = utils.tmp_filename('test')
        second = utils.tmp_filename('test')

        self.assertFalse(core.exists(first))
        self.assertFalse(core.exists(second))

        self.assertNotEqual(first, second)
        self.assertTrue(os.path.basename(first).startswith('git-cola-test'))
        self.assertTrue(os.path.basename(second).startswith('git-cola-test'))
Example #5
0
    def test_tmp_filename_gives_good_file(self):
        utils.tmpdir.func.cache.clear()

        tmpdir = utils.tmpdir()
        first = utils.tmp_filename('test')
        second = utils.tmp_filename('test')

        self.assertNotEqual(first, second)
        self.assertTrue(first.startswith(os.path.join(tmpdir, 'test')))
        self.assertTrue(second.startswith(os.path.join(tmpdir, 'test')))

        shutil.rmtree(tmpdir)
Example #6
0
    def test_tmp_filename_gives_good_file(self):
        utils.tmpdir.func.cache.clear()

        tmpdir = utils.tmpdir()
        first = utils.tmp_filename('test')
        second = utils.tmp_filename('test')

        self.assertNotEqual(first, second)
        self.assertTrue(first.startswith(os.path.join(tmpdir, 'test')))
        self.assertTrue(second.startswith(os.path.join(tmpdir, 'test')))

        shutil.rmtree(tmpdir)
Example #7
0
    def do(self):
        log_msg = N_('Tagging "%(revision)s" as "%(name)s"') % dict(revision=self._revision, name=self._name)
        opts = {}
        try:
            if self._message:
                opts["F"] = utils.tmp_filename("tag-message")
                core.write(opts["F"], self._message)

            if self._sign:
                log_msg += " (%s)" % N_("GPG-signed")
                opts["s"] = True
            else:
                opts["a"] = bool(self._message)
            status, output, err = self.model.git.tag(self._name, self._revision, **opts)
        finally:
            if "F" in opts:
                os.unlink(opts["F"])

        if output:
            log_msg += "\n" + (N_("Output: %s") % output)

        Interaction.log_status(status, log_msg, err)
        if status == 0:
            self.model.update_status()
        return (status, output, err)
Example #8
0
    def do(self):
        log_msg = (N_('Tagging "%(revision)s" as "%(name)s"') %
                   dict(revision=self._revision, name=self._name))
        opts = {}
        if self._message:
            opts['F'] = utils.tmp_filename('tag-message')
            utils.write(opts['F'], self._message)

        if self._sign:
            log_msg += ' (%s)' % N_('GPG-signed')
            opts['s'] = True
            status, output = self.model.git.tag(self._name,
                                                self._revision,
                                                with_status=True,
                                                with_stderr=True,
                                                **opts)
        else:
            opts['a'] = bool(self._message)
            status, output = self.model.git.tag(self._name,
                                                self._revision,
                                                with_status=True,
                                                with_stderr=True,
                                                **opts)
        if 'F' in opts:
            os.unlink(opts['F'])

        if output:
            log_msg += '\n' + N_('Output: %s') % output

        Interaction.log_status(status, log_msg, '')
        if status == 0:
            self.model.update_status()
Example #9
0
    def do(self):
        # Create the commit message file
        msg = self.strip_comments(self.msg)
        tmpfile = utils.tmp_filename('commit-message')
        try:
            core.write(tmpfile, msg)

            # Run 'git commit'
            status, out, err = self.model.git.commit(F=tmpfile,
                                                     v=True,
                                                     gpg_sign=self.sign,
                                                     amend=self.amend,
                                                     no_verify=self.no_verify)
        finally:
            core.unlink(tmpfile)

        if status == 0:
            ResetMode.do(self)
            self.model.set_commitmsg(self.new_commitmsg)
            msg = N_('Created commit: %s') % out
        else:
            msg = N_('Commit failed: %s') % out
        Interaction.log_status(status, msg, err)

        return status, out, err
Example #10
0
    def do(self):
        log_msg = (N_('Tagging "%(revision)s" as "%(name)s"') %
                   dict(revision=self._revision, name=self._name))
        opts = {}
        if self._message:
            opts['F'] = utils.tmp_filename('tag-message')
            core.write(opts['F'], self._message)

        if self._sign:
            log_msg += ' (%s)' % N_('GPG-signed')
            opts['s'] = True
            status, output, err = self.model.git.tag(self._name,
                                                     self._revision, **opts)
        else:
            opts['a'] = bool(self._message)
            status, output, err = self.model.git.tag(self._name,
                                                     self._revision, **opts)
        if 'F' in opts:
            os.unlink(opts['F'])

        if output:
            log_msg += '\n' + (N_('Output: %s') % output)

        Interaction.log_status(status, log_msg, err)
        if status == 0:
            self.model.update_status()
Example #11
0
def test_tmp_filename_gives_good_file():
    try:
        first = utils.tmp_filename('test')
        assert core.exists(first)
        assert os.path.basename(first).startswith('git-cola-test')
    finally:
        os.remove(first)

    try:
        second = utils.tmp_filename('test')
        assert core.exists(second)
        assert os.path.basename(second).startswith('git-cola-test')
    finally:
        os.remove(second)

    assert first != second
Example #12
0
    def do(self):
        parser = DiffParser(self.model.filename, self.model.diff_text)
        if self.has_selection:
            patch = parser.generate_patch(self.first_line_idx,
                                          self.last_line_idx,
                                          reverse=self.reverse)
        else:
            patch = parser.generate_hunk_patch(self.first_line_idx,
                                               reverse=self.reverse)
        if patch is None:
            return

        cfg = gitcfg.current()
        tmp_path = utils.tmp_filename('patch')
        try:
            core.write(tmp_path, patch,
                       encoding=cfg.file_encoding(self.model.filename))
            if self.apply_to_worktree:
                status, out, err = self.model.apply_diff_to_worktree(tmp_path)
            else:
                status, out, err = self.model.apply_diff(tmp_path)
        finally:
            os.unlink(tmp_path)
        Interaction.log_status(status, out, err)
        self.model.update_file_status(update_index=True)
Example #13
0
    def do(self):
        log_msg = 'Tagging: "%s" as "%s"' % (self._revision, self._name)
        opts = {}
        if self._message:
            opts['F'] = utils.tmp_filename('tag-message')
            utils.write(opts['F'], self._message)

        if self._sign:
            log_msg += ', GPG-signed'
            opts['s'] = True
            status, output = self.model.git.tag(self._name,
                                                self._revision,
                                                with_status=True,
                                                with_stderr=True,
                                                **opts)
        else:
            opts['a'] = bool(self._message)
            status, output = self.model.git.tag(self._name,
                                                self._revision,
                                                with_status=True,
                                                with_stderr=True,
                                                **opts)
        if 'F' in opts:
            os.unlink(opts['F'])

        if output:
            log_msg += '\nOutput:\n%s' % output

        _notifier.broadcast(signals.log_cmd, status, log_msg)
        if status == 0:
            self.model.update_status()
Example #14
0
    def do(self):
        # Create the commit message file
        msg = self.strip_comments(self.msg)
        tmpfile = utils.tmp_filename('commit-message')
        try:
            core.write(tmpfile, msg)

            # Run 'git commit'
            status, out, err = self.model.git.commit(F=tmpfile,
                                                     v=True,
                                                     gpg_sign=self.sign,
                                                     amend=self.amend,
                                                     no_verify=self.no_verify)
        finally:
            core.unlink(tmpfile)

        if status == 0:
            ResetMode.do(self)
            self.model.set_commitmsg(self.new_commitmsg)
            msg = N_('Created commit: %s') % out
        else:
            msg = N_('Commit failed: %s') % out
        Interaction.log_status(status, msg, err)

        return status, out, err
Example #15
0
    def do(self):
        log_msg = (N_('Tagging "%(revision)s" as "%(name)s"') %
                   dict(revision=self._revision, name=self._name))
        opts = {}
        try:
            if self._message:
                opts['F'] = utils.tmp_filename('tag-message')
                core.write(opts['F'], self._message)

            if self._sign:
                log_msg += ' (%s)' % N_('GPG-signed')
                opts['s'] = True
            else:
                opts['a'] = bool(self._message)
            status, output, err = self.model.git.tag(self._name,
                                                     self._revision, **opts)
        finally:
            if 'F' in opts:
                os.unlink(opts['F'])

        if output:
            log_msg += '\n' + (N_('Output: %s') % output)

        Interaction.log_status(status, log_msg, err)
        if status == 0:
            self.model.update_status()
        return (status, output, err)
Example #16
0
    def do(self):
        parser = DiffParser(self.model.filename, self.model.diff_text)
        if self.has_selection:
            patch = parser.generate_patch(self.first_line_idx,
                                          self.last_line_idx,
                                          reverse=self.reverse)
        else:
            patch = parser.generate_hunk_patch(self.first_line_idx,
                                               reverse=self.reverse)
        if patch is None:
            return

        cfg = gitcfg.current()
        tmp_path = utils.tmp_filename('patch')
        try:
            core.write(tmp_path, patch,
                       encoding=cfg.file_encoding(self.model.filename))
            if self.apply_to_worktree:
                status, out, err = self.model.apply_diff_to_worktree(tmp_path)
            else:
                status, out, err = self.model.apply_diff(tmp_path)
        finally:
            os.unlink(tmp_path)
        Interaction.log_status(status, out, err)
        self.model.update_file_status(update_index=True)
Example #17
0
    def do(self):
        log_msg = 'Tagging: "%s" as "%s"' % (self._revision, self._name)
        opts = {}
        if self._message:
            opts['F'] = utils.tmp_filename('tag-message')
            utils.write(opts['F'], self._message)

        if self._sign:
            log_msg += ', GPG-signed'
            opts['s'] = True
            status, output = self.model.git.tag(self._name,
                                                self._revision,
                                                with_status=True,
                                                with_stderr=True,
                                                **opts)
        else:
            opts['a'] = bool(self._message)
            status, output = self.model.git.tag(self._name,
                                                self._revision,
                                                with_status=True,
                                                with_stderr=True,
                                                **opts)
        if 'F' in opts:
            os.unlink(opts['F'])

        if output:
            log_msg += '\nOutput:\n%s' % output

        Interaction.log_status(status, log_msg, '')
        if status == 0:
            self.model.update_status()
Example #18
0
def test_select_directory():
    filename = utils.tmp_filename('test')
    try:
        expect = os.path.dirname(filename)
        actual = utils.select_directory([filename])
        assert expect == actual
    finally:
        os.remove(filename)
Example #19
0
def test_select_directory_prefers_directories():
    filename = utils.tmp_filename('test')
    try:
        expect = '.'
        actual = utils.select_directory([filename, '.'])
        assert expect == actual
    finally:
        os.remove(filename)
Example #20
0
 def do(self):
     tmpfile = utils.tmp_filename('commit-message')
     status, output = self.model.commit_with_msg(self.msg, tmpfile, amend=self.amend)
     if status == 0:
         ResetMode.do(self)
         self.model.set_commitmsg(self.new_commitmsg)
         title = 'Commit: '
     else:
         title = 'Commit failed: '
     Interaction.log_status(status, title+output, '')
Example #21
0
 def do(self):
     tmpfile = utils.tmp_filename("commit-message")
     status, output = self.model.commit_with_msg(self.msg, tmpfile, amend=self.amend)
     if status == 0:
         ResetMode.do(self)
         self.model.set_commitmsg(self.new_commitmsg)
         title = "Commit: "
     else:
         title = "Commit failed: "
     _notifier.broadcast(signals.log_cmd, status, title + output)
Example #22
0
 def do(self):
     tmpfile = utils.tmp_filename('commit-message')
     status, output = self.model.commit_with_msg(self.msg, tmpfile, amend=self.amend)
     if status == 0:
         ResetMode.do(self)
         self.model.set_commitmsg(self.new_commitmsg)
         title = 'Commit: '
     else:
         title = 'Commit failed: '
     _notifier.broadcast(signals.log_cmd, status, title+output)
Example #23
0
 def do(self):
     tmpfile = utils.tmp_filename('commit-message')
     status, output = self.model.commit_with_msg(self.msg,
                                                 tmpfile,
                                                 amend=self.amend)
     if status == 0:
         ResetMode.do(self)
         self.model.set_commitmsg(self.new_commitmsg)
         title = 'Commit: '
     else:
         title = 'Commit failed: '
     Interaction.log_status(status, title + output, '')
Example #24
0
    def do(self):
        tmpfile = utils.tmp_filename('commit-message')
        status, output = self.model.commit_with_msg(self.msg, tmpfile, amend=self.amend)
        if status == 0:
            ResetMode.do(self)
            self.model.set_commitmsg(self.new_commitmsg)
            msg = N_('Created commit: %s') % output
        else:
            msg = N_('Commit failed: %s') % output
        Interaction.log_status(status, msg, '')

        return status, output
Example #25
0
    def do(self):
        tmpfile = utils.tmp_filename('commit-message')
        status, out, err = self.model.commit_with_msg(self.msg,
                                                      tmpfile,
                                                      amend=self.amend)
        if status == 0:
            ResetMode.do(self)
            self.model.set_commitmsg(self.new_commitmsg)
            msg = N_('Created commit: %s') % out
        else:
            msg = N_('Commit failed: %s') % out
        Interaction.log_status(status, msg, err)

        return status, out, err
Example #26
0
    def do(self):
        log_msg = 'Tagging: "%s" as "%s"' % (self._revision, self._name)
        opts = {}
        if self._message:
            opts["F"] = utils.tmp_filename("tag-message")
            utils.write(opts["F"], self._message)

        if self._sign:
            log_msg += ", GPG-signed"
            opts["s"] = True
            status, output = self.model.git.tag(self._name, self._revision, with_status=True, with_stderr=True, **opts)
        else:
            opts["a"] = bool(self._message)
            status, output = self.model.git.tag(self._name, self._revision, with_status=True, with_stderr=True, **opts)
        if "F" in opts:
            os.unlink(opts["F"])

        if output:
            log_msg += "\nOutput:\n%s" % output

        _notifier.broadcast(signals.log_cmd, status, log_msg)
        if status == 0:
            self.model.update_status()
Example #27
0
 def test_select_directory(self):
     filename = utils.tmp_filename('test')
     expect = os.path.dirname(filename)
     actual = utils.select_directory([filename])
     self.assertEqual(expect, actual)
Example #28
0
 def test_select_directory(self):
     filename = utils.tmp_filename('test')
     expect = os.path.dirname(filename)
     actual = utils.select_directory([filename])
     self.assertEqual(expect, actual)
Example #29
0
 def test_select_directory_prefers_directories(self):
     filename = utils.tmp_filename('test')
     expect = '.'
     actual = utils.select_directory([filename, '.'])
     self.assertEqual(expect, actual)
Example #30
0
 def test_select_directory_prefers_directories(self):
     filename = utils.tmp_filename('test')
     expect = '.'
     actual = utils.select_directory([filename, '.'])
     self.assertEqual(expect, actual)