Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 1 | # git-gui misc. commit reading/writing support |
| 2 | # Copyright (C) 2006, 2007 Shawn Pearce |
| 3 | |
| 4 | proc load_last_commit {} { |
| 5 | global HEAD PARENT MERGE_HEAD commit_type ui_comm |
| 6 | global repo_config |
| 7 | |
| 8 | if {[llength $PARENT] == 0} { |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 9 | error_popup [mc "There is nothing to amend. |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 10 | |
| 11 | You are about to create the initial commit. There is no commit before this to amend. |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 12 | "] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 13 | return |
| 14 | } |
| 15 | |
| 16 | repository_state curType curHEAD curMERGE_HEAD |
| 17 | if {$curType eq {merge}} { |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 18 | error_popup [mc "Cannot amend while merging. |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 19 | |
| 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. |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 21 | "] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 22 | return |
| 23 | } |
| 24 | |
| 25 | set msg {} |
| 26 | set parents [list] |
| 27 | if {[catch { |
Shawn O. Pearce | 0b81261 | 2007-07-09 01:17:09 -0400 | [diff] [blame] | 28 | set fd [git_read cat-file commit $curHEAD] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 29 | fconfigure $fd -encoding binary -translation lf |
| 30 | if {[catch {set enc $repo_config(i18n.commitencoding)}]} { |
| 31 | set enc utf-8 |
| 32 | } |
| 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]] |
| 38 | } |
| 39 | } |
Shawn O. Pearce | c4638f6 | 2007-07-19 01:13:29 -0400 | [diff] [blame] | 40 | set msg [read $fd] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 41 | close $fd |
Shawn O. Pearce | c4638f6 | 2007-07-19 01:13:29 -0400 | [diff] [blame] | 42 | |
| 43 | set enc [tcl_encoding $enc] |
| 44 | if {$enc ne {}} { |
| 45 | set msg [encoding convertfrom $enc $msg] |
| 46 | } |
| 47 | set msg [string trim $msg] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 48 | } err]} { |
Shawn O. Pearce | 31bb1d1 | 2007-09-14 01:50:09 -0400 | [diff] [blame] | 49 | error_popup [strcat [mc "Error loading commit data for amend:"] "\n\n$err"] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 50 | return |
| 51 | } |
| 52 | |
| 53 | set HEAD $curHEAD |
| 54 | set PARENT $parents |
| 55 | set MERGE_HEAD [list] |
| 56 | switch -- [llength $parents] { |
| 57 | 0 {set commit_type amend-initial} |
| 58 | 1 {set commit_type amend} |
| 59 | default {set commit_type amend-merge} |
| 60 | } |
| 61 | |
| 62 | $ui_comm delete 0.0 end |
| 63 | $ui_comm insert end $msg |
| 64 | $ui_comm edit reset |
| 65 | $ui_comm edit modified false |
Shawn O. Pearce | 699d560 | 2007-07-05 23:16:13 -0400 | [diff] [blame] | 66 | rescan ui_ready |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | set GIT_COMMITTER_IDENT {} |
| 70 | |
| 71 | proc committer_ident {} { |
| 72 | global GIT_COMMITTER_IDENT |
| 73 | |
| 74 | if {$GIT_COMMITTER_IDENT eq {}} { |
| 75 | if {[catch {set me [git var GIT_COMMITTER_IDENT]} err]} { |
Shawn O. Pearce | 31bb1d1 | 2007-09-14 01:50:09 -0400 | [diff] [blame] | 76 | error_popup [strcat [mc "Unable to obtain your identity:"] "\n\n$err"] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 77 | return {} |
| 78 | } |
| 79 | if {![regexp {^(.*) [0-9]+ [-+0-9]+$} \ |
| 80 | $me me GIT_COMMITTER_IDENT]} { |
Shawn O. Pearce | 31bb1d1 | 2007-09-14 01:50:09 -0400 | [diff] [blame] | 81 | error_popup [strcat [mc "Invalid GIT_COMMITTER_IDENT:"] "\n\n$me"] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 82 | return {} |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return $GIT_COMMITTER_IDENT |
| 87 | } |
| 88 | |
| 89 | proc do_signoff {} { |
| 90 | global ui_comm |
| 91 | |
| 92 | set me [committer_ident] |
| 93 | if {$me eq {}} return |
| 94 | |
| 95 | set sob "Signed-off-by: $me" |
| 96 | set last [$ui_comm get {end -1c linestart} {end -1c}] |
| 97 | if {$last ne $sob} { |
| 98 | $ui_comm edit separator |
| 99 | if {$last ne {} |
| 100 | && ![regexp {^[A-Z][A-Za-z]*-[A-Za-z-]+: *} $last]} { |
| 101 | $ui_comm insert end "\n" |
| 102 | } |
| 103 | $ui_comm insert end "\n$sob" |
| 104 | $ui_comm edit separator |
| 105 | $ui_comm see end |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | proc create_new_commit {} { |
| 110 | global commit_type ui_comm |
| 111 | |
| 112 | set commit_type normal |
| 113 | $ui_comm delete 0.0 end |
| 114 | $ui_comm edit reset |
| 115 | $ui_comm edit modified false |
Shawn O. Pearce | 699d560 | 2007-07-05 23:16:13 -0400 | [diff] [blame] | 116 | rescan ui_ready |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | proc commit_tree {} { |
| 120 | global HEAD commit_type file_states ui_comm repo_config |
Shawn O. Pearce | 699d560 | 2007-07-05 23:16:13 -0400 | [diff] [blame] | 121 | global pch_error |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 122 | |
| 123 | if {[committer_ident] eq {}} return |
| 124 | if {![lock_index update]} return |
| 125 | |
| 126 | # -- Our in memory state should match the repository. |
| 127 | # |
| 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} { |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 133 | info_popup [mc "Last scanned state does not match repository state. |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 134 | |
| 135 | Another Git program has modified this repository since the last scan. A rescan must be performed before another commit can be created. |
| 136 | |
| 137 | The rescan will be automatically started now. |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 138 | "] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 139 | unlock_index |
Shawn O. Pearce | 699d560 | 2007-07-05 23:16:13 -0400 | [diff] [blame] | 140 | rescan ui_ready |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 141 | return |
| 142 | } |
| 143 | |
| 144 | # -- At least one file should differ in the index. |
| 145 | # |
| 146 | set files_ready 0 |
| 147 | foreach path [array names file_states] { |
| 148 | switch -glob -- [lindex $file_states($path) 0] { |
| 149 | _? {continue} |
| 150 | A? - |
| 151 | D? - |
| 152 | M? {set files_ready 1} |
| 153 | U? { |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 154 | error_popup [mc "Unmerged files cannot be committed. |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 155 | |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 156 | File %s has merge conflicts. You must resolve them and stage the file before committing. |
| 157 | " [short_path $path]] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 158 | unlock_index |
| 159 | return |
| 160 | } |
| 161 | default { |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 162 | error_popup [mc "Unknown file state %s detected. |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 163 | |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 164 | File %s cannot be committed by this program. |
| 165 | " [lindex $s 0] [short_path $path]] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
| 169 | if {!$files_ready && ![string match *merge $curType]} { |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 170 | info_popup [mc "No changes to commit. |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 171 | |
Christian Stimming | 360cc10 | 2007-07-28 22:17:10 +0200 | [diff] [blame] | 172 | You must stage at least 1 file before you can commit. |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 173 | "] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 174 | unlock_index |
| 175 | return |
| 176 | } |
| 177 | |
| 178 | # -- A message is required. |
| 179 | # |
| 180 | set msg [string trim [$ui_comm get 1.0 end]] |
| 181 | regsub -all -line {[ \t\r]+$} $msg {} msg |
| 182 | if {$msg eq {}} { |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 183 | error_popup [mc "Please supply a commit message. |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 184 | |
| 185 | A good commit message has the following format: |
| 186 | |
Michele Ballabio | 208320d | 2007-11-22 16:20:08 +0100 | [diff] [blame] | 187 | - First line: Describe in one sentence what you did. |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 188 | - Second line: Blank |
| 189 | - Remaining lines: Describe why this change is good. |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 190 | "] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 191 | unlock_index |
| 192 | return |
| 193 | } |
| 194 | |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 195 | # -- Build the message file. |
| 196 | # |
| 197 | set msg_p [gitdir GITGUI_EDITMSG] |
| 198 | set msg_wt [open $msg_p w] |
| 199 | fconfigure $msg_wt -translation lf |
| 200 | if {[catch {set enc $repo_config(i18n.commitencoding)}]} { |
| 201 | set enc utf-8 |
| 202 | } |
| 203 | set use_enc [tcl_encoding $enc] |
| 204 | if {$use_enc ne {}} { |
| 205 | fconfigure $msg_wt -encoding $use_enc |
| 206 | } else { |
| 207 | puts stderr [mc "warning: Tcl does not support encoding '%s'." $enc] |
| 208 | fconfigure $msg_wt -encoding utf-8 |
| 209 | } |
| 210 | puts $msg_wt $msg |
| 211 | close $msg_wt |
| 212 | |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 213 | # -- Run the pre-commit hook. |
| 214 | # |
Shawn O. Pearce | ed76cb7 | 2008-01-20 14:46:59 -0500 | [diff] [blame] | 215 | set fd_ph [githook_read pre-commit] |
| 216 | if {$fd_ph eq {}} { |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 217 | commit_commitmsg $curHEAD $msg_p |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 218 | return |
| 219 | } |
| 220 | |
Christian Stimming | 5e6d776 | 2008-02-02 10:20:17 +0100 | [diff] [blame] | 221 | ui_status [mc "Calling pre-commit hook..."] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 222 | set pch_error {} |
Shawn O. Pearce | 6eb420e | 2007-07-17 01:50:10 -0400 | [diff] [blame] | 223 | fconfigure $fd_ph -blocking 0 -translation binary -eofchar {} |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 224 | fileevent $fd_ph readable \ |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 225 | [list commit_prehook_wait $fd_ph $curHEAD $msg_p] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 228 | proc commit_prehook_wait {fd_ph curHEAD msg_p} { |
Shawn O. Pearce | 699d560 | 2007-07-05 23:16:13 -0400 | [diff] [blame] | 229 | global pch_error |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 230 | |
| 231 | append pch_error [read $fd_ph] |
| 232 | fconfigure $fd_ph -blocking 1 |
| 233 | if {[eof $fd_ph]} { |
| 234 | if {[catch {close $fd_ph}]} { |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 235 | catch {file delete $msg_p} |
Christian Stimming | 5e6d776 | 2008-02-02 10:20:17 +0100 | [diff] [blame] | 236 | ui_status [mc "Commit declined by pre-commit hook."] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 237 | hook_failed_popup pre-commit $pch_error |
| 238 | unlock_index |
| 239 | } else { |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 240 | commit_commitmsg $curHEAD $msg_p |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 241 | } |
| 242 | set pch_error {} |
| 243 | return |
| 244 | } |
| 245 | fconfigure $fd_ph -blocking 0 |
| 246 | } |
| 247 | |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 248 | proc commit_commitmsg {curHEAD msg_p} { |
| 249 | global pch_error |
| 250 | |
| 251 | # -- Run the commit-msg hook. |
| 252 | # |
Shawn O. Pearce | ed76cb7 | 2008-01-20 14:46:59 -0500 | [diff] [blame] | 253 | set fd_ph [githook_read commit-msg $msg_p] |
| 254 | if {$fd_ph eq {}} { |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 255 | commit_writetree $curHEAD $msg_p |
| 256 | return |
| 257 | } |
| 258 | |
Christian Stimming | 5e6d776 | 2008-02-02 10:20:17 +0100 | [diff] [blame] | 259 | ui_status [mc "Calling commit-msg hook..."] |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 260 | set pch_error {} |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 261 | fconfigure $fd_ph -blocking 0 -translation binary -eofchar {} |
| 262 | fileevent $fd_ph readable \ |
| 263 | [list commit_commitmsg_wait $fd_ph $curHEAD $msg_p] |
| 264 | } |
| 265 | |
| 266 | proc commit_commitmsg_wait {fd_ph curHEAD msg_p} { |
| 267 | global pch_error |
| 268 | |
| 269 | append pch_error [read $fd_ph] |
| 270 | fconfigure $fd_ph -blocking 1 |
| 271 | if {[eof $fd_ph]} { |
| 272 | if {[catch {close $fd_ph}]} { |
| 273 | catch {file delete $msg_p} |
Christian Stimming | 5e6d776 | 2008-02-02 10:20:17 +0100 | [diff] [blame] | 274 | ui_status [mc "Commit declined by commit-msg hook."] |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 275 | hook_failed_popup commit-msg $pch_error |
| 276 | unlock_index |
| 277 | } else { |
| 278 | commit_writetree $curHEAD $msg_p |
| 279 | } |
| 280 | set pch_error {} |
| 281 | return |
| 282 | } |
| 283 | fconfigure $fd_ph -blocking 0 |
| 284 | } |
| 285 | |
| 286 | proc commit_writetree {curHEAD msg_p} { |
Christian Stimming | 5e6d776 | 2008-02-02 10:20:17 +0100 | [diff] [blame] | 287 | ui_status [mc "Committing changes..."] |
Shawn O. Pearce | 0b81261 | 2007-07-09 01:17:09 -0400 | [diff] [blame] | 288 | set fd_wt [git_read write-tree] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 289 | fileevent $fd_wt readable \ |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 290 | [list commit_committree $fd_wt $curHEAD $msg_p] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 293 | proc commit_committree {fd_wt curHEAD msg_p} { |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 294 | global HEAD PARENT MERGE_HEAD commit_type |
Shawn O. Pearce | 699d560 | 2007-07-05 23:16:13 -0400 | [diff] [blame] | 295 | global current_branch |
| 296 | global ui_comm selected_commit_type |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 297 | global file_states selected_paths rescan_active |
| 298 | global repo_config |
| 299 | |
| 300 | gets $fd_wt tree_id |
Shawn O. Pearce | 8af52d7 | 2007-10-20 01:42:01 -0400 | [diff] [blame] | 301 | if {[catch {close $fd_wt} err]} { |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 302 | catch {file delete $msg_p} |
Shawn O. Pearce | 31bb1d1 | 2007-09-14 01:50:09 -0400 | [diff] [blame] | 303 | error_popup [strcat [mc "write-tree failed:"] "\n\n$err"] |
Christian Stimming | 5e6d776 | 2008-02-02 10:20:17 +0100 | [diff] [blame] | 304 | ui_status [mc "Commit failed."] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 305 | unlock_index |
| 306 | return |
| 307 | } |
| 308 | |
| 309 | # -- Verify this wasn't an empty change. |
| 310 | # |
| 311 | if {$commit_type eq {normal}} { |
Shawn O. Pearce | b215883 | 2007-07-12 02:45:23 -0400 | [diff] [blame] | 312 | set fd_ot [git_read cat-file commit $PARENT] |
Shawn O. Pearce | 20f1a10 | 2007-07-12 02:31:28 -0400 | [diff] [blame] | 313 | fconfigure $fd_ot -encoding binary -translation lf |
| 314 | set old_tree [gets $fd_ot] |
| 315 | close $fd_ot |
| 316 | |
| 317 | if {[string equal -length 5 {tree } $old_tree] |
| 318 | && [string length $old_tree] == 45} { |
| 319 | set old_tree [string range $old_tree 5 end] |
| 320 | } else { |
Michele Ballabio | c8c4854 | 2007-09-13 15:19:05 +0200 | [diff] [blame] | 321 | error [mc "Commit %s appears to be corrupt" $PARENT] |
Shawn O. Pearce | 20f1a10 | 2007-07-12 02:31:28 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 324 | if {$tree_id eq $old_tree} { |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 325 | catch {file delete $msg_p} |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 326 | info_popup [mc "No changes to commit. |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 327 | |
| 328 | No files were modified by this commit and it was not a merge commit. |
| 329 | |
| 330 | A rescan will be automatically started now. |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 331 | "] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 332 | unlock_index |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 333 | rescan {ui_status [mc "No changes to commit."]} |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 334 | return |
| 335 | } |
| 336 | } |
| 337 | |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 338 | # -- Create the commit. |
| 339 | # |
| 340 | set cmd [list commit-tree $tree_id] |
| 341 | foreach p [concat $PARENT $MERGE_HEAD] { |
| 342 | lappend cmd -p $p |
| 343 | } |
| 344 | lappend cmd <$msg_p |
| 345 | if {[catch {set cmt_id [eval git $cmd]} err]} { |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 346 | catch {file delete $msg_p} |
Shawn O. Pearce | 31bb1d1 | 2007-09-14 01:50:09 -0400 | [diff] [blame] | 347 | error_popup [strcat [mc "commit-tree failed:"] "\n\n$err"] |
Christian Stimming | 5e6d776 | 2008-02-02 10:20:17 +0100 | [diff] [blame] | 348 | ui_status [mc "Commit failed."] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 349 | unlock_index |
| 350 | return |
| 351 | } |
| 352 | |
| 353 | # -- Update the HEAD ref. |
| 354 | # |
| 355 | set reflogm commit |
| 356 | if {$commit_type ne {normal}} { |
| 357 | append reflogm " ($commit_type)" |
| 358 | } |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 359 | set msg_fd [open $msg_p r] |
| 360 | gets $msg_fd subject |
| 361 | close $msg_fd |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 362 | append reflogm {: } $subject |
| 363 | if {[catch { |
| 364 | git update-ref -m $reflogm HEAD $cmt_id $curHEAD |
| 365 | } err]} { |
Shawn O. Pearce | fb0ca47 | 2008-01-20 14:11:52 -0500 | [diff] [blame] | 366 | catch {file delete $msg_p} |
Shawn O. Pearce | 31bb1d1 | 2007-09-14 01:50:09 -0400 | [diff] [blame] | 367 | error_popup [strcat [mc "update-ref failed:"] "\n\n$err"] |
Christian Stimming | 5e6d776 | 2008-02-02 10:20:17 +0100 | [diff] [blame] | 368 | ui_status [mc "Commit failed."] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 369 | unlock_index |
| 370 | return |
| 371 | } |
| 372 | |
| 373 | # -- Cleanup after ourselves. |
| 374 | # |
| 375 | catch {file delete $msg_p} |
| 376 | catch {file delete [gitdir MERGE_HEAD]} |
| 377 | catch {file delete [gitdir MERGE_MSG]} |
| 378 | catch {file delete [gitdir SQUASH_MSG]} |
| 379 | catch {file delete [gitdir GITGUI_MSG]} |
| 380 | |
| 381 | # -- Let rerere do its thing. |
| 382 | # |
Shawn O. Pearce | d4c5307 | 2007-07-08 17:41:24 -0400 | [diff] [blame] | 383 | if {[get_config rerere.enabled] eq {}} { |
| 384 | set rerere [file isdirectory [gitdir rr-cache]] |
| 385 | } else { |
| 386 | set rerere [is_config_true rerere.enabled] |
| 387 | } |
| 388 | if {$rerere} { |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 389 | catch {git rerere} |
| 390 | } |
| 391 | |
| 392 | # -- Run the post-commit hook. |
| 393 | # |
Shawn O. Pearce | ed76cb7 | 2008-01-20 14:46:59 -0500 | [diff] [blame] | 394 | set fd_ph [githook_read post-commit] |
| 395 | if {$fd_ph ne {}} { |
| 396 | upvar #0 pch_error$cmt_id pc_err |
| 397 | set pc_err {} |
| 398 | fconfigure $fd_ph -blocking 0 -translation binary -eofchar {} |
| 399 | fileevent $fd_ph readable \ |
| 400 | [list commit_postcommit_wait $fd_ph $cmt_id] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | $ui_comm delete 0.0 end |
| 404 | $ui_comm edit reset |
| 405 | $ui_comm edit modified false |
Shawn O. Pearce | 4578c5c | 2007-07-21 04:57:57 -0400 | [diff] [blame] | 406 | if {$::GITGUI_BCK_exists} { |
| 407 | catch {file delete [gitdir GITGUI_BCK]} |
Shawn O. Pearce | 9c5a3c7 | 2007-07-23 00:20:04 -0400 | [diff] [blame] | 408 | set ::GITGUI_BCK_exists 0 |
Shawn O. Pearce | 4578c5c | 2007-07-21 04:57:57 -0400 | [diff] [blame] | 409 | } |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 410 | |
| 411 | if {[is_enabled singlecommit]} do_quit |
| 412 | |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 413 | # -- Update in memory status |
| 414 | # |
| 415 | set selected_commit_type new |
| 416 | set commit_type normal |
| 417 | set HEAD $cmt_id |
| 418 | set PARENT $cmt_id |
| 419 | set MERGE_HEAD [list] |
| 420 | |
| 421 | foreach path [array names file_states] { |
| 422 | set s $file_states($path) |
| 423 | set m [lindex $s 0] |
| 424 | switch -glob -- $m { |
| 425 | _O - |
| 426 | _M - |
| 427 | _D {continue} |
| 428 | __ - |
| 429 | A_ - |
| 430 | M_ - |
| 431 | D_ { |
| 432 | unset file_states($path) |
| 433 | catch {unset selected_paths($path)} |
| 434 | } |
| 435 | DO { |
| 436 | set file_states($path) [list _O [lindex $s 1] {} {}] |
| 437 | } |
| 438 | AM - |
| 439 | AD - |
| 440 | MM - |
| 441 | MD { |
| 442 | set file_states($path) [list \ |
| 443 | _[string index $m 1] \ |
| 444 | [lindex $s 1] \ |
| 445 | [lindex $s 3] \ |
| 446 | {}] |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | display_all_files |
| 452 | unlock_index |
| 453 | reshow_diff |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 454 | ui_status [mc "Created commit %s: %s" [string range $cmt_id 0 7] $subject] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 455 | } |
Shawn O. Pearce | ed76cb7 | 2008-01-20 14:46:59 -0500 | [diff] [blame] | 456 | |
| 457 | proc commit_postcommit_wait {fd_ph cmt_id} { |
| 458 | upvar #0 pch_error$cmt_id pch_error |
| 459 | |
| 460 | append pch_error [read $fd_ph] |
| 461 | fconfigure $fd_ph -blocking 1 |
| 462 | if {[eof $fd_ph]} { |
| 463 | if {[catch {close $fd_ph}]} { |
| 464 | hook_failed_popup post-commit $pch_error 0 |
| 465 | } |
| 466 | unset pch_error |
| 467 | return |
| 468 | } |
| 469 | fconfigure $fd_ph -blocking 0 |
| 470 | } |