Skip to content

Instantly share code, notes, and snippets.

@LeonT-A
Last active August 1, 2020 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeonT-A/ce2db30847aeb6d69a0202d0b9b45528 to your computer and use it in GitHub Desktop.
Save LeonT-A/ce2db30847aeb6d69a0202d0b9b45528 to your computer and use it in GitHub Desktop.
Git-Bash Wrapper
# SCRIPT [FILE|FOLDER]
"""
Python3.6+ (Or compile to stand-alone exe using 'auto-py-to-exe')
save as 'filename.pyw' and place in the same folder as git-bash.exe
The HOME folder you specify below will be used by Git-Bash and opened by default.
You can also pass a directory to have it opened (Good for things like Rainmeter launchers) or pass a filename and its directory will be opened (Good for the 'Send to' part of the context menu)
"""
from pathlib import Path
import logging as log
import os
import subprocess
import sys
log.basicConfig(level=log.DEBUG, style='{', format='Line: {lineno} | level: {levelname} | Time: {asctime} | Info: {message}')
# log.disable()
log.debug(f"CWD: {Path.cwd()}")
log.debug(f"Arguments: {sys.argv}")
program_path = Path(sys.argv[0]).resolve().parents[0] / './git-bash.exe'
command = [str(program_path)]
log.debug(f"Program: {program_path}")
# - --- Environment Variables ---
os.environ['HOME'] = '/home' # The home directory where you will put your .bashrc file and other configurations. Git-Bash will also open here by default (Make a folder in the relative directory of git-bash.exe, start with a '/')
os.environ['MSYS'] = 'enable_pcon' # For running nvim and other terminals without appending 'winpty' to the line.
os.environ['XDG_CONFIG_HOME'] = f"{os.environ['HOME']}/.config/" # nvim setting (:h XDG_CONFIG_HOME)
os.environ['XDG_DATA_HOME'] = f"{os.environ['HOME']}/.config/nvim/" #nvim setting (:h XDG_DATA_HOME)
os.environ['PORTABLEDRIVE'] = str(program_path.drive)[0].lower() # The drive letter this script resides on, good for using as a variable for scripts and programs inside of Git-Bash
for variable, value in os.environ.items():
log.debug(f"OS - {variable} = {value}")
# - --- Parse Arguments ---
try:
input_file = Path(sys.argv[1]).resolve()
except IndexError:
# If there are no arguments, launch the program
command.append(f"--cd={program_path.parents[0] / os.environ['HOME'][1:]}")
log.debug(f"No arguments: {command}")
subprocess.Popen(command)
sys.exit()
log.debug(f"InputFile: {input_file}")
# - --- Input Type ---
if input_file.is_file():
input_type = 'file'
command.append(f"--cd={input_file.parents[0]}")
elif input_file.is_dir():
input_type = 'folder'
command.append(f"--cd={input_file}")
else:
log.error(f"Type Unknown: {input_file}")
sys.exit()
log.debug(f"Type: {input_type}")
# - --- Launch Program ---
log.debug(f"cmd: {command}")
subprocess.Popen(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment