Skip to content

Instantly share code, notes, and snippets.

@jotson
Created March 12, 2020 14:10
Show Gist options
  • Save jotson/827f14210e38e1dabc34595fc12a1414 to your computer and use it in GitHub Desktop.
Save jotson/827f14210e38e1dabc34595fc12a1414 to your computer and use it in GitHub Desktop.
import subprocess
import configparser
import os
VERSION_FILE = '/home/john/code/games/gravity/game/version.cfg'
GITDIR = '/home/john/code/games/gravity/game'
'''
Fix for fatal: not a git repository: '.git'
Removed all os.chdir() statements
See: https://github.com/DonJayamanne/gitHistoryVSCode/issues/233#issuecomment-375769718
Just before calling any hooks, git sets several env vars, among them is one called GIT_DIR
which points to the git index dir (if you're at the top level, it will be set to .git prior to calling your hooks)
The GIT_DIR setting becomes invalid when the hook does a chdir and calls git reentrantly to figure out the git top level dir.
'''
def get_version():
config = configparser.ConfigParser()
config.read(VERSION_FILE)
return int(config['version']['version'])
def get_status():
config = configparser.ConfigParser()
config.read(VERSION_FILE)
status = config['version']['status']
status = status.replace('"', '') # Godot does strings differently
return status
def get_commit():
config = configparser.ConfigParser()
config.read(VERSION_FILE)
commit = config['version']['commit']
commit = commit.replace('"', '') # Godot does strings differently
return commit
def get_public_version():
version = get_version()
status = get_status()
return '%s-%s' % (version, status)
def increment_version():
config = configparser.ConfigParser()
config.read(VERSION_FILE)
version = int(config['version']['version']) + 1
config['version']['version'] = str(version)
with open(VERSION_FILE, 'w') as configfile:
config.write(configfile)
def get_latest_commit():
output = subprocess.check_output(['git', 'log', '-1', '--oneline', 'HEAD'])
commit = output.split()[0].decode('utf-8')
return commit
def get_latest_commit_message():
output = subprocess.check_output(['git', 'log', '-1', '--format=%s'])
title = output.decode('utf-8').strip()
output = subprocess.check_output(['git', 'log', '-1', '--format=%b', '--shortstat'])
message = output.decode('utf-8').strip()
return (title, message)
def get_branch():
output = subprocess.check_output(['git', 'status', '--short', '--branch'])
branch = output.decode('utf-8').split('...')[0].replace('## ', '')
return branch
def set_commit(commit):
config = configparser.ConfigParser()
config.read(VERSION_FILE)
# Godot does strings differently
config['version']['commit'] = '"%s"' % commit
with open(VERSION_FILE, 'w') as configfile:
config.write(configfile)
def tag_git():
subprocess.check_output(['git', 'tag', 'v%s' % get_public_version(), get_commit() ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment