]> asedeno.scripts.mit.edu Git - git.git/blob - lib/commit.tcl
Merge branch 'maint'
[git.git] / lib / commit.tcl
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} {
9                 error_popup [mc "There is nothing to amend.
10
11 You 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
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.
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
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]} {
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
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
116         rescan ui_ready
117 }
118
119 proc 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
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.
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                 T_ -
153                 M? {set files_ready 1}
154                 U? {
155                         error_popup [mc "Unmerged files cannot be committed.
156
157 File %s has merge conflicts.  You must resolve them and stage the file before committing.
158 " [short_path $path]]
159                         unlock_index
160                         return
161                 }
162                 default {
163                         error_popup [mc "Unknown file state %s detected.
164
165 File %s cannot be committed by this program.
166 " [lindex $s 0] [short_path $path]]
167                 }
168                 }
169         }
170         if {!$files_ready && ![string match *merge $curType]} {
171                 info_popup [mc "No changes to commit.
172
173 You must stage at least 1 file before you can commit.
174 "]
175                 unlock_index
176                 return
177         }
178
179         # -- A message is required.
180         #
181         set msg [string trim [$ui_comm get 1.0 end]]
182         regsub -all -line {[ \t\r]+$} $msg {} msg
183         if {$msg eq {}} {
184                 error_popup [mc "Please supply a commit message.
185
186 A good commit message has the following format:
187
188 - First line: Describe in one sentence what you did.
189 - Second line: Blank
190 - Remaining lines: Describe why this change is good.
191 "]
192                 unlock_index
193                 return
194         }
195
196         # -- Build the message file.
197         #
198         set msg_p [gitdir GITGUI_EDITMSG]
199         set msg_wt [open $msg_p w]
200         fconfigure $msg_wt -translation lf
201         if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
202                 set enc utf-8
203         }
204         set use_enc [tcl_encoding $enc]
205         if {$use_enc ne {}} {
206                 fconfigure $msg_wt -encoding $use_enc
207         } else {
208                 puts stderr [mc "warning: Tcl does not support encoding '%s'." $enc]
209                 fconfigure $msg_wt -encoding utf-8
210         }
211         puts $msg_wt $msg
212         close $msg_wt
213
214         # -- Run the pre-commit hook.
215         #
216         set fd_ph [githook_read pre-commit]
217         if {$fd_ph eq {}} {
218                 commit_commitmsg $curHEAD $msg_p
219                 return
220         }
221
222         ui_status [mc "Calling pre-commit hook..."]
223         set pch_error {}
224         fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
225         fileevent $fd_ph readable \
226                 [list commit_prehook_wait $fd_ph $curHEAD $msg_p]
227 }
228
229 proc commit_prehook_wait {fd_ph curHEAD msg_p} {
230         global pch_error
231
232         append pch_error [read $fd_ph]
233         fconfigure $fd_ph -blocking 1
234         if {[eof $fd_ph]} {
235                 if {[catch {close $fd_ph}]} {
236                         catch {file delete $msg_p}
237                         ui_status [mc "Commit declined by pre-commit hook."]
238                         hook_failed_popup pre-commit $pch_error
239                         unlock_index
240                 } else {
241                         commit_commitmsg $curHEAD $msg_p
242                 }
243                 set pch_error {}
244                 return
245         }
246         fconfigure $fd_ph -blocking 0
247 }
248
249 proc commit_commitmsg {curHEAD msg_p} {
250         global pch_error
251
252         # -- Run the commit-msg hook.
253         #
254         set fd_ph [githook_read commit-msg $msg_p]
255         if {$fd_ph eq {}} {
256                 commit_writetree $curHEAD $msg_p
257                 return
258         }
259
260         ui_status [mc "Calling commit-msg hook..."]
261         set pch_error {}
262         fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
263         fileevent $fd_ph readable \
264                 [list commit_commitmsg_wait $fd_ph $curHEAD $msg_p]
265 }
266
267 proc commit_commitmsg_wait {fd_ph curHEAD msg_p} {
268         global pch_error
269
270         append pch_error [read $fd_ph]
271         fconfigure $fd_ph -blocking 1
272         if {[eof $fd_ph]} {
273                 if {[catch {close $fd_ph}]} {
274                         catch {file delete $msg_p}
275                         ui_status [mc "Commit declined by commit-msg hook."]
276                         hook_failed_popup commit-msg $pch_error
277                         unlock_index
278                 } else {
279                         commit_writetree $curHEAD $msg_p
280                 }
281                 set pch_error {}
282                 return
283         }
284         fconfigure $fd_ph -blocking 0
285 }
286
287 proc commit_writetree {curHEAD msg_p} {
288         ui_status [mc "Committing changes..."]
289         set fd_wt [git_read write-tree]
290         fileevent $fd_wt readable \
291                 [list commit_committree $fd_wt $curHEAD $msg_p]
292 }
293
294 proc commit_committree {fd_wt curHEAD msg_p} {
295         global HEAD PARENT MERGE_HEAD commit_type
296         global current_branch
297         global ui_comm selected_commit_type
298         global file_states selected_paths rescan_active
299         global repo_config
300
301         gets $fd_wt tree_id
302         if {[catch {close $fd_wt} err]} {
303                 catch {file delete $msg_p}
304                 error_popup [strcat [mc "write-tree failed:"] "\n\n$err"]
305                 ui_status [mc "Commit failed."]
306                 unlock_index
307                 return
308         }
309
310         # -- Verify this wasn't an empty change.
311         #
312         if {$commit_type eq {normal}} {
313                 set fd_ot [git_read cat-file commit $PARENT]
314                 fconfigure $fd_ot -encoding binary -translation lf
315                 set old_tree [gets $fd_ot]
316                 close $fd_ot
317
318                 if {[string equal -length 5 {tree } $old_tree]
319                         && [string length $old_tree] == 45} {
320                         set old_tree [string range $old_tree 5 end]
321                 } else {
322                         error [mc "Commit %s appears to be corrupt" $PARENT]
323                 }
324
325                 if {$tree_id eq $old_tree} {
326                         catch {file delete $msg_p}
327                         info_popup [mc "No changes to commit.
328
329 No files were modified by this commit and it was not a merge commit.
330
331 A rescan will be automatically started now.
332 "]
333                         unlock_index
334                         rescan {ui_status [mc "No changes to commit."]}
335                         return
336                 }
337         }
338
339         # -- Create the commit.
340         #
341         set cmd [list commit-tree $tree_id]
342         foreach p [concat $PARENT $MERGE_HEAD] {
343                 lappend cmd -p $p
344         }
345         lappend cmd <$msg_p
346         if {[catch {set cmt_id [eval git $cmd]} err]} {
347                 catch {file delete $msg_p}
348                 error_popup [strcat [mc "commit-tree failed:"] "\n\n$err"]
349                 ui_status [mc "Commit failed."]
350                 unlock_index
351                 return
352         }
353
354         # -- Update the HEAD ref.
355         #
356         set reflogm commit
357         if {$commit_type ne {normal}} {
358                 append reflogm " ($commit_type)"
359         }
360         set msg_fd [open $msg_p r]
361         gets $msg_fd subject
362         close $msg_fd
363         append reflogm {: } $subject
364         if {[catch {
365                         git update-ref -m $reflogm HEAD $cmt_id $curHEAD
366                 } err]} {
367                 catch {file delete $msg_p}
368                 error_popup [strcat [mc "update-ref failed:"] "\n\n$err"]
369                 ui_status [mc "Commit failed."]
370                 unlock_index
371                 return
372         }
373
374         # -- Cleanup after ourselves.
375         #
376         catch {file delete $msg_p}
377         catch {file delete [gitdir MERGE_HEAD]}
378         catch {file delete [gitdir MERGE_MSG]}
379         catch {file delete [gitdir SQUASH_MSG]}
380         catch {file delete [gitdir GITGUI_MSG]}
381
382         # -- Let rerere do its thing.
383         #
384         if {[get_config rerere.enabled] eq {}} {
385                 set rerere [file isdirectory [gitdir rr-cache]]
386         } else {
387                 set rerere [is_config_true rerere.enabled]
388         }
389         if {$rerere} {
390                 catch {git rerere}
391         }
392
393         # -- Run the post-commit hook.
394         #
395         set fd_ph [githook_read post-commit]
396         if {$fd_ph ne {}} {
397                 upvar #0 pch_error$cmt_id pc_err
398                 set pc_err {}
399                 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
400                 fileevent $fd_ph readable \
401                         [list commit_postcommit_wait $fd_ph $cmt_id]
402         }
403
404         $ui_comm delete 0.0 end
405         $ui_comm edit reset
406         $ui_comm edit modified false
407         if {$::GITGUI_BCK_exists} {
408                 catch {file delete [gitdir GITGUI_BCK]}
409                 set ::GITGUI_BCK_exists 0
410         }
411
412         if {[is_enabled singlecommit]} do_quit
413
414         # -- Update in memory status
415         #
416         set selected_commit_type new
417         set commit_type normal
418         set HEAD $cmt_id
419         set PARENT $cmt_id
420         set MERGE_HEAD [list]
421
422         foreach path [array names file_states] {
423                 set s $file_states($path)
424                 set m [lindex $s 0]
425                 switch -glob -- $m {
426                 _O -
427                 _M -
428                 _D {continue}
429                 __ -
430                 A_ -
431                 M_ -
432                 T_ -
433                 D_ {
434                         unset file_states($path)
435                         catch {unset selected_paths($path)}
436                 }
437                 DO {
438                         set file_states($path) [list _O [lindex $s 1] {} {}]
439                 }
440                 AM -
441                 AD -
442                 MM -
443                 MD {
444                         set file_states($path) [list \
445                                 _[string index $m 1] \
446                                 [lindex $s 1] \
447                                 [lindex $s 3] \
448                                 {}]
449                 }
450                 }
451         }
452
453         display_all_files
454         unlock_index
455         reshow_diff
456         ui_status [mc "Created commit %s: %s" [string range $cmt_id 0 7] $subject]
457 }
458
459 proc commit_postcommit_wait {fd_ph cmt_id} {
460         upvar #0 pch_error$cmt_id pch_error
461
462         append pch_error [read $fd_ph]
463         fconfigure $fd_ph -blocking 1
464         if {[eof $fd_ph]} {
465                 if {[catch {close $fd_ph}]} {
466                         hook_failed_popup post-commit $pch_error 0
467                 }
468                 unset pch_error
469                 return
470         }
471         fconfigure $fd_ph -blocking 0
472 }