1 from __future__
import division
, absolute_import
, unicode_literals
11 from .interaction
import Interaction
12 from .widgets
import completion
13 from .widgets
import editremotes
14 from .widgets
.browse
import BrowseBranch
15 from .widgets
.selectcommits
import select_commits
16 from .widgets
.selectcommits
import select_commits_and_output
19 def delete_branch(context
):
20 """Launch the 'Delete Branch' dialog."""
21 icon
= icons
.discard()
22 branch
= choose_branch(
23 context
, N_('Delete Branch'), N_('Delete'), icon
=icon
)
26 cmds
.do(cmds
.DeleteBranch
, context
, branch
)
29 def delete_remote_branch(context
):
30 """Launch the 'Delete Remote Branch' dialog."""
31 remote_branch
= choose_remote_branch(
32 context
, N_('Delete Remote Branch'), N_('Delete'),
36 remote
, branch
= gitcmds
.parse_remote_branch(remote_branch
)
38 cmds
.do(cmds
.DeleteRemoteBranch
, context
, remote
, branch
)
41 def browse_current(context
):
42 """Launch the 'Browse Current Branch' dialog."""
43 branch
= gitcmds
.current_branch(context
)
44 BrowseBranch
.browse(context
, branch
)
47 def browse_other(context
):
48 """Prompt for a branch and inspect content at that point in time."""
49 # Prompt for a branch to browse
50 branch
= choose_ref(context
, N_('Browse Commits...'), N_('Browse'))
53 BrowseBranch
.browse(context
, branch
)
56 def checkout_branch(context
):
57 """Launch the 'Checkout Branch' dialog."""
58 branch
= choose_potential_branch(
59 context
, N_('Checkout Branch'), N_('Checkout'))
62 cmds
.do(cmds
.CheckoutBranch
, context
, branch
)
65 def cherry_pick(context
):
66 """Launch the 'Cherry-Pick' dialog."""
67 revs
, summaries
= gitcmds
.log_helper(context
, all
=True)
68 commits
= select_commits(
69 context
, N_('Cherry-Pick Commit'), revs
, summaries
, multiselect
=False)
72 cmds
.do(cmds
.CherryPick
, context
, commits
)
75 def new_repo(context
):
76 """Prompt for a new directory and create a new Git repository
78 :returns str: repository path or None if no repository was created.
82 path
= qtutils
.opendir_dialog(N_('New Repository...'), core
.getcwd())
85 # Avoid needlessly calling `git init`.
86 if git
.is_git_repository(path
):
87 # We could prompt here and confirm that they really didn't
88 # mean to open an existing repository, but I think
89 # treating it like an "Open" is a sensible DWIM answer.
92 status
, out
, err
= git
.init(path
)
96 title
= N_('Error Creating Repository')
97 Interaction
.command_error(title
, 'git init', status
, out
, err
)
101 def open_new_repo(context
):
102 dirname
= new_repo(context
)
105 cmds
.do(cmds
.OpenRepo
, context
, dirname
)
108 def new_bare_repo(context
):
110 repo
= prompt_for_new_bare_repo()
114 ok
= cmds
.do(cmds
.NewBareRepo
, context
, repo
)
117 # Add a new remote pointing to the bare repo
118 parent
= qtutils
.active_window()
119 add_remote
= editremotes
.add_remote(
120 context
, parent
, name
=os
.path
.basename(repo
),
121 url
=repo
, readonly_url
=True)
128 def prompt_for_new_bare_repo():
129 path
= qtutils
.opendir_dialog(N_('Select Directory...'), core
.getcwd())
134 default
= os
.path
.basename(core
.getcwd())
135 if not default
.endswith('.git'):
138 name
, ok
= qtutils
.prompt(
139 N_('Enter a name for the new bare repo'),
140 title
=N_('New Bare Repository...'),
142 if not name
or not ok
:
144 if not name
.endswith('.git'):
146 repo
= os
.path
.join(path
, name
)
148 Interaction
.critical(
149 N_('Error'), N_('"%s" already exists') % repo
)
156 def export_patches(context
):
157 """Run 'git format-patch' on a list of commits."""
158 revs
, summaries
= gitcmds
.log_helper(context
)
159 to_export_and_output
= select_commits_and_output(
160 context
, N_('Export Patches'), revs
, summaries
)
161 if not to_export_and_output
['to_export']:
165 cmds
.FormatPatch
, context
,
166 reversed(to_export_and_output
['to_export']), reversed(revs
),
167 to_export_and_output
['output'])
170 def diff_expression(context
):
171 """Diff using an arbitrary expression."""
172 tracked
= gitcmds
.tracked_branch(context
)
173 current
= gitcmds
.current_branch(context
)
174 if tracked
and current
:
175 ref
= tracked
+ '..' + current
177 ref
= 'origin/master..'
178 difftool
.diff_expression(context
, qtutils
.active_window(), ref
)
181 def open_repo(context
):
182 model
= context
.model
183 dirname
= qtutils
.opendir_dialog(
184 N_('Open Git Repository...'), model
.getcwd())
187 cmds
.do(cmds
.OpenRepo
, context
, dirname
)
190 def open_repo_in_new_window(context
):
191 """Spawn a new cola session."""
192 model
= context
.model
193 dirname
= qtutils
.opendir_dialog(
194 N_('Open Git Repository...'), model
.getcwd())
197 cmds
.do(cmds
.OpenNewRepo
, context
, dirname
)
200 def load_commitmsg(context
):
201 """Load a commit message from a file."""
202 model
= context
.model
203 filename
= qtutils
.open_file(
204 N_('Load Commit Message'), directory
=model
.getcwd())
206 cmds
.do(cmds
.LoadCommitMessageFromFile
, context
, filename
)
209 def choose_from_dialog(get
, context
, title
, button_text
, default
, icon
=None):
210 parent
= qtutils
.active_window()
211 return get(context
, title
, button_text
, parent
, default
=default
, icon
=icon
)
214 def choose_ref(context
, title
, button_text
, default
=None, icon
=None):
215 return choose_from_dialog(completion
.GitRefDialog
.get
,
216 context
, title
, button_text
, default
, icon
=icon
)
219 def choose_branch(context
, title
, button_text
, default
=None, icon
=None):
220 return choose_from_dialog(completion
.GitBranchDialog
.get
,
221 context
, title
, button_text
, default
, icon
=icon
)
224 def choose_potential_branch(context
, title
, button_text
,
225 default
=None, icon
=None):
226 return choose_from_dialog(completion
.GitCheckoutBranchDialog
.get
,
227 context
, title
, button_text
, default
, icon
=icon
)
230 def choose_remote_branch(context
, title
, button_text
, default
=None, icon
=None):
231 return choose_from_dialog(completion
.GitRemoteBranchDialog
.get
,
232 context
, title
, button_text
, default
, icon
=icon
)
235 def review_branch(context
):
236 """Diff against an arbitrary revision, branch, tag, etc."""
237 branch
= choose_ref(context
, N_('Select Branch to Review'), N_('Review'))
240 merge_base
= gitcmds
.merge_base_parent(context
, branch
)
241 difftool
.diff_commits(context
, qtutils
.active_window(), merge_base
, branch
)
244 def rename_branch(context
):
245 """Launch the 'Rename Branch' dialogs."""
246 branch
= choose_branch(context
, N_('Rename Existing Branch'), N_('Select'))
249 new_branch
= choose_branch(
250 context
, N_('Enter New Branch Name'), N_('Rename'))
253 cmds
.do(cmds
.RenameBranch
, context
, branch
, new_branch
)
256 def reset_branch_head(context
):
258 context
, N_('Reset Branch Head'), N_('Reset'), default
='HEAD^')
260 cmds
.do(cmds
.ResetBranchHead
, context
, ref
)
263 def reset_worktree(context
):
265 context
, N_('Reset Worktree'), N_('Reset'))
267 cmds
.do(cmds
.ResetWorktree
, context
, ref
)
271 """Install the GUI-model interaction hooks"""
272 Interaction
.choose_ref
= staticmethod(choose_ref
)