Last active
March 13, 2023 19:39
-
-
Save connordavenport/38702ec1181b5fb86bbe214122682bec to your computer and use it in GitHub Desktop.
a custom fontparts function to get the git commits of a specific glyph. adapted from Mathieu Reguer's robofont-git tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from git import Repo | |
import os | |
from fontTools.ufoLib.glifLib import readGlyphFromString, GlyphSet | |
import time | |
def getCommits(self,): | |
glif_path = get_glif_path_from_glyph(self) | |
commits = get_all_commit_for_file(glif_path) | |
history = {GlyphCommit(b):GlyphCommit(b).hash for b in commits} | |
return history | |
def get_glyph_from_string(string): | |
dummy = RGlyph().naked() | |
readGlyphFromString(string, dummy, dummy.getPointPen()) | |
return RGlyph(dummy) | |
def get_glif_path_from_glyph(glyph): | |
font_path = glyph.font.path | |
glyphs_path = os.path.join(font_path, "glyphs") | |
glyphSet = GlyphSet(glyphs_path) | |
glif_path = glyphSet.contents.get(glyph.name) | |
return os.path.join(glyphs_path, glif_path) | |
def get_all_commit_for_file(path, max_count=100): | |
repo = get_repo_for_file(path) | |
repo_path = repo.git.rev_parse("--show-toplevel") | |
tree_path = os.path.relpath(path, repo_path) | |
commits = repo.iter_commits( | |
'--all', max_count=max_count, paths=path) | |
blob_commits = [BlobCommit(c, repo.commit(c).tree[tree_path]) for c in commits] | |
return blob_commits | |
def get_repo_for_file(path): | |
git_repo = Repo(path, search_parent_directories=True) | |
return git_repo | |
class BlobCommit(object): | |
def __init__(self, commit, blob): | |
self.commit = commit | |
self.blob = blob | |
class GlyphCommit(object): | |
def __init__(self, blob_commit): | |
self.load_from_blob_commit(blob_commit) | |
def load_from_blob_commit(self, blob_commit): | |
blob = blob_commit.blob | |
commit = blob_commit.commit | |
self.glyph = get_glyph_from_string(blob.data_stream.read()) | |
self.date = time.strftime("%d %b %Y | %H:%m", | |
time.gmtime(commit.committed_date)) | |
self.hash = commit.hexsha | |
self.message = commit.message.replace("\n","") | |
self.author = commit.author.name | |
def __repr__(self): | |
return self.message | |
# ------------------------------------------------------------------------------------------ | |
# ------------------------------------------------------------------------------------------ | |
from mojo.roboFont import RGlyph | |
RGlyph.getCommits = getCommits | |
# ------------------------------------------------------------------------------------------ | |
# ------------------------------------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment