-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-branch-deleter
executable file
·168 lines (136 loc) · 6.3 KB
/
git-branch-deleter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2019 Jussi Lind <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import argparse
import signal
import subprocess
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
class Application:
class MainWindow(Gtk.Window):
def __init__(self, app):
Gtk.Window.__init__(self, title="Git Branch Deleter")
self._app = app
self._toggleColumnIndex = 2
self.set_default_size(800, 600)
self.set_border_width(5)
sw = Gtk.ScrolledWindow()
sw.set_border_width(10)
sw.set_hexpand(True)
sw.set_vexpand(True)
self.applyButton = Gtk.Button.new_with_mnemonic("_Apply")
self.applyButton.connect("clicked", self.onApplyClicked)
self.applyButton.set_sensitive(False)
self.listStore = Gtk.ListStore(str, str, bool)
self.updateListStore()
treeView = Gtk.TreeView(model=self.listStore)
branchNameRendererText = Gtk.CellRendererText()
branchNameText = Gtk.TreeViewColumn("Local Branch", branchNameRendererText, text=0)
treeView.append_column(branchNameText)
branchUseRendererText = Gtk.CellRendererText()
branchUseText = Gtk.TreeViewColumn("Last Time Used", branchUseRendererText, text=1)
treeView.append_column(branchUseText)
rendererToggle = Gtk.CellRendererToggle()
rendererToggle.connect("toggled", self.onCellToggled)
columnToggle = Gtk.TreeViewColumn("Delete", rendererToggle, active=self._toggleColumnIndex)
treeView.append_column(columnToggle)
grid = Gtk.Grid()
sw.add(treeView)
grid.attach(sw, 0, 0, 12, 6)
grid.attach(self.applyButton, 0, 13, 12, 1)
self.add(grid)
def atLeastOneChecked(self):
for toggle in self.listStore:
if toggle[self._toggleColumnIndex]:
return True
return False
def onCellToggled(self, widget, path):
self.listStore[path][self._toggleColumnIndex] = not self.listStore[path][self._toggleColumnIndex]
self.updateApplyButton()
def onApplyClicked(self, button):
dialog = Gtk.MessageDialog(parent=self, flags=0, message_type=Gtk.MessageType.QUESTION, buttons=Gtk.ButtonsType.YES_NO, text="Confirm Deletion")
dialog.format_secondary_text("Delete selected branches?")
response = dialog.run()
if response == Gtk.ResponseType.YES:
self.deleteSelectedBranches()
dialog.destroy()
def getSelectedBranches(self):
branches = []
for toggle in self.listStore:
if toggle[self._toggleColumnIndex]:
branches.append(toggle[0])
return branches
def deleteSelectedBranches(self):
for branch in self.getSelectedBranches():
self._app.deleteBranch(branch)
self.updateListStore()
def updateListStore(self):
self.listStore.clear()
for branch in self._app.getBranches():
self.listStore.append([branch, self._app.getBranchUse(branch), False])
self.updateApplyButton()
def updateApplyButton(self):
self.applyButton.set_sensitive(self.atLeastOneChecked())
def __init__(self, useD=False):
self._useD = useD
self._mainWindow = Application.MainWindow(self)
self._mainWindow.set_position(Gtk.WindowPosition.CENTER)
self._mainWindow.connect("destroy", Gtk.main_quit)
def deleteBranch(self, branch):
try:
subprocess.run(["git", "branch", "-D" if self._useD else "-d", branch], check=True)
except Exception as e:
dialog = Gtk.MessageDialog(parent=self._mainWindow, flags=0, message_type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK, text="Error")
dialog.format_secondary_text("Failed to delete branch '{0}': {1}".format(branch, e))
dialog.run()
dialog.destroy()
def getBranches(self):
try:
def filterDeletableBranches(branch):
return "*" not in branch and not branch.isspace() and len(branch) > 0
output = subprocess.check_output(["git", "branch", "--list", "--no-color"], universal_newlines=True)
branches = output.split('\n')
branches = [x.strip() for x in branches]
return filter(filterDeletableBranches, branches)
except Exception:
return []
def getBranchUse(self, branch):
try:
output = subprocess.check_output(["git", "log", branch, "--pretty=format:\"%cr\""], universal_newlines=True)
return output.split('\n')[0].strip('"')
except Exception:
return ""
def run(self):
self._mainWindow.show_all()
Gtk.main()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-D', action='store_true', help='Delete branches with -D instead of -d')
args = parser.parse_args()
app = Application(args.D)
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, Gtk.main_quit)
app.run()
if __name__ == '__main__':
main()