git-gui: Implemented amended commits.
Also fixed a bug related that caused a crash if the file currently in the diff viewer is no longer modified after the commit. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Nov 6, 2006 at 21:34 UTC
e57ca85e113437649ec908ce48fc43a659d650f8
1 file changed
+119
-41
git-gui
+119
-41
@@ -21,6 +21,10 @@ set update_index_fd {}
21
set disable_on_lock [list]
22
set index_lock_type none
23
24
+set HEAD {}
25
+set PARENT {}
26
+set commit_type {}
27
+
28
proc lock_index {type} {
29
global index_lock_type disable_on_lock
30
@@ -63,14 +67,23 @@ proc repository_state {hdvar ctvar} {
67
}
68
}
69
66
-proc update_status {} {
67
- global HEAD commit_type
70
+proc update_status {{final Ready.}} {
71
+ global HEAD PARENT commit_type
72
global ui_index ui_other ui_status_value ui_comm
73
global status_active file_states
74
75
if {$status_active || ![lock_index read]} return
76
73
- repository_state HEAD commit_type
77
+ repository_state new_HEAD new_type
78
+ if {$commit_type == {amend}
79
+ && $new_type == {normal}
80
+ && $new_HEAD == $HEAD} {
81
+ } else {
82
+ set HEAD $new_HEAD
83
+ set PARENT $new_HEAD
84
+ set commit_type $new_type
85
+ }
86
+
87
array unset file_states
88
foreach w [list $ui_index $ui_other] {
89
$w conf -state normal
@@ -91,11 +104,11 @@ proc update_status {} {
104
set ui_status_value {Refreshing file status...}
105
set fd_rf [open "| git update-index -q --unmerged --refresh" r]
106
fconfigure $fd_rf -blocking 0 -translation binary
94
- fileevent $fd_rf readable [list read_refresh $fd_rf]
107
+ fileevent $fd_rf readable [list read_refresh $fd_rf $final]
108
}
109
97
-proc read_refresh {fd} {
98
- global gitdir HEAD commit_type
110
+proc read_refresh {fd final} {
111
+ global gitdir PARENT commit_type
112
global ui_index ui_other ui_status_value ui_comm
113
global status_active file_states
114
@@ -112,27 +125,27 @@ proc read_refresh {fd} {
125
126
set status_active 3
127
set ui_status_value {Scanning for modified files ...}
115
- set fd_di [open "| git diff-index --cached -z $HEAD" r]
128
+ set fd_di [open "| git diff-index --cached -z $PARENT" r]
129
set fd_df [open "| git diff-files -z" r]
130
set fd_lo [open $ls_others r]
131
132
fconfigure $fd_di -blocking 0 -translation binary
133
fconfigure $fd_df -blocking 0 -translation binary
134
fconfigure $fd_lo -blocking 0 -translation binary
122
- fileevent $fd_di readable [list read_diff_index $fd_di]
123
- fileevent $fd_df readable [list read_diff_files $fd_df]
124
- fileevent $fd_lo readable [list read_ls_others $fd_lo]
135
+ fileevent $fd_di readable [list read_diff_index $fd_di $final]
136
+ fileevent $fd_df readable [list read_diff_files $fd_df $final]
137
+ fileevent $fd_lo readable [list read_ls_others $fd_lo $final]
138
}
139
140
proc load_message {file} {
141
global gitdir ui_comm
142
143
set f [file join $gitdir $file]
131
- if {[file exists $f]} {
144
+ if {[file isfile $f]} {
145
if {[catch {set fd [open $f r]}]} {
146
return 0
147
}
135
- set content [read $fd]
148
+ set content [string trim [read $fd]]
149
close $fd
150
$ui_comm delete 0.0 end
151
$ui_comm insert end $content
@@ -141,7 +154,7 @@ proc load_message {file} {
154
return 0
155
}
156
144
-proc read_diff_index {fd} {
157
+proc read_diff_index {fd final} {
158
global buf_rdi
159
160
append buf_rdi [read $fd]
@@ -152,10 +165,10 @@ proc read_diff_index {fd} {
165
display_file $p [string index $m end]_
166
}
167
}
155
- status_eof $fd buf_rdi
168
+ status_eof $fd buf_rdi $final
169
}
170
158
-proc read_diff_files {fd} {
171
+proc read_diff_files {fd final} {
172
global buf_rdf
173
174
append buf_rdf [read $fd]
@@ -166,10 +179,10 @@ proc read_diff_files {fd} {
179
display_file $p _[string index $m end]
180
}
181
}
169
- status_eof $fd buf_rdf
182
+ status_eof $fd buf_rdf $final
183
}
184
172
-proc read_ls_others {fd} {
185
+proc read_ls_others {fd final} {
186
global buf_rlo
187
188
append buf_rlo [read $fd]
@@ -178,21 +191,25 @@ proc read_ls_others {fd} {
191
foreach p [lrange $pck 0 end-1] {
192
display_file $p _O
193
}
181
- status_eof $fd buf_rlo
194
+ status_eof $fd buf_rlo $final
195
}
196
184
-proc status_eof {fd buf} {
197
+proc status_eof {fd buf final} {
198
global status_active $buf
186
- global ui_fname_value ui_status_value
199
+ global ui_fname_value ui_status_value file_states
200
201
if {[eof $fd]} {
202
set $buf {}
203
close $fd
204
if {[incr status_active -1] == 0} {
205
unlock_index
193
- set ui_status_value {Ready.}
194
- if {$ui_fname_value != {}} {
206
+
207
+ set ui_status_value $final
208
+ if {$ui_fname_value != {} && [array names file_states \
209
+ -exact $ui_fname_value] != {}} {
210
show_diff $ui_fname_value
211
+ } else {
212
+ clear_diff
213
}
214
}
215
}
@@ -213,7 +230,7 @@ proc clear_diff {} {
230
}
231
232
proc show_diff {path} {
216
- global file_states HEAD diff_3way diff_active
233
+ global file_states PARENT diff_3way diff_active
234
global ui_diff ui_fname_value ui_fstatus_value ui_status_value
235
236
if {$diff_active || ![lock_index read]} return
@@ -227,12 +244,12 @@ proc show_diff {path} {
244
set ui_fstatus_value [mapdesc $m $path]
245
set ui_status_value "Loading diff of $path..."
246
230
- set cmd [list | git diff-index -p $HEAD -- $path]
247
+ set cmd [list | git diff-index -p $PARENT -- $path]
248
switch $m {
249
AM {
250
}
251
MM {
235
- set cmd [list | git diff-index -p -c $HEAD $path]
252
+ set cmd [list | git diff-index -p -c $PARENT $path]
253
}
254
_O {
255
if {[catch {
@@ -320,6 +337,51 @@ proc read_diff {fd} {
337
##
338
## commit
339
340
+proc load_last_commit {} {
341
+ global HEAD PARENT commit_type ui_comm
342
+
343
+ if {$commit_type == {amend}} return
344
+ if {$commit_type != {normal}} {
345
+ error_popup "Can't amend a $commit_type commit."
346
+ return
347
+ }
348
+
349
+ set msg {}
350
+ set parent {}
351
+ set parent_count 0
352
+ if {[catch {
353
+ set fd [open "| git cat-file commit $HEAD" r]
354
+ while {[gets $fd line] > 0} {
355
+ if {[string match {parent *} $line]} {
356
+ set parent [string range $line 7 end]
357
+ incr parent_count
358
+ }
359
+ }
360
+ set msg [string trim [read $fd]]
361
+ close $fd
362
+ } err]} {
363
+ error_popup "Error loading commit data for amend:\n$err"
364
+ return
365
+ }
366
+
367
+ if {$parent_count == 0} {
368
+ set commit_type amend
369
+ set HEAD {}
370
+ set PARENT {}
371
+ update_status
372
+ } elseif {$parent_count == 1} {
373
+ set commit_type amend
374
+ set PARENT $parent
375
+ $ui_comm delete 0.0 end
376
+ $ui_comm insert end $msg
377
+ $ui_comm edit modified false
378
+ update_status
379
+ } else {
380
+ error_popup {You can't amend a merge commit.}
381
+ return
382
+ }
383
+}
384
+
385
proc commit_tree {} {
386
global tcl_platform HEAD gitdir commit_type file_states
387
global commit_active ui_status_value
@@ -330,7 +392,10 @@ proc commit_tree {} {
392
# -- Our in memory state should match the repository.
393
#
394
repository_state curHEAD cur_type
333
- if {$commit_type != $cur_type || $HEAD != $curHEAD} {
395
+ if {$commit_type == {amend}
396
+ && $cur_type == {normal}
397
+ && $curHEAD == $HEAD} {
398
+ } elseif {$commit_type != $cur_type || $HEAD != $curHEAD} {
399
error_popup {Last scanned state does not match repository state.
400
401
Its highly likely that another Git program modified the
@@ -397,7 +462,7 @@ A good commit message has the following format:
462
# -- Ask the pre-commit hook for the go-ahead.
463
#
464
set pchook [file join $gitdir hooks pre-commit]
400
- if {$tcl_platform(platform) == {windows} && [file exists $pchook]} {
465
+ if {$tcl_platform(platform) == {windows} && [file isfile $pchook]} {
466
set pchook [list sh -c \
467
"if test -x \"$pchook\"; then exec \"$pchook\"; fi"]
468
} elseif {[file executable $pchook]} {
@@ -422,8 +487,8 @@ A good commit message has the following format:
487
}
488
489
proc commit_stage2 {fd_wt curHEAD msg} {
425
- global single_commit gitdir HEAD commit_type
426
- global commit_active ui_status_value comm_ui
490
+ global single_commit gitdir PARENT commit_type
491
+ global commit_active ui_status_value ui_comm
492
493
gets $fd_wt tree_id
494
close $fd_wt
@@ -439,8 +504,8 @@ proc commit_stage2 {fd_wt curHEAD msg} {
504
# -- Create the commit.
505
#
506
set cmd [list git commit-tree $tree_id]
442
- if {$commit_type != {initial}} {
443
- lappend cmd -p $HEAD
507
+ if {$PARENT != {}} {
508
+ lappend cmd -p $PARENT
509
}
510
if {$commit_type == {merge}} {
511
if {[catch {
@@ -457,7 +522,7 @@ proc commit_stage2 {fd_wt curHEAD msg} {
522
return
523
}
524
}
460
- if {$commit_type == {initial}} {
525
+ if {$PARENT == {}} {
526
# git commit-tree writes to stderr during initial commit.
527
lappend cmd 2>/dev/null
528
}
@@ -482,9 +547,7 @@ proc commit_stage2 {fd_wt curHEAD msg} {
547
} else {
548
append reflogm {: } $msg
549
}
485
- set cmd [list git update-ref \
486
- -m $reflogm \
487
- HEAD $cmt_id $curHEAD]
550
+ set cmd [list git update-ref -m $reflogm HEAD $cmt_id $curHEAD]
551
if {[catch {eval exec $cmd} err]} {
552
error_popup "update-ref failed:\n$err"
553
set commit_active 0
@@ -506,15 +569,15 @@ proc commit_stage2 {fd_wt curHEAD msg} {
569
catch {exec git rerere}
570
}
571
509
- $comm_ui delete 0.0 end
510
- $comm_ui edit modified false
572
+ $ui_comm delete 0.0 end
573
+ $ui_comm edit modified false
574
575
if {$single_commit} do_quit
576
577
+ set commit_type {}
578
set commit_active 0
515
- set ui_status_value "Changes committed as $cmt_id."
579
unlock_index
517
- update_status
580
+ update_status "Changes committed as $cmt_id."
581
}
582
583
######################################################################
@@ -877,7 +940,7 @@ proc do_gitk {} {
940
global tcl_platform ui_status_value starting_gitk_msg
941
942
set ui_status_value $starting_gitk_msg
880
- after 5000 {
943
+ after 10000 {
944
if {$ui_status_value == $starting_gitk_msg} {
945
set ui_status_value {Ready.}
946
}
@@ -953,6 +1016,10 @@ proc do_signoff {} {
1016
}
1017
}
1018
1019
+proc do_amend_last {} {
1020
+ load_last_commit
1021
+}
1022
+
1023
proc do_commit {} {
1024
commit_tree
1025
}
@@ -1027,6 +1094,11 @@ menu .mbar.commit
1094
-font $mainfont
1095
lappend disable_on_lock \
1096
[list .mbar.commit entryconf [.mbar.commit index last] -state]
1097
+.mbar.commit add command -label {Amend Last Commit} \
1098
+ -command do_amend_last \
1099
+ -font $mainfont
1100
+lappend disable_on_lock \
1101
+ [list .mbar.commit entryconf [.mbar.commit index last] -state]
1102
.mbar.commit add command -label {Check-in All Files} \
1103
-command do_checkin_all \
1104
-accelerator $M1T-U \
@@ -1145,7 +1217,7 @@ $ui_diff tag conf dni -foreground "#a000a0"
1217
$ui_diff tag conf bold -font [concat $difffont bold]
1218
1219
# -- Commit Area
1148
-frame .vpane.commarea -height 150
1220
+frame .vpane.commarea -height 170
1221
.vpane add .vpane.commarea -stick nsew
1222
1223
# -- Commit Area Buttons
@@ -1163,6 +1235,12 @@ button .vpane.commarea.buttons.rescan -text {Rescan} \
1235
pack .vpane.commarea.buttons.rescan -side top -fill x
1236
lappend disable_on_lock {.vpane.commarea.buttons.rescan conf -state}
1237
1238
+button .vpane.commarea.buttons.amend -text {Amend Last} \
1239
+ -command do_amend_last \
1240
+ -font $mainfont
1241
+pack .vpane.commarea.buttons.amend -side top -fill x
1242
+lappend disable_on_lock {.vpane.commarea.buttons.amend conf -state}
1243
+
1244
button .vpane.commarea.buttons.ciall -text {Check-in All} \
1245
-command do_checkin_all \
1246
-font $mainfont