Create or set a working tree uptodate using bzrlib!

Robert Collins robertc at robertcollins.net
Mon Jun 25 08:51:39 BST 2007


On Sat, 2007-06-23 at 17:16 +0100, Luís Soares wrote:
> 
> 
> Hello all, 
> 
> 
>  I am a newbie, when it comes to have things done through bzrlib
> directly, rather than the command line... This is the first time I am
> using bzrlib.
> 
> 
> I have the following situation: I have this program that needs to
> check if there is a branch in a certain filesystem url. If the branch
> does not exist, it will clone a remote branch. If it does exist, than
> it will only pull/update the local copy.
>
> Going through bzrlib, I combined snippets of code and came up with the
> following:
> 
> 
> from bzrlib.builtins  import cmd_branch, cmd_pull
> from bzrlib.workingtree import WorkingTree
> from bzrlib.errors import NotBranchError
> from bzrlib.tests import StringIOWrapper
> 
> 
> def do_workingtree_refresh(remote, local):
>   is_pull_needed = False
> 
>   res = StringIOWrapper()
> 
>   try:
>     tree, relpath = WorkingTree.open_containing(local)

If you want to check for a specific path, use 
    tree = WorkingTree.open(local)
as that will not search up the tree for a containing WorkingTree, but
only look at the exact path.

>     is_pull_needed = True
>   except NotBranchError:
>     cmd=cmd_branch()
>     cmd.outf = res
>     cmd.run(from_location=remote, to_location=local)
>   if is_pull_needed: 
>     cmd=cmd_pull()
>     cmd.outf = res
>     cmd.run(directory=local)

You don't need to use the cmd objects - you can pull and clone directly
from the branches. Specifically I would write what you have as:

def do_workingtree_refresh(remote, local):
  source = Branch.open(remote)
  try:
    tree = WorkingTree.open(local)
  except NotBranchError:
    source.bzrdir.clone(local)
  else:
    tree.pull(remote)
  return 0

Hope this helps.
-Rob

-- 
GPG key available at: <http://www.robertcollins.net/keys.txt>.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20070625/021c777e/attachment.pgp 


More information about the bazaar mailing list