]>
Commit | Line | Data |
---|---|---|
f522c9b5 SP |
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} { | |
1ac17950 | 9 | error_popup [mc "There is nothing to amend. |
f522c9b5 SP |
10 | |
11 | You are about to create the initial commit. There is no commit before this to amend. | |
1ac17950 | 12 | "] |
f522c9b5 SP |
13 | return |
14 | } | |
15 | ||
16 | repository_state curType curHEAD curMERGE_HEAD | |
17 | if {$curType eq {merge}} { | |
1ac17950 | 18 | error_popup [mc "Cannot amend while merging. |
f522c9b5 SP |
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. | |
1ac17950 | 21 | "] |
f522c9b5 SP |
22 | return |
23 | } | |
24 | ||
25 | set msg {} | |
26 | set parents [list] | |
27 | if {[catch { | |
0b812616 | 28 | set fd [git_read cat-file commit $curHEAD] |
f522c9b5 SP |
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 | } | |
c4638f66 | 40 | set msg [read $fd] |
f522c9b5 | 41 | close $fd |
c4638f66 SP |
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] | |
f522c9b5 | 48 | } err]} { |
31bb1d1b | 49 | error_popup [strcat [mc "Error loading commit data for amend:"] "\n\n$err"] |
f522c9b5 SP |
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 | |
699d5601 | 66 | rescan ui_ready |
f522c9b5 SP |
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]} { | |
31bb1d1b | 76 | error_popup [strcat [mc "Unable to obtain your identity:"] "\n\n$err"] |
f522c9b5 SP |
77 | return {} |
78 | } | |
79 | if {![regexp {^(.*) [0-9]+ [-+0-9]+$} \ | |
80 | $me me GIT_COMMITTER_IDENT]} { | |
31bb1d1b | 81 | error_popup [strcat [mc "Invalid GIT_COMMITTER_IDENT:"] "\n\n$me"] |
f522c9b5 SP |
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 | |
699d5601 | 116 | rescan ui_ready |
f522c9b5 SP |
117 | } |
118 | ||
119 | proc commit_tree {} { | |
120 | global HEAD commit_type file_states ui_comm repo_config | |
699d5601 | 121 | global pch_error |
f522c9b5 SP |
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} { | |
1ac17950 | 133 | info_popup [mc "Last scanned state does not match repository state. |
f522c9b5 SP |
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. | |
1ac17950 | 138 | "] |
f522c9b5 | 139 | unlock_index |
699d5601 | 140 | rescan ui_ready |
f522c9b5 SP |
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? { | |
1ac17950 | 154 | error_popup [mc "Unmerged files cannot be committed. |
f522c9b5 | 155 | |
1ac17950 CS |
156 | File %s has merge conflicts. You must resolve them and stage the file before committing. |
157 | " [short_path $path]] | |
f522c9b5 SP |
158 | unlock_index |
159 | return | |
160 | } | |
161 | default { | |
1ac17950 | 162 | error_popup [mc "Unknown file state %s detected. |
f522c9b5 | 163 | |
1ac17950 CS |
164 | File %s cannot be committed by this program. |
165 | " [lindex $s 0] [short_path $path]] | |
f522c9b5 SP |
166 | } |
167 | } | |
168 | } | |
169 | if {!$files_ready && ![string match *merge $curType]} { | |
1ac17950 | 170 | info_popup [mc "No changes to commit. |
f522c9b5 | 171 | |
360cc106 | 172 | You must stage at least 1 file before you can commit. |
1ac17950 | 173 | "] |
f522c9b5 SP |
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 {}} { | |
1ac17950 | 183 | error_popup [mc "Please supply a commit message. |
f522c9b5 SP |
184 | |
185 | A good commit message has the following format: | |
186 | ||
208320de | 187 | - First line: Describe in one sentence what you did. |
f522c9b5 SP |
188 | - Second line: Blank |
189 | - Remaining lines: Describe why this change is good. | |
1ac17950 | 190 | "] |
f522c9b5 SP |
191 | unlock_index |
192 | return | |
193 | } | |
194 | ||
195 | # -- Run the pre-commit hook. | |
196 | # | |
197 | set pchook [gitdir hooks pre-commit] | |
198 | ||
199 | # On Cygwin [file executable] might lie so we need to ask | |
200 | # the shell if the hook is executable. Yes that's annoying. | |
201 | # | |
202 | if {[is_Cygwin] && [file isfile $pchook]} { | |
203 | set pchook [list sh -c [concat \ | |
204 | "if test -x \"$pchook\";" \ | |
205 | "then exec \"$pchook\" 2>&1;" \ | |
206 | "fi"]] | |
207 | } elseif {[file executable $pchook]} { | |
208 | set pchook [list $pchook |& cat] | |
209 | } else { | |
210 | commit_writetree $curHEAD $msg | |
211 | return | |
212 | } | |
213 | ||
699d5601 | 214 | ui_status {Calling pre-commit hook...} |
f522c9b5 SP |
215 | set pch_error {} |
216 | set fd_ph [open "| $pchook" r] | |
6eb420ef | 217 | fconfigure $fd_ph -blocking 0 -translation binary -eofchar {} |
f522c9b5 SP |
218 | fileevent $fd_ph readable \ |
219 | [list commit_prehook_wait $fd_ph $curHEAD $msg] | |
220 | } | |
221 | ||
222 | proc commit_prehook_wait {fd_ph curHEAD msg} { | |
699d5601 | 223 | global pch_error |
f522c9b5 SP |
224 | |
225 | append pch_error [read $fd_ph] | |
226 | fconfigure $fd_ph -blocking 1 | |
227 | if {[eof $fd_ph]} { | |
228 | if {[catch {close $fd_ph}]} { | |
699d5601 | 229 | ui_status {Commit declined by pre-commit hook.} |
f522c9b5 SP |
230 | hook_failed_popup pre-commit $pch_error |
231 | unlock_index | |
232 | } else { | |
233 | commit_writetree $curHEAD $msg | |
234 | } | |
235 | set pch_error {} | |
236 | return | |
237 | } | |
238 | fconfigure $fd_ph -blocking 0 | |
239 | } | |
240 | ||
241 | proc commit_writetree {curHEAD msg} { | |
699d5601 | 242 | ui_status {Committing changes...} |
0b812616 | 243 | set fd_wt [git_read write-tree] |
f522c9b5 SP |
244 | fileevent $fd_wt readable \ |
245 | [list commit_committree $fd_wt $curHEAD $msg] | |
246 | } | |
247 | ||
248 | proc commit_committree {fd_wt curHEAD msg} { | |
249 | global HEAD PARENT MERGE_HEAD commit_type | |
699d5601 SP |
250 | global current_branch |
251 | global ui_comm selected_commit_type | |
f522c9b5 SP |
252 | global file_states selected_paths rescan_active |
253 | global repo_config | |
254 | ||
255 | gets $fd_wt tree_id | |
8af52d7a | 256 | if {[catch {close $fd_wt} err]} { |
31bb1d1b | 257 | error_popup [strcat [mc "write-tree failed:"] "\n\n$err"] |
699d5601 | 258 | ui_status {Commit failed.} |
f522c9b5 SP |
259 | unlock_index |
260 | return | |
261 | } | |
262 | ||
263 | # -- Verify this wasn't an empty change. | |
264 | # | |
265 | if {$commit_type eq {normal}} { | |
b215883d | 266 | set fd_ot [git_read cat-file commit $PARENT] |
20f1a10b SP |
267 | fconfigure $fd_ot -encoding binary -translation lf |
268 | set old_tree [gets $fd_ot] | |
269 | close $fd_ot | |
270 | ||
271 | if {[string equal -length 5 {tree } $old_tree] | |
272 | && [string length $old_tree] == 45} { | |
273 | set old_tree [string range $old_tree 5 end] | |
274 | } else { | |
c8c4854b | 275 | error [mc "Commit %s appears to be corrupt" $PARENT] |
20f1a10b SP |
276 | } |
277 | ||
f522c9b5 | 278 | if {$tree_id eq $old_tree} { |
1ac17950 | 279 | info_popup [mc "No changes to commit. |
f522c9b5 SP |
280 | |
281 | No files were modified by this commit and it was not a merge commit. | |
282 | ||
283 | A rescan will be automatically started now. | |
1ac17950 | 284 | "] |
f522c9b5 | 285 | unlock_index |
1ac17950 | 286 | rescan {ui_status [mc "No changes to commit."]} |
f522c9b5 SP |
287 | return |
288 | } | |
289 | } | |
290 | ||
291 | # -- Build the message. | |
292 | # | |
293 | set msg_p [gitdir COMMIT_EDITMSG] | |
294 | set msg_wt [open $msg_p w] | |
c4638f66 | 295 | fconfigure $msg_wt -translation lf |
f522c9b5 SP |
296 | if {[catch {set enc $repo_config(i18n.commitencoding)}]} { |
297 | set enc utf-8 | |
298 | } | |
c4638f66 SP |
299 | set use_enc [tcl_encoding $enc] |
300 | if {$use_enc ne {}} { | |
301 | fconfigure $msg_wt -encoding $use_enc | |
302 | } else { | |
c8c4854b | 303 | puts stderr [mc "warning: Tcl does not support encoding '%s'." $enc] |
c4638f66 SP |
304 | fconfigure $msg_wt -encoding utf-8 |
305 | } | |
a6d5888f | 306 | puts $msg_wt $msg |
f522c9b5 SP |
307 | close $msg_wt |
308 | ||
309 | # -- Create the commit. | |
310 | # | |
311 | set cmd [list commit-tree $tree_id] | |
312 | foreach p [concat $PARENT $MERGE_HEAD] { | |
313 | lappend cmd -p $p | |
314 | } | |
315 | lappend cmd <$msg_p | |
316 | if {[catch {set cmt_id [eval git $cmd]} err]} { | |
31bb1d1b | 317 | error_popup [strcat [mc "commit-tree failed:"] "\n\n$err"] |
699d5601 | 318 | ui_status {Commit failed.} |
f522c9b5 SP |
319 | unlock_index |
320 | return | |
321 | } | |
322 | ||
323 | # -- Update the HEAD ref. | |
324 | # | |
325 | set reflogm commit | |
326 | if {$commit_type ne {normal}} { | |
327 | append reflogm " ($commit_type)" | |
328 | } | |
329 | set i [string first "\n" $msg] | |
330 | if {$i >= 0} { | |
331 | set subject [string range $msg 0 [expr {$i - 1}]] | |
332 | } else { | |
333 | set subject $msg | |
334 | } | |
335 | append reflogm {: } $subject | |
336 | if {[catch { | |
337 | git update-ref -m $reflogm HEAD $cmt_id $curHEAD | |
338 | } err]} { | |
31bb1d1b | 339 | error_popup [strcat [mc "update-ref failed:"] "\n\n$err"] |
699d5601 | 340 | ui_status {Commit failed.} |
f522c9b5 SP |
341 | unlock_index |
342 | return | |
343 | } | |
344 | ||
345 | # -- Cleanup after ourselves. | |
346 | # | |
347 | catch {file delete $msg_p} | |
348 | catch {file delete [gitdir MERGE_HEAD]} | |
349 | catch {file delete [gitdir MERGE_MSG]} | |
350 | catch {file delete [gitdir SQUASH_MSG]} | |
351 | catch {file delete [gitdir GITGUI_MSG]} | |
352 | ||
353 | # -- Let rerere do its thing. | |
354 | # | |
d4c53077 SP |
355 | if {[get_config rerere.enabled] eq {}} { |
356 | set rerere [file isdirectory [gitdir rr-cache]] | |
357 | } else { | |
358 | set rerere [is_config_true rerere.enabled] | |
359 | } | |
360 | if {$rerere} { | |
f522c9b5 SP |
361 | catch {git rerere} |
362 | } | |
363 | ||
364 | # -- Run the post-commit hook. | |
365 | # | |
366 | set pchook [gitdir hooks post-commit] | |
367 | if {[is_Cygwin] && [file isfile $pchook]} { | |
368 | set pchook [list sh -c [concat \ | |
369 | "if test -x \"$pchook\";" \ | |
370 | "then exec \"$pchook\";" \ | |
371 | "fi"]] | |
372 | } elseif {![file executable $pchook]} { | |
373 | set pchook {} | |
374 | } | |
375 | if {$pchook ne {}} { | |
376 | catch {exec $pchook &} | |
377 | } | |
378 | ||
379 | $ui_comm delete 0.0 end | |
380 | $ui_comm edit reset | |
381 | $ui_comm edit modified false | |
4578c5cb SP |
382 | if {$::GITGUI_BCK_exists} { |
383 | catch {file delete [gitdir GITGUI_BCK]} | |
9c5a3c77 | 384 | set ::GITGUI_BCK_exists 0 |
4578c5cb | 385 | } |
f522c9b5 SP |
386 | |
387 | if {[is_enabled singlecommit]} do_quit | |
388 | ||
f522c9b5 SP |
389 | # -- Update in memory status |
390 | # | |
391 | set selected_commit_type new | |
392 | set commit_type normal | |
393 | set HEAD $cmt_id | |
394 | set PARENT $cmt_id | |
395 | set MERGE_HEAD [list] | |
396 | ||
397 | foreach path [array names file_states] { | |
398 | set s $file_states($path) | |
399 | set m [lindex $s 0] | |
400 | switch -glob -- $m { | |
401 | _O - | |
402 | _M - | |
403 | _D {continue} | |
404 | __ - | |
405 | A_ - | |
406 | M_ - | |
407 | D_ { | |
408 | unset file_states($path) | |
409 | catch {unset selected_paths($path)} | |
410 | } | |
411 | DO { | |
412 | set file_states($path) [list _O [lindex $s 1] {} {}] | |
413 | } | |
414 | AM - | |
415 | AD - | |
416 | MM - | |
417 | MD { | |
418 | set file_states($path) [list \ | |
419 | _[string index $m 1] \ | |
420 | [lindex $s 1] \ | |
421 | [lindex $s 3] \ | |
422 | {}] | |
423 | } | |
424 | } | |
425 | } | |
426 | ||
427 | display_all_files | |
428 | unlock_index | |
429 | reshow_diff | |
1ac17950 | 430 | ui_status [mc "Created commit %s: %s" [string range $cmt_id 0 7] $subject] |
f522c9b5 | 431 | } |