opening git diff in sublime using python for custom command

The core.editor of my git is sublime, but I am launching sublime in my custom git command made in python, so how can i pass git diff HEAD^ HEAD to sublime as argument in python

I have stored the value of core.editor in configdb['core.editor'] which i can launch using subprocess.Popen but passing git diff HEAD^ HEAD as argument opens 4 tabs with title git, diff, HEAD^, HEAD... how should I make any sublime launched with git diff into which i can add my own message that I can store in a variable using python.

# Read git config file
configFile, _ = execGitCommand('git config --list')
configDb = {}
for line in filter(None, configFile.split("\n")):
    configDb[line.split("=")[0]] = line.split("=")[-1]

now configDb['core.editor'] = /Applications/Sublime_Text.app/Contents/SharedSupport/bin/subl -n -w

and then

diff = subprocess.Popen(['git', 'diff', 'HEAD^', 'HEAD'], stdout=subprocess.PIPE)
msg, err, = subprocess.Popen(configDb['core.editor'].split(" ")[0], stdin=diff.stdout)

but executing the last line above does opens the diff in sublime but gives below error

Traceback (most recent call last):
  File "/Users/san/Development/executables//git-ipush", line 186, in <module>
    sys.exit(main())
  File "/Users/san/Development/executables//git-ipush", line 43, in main
    preCheck(args)
  File "/Users/san/Development/executables//git-ipush", line 55, in preCheck
    msg, err, = subprocess.Popen(configDb['core.editor'].split(" ")[0], stdin=diff.stdout)
TypeError: 'Popen' object is not utterable

and terminal is now not waiting for sublime to finish editing, but it should as I am passing -w flag as you can see above. The code is a part of this git-command

Answers


The following worked for me in the Python console of Sublime Text 3 (after I had already set my current working directory to my git working copy):

    >>> import subprocess
    >>> diff = subprocess.Popen(['git', 'diff', 'HEAD^', 'HEAD'], stdout=subprocess.PIPE)
    >>> subprocess.Popen(['/usr/local/bin/subl'], stdin=diff.stdout)

edit: OK, I think I understand what you want now. You want to edit the text of the diff before you send it into Sublime, right? The following worked for me:

import subprocess, tempfile
diff = subprocess.Popen(['git', 'diff', 'HEAD^', 'HEAD'], stdout=subprocess.PIPE)

with tempfile.TemporaryFile() as f:
    f.write('Hello, World\n')
    f.write(diff.stdout.read())
    f.seek(0)
    subprocess.Popen(['/usr/local/bin/subl'], stdin=f)

This is just a sample. If you actually want to modify the contents of the diff itself, you can read it into a string first. Note that you can't use StringIO (which I think would be better), because somewhere in the second Popen somebody needs a fileno.

edit 2: Here's how you get the text from Sublime into a variable

import sublime

window = sublime.active_window()
view = window.active_view()
region = sublime.Region(0, view.size())
text = view.substr(region)

Need Your Help

jQuery scrollTop not scrolling to last-child

javascript jquery html css

I am creating a chat system. What I would like to have happen is when the div begins overflow, force it to stay on the last element. I have tried several suggestions from other questions on here, ...

using ftp4j to connect using FTPES to IBM MVS OS/390 server

java python m2crypto ftps ftp4j

I am trying to connect FTPES server (IBM MVS OS/390), when trying with ftp4j I am getting PASS failed exception when using their example code