8a07660e156ae3c3f4b9b05455064d30405ea193
   1# git-gui branch merge support
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4namespace eval merge {
   5
   6proc _can_merge {} {
   7        global HEAD commit_type file_states
   8
   9        if {[string match amend* $commit_type]} {
  10                info_popup {Cannot merge while amending.
  11
  12You must finish amending this commit before starting any type of merge.
  13}
  14                return 0
  15        }
  16
  17        if {[committer_ident] eq {}} {return 0}
  18        if {![lock_index merge]} {return 0}
  19
  20        # -- Our in memory state should match the repository.
  21        #
  22        repository_state curType curHEAD curMERGE_HEAD
  23        if {$commit_type ne $curType || $HEAD ne $curHEAD} {
  24                info_popup {Last scanned state does not match repository state.
  25
  26Another Git program has modified this repository since the last scan.  A rescan must be performed before a merge can be performed.
  27
  28The rescan will be automatically started now.
  29}
  30                unlock_index
  31                rescan {set ui_status_value {Ready.}}
  32                return 0
  33        }
  34
  35        foreach path [array names file_states] {
  36                switch -glob -- [lindex $file_states($path) 0] {
  37                _O {
  38                        continue; # and pray it works!
  39                }
  40                U? {
  41                        error_popup "You are in the middle of a conflicted merge.
  42
  43File [short_path $path] has merge conflicts.
  44
  45You must resolve them, add the file, and commit to complete the current merge.  Only then can you begin another merge.
  46"
  47                        unlock_index
  48                        return 0
  49                }
  50                ?? {
  51                        error_popup "You are in the middle of a change.
  52
  53File [short_path $path] is modified.
  54
  55You should complete the current commit before starting a merge.  Doing so will help you abort a failed merge, should the need arise.
  56"
  57                        unlock_index
  58                        return 0
  59                }
  60                }
  61        }
  62
  63        return 1
  64}
  65
  66proc _visualize {w} {
  67        set revs {}
  68        foreach i [$w.source.l curselection] {
  69                lappend revs [$w.source.l get $i]
  70        }
  71        if {$revs eq {}} return
  72        lappend revs --not HEAD
  73        do_gitk $revs
  74}
  75
  76proc _start {w} {
  77        global HEAD ui_status_value current_branch
  78
  79        set cmd [list git merge]
  80        set names {}
  81        set revcnt 0
  82        foreach i [$w.source.l curselection] {
  83                set b [$w.source.l get $i]
  84                lappend cmd $b
  85                lappend names $b
  86                incr revcnt
  87        }
  88
  89        if {$revcnt == 0} {
  90                return
  91        } elseif {$revcnt == 1} {
  92                set unit branch
  93        } elseif {$revcnt <= 15} {
  94                set unit branches
  95
  96                if {[tk_dialog \
  97                $w.confirm_octopus \
  98                [wm title $w] \
  99                "Use octopus merge strategy?
 100
 101You are merging $revcnt branches at once.  This requires using the octopus merge driver, which may not succeed if there are file-level conflicts.
 102" \
 103                question \
 104                0 \
 105                {Cancel} \
 106                {Use octopus} \
 107                ] != 1} return
 108        } else {
 109                tk_messageBox \
 110                        -icon error \
 111                        -type ok \
 112                        -title [wm title $w] \
 113                        -parent $w \
 114                        -message "Too many branches selected.
 115
 116You have requested to merge $revcnt branches in an octopus merge.  This exceeds Git's internal limit of 15 branches per merge.
 117
 118Please select fewer branches.  To merge more than 15 branches, merge the branches in batches.
 119"
 120                return
 121        }
 122
 123        set msg "Merging $current_branch, [join $names {, }]"
 124        set ui_status_value "$msg..."
 125        set cons [console::new "Merge" $msg]
 126        console::exec $cons $cmd [namespace code [list _finish $revcnt]]
 127        bind $w <Destroy> {}
 128        destroy $w
 129}
 130
 131proc _finish {revcnt w ok} {
 132        console::done $w $ok
 133        if {$ok} {
 134                set msg {Merge completed successfully.}
 135        } else {
 136                if {$revcnt != 1} {
 137                        info_popup "Octopus merge failed.
 138
 139Your merge of $revcnt branches has failed.
 140
 141There are file-level conflicts between the branches which must be resolved manually.
 142
 143The working directory will now be reset.
 144
 145You can attempt this merge again by merging only one branch at a time." $w
 146
 147                        set fd [open "| git read-tree --reset -u HEAD" r]
 148                        fconfigure $fd -blocking 0 -translation binary
 149                        fileevent $fd readable \
 150                                [namespace code [list _reset_wait $fd]]
 151                        set ui_status_value {Aborting... please wait...}
 152                        return
 153                }
 154
 155                set msg {Merge failed.  Conflict resolution is required.}
 156        }
 157        unlock_index
 158        rescan [list set ui_status_value $msg]
 159}
 160
 161proc dialog {} {
 162        global current_branch
 163
 164        if {![_can_merge]} return
 165
 166        set w .merge_setup
 167        toplevel $w
 168        wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
 169
 170        label $w.header \
 171                -text "Merge Into $current_branch" \
 172                -font font_uibold
 173        pack $w.header -side top -fill x
 174
 175        frame $w.buttons
 176        button $w.buttons.visualize -text Visualize \
 177                -command [namespace code [list _visualize $w]]
 178        pack $w.buttons.visualize -side left
 179        button $w.buttons.create -text Merge \
 180                -command [namespace code [list _start $w]]
 181        pack $w.buttons.create -side right
 182        button $w.buttons.cancel -text {Cancel} \
 183                -command [list destroy $w]
 184        pack $w.buttons.cancel -side right -padx 5
 185        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
 186
 187        labelframe $w.source -text {Source Branches}
 188        listbox $w.source.l \
 189                -height 10 \
 190                -width 70 \
 191                -selectmode extended \
 192                -yscrollcommand [list $w.source.sby set]
 193        scrollbar $w.source.sby -command [list $w.source.l yview]
 194        pack $w.source.sby -side right -fill y
 195        pack $w.source.l -side left -fill both -expand 1
 196        pack $w.source -fill both -expand 1 -pady 5 -padx 5
 197
 198        set fmt {list %(objectname) %(*objectname) %(refname) %(subject)}
 199        set cmd [list git for-each-ref --tcl --format=$fmt]
 200        lappend cmd refs/heads
 201        lappend cmd refs/remotes
 202        lappend cmd refs/tags
 203        set fr_fd [open "| $cmd" r]
 204        fconfigure $fr_fd -translation binary
 205        while {[gets $fr_fd line] > 0} {
 206                set line [eval $line]
 207                set ref [lindex $line 2]
 208                regsub ^refs/(heads|remotes|tags)/ $ref {} ref
 209                set subj($ref) [lindex $line 3]
 210                lappend sha1([lindex $line 0]) $ref
 211                if {[lindex $line 1] ne {}} {
 212                        lappend sha1([lindex $line 1]) $ref
 213                }
 214        }
 215        close $fr_fd
 216
 217        set to_show {}
 218        set fr_fd [open "| git rev-list --all --not HEAD"]
 219        while {[gets $fr_fd line] > 0} {
 220                if {[catch {set ref $sha1($line)}]} continue
 221                foreach n $ref {
 222                        lappend to_show $n
 223                }
 224        }
 225        close $fr_fd
 226
 227        foreach ref [lsort -unique $to_show] {
 228                $w.source.l insert end $ref
 229        }
 230
 231        bind $w <Visibility> "grab $w"
 232        bind $w <Key-Escape> "unlock_index;destroy $w"
 233        bind $w <Destroy> unlock_index
 234        wm title $w "[appname] ([reponame]): Merge"
 235        tkwait window $w
 236}
 237
 238proc reset_hard {} {
 239        global HEAD commit_type file_states
 240
 241        if {[string match amend* $commit_type]} {
 242                info_popup {Cannot abort while amending.
 243
 244You must finish amending this commit.
 245}
 246                return
 247        }
 248
 249        if {![lock_index abort]} return
 250
 251        if {[string match *merge* $commit_type]} {
 252                set op merge
 253        } else {
 254                set op commit
 255        }
 256
 257        if {[ask_popup "Abort $op?
 258
 259Aborting the current $op will cause *ALL* uncommitted changes to be lost.
 260
 261Continue with aborting the current $op?"] eq {yes}} {
 262                set fd [open "| git read-tree --reset -u HEAD" r]
 263                fconfigure $fd -blocking 0 -translation binary
 264                fileevent $fd readable [namespace code [list _reset_wait $fd]]
 265                set ui_status_value {Aborting... please wait...}
 266        } else {
 267                unlock_index
 268        }
 269}
 270
 271proc _reset_wait {fd} {
 272        global ui_comm
 273
 274        read $fd
 275        if {[eof $fd]} {
 276                close $fd
 277                unlock_index
 278
 279                $ui_comm delete 0.0 end
 280                $ui_comm edit modified false
 281
 282                catch {file delete [gitdir MERGE_HEAD]}
 283                catch {file delete [gitdir rr-cache MERGE_RR]}
 284                catch {file delete [gitdir SQUASH_MSG]}
 285                catch {file delete [gitdir MERGE_MSG]}
 286                catch {file delete [gitdir GITGUI_MSG]}
 287
 288                rescan {set ui_status_value {Abort completed.  Ready.}}
 289        }
 290}
 291
 292}