Skip to content

Instantly share code, notes, and snippets.

@benoitjacquier
Last active January 17, 2024 08:41
Show Gist options
  • Save benoitjacquier/51fdcc12b19b4091c7eac3c087b5e00e to your computer and use it in GitHub Desktop.
Save benoitjacquier/51fdcc12b19b4091c7eac3c087b5e00e to your computer and use it in GitHub Desktop.
10X TortoiseGIT plugin
#------------------------------------------------------------------------
import N10X
import subprocess
import os
TORTOISE_GIT_EXE = "TortoiseGitProc.exe"
class TortoiseGIT_Options():
def __init__(self):
self.bin_path = N10X.Editor.GetSetting("TortoiseGIT.TortoisePath")
def TortoiseExec(self, cmd, path):
try:
subprocess.Popen([self.bin_path,
'/command:' + cmd,
'/path:' + path],
shell=False, stdin=None, stdout=None, stderr=None,
close_fds=True)
except FileNotFoundError:
print('[TortoiseGIT]: TortoiseProc executable "' + self.bin_path + '" could not be found')
def TortoiseGetGitRootFromCurrentFile(self):
git_root = ""
dirname = os.path.dirname(N10X.Editor.GetCurrentFilename())
while True:
dirname_git =os.path.join(dirname,".git")
if os.path.isdir(dirname_git):
git_root = dirname_git
break
new_dirname = os.path.dirname(dirname)
if new_dirname=="" or new_dirname==dirname:
break
dirname = new_dirname
return dirname if len(git_root)>0 else ""
def TortoiseGITLog():
global _tortoise_git_options
if (len(N10X.Editor.GetCurrentFilename()) == 0):
print("No current file")
return
_tortoise_git_options.TortoiseExec("log", N10X.Editor.GetCurrentFilename())
def TortoiseGITRepoLog():
global _tortoise_git_options
git_root = _tortoise_git_options.TortoiseGetGitRootFromCurrentFile()
if not git_root:
print("No .git found above " + N10X.Editor.GetCurrentFilename())
return
_tortoise_git_options.TortoiseExec("log", git_root)
def TortoiseGITDiff():
global _tortoise_git_options
if (len(N10X.Editor.GetCurrentFilename()) == 0):
print("No current file")
return
_tortoise_git_options.TortoiseExec("diff", N10X.Editor.GetCurrentFilename())
def TortoiseGITBlame():
global _tortoise_git_options
if (len(N10X.Editor.GetCurrentFilename()) == 0):
print("No current file")
return
try:
subprocess.Popen([_tortoise_git_options.bin_path,
'/command:blame',
'/path:' + N10X.Editor.GetCurrentFilename(),
'/line:' + str(N10X.Editor.GetCursorPos()[1] + 1),
'/startrev:1',
'/endrev:HEAD'],
shell=False, stdin=None, stdout=None, stderr=None,
close_fds=True)
except FileNotFoundError:
print('[TortoiseGIT]: TortoiseProc executable "' + _tortoise_git_options.bin_path + '" could not be found')
def TortoiseGITStatus():
global _tortoise_git_options
if (len(N10X.Editor.GetCurrentFilename()) == 0):
print("No current file")
return
_tortoise_git_options.TortoiseExec("repostatus", N10X.Editor.GetCurrentFilename())
def TortoiseGITRepoStatus():
global _tortoise_git_options
git_root = _tortoise_git_options.TortoiseGetGitRootFromCurrentFile()
if not git_root:
print("No .git found above " + N10X.Editor.GetCurrentFilename())
return
_tortoise_git_options.TortoiseExec("repostatus", git_root)
def InitialiseTortoiseGIT():
global _tortoise_git_options
_tortoise_git_options = TortoiseGIT_Options()
if not N10X.Editor.GetSetting("TortoiseGIT.TortoisePath"):
N10X.Editor.SetSetting("TortoiseGIT.TortoisePath", TORTOISE_GIT_EXE)
N10X.Editor.CallOnMainThread(InitialiseTortoiseGIT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment