-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-latexdiff.ps1
244 lines (192 loc) · 6.59 KB
/
git-latexdiff.ps1
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<#
.SYNOPSIS
A script to generate a LaTeX diff between two Git commits or branches. Version 1.2
.DESCRIPTION
This script uses Git and LaTeX tools to create a diff of a LaTeX document between two specified commits or branches.
.PARAMETER Old
The old commit or branch to compare from. Default is 'HEAD'.
.PARAMETER New
The new commit or branch to compare to. Default is '--' (working directory).
.PARAMETER Main
The main LaTeX file to use. If not specified, the script will try to detect it.
.PARAMETER NoView
Do not open the resulting PDF file.
.PARAMETER NoBibtex
Do not run BibTeX. By default, BibTeX is run.
.PARAMETER Output
The output PDF file name. Default is 'diff.pdf'.
.PARAMETER TmpdirPrefix
The prefix for the temporary directory. Default is the system's TEMP directory.
.PARAMETER Silent
Suppress verbose output.
.PARAMETER NoCleanup
Do not clean up temporary files.
.PARAMETER Help
Show help information.
.PARAMETER PdfViewer
The PDF viewer to use. Default is the system's default viewer.
.EXAMPLE
.\git-latexdiff.ps1 -Old "commit1" -New "commit2" -Main "main.tex" -Output "diff_output.pdf"
.NOTES
Author: Rakhul Raj
Date: 24-08-2024
#>
[CmdletBinding()]
param (
[Parameter(Position=0, Mandatory=$false, HelpMessage="The old commit or branch to compare from. Default is 'HEAD'.")]
[string]$Old = "HEAD",
[Parameter(Position=1, HelpMessage="The new commit or branch to compare to. Default is '--' (working directory).")]
[string]$New = "--",
[Parameter(HelpMessage="The main LaTeX file to use. If not specified, the script will try to detect it.")]
[string]$Main,
[Parameter(HelpMessage="Do not open the resulting PDF file.")]
[switch]$NoView,
[Parameter(HelpMessage="Do not run BibTeX. By default, BibTeX is run.")]
[switch]$NoBibtex,
[Parameter(HelpMessage="The output PDF file name. Default is 'diff.pdf'.")]
[string]$Output = 'diff.pdf',
[Parameter(HelpMessage="The prefix for the temporary directory. Default is the system's TEMP directory.")]
[string]$TmpdirPrefix = "$env:TEMP",
[Alias("s")]
[Parameter(HelpMessage="Suppress verbose output.")]
[switch]$Silent,
[Parameter(HelpMessage="Do not clean up temporary files.")]
[switch]$NoCleanup,
[Alias("h")]
[Parameter(HelpMessage="Show help information.")]
[switch]$Help,
[Parameter(HelpMessage="The PDF viewer (full path of the exe file) to use. Default is the system's default viewer.")]
[string]$PdfViewer
)
$ErrorActionPreference = "Stop"
function Write-Verbose-Custom {
param([string]$Message)
if (-not $Silent) {
Write-Host $Message
}
}
function Cleanup{
if ($NoCleanup) {
Write-Verbose-Custom "Keeping all generated files in $TmpDir"
} else {
Write-Verbose-Custom "Cleaning up all files"
Remove-Item -Recurse -Force $TmpDir
}
}
# Display help if the Help switch is set
if ($Help) {
Get-Help -Full $MyInvocation.MyCommand.Path
exit
}
try{
# Store the current directory
$originalDir = Get-Location
# Detect PDF Viewer if not specified
if (-not $PdfViewer) {
$PdfViewer = "start" # Default to Windows' default program
# if (Test-Path "C:\Program Files (x86)\Foxit Software\Foxit PDF Editor\FoxitPDFEditor.exe") {
# $PdfViewer = "C:\Program Files (x86)\Foxit Software\FoxitPDFEditor.exe"
# }
}
# Create temporary directories
if (-not (Test-Path -Path $TmpdirPrefix)) {
Write-Verbose-Custom "crat"
New-Item -ItemType Directory -Path $TmpdirPrefix | Out-Null
Write-Verbose-Custom "Directory created: $TmpdirPrefix"
} else {
Write-Verbose-Custom "Directory already exists: $TmpdirPrefix"
}
$TmpdirPrefix = Convert-Path $TmpdirPrefix
$TmpDir = Join-Path $TmpdirPrefix "git-latexdiff-$([Guid]::NewGuid().ToString())"
New-Item -ItemType Directory -Path $TmpDir | Out-Null
$OldDir = Join-Path $TmpDir "old"
$NewDir = Join-Path $TmpDir "new"
New-Item -ItemType Directory -Path $OldDir, $NewDir | Out-Null
Write-Verbose-Custom "Temporary directories created: $OldDir and $NewDir"
# Determine main file if not specified
if (-not $Main) {
Set-Location $originalDir
$Main = git grep -l '^\s*\\documentclass' | Select-Object -First 1
if (-not $Main) {
throw "No main file specified and couldn't detect one. Please use -Main parameter."
}
Write-Verbose-Custom "Detected main file: $Main"
}
$MainBase = [System.IO.Path]::GetFileNameWithoutExtension($Main)
$MainDir = [System.IO.Path]::GetDirectoryName($Main)
# Change back to the original directory before running git commands
Set-Location $originalDir
try {
# Checkout old and new versions
# Create tar files in the original directory
git archive --format=tar $Old -o "$TmpDir\old.tar"
if ($New -eq "--") {
git ls-files | tar -cf "$TmpDir\new.tar" -T -
} else {
git archive --format=tar $New -o "$TmpDir\new.tar"
}
# Extract tar files
Set-Location $OldDir
tar -xf "$TmpDir\old.tar"
Set-Location $NewDir
tar -xf "$TmpDir\new.tar"
} catch {
Write-Error "Error checking out old and new versions: $_"
exit 1
}
Set-Location $TmpDir
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
try {
# Flatten documents
Write-Verbose-Custom "Flattening documents with latexpand"
$oldContent = latexpand "$OldDir\$Main"
[System.IO.File]::WriteAllLines("$TmpDir\old-$MainBase-fl.tex", $oldContent, $Utf8NoBomEncoding)
$newContent = latexpand "$NewDir\$Main"
[System.IO.File]::WriteAllLines("$TmpDir\new-$MainBase-fl.tex", $newContent, $Utf8NoBomEncoding)
} catch {
Write-Error "Error flattening documents: $_"
exit 1
}
try {
# Run latexdiff
Write-Verbose-Custom "Running latexdiff"
$Content = latexdiff "old-$MainBase-fl.tex" "new-$MainBase-fl.tex"
[System.IO.File]::WriteAllLines("$TmpDir\diff.tex", $Content, $Utf8NoBomEncoding)
} catch {
Write-Error "Error running latexdiff: $_"
exit 1
}
Move-Item -Force "$TmpDir\diff.tex" "$NewDir\diff.tex"
# Compile result
Write-Verbose-Custom "Compiling result"
Set-Location "$NewDir\$MainDir"
pdflatex "diff"
if ($NoBibtex) {
pdflatex "diff"
} else {
bibtex "diff"
pdflatex "diff"
pdflatex "diff"
}
$PdfFile = "$NewDir\$MainDir\diff.pdf"
if (-not (Test-Path $PdfFile)) {
throw "No PDF file generated."
}
if ((Get-Item $PdfFile).Length -eq 0) {
throw "PDF file generated is empty."
}
Set-Location $originalDir
if ($Output) {
Move-Item -Force $PdfFile $Output
$PdfFile = $Output
Write-Host "Output written to $PdfFile"
}
# View PDF
if (-not $NoView) {
& $PdfViewer $PdfFile
}
} finally{
Set-Location $originalDir
# Cleanup
Cleanup
}