Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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
Exemplo n.º 5
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()
Exemplo n.º 6
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)
Exemplo n.º 7
0
def write(path, contents, encoding=None):
    """Writes a raw string to a file."""
    with open(core.encode(path), 'wb') as fh:
        try:
            core.write(fh, core.encode(contents, encoding=encoding))
        except IOError:
            fh.close()
Exemplo n.º 8
0
def write(path, contents, encoding=None):
    """Writes a raw string to a file."""
    with open(core.encode(path), 'wb') as fh:
        try:
            core.write(fh, core.encode(contents, encoding=encoding))
        except IOError:
            fh.close()
Exemplo n.º 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
Exemplo n.º 10
0
 def write_diff(self,filename,which,selected=False,noop=False):
     """Writes a new diff corresponding to the user's selection."""
     if not noop and which < len(self.diff_sel):
         diff = self.diff_sel[which]
         encoding = self.config.file_encoding(self.filename)
         core.write(filename, self.header + '\n' + diff + '\n',
                    encoding=encoding)
         return True
     else:
         return False
Exemplo n.º 11
0
 def save(self, string):
     """Save the instruction sheet"""
     try:
         core.write(self.filename, string)
         status = 0
     except (OSError, IOError, ValueError) as e:
         msg, details = utils.format_exception(e)
         sys.stderr.write(msg + '\n\n' + details)
         status = 128
     return status
Exemplo n.º 12
0
 def write_diff(self,filename,which,selected=False,noop=False):
     """Writes a new diff corresponding to the user's selection."""
     if not noop and which < len(self.diff_sel):
         diff = self.diff_sel[which]
         encoding = self.config.file_encoding(self.filename)
         core.write(filename, self.header + '\n' + diff + '\n',
                    encoding=encoding)
         return True
     else:
         return False
Exemplo n.º 13
0
 def do(self):
     if not self.filenames:
         return
     new_additions = '\n'.join(self.filenames) + '\n'
     for_status = new_additions
     if core.exists('.gitignore'):
         current_list = core.read('.gitignore')
         new_additions = current_list.rstrip() + '\n' + new_additions
     core.write('.gitignore', new_additions)
     Interaction.log_status(0, 'Added to .gitignore:\n%s' % for_status, '')
     self.model.update_file_status()
Exemplo n.º 14
0
 def do(self):
     if not self.filenames:
         return
     new_additions = "\n".join(self.filenames) + "\n"
     for_status = new_additions
     if core.exists(".gitignore"):
         current_list = core.read(".gitignore")
         new_additions = current_list.rstrip() + "\n" + new_additions
     core.write(".gitignore", new_additions)
     Interaction.log_status(0, "Added to .gitignore:\n%s" % for_status, "")
     self.model.update_file_status()
Exemplo n.º 15
0
 def do(self):
     if not self.filenames:
         return
     new_additions = '\n'.join(self.filenames) + '\n'
     for_status = new_additions
     if core.exists('.gitignore'):
         current_list = core.read('.gitignore')
         new_additions = current_list.rstrip() + '\n' + new_additions
     core.write('.gitignore', new_additions)
     Interaction.log_status(0, 'Added to .gitignore:\n%s' % for_status, '')
     self.model.update_file_status()
Exemplo n.º 16
0
    def commit_with_msg(self, msg, tmpfile, amend=False):
        """Creates a git commit."""

        if not msg.endswith('\n'):
            msg += '\n'

        # Create the commit message file
        core.write(tmpfile, msg)

        # Run 'git commit'
        status, out, err = self.git.commit(F=tmpfile, v=True, amend=amend)
        core.unlink(tmpfile)
        return (status, out, err)
Exemplo n.º 17
0
    def commit_with_msg(self, msg, tmpfile, amend=False):
        """Creates a git commit."""

        if not msg.endswith('\n'):
            msg += '\n'

        # Create the commit message file
        core.write(tmpfile, msg)

        # Run 'git commit'
        status, out, err = self.git.commit(F=tmpfile, v=True, amend=amend)
        core.unlink(tmpfile)
        return (status, out, err)
Exemplo n.º 18
0
 def do(self):
     new_additions = ''
     for fname in self.filenames:
         new_additions = new_additions + fname + '\n'
     for_status = new_additions
     if new_additions:
         if core.exists('.gitignore'):
             current_list = core.read('.gitignore')
             new_additions = new_additions + current_list
         core.write('.gitignore', new_additions)
         Interaction.log_status(
                 0, 'Added to .gitignore:\n%s' % for_status, '')
         self.model.update_file_status()
