+######################################################################
+##
+## commit
+
+proc commit_tree {} {
+ global tcl_platform HEAD gitdir commit_type file_states
+ global commit_active ui_status_value
+ global ui_comm
+
+ if {$commit_active || ![lock_index update]} return
+
+ # -- Our in memory state should match the repository.
+ #
+ repository_state curHEAD cur_type
+ if {$commit_type != $cur_type || $HEAD != $curHEAD} {
+ error_popup {Last scanned state does not match repository state.
+
+Its highly likely that another Git program modified the
+repository since our last scan. A rescan is required
+before committing.
+}
+ unlock_index
+ update_status
+ return
+ }
+
+ # -- At least one file should differ in the index.
+ #
+ set files_ready 0
+ foreach path [array names file_states] {
+ set s $file_states($path)
+ switch -glob -- [lindex $s 0] {
+ _* {continue}
+ A* -
+ D* -
+ M* {set files_ready 1; break}
+ U* {
+ error_popup "Unmerged files cannot be committed.
+
+File $path has merge conflicts.
+You must resolve them and check the file in before committing.
+"
+ unlock_index
+ return
+ }
+ default {
+ error_popup "Unknown file state [lindex $s 0] detected.
+
+File $path cannot be committed by this program.
+"
+ }
+ }
+ }
+ if {!$files_ready} {
+ error_popup {No checked-in files to commit.
+
+You must check-in at least 1 file before you can commit.
+}
+ unlock_index
+ return
+ }
+
+ # -- A message is required.
+ #
+ set msg [string trim [$ui_comm get 1.0 end]]
+ if {$msg == {}} {
+ error_popup {Please supply a commit message.
+
+A good commit message has the following format:
+
+- First line: Describe in one sentance what you did.
+- Second line: Blank
+- Remaining lines: Describe why this change is good.
+}
+ unlock_index
+ return
+ }
+
+ # -- Ask the pre-commit hook for the go-ahead.
+ #
+ set pchook [file join $gitdir hooks pre-commit]
+ if {$tcl_platform(platform) == {windows} && [file exists $pchook]} {
+ set pchook [list sh -c \
+ "if test -x \"$pchook\"; then exec \"$pchook\"; fi"]
+ } elseif {[file executable $pchook]} {
+ set pchook [list $pchook]
+ } else {
+ set pchook {}
+ }
+ if {$pchook != {} && [catch {eval exec $pchook} err]} {
+ hook_failed_popup pre-commit $err
+ unlock_index
+ return
+ }
+
+ # -- Write the tree in the background.
+ #
+ set commit_active 1
+ set ui_status_value {Committing changes...}
+
+ set fd_wt [open "| git write-tree" r]
+ fileevent $fd_wt readable \
+ [list commit_stage2 $fd_wt $curHEAD $msg]
+}
+
+proc commit_stage2 {fd_wt curHEAD msg} {
+ global single_commit gitdir HEAD commit_type
+ global commit_active ui_status_value comm_ui
+
+ gets $fd_wt tree_id
+ close $fd_wt
+
+ if {$tree_id == {}} {
+ error_popup "write-tree failed"
+ set commit_active 0
+ set ui_status_value {Commit failed.}
+ unlock_index
+ return
+ }
+
+ # -- Create the commit.
+ #
+ set cmd [list git commit-tree $tree_id]
+ if {$commit_type != {initial}} {
+ lappend cmd -p $HEAD
+ }
+ if {$commit_type == {merge}} {
+ if {[catch {
+ set fd_mh [open [file join $gitdir MERGE_HEAD] r]
+ while {[gets $fd_mh merge_head] > 0} {
+ lappend -p $merge_head
+ }
+ close $fd_mh
+ } err]} {
+ error_popup "Loading MERGE_HEADs failed:\n$err"
+ set commit_active 0
+ set ui_status_value {Commit failed.}
+ unlock_index
+ return
+ }
+ }
+ if {$commit_type == {initial}} {
+ # git commit-tree writes to stderr during initial commit.
+ lappend cmd 2>/dev/null
+ }
+ lappend cmd << $msg
+ if {[catch {set cmt_id [eval exec $cmd]} err]} {
+ error_popup "commit-tree failed:\n$err"
+ set commit_active 0
+ set ui_status_value {Commit failed.}
+ unlock_index
+ return
+ }
+
+ # -- Update the HEAD ref.
+ #
+ set reflogm commit
+ if {$commit_type != {normal}} {
+ append reflogm " ($commit_type)"
+ }
+ set i [string first "\n" $msg]
+ if {$i >= 0} {
+ append reflogm {: } [string range $msg 0 [expr $i - 1]]
+ } else {
+ append reflogm {: } $msg
+ }
+ set cmd [list git update-ref \
+ -m $reflogm \
+ HEAD $cmt_id $curHEAD]
+ if {[catch {eval exec $cmd} err]} {
+ error_popup "update-ref failed:\n$err"
+ set commit_active 0
+ set ui_status_value {Commit failed.}
+ unlock_index
+ return
+ }
+
+ # -- Cleanup after ourselves.
+ #
+ catch {file delete [file join $gitdir MERGE_HEAD]}
+ catch {file delete [file join $gitdir MERGE_MSG]}
+ catch {file delete [file join $gitdir SQUASH_MSG]}
+ catch {file delete [file join $gitdir GITGUI_MSG]}
+
+ # -- Let rerere do its thing.
+ #
+ if {[file isdirectory [file join $gitdir rr-cache]]} {
+ catch {exec git rerere}
+ }
+
+ $comm_ui delete 0.0 end
+ $comm_ui edit modified false
+
+ if {$single_commit} do_quit
+
+ set commit_active 0
+ set ui_status_value "Changes committed as $cmt_id."
+ unlock_index
+ update_status
+}
+