1 # Copyright 2009 -- Robert Toscano
8 <popup name="RepositoryManagerPopup">
9 <menuitem action="GitCommit"/>
10 <menuitem action="GitPull"/>
11 <menuitem action="GitFetch"/>
12 <menuitem action="GitPush"/>
19 def load_emblem(name
):
20 return gtk
.icon_theme_get_default().load_icon(name
, 20, gtk
.ICON_LOOKUP_USE_BUILTIN
)
22 emblems
= { 'emblem-default': load_emblem('emblem-default'),
23 'emblem-new': load_emblem('emblem-new'),
24 'emblem-cvs-modified': load_emblem('emblem-cvs-modified'),
25 'emblem-cvs-added': load_emblem('emblem-cvs-added') }
28 class RepositoryManager(gtk
.TreeView
):
29 def __init__(self
, repos
, statusbar
, statusbar_id
):
32 self
.statusbar
= statusbar
33 self
.statusbar_id
= statusbar_id
34 self
.repomodel
= gtk
.ListStore(str, # full path to repo
35 str, # name to be displayed
37 gtk
.gdk
.Pixbuf
) # icon with emblem composited
39 super(RepositoryManager
, self
).__init
__(self
.repomodel
)
41 self
.set_grid_lines(False)
42 self
.set_headers_clickable(True)
43 self
.append_column(gtk
.TreeViewColumn(None, gtk
.CellRendererPixbuf(), pixbuf
=3))
44 self
.append_column(gtk
.TreeViewColumn('Name', gtk
.CellRendererText(), text
=1))
45 self
.append_column(gtk
.TreeViewColumn('Branch', gtk
.CellRendererText(), text
=2))
46 self
.connect('button-release-event', self
.on_button_released
)
48 # Create the Repository Manager popup
49 self
.uimanager
= gtk
.UIManager()
50 self
.uimanager
.add_ui_from_string(popup_xml
)
52 actiongroup
= gtk
.ActionGroup('GitBase')
53 actiongroup
.add_actions([ ('GitCommit', gtk
.STOCK_SAVE_AS
, 'Commit', None,
54 'Commit all staged files', self
.on_git_commit_all
),
55 ('GitPush', gtk
.STOCK_GOTO_TOP
, 'Push', None,
56 'Push to origin repository', self
.on_git_push
),
57 ('GitPull', gtk
.STOCK_GOTO_BOTTOM
, 'Pull', None,
58 'Pull from remote reposities', self
.on_git_pull
),
59 ('GitFetch', gtk
.STOCK_GO_DOWN
, 'Fetch', None,
60 'Fetch from remote repositories', self
.on_git_fetch
) ])
62 self
.uimanager
.insert_action_group(actiongroup
, 0)
65 def update_root(self
, root
):
67 repopaths
= self
.repos
.keys()
69 for path
in repopaths
:
70 if path
.startswith(self
.curr_root
): toadd
.append(path
)
74 for row
in self
.repomodel
:
75 if not row
[0].startswith(self
.curr_root
): toremove
.append(row
)
76 else: tokeep
.append(row
[0])
78 # remove paths already in the model from toadd
79 for path
in tokeep
: toadd
.remove(path
)
81 # remove paths from the model
83 self
.repomodel
.remove(row
.iter)
85 # modify existing rows to match new root
86 for row
in self
.repomodel
:
87 if row
[0] == self
.curr_root
: row
[1] = os
.path
.basename(row
[0])
88 elif self
.curr_root
.startswith(row
[0]): row
[1] = os
.path
.basename(row
[0])
89 else: row
[1] = '.'+row
[0][len(self
.curr_root
):]
95 for path
in toadd
: self
.repomodel
.append( (git_dir
, 'master') )
98 def add_repo(self
, repopath
):
100 if repopath
== self
.curr_root
:
101 name
= os
.path
.basename(repopath
)
103 name
= '.'+repopath
[len(self
.curr_root
):]
105 stock_id
= gtk
.STOCK_DIRECTORY
107 emblem_buf
= emblems
['emblem-default']
109 self
.iters
[repopath
] = self
.repomodel
.append((repopath
, name
, branch
, emblem_buf
))
113 def update_repo(self
, repopath
):
114 status
= self
.repos
[repopath
]
116 if repopath
in status
['untracked']:
117 self
.repomodel
.set_value(self
.iters
[repopath
], 3, emblems('emblem-new'))
118 elif repopath
in status
['dirty']:
119 self
.repomodel
.set_value(self
.iters
[repopath
], 3, emblems('emblem-cvs-modified'))
120 elif repopath
in status
['staged']:
121 self
.repomodel
.set_value(self
.iters
[repopath
], 3, emblems('emblem-cvs-added'))
124 def on_button_released(self
, widget
, event
):
125 if event
.button
== 3:
126 self
.uimanager
.get_widget('/RepositoryManagerPopup').popup(None, None, None, event
.button
, 0)
129 def on_git_commit_all(self
, action
):
130 model
, rows
= self
.get_selection().get_selected_rows()
132 for path
in [ row
[0] for row
in rows
]:
135 def on_git_pull(self
, action
):
136 model
, rows
= self
.get_selection().get_selected_rows()
138 for path
in [ row
[0] for row
in rows
]:
141 def on_git_push(self
, action
):
142 model
, rows
= self
.get_selection().get_selected_rows()
144 for path
in [ row
[0] for row
in rows
]:
147 def on_git_fetch(self
, action
):
148 model
, rows
= self
.get_selection().get_selected_rows()
150 for path
in [ model
[row
][0] for row
in rows
]:
151 self
.statusbar
.flash_message(self
.statusbar_id
, 'Fetching repository: '+path
)