Best way to check if a directory is an brz branch

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Feb 17 14:53:30 GMT 2009


>>>>> "Nicholas" == Nicholas Allen <nick.allen at onlinehome.de> writes:

    Nicholas> Hi,
    Nicholas> I'm wondering what's the best way to check if a directory on the file
    Nicholas> system is a Bazaar branch. At the moment I do something like:

    Nicholas> if os.path.isdir(os.path.join(directory, '.bzr/branch')):

    Nicholas> but this seems overly tied to the implementation details of Bazaar. I
    Nicholas> couldn't find a method to do this. One way would be to try to open a
    Nicholas> branch for a directory and catch exceptions but that seems inefficient
    Nicholas> and would also fail if there was a problem with the branch itself.

    Nicholas> I expected a static method in Branch or BzrDir to check this. eg
    Nicholas> is_branch(directory).

def is_branch(p):
    from  bzrlib import (branch, errors)
    try:
        br, relpath = branch.Branch.open_containing(p)
    except errors.NotBranchError:
        return False
    if relpath != '':
        return False
    else:
        return True

should work.

Hth,

        Vincent



More information about the bazaar mailing list