lib / commit.tclon commit git-gui: Maintain remote and source ref for tracking branches (79a060e)
   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 {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 {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 [open "| git cat-file commit $curHEAD" r]
  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 [encoding convertfrom $enc [read $fd]]
  41                        set msg [string trim $msg]
  42                        close $fd
  43                } err]} {
  44                error_popup "Error loading commit data for amend:\n\n$err"
  45                return
  46        }
  47
  48        set HEAD $curHEAD
  49        set PARENT $parents
  50        set MERGE_HEAD [list]
  51        switch -- [llength $parents] {
  52        0       {set commit_type amend-initial}
  53        1       {set commit_type amend}
  54        default {set commit_type amend-merge}
  55        }
  56
  57        $ui_comm delete 0.0 end
  58        $ui_comm insert end $msg
  59        $ui_comm edit reset
  60        $ui_comm edit modified false
  61        rescan {set ui_status_value {Ready.}}
  62}
  63
  64set GIT_COMMITTER_IDENT {}
  65
  66proc committer_ident {} {
  67        global GIT_COMMITTER_IDENT
  68
  69        if {$GIT_COMMITTER_IDENT eq {}} {
  70                if {[catch {set me [git var GIT_COMMITTER_IDENT]} err]} {
  71                        error_popup "Unable to obtain your identity:\n\n$err"
  72                        return {}
  73                }
  74                if {![regexp {^(.*) [0-9]+ [-+0-9]+$} \
  75                        $me me GIT_COMMITTER_IDENT]} {
  76                        error_popup "Invalid GIT_COMMITTER_IDENT:\n\n$me"
  77                        return {}
  78                }
  79        }
  80
  81        return $GIT_COMMITTER_IDENT
  82}
  83
  84proc do_signoff {} {
  85        global ui_comm
  86
  87        set me [committer_ident]
  88        if {$me eq {}} return
  89
  90        set sob "Signed-off-by: $me"
  91        set last [$ui_comm get {end -1c linestart} {end -1c}]
  92        if {$last ne $sob} {
  93                $ui_comm edit separator
  94                if {$last ne {}
  95                        && ![regexp {^[A-Z][A-Za-z]*-[A-Za-z-]+: *} $last]} {
  96                        $ui_comm insert end "\n"
  97                }
  98                $ui_comm insert end "\n$sob"
  99                $ui_comm edit separator
 100                $ui_comm see end
 101        }
 102}
 103
 104proc create_new_commit {} {
 105        global commit_type ui_comm
 106
 107        set commit_type normal
 108        $ui_comm delete 0.0 end
 109        $ui_comm edit reset
 110        $ui_comm edit modified false
 111        rescan {set ui_status_value {Ready.}}
 112}
 113
 114proc commit_tree {} {
 115        global HEAD commit_type file_states ui_comm repo_config
 116        global ui_status_value pch_error
 117
 118        if {[committer_ident] eq {}} return
 119        if {![lock_index update]} return
 120
 121        # -- Our in memory state should match the repository.
 122        #
 123        repository_state curType curHEAD curMERGE_HEAD
 124        if {[string match amend* $commit_type]
 125                && $curType eq {normal}
 126                && $curHEAD eq $HEAD} {
 127        } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
 128                info_popup {Last scanned state does not match repository state.
 129
 130Another Git program has modified this repository since the last scan.  A rescan must be performed before another commit can be created.
 131
 132The rescan will be automatically started now.
 133}
 134                unlock_index
 135                rescan {set ui_status_value {Ready.}}
 136                return
 137        }
 138
 139        # -- At least one file should differ in the index.
 140        #
 141        set files_ready 0
 142        foreach path [array names file_states] {
 143                switch -glob -- [lindex $file_states($path) 0] {
 144                _? {continue}
 145                A? -
 146                D? -
 147                M? {set files_ready 1}
 148                U? {
 149                        error_popup "Unmerged files cannot be committed.
 150
 151File [short_path $path] has merge conflicts.  You must resolve them and add the file before committing.
 152"
 153                        unlock_index
 154                        return
 155                }
 156                default {
 157                        error_popup "Unknown file state [lindex $s 0] detected.
 158
 159File [short_path $path] cannot be committed by this program.
 160"
 161                }
 162                }
 163        }
 164        if {!$files_ready && ![string match *merge $curType]} {
 165                info_popup {No changes to commit.
 166
 167You must add at least 1 file before you can commit.
 168}
 169                unlock_index
 170                return
 171        }
 172
 173        # -- A message is required.
 174        #
 175        set msg [string trim [$ui_comm get 1.0 end]]
 176        regsub -all -line {[ \t\r]+$} $msg {} msg
 177        if {$msg eq {}} {
 178                error_popup {Please supply a commit message.
 179
 180A good commit message has the following format:
 181
 182- First line: Describe in one sentance what you did.
 183- Second line: Blank
 184- Remaining lines: Describe why this change is good.
 185}
 186                unlock_index
 187                return
 188        }
 189
 190        # -- Run the pre-commit hook.
 191        #
 192        set pchook [gitdir hooks pre-commit]
 193
 194        # On Cygwin [file executable] might lie so we need to ask
 195        # the shell if the hook is executable.  Yes that's annoying.
 196        #
 197        if {[is_Cygwin] && [file isfile $pchook]} {
 198                set pchook [list sh -c [concat \
 199                        "if test -x \"$pchook\";" \
 200                        "then exec \"$pchook\" 2>&1;" \
 201                        "fi"]]
 202        } elseif {[file executable $pchook]} {
 203                set pchook [list $pchook |& cat]
 204        } else {
 205                commit_writetree $curHEAD $msg
 206                return
 207        }
 208
 209        set ui_status_value {Calling pre-commit hook...}
 210        set pch_error {}
 211        set fd_ph [open "| $pchook" r]
 212        fconfigure $fd_ph -blocking 0 -translation binary
 213        fileevent $fd_ph readable \
 214                [list commit_prehook_wait $fd_ph $curHEAD $msg]
 215}
 216
 217proc commit_prehook_wait {fd_ph curHEAD msg} {
 218        global pch_error ui_status_value
 219
 220        append pch_error [read $fd_ph]
 221        fconfigure $fd_ph -blocking 1
 222        if {[eof $fd_ph]} {
 223                if {[catch {close $fd_ph}]} {
 224                        set ui_status_value {Commit declined by pre-commit hook.}
 225                        hook_failed_popup pre-commit $pch_error
 226                        unlock_index
 227                } else {
 228                        commit_writetree $curHEAD $msg
 229                }
 230                set pch_error {}
 231                return
 232        }
 233        fconfigure $fd_ph -blocking 0
 234}
 235
 236proc commit_writetree {curHEAD msg} {
 237        global ui_status_value
 238
 239        set ui_status_value {Committing changes...}
 240        set fd_wt [open "| git write-tree" r]
 241        fileevent $fd_wt readable \
 242                [list commit_committree $fd_wt $curHEAD $msg]
 243}
 244
 245proc commit_committree {fd_wt curHEAD msg} {
 246        global HEAD PARENT MERGE_HEAD commit_type
 247        global all_heads current_branch
 248        global ui_status_value ui_comm selected_commit_type
 249        global file_states selected_paths rescan_active
 250        global repo_config
 251
 252        gets $fd_wt tree_id
 253        if {$tree_id eq {} || [catch {close $fd_wt} err]} {
 254                error_popup "write-tree failed:\n\n$err"
 255                set ui_status_value {Commit failed.}
 256                unlock_index
 257                return
 258        }
 259
 260        # -- Verify this wasn't an empty change.
 261        #
 262        if {$commit_type eq {normal}} {
 263                set old_tree [git rev-parse "$PARENT^{tree}"]
 264                if {$tree_id eq $old_tree} {
 265                        info_popup {No changes to commit.
 266
 267No files were modified by this commit and it was not a merge commit.
 268
 269A rescan will be automatically started now.
 270}
 271                        unlock_index
 272                        rescan {set ui_status_value {No changes to commit.}}
 273                        return
 274                }
 275        }
 276
 277        # -- Build the message.
 278        #
 279        set msg_p [gitdir COMMIT_EDITMSG]
 280        set msg_wt [open $msg_p w]
 281        if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
 282                set enc utf-8
 283        }
 284        fconfigure $msg_wt -encoding binary -translation binary
 285        puts -nonewline $msg_wt [encoding convertto $enc $msg]
 286        close $msg_wt
 287
 288        # -- Create the commit.
 289        #
 290        set cmd [list commit-tree $tree_id]
 291        foreach p [concat $PARENT $MERGE_HEAD] {
 292                lappend cmd -p $p
 293        }
 294        lappend cmd <$msg_p
 295        if {[catch {set cmt_id [eval git $cmd]} err]} {
 296                error_popup "commit-tree failed:\n\n$err"
 297                set ui_status_value {Commit failed.}
 298                unlock_index
 299                return
 300        }
 301
 302        # -- Update the HEAD ref.
 303        #
 304        set reflogm commit
 305        if {$commit_type ne {normal}} {
 306                append reflogm " ($commit_type)"
 307        }
 308        set i [string first "\n" $msg]
 309        if {$i >= 0} {
 310                set subject [string range $msg 0 [expr {$i - 1}]]
 311        } else {
 312                set subject $msg
 313        }
 314        append reflogm {: } $subject
 315        if {[catch {
 316                        git update-ref -m $reflogm HEAD $cmt_id $curHEAD
 317                } err]} {
 318                error_popup "update-ref failed:\n\n$err"
 319                set ui_status_value {Commit failed.}
 320                unlock_index
 321                return
 322        }
 323
 324        # -- Cleanup after ourselves.
 325        #
 326        catch {file delete $msg_p}
 327        catch {file delete [gitdir MERGE_HEAD]}
 328        catch {file delete [gitdir MERGE_MSG]}
 329        catch {file delete [gitdir SQUASH_MSG]}
 330        catch {file delete [gitdir GITGUI_MSG]}
 331
 332        # -- Let rerere do its thing.
 333        #
 334        if {[get_config rerere.enabled] eq {}} {
 335                set rerere [file isdirectory [gitdir rr-cache]]
 336        } else {
 337                set rerere [is_config_true rerere.enabled]
 338        }
 339        if {$rerere} {
 340                catch {git rerere}
 341        }
 342
 343        # -- Run the post-commit hook.
 344        #
 345        set pchook [gitdir hooks post-commit]
 346        if {[is_Cygwin] && [file isfile $pchook]} {
 347                set pchook [list sh -c [concat \
 348                        "if test -x \"$pchook\";" \
 349                        "then exec \"$pchook\";" \
 350                        "fi"]]
 351        } elseif {![file executable $pchook]} {
 352                set pchook {}
 353        }
 354        if {$pchook ne {}} {
 355                catch {exec $pchook &}
 356        }
 357
 358        $ui_comm delete 0.0 end
 359        $ui_comm edit reset
 360        $ui_comm edit modified false
 361
 362        if {[is_enabled singlecommit]} do_quit
 363
 364        # -- Make sure our current branch exists.
 365        #
 366        if {$commit_type eq {initial}} {
 367                lappend all_heads $current_branch
 368                set all_heads [lsort -unique $all_heads]
 369                populate_branch_menu
 370        }
 371
 372        # -- Update in memory status
 373        #
 374        set selected_commit_type new
 375        set commit_type normal
 376        set HEAD $cmt_id
 377        set PARENT $cmt_id
 378        set MERGE_HEAD [list]
 379
 380        foreach path [array names file_states] {
 381                set s $file_states($path)
 382                set m [lindex $s 0]
 383                switch -glob -- $m {
 384                _O -
 385                _M -
 386                _D {continue}
 387                __ -
 388                A_ -
 389                M_ -
 390                D_ {
 391                        unset file_states($path)
 392                        catch {unset selected_paths($path)}
 393                }
 394                DO {
 395                        set file_states($path) [list _O [lindex $s 1] {} {}]
 396                }
 397                AM -
 398                AD -
 399                MM -
 400                MD {
 401                        set file_states($path) [list \
 402                                _[string index $m 1] \
 403                                [lindex $s 1] \
 404                                [lindex $s 3] \
 405                                {}]
 406                }
 407                }
 408        }
 409
 410        display_all_files
 411        unlock_index
 412        reshow_diff
 413        set ui_status_value \
 414                "Created commit [string range $cmt_id 0 7]: $subject"
 415}