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