Implement the 'clone' command
authorCatalin Marinas <catalin.marinas@gmail.com>
Wed, 3 Aug 2005 12:16:17 +0000 (13:16 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Wed, 3 Aug 2005 12:16:17 +0000 (13:16 +0100)
This command is based on the existing git-clone-script.

Signed-off-by: Catalin Marinas <catalin.marinas@gmail.com>
TODO
stgit/commands/clone.py [new file with mode: 0644]
stgit/git.py
stgit/main.py

diff --git a/TODO b/TODO
index 1caac50..26c9aae 100644 (file)
--- a/TODO
+++ b/TODO
@@ -2,7 +2,6 @@ The TODO list until 1.0:
 
 - tag (snapshot) command
 - log command (it should also show the log per single patch)
-- clone command
 - improved import command to import patches from a different branch in
   the same tree
 - tutorial, man page
diff --git a/stgit/commands/clone.py b/stgit/commands/clone.py
new file mode 100644 (file)
index 0000000..17a7d36
--- /dev/null
@@ -0,0 +1,56 @@
+__copyright__ = """
+Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 2 as
+published by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""
+
+import sys, os
+from optparse import OptionParser, make_option
+
+from stgit.commands.common import *
+from stgit.utils import *
+from stgit import stack, git
+
+
+help = 'pull the changes from the remote repository'
+usage = """%prog [options] <repository> <dir>
+
+Clone a GIT <repository> into the local <dir> and initialise the
+patch stack."""
+
+options = []
+
+
+def func(parser, options, args):
+    """Clone the <repository> into the local <dir> and initialises the
+    stack
+    """
+    if len(args) != 2:
+        parser.error('incorrect number of arguments')
+
+    repository = args[0]
+    local_dir = args[1]
+
+    if os.path.exists(local_dir):
+        raise CmdException, '"%s" exists. Remove it first' % local_dir
+
+    print 'Cloning "%s" into "%s"...' % (repository, local_dir)
+
+    git.clone(repository, local_dir)
+    os.chdir(local_dir)
+    git.checkout(tree_id = 'HEAD')
+
+    crt_series.init()
+
+    print 'done'
index fe2c447..3a999e4 100644 (file)
@@ -420,18 +420,21 @@ def files(rev1, rev2):
 
     return str.rstrip()
 
-def checkout(files = [], force = False):
+def checkout(files = [], tree_id = None, force = False):
     """Check out the given or all files
     """
-    git_flags = 'git-checkout-cache -q -u'
+    if tree_id and __run('git-read-tree -m', [tree_id]) != 0:
+        raise GitException, 'Failed git-read-tree -m %s' % tree_id
+
+    checkout_cmd = 'git-checkout-cache -q -u'
     if force:
-        git_flags += ' -f'
+        checkout_cmd += ' -f'
     if len(files) == 0:
-        git_flags += ' -a'
+        checkout_cmd += ' -a'
     else:
-        git_flags += ' --'
+        checkout_cmd += ' --'
 
-    if __run(git_flags, files) != 0:
+    if __run(checkout_cmd, files) != 0:
         raise GitException, 'Failed git-checkout-cache'
 
 def switch(tree_id):
@@ -440,10 +443,7 @@ def switch(tree_id):
     to_delete = filter(lambda x: x[0] in ['N', 'A'],
                        __tree_status(tree_id = tree_id))
 
-    if __run('git-read-tree -m', [tree_id]) != 0:
-        raise GitException, 'Failed git-read-tree -m %s' % tree_id
-
-    checkout(force = True)
+    checkout(tree_id = tree_id, force = True)
     __set_head(tree_id)
 
     # checkout doesn't remove files
@@ -476,3 +476,11 @@ def apply_patch(filename = None):
             raise GitException, 'Patch does not apply cleanly'
     else:
         _input('git-apply --index', sys.stdin)
+
+def clone(repository, local_dir):
+    """Clone a remote repository. At the moment, just use the
+    'git clone' script
+    """
+    if __run('git clone', [repository, local_dir]) != 0:
+        raise GitException, 'Failed "git clone %s %s"' \
+              % (repository, local_dir)
index 3fd11a1..fee0cc1 100644 (file)
@@ -33,6 +33,7 @@ import stgit.commands.applied
 import stgit.commands.delete
 import stgit.commands.diff
 import stgit.commands.clean
+import stgit.commands.clone
 import stgit.commands.export
 import stgit.commands.files
 import stgit.commands.imprt
@@ -61,6 +62,7 @@ commands = {
     'delete':   stgit.commands.delete,
     'diff':     stgit.commands.diff,
     'clean':    stgit.commands.clean,
+    'clone':    stgit.commands.clone,
     'export':   stgit.commands.export,
     'files':    stgit.commands.files,
     'import':   stgit.commands.imprt,
@@ -128,9 +130,13 @@ def main():
                           option_list = command.options)
     options, args = parser.parse_args()
     try:
-        # the lines below are a simple way to avoid an exception when
+        # 'clone' doesn't expect an already initialised GIT tree
+        if cmd == 'clone':
+            stgit.commands.common.crt_series = stack.Series('master')
+        else:
+            stgit.commands.common.crt_series = stack.Series()
+        # the line below is a simple way to avoid an exception when
         # stgit is run outside an initialised tree
-        stgit.commands.common.crt_series = stack.Series()
         setattr(command, 'crt_series', stgit.commands.common.crt_series)
 
         command.func(parser, options, args)