Code

git-gui: Allow double-click in checkout dialog to start checkout
[git.git] / lib / branch_create.tcl
index 7f82424edace4b473977d5fe9d37ee268cb3148f..def615d19d6a0ba4fd76553146f29129be5baf17 100644 (file)
@@ -10,7 +10,10 @@ field w_name         ; # new branch name widget
 field name         {}; # name of the branch the user has chosen
 field name_type  user; # type of branch name to use
 
+field opt_merge    ff; # type of merge to apply to existing branch
 field opt_checkout  1; # automatically checkout the new branch?
+field opt_fetch     1; # refetch tracking branch if used?
+field reset_ok      0; # did the user agree to reset?
 
 constructor dialog {} {
        global repo_config
@@ -61,37 +64,64 @@ constructor dialog {} {
        pack $w.desc -anchor nw -fill x -pady 5 -padx 5
 
        set w_rev [::choose_rev::new $w.rev {Starting Revision}]
-       pack $w.rev -anchor nw -fill x -pady 5 -padx 5
-
-       labelframe $w.postActions -text {Post Creation Actions}
-       checkbutton $w.postActions.checkout \
-               -text {Checkout after creation} \
+       pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
+
+       labelframe $w.options -text {Options}
+
+       frame $w.options.merge
+       label $w.options.merge.l -text {Update Existing Branch:}
+       pack $w.options.merge.l -side left
+       radiobutton $w.options.merge.no \
+               -text No \
+               -value none \
+               -variable @opt_merge
+       pack $w.options.merge.no -side left
+       radiobutton $w.options.merge.ff \
+               -text {Fast Forward Only} \
+               -value ff \
+               -variable @opt_merge
+       pack $w.options.merge.ff -side left
+       radiobutton $w.options.merge.reset \
+               -text {Reset} \
+               -value reset \
+               -variable @opt_merge
+       pack $w.options.merge.reset -side left
+       pack $w.options.merge -anchor nw
+
+       checkbutton $w.options.fetch \
+               -text {Fetch Tracking Branch} \
+               -variable @opt_fetch
+       pack $w.options.fetch -anchor nw
+
+       checkbutton $w.options.checkout \
+               -text {Checkout After Creation} \
                -variable @opt_checkout
-       pack $w.postActions.checkout -anchor nw
-       pack $w.postActions -anchor nw -fill x -pady 5 -padx 5
+       pack $w.options.checkout -anchor nw
+       pack $w.options -anchor nw -fill x -pady 5 -padx 5
+
+       trace add variable @name_type write [cb _select]
 
        set name $repo_config(gui.newbranchtemplate)
+       if {[is_config_true gui.matchtrackingbranch]} {
+               set name_type match
+       }
 
-       bind $w <Visibility> "
-               grab $w
-               $w_name icursor end
-               focus $w_name
-       "
+       bind $w <Visibility> [cb _visible]
        bind $w <Key-Escape> [list destroy $w]
        bind $w <Key-Return> [cb _create]\;break
        tkwait window $w
 }
 
 method _create {} {
-       global null_sha1 repo_config
-       global all_heads
+       global repo_config
+       global M1B
 
+       set spec [$w_rev get_tracking_branch]
        switch -- $name_type {
        user {
                set newbranch $name
        }
        match {
-               set spec [$w_rev get_tracking_branch]
                if {$spec eq {}} {
                        tk_messageBox \
                                -icon error \
@@ -124,58 +154,40 @@ method _create {} {
                focus $w_name
                return
        }
-       if {![catch {git show-ref --verify -- "refs/heads/$newbranch"}]} {
-               tk_messageBox \
-                       -icon error \
-                       -type ok \
-                       -title [wm title $w] \
-                       -parent $w \
-                       -message "Branch '$newbranch' already exists."
-               focus $w_name
-               return
-       }
+
        if {[catch {git check-ref-format "heads/$newbranch"}]} {
                tk_messageBox \
                        -icon error \
                        -type ok \
                        -title [wm title $w] \
                        -parent $w \
-                       -message "We do not like '$newbranch' as a branch name."
+                       -message "'$newbranch' is not an acceptable branch name."
                focus $w_name
                return
        }
 
-       if {[catch {set cmt [$w_rev get_commit]}]} {
-               tk_messageBox \
-                       -icon error \
-                       -type ok \
-                       -title [wm title $w] \
-                       -parent $w \
-                       -message "Invalid starting revision: [$w_rev get]"
+       if {$spec ne {} && $opt_fetch} {
+               set new {}
+       } elseif {[catch {set new [$w_rev commit_or_die]}]} {
                return
        }
-       if {[catch {
-                       git update-ref \
-                               -m "branch: Created from [$w_rev get]" \
-                               "refs/heads/$newbranch" \
-                               $cmt \
-                               $null_sha1
-               } err]} {
-               tk_messageBox \
-                       -icon error \
-                       -type ok \
-                       -title [wm title $w] \
-                       -parent $w \
-                       -message "Failed to create '$newbranch'.\n\n$err"
-               return
+
+       set co [::checkout_op::new \
+               [$w_rev get] \
+               $new \
+               refs/heads/$newbranch]
+       $co parent $w
+       $co enable_create   1
+       $co enable_merge    $opt_merge
+       $co enable_checkout $opt_checkout
+       if {$spec ne {} && $opt_fetch} {
+               $co enable_fetch $spec
        }
 
-       lappend all_heads $newbranch
-       set all_heads [lsort $all_heads]
-       populate_branch_menu
-       destroy $w
-       if {$opt_checkout} {
-               switch_branch $newbranch
+       if {[$co run]} {
+               destroy $w
+       } else {
+               focus $w_name
        }
 }
 
@@ -191,4 +203,18 @@ method _validate {d S} {
        return 1
 }
 
+method _select {args} {
+       if {$name_type eq {match}} {
+               $w_rev pick_tracking_branch
+       }
+}
+
+method _visible {} {
+       grab $w
+       if {$name_type eq {user}} {
+               $w_name icursor end
+               focus $w_name
+       }
+}
+
 }