git-gui: Add support for calling out to the prepare-commit-msg hook
Signed-off-by: Joshua Williams <joshua.williams@qlogic.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Joshua Williams committed
Sep 24, 2008 at 14:11 UTC
2cd1fd1f6da5243226779acb17be9132d53ec4a9
1 file changed
+65
git-gui.sh
+65
@@ -1170,6 +1170,7 @@ proc rescan {after {honor_trustmtime 1}} {
1170
|| [string trim [$ui_comm get 0.0 end]] eq {})} {
1171
if {[string match amend* $commit_type]} {
1172
} elseif {[load_message GITGUI_MSG]} {
1173
+ } elseif {[run_prepare_commit_msg_hook]} {
1174
} elseif {[load_message MERGE_MSG]} {
1175
} elseif {[load_message SQUASH_MSG]} {
1176
}
@@ -1269,6 +1270,70 @@ proc load_message {file} {
1270
return 0
1271
}
1272
1273
+proc run_prepare_commit_msg_hook {} {
1274
+ global pch_error
1275
+
1276
+ # prepare-commit-msg requires PREPARE_COMMIT_MSG exist. From git-gui
1277
+ # it will be .git/MERGE_MSG (merge), .git/SQUASH_MSG (squash), or an
1278
+ # empty file but existant file.
1279
+
1280
+ set fd_pcm [open [gitdir PREPARE_COMMIT_MSG] a]
1281
+
1282
+ if {[file isfile [gitdir MERGE_MSG]]} {
1283
+ set pcm_source "merge"
1284
+ set fd_mm [open [gitdir MERGE_MSG] r]
1285
+ puts -nonewline $fd_pcm [read $fd_mm]
1286
+ close $fd_mm
1287
+ } elseif {[file isfile [gitdir SQUASH_MSG]]} {
1288
+ set pcm_source "squash"
1289
+ set fd_sm [open [gitdir SQUASH_MSG] r]
1290
+ puts -nonewline $fd_pcm [read $fd_sm]
1291
+ close $fd_sm
1292
+ } else {
1293
+ set pcm_source ""
1294
+ }
1295
+
1296
+ close $fd_pcm
1297
+
1298
+ set fd_ph [githook_read prepare-commit-msg \
1299
+ [gitdir PREPARE_COMMIT_MSG] $pcm_source]
1300
+ if {$fd_ph eq {}} {
1301
+ catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1302
+ return 0;
1303
+ }
1304
+
1305
+ ui_status [mc "Calling prepare-commit-msg hook..."]
1306
+ set pch_error {}
1307
+
1308
+ fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
1309
+ fileevent $fd_ph readable \
1310
+ [list prepare_commit_msg_hook_wait $fd_ph]
1311
+
1312
+ return 1;
1313
+}
1314
+
1315
+proc prepare_commit_msg_hook_wait {fd_ph} {
1316
+ global pch_error
1317
+
1318
+ append pch_error [read $fd_ph]
1319
+ fconfigure $fd_ph -blocking 1
1320
+ if {[eof $fd_ph]} {
1321
+ if {[catch {close $fd_ph}]} {
1322
+ ui_status [mc "Commit declined by prepare-commit-msg hook."]
1323
+ hook_failed_popup prepare-commit-msg $pch_error
1324
+ catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1325
+ exit 1
1326
+ } else {
1327
+ load_message PREPARE_COMMIT_MSG
1328
+ }
1329
+ set pch_error {}
1330
+ catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1331
+ return
1332
+ }
1333
+ fconfigure $fd_ph -blocking 0
1334
+ catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1335
+}
1336
+
1337
proc read_diff_index {fd after} {
1338
global buf_rdi
1339