@matheusbsbs / matheus-bagliano / commits / 4539eacd6d
Fork 0

git-gui: Make initial commits work properly.

Apparently I never really tested the logic for making or amending an initial commit, so although most of the code was here in git-gui it didn't quite work as it was intended to. So this is all just bug fixes to make initial commits correctly generate the list of files going into the initial commit, or to show a newly added file's diff, and to amend an initial commit. Because we really want to diff the index against a tree-ish and there is no such tree-ish on an initial commit we create an empty tree through git-mktree and diff against that. This unfortunately creates a dangling tree, which may confuse a new user who uses git-gui to make a new commit and then immediately afterwards runs git fsck-objects to see if their object database is corrupt or not. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Nov 18, 2006 at 02:50 UTC 4539eacd6dde87b4e73c859e946bb0a2c89d71f4
1 file changed +46 -28
git-gui
+46 -28
@@ -205,6 +205,7 @@ set index_lock_type none
205 set HEAD {}
206 set PARENT {}
207 set commit_type {}
208 +set empty_tree {}
209
210 proc lock_index {type} {
211 global index_lock_type disable_on_lock
@@ -240,6 +241,7 @@ proc repository_state {hdvar ctvar} {
241 upvar $hdvar hd $ctvar ct
242
243 if {[catch {set hd [exec git rev-parse --verify HEAD]}]} {
244 + set hd {}
245 set ct initial
246 } elseif {[file exists [file join $gitdir MERGE_HEAD]]} {
247 set ct merge
@@ -248,6 +250,18 @@ proc repository_state {hdvar ctvar} {
250 }
251 }
252
253 +proc PARENT {} {
254 + global PARENT empty_tree
255 +
256 + if {$PARENT ne {}} {
257 + return $PARENT
258 + }
259 + if {$empty_tree eq {}} {
260 + set empty_tree [exec git mktree << {}]
261 + }
262 + return $empty_tree
263 +}
264 +
265 proc rescan {after} {
266 global HEAD PARENT commit_type
267 global ui_index ui_other ui_status_value ui_comm
@@ -257,7 +271,7 @@ proc rescan {after} {
271 if {$rescan_active > 0 || ![lock_index read]} return
272
273 repository_state new_HEAD new_type
260 - if {$commit_type eq {amend}
274 + if {[string match amend* $commit_type]
275 && $new_type eq {normal}
276 && $new_HEAD eq $HEAD} {
277 } else {
@@ -296,10 +310,8 @@ proc rescan {after} {
310 }
311
312 proc rescan_stage2 {fd after} {
299 - global gitdir PARENT commit_type
300 - global ui_index ui_other ui_status_value ui_comm
301 - global rescan_active
302 - global buf_rdi buf_rdf buf_rlo
313 + global gitdir ui_status_value
314 + global rescan_active buf_rdi buf_rdf buf_rlo
315
316 if {$fd ne {}} {
317 read $fd
@@ -320,7 +332,7 @@ proc rescan_stage2 {fd after} {
332
333 set rescan_active 3
334 set ui_status_value {Scanning for modified files ...}
323 - set fd_di [open "| git diff-index --cached -z $PARENT" r]
335 + set fd_di [open "| git diff-index --cached -z [PARENT]" r]
336 set fd_df [open "| git diff-files -z" r]
337 set fd_lo [open $ls_others r]
338
@@ -532,7 +544,7 @@ files list, to prevent possible confusion.
544
545 proc show_diff {path {w {}} {lno {}}} {
546 global file_states file_lists
535 - global PARENT diff_3way diff_active repo_config
547 + global diff_3way diff_active repo_config
548 global ui_diff current_diff ui_status_value
549
550 if {$diff_active || ![lock_index read]} return
@@ -591,7 +603,7 @@ proc show_diff {path {w {}} {lno {}}} {
603 }
604 }
605
594 - lappend cmd $PARENT
606 + lappend cmd [PARENT]
607 lappend cmd --
608 lappend cmd $path
609
@@ -671,7 +683,7 @@ proc read_diff {fd} {
683 proc load_last_commit {} {
684 global HEAD PARENT commit_type ui_comm
685
674 - if {$commit_type eq {amend}} return
686 + if {[string match amend* $commit_type]} return
687 if {$commit_type ne {normal}} {
688 error_popup "Can't amend a $commit_type commit."
689 return
@@ -695,23 +707,24 @@ proc load_last_commit {} {
707 return
708 }
709
710 + if {$parent_count > 1} {
711 + error_popup {Can't amend a merge commit.}
712 + return
713 + }
714 +
715 if {$parent_count == 0} {
699 - set commit_type amend
700 - set HEAD {}
716 + set commit_type amend-initial
717 set PARENT {}
702 - rescan {set ui_status_value {Ready.}}
718 } elseif {$parent_count == 1} {
719 set commit_type amend
720 set PARENT $parent
706 - $ui_comm delete 0.0 end
707 - $ui_comm insert end $msg
708 - $ui_comm edit modified false
709 - $ui_comm edit reset
710 - rescan {set ui_status_value {Ready.}}
711 - } else {
712 - error_popup {You can't amend a merge commit.}
713 - return
721 }
722 +
723 + $ui_comm delete 0.0 end
724 + $ui_comm insert end $msg
725 + $ui_comm edit modified false
726 + $ui_comm edit reset
727 + rescan {set ui_status_value {Ready.}}
728 }
729
730 proc commit_tree {} {
@@ -722,7 +735,7 @@ proc commit_tree {} {
735 # -- Our in memory state should match the repository.
736 #
737 repository_state curHEAD cur_type
725 - if {$commit_type eq {amend}
738 + if {[string match amend* $commit_type]
739 && $cur_type eq {normal}
740 && $curHEAD eq $HEAD} {
741 } elseif {$commit_type ne $cur_type || $HEAD ne $curHEAD} {
@@ -2559,13 +2572,18 @@ label $ui_coml -text {Commit Message:} \
2572 -anchor w \
2573 -justify left \
2574 -font font_ui
2562 -trace add variable commit_type write {uplevel #0 {
2563 - switch -glob $commit_type \
2564 - initial {$ui_coml conf -text {Initial Commit Message:}} \
2565 - amend {$ui_coml conf -text {Amended Commit Message:}} \
2566 - merge {$ui_coml conf -text {Merge Commit Message:}} \
2567 - * {$ui_coml conf -text {Commit Message:}}
2568 -}}
2575 +proc trace_commit_type {varname args} {
2576 + global ui_coml commit_type
2577 + switch -glob -- $commit_type {
2578 + initial {set txt {Initial Commit Message:}}
2579 + amend {set txt {Amended Commit Message:}}
2580 + amend-initial {set txt {Amended Initial Commit Message:}}
2581 + merge {set txt {Merge Commit Message:}}
2582 + * {set txt {Commit Message:}}
2583 + }
2584 + $ui_coml conf -text $txt
2585 +}
2586 +trace add variable commit_type write trace_commit_type
2587 text $ui_comm -background white -borderwidth 1 \
2588 -undo true \
2589 -maxundo 20 \