Rev 3: Add a function for determining the final location to archive a branch. in http://bzr.arbash-meinel.com/plugins/raf

John Arbash Meinel john at arbash-meinel.com
Wed Jan 14 20:44:55 GMT 2009


At http://bzr.arbash-meinel.com/plugins/raf

------------------------------------------------------------
revno: 3
revision-id: john at arbash-meinel.com-20090114204439-0pfzgwq3zu4dkyrw
parent: john at arbash-meinel.com-20090114200220-ts6fo1jexvlalemz
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: raf
timestamp: Wed 2009-01-14 14:44:39 -0600
message:
  Add a function for determining the final location to archive a branch.
-------------- next part --------------
=== modified file '__init__.py'
--- a/__init__.py	2009-01-14 20:02:20 +0000
+++ b/__init__.py	2009-01-14 20:44:39 +0000
@@ -21,10 +21,10 @@
 """
 
 # This is meant to be the 'trunk' branch of your repository
-DEFAULT_TARGET_BRANCH = 'bzr+ssh://?????'
+DEFAULT_TARGET_BRANCH = 'bzr+ssh://host/path/to/repo/trunk'
 # This is meant to be the 'archive' of old branches, when a new feature branch
 # is archived, it will be renamed into this directory
-DEFAULT_ARCHIVE_BASE = 'bzr+ssh://path/to/repo/archived'
+DEFAULT_ARCHIVE_BASE = 'bzr+ssh://host/path/to/repo/archived'
 
 
 from bzrlib import (
@@ -49,18 +49,33 @@
 
     This will also ensure that all revisions are merged into the 'target'
     branch before archiving the branch. If no target is specified then it
-    defaults to the 'trunk' branch.
+    defaults to the 'trunk' branch. If '--no-check' is supplied, then we will
+    skip the check to see if all revisions have been merged.
+
+
+    Open question... do we want this to work on branches that are 'local only'?
+    So that you can push up your local-only feature branch into the final
+    archived location, and remove it from your local disk?
     """
 
     takes_args = ['url?']