Exemplo n.º 19
0
 def do(self):
     new_additions = ''
     for fname in self.filenames:
         new_additions = new_additions + fname + '\n'
     for_status = new_additions
     if new_additions:
         if core.exists('.gitignore'):
             current_list = core.read('.gitignore')
             new_additions = new_additions + current_list
         core.write('.gitignore', new_additions)
         Interaction.log_status(0, 'Added to .gitignore:\n%s' % for_status,
                                '')
         self.model.update_file_status()
Exemplo n.º 20
0
    def commit_with_msg(self, msg, tmpfile, amend=False):
        """Creates a git commit."""

        if not msg.endswith('\n'):
            msg += '\n'

        # Create the commit message file
        fh = open(tmpfile, 'w')
        core.write(fh, msg)
        fh.close()

        # Run 'git commit'
        status, out = self.git.commit(F=tmpfile, v=True, amend=amend,
                                      with_status=True,
                                      with_stderr=True)
        os.unlink(tmpfile)
        return (status, core.decode(out))
Exemplo n.º 21
0
    def commit_with_msg(self, msg, tmpfile, amend=False):
        """Creates a git commit."""

        if not msg.endswith('\n'):
            msg += '\n'

        # Create the commit message file
        fh = open(tmpfile, 'w')
        core.write(fh, msg)
        fh.close()

        # Run 'git commit'
        status, out = self.git.commit(F=tmpfile, v=True, amend=amend,
                                      with_status=True,
                                      with_stderr=True)
        os.unlink(tmpfile)
        return (status, core.decode(out))
Exemplo n.º 22
0
 def save_commitmsg(self, msg):
     path = self.git.git_path('GIT_COLA_MSG')
     core.write(path, msg)
Exemplo n.º 23
0
 def save_commitmsg(self, msg):
     path = self.git.git_path('GIT_COLA_MSG')
     try:
         core.write(path, msg)
     except:
         pass
Exemplo n.º 24
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 = ''
        error = ''
        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')
                core.write(tmpfile, contents, encoding=encoding)
                if apply_to_worktree:
                    stat, out, err = self.model.apply_diff_to_worktree(tmpfile)
                    output += out
                    error += err
                    status = max(status, stat)
                else:
                    stat, out, err = self.model.apply_diff(tmpfile)
                    output += out
                    error += err
                    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, err = self.model.apply_diff_to_worktree(tmpfile)
                    output += out
                    error += err
                    status = max(status, stat)
                else:
                    stat, out, err = self.model.apply_diff(tmpfile)
                    output += out
                    error += err
                    status = max(status, stat)
                os.unlink(tmpfile)
        return status, output, error
Exemplo n.º 25
0
 def save_commitmsg(self, msg):
     path = self.git.git_path('GIT_COLA_MSG')
     core.write(path, msg)
Exemplo n.º 26
0
def write(path, contents, encoding=None):
    """Writes a raw string to a file."""
    fh = open(core.encode(path), 'wb')
    core.write(fh, core.encode(contents, encoding=encoding))
    fh.close()
Exemplo n.º 27
0
 def save_commitmsg(self, msg):
     path = self.git.git_path('GIT_COLA_MSG')
     try:
         core.write(path, msg)
     except:
         pass
Exemplo n.º 28
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 = ''
        error = ''
        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')
                core.write(tmpfile, contents, encoding=encoding)
                if apply_to_worktree:
                    stat, out, err = self.model.apply_diff_to_worktree(tmpfile)
                    output += out
                    error += err
                    status = max(status, stat)
                else:
                    stat, out, err = self.model.apply_diff(tmpfile)
                    output += out
                    error += err
                    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, err = self.model.apply_diff_to_worktree(tmpfile)
                    output += out
                    error += err
                    status = max(status, stat)
                else:
                    stat, out, err = self.model.apply_diff(tmpfile)
                    output += out
                    error += err
                    status = max(status, stat)
                os.unlink(tmpfile)
        return status, output, error
Exemplo n.º 29
0
def write(path, contents, encoding=None):
    """Writes a raw string to a file."""
    fh = open(core.encode(path), 'wb')
    core.write(fh, core.encode(contents, encoding=encoding))
    fh.close()