git-gui / lib / merge.tclon commit Merge branch 'maint-1.5.1' into maint (baf5597)
   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 _refs {w list} {
  67        set r {}
  68        foreach i [$w.source.l curselection] {
  69                lappend r [lindex [lindex $list $i] 0]
  70        }
  71        return $r
  72}
  73
  74proc _visualize {w list} {
  75        set revs [_refs $w $list]
  76        if {$revs eq {}} return
  77        lappend revs --not HEAD
  78        do_gitk $revs
  79}
  80
  81proc _start {w list} {
  82        global HEAD ui_status_value current_branch
  83
  84        set cmd [list git merge]
  85        set names [_refs $w $list]
  86        set revcnt [llength $names]
  87        append cmd { } $names
  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 \
 127                [namespace code [list _finish $revcnt $cons]]
 128        bind $w <Destroy> {}
 129        destroy $w
 130}
 131
 132proc _finish {revcnt w ok} {
 133        console::done $w $ok
 134        if {$ok} {
 135                set msg {Merge completed successfully.}
 136        } else {
 137                if {$revcnt != 1} {
 138                        info_popup "Octopus merge failed.
 139
 140Your merge of $revcnt branches has failed.
 141
 142There are file-level conflicts between the branches which must be resolved manually.
 143
 144The working directory will now be reset.
 145
 146You can attempt this merge again by merging only one branch at a time." $w
 147
 148                        set fd [open "| git read-tree --reset -u HEAD" r]
 149                        fconfigure $fd -blocking 0 -translation binary
 150                        fileevent $fd readable \
 151                                [namespace code [list _reset_wait $fd]]
 152                        set ui_status_value {Aborting... please wait...}
 153                        return
 154                }
 155
 156                set msg {Merge failed.  Conflict resolution is required.}
 157        }
 158        unlock_index
 159        rescan [list set ui_status_value $msg]
 160}
 161
 162proc dialog {} {
 163        global current_branch
 164        global M1B
 165
 166        if {![_can_merge]} return
 167
 168        set fmt {list %(objectname) %(*objectname) %(refname) %(subject)}
 169        set cmd [list git for-each-ref --tcl --format=$fmt]
 170        lappend cmd refs/heads
 171        lappend cmd refs/remotes
 172        lappend cmd refs/tags
 173        set fr_fd [open "| $cmd" r]
 174        fconfigure $fr_fd -translation binary
 175        while {[gets $fr_fd line] > 0} {
 176                set line [eval $line]
 177                set ref [lindex $line 2]
 178                regsub ^refs/(heads|remotes|tags)/ $ref {} ref
 179                set subj($ref) [lindex $line 3]
 180                lappend sha1([lindex $line 0]) $ref
 181                if {[lindex $line 1] ne {}} {
 182                        lappend sha1([lindex $line 1]) $ref
 183                }
 184        }
 185        close $fr_fd
 186
 187        set to_show {}
 188        set fr_fd [open "| git rev-list --all --not HEAD"]
 189        while {[gets $fr_fd line] > 0} {
 190                if {[catch {set ref $sha1($line)}]} continue
 191                foreach n $ref {
 192                        lappend to_show [list $n $line]
 193                }
 194        }
 195        close $fr_fd
 196        set to_show [lsort -unique $to_show]
 197
 198        set w .merge_setup
 199        toplevel $w
 200        wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
 201
 202        set _visualize [namespace code [list _visualize $w $to_show]]
 203        set _start [namespace code [list _start $w $to_show]]
 204
 205        label $w.header \
 206                -text "Merge Into $current_branch" \
 207                -font font_uibold
 208        pack $w.header -side top -fill x
 209
 210        frame $w.buttons
 211        button $w.buttons.visualize -text Visualize -command $_visualize
 212        pack $w.buttons.visualize -side left
 213        button $w.buttons.create -text Merge -command $_start
 214        pack $w.buttons.create -side right
 215        button $w.buttons.cancel -text {Cancel} -command [list destroy $w]
 216        pack $w.buttons.cancel -side right -padx 5
 217        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
 218
 219        labelframe $w.source -text {Source Branches}
 220        listbox $w.source.l \
 221                -height 10 \
 222                -width 70 \
 223                -font font_diff \
 224                -selectmode extended \
 225                -yscrollcommand [list $w.source.sby set]
 226        scrollbar $w.source.sby -command [list $w.source.l yview]
 227        pack $w.source.sby -side right -fill y
 228        pack $w.source.l -side left -fill both -expand 1
 229        pack $w.source -fill both -expand 1 -pady 5 -padx 5
 230
 231        foreach ref $to_show {
 232                set n [lindex $ref 0]
 233                if {[string length $n] > 20} {
 234                        set n "[string range $n 0 16]..."
 235                }
 236                $w.source.l insert end [format {%s %-20s %s} \
 237                        [string range [lindex $ref 1] 0 5] \
 238                        $n \
 239                        $subj([lindex $ref 0])]
 240        }
 241
 242        bind $w.source.l <Key-K> [list event generate %W <Shift-Key-Up>]
 243        bind $w.source.l <Key-J> [list event generate %W <Shift-Key-Down>]
 244        bind $w.source.l <Key-k> [list event generate %W <Key-Up>]
 245        bind $w.source.l <Key-j> [list event generate %W <Key-Down>]
 246        bind $w.source.l <Key-h> [list event generate %W <Key-Left>]
 247        bind $w.source.l <Key-l> [list event generate %W <Key-Right>]
 248        bind $w.source.l <Key-v> $_visualize
 249
 250        bind $w <$M1B-Key-Return> $_start
 251        bind $w <Visibility> "grab $w; focus $w.source.l"
 252        bind $w <Key-Escape> "unlock_index;destroy $w"
 253        bind $w <Destroy> unlock_index
 254        wm title $w "[appname] ([reponame]): Merge"
 255        tkwait window $w
 256}
 257
 258proc reset_hard {} {
 259        global HEAD commit_type file_states
 260
 261        if {[string match amend* $commit_type]} {
 262                info_popup {Cannot abort while amending.
 263
 264You must finish amending this commit.
 265}
 266                return
 267        }
 268
 269        if {![lock_index abort]} return
 270
 271        if {[string match *merge* $commit_type]} {
 272                set op merge
 273        } else {
 274                set op commit
 275        }
 276
 277        if {[ask_popup "Abort $op?
 278
 279Aborting the current $op will cause *ALL* uncommitted changes to be lost.
 280
 281Continue with aborting the current $op?"] eq {yes}} {
 282                set fd [open "| git read-tree --reset -u HEAD" r]
 283                fconfigure $fd -blocking 0 -translation binary
 284                fileevent $fd readable [namespace code [list _reset_wait $fd]]
 285                set ui_status_value {Aborting... please wait...}
 286        } else {
 287                unlock_index
 288        }
 289}
 290
 291proc _reset_wait {fd} {
 292        global ui_comm
 293
 294        read $fd
 295        if {[eof $fd]} {
 296                close $fd
 297                unlock_index
 298
 299                $ui_comm delete 0.0 end
 300                $ui_comm edit modified false
 301
 302                catch {file delete [gitdir MERGE_HEAD]}
 303                catch {file delete [gitdir rr-cache MERGE_RR]}
 304                catch {file delete [gitdir SQUASH_MSG]}
 305                catch {file delete [gitdir MERGE_MSG]}
 306                catch {file delete [gitdir GITGUI_MSG]}
 307
 308                rescan {set ui_status_value {Abort completed.  Ready.}}
 309        }
 310}
 311
 312}