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)
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)
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
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()
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)
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()
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
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
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()
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()
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)
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()
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()
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))
def save_commitmsg(self, msg): path = self.git.git_path('GIT_COLA_MSG') core.write(path, msg)
def save_commitmsg(self, msg): path = self.git.git_path('GIT_COLA_MSG') try: core.write(path, msg) except: pass
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
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()
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