git-gui / lib / merge.tclon commit Merge branch 'da/mergetool-tests' (64bff25)
   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 [mc "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 [mc "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                U? {
  45                        error_popup [mc "You are in the middle of a conflicted merge.
  46
  47File %s has merge conflicts.
  48
  49You must resolve them, stage the file, and commit to complete the current merge.  Only then can you begin another merge.
  50" [short_path $path]]
  51                        unlock_index
  52                        return 0
  53                }
  54                ?? {
  55                        error_popup [mc "You are in the middle of a change.
  56
  57File %s is modified.
  58
  59You should complete the current commit before starting a merge.  Doing so will help you abort a failed merge, should the need arise.
  60" [short_path $path]]
  61                        unlock_index
  62                        return 0
  63                }
  64                }
  65        }
  66
  67        return 1
  68}
  69
  70method _rev {} {
  71        if {[catch {$w_rev commit_or_die}]} {
  72                return {}
  73        }
  74        return [$w_rev get]
  75}
  76
  77method _visualize {} {
  78        set rev [_rev $this]
  79        if {$rev ne {}} {
  80                do_gitk [list $rev --not HEAD]
  81        }
  82}
  83
  84method _start {} {
  85        global HEAD current_branch remote_url
  86        global _last_merged_branch
  87
  88        set name [_rev $this]
  89        if {$name eq {}} {
  90                return
  91        }
  92
  93        set spec [$w_rev get_tracking_branch]
  94        set cmit [$w_rev get_commit]
  95
  96        set fh [open [gitdir FETCH_HEAD] w]
  97        fconfigure $fh -translation lf
  98        if {$spec eq {}} {
  99                set remote .
 100                set branch $name
 101                set stitle $branch
 102        } else {
 103                set remote $remote_url([lindex $spec 1])
 104                if {[regexp {^[^:@]*@[^:]*:/} $remote]} {
 105                        regsub {^[^:@]*@} $remote {} remote
 106                }
 107                set branch [lindex $spec 2]
 108                set stitle [mc "%s of %s" $branch $remote]
 109        }
 110        regsub ^refs/heads/ $branch {} branch
 111        puts $fh "$cmit\t\tbranch '$branch' of $remote"
 112        close $fh
 113        set _last_merged_branch $branch
 114
 115        set cmd [list git]
 116        lappend cmd merge
 117        lappend cmd --strategy=recursive
 118        lappend cmd [git fmt-merge-msg <[gitdir FETCH_HEAD]]
 119        lappend cmd HEAD
 120        lappend cmd $name
 121
 122        ui_status [mc "Merging %s and %s..." $current_branch $stitle]
 123        set cons [console::new [mc "Merge"] "merge $stitle"]
 124        console::exec $cons $cmd [cb _finish $cons]
 125
 126        wm protocol $w WM_DELETE_WINDOW {}
 127        destroy $w
 128}
 129
 130method _finish {cons ok} {
 131        console::done $cons $ok
 132        if {$ok} {
 133                set msg [mc "Merge completed successfully."]
 134        } else {
 135                set msg [mc "Merge failed.  Conflict resolution is required."]
 136        }
 137        unlock_index
 138        rescan [list ui_status $msg]
 139        delete_this
 140}
 141
 142constructor dialog {} {
 143        global current_branch
 144        global M1B use_ttk NS
 145
 146        if {![_can_merge $this]} {
 147                delete_this
 148                return
 149        }
 150
 151        make_dialog top w
 152        wm title $top [append "[appname] ([reponame]): " [mc "Merge"]]
 153        if {$top ne {.}} {
 154                wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
 155        }
 156
 157        set _start [cb _start]
 158
 159        ${NS}::label $w.header \
 160                -text [mc "Merge Into %s" $current_branch] \
 161                -font font_uibold
 162        pack $w.header -side top -fill x
 163
 164        ${NS}::frame $w.buttons
 165        ${NS}::button $w.buttons.visualize \
 166                -text [mc Visualize] \
 167                -command [cb _visualize]
 168        pack $w.buttons.visualize -side left
 169        ${NS}::button $w.buttons.merge \
 170                -text [mc Merge] \
 171                -command $_start
 172        pack $w.buttons.merge -side right
 173        ${NS}::button $w.buttons.cancel \
 174                -text [mc "Cancel"] \
 175                -command [cb _cancel]
 176        pack $w.buttons.cancel -side right -padx 5
 177        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
 178
 179        set w_rev [::choose_rev::new_unmerged $w.rev [mc "Revision To Merge"]]
 180        pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
 181
 182        bind $w <$M1B-Key-Return> $_start
 183        bind $w <Key-Return> $_start
 184        bind $w <Key-Escape> [cb _cancel]
 185        wm protocol $w WM_DELETE_WINDOW [cb _cancel]
 186
 187        bind $w.buttons.merge <Visibility> [cb _visible]
 188        tkwait window $w
 189}
 190
 191method _visible {} {
 192        grab $w
 193        if {[is_config_true gui.matchtrackingbranch]} {
 194                $w_rev pick_tracking_branch
 195        }
 196        $w_rev focus_filter
 197}
 198
 199method _cancel {} {
 200        wm protocol $w WM_DELETE_WINDOW {}
 201        unlock_index
 202        destroy $w
 203        delete_this
 204}
 205
 206}
 207
 208namespace eval merge {
 209
 210proc reset_hard {} {
 211        global HEAD commit_type file_states
 212
 213        if {[string match amend* $commit_type]} {
 214                info_popup [mc "Cannot abort while amending.
 215
 216You must finish amending this commit.
 217"]
 218                return
 219        }
 220
 221        if {![lock_index abort]} return
 222
 223        if {[string match *merge* $commit_type]} {
 224                set op_question [mc "Abort merge?
 225
 226Aborting the current merge will cause *ALL* uncommitted changes to be lost.
 227
 228Continue with aborting the current merge?"]
 229        } else {
 230                set op_question [mc "Reset changes?
 231
 232Resetting the changes will cause *ALL* uncommitted changes to be lost.
 233
 234Continue with resetting the current changes?"]
 235        }
 236
 237        if {[ask_popup $op_question] eq {yes}} {
 238                set fd [git_read --stderr read-tree --reset -u -v HEAD]
 239                fconfigure $fd -blocking 0 -translation binary
 240                fileevent $fd readable [namespace code [list _reset_wait $fd]]
 241                $::main_status start [mc "Aborting"] [mc "files reset"]
 242        } else {
 243                unlock_index
 244        }
 245}
 246
 247proc _reset_wait {fd} {
 248        global ui_comm
 249
 250        $::main_status update_meter [read $fd]
 251
 252        fconfigure $fd -blocking 1
 253        if {[eof $fd]} {
 254                set fail [catch {close $fd} err]
 255                $::main_status stop
 256                unlock_index
 257
 258                $ui_comm delete 0.0 end
 259                $ui_comm edit modified false
 260
 261                catch {file delete [gitdir MERGE_HEAD]}
 262                catch {file delete [gitdir rr-cache MERGE_RR]}
 263                catch {file delete [gitdir MERGE_RR]}
 264                catch {file delete [gitdir SQUASH_MSG]}
 265                catch {file delete [gitdir MERGE_MSG]}
 266                catch {file delete [gitdir GITGUI_MSG]}
 267
 268                if {$fail} {
 269                        warn_popup "[mc "Abort failed."]\n\n$err"
 270                }
 271                rescan {ui_status [mc "Abort completed.  Ready."]}
 272        } else {
 273                fconfigure $fd -blocking 0
 274        }
 275}
 276
 277}