git-gui: Implemented local branch deletion.
Users can now delete a local branch by selecting from a list of available branches. The list automatically does not include the current branch, as deleting the current branch could be quite dangerous and should not be supported. The user may also chose to have us verify the branches are fully merged into another branch before deleting them. By default we select the current branch, matching 'git branch -d' behavior, but the user could also select any other local branch. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Jan 21, 2007 at 02:14 UTC
887412d4e84c8249987a686aded6a7eab4345cb8
1 file changed
+131
-2
git-gui.sh
+131
-2
@@ -1713,7 +1713,6 @@ proc do_create_branch_action {w} {
1713
lappend all_heads $newbranch
1714
set all_heads [lsort $all_heads]
1715
populate_branch_menu
1716
-
1716
destroy $w
1717
}
1718
@@ -1819,8 +1818,138 @@ proc do_create_branch {} {
1818
tkwait window $w
1819
}
1820
1821
+proc do_delete_branch_action {w} {
1822
+ global all_heads
1823
+ global delete_branch_checkhead delete_branch_head
1824
+
1825
+ set to_delete [list]
1826
+ set msg {Are you sure you want to delete the following branches?
1827
+
1828
+}
1829
+ foreach i [$w.list.l curselection] {
1830
+ set b [$w.list.l get $i]
1831
+ if {[catch {set o [exec git rev-parse --verify $b]}]} continue
1832
+ if {$delete_branch_checkhead} {
1833
+ if {[catch {set m [exec git merge-base $o $delete_branch_head]}]} continue
1834
+ if {$o ne $m} continue
1835
+ }
1836
+ lappend to_delete [list $b $o]
1837
+ append msg " - $b\n"
1838
+ }
1839
+ if {$to_delete eq {}} {
1840
+ tk_messageBox \
1841
+ -icon info \
1842
+ -type ok \
1843
+ -title [wm title $w] \
1844
+ -parent $w \
1845
+ -message {No branches are able to be deleted.
1846
+
1847
+This is likely because you did not select any branches,
1848
+or all selected branches are not completely merged.
1849
+}
1850
+ return
1851
+ }
1852
+ append msg {
1853
+It can be difficult to recover deleted branches.
1854
+
1855
+Delete the above branches?}
1856
+ if {[tk_messageBox \
1857
+ -icon warning \
1858
+ -type yesno \
1859
+ -title [wm title $w] \
1860
+ -parent $w \
1861
+ -message $msg] ne yes} {
1862
+ return
1863
+ }
1864
+
1865
+ set failed {}
1866
+ foreach i $to_delete {
1867
+ set b [lindex $i 0]
1868
+ set o [lindex $i 1]
1869
+ if {[catch {exec git update-ref -d "refs/heads/$b" $o} err]} {
1870
+ append failed " - $b: $err\n"
1871
+ } else {
1872
+ set x [lsearch -sorted $all_heads $b]
1873
+ if {$x >= 0} {
1874
+ set all_heads [lreplace $all_heads $x $x]
1875
+ }
1876
+ }
1877
+ }
1878
+
1879
+ if {$failed ne {}} {
1880
+ tk_messageBox \
1881
+ -icon error \
1882
+ -type ok \
1883
+ -title [wm title $w] \
1884
+ -parent $w \
1885
+ -message "Failed to delete branches:\n$failed"
1886
+ }
1887
+
1888
+ set all_heads [lsort $all_heads]
1889
+ populate_branch_menu
1890
+ destroy $w
1891
+}
1892
+
1893
proc do_delete_branch {} {
1823
- error "NOT IMPLEMENTED"
1894
+ global all_heads current_branch
1895
+ global delete_branch_checkhead delete_branch_head
1896
+
1897
+ set delete_branch_checkhead 1
1898
+ set delete_branch_head $current_branch
1899
+
1900
+ set w .branch_editor
1901
+ toplevel $w
1902
+ wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
1903
+
1904
+ label $w.header -text {Delete Local Branch} \
1905
+ -font font_uibold
1906
+ pack $w.header -side top -fill x
1907
+
1908
+ frame $w.buttons
1909
+ button $w.buttons.create -text Delete \
1910
+ -font font_ui \
1911
+ -command [list do_delete_branch_action $w]
1912
+ pack $w.buttons.create -side right
1913
+ button $w.buttons.cancel -text {Cancel} \
1914
+ -font font_ui \
1915
+ -command [list destroy $w]
1916
+ pack $w.buttons.cancel -side right -padx 5
1917
+ pack $w.buttons -side bottom -fill x -pady 10 -padx 10
1918
+
1919
+ labelframe $w.list \
1920
+ -text {Local Branches} \
1921
+ -font font_ui
1922
+ listbox $w.list.l \
1923
+ -height 10 \
1924
+ -width 50 \
1925
+ -selectmode extended \
1926
+ -font font_ui
1927
+ foreach h $all_heads {
1928
+ if {$h ne $current_branch} {
1929
+ $w.list.l insert end $h
1930
+ }
1931
+ }
1932
+ pack $w.list.l -fill both -pady 5 -padx 5
1933
+ pack $w.list -fill both -pady 5 -padx 5
1934
+
1935
+ labelframe $w.validate \
1936
+ -text {Only Delete If} \
1937
+ -font font_ui
1938
+ frame $w.validate.head
1939
+ checkbutton $w.validate.head.r \
1940
+ -text {Already Merged Into Local Branch:} \
1941
+ -variable delete_branch_checkhead \
1942
+ -font font_ui
1943
+ eval tk_optionMenu $w.validate.head.m delete_branch_head $all_heads
1944
+ pack $w.validate.head.r -side left
1945
+ pack $w.validate.head.m -side left
1946
+ pack $w.validate.head -padx 5 -fill x -expand 1
1947
+ pack $w.validate -anchor nw -fill x -pady 5 -padx 5
1948
+
1949
+ bind $w <Visibility> "grab $w; focus $w"
1950
+ bind $w <Key-Escape> "destroy $w"
1951
+ wm title $w "[appname] ([reponame]): Delete Branch"
1952
+ tkwait window $w
1953
}
1954
1955
proc switch_branch {b} {