git-gui: Implemented create branch GUI.
Users may now create new branches by activating the Branch->Create menu item. This opens a dialog which lets the user enter the new branch name and select the starting revision for the new branch. For the starting revision we allow the user to either select from a list of known heads (aka local branches) or to enter an arbitrary SHA1 expression. For either creation technique we run the starting revision through rev-parse to verify it is valid before trying to create the ref with update-ref. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Jan 21, 2007 at 01:31 UTC
c5ab47cbe4cf9f2f3068c9bf23a666404b615176
1 file changed
+180
-3
git-gui.sh
+180
-3
@@ -1622,9 +1622,25 @@ proc load_all_heads {} {
1622
set all_heads [lsort $all_heads]
1623
}
1624
1625
-proc populate_branch_menu {m} {
1625
+proc populate_branch_menu {} {
1626
global all_heads disable_on_lock
1627
1628
+ set m .mbar.branch
1629
+ set last [$m index last]
1630
+ for {set i 0} {$i <= $last} {incr i} {
1631
+ if {[$m type $i] eq {separator}} {
1632
+ $m delete $i last
1633
+ set new_dol [list]
1634
+ foreach a $disable_on_lock {
1635
+ if {[lindex $a 0] ne $m || [lindex $a 2] < $i} {
1636
+ lappend new_dol $a
1637
+ }
1638
+ }
1639
+ set disable_on_lock $new_dol
1640
+ break
1641
+ }
1642
+ }
1643
+
1644
$m add separator
1645
foreach b $all_heads {
1646
$m add radiobutton \
@@ -1638,8 +1654,169 @@ proc populate_branch_menu {m} {
1654
}
1655
}
1656
1657
+proc do_create_branch_action {w} {
1658
+ global all_heads null_sha1
1659
+ global create_branch_checkout create_branch_revtype create_branch_head
1660
+
1661
+ set newbranch [string trim [$w.name.t get 0.0 end]]
1662
+ if {![catch {exec git show-ref --verify -- "refs/heads/$newbranch"}]} {
1663
+ tk_messageBox \
1664
+ -icon error \
1665
+ -type ok \
1666
+ -title [wm title $w] \
1667
+ -parent $w \
1668
+ -message "Branch '$newbranch' already exists."
1669
+ focus $w.name.t
1670
+ return
1671
+ }
1672
+ if {[catch {exec git check-ref-format "heads/$newbranch"}]} {
1673
+ tk_messageBox \
1674
+ -icon error \
1675
+ -type ok \
1676
+ -title [wm title $w] \
1677
+ -parent $w \
1678
+ -message "We do not like '$newbranch' as a branch name."
1679
+ focus $w.name.t
1680
+ return
1681
+ }
1682
+
1683
+ set rev {}
1684
+ switch -- $create_branch_revtype {
1685
+ head {set rev $create_branch_head}
1686
+ expression {set rev [string trim [$w.from.exp.t get 0.0 end]]}
1687
+ }
1688
+ if {[catch {set cmt [exec git rev-parse --verify "${rev}^0"]}]} {
1689
+ tk_messageBox \
1690
+ -icon error \
1691
+ -type ok \
1692
+ -title [wm title $w] \
1693
+ -parent $w \
1694
+ -message "Invalid starting revision: $rev"
1695
+ return
1696
+ }
1697
+ set cmd [list git update-ref]
1698
+ lappend cmd -m
1699
+ lappend cmd "branch: Created from $rev"
1700
+ lappend cmd "refs/heads/$newbranch"
1701
+ lappend cmd $cmt
1702
+ lappend cmd $null_sha1
1703
+ if {[catch {eval exec $cmd} err]} {
1704
+ tk_messageBox \
1705
+ -icon error \
1706
+ -type ok \
1707
+ -title [wm title $w] \
1708
+ -parent $w \
1709
+ -message "Failed to create '$newbranch'.\n\n$err"
1710
+ return
1711
+ }
1712
+
1713
+ lappend all_heads $newbranch
1714
+ set all_heads [lsort $all_heads]
1715
+ populate_branch_menu
1716
+
1717
+ destroy $w
1718
+}
1719
+
1720
proc do_create_branch {} {
1642
- error "NOT IMPLEMENTED"
1721
+ global all_heads current_branch
1722
+ global create_branch_checkout create_branch_revtype create_branch_head
1723
+
1724
+ set create_branch_checkout true
1725
+ set create_branch_revtype head
1726
+ set create_branch_head $current_branch
1727
+
1728
+ set w .branch_editor
1729
+ toplevel $w
1730
+ wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
1731
+
1732
+ label $w.header -text {Create New Branch} \
1733
+ -font font_uibold
1734
+ pack $w.header -side top -fill x
1735
+
1736
+ frame $w.buttons
1737
+ button $w.buttons.create -text Create \
1738
+ -font font_ui \
1739
+ -default active \
1740
+ -command [list do_create_branch_action $w]
1741
+ pack $w.buttons.create -side right
1742
+ button $w.buttons.cancel -text {Cancel} \
1743
+ -font font_ui \
1744
+ -command [list destroy $w]
1745
+ pack $w.buttons.cancel -side right -padx 5
1746
+ pack $w.buttons -side bottom -fill x -pady 10 -padx 10
1747
+
1748
+ labelframe $w.name \
1749
+ -text {Branch Description} \
1750
+ -font font_ui
1751
+ label $w.name.l -text {Name:} -font font_ui
1752
+ text $w.name.t \
1753
+ -height 1 \
1754
+ -width 40 \
1755
+ -font font_ui
1756
+ bind $w.name.t <Shift-Key-Tab> "focus $w.postActions.checkout;break"
1757
+ bind $w.name.t <Key-Tab> "focus $w.from.exp.t;break"
1758
+ bind $w.name.t <Key-Return> "do_create_branch_action $w;break"
1759
+ bind $w.name.t <Key> {
1760
+ if {{%K} ne {BackSpace}
1761
+ && {%K} ne {Tab}
1762
+ && {%K} ne {Escape}
1763
+ && {%K} ne {Return}} {
1764
+ if {%k <= 32} break
1765
+ if {[string first %A {~^:?*[}] >= 0} break
1766
+ }
1767
+ }
1768
+ pack $w.name.l -side left -padx 5
1769
+ pack $w.name.t -side left -fill x -expand 1
1770
+ pack $w.name -anchor nw -fill x -pady 5 -padx 5
1771
+
1772
+ labelframe $w.from \
1773
+ -text {Starting Revision} \
1774
+ -font font_ui
1775
+ frame $w.from.head
1776
+ radiobutton $w.from.head.r \
1777
+ -text {Local Branch:} \
1778
+ -value head \
1779
+ -variable create_branch_revtype \
1780
+ -font font_ui
1781
+ eval tk_optionMenu $w.from.head.m create_branch_head $all_heads
1782
+ pack $w.from.head.r -side left
1783
+ pack $w.from.head.m -side left
1784
+ frame $w.from.exp
1785
+ radiobutton $w.from.exp.r \
1786
+ -text {Revision Expression:} \
1787
+ -value expression \
1788
+ -variable create_branch_revtype \
1789
+ -font font_ui
1790
+ text $w.from.exp.t \
1791
+ -height 1 \
1792
+ -width 50 \
1793
+ -font font_ui
1794
+ bind $w.from.exp.t <Shift-Key-Tab> "focus $w.name.t;break"
1795
+ bind $w.from.exp.t <Key-Tab> "focus $w.postActions.checkout;break"
1796
+ bind $w.from.exp.t <Key-Return> "do_create_branch_action $w;break"
1797
+ pack $w.from.exp.r -side left
1798
+ pack $w.from.exp.t -side left -fill x -expand 1
1799
+ pack $w.from.head -padx 5 -fill x -expand 1
1800
+ pack $w.from.exp -padx 5 -fill x -expand 1
1801
+ pack $w.from -anchor nw -fill x -pady 5 -padx 5
1802
+
1803
+ labelframe $w.postActions \
1804
+ -text {Post Creation Actions} \
1805
+ -font font_ui
1806
+ checkbutton $w.postActions.checkout \
1807
+ -text {Checkout after creation} \
1808
+ -offvalue false \
1809
+ -onvalue true \
1810
+ -variable create_branch_checkout \
1811
+ -font font_ui
1812
+ pack $w.postActions.checkout -anchor nw
1813
+ pack $w.postActions -anchor nw -fill x -pady 5 -padx 5
1814
+
1815
+ bind $w <Visibility> "grab $w; focus $w.name.t"
1816
+ bind $w <Key-Escape> "destroy $w"
1817
+ bind $w <Key-Return> "do_create_branch_action $w;break"
1818
+ wm title $w "[appname] ([reponame]): Create Branch"
1819
+ tkwait window $w
1820
}
1821
1822
proc do_delete_branch {} {
@@ -3734,7 +3911,7 @@ if {!$single_commit} {
3911
load_all_remotes
3912
load_all_heads
3913
3737
- populate_branch_menu .mbar.branch
3914
+ populate_branch_menu
3915
populate_fetch_menu .mbar.fetch
3916
populate_pull_menu .mbar.pull
3917
populate_push_menu .mbar.push