7945d746f8dae6d5f3d9264df4737ccb89ef9c32
   1# git-gui merge conflict resolution
   2# parts based on git-mergetool (c) 2006 Theodore Y. Ts'o
   3
   4proc merge_resolve_one {stage} {
   5        global current_diff_path
   6
   7        switch -- $stage {
   8                1 { set target [mc "the base version"] }
   9                2 { set target [mc "this branch"] }
  10                3 { set target [mc "the other branch"] }
  11        }
  12
  13        set op_question [mc "Force resolution to %s?
  14Note that the diff shows only conflicting changes.
  15
  16%s will be overwritten.
  17
  18This operation can be undone only by restarting the merge." \
  19                $target [short_path $current_diff_path]]
  20
  21        if {[ask_popup $op_question] eq {yes}} {
  22                merge_load_stages $current_diff_path [list merge_force_stage $stage]
  23        }
  24}
  25
  26proc merge_add_resolution {path} {
  27        global current_diff_path
  28
  29        if {$path eq $current_diff_path} {
  30                set after {reshow_diff;}
  31        } else {
  32                set after {}
  33        }
  34
  35        update_index \
  36                [mc "Adding resolution for %s" [short_path $path]] \
  37                [list $path] \
  38                [concat $after [list ui_ready]]
  39}
  40
  41proc merge_force_stage {stage} {
  42        global current_diff_path merge_stages
  43
  44        if {$merge_stages($stage) ne {}} {
  45                git checkout-index -f --stage=$stage -- $current_diff_path
  46        } else {
  47                file delete -- $current_diff_path
  48        }
  49
  50        merge_add_resolution $current_diff_path
  51}
  52
  53proc merge_load_stages {path cont} {
  54        global merge_stages_fd merge_stages merge_stages_buf
  55
  56        if {[info exists merge_stages_fd]} {
  57                catch { kill_file_process $merge_stages_fd }
  58                catch { close $merge_stages_fd }
  59        }
  60
  61        set merge_stages(0) {}
  62        set merge_stages(1) {}
  63        set merge_stages(2) {}
  64        set merge_stages(3) {}
  65        set merge_stages_buf {}
  66
  67        set merge_stages_fd [eval git_read ls-files -u -z -- $path]
  68
  69        fconfigure $merge_stages_fd -blocking 0 -translation binary -encoding binary
  70        fileevent $merge_stages_fd readable [list read_merge_stages $merge_stages_fd $cont]
  71}
  72
  73proc read_merge_stages {fd cont} {
  74        global merge_stages_buf merge_stages_fd merge_stages
  75
  76        append merge_stages_buf [read $fd]
  77        set pck [split $merge_stages_buf "\0"]
  78        set merge_stages_buf [lindex $pck end]
  79
  80        if {[eof $fd] && $merge_stages_buf ne {}} {
  81                lappend pck {}
  82                set merge_stages_buf {}
  83        }
  84
  85        foreach p [lrange $pck 0 end-1] {
  86                set fcols [split $p "\t"]
  87                set cols  [split [lindex $fcols 0] " "]
  88                set stage [lindex $cols 2]
  89                
  90                set merge_stages($stage) [lrange $cols 0 1]
  91        }
  92
  93        if {[eof $fd]} {
  94                close $fd
  95                unset merge_stages_fd
  96                eval $cont
  97        }
  98}