f8d92b320e2ec6ed50b235a429e9e4dee785bc28
   1# git-gui branch merge support
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4class merge {
   5
   6field w         ; # top level window
   7field w_rev     ; # mega-widget to pick the revision to merge
   8
   9method _can_merge {} {
  10        global HEAD commit_type file_states
  11
  12        if {[string match amend* $commit_type]} {
  13                info_popup {Cannot merge while amending.
  14
  15You must finish amending this commit before starting any type of merge.
  16}
  17                return 0
  18        }
  19
  20        if {[committer_ident] eq {}} {return 0}
  21        if {![lock_index merge]} {return 0}
  22
  23        # -- Our in memory state should match the repository.
  24        #
  25        repository_state curType curHEAD curMERGE_HEAD
  26        if {$commit_type ne $curType || $HEAD ne $curHEAD} {
  27                info_popup {Last scanned state does not match repository state.
  28
  29Another Git program has modified this repository since the last scan.  A rescan must be performed before a merge can be performed.
  30
  31The rescan will be automatically started now.
  32}
  33                unlock_index
  34                rescan ui_ready
  35                return 0
  36        }
  37
  38        foreach path [array names file_states] {
  39                switch -glob -- [lindex $file_states($path) 0] {
  40                _O {
  41                        continue; # and pray it works!
  42                }
  43                U? {
  44                        error_popup "You are in the middle of a conflicted merge.
  45
  46File [short_path $path] has merge conflicts.
  47
  48You must resolve them, add the file, and commit to complete the current merge.  Only then can you begin another merge.
  49"
  50                        unlock_index
  51                        return 0
  52                }
  53                ?? {
  54                        error_popup "You are in the middle of a change.
  55
  56File [short_path $path] is modified.
  57
  58You should complete the current commit before starting a merge.  Doing so will help you abort a failed merge, should the need arise.
  59"
  60                        unlock_index
  61                        return 0
  62                }
  63                }
  64        }
  65
  66        return 1
  67}
  68
  69method _rev {} {
  70        if {[catch {$w_rev commit_or_die}]} {
  71                return {}
  72        }
  73        return [$w_rev get]
  74}
  75
  76method _visualize {} {
  77        set rev [_rev $this]
  78        if {$rev ne {}} {
  79                do_gitk [list $rev --not HEAD]
  80        }
  81}
  82
  83method _start {} {
  84        global HEAD current_branch
  85
  86        set name [_rev $this]
  87        if {$name eq {}} {
  88                return
  89        }
  90
  91        set cmd [list git merge $name]
  92        set msg "Merging $current_branch and $name"
  93        ui_status "$msg..."
  94        set cons [console::new "Merge" $cmd]
  95        console::exec $cons $cmd [cb _finish $cons]
  96
  97        wm protocol $w WM_DELETE_WINDOW {}
  98        destroy $w
  99}
 100
 101method _finish {cons ok} {
 102        console::done $cons $ok
 103        if {$ok} {
 104                set msg {Merge completed successfully.}
 105        } else {
 106                set msg {Merge failed.  Conflict resolution is required.}
 107        }
 108        unlock_index
 109        rescan [list ui_status $msg]
 110        delete_this
 111}
 112
 113constructor dialog {} {
 114        global current_branch
 115        global M1B
 116
 117        if {![_can_merge $this]} {
 118                delete_this
 119                return
 120        }
 121
 122        make_toplevel top w
 123        wm title $top "[appname] ([reponame]): Merge"
 124        if {$top ne {.}} {
 125                wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
 126        }
 127
 128        set _visualize [cb _visualize]
 129        set _start [cb _start]
 130
 131        label $w.header \
 132                -text "Merge Into $current_branch" \
 133                -font font_uibold
 134        pack $w.header -side top -fill x
 135
 136        frame $w.buttons
 137        button $w.buttons.visualize -text Visualize -command $_visualize
 138        pack $w.buttons.visualize -side left
 139        button $w.buttons.create -text Merge -command $_start
 140        pack $w.buttons.create -side right
 141        button $w.buttons.cancel \
 142                -text {Cancel} \
 143                -command [cb _cancel]
 144        pack $w.buttons.cancel -side right -padx 5
 145        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
 146
 147        set w_rev [::choose_rev::new_unmerged $w.rev {Revision To Merge}]
 148        pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
 149
 150        bind $w <$M1B-Key-Return> $_start
 151        bind $w <Key-Return> $_start
 152        bind $w <Visibility> [cb _visible]
 153        bind $w <Key-Escape> [cb _cancel]
 154        wm protocol $w WM_DELETE_WINDOW [cb _cancel]
 155        tkwait window $w
 156}
 157
 158method _visible {} {
 159        grab $w
 160        if {[is_config_true gui.matchtrackingbranch]} {
 161                $w_rev pick_tracking_branch
 162        }
 163        $w_rev focus_filter
 164}
 165
 166method _cancel {} {
 167        wm protocol $w WM_DELETE_WINDOW {}
 168        unlock_index
 169        destroy $w
 170        delete_this
 171}
 172
 173}
 174
 175namespace eval merge {
 176
 177proc reset_hard {} {
 178        global HEAD commit_type file_states
 179
 180        if {[string match amend* $commit_type]} {
 181                info_popup {Cannot abort while amending.
 182
 183You must finish amending this commit.
 184}
 185                return
 186        }
 187
 188        if {![lock_index abort]} return
 189
 190        if {[string match *merge* $commit_type]} {
 191                set op merge
 192        } else {
 193                set op commit
 194        }
 195
 196        if {[ask_popup "Abort $op?
 197
 198Aborting the current $op will cause *ALL* uncommitted changes to be lost.
 199
 200Continue with aborting the current $op?"] eq {yes}} {
 201                set fd [git_read read-tree --reset -u HEAD]
 202                fconfigure $fd -blocking 0 -translation binary
 203                fileevent $fd readable [namespace code [list _reset_wait $fd]]
 204                ui_status {Aborting... please wait...}
 205        } else {
 206                unlock_index
 207        }
 208}
 209
 210proc _reset_wait {fd} {
 211        global ui_comm
 212
 213        read $fd
 214        if {[eof $fd]} {
 215                close $fd
 216                unlock_index
 217
 218                $ui_comm delete 0.0 end
 219                $ui_comm edit modified false
 220
 221                catch {file delete [gitdir MERGE_HEAD]}
 222                catch {file delete [gitdir rr-cache MERGE_RR]}
 223                catch {file delete [gitdir SQUASH_MSG]}
 224                catch {file delete [gitdir MERGE_MSG]}
 225                catch {file delete [gitdir GITGUI_MSG]}
 226
 227                rescan {ui_status {Abort completed.  Ready.}}
 228        }
 229}
 230
 231}