1 # git-gui misc. commit reading/writing support
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc load_last_commit {} {
5 global HEAD PARENT MERGE_HEAD commit_type ui_comm
8 if {[llength $PARENT] == 0} {
9 error_popup [mc "There is nothing to amend.
11 You are about to create the initial commit. There is no commit before this to amend.
16 repository_state curType curHEAD curMERGE_HEAD
17 if {$curType eq {merge}} {
18 error_popup [mc "Cannot amend while merging.
20 You are currently in the middle of a merge that has not been fully completed. You cannot amend the prior commit unless you first abort the current merge activity.
28 set fd [git_read cat-file commit $curHEAD]
29 fconfigure $fd -encoding binary -translation lf
30 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
33 while {[gets $fd line] > 0} {
34 if {[string match {parent *} $line]} {
35 lappend parents [string range $line 7 end]
36 } elseif {[string match {encoding *} $line]} {
37 set enc [string tolower [string range $line 9 end]]
43 set enc [tcl_encoding $enc]
45 set msg [encoding convertfrom $enc $msg]
47 set msg [string trim $msg]
49 error_popup [strcat [mc "Error loading commit data for amend:"] "\n\n$err"]
56 switch -- [llength $parents] {
57 0 {set commit_type amend-initial}
58 1 {set commit_type amend}
59 default {set commit_type amend-merge}
62 $ui_comm delete 0.0 end
63 $ui_comm insert end $msg
65 $ui_comm edit modified false
69 set GIT_COMMITTER_IDENT {}
71 proc committer_ident {} {
72 global GIT_COMMITTER_IDENT
74 if {$GIT_COMMITTER_IDENT eq {}} {
75 if {[catch {set me [git var GIT_COMMITTER_IDENT]} err]} {
76 error_popup [strcat [mc "Unable to obtain your identity:"] "\n\n$err"]
79 if {![regexp {^(.*) [0-9]+ [-+0-9]+$} \
80 $me me GIT_COMMITTER_IDENT]} {
81 error_popup [strcat [mc "Invalid GIT_COMMITTER_IDENT:"] "\n\n$me"]
86 return $GIT_COMMITTER_IDENT
92 set me [committer_ident]
95 set sob "Signed-off-by: $me"
96 set last [$ui_comm get {end -1c linestart} {end -1c}]
98 $ui_comm edit separator
100 && ![regexp {^[A-Z][A-Za-z]*-[A-Za-z-]+: *} $last]} {
101 $ui_comm insert end "\n"
103 $ui_comm insert end "\n$sob"
104 $ui_comm edit separator
109 proc create_new_commit {} {
110 global commit_type ui_comm
112 set commit_type normal
113 $ui_comm delete 0.0 end
115 $ui_comm edit modified false
119 proc commit_tree {} {
120 global HEAD commit_type file_states ui_comm repo_config
123 if {[committer_ident] eq {}} return
124 if {![lock_index update]} return
126 # -- Our in memory state should match the repository.
128 repository_state curType curHEAD curMERGE_HEAD
129 if {[string match amend* $commit_type]
130 && $curType eq {normal}
131 && $curHEAD eq $HEAD} {
132 } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
133 info_popup [mc "Last scanned state does not match repository state.
135 Another Git program has modified this repository since the last scan. A rescan must be performed before another commit can be created.
137 The rescan will be automatically started now.
144 # -- At least one file should differ in the index.
147 foreach path [array names file_states] {
148 switch -glob -- [lindex $file_states($path) 0] {
153 M? {set files_ready 1}
156 error_popup [mc "Unmerged files cannot be committed.
158 File %s has merge conflicts. You must resolve them and stage the file before committing.
159 " [short_path $path]]
164 error_popup [mc "Unknown file state %s detected.
166 File %s cannot be committed by this program.
167 " [lindex $s 0] [short_path $path]]
171 if {!$files_ready && ![string match *merge $curType] && ![is_enabled nocommit]} {
172 info_popup [mc "No changes to commit.
174 You must stage at least 1 file before you can commit.
180 if {[is_enabled nocommitmsg]} { do_quit 0 }
182 # -- A message is required.
184 set msg [string trim [$ui_comm get 1.0 end]]
185 regsub -all -line {[ \t\r]+$} $msg {} msg
187 error_popup [mc "Please supply a commit message.
189 A good commit message has the following format:
191 - First line: Describe in one sentence what you did.
193 - Remaining lines: Describe why this change is good.
199 # -- Build the message file.
201 set msg_p [gitdir GITGUI_EDITMSG]
202 set msg_wt [open $msg_p w]
203 fconfigure $msg_wt -translation lf
204 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
207 set use_enc [tcl_encoding $enc]
208 if {$use_enc ne {}} {
209 fconfigure $msg_wt -encoding $use_enc
211 puts stderr [mc "warning: Tcl does not support encoding '%s'." $enc]
212 fconfigure $msg_wt -encoding utf-8
217 if {[is_enabled nocommit]} { do_quit 0 }
219 # -- Run the pre-commit hook.
221 set fd_ph [githook_read pre-commit]
223 commit_commitmsg $curHEAD $msg_p
227 ui_status [mc "Calling pre-commit hook..."]
229 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
230 fileevent $fd_ph readable \
231 [list commit_prehook_wait $fd_ph $curHEAD $msg_p]
234 proc commit_prehook_wait {fd_ph curHEAD msg_p} {
237 append pch_error [read $fd_ph]
238 fconfigure $fd_ph -blocking 1
240 if {[catch {close $fd_ph}]} {
241 catch {file delete $msg_p}
242 ui_status [mc "Commit declined by pre-commit hook."]
243 hook_failed_popup pre-commit $pch_error
246 commit_commitmsg $curHEAD $msg_p
251 fconfigure $fd_ph -blocking 0
254 proc commit_commitmsg {curHEAD msg_p} {
257 # -- Run the commit-msg hook.
259 set fd_ph [githook_read commit-msg $msg_p]
261 commit_writetree $curHEAD $msg_p
265 ui_status [mc "Calling commit-msg hook..."]
267 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
268 fileevent $fd_ph readable \
269 [list commit_commitmsg_wait $fd_ph $curHEAD $msg_p]
272 proc commit_commitmsg_wait {fd_ph curHEAD msg_p} {
275 append pch_error [read $fd_ph]
276 fconfigure $fd_ph -blocking 1
278 if {[catch {close $fd_ph}]} {
279 catch {file delete $msg_p}
280 ui_status [mc "Commit declined by commit-msg hook."]
281 hook_failed_popup commit-msg $pch_error
284 commit_writetree $curHEAD $msg_p
289 fconfigure $fd_ph -blocking 0
292 proc commit_writetree {curHEAD msg_p} {
293 ui_status [mc "Committing changes..."]
294 set fd_wt [git_read write-tree]
295 fileevent $fd_wt readable \
296 [list commit_committree $fd_wt $curHEAD $msg_p]
299 proc commit_committree {fd_wt curHEAD msg_p} {
300 global HEAD PARENT MERGE_HEAD commit_type
301 global current_branch
302 global ui_comm selected_commit_type
303 global file_states selected_paths rescan_active
307 if {[catch {close $fd_wt} err]} {
308 catch {file delete $msg_p}
309 error_popup [strcat [mc "write-tree failed:"] "\n\n$err"]
310 ui_status [mc "Commit failed."]
315 # -- Verify this wasn't an empty change.
317 if {$commit_type eq {normal}} {
318 set fd_ot [git_read cat-file commit $PARENT]
319 fconfigure $fd_ot -encoding binary -translation lf
320 set old_tree [gets $fd_ot]
323 if {[string equal -length 5 {tree } $old_tree]
324 && [string length $old_tree] == 45} {
325 set old_tree [string range $old_tree 5 end]
327 error [mc "Commit %s appears to be corrupt" $PARENT]
330 if {$tree_id eq $old_tree} {
331 catch {file delete $msg_p}
332 info_popup [mc "No changes to commit.
334 No files were modified by this commit and it was not a merge commit.
336 A rescan will be automatically started now.
339 rescan {ui_status [mc "No changes to commit."]}
344 # -- Create the commit.
346 set cmd [list commit-tree $tree_id]
347 foreach p [concat $PARENT $MERGE_HEAD] {
351 if {[catch {set cmt_id [eval git $cmd]} err]} {
352 catch {file delete $msg_p}
353 error_popup [strcat [mc "commit-tree failed:"] "\n\n$err"]
354 ui_status [mc "Commit failed."]
359 # -- Update the HEAD ref.
362 if {$commit_type ne {normal}} {
363 append reflogm " ($commit_type)"
365 set msg_fd [open $msg_p r]
368 append reflogm {: } $subject
370 git update-ref -m $reflogm HEAD $cmt_id $curHEAD
372 catch {file delete $msg_p}
373 error_popup [strcat [mc "update-ref failed:"] "\n\n$err"]
374 ui_status [mc "Commit failed."]
379 # -- Cleanup after ourselves.
381 catch {file delete $msg_p}
382 catch {file delete [gitdir MERGE_HEAD]}
383 catch {file delete [gitdir MERGE_MSG]}
384 catch {file delete [gitdir SQUASH_MSG]}
385 catch {file delete [gitdir GITGUI_MSG]}
387 # -- Let rerere do its thing.
389 if {[get_config rerere.enabled] eq {}} {
390 set rerere [file isdirectory [gitdir rr-cache]]
392 set rerere [is_config_true rerere.enabled]
398 # -- Run the post-commit hook.
400 set fd_ph [githook_read post-commit]
402 upvar #0 pch_error$cmt_id pc_err
404 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
405 fileevent $fd_ph readable \
406 [list commit_postcommit_wait $fd_ph $cmt_id]
409 $ui_comm delete 0.0 end
411 $ui_comm edit modified false
412 if {$::GITGUI_BCK_exists} {
413 catch {file delete [gitdir GITGUI_BCK]}
414 set ::GITGUI_BCK_exists 0
417 if {[is_enabled singlecommit]} { do_quit 0 }
419 # -- Update in memory status
421 set selected_commit_type new
422 set commit_type normal
425 set MERGE_HEAD [list]
427 foreach path [array names file_states] {
428 set s $file_states($path)
439 unset file_states($path)
440 catch {unset selected_paths($path)}
443 set file_states($path) [list _O [lindex $s 1] {} {}]
449 set file_states($path) [list \
450 _[string index $m 1] \
461 ui_status [mc "Created commit %s: %s" [string range $cmt_id 0 7] $subject]
464 proc commit_postcommit_wait {fd_ph cmt_id} {
465 upvar #0 pch_error$cmt_id pch_error
467 append pch_error [read $fd_ph]
468 fconfigure $fd_ph -blocking 1
470 if {[catch {close $fd_ph}]} {
471 hook_failed_popup post-commit $pch_error 0
476 fconfigure $fd_ph -blocking 0