Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Last active October 11, 2023 20:58
Show Gist options
  • Select an option

  • Save connordavenport/2a428c05a5d1c77f993ffca2165ea6a1 to your computer and use it in GitHub Desktop.

Select an option

Save connordavenport/2a428c05a5d1c77f993ffca2165ea6a1 to your computer and use it in GitHub Desktop.
Automatic Git Commiter
from AppKit import NSBeep,NSTimer,NSRunLoop,NSRunLoopCommonModes
import git
import os
from mojo.extensions import setExtensionDefault, getExtensionDefault
import re
from mojo.UI import PostBannerNotification
'''
A startup script for RoboFont to automatically commit to Git every 5 minutes.
To use, run the following script with your GitHub email:
from mojo.extensions import setExtensionDefault
myEmail = johnDoe@gmail.com
setExtensionDefault(KEY, myEmail)
'''
KEY = "com.connordavenport.autoCommiter.eMail"
EMAIL = getExtensionDefault(KEY)
class commitTimer():
def __init__(self):
self.commitTimer = None
self.startCommitTimer()
def resetCommitTimer(self):
if self.commitTimer is not None:
self.startCommitTimer()
def stopCommitTimer(self):
if self.commitTimer is not None:
self.commitTimer.invalidate()
self.commitTimer = None
def startCommitTimer(self):
delay = 300
if not delay:
return
self.stopCommitTimer()
PostBannerNotification("GitHub", "Committing Files...")
self.commitTimer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(
delay,
self,
"commitTimerFire:",
None,
False
)
runloop = NSRunLoop.mainRunLoop()
runloop.addTimer_forMode_(self.commitTimer, NSRunLoopCommonModes)
def commitTimerFire_(self, timer):
self.commitTimer = None
self.startCommitTimer()
self.getOpenFiles()
def isGitRepo(self,file):
path = file
filename = ".git"
os.chdir(path)
dirc = os.getcwd()
while not os.path.isdir(os.path.join(os.getcwd(),filename)):
os.chdir('..')
dirc = os.getcwd()
if len(os.getcwd()) == 1:
break
os.chdir(path)
if dirc == "/Users":
return False, None
else:
return True, dirc
def getGlifName(self,glif):
with open(glif, 'r', encoding="utf-8") as glifFile:
gf = glifFile.read()
temp = RGlyph()
temp.readGlyphFromString(gf)
return temp.name
def formattedDiff(self,files):
output = ""
bases = [fs.a_path for fs in files]
glif = list(set([self.getGlifName(g) for g in bases if g[-5:] == ".glif"]))
others = list(set([os.path.basename(g) for g in bases if g[-5:] != ".glif"]))
if others:
output += f"{len(others)} plists changed"
if others and glif:
output += " and "
if glif:
output += f"the following glyphs changed: glyphs changed {', '.join(glif)}"
return output
def loadRepo(self,path):
status, self.dirPath = self.isGitRepo(path)
if status:
if self.dirPath:
self.repo = git.Repo(self.dirPath,search_parent_directories=True)
self.changedFiles = [ item for item in self.repo.index.diff(None)]
self.message = self.formattedDiff(self.changedFiles)
self.commitCallback()
def getOpenFiles(self):
fontDirs = list(set([os.path.dirname(f.path) for f in AllFonts()]))
if not fontDirs or len(fontDirs) != 1:
pass
else:
s,p = self.isGitRepo(fontDirs[0])
if s:
self.loadRepo(p)
def simplifyPath(self,path):
if "/Users/" in path:
split = path.split("/")[3:]
return f'~/{"/".join(split)}'
def commitCallback(self):
commitMessage = self.message
if commitMessage and (EMAIL in list(set([cs.author.email for cs in self.repo.iter_commits()]))):
self.repo.git.add(all=True)
self.repo.git.commit('-m', commitMessage, author=EMAIL)
commitTimer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment