Skip to content

Commit

Permalink
bookmarks: add a "Default Repository" feature
Browse files Browse the repository at this point in the history
Allow setting the "Default Repository" by right-clicking in the
"Favorites" or "Bookmarks" widgets.

The "Default Repository" is used when the working directory does not
contain a Git repository.  If it is not set, or if it is set to a bogus
value, then `git cola` will still prompt for a repository.

Closes #513
Suggested-by: Zeioth <zeioth@hotmail.com>
Signed-off-by: David Aguilar <davvid@gmail.com>
  • Loading branch information
davvid committed Oct 23, 2015
1 parent e960835 commit 9f3c3d7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
65 changes: 58 additions & 7 deletions cola/widgets/bookmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cola import cmds
from cola import core
from cola import git
from cola import gitcfg
from cola import hotkeys
from cola import icons
from cola import qtutils
Expand Down Expand Up @@ -101,6 +102,12 @@ def __init__(self, style, settings, parent=None):
self.open_new_action = qtutils.add_action(self,
N_('Open in New Window'), self.open_new_repo, hotkeys.NEW)

self.set_default_repo_action = qtutils.add_action(self,
N_('Set Default Repository'), self.set_default_repo)

self.clear_default_repo_action = qtutils.add_action(self,
N_('Clear Default Repository'), self.clear_default_repo)

self.open_default_action = qtutils.add_action(self,
cmds.OpenDefaultApp.name(), self.open_default,
hotkeys.PRIMARY_ACTION)
Expand Down Expand Up @@ -128,22 +135,22 @@ def __init__(self, style, settings, parent=None):
self.launch_terminal_action,
self.open_default_action)
self.action_group.setEnabled(False)
self.set_default_repo_action.setEnabled(False)
self.clear_default_repo_action.setEnabled(False)

def refresh(self):
icon = icons.folder()
settings = self.settings
builder = BuildItem(self.style)

# bookmarks
if self.style == BOOKMARKS:
items = [BookmarksTreeWidgetItem(path, icon)
for path in settings.bookmarks]
items = [builder.get(path) for path in settings.bookmarks]

if prefs.sort_bookmarks():
items.sort()
elif self.style == RECENT_REPOS:
# recent items
items = [BookmarksTreeWidgetItem(path, icon)
for path in settings.recent]
items = [builder.get(path) for path in settings.recent]
else:
items = []
self.clear()
Expand All @@ -158,6 +165,13 @@ def contextMenuEvent(self, event):
menu.addAction(self.copy_action)
menu.addAction(self.launch_editor_action)
menu.addAction(self.launch_terminal_action)
menu.addSeparator()
item = self.selected_item()
is_default = bool(item and item.is_default)
if is_default:
menu.addAction(self.clear_default_repo_action)
else:
menu.addAction(self.set_default_repo_action)
menu.exec_(self.mapToGlobal(event.pos()))

def apply_fn(self, fn, *args, **kwargs):
Expand All @@ -171,6 +185,20 @@ def copy(self):
def open_default(self):
self.apply_fn(lambda item: cmds.do(cmds.OpenDefaultApp, [item.path]))

def set_default_repo(self):
self.apply_fn(self.set_default_item)

def set_default_item(self, item):
cmds.do(cmds.SetDefaultRepo, item.path)
self.refresh()

def clear_default_repo(self):
self.apply_fn(self.clear_default_item)

def clear_default_item(self, item):
cmds.do(cmds.SetDefaultRepo, None)
self.refresh()

def open_repo(self):
self.apply_fn(lambda item: cmds.do(cmds.OpenRepo, item.path))

Expand All @@ -184,9 +212,14 @@ def launch_terminal(self):
self.apply_fn(lambda item: cmds.do(cmds.LaunchTerminal, item.path))

def item_selection_changed(self):
enabled = bool(self.selected_item())
item = self.selected_item()
enabled = bool(item)
self.action_group.setEnabled(enabled)

is_default = bool(item and item.is_default)
self.set_default_repo_action.setEnabled(not is_default)
self.clear_default_repo_action.setEnabled(is_default)

def tree_double_clicked(self, item, column):
cmds.do(cmds.OpenRepo, item.path)

Expand Down Expand Up @@ -222,11 +255,29 @@ def delete_bookmark(self):
self.refresh()


class BuildItem(object):

def __init__(self, style):
self.star_icon = icons.star()
self.folder_icon = icons.folder()
self.default_repo = gitcfg.current().get('cola.defaultrepo')

def get(self, path):
is_default = self.default_repo == path
if is_default:
icon = self.star_icon
else:
icon = self.folder_icon
return BookmarksTreeWidgetItem(path, icon, is_default)


class BookmarksTreeWidgetItem(QtGui.QTreeWidgetItem):

def __init__(self, path, icon):
def __init__(self, path, icon, is_default):
QtGui.QTreeWidgetItem.__init__(self)
self.path = path
self.is_default = is_default

self.setIcon(0, icon)
normpath = os.path.normpath(path)
basename = os.path.basename(normpath)
Expand Down
8 changes: 8 additions & 0 deletions share/doc/git-cola/relnotes/unreleased.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ Usability, bells and whistles
* Ctrl+O was added as a hotkey for opening repositories.

https://github.com/git-cola/git-cola/pull/507

* `git cola`'s Bookmarks widget can now be used to set a "Default Repository".
Under the hood, we set the `cola.defaultrepo` configuration variable.
The default repository is used whenever `git cola` is launched outside of
a Git repostiory. When unset, or when set to a bogus value, `git cola`
will still prompt for a repository.

https://github.com/git-cola/git-cola/issues/513
1 change: 1 addition & 0 deletions share/doc/git-cola/thanks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@ Thanks
* Vitor Lobo
* v.paritskiy
* Wolfgang Ocker
* Zeioth
* Zhang Han

0 comments on commit 9f3c3d7

Please sign in to comment.