-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgit-diff-gif.py
More file actions
executable file
·190 lines (159 loc) · 7.2 KB
/
Copy pathgit-diff-gif.py
File metadata and controls
executable file
·190 lines (159 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "click",
# ]
# ///
import shlex
import shutil
import sys
from os import environ, makedirs
from os.path import splitext, exists, dirname, isdir
from subprocess import check_call, DEVNULL, CalledProcessError, check_output
from tempfile import TemporaryDirectory
import click
# Env var for optionally storing command (incl. any extra arguments) for the final `open` call. Will be parsed with
# `shlex.split`. Default command is:
# - `open -a "/Applications/Google Chrome.app"` (if `open` and Chrome both exist)
# - `open` (if only `open` exists)
# - skip opening otherwise
GIT_DIFF_GIF_OPEN_CMD = 'GIT_DIFF_GIF_OPEN_CMD'
DEFAULT_EXTENSIONS = [ '.jpg', '.png', '.jpeg', ]
def stderr(msg):
sys.stderr.write(msg)
sys.stderr.write('\n')
def run(*cmd, log=stderr, **kwargs):
log(f'Running: {shlex.join(cmd)}')
check_call(cmd, **kwargs)
def check(*cmd, stdout=DEVNULL, stderr=DEVNULL, **kwargs):
try:
check_call(cmd, stdout=stdout, stderr=stderr, **kwargs)
return True
except CalledProcessError as e:
return False
def get_repo_root():
"""Get the root directory of the git repository."""
return check_output(['git', 'rev-parse', '--show-toplevel']).decode().strip()
def to_repo_path(path):
"""Convert a path (possibly relative to cwd) to a path relative to repo root."""
from os.path import abspath, relpath
repo_root = get_repo_root()
abs_path = abspath(path)
return relpath(abs_path, repo_root)
def get_changed_imgs(refspec, *paths):
return [
path
for path in check_output(['git', 'diff', '--name-only', refspec, '--', *paths]).decode().split('\n')
if splitext(path)[-1] in DEFAULT_EXTENSIONS and exists(path)
]
def add_watermark(input_path, output_path, label, ref_info):
"""Add a watermark label to an image using ImageMagick."""
# Use ImageMagick to add text overlay
# Position: top-left corner with padding
# White text with black shadow for visibility on any background
run(
'magick', input_path,
'-gravity', 'NorthWest',
'-pointsize', '24',
'-fill', 'black',
'-annotate', '+11+11', f'{label}\n{ref_info}',
'-fill', 'white',
'-annotate', '+10+10', f'{label}\n{ref_info}',
output_path
)
@click.command()
@click.option('-a', '--after', 'after_ref', help='Git ref for the "after" image; default: current worktree file')
@click.option('-b', '--before', 'before_ref', help='Git ref for the "before" image; default: `HEAD`')
@click.option('-d', '--delay', default='100', help='Gif delay between frames, in 1/100ths of a second')
@click.option('-f', '--force', is_flag=True, help='Overwrite an existing .gif at the --output path')
@click.option('-o', '--output', 'out_dir', help="Write resulting .gif(s) to this directory (default: next to source image)")
@click.option('-O', '--no-open', is_flag=True, help='Skip `open`ing the generated .gif')
@click.option('-t', '--tmpdir', 'use_tmpdir', is_flag=True, help='Write .gif(s) to a tmpdir (cleaned up on exit)')
@click.argument('paths', nargs=-1) # Paths to Git-tracked image to make diff-gifs of
def main(before_ref, after_ref, delay, force, out_dir, no_open, use_tmpdir, paths):
if after_ref:
if not before_ref:
before_ref = f'{after_ref}^'
refspec = f'{before_ref}..{after_ref}'
else:
if not before_ref:
before_ref = 'HEAD'
refspec = before_ref
if paths:
paths = [
expanded_path
for path in paths
for expanded_path in (get_changed_imgs(refspec, path) if isdir(path) else [path])
]
else:
paths = get_changed_imgs(refspec)
tmp_out_paths = use_tmpdir
out_paths = []
with TemporaryDirectory() as tmpdir:
for path in paths:
# Convert to repo-relative path for git show
repo_path = to_repo_path(path)
name, ext = splitext(path)
before_path_raw = f'{tmpdir}/before_raw{ext}'
with open(before_path_raw, 'wb') as f:
run('git', 'show', f'{before_ref}:{repo_path}', stdout=f)
# Get short SHA for before ref
before_sha = check_output(['git', 'rev-parse', '--short', before_ref]).decode().strip()
if after_ref:
after_path_raw = f'{tmpdir}/after_raw{ext}'
with open(after_path_raw, 'wb') as f:
run('git', 'show', f'{after_ref}:{repo_path}', stdout=f)
after_sha = check_output(['git', 'rev-parse', '--short', after_ref]).decode().strip()
after_info = f'{after_ref} ({after_sha})'
else:
# Copy worktree file to temp location to avoid ImageMagick issues with special chars (e.g., colons)
after_path_raw = f'{tmpdir}/after_raw{ext}'
shutil.copy2(path, after_path_raw)
after_info = 'worktree'
# Add watermarks
before_path = f'{tmpdir}/before{ext}'
after_path = f'{tmpdir}/after{ext}'
add_watermark(before_path_raw, before_path, 'Before', f'{before_ref} ({before_sha})')
add_watermark(after_path_raw, after_path, 'After', after_info)
basename = splitext(path.split('/')[-1])[0]
if use_tmpdir:
cur_out_path = f'{tmpdir}/{basename}.gif'
elif out_dir:
cur_out_path = f'{out_dir}/{basename}.gif'
makedirs(out_dir, exist_ok=True)
else:
# Default: put .gif next to the source image (using original path)
cur_out_path = f'{name}.gif'
if dirname(cur_out_path):
makedirs(dirname(cur_out_path), exist_ok=True)
if exists(cur_out_path):
if force:
stderr(f'Overwriting {cur_out_path}')
else:
raise RuntimeError(f'--output {cur_out_path} exists; pass -f/--force to overwrite')
run('magick', '-delay', delay, '-dispose', 'previous', before_path, after_path, cur_out_path)
out_paths.append(cur_out_path)
do_open = not no_open
if do_open:
if check('which', 'open'):
for out_path in out_paths:
open_cmd = environ.get(GIT_DIFF_GIF_OPEN_CMD, '')
if open_cmd:
open_cmd = shlex.split(open_cmd)
else:
open_cmd = ['open']
# On macOS, Chrome seems like the best way to `open` .gifs. Preview opens all the frames as separate
# images.
chrome_path = '/Applications/Google Chrome.app'
if exists(chrome_path):
open_cmd += [ '-a', chrome_path]
open_cmd += [ out_path ]
run(*open_cmd)
if tmp_out_paths:
# Give user a chance to inspect `open`ed .gifs before the tempdir is cleaned up
input('[enter] to exit')
else:
stderr('No `open` executable found, skipping `open`')
if __name__ == '__main__':
main()