-    takes_options = [option.Option('target'),
-                     option.Option('no-check'),
+    takes_options = [option.Option('target', type=unicode,
+                        help='Ensure that the branch being archived is'
+                            ' fully merged into this "target" branch.'
+                            ' If not supplied it defaults to "%s"'
+                            % (DEFAULT_TARGET_BRANCH,)),
+                     option.Option('no-check',
+                        help='Do not check if the branch is fully merged.'),
+                     option.Option('archive', type=unicode,
+                        help='The directory to use to archive branches.'
+                             ' If not supplied it defaults to "%s"'
+                             % (DEFAULT_ARCHIVE_BASE,)),
                     ]
 
-    def run(self, url=None):
+    def run(self, url=None, target=None, archive=None, no_check=False):
         from bzrlib.raf import raf
 
-        wt, b = raf.find_branch_and_workingtree(url)
+        b, wt = raf.find_branch_and_workingtree(url)
 
 commands.register_command(cmd_archive_branch)
 

=== modified file 'raf.py'
--- a/raf.py	2009-01-14 20:02:20 +0000
+++ b/raf.py	2009-01-14 20:44:39 +0000
@@ -24,9 +24,12 @@
 from bzrlib import (
     branch,
     errors,
+    transport,
+    urlutils,
     workingtree,
     )
 
+
 def find_branch_to_archive(url):
     """Find the branch that we want to archive.
 
@@ -42,12 +45,46 @@
     if url is None:
         try:
             wt, relpath = workingtree.WorkingTree.open_containing('.')
-            b = wt.branch
+            branch_to_archive = wt.branch
         except errors.NoWorkingTree:
-            b = branch.Branch.open('.')
+            branch_to_archive = branch.Branch.open('.')
             wt = None
     else:
         wt = None
-        b = branch.Branch.open(url)
-
-    return b, wt
+        branch_to_archive = branch.Branch.open(url)
+
+    if wt is not None:
+        # Check to see if this is actually a checkout of another branch
+        master = wt.branch.get_master_branch()
+        if master is not None:
+            # We actually want to archive the master branch
+            branch_to_archive = master
+    return branch_to_archive, wt
+
+
+def get_archived_location(b, archive):
+    """Determine the URL that we will try to archive this branch to.
+
+    The default is to use the archived path, and append the last portion of the
+    branch name + '-01'. If something already exists at that location, the
+    number is incremented until it is unique.
+
+    :return: (transport, url)
+        transport is a Transport connected to the archive location
+        url is the full URL that should be used as the target location.
+    """
+    if archive is None:
+        from bzrlib.plugins.raf import DEFAULT_ARCHIVE_BASE
+        archive = DEFAULT_ARCHIVE_BASE
+    trans = transport.get_transport(archive,
+                                    # pass possible_transports so we don't
+                                    # connect multiple times to a remote
+                                    # location
+                                    possible_transports=[b.bzrdir.transport])
+    basename = urlutils.basename(b.base)
+    for count in range(1, 100):
+        archived_name = '%s-%02d' % (basename, count,)
+        try:
+            trans.stat(archived_name)
+        except errors.NoSuchFile: # This path doesn't exist
+            return trans, trans.abspath(archived_name)

=== modified file 'test_raf.py'
--- a/test_raf.py	2009-01-14 20:02:20 +0000
+++ b/test_raf.py	2009-01-14 20:44:39 +0000
@@ -62,3 +62,73 @@
         os.chdir('subdir')
         self.assertRaises(errors.NotBranchError,
                           raf.find_branch_to_archive, None)
+
+    def test_find_branch_from_url(self):
+        b = self.make_branch('a_branch')
+        found_b, wt = raf.find_branch_to_archive('a_branch')
+        self.assertIs(None, wt)
+        self.assertEqual(b.base, found_b.base)
+
+        # b.base is already a URL, so make sure we can find it from that
+        found_b, wt = raf.find_branch_to_archive(b.base)
+        self.assertEqual(b.base, found_b.base)
+
+        # No branch exists
+        self.assertRaises(errors.NotBranchError,
+                          raf.find_branch_to_archive, 'not_a_branch')
+        # Ensure that we get an error if we try to open a branch via a subdir
+        self.assertRaises(errors.NotBranchError,
+                          raf.find_branch_to_archive, 'a_branch/subdir')
+        self.assertRaises(errors.NotBranchError,
+                          raf.find_branch_to_archive, b.base + '/subdir')
+
+    def test_find_master_branch(self):
+        master = self.make_branch('master')
+        checkout = self.make_branch_and_tree('checkout')
+        checkout.branch.bind(master)
+
+        os.chdir('checkout')
+        b, wt = raf.find_branch_to_archive(None)
+        self.assertEqual(master.base, b.base)
+        self.assertIsNot(None, wt)
+        self.assertEqual(wt.basedir, checkout.basedir)
+
+
+# passing archive=None is allowed, so we use a simple object to tell if a
+# parameter is passed
+_signet = object()
+
+class TestGetArchivedLocation(tests.TestCaseWithTransport):
+
+    def setUp(self):
+        super(TestGetArchivedLocation, self).setUp()
+        self.archive_url = self.get_url('archive')
+
+    def assertArchived(self, expected, a_branch, archive=_signet):
+        if archive is _signet:
+            archive = self.archive_url
+        trans, url = raf.get_archived_location(a_branch, archive)
+        self.assertIsNot(None, trans)
+        self.assertEqual(expected, url)
+
+    def test_uses_archive_path(self):
+        b = self.make_branch('a_branch')
+        self.assertArchived(self.archive_url + '/a_branch-01', b)
+
+    def test_location_exists(self):
+        b = self.make_branch('a_branch')
+        self.build_tree(['archive/', 'archive/a_branch-01/'])
+        self.assertArchived(self.archive_url + '/a_branch-02', b)
+
+    def test_use_default_archive(self):
+        b = self.make_branch('a_branch')
+        from bzrlib.plugins import raf as plugin_raf
+        orig_default = plugin_raf.DEFAULT_ARCHIVE_BASE
+        try:
+            default_archive = self.get_url('default_archive')
+            plugin_raf.DEFAULT_ARCHIVE_BASE = default_archive
+
+            self.assertArchived(default_archive + '/a_branch-01',
+                                b, None)
+        finally:
+            plugin_raf.DEFAULT_ARCHIVE_BASE = orig_default



More information about the bazaar-commits mailing list