Skip to content

Instantly share code, notes, and snippets.

@labocho
Last active August 17, 2016 05:26
Show Gist options
  • Select an option

  • Save labocho/8e8633637e2c145a7014 to your computer and use it in GitHub Desktop.

Select an option

Save labocho/8e8633637e2c145a7014 to your computer and use it in GitHub Desktop.
Sublime Text 2/3 command to show result of `git log -p` current file
import sublime, sublime_plugin, subprocess
class GitLogPatchCommand(sublime_plugin.TextCommand):
'''
Show result of `git log -p` current file
# Installation
1. Save to Packages/User/git-log-p.py
2. Edit Packages/User/User.sublime-commands
3. Add `{ "caption": "git log -p current file", "command": "git_log_patch" }` and save
# Usage
Open file in repository, cmd+ctrl+p, select "git log -p current file"
'''
def str(self, s):
try:
# Python 2
return unicode(s, "utf_8")
except NameError:
# Python 3
return str(s, "utf_8")
def run(self, edit):
repository_root = self.view.window().folders()[0]
file_name = self.view.file_name()
proc = subprocess.Popen(["git", "log", "-p", file_name], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, cwd = repository_root)
stdout, stderr = proc.communicate("")
returncode = proc.wait()
if returncode != 0:
sublime.error_message(self.str(stderr))
raise
log_view = self.view.window().new_file()
log_view.set_scratch(True)
log_view.set_name("git log")
log_view.insert(edit, 0, self.str(stdout))
log_view.set_syntax_file('Packages/Git/syntax/Git Commit View.tmLanguage')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment