File: clone.py

package info (click to toggle)
git-cola 4.14.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,812 kB
  • sloc: python: 37,625; sh: 298; makefile: 223; xml: 102; tcl: 62
file content (213 lines) | stat: -rw-r--r-- 6,815 bytes parent folder | download | duplicates (2)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
from functools import partial

from qtpy import QtCore
from qtpy import QtWidgets
from qtpy.QtCore import Qt

from .. import cmds
from .. import core
from .. import icons
from .. import utils
from .. import qtutils
from ..i18n import N_
from ..interaction import Interaction
from ..qtutils import get
from . import defs
from . import standard
from . import text


def clone(context, spawn=True, show=True):
    """Clone a repository and spawn a new git-cola instance"""
    parent = qtutils.active_window()
    progress = standard.progress('', '', parent)
    return clone_repo(context, show, progress, task_finished, spawn)


def clone_repo(context, show, progress, finish, spawn):
    """Clone a repository asynchronously with progress animation"""
    func = partial(start_clone_task, context, progress, finish, spawn)
    prompt = prompt_for_clone(context, show=show)
    prompt.result.connect(func)
    return prompt


def prompt_for_clone(context, show=True):
    """Presents a GUI for cloning a repository"""
    view = Clone(context, parent=qtutils.active_window())
    if show:
        view.show()
    return view


def task_finished(task):
    """Report errors from the clone task if they exist"""
    cmd = task.cmd
    if cmd is None:
        return
    status = cmd.status
    out = cmd.out
    err = cmd.err
    title = N_('Error: could not clone "%s"') % cmd.url
    Interaction.command(title, 'git clone', status, out, err)


def start_clone_task(
    context, progress, finish, spawn, url, destdir, submodules, shallow
):
    # Use a thread to update in the background
    runtask = context.runtask
    progress.set_details(N_('Clone Repository'), N_('Cloning repository at %s') % url)
    task = CloneTask(context, url, destdir, submodules, shallow, spawn)
    runtask.start(task, finish=finish, progress=progress)


class CloneTask(qtutils.Task):
    """Clones a Git repository"""

    def __init__(self, context, url, destdir, submodules, shallow, spawn):
        qtutils.Task.__init__(self)
        self.context = context
        self.url = url
        self.destdir = destdir
        self.submodules = submodules
        self.shallow = shallow
        self.spawn = spawn
        self.cmd = None

    def task(self):
        """Runs the model action and captures the result"""
        self.cmd = cmds.do(
            cmds.Clone,
            self.context,
            self.url,
            self.destdir,
            self.submodules,
            self.shallow,
            spawn=self.spawn,
        )
        return self.cmd


class Clone(standard.Dialog):
    # Signal binding for returning the input data
    result = QtCore.Signal(object, object, bool, bool)

    def __init__(self, context, parent=None):
        standard.Dialog.__init__(self, parent=parent)
        self.context = context
        self.model = context.model

        self.setWindowTitle(N_('Clone Repository'))
        if parent is not None:
            self.setWindowModality(Qt.WindowModal)

        # Repository location
        self.url_label = QtWidgets.QLabel(N_('URL'))
        hint = 'git://git.example.com/repo.git'
        self.url = text.HintedLineEdit(context, hint, parent=self)
        self.url.setToolTip(N_('Path or URL to clone (Env. $VARS okay)'))

        # Initialize submodules
        self.submodules = qtutils.checkbox(
            text=N_('Initialize submodules'), checked=False
        )

        # Reduce commit history
        self.shallow = qtutils.checkbox(
            text=N_('Reduce commit history to minimum'), checked=False
        )

        # Buttons
        self.ok_button = qtutils.create_button(
            text=N_('Clone'), icon=icons.ok(), default=True
        )
        self.close_button = qtutils.close_button()

        # Form layout for inputs
        self.input_layout = qtutils.form(
            defs.no_margin, defs.button_spacing, (self.url_label, self.url)
        )

        self.button_layout = qtutils.hbox(
            defs.margin,
            defs.spacing,
            self.submodules,
            defs.button_spacing,
            self.shallow,
            qtutils.STRETCH,
            self.close_button,
            self.ok_button,
        )

        self.main_layout = qtutils.vbox(
            defs.margin, defs.spacing, self.input_layout, self.button_layout
        )
        self.setLayout(self.main_layout)

        qtutils.connect_button(self.close_button, self.close)
        qtutils.connect_button(self.ok_button, self.prepare_to_clone)
        self.url.textChanged.connect(lambda x: self.update_actions())

        self.init_state(context.settings, self.resize, 720, 200)
        self.update_actions()

    def update_actions(self):
        url = get(self.url).strip()
        enabled = bool(url)
        self.ok_button.setEnabled(enabled)

    def prepare_to_clone(self):
        """Grabs and validates the input data"""

        submodules = get(self.submodules)
        shallow = get(self.shallow)

        url = get(self.url)
        url = utils.expandpath(url)
        if not url:
            return
        # Pick a suitable basename by parsing the URL
        newurl = url.replace('\\', '/').rstrip('/')
        try:
            default = newurl.rsplit('/', 1)[-1]
        except IndexError:
            default = ''
        if default == '.git':
            # The end of the URL is /.git, so assume it's a file path
            default = os.path.basename(os.path.dirname(newurl))
        if default.endswith('.git'):
            # The URL points to a bare repo
            default = default[:-4]
        if url == '.':
            # The URL is the current repo
            default = os.path.basename(core.getcwd())
        if not default:
            Interaction.information(
                N_('Error Cloning'), N_('Could not parse Git URL: "%s"') % url
            )
            Interaction.log(N_('Could not parse Git URL: "%s"') % url)
            return

        # Prompt the user for a directory to use as the parent directory
        msg = N_('Select a parent directory for the new clone')
        dirname = qtutils.opendir_dialog(msg, os.path.dirname(self.model.getcwd()))
        if not dirname:
            return
        count = 1
        destdir = os.path.join(dirname, default)
        olddestdir = destdir
        if core.exists(destdir):
            # An existing path can be specified
            msg = N_('"%s" already exists, cola will create a new directory') % destdir
            Interaction.information(N_('Directory Exists'), msg)

        # Make sure the directory doesn't exist
        while core.exists(destdir):
            destdir = olddestdir + str(count)
            count += 1

        # Return the input data and close the dialog
        self.result.emit(url, destdir, submodules, shallow)
        self.close()