73e18bf9825d7ce7c5dfab4a1a35082333864bd0
1# git-gui misc. commit reading/writing support
2# Copyright (C) 2006, 2007 Shawn Pearce
3
4proc load_last_commit {} {
5 global HEAD PARENT MERGE_HEAD commit_type ui_comm
6 global repo_config
7
8 if {[llength $PARENT] == 0} {
9 error_popup [mc "There is nothing to amend.
10
11You are about to create the initial commit. There is no commit before this to amend.
12"]
13 return
14 }
15
16 repository_state curType curHEAD curMERGE_HEAD
17 if {$curType eq {merge}} {
18 error_popup [mc "Cannot amend while merging.
19
20You 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.
21"]
22 return
23 }
24
25 set msg {}
26 set parents [list]
27 if {[catch {
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)}]} {
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 }
40 set msg [read $fd]
41 close $fd
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]
48 } err]} {
49 error_popup [strcat [mc "Error loading commit data for amend:"] "\n\n$err"]
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
66 rescan ui_ready
67}
68
69set GIT_COMMITTER_IDENT {}
70
71proc 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]} {
76 error_popup [strcat [mc "Unable to obtain your identity:"] "\n\n$err"]
77 return {}
78 }
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"]
82 return {}
83 }
84 }
85
86 return $GIT_COMMITTER_IDENT
87}
88
89proc 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
109proc 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
116 rescan ui_ready
117}
118
119proc commit_tree {} {
120 global HEAD commit_type file_states ui_comm repo_config
121 global pch_error
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} {
133 info_popup [mc "Last scanned state does not match repository state.
134
135Another Git program has modified this repository since the last scan. A rescan must be performed before another commit can be created.
136
137The rescan will be automatically started now.
138"]
139 unlock_index
140 rescan ui_ready
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? {
154 error_popup [mc "Unmerged files cannot be committed.
155
156File %s has merge conflicts. You must resolve them and stage the file before committing.
157" [short_path $path]]
158 unlock_index
159 return
160 }
161 default {
162 error_popup [mc "Unknown file state %s detected.
163
164File %s cannot be committed by this program.
165" [lindex $s 0] [short_path $path]]
166 }
167 }
168 }
169 if {!$files_ready && ![string match *merge $curType]} {
170 info_popup [mc "No changes to commit.
171
172You must stage at least 1 file before you can commit.
173"]
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 {}} {
183 error_popup [mc "Please supply a commit message.
184
185A good commit message has the following format:
186
187- First line: Describe in one sentence what you did.
188- Second line: Blank
189- Remaining lines: Describe why this change is good.
190"]
191 unlock_index
192 return
193 }
194
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
213 # -- Run the pre-commit hook.
214 #
215 set pchook [gitdir hooks pre-commit]
216
217 # On Cygwin [file executable] might lie so we need to ask
218 # the shell if the hook is executable. Yes that's annoying.
219 #
220 if {[is_Cygwin] && [file isfile $pchook]} {
221 set pchook [list sh -c [concat \
222 "if test -x \"$pchook\";" \
223 "then exec \"$pchook\" 2>&1;" \
224 "fi"]]
225 } elseif {[file executable $pchook]} {
226 set pchook [list $pchook |& cat]
227 } else {
228 commit_commitmsg $curHEAD $msg_p
229 return
230 }
231
232 ui_status {Calling pre-commit hook...}
233 set pch_error {}
234 set fd_ph [open "| $pchook" r]
235 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
236 fileevent $fd_ph readable \
237 [list commit_prehook_wait $fd_ph $curHEAD $msg_p]
238}
239
240proc commit_prehook_wait {fd_ph curHEAD msg_p} {
241 global pch_error
242
243 append pch_error [read $fd_ph]
244 fconfigure $fd_ph -blocking 1
245 if {[eof $fd_ph]} {
246 if {[catch {close $fd_ph}]} {
247 catch {file delete $msg_p}
248 ui_status {Commit declined by pre-commit hook.}
249 hook_failed_popup pre-commit $pch_error
250 unlock_index
251 } else {
252 commit_commitmsg $curHEAD $msg_p
253 }
254 set pch_error {}
255 return
256 }
257 fconfigure $fd_ph -blocking 0
258}
259
260proc commit_commitmsg {curHEAD msg_p} {
261 global pch_error
262
263 # -- Run the commit-msg hook.
264 #
265 set pchook [gitdir hooks commit-msg]
266
267 # On Cygwin [file executable] might lie so we need to ask
268 # the shell if the hook is executable. Yes that's annoying.
269 #
270 if {[is_Cygwin] && [file isfile $pchook]} {
271 set pchook [list sh -c [concat \
272 "if test -x \"$pchook\";" \
273 "then exec \"$pchook\" \"$msg_p\" 2>&1;" \
274 "fi"]]
275 } elseif {[file executable $pchook]} {
276 set pchook [list $pchook $msg_p |& cat]
277 } else {
278 commit_writetree $curHEAD $msg_p
279 return
280 }
281
282 ui_status {Calling commit-msg hook...}
283 set pch_error {}
284 set fd_ph [open "| $pchook" r]
285 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
286 fileevent $fd_ph readable \
287 [list commit_commitmsg_wait $fd_ph $curHEAD $msg_p]
288}
289
290proc commit_commitmsg_wait {fd_ph curHEAD msg_p} {
291 global pch_error
292
293 append pch_error [read $fd_ph]
294 fconfigure $fd_ph -blocking 1
295 if {[eof $fd_ph]} {
296 if {[catch {close $fd_ph}]} {
297 catch {file delete $msg_p}
298 ui_status {Commit declined by commit-msg hook.}
299 hook_failed_popup commit-msg $pch_error
300 unlock_index
301 } else {
302 commit_writetree $curHEAD $msg_p
303 }
304 set pch_error {}
305 return
306 }
307 fconfigure $fd_ph -blocking 0
308}
309
310proc commit_writetree {curHEAD msg_p} {
311 ui_status {Committing changes...}
312 set fd_wt [git_read write-tree]
313 fileevent $fd_wt readable \
314 [list commit_committree $fd_wt $curHEAD $msg_p]
315}
316
317proc commit_committree {fd_wt curHEAD msg_p} {
318 global HEAD PARENT MERGE_HEAD commit_type
319 global current_branch
320 global ui_comm selected_commit_type
321 global file_states selected_paths rescan_active
322 global repo_config
323
324 gets $fd_wt tree_id
325 if {[catch {close $fd_wt} err]} {
326 catch {file delete $msg_p}
327 error_popup [strcat [mc "write-tree failed:"] "\n\n$err"]
328 ui_status {Commit failed.}
329 unlock_index
330 return
331 }
332
333 # -- Verify this wasn't an empty change.
334 #
335 if {$commit_type eq {normal}} {
336 set fd_ot [git_read cat-file commit $PARENT]
337 fconfigure $fd_ot -encoding binary -translation lf
338 set old_tree [gets $fd_ot]
339 close $fd_ot
340
341 if {[string equal -length 5 {tree } $old_tree]
342 && [string length $old_tree] == 45} {
343 set old_tree [string range $old_tree 5 end]
344 } else {
345 error [mc "Commit %s appears to be corrupt" $PARENT]
346 }
347
348 if {$tree_id eq $old_tree} {
349 catch {file delete $msg_p}
350 info_popup [mc "No changes to commit.
351
352No files were modified by this commit and it was not a merge commit.
353
354A rescan will be automatically started now.
355"]
356 unlock_index
357 rescan {ui_status [mc "No changes to commit."]}
358 return
359 }
360 }
361
362 # -- Create the commit.
363 #
364 set cmd [list commit-tree $tree_id]
365 foreach p [concat $PARENT $MERGE_HEAD] {
366 lappend cmd -p $p
367 }
368 lappend cmd <$msg_p
369 if {[catch {set cmt_id [eval git $cmd]} err]} {
370 catch {file delete $msg_p}
371 error_popup [strcat [mc "commit-tree failed:"] "\n\n$err"]
372 ui_status {Commit failed.}
373 unlock_index
374 return
375 }
376
377 # -- Update the HEAD ref.
378 #
379 set reflogm commit
380 if {$commit_type ne {normal}} {
381 append reflogm " ($commit_type)"
382 }
383 set msg_fd [open $msg_p r]
384 gets $msg_fd subject
385 close $msg_fd
386 append reflogm {: } $subject
387 if {[catch {
388 git update-ref -m $reflogm HEAD $cmt_id $curHEAD
389 } err]} {
390 catch {file delete $msg_p}
391 error_popup [strcat [mc "update-ref failed:"] "\n\n$err"]
392 ui_status {Commit failed.}
393 unlock_index
394 return
395 }
396
397 # -- Cleanup after ourselves.
398 #
399 catch {file delete $msg_p}
400 catch {file delete [gitdir MERGE_HEAD]}
401 catch {file delete [gitdir MERGE_MSG]}
402 catch {file delete [gitdir SQUASH_MSG]}
403 catch {file delete [gitdir GITGUI_MSG]}
404
405 # -- Let rerere do its thing.
406 #
407 if {[get_config rerere.enabled] eq {}} {
408 set rerere [file isdirectory [gitdir rr-cache]]
409 } else {
410 set rerere [is_config_true rerere.enabled]
411 }
412 if {$rerere} {
413 catch {git rerere}
414 }
415
416 # -- Run the post-commit hook.
417 #
418 set pchook [gitdir hooks post-commit]
419 if {[is_Cygwin] && [file isfile $pchook]} {
420 set pchook [list sh -c [concat \
421 "if test -x \"$pchook\";" \
422 "then exec \"$pchook\";" \
423 "fi"]]
424 } elseif {![file executable $pchook]} {
425 set pchook {}
426 }
427 if {$pchook ne {}} {
428 catch {exec $pchook &}
429 }
430
431 $ui_comm delete 0.0 end
432 $ui_comm edit reset
433 $ui_comm edit modified false
434 if {$::GITGUI_BCK_exists} {
435 catch {file delete [gitdir GITGUI_BCK]}
436 set ::GITGUI_BCK_exists 0
437 }
438
439 if {[is_enabled singlecommit]} do_quit
440
441 # -- Update in memory status
442 #
443 set selected_commit_type new
444 set commit_type normal
445 set HEAD $cmt_id
446 set PARENT $cmt_id
447 set MERGE_HEAD [list]
448
449 foreach path [array names file_states] {
450 set s $file_states($path)
451 set m [lindex $s 0]
452 switch -glob -- $m {
453 _O -
454 _M -
455 _D {continue}
456 __ -
457 A_ -
458 M_ -
459 D_ {
460 unset file_states($path)
461 catch {unset selected_paths($path)}
462 }
463 DO {
464 set file_states($path) [list _O [lindex $s 1] {} {}]
465 }
466 AM -
467 AD -
468 MM -
469 MD {
470 set file_states($path) [list \
471 _[string index $m 1] \
472 [lindex $s 1] \
473 [lindex $s 3] \
474 {}]
475 }
476 }
477 }
478
479 display_all_files
480 unlock_index
481 reshow_diff
482 ui_status [mc "Created commit %s: %s" [string range $cmt_id 0 7] $subject]
483}