Esempio n. 1
0
 def test_open_from_path_bare(self):
     d = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, d)
     Repo.init_bare(d)
     gc = GitStore.open_from_path(d)
     self.assertIsInstance(gc, BareGitStore)
     self.assertEqual(gc.repo.path, d)
Esempio n. 2
0
    def new(self, path, desc=None, bare=True):
        """
        Create a new bare repo.Local instance.
        
        :param path: Path to new repo.
        :param desc: Repo description.
        :param bare: Create as bare repo.

        :returns: New repo.Local instance.
        """
        if os.path.exists(path):
            raise RepoError('Path already exists: %s' % path)
        try:
            os.mkdir(path)
            if bare:
                Repo.init_bare(path)
            else:
                Repo.init(path)
            repo = Local(path)
            if desc:
                repo.setDescription(desc)
            version = repo.addVersion()
            version.save('Box Initialization')
            return repo
        except Exception, e:
            traceback.print_exc()
            raise RepoError('Error creating repo')
Esempio n. 3
0
def add_new_repository(req, backen, mat):
    req.respond(HTTP_OK, 'text/html')
    repos = [c for c in backen.repos.keys()]
    repos_make_message = None
    repos_make = False
    params = parse_qs(req.environ['QUERY_STRING'])
    new_repository = params.get('add_new_repository', [None])[0]+".git"

    if new_repository and new_repository not in repos:
        repo_dir = os.path.join(InitRepoPath().get_scanpath(), new_repository)
        try:
            Repo.init_bare(repo_dir, mkdir=True)
        except PermissionError:
            repos_make_message = "make repository folder permission error."
        except FileExistsError:
            repos_make_message = "make repository folder file exists error."
        except OSError:
            repos_make_message = "make repository folder os error."
        else:
            try:
                backen.repos[str('/' + new_repository)] = Repo(repo_dir)
            except:
                pass
            finally:
                repos = [c for c in backen.repos.keys()]
                repos_make = True

    yield env.get_template("add_new_repository.html").render(repos=repos,
                                                             repos_make_message=repos_make_message,
                                                             repos_make=repos_make).encode()
Esempio n. 4
0
def create_repository(instance, **kwargs):
    # Return if the repository has already been created
    if os.path.exists(instance.path):
        return

    # Create the repository and initialize it as a bare Git repo
    os.makedirs(instance.path)
    Repo.init_bare(instance.path)
Esempio n. 5
0
    def test_common_revisions(self):
        """
        This test demonstrates that ``find_common_revisions()`` actually returns
        common heads, not revisions; dulwich already uses
        ``find_common_revisions()`` in such a manner (see
        ``Repo.fetch_objects()``).
        """

        expected_shas = set(["60dacdc733de308bb77bb76ce0fb0f9b44c9769e"])

        # Source for objects.
        r_base = open_repo("simple_merge.git")

        # Re-create each-side of the merge in simple_merge.git.
        #
        # Since the trees and blobs are missing, the repository created is
        # corrupted, but we're only checking for commits for the purpose of this
        # test, so it's immaterial.
        r1_dir = tempfile.mkdtemp()
        r1_commits = [
            "ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",  # HEAD
            "60dacdc733de308bb77bb76ce0fb0f9b44c9769e",
            "0d89f20333fbb1d2f3a94da77f4981373d8f4310",
        ]

        r2_dir = tempfile.mkdtemp()
        r2_commits = [
            "4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",  # HEAD
            "60dacdc733de308bb77bb76ce0fb0f9b44c9769e",
            "0d89f20333fbb1d2f3a94da77f4981373d8f4310",
        ]

        try:
            r1 = Repo.init_bare(r1_dir)
            for c in r1_commits:
                r1.object_store.add_object(r_base.get_object(c))
            r1.refs["HEAD"] = r1_commits[0]

            r2 = Repo.init_bare(r2_dir)
            for c in r2_commits:
                r2.object_store.add_object(r_base.get_object(c))
            r2.refs["HEAD"] = r2_commits[0]

            # Finally, the 'real' testing!
            shas = r2.object_store.find_common_revisions(r1.get_graph_walker())
            self.assertEqual(set(shas), expected_shas)

            shas = r1.object_store.find_common_revisions(r2.get_graph_walker())
            self.assertEqual(set(shas), expected_shas)
        finally:
            shutil.rmtree(r1_dir)
            shutil.rmtree(r2_dir)
Esempio n. 6
0
    def test_common_revisions(self):
        """
        This test demonstrates that ``find_common_revisions()`` actually returns
        common heads, not revisions; dulwich already uses
        ``find_common_revisions()`` in such a manner (see
        ``Repo.fetch_objects()``).
        """

        expected_shas = set(['60dacdc733de308bb77bb76ce0fb0f9b44c9769e'])

        # Source for objects.
        r_base = open_repo('simple_merge.git')

        # Re-create each-side of the merge in simple_merge.git.
        #
        # Since the trees and blobs are missing, the repository created is
        # corrupted, but we're only checking for commits for the purpose of this
        # test, so it's immaterial.
        r1_dir = tempfile.mkdtemp()
        r1_commits = [
            'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',  # HEAD
            '60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
            '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
        ]

        r2_dir = tempfile.mkdtemp()
        r2_commits = [
            '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6',  # HEAD
            '60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
            '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
        ]

        try:
            r1 = Repo.init_bare(r1_dir)
            map(lambda c: r1.object_store.add_object(r_base.get_object(c)), \
                r1_commits)
            r1.refs['HEAD'] = r1_commits[0]

            r2 = Repo.init_bare(r2_dir)
            map(lambda c: r2.object_store.add_object(r_base.get_object(c)), \
                r2_commits)
            r2.refs['HEAD'] = r2_commits[0]

            # Finally, the 'real' testing!
            shas = r2.object_store.find_common_revisions(r1.get_graph_walker())
            self.assertEqual(set(shas), expected_shas)

            shas = r1.object_store.find_common_revisions(r2.get_graph_walker())
            self.assertEqual(set(shas), expected_shas)
        finally:
            shutil.rmtree(r1_dir)
            shutil.rmtree(r2_dir)
Esempio n. 7
0
def clone(source, target=None, bare=False, checkout=None, outstream=sys.stdout):
    """Clone a local or remote git repository.

    :param source: Path or URL for source repository
    :param target: Path to target repository (optional)
    :param bare: Whether or not to create a bare repository
    :param outstream: Optional stream to write progress to
    :return: The new repository
    """
    if checkout is None:
        checkout = (not bare)
    if checkout and bare:
        raise ValueError("checkout and bare are incompatible")
    client, host_path = get_transport_and_path(source)

    if target is None:
        target = host_path.split("/")[-1]

    if not os.path.exists(target):
        os.mkdir(target)
    if bare:
        r = Repo.init_bare(target)
    else:
        r = Repo.init(target)
    remote_refs = client.fetch(host_path, r,
        determine_wants=r.object_store.determine_wants_all,
        progress=outstream.write)
    r["HEAD"] = remote_refs["HEAD"]
    if checkout:
        outstream.write('Checking out HEAD')
        index.build_index_from_tree(r.path, r.index_path(),
                                    r.object_store, r["HEAD"].tree)

    return r
Esempio n. 8
0
    def test_git(self):
        """Test the behaviour of the Git object."""
        repo = Repo.init_bare(self.tempdir)
        git = Git(repo, branch="master")
        git2 = Git(repo, branch="master")

        def get_hello():
            """Return the hello.jinja template."""
            env = jinja2.Environment(loader=git.jinja_loader("templates"))
            return env.get_template("hello.jinja")

        self.assertRaises(jinja2.TemplateNotFound, get_hello)
        self.assertRaises(ValueError, git.write, "", "foo")  # empty path

        git.write("templates/hello.jinja", "Hello, World!")
        self.assertRaises(ObjectTypeError, git.write, "templates", "foo")
        self.assertRaises(ObjectTypeError, git.write, "templates/hello.jinja/foo", "foo")
        assert list(git.history()) == []
        git.commit("Alice", "*****@*****.**", "First commit")
        commit_1 = git.head.id
        assert list(git.history()) == [commit_1]
        self.assertRaises(ConflictError, git2.commit, "Alice", "*****@*****.**", "(not) First commit")

        git.write("templates/hello.jinja", '{% from "sub/name.jinja" import name %}Hello {{ name() }}!')
        git.write("templates/sub/name.jinja", "{% macro name() %}from Pynuts{% endmacro %}")
        git.commit("Bob", "*****@*****.**", "Second commit")
        commit_2 = git.head.id
        assert commit_2 != commit_1
        assert git.head.parents == [commit_1]
        assert git.repository.refs["refs/heads/master"] == commit_2
        assert list(git.history()) == [commit_2, commit_1]

        # Make sure we read from the filesystem
        git = Git(repo, branch="master", commit=commit_1)

        self.assertRaises(ConflictError, git.commit, "Bob", "*****@*****.**", "(not) Second commit")

        self.assertRaises(ValueError, git.read, "")
        self.assertRaises(ValueError, git.read, "/")
        self.assertRaises(ObjectTypeError, git.read, "templates")
        self.assertRaises(ObjectTypeError, git.read, "templates/hello.jinja/foo")
        self.assertRaises(NotFoundError, git.read, "foo")
        self.assertRaises(NotFoundError, git.read, "foo/bar")
        self.assertRaises(NotFoundError, git.read, "templates/bar")
        assert git.read("templates/hello.jinja") == "Hello, World!"
        assert git.read("/templates//hello.jinja") == "Hello, World!"

        template = get_hello()
        assert template.filename.endswith("/<git commit %s>/templates/hello.jinja" % commit_1)
        assert template.render() == "Hello, World!"

        git = Git(repo, branch="master")
        assert git.head.id == commit_2
        template = get_hello()
        assert template.filename.endswith("/<git commit %s>/templates/hello.jinja" % commit_2)
        assert template.render() == "Hello from Pynuts!"

        git = Git(repo, branch="inexistent")
        git.tree = git.store_directory(os.path.join(self.tempdir, "refs"))
        assert git.read("heads/master").strip() == commit_2
Esempio n. 9
0
def git_repo_init(gitdir):
    os.mkdir(gitdir)
    repo = Repo.init_bare(gitdir)
    blob = Blob.from_string("""Why, Hello there!

This is your friendly Legislation tracker, Billy here.

This is a git repo full of everything I write to the DB. This isn't super
useful unless you're debugging production issues.

Fondly,
   Bill, your local Billy instance.""")
    tree = Tree()
    tree.add("README", 0100644, blob.id)
    commit = Commit()
    commit.tree = tree.id
    author = "Billy <billy@localhost>"
    commit.author = commit.committer = author
    commit.commit_time = commit.author_time = int(time())
    tz = parse_timezone('-0400')[0]
    commit.commit_timezone = commit.author_timezone = tz
    commit.encoding = "UTF-8"
    commit.message = "Initial commit"
    repo.object_store.add_object(blob)
    repo.object_store.add_object(tree)
    repo.object_store.add_object(commit)
    repo.refs['refs/heads/master'] = commit.id
Esempio n. 10
0
 def test_create(self):
     tmp_dir = tempfile.mkdtemp()
     try:
         repo = Repo.init_bare(tmp_dir)
         self.assertEquals(tmp_dir, repo._controldir)
     finally:
         shutil.rmtree(tmp_dir)
Esempio n. 11
0
def TestRepo():
    checkout = Repo.init(tempfile.mkdtemp())

    with open(os.path.join(checkout.path, 'foo'), 'w') as fp:
        fp.write('monty')
    with open(os.path.join(checkout.path, 'bar'), 'w') as fp:
        fp.write('python')

    sub_path = os.path.join(checkout.path, 'sub')
    os.mkdir(sub_path)

    with open(os.path.join(sub_path, 'foo'), 'w') as fp:
        fp.write('sub_monty')
    with open(os.path.join(sub_path, 'bar'), 'w') as fp:
        fp.write('sub_python')

    checkout.stage(
        ['foo', 'bar',
         os.path.join('sub', 'foo'),
         os.path.join('sub', 'bar')])
    checkout.do_commit('The first commit',
                       committer='John Doe <*****@*****.**>')

    bare = Repo.init_bare(tempfile.mkdtemp())
    client, host_path = get_transport_and_path(checkout.path)

    refs = client.fetch(
        host_path,
        bare,
        determine_wants=bare.object_store.determine_wants_all,
    )
    bare["HEAD"] = refs["HEAD"]
    bare["refs/heads/master"] = refs["refs/heads/master"]

    return bare, checkout
Esempio n. 12
0
    def document_repository(self):
        """Return the application bare git document repository.

        If the application has a ``PYNUTS_DOCUMENT_REPOSITORY`` configuration
        key expressed as an absolute path, the repo will be located at this
        path. If this configuration key is expressed as a relative path, its
        location will be taken relatively to the application instance path.
        Finally, if no such configuration is found, a bare git repository will
        be created in the ``documents.git`` directory, located at the
        application instance path.

        All parent directories will be created if needed, and if
        non-existent, the repository will be initialized.

        """
        self.document_repository_path = os.path.join(
            self.app.instance_path, self.app.config.get("PYNUTS_DOCUMENT_REPOSITORY")
        )
        # If document_repository_path does not exist,
        # create it (and possible parent folders) and initialize the bare repo
        if os.path.exists(self.document_repository_path):
            return Repo(self.document_repository_path)
        else:
            os.makedirs(self.document_repository_path)
            return Repo.init_bare(self.document_repository_path)
Esempio n. 13
0
def create():
    form = CreateForm()
    if form.validate_on_submit():
	newuser = form.username.data
	password = crypt.crypt(form.password.data,salt())
	user = User(nickname=newuser, password=sha256_crypt.encrypt(form.password.data), email=form.email.data, role=ROLE_USER)
	db.session.add(user)
	db.session.commit()
	os.system("sudo mkdir " + settings.WORKING_DIR + newuser)
        os.system("sudo mkdir " + settings.REMOTE_DIR + newuser)
	os.system("sudo mkdir " + settings.WORKING_DIR + newuser + "/myRepo")
	os.system("sudo chmod 777 " + settings.REMOTE_DIR + newuser)
        os.system("sudo chmod 777 " + settings.WORKING_DIR + newuser)
	repo = Repo.init_bare(settings.REMOTE_DIR + newuser + "/")
	newrepo = Rpstry(repourl="/myRepo/", owner=user)
	db.session.add(newrepo)
        db.session.commit()
	working_repo = repo.clone(settings.WORKING_DIR + newuser + newrepo.repourl, False, False, "origin")
	p = subprocess.Popen(["sudo", "git", "remote", "add", "origin", "file:///" + settings.REMOTE_DIR + newuser + "/"], cwd=settings.WORKING_DIR + newuser + "/myRepo/")
	p.wait()
	open(settings.REMOTE_DIR + newuser + "/.htpasswd", 'a').writelines(newuser + ":" + password + "\n")
	s = get_serializer()
    	payload = s.dumps(user.id)
    	url = url_for('activate_user', payload=payload, _external=True)	
	msg = Message('Confirm Email Address', sender = ADMINS[0], recipients = [user.email])
	msg.body = "Follow this link to activate account: " + url
	mail.send(msg)
	flash("User created and activation email sent")
	return redirect(url_for('login'))
    return render_template('createAccount.html',
        title = 'Create New Account',
        form = form)
Esempio n. 14
0
def clone(source, target=None, bare=False, checkout=None, outstream=sys.stdout):
    """Clone a local or remote git repository.

    :param source: Path or URL for source repository
    :param target: Path to target repository (optional)
    :param bare: Whether or not to create a bare repository
    :param outstream: Optional stream to write progress to
    :return: The new repository
    """
    if checkout is None:
        checkout = (not bare)
    if checkout and bare:
        raise ValueError("checkout and bare are incompatible")
    client, host_path = get_transport_and_path(source)

    if target is None:
        target = host_path.split("/")[-1]

    if not os.path.exists(target):
        os.mkdir(target)
    if bare:
        r = Repo.init_bare(target)
    else:
        r = Repo.init(target)
    remote_refs = client.fetch(host_path, r,
        determine_wants=r.object_store.determine_wants_all,
        progress=outstream.write)
    r["HEAD"] = remote_refs["HEAD"]
    if checkout:
        outstream.write('Checking out HEAD')
        index.build_index_from_tree(r.path, r.index_path(),
                                    r.object_store, r["HEAD"].tree)

    return r
Esempio n. 15
0
def git_repo_init(gitdir):
    os.mkdir(gitdir)
    repo = Repo.init_bare(gitdir)
    blob = Blob.from_string("""Why, Hello there!

This is your friendly Legislation tracker, Billy here.

This is a git repo full of everything I write to the DB. This isn't super
useful unless you're debugging production issues.

Fondly,
   Bill, your local Billy instance.""")
    tree = Tree()
    tree.add("README", 0100644, blob.id)
    commit = Commit()
    commit.tree = tree.id
    author = "Billy <billy@localhost>"
    commit.author = commit.committer = author
    commit.commit_time = commit.author_time = int(time())
    tz = parse_timezone('-0400')[0]
    commit.commit_timezone = commit.author_timezone = tz
    commit.encoding = "UTF-8"
    commit.message = "Initial commit"
    repo.object_store.add_object(blob)
    repo.object_store.add_object(tree)
    repo.object_store.add_object(commit)
    repo.refs['refs/heads/master'] = commit.id
Esempio n. 16
0
def TestRepo():
    checkout = Repo.init(tempfile.mkdtemp())

    with open(os.path.join(checkout.path, 'foo'), 'w') as fp:
        fp.write('monty')
    with open(os.path.join(checkout.path, 'bar'), 'w') as fp:
        fp.write('python')

    sub_path = os.path.join(checkout.path, 'sub')
    os.mkdir(sub_path)

    with open(os.path.join(sub_path, 'foo'), 'w') as fp:
        fp.write('sub_monty')
    with open(os.path.join(sub_path, 'bar'), 'w') as fp:
        fp.write('sub_python')

    checkout.stage(['foo', 'bar',
                    os.path.join('sub', 'foo'),
                    os.path.join('sub', 'bar')])
    checkout.do_commit(
        'The first commit',
        committer='John Doe <*****@*****.**>'
    )

    bare = Repo.init_bare(tempfile.mkdtemp())
    client, host_path = get_transport_and_path(checkout.path)

    refs = client.fetch(
        host_path, bare,
        determine_wants=bare.object_store.determine_wants_all,
    )
    bare["HEAD"] = refs["HEAD"]
    bare["refs/heads/master"] = refs["refs/heads/master"]

    return bare, checkout
Esempio n. 17
0
 def test_create_disk_bare_mkdir(self):
     tmp_dir = tempfile.mkdtemp()
     target_dir = os.path.join(tmp_dir, "target")
     self.addCleanup(shutil.rmtree, tmp_dir)
     repo = Repo.init_bare(target_dir, mkdir=True)
     self.assertEqual(target_dir, repo._controldir)
     self._check_repo_contents(repo, True)
Esempio n. 18
0
 def test_create_disk_bare_mkdir(self):
     tmp_dir = tempfile.mkdtemp()
     target_dir = os.path.join(tmp_dir, "target")
     self.addCleanup(shutil.rmtree, tmp_dir)
     repo = Repo.init_bare(target_dir, mkdir=True)
     self.assertEqual(target_dir, repo._controldir)
     self._check_repo_contents(repo, True)
Esempio n. 19
0
 def _get_repo(self,
               create,
               src_url=None,
               update_after_clone=False,
               bare=False):
     if create and os.path.exists(self.path):
         raise RepositoryError("Location already exist")
     if src_url and not create:
         raise RepositoryError("Create should be set to True if src_url is "
                               "given (clone operation creates repository)")
     try:
         if create and src_url:
             self._check_url(src_url)
             self.clone(src_url, update_after_clone, bare)
             return Repo(self.path)
         elif create:
             os.mkdir(self.path)
             if bare:
                 return Repo.init_bare(self.path)
             else:
                 return Repo.init(self.path)
         else:
             return Repo(self.path)
     except (NotGitRepository, OSError), err:
         raise RepositoryError(err)
Esempio n. 20
0
def clone(source, target=None, bare=False, outstream=sys.stdout):
    """Clone a local or remote git repository.

    :param source: Path or URL for source repository
    :param target: Path to target repository (optional)
    :param bare: Whether or not to create a bare repository
    :param outstream: Optional stream to write progress to
    :return: The new repository
    """
    client, host_path = get_transport_and_path(source)

    if target is None:
        target = host_path.split("/")[-1]

    if not os.path.exists(target):
        os.mkdir(target)
    if bare:
        r = Repo.init_bare(target)
    else:
        r = Repo.init(target)
    remote_refs = client.fetch(host_path, r,
        determine_wants=r.object_store.determine_wants_all,
        progress=outstream.write)
    r["HEAD"] = remote_refs["HEAD"]
    return r
Esempio n. 21
0
def repo(tmpdir):
    """Create a repo."""
    curdir = os.getcwd()
    try:
        os.chdir(str(tmpdir))
        yield Repo.init_bare(str(tmpdir))
    finally:
        os.chdir(curdir)
Esempio n. 22
0
    def test_send_pack_with_changes(self):
        local = open_repo('a.git')
        self.addCleanup(tear_down_repo, local)

        target_path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, target_path)
        with Repo.init_bare(target_path) as target:
            self.send_and_verify(b"master", local, target)
Esempio n. 23
0
 def test_create_disk_bare(self):
     tmp_dir = tempfile.mkdtemp()
     try:
         repo = Repo.init_bare(tmp_dir)
         self.assertEquals(tmp_dir, repo._controldir)
         self._check_repo_contents(repo, True)
     finally:
         shutil.rmtree(tmp_dir)
Esempio n. 24
0
    def test_send_pack_with_changes(self):
        local = open_repo('a.git')
        self.addCleanup(tear_down_repo, local)

        target_path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, target_path)
        with Repo.init_bare(target_path) as target:
            self.send_and_verify(b"master", local, target)
Esempio n. 25
0
    def init(cls, path, mkdir=False, bare=False):
        """
        Initializes a normal or bare repository. This is mostly a
        handoff to Dulwich.
        
        :param path: the path (which must be a directory) to create
                     the repository within.
        :param mkdir: if True, make a directory at **path**. Equivalent 
                      to ``mkdir [path] && cd [path] && git init``.
        :param bare: if True, create a bare repository at the path.

        :return: a ``Repo`` instance.
        """
        if bare:
            DulwichRepo.init_bare(path)
        else:
            DulwichRepo.init(path, mkdir)
        return cls(path)
Esempio n. 26
0
def create(repo_path):
    if os.path.exists(repo_path):
        return Store(repo_path)
    os.mkdir(repo_path)
    repo = Repo.init_bare(repo_path)
    tree = Tree()
    repo.object_store.add_object(tree)
    repo.do_commit(tree=tree.id, message="Initial version")
    return Store(repo_path)
Esempio n. 27
0
def create_repo(design, initialize):
    full_path = os.path.join(settings.GIT_ROOT, design.repo_path)
    os.makedirs(full_path)
    repo = Repo.init_bare(full_path)
    if initialize:
        blob = Blob.from_string(str("%s Git-a-thing design repository\n" % design.name))
        tree = Tree()
        tree.add("README", 0100644, blob.id)
        do_commit(repo, tree, [blob], "Initialize repository", settings.GITATHING_COMMITER)
Esempio n. 28
0
def create(repo_path):
    if os.path.exists(repo_path):
        return Store(repo_path)
    os.mkdir(repo_path)
    repo = Repo.init_bare(repo_path)
    tree = Tree()
    repo.object_store.add_object(tree)
    repo.do_commit(tree=tree.id, message="Initial version")
    return Store(repo_path)
Esempio n. 29
0
    def init(cls, path, mkdir=False, bare=False):
        """
        Initializes a normal or bare repository. This is mostly a
        handoff to Dulwich.
        
        :param path: the path (which must be a directory) to create
                     the repository within.
        :param mkdir: if True, make a directory at **path**. Equivalent 
                      to ``mkdir [path] && cd [path] && git init``.
        :param bare: if True, create a bare repository at the path.

        :return: a ``Repo`` instance.
        """
        if bare:
            DulwichRepo.init_bare(path)
        else:
            DulwichRepo.init(path, mkdir)
        return cls(path)
Esempio n. 30
0
def clone(source,
          target=None,
          bare=False,
          checkout=None,
          errstream=default_bytes_err_stream,
          outstream=None):
    """Clone a local or remote git repository.

    :param source: Path or URL for source repository
    :param target: Path to target repository (optional)
    :param bare: Whether or not to create a bare repository
    :param checkout: checkout branch when done?
    :param errstream: Optional stream to write progress to
    :param outstream: Optional stream to write progress to (deprecated)
    :return: The new repository
    """
    if outstream is not None:
        import warnings
        warnings.warn(
            "outstream= has been deprecated in favour of errstream=.",
            DeprecationWarning,
            stacklevel=3)
        errstream = outstream

    if checkout is None:
        checkout = (not bare)
    if checkout and bare:
        raise ValueError("checkout and bare are incompatible")
    client, host_path = get_transport_and_path(source)

    if target is None:
        target = host_path.split("/")[-1]

    if not os.path.exists(target):
        os.mkdir(target)

    if bare:
        r = Repo.init_bare(target)
    else:
        r = Repo.init(target)
    try:
        remote_refs = client.fetch(
            host_path,
            r,
            determine_wants=r.object_store.determine_wants_all,
            progress=errstream.write)
        if checkout:
            if b"HEAD" in remote_refs:
                errstream.write(b'Checking out HEAD\n')
                r[b"HEAD"] = remote_refs[b"HEAD"]
                r.reset_index()
    except:
        r.close()
        raise

    return r
Esempio n. 31
0
def init_team(name):
    repo_dir = "/tmp/mepc/{}.git".format(name)
    os.mkdir(repo_dir)
    Repo.init_bare(repo_dir)
    for r, d, f in os.walk(repo_dir):
        os.chmod(r, 0777)
    hook_name = "{dir}/hooks/post-receive".format(dir=repo_dir)
    with open(hook_name, "w") as hook_file:
        hook_file.write(render_template("files/post-receive.py", team=name))
    os.chmod(hook_name, 0755)
    deploy_dir = "/home/pchaussalet/projects/mepc/filer/deploy/{}".format(name)
    os.mkdir(deploy_dir)
    os.chmod(deploy_dir, 0777)
    if name == "demo":
        dhcp = "00"
    else:
        dhcp = "0{}".format(max(map(lambda x: int(x), redis.hvals("dhcp"))) + 1)
    redis.hmset("dhcp", {name: dhcp})
    redis.hmset("teams", {name: 0})
Esempio n. 32
0
def bjadd(ui, repo):
    # we need to do whatever it is bananjour add does. i suspect
    # it's just a git init. if so, do the same, and print a message
    if hg_bananajour_curr_repo:
        bj_repo_dir = os.path.expanduser('~/.bananajour/repositories/')
        repo_dir = bj_repo_dir + hg_bananajour_reponame + '.git'
        if os.path.isdir(repo_dir):
            ui.write ("This repository already exists!")
            ui.write ("If you want to push to this repository, try hg push bananajour")
            ui.write ("Otherwise, delete it before re-creating")
            exit()
        else:
            os.makedirs(repo_dir)
            Repo.init_bare(repo_dir)
            ui.write ("Repository added to bananajour")
            ui.write ("Run hg push bananajour to add your commits, and to update!")
    else:
        ui.write("You need to run this command from within a repository")
        exit()
Esempio n. 33
0
    def fetch(self, retry=False):
        def on_error(e):
            self.source.log.exception('failed fetching repository')
            with transaction:
                self.source.last_error = 'failed fetching repository: {}'.format(e)
            return False

        old_version = self.source.version
        try:
            repo = self.source._open_repo()
            if repo is None:
                p = self.source.basepath
                if not os.path.exists(p):
                    os.makedirs(p)
                repo = Repo.init_bare(p)

            client, host_path = get_transport_and_path(self.source.url)
            remote_refs = client.fetch(host_path, repo)
            repo["HEAD"] = remote_refs["HEAD"]
        except (KeyboardInterrupt, SystemExit, gevent.GreenletExit):
            self.source.unlink()  # it is possible that the clone process is broken when the operation was interrupted
            raise
        except BaseException as e:
            if retry:
                return on_error(e)
            self.source.log.exception('failed fetching repository; deleting repo')
            del repo
            try:
                really_clean_repo(self.source.basepath)
            except:
                m = re.match('^(.+)-tmp(\d+)$', self.source.basepath)
                if m:
                    basepath = m.group(1)
                    tmp = int(m.group(2)) + 1
                else:
                    basepath = self.source.basepath
                    tmp = 1
                while True:
                    p = '{}-tmp{}'.format(basepath, tmp)
                    if not os.path.exists(p):
                        break
                    tmp += 1
                self.source.log.error('failed deleting broken repo, trying alternative base path {}'.format(p))
                self.source.basepath = p
            return self.fetch(True)
        #except BaseException as e:
        #    return on_error(e)
        else:
            self.source.log.debug('fetch complete; fetched ({})'.format(', '.join(remote_refs)))
            new_version = self.source.version
            if old_version == new_version:
                return False
            self.source.log.info('updated branch {} from {} to {}'.format(self.source.get_branch(), old_version, new_version))
            return True
Esempio n. 34
0
def create_repo(design, initialize):
    full_path = os.path.join(settings.GIT_ROOT, design.repo_path)
    os.makedirs(full_path)
    repo = Repo.init_bare(full_path)
    if initialize:
        blob = Blob.from_string(
            str("%s Git-a-thing design repository\n" % design.name))
        tree = Tree()
        tree.add("README", 0100644, blob.id)
        do_commit(repo, tree, [blob], "Initialize repository",
                  settings.GITATHING_COMMITER)
Esempio n. 35
0
    def test_clone_from_dulwich_empty(self):
        old_repo_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, old_repo_dir)
        self._old_repo = Repo.init_bare(old_repo_dir)
        port = self._start_server(self._old_repo)

        new_repo_base_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, new_repo_base_dir)
        new_repo_dir = os.path.join(new_repo_base_dir, "empty_new")
        run_git_or_fail(["clone", self.url(port), new_repo_dir], cwd=new_repo_base_dir)
        new_repo = Repo(new_repo_dir)
        self.assertReposEqual(self._old_repo, new_repo)
Esempio n. 36
0
def setup_repo(instance):
    if os.path.exists(instance.path):
        # If the repository exists, archive it
        logger.warning("Path %s exists. Archiving it.", instance.path)
        archive_directory(instance.path)
        logger.info("Directory archived.")

    # If we have an abc/dev.git repo, create the "abc" directory
    # if necessary
    if '/' in instance.name:
        logger.info("Repository requires a subdirectory")
        if not os.path.exists(os.path.dirname(instance.path)):
            os.mkdir(os.path.dirname(instance.path))
            logger.info("Created parent directory")
        else:
            logger.info("Parent directory exists")

    # Create the path for the repository
    os.mkdir(instance.path)
    logger.info("Created path for repo at %s", instance.path)

    if instance.forked_from is not None:
        # Make sure the repo we're forking from exists
        task_id = instance.forked_from.task_id
        if task_id is not None:
            # Wait for the repo to be created
            msg = "Wait for {}'s parent repository ({}) to be created..."
            logger.info(msg.format(instance.name, instance.forked_from.name))
            AsyncResult(task_id).wait()

        # If we're forking a repo, clone from the parent
        logger.info("Cloning repo to %s", instance.path)
        instance.forked_from.repo.clone(instance.path,
                                        mkdir=False, bare=True)
        logger.info("Cloned repo to %s", instance.path)
    else:
        # If we're not forking a repo, just init a new one
        logger.info("Initializing repo at %s", instance.path)
        DulwichRepo.init_bare(instance.path)
        logger.info("Initialized repo at %s", instance.path)
Esempio n. 37
0
    def test_clone_from_dulwich_empty(self):
        old_repo_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, old_repo_dir)
        self._old_repo = Repo.init_bare(old_repo_dir)
        port = self._start_server(self._old_repo)

        new_repo_base_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, new_repo_base_dir)
        new_repo_dir = os.path.join(new_repo_base_dir, 'empty_new')
        run_git_or_fail(['clone', self.url(port), new_repo_dir],
                        cwd=new_repo_base_dir)
        new_repo = Repo(new_repo_dir)
        self.assertReposEqual(self._old_repo, new_repo)
Esempio n. 38
0
def setup_repo(instance):
    if os.path.exists(instance.path):
        # If the repository exists, archive it
        logger.warning("Path %s exists. Archiving it.", instance.path)
        archive_directory(instance.path)
        logger.info("Directory archived.")

    # If we have an abc/dev.git repo, create the "abc" directory
    # if necessary
    if '/' in instance.name:
        logger.info("Repository requires a subdirectory")
        if not os.path.exists(os.path.dirname(instance.path)):
            os.mkdir(os.path.dirname(instance.path))
            logger.info("Created parent directory")
        else:
            logger.info("Parent directory exists")

    # Create the path for the repository
    os.mkdir(instance.path)
    logger.info("Created path for repo at %s", instance.path)

    if instance.forked_from is not None:
        # Make sure the repo we're forking from exists
        task_id = instance.forked_from.task_id
        if task_id is not None:
            # Wait for the repo to be created
            msg = "Wait for {}'s parent repository ({}) to be created..."
            logger.info(msg.format(instance.name, instance.forked_from.name))
            AsyncResult(task_id).wait()

        # If we're forking a repo, clone from the parent
        logger.info("Cloning repo to %s", instance.path)
        instance.forked_from.repo.clone(instance.path, mkdir=False, bare=True)
        logger.info("Cloned repo to %s", instance.path)
    else:
        # If we're not forking a repo, just init a new one
        logger.info("Initializing repo at %s", instance.path)
        DulwichRepo.init_bare(instance.path)
        logger.info("Initialized repo at %s", instance.path)
Esempio n. 39
0
def init(path=".", bare=False):
    """Create a new git repository.

    :param path: Path to repository.
    :param bare: Whether to create a bare repository.
    :return: A Repo instance
    """
    if not os.path.exists(path):
        os.mkdir(path)

    if bare:
        return Repo.init_bare(path)
    else:
        return Repo.init(path)
Esempio n. 40
0
def init(path=".", bare=False):
    """Create a new git repository.

    :param path: Path to repository.
    :param bare: Whether to create a bare repository.
    :return: A Repo instance
    """
    if not os.path.exists(path):
        os.mkdir(path)

    if bare:
        return Repo.init_bare(path)
    else:
        return Repo.init(path)
Esempio n. 41
0
def clone(source, target=None, bare=False, checkout=None, errstream=OUTPUT_STREAM, outstream=None):
    """Clone a local or remote git repository.

    :param source: Path or URL for source repository
    :param target: Path to target repository (optional)
    :param bare: Whether or not to create a bare repository
    :param errstream: Optional stream to write progress to
    :param outstream: Optional stream to write progress to (deprecated)
    :return: The new repository
    """
    if outstream is not None:
        import warnings
        warnings.warn("outstream= has been deprecated in favour of errstream=.", DeprecationWarning,
                stacklevel=3)
        errstream = outstream

    if checkout is None:
        checkout = (not bare)
    if checkout and bare:
        raise ValueError("checkout and bare are incompatible")
    client, host_path = get_transport_and_path(source)

    if target is None:
        target = host_path.split("/")[-1]

    if not os.path.exists(target):
        os.mkdir(target)

    if bare:
        r = Repo.init_bare(target)
    else:
        r = Repo.init(target)
    try:
        remote_refs = client.fetch(host_path, r,
            determine_wants=r.object_store.determine_wants_all,
            progress=errstream.write)
        r[b"HEAD"] = remote_refs[b"HEAD"]
        if checkout:
            errstream.write(b'Checking out HEAD\n')
            r.reset_index()
    except:
        r.close()
        raise

    return r
Esempio n. 42
0
    def __init__(self, path, init = False):
        if init :
          os.makedirs(path)
          repo = Repo.init_bare(path)
        else:
          repo = Repo(path)


        self._repo    = repo
        self.path     = path
        self.branches = self._getBranches() 

        try:
            head = repo.head()
            commitLast = repo.commit(head)
            
            self.head     = {
                'sha'    : head,
                'tree'   : commitLast.tree
            }
        except KeyError:
            self.head = None
Esempio n. 43
0
    def __init__(self, name=None, **options):
        self.name = name or uuid.uuid4().hex
        self.options = dict(DEFAULTS, **options)
        self.data = self.options.pop('data').format(name=self.name)
        new = False

        self.repo = None
        if not os.path.exists(self.data):
            if not self.options['create']: raise GameError("Game does not exist")
            os.makedirs(self.data)

        try:
            self.repo = Repo(self.data)
        except dulwich.errors.NotGitRepository:
            if not self.options['create']: raise GameError("Game does not exist")
            self.repo = Repo.init_bare(self.data)
            new = True


        self.board = (new and BoardState()) or self.get_board()

        if new: self.save("New blank board for game: %s" % self.name)
Esempio n. 44
0
 def _get_repo(self, create, src_url=None, update_after_clone=False,
               bare=False):
     if create and os.path.exists(self.path):
         raise RepositoryError("Location already exist")
     if src_url and not create:
         raise RepositoryError("Create should be set to True if src_url is "
                               "given (clone operation creates repository)")
     try:
         if create and src_url:
             GitRepository._check_url(src_url)
             self.clone(src_url, update_after_clone, bare)
             return Repo(self.path)
         elif create:
             os.mkdir(self.path)
             if bare:
                 return Repo.init_bare(self.path)
             else:
                 return Repo.init(self.path)
         else:
             return self._repo
     except (NotGitRepository, OSError), err:
         raise RepositoryError(err)
Esempio n. 45
0
 def createRepository(self, path, bare=False):
     if bare:
         GitRepo.init_bare(path)
     else:
         GitRepo.init(path)
Esempio n. 46
0
def clone(source,
          target=None,
          bare=False,
          checkout=None,
          errstream=default_bytes_err_stream,
          outstream=None,
          origin=b"origin",
          depth=None,
          **kwargs):
    """Clone a local or remote git repository.

    :param source: Path or URL for source repository
    :param target: Path to target repository (optional)
    :param bare: Whether or not to create a bare repository
    :param checkout: Whether or not to check-out HEAD after cloning
    :param errstream: Optional stream to write progress to
    :param outstream: Optional stream to write progress to (deprecated)
    :param origin: Name of remote from the repository used to clone
    :param depth: Depth to fetch at
    :return: The new repository
    """
    # TODO(jelmer): This code overlaps quite a bit with Repo.clone
    if outstream is not None:
        import warnings
        warnings.warn(
            "outstream= has been deprecated in favour of errstream=.",
            DeprecationWarning,
            stacklevel=3)
        errstream = outstream

    if checkout is None:
        checkout = (not bare)
    if checkout and bare:
        raise ValueError("checkout and bare are incompatible")

    if target is None:
        target = source.split("/")[-1]

    if not os.path.exists(target):
        os.mkdir(target)

    if bare:
        r = Repo.init_bare(target)
    else:
        r = Repo.init(target)

    reflog_message = b'clone: from ' + source.encode('utf-8')
    try:
        fetch_result = fetch(r,
                             source,
                             origin,
                             errstream=errstream,
                             message=reflog_message,
                             depth=depth,
                             **kwargs)
        target_config = r.get_config()
        if not isinstance(source, bytes):
            source = source.encode(DEFAULT_ENCODING)
        target_config.set((b'remote', origin), b'url', source)
        target_config.set((b'remote', origin), b'fetch',
                          b'+refs/heads/*:refs/remotes/' + origin + b'/*')
        target_config.write_to_path()
        # TODO(jelmer): Support symref capability,
        # https://github.com/jelmer/dulwich/issues/485
        try:
            head = r[fetch_result[b'HEAD']]
        except KeyError:
            head = None
        else:
            r[b'HEAD'] = head.id
        if checkout and not bare and head is not None:
            errstream.write(b'Checking out ' + head.id + b'\n')
            r.reset_index(head.tree)
    except BaseException:
        r.close()
        raise

    return r
Esempio n. 47
0
 def test_create_disk_bare(self):
     tmp_dir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, tmp_dir)
     repo = Repo.init_bare(tmp_dir)
     self.assertEqual(tmp_dir, repo._controldir)
     self._check_repo_contents(repo, True)
Esempio n. 48
0
    def test_open_workingtree_bare(self):
        GitRepo.init_bare(".")

        gd = controldir.ControlDir.open('.')
        self.assertRaises(errors.NoWorkingTree, gd.open_workingtree)
Esempio n. 49
0
 def init_if_missing(self):
     if os.path.exists(self.gitdir):
         self.git = Repo(self.gitdir)
     else:
         os.mkdir(self.gitdir)
         self.git = Repo.init_bare(self.gitdir)
Esempio n. 50
0
 def init_if_missing(self):
     if os.path.exists(self.gitdir):
         self.git = Repo(self.gitdir)
     else:
         os.mkdir(self.gitdir)
         self.git = Repo.init_bare(self.gitdir)
Esempio n. 51
0
def clone(source,
          target=None,
          bare=False,
          checkout=None,
          errstream=default_bytes_err_stream,
          outstream=None,
          origin=b"origin"):
    """Clone a local or remote git repository.

    :param source: Path or URL for source repository
    :param target: Path to target repository (optional)
    :param bare: Whether or not to create a bare repository
    :param checkout: Whether or not to check-out HEAD after cloning
    :param errstream: Optional stream to write progress to
    :param outstream: Optional stream to write progress to (deprecated)
    :return: The new repository
    """
    if outstream is not None:
        import warnings
        warnings.warn(
            "outstream= has been deprecated in favour of errstream=.",
            DeprecationWarning,
            stacklevel=3)
        errstream = outstream

    if checkout is None:
        checkout = (not bare)
    if checkout and bare:
        raise ValueError("checkout and bare are incompatible")
    client, host_path = get_transport_and_path(source)

    if target is None:
        target = host_path.split("/")[-1]

    if not os.path.exists(target):
        os.mkdir(target)

    if bare:
        r = Repo.init_bare(target)
    else:
        r = Repo.init(target)
    try:
        remote_refs = client.fetch(
            host_path,
            r,
            determine_wants=r.object_store.determine_wants_all,
            progress=errstream.write)
        r.refs.import_refs(
            b'refs/remotes/' + origin, {
                n[len(b'refs/heads/'):]: v
                for (n, v) in remote_refs.items()
                if n.startswith(b'refs/heads/')
            })
        r.refs.import_refs(
            b'refs/tags', {
                n[len(b'refs/tags/'):]: v
                for (n, v) in remote_refs.items()
                if n.startswith(b'refs/tags/')
                and not n.endswith(ANNOTATED_TAG_SUFFIX)
            })
        if b"HEAD" in remote_refs and not bare:
            # TODO(jelmer): Support symref capability,
            # https://github.com/jelmer/dulwich/issues/485
            r[b"HEAD"] = remote_refs[b"HEAD"]
        target_config = r.get_config()
        if not isinstance(source, bytes):
            source = source.encode(DEFAULT_ENCODING)
        target_config.set((b'remote', b'origin'), b'url', source)
        target_config.set((b'remote', b'origin'), b'fetch',
                          b'+refs/heads/*:refs/remotes/origin/*')
        target_config.write_to_path()
        if checkout and b"HEAD" in r.refs:
            errstream.write(b'Checking out HEAD\n')
            r.reset_index()
    except:
        r.close()
        raise

    return r
Esempio n. 52
0
 def test_create_disk_bare(self):
     tmp_dir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, tmp_dir)
     repo = Repo.init_bare(tmp_dir)
     self.assertEquals(tmp_dir, repo._controldir)
     self._check_repo_contents(repo, True)
Esempio n. 53
0
def clone(source,
          target=None,
          bare=False,
          checkout=None,
          errstream=default_bytes_err_stream,
          outstream=None,
          origin=b"origin",
          **kwargs):
    """Clone a local or remote git repository.

    :param source: Path or URL for source repository
    :param target: Path to target repository (optional)
    :param bare: Whether or not to create a bare repository
    :param checkout: Whether or not to check-out HEAD after cloning
    :param errstream: Optional stream to write progress to
    :param outstream: Optional stream to write progress to (deprecated)
    :param origin: Name of remote from the repository used to clone
    :return: The new repository
    """
    # TODO(jelmer): This code overlaps quite a bit with Repo.clone
    if outstream is not None:
        import warnings
        warnings.warn(
            "outstream= has been deprecated in favour of errstream=.",
            DeprecationWarning,
            stacklevel=3)
        errstream = outstream

    if checkout is None:
        checkout = (not bare)
    if checkout and bare:
        raise ValueError("checkout and bare are incompatible")

    config = StackedConfig.default()
    client, host_path = get_transport_and_path(source, config=config, **kwargs)

    if target is None:
        target = host_path.split("/")[-1]

    if not os.path.exists(target):
        os.mkdir(target)

    if bare:
        r = Repo.init_bare(target)
    else:
        r = Repo.init(target)
    try:
        fetch_result = client.fetch(
            host_path,
            r,
            determine_wants=r.object_store.determine_wants_all,
            progress=errstream.write)
        ref_message = b"clone: from " + source.encode('utf-8')
        r.refs.import_refs(b'refs/remotes/' + origin, {
            n[len(b'refs/heads/'):]: v
            for (n, v) in fetch_result.refs.items()
            if n.startswith(b'refs/heads/')
        },
                           message=ref_message)
        r.refs.import_refs(b'refs/tags', {
            n[len(b'refs/tags/'):]: v
            for (n, v) in fetch_result.refs.items()
            if n.startswith(b'refs/tags/')
            and not n.endswith(ANNOTATED_TAG_SUFFIX)
        },
                           message=ref_message)
        target_config = r.get_config()
        if not isinstance(source, bytes):
            source = source.encode(DEFAULT_ENCODING)
        target_config.set((b'remote', origin), b'url', source)
        target_config.set((b'remote', origin), b'fetch',
                          b'+refs/heads/*:refs/remotes/' + origin + b'/*')
        target_config.write_to_path()
        # TODO(jelmer): Support symref capability,
        # https://github.com/jelmer/dulwich/issues/485
        try:
            head = r[fetch_result.refs[b"HEAD"]]
        except KeyError:
            head = None
        else:
            r[b'HEAD'] = head.id
        if checkout and not bare and head is not None:
            errstream.write(b'Checking out ' + head.id + b'\n')
            r.reset_index(head.tree)
    except BaseException:
        r.close()
        raise

    return r
Esempio n. 54
0
def open_or_init_repo(path):
    if not os.path.exists(path):
        os.mkdir(path)
        return Repo.init_bare(path)
    else:
        return Repo(path)
Esempio n. 55
0
 def repo_path(self, tmp_path):
     Repo.init_bare(str(tmp_path))
     yield str(tmp_path)
Esempio n. 56
0
 def repo_path(self):
     with TempDir() as tmpdir:
         Repo.init_bare(tmpdir)
         yield tmpdir
Esempio n. 57
0
 def init_repo(self):
     if os.path.exists(self.path):
         return
     os.mkdir(self.path)
     self._repo = Repo.init_bare(self.path)
Esempio n. 58
0
#!/usr/bin/python3

from dulwich.repo import Repo
from dulwich.cloud.gcs import GcsObjectStore

import tempfile

from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket('mybucket')

gcs_object_store = GcsObjectStore(bucket, 'path')
r = Repo.init_bare(tempfile.mkdtemp(), object_store=gcs_object_store)
Esempio n. 59
0
def clone(source, target=None, bare=False, checkout=None,
          errstream=default_bytes_err_stream, outstream=None,
          origin=b"origin", depth=None, **kwargs):
    """Clone a local or remote git repository.

    :param source: Path or URL for source repository
    :param target: Path to target repository (optional)
    :param bare: Whether or not to create a bare repository
    :param checkout: Whether or not to check-out HEAD after cloning
    :param errstream: Optional stream to write progress to
    :param outstream: Optional stream to write progress to (deprecated)
    :param origin: Name of remote from the repository used to clone
    :param depth: Depth to fetch at
    :return: The new repository
    """
    # TODO(jelmer): This code overlaps quite a bit with Repo.clone
    if outstream is not None:
        import warnings
        warnings.warn(
            "outstream= has been deprecated in favour of errstream=.",
            DeprecationWarning, stacklevel=3)
        errstream = outstream

    if checkout is None:
        checkout = (not bare)
    if checkout and bare:
        raise ValueError("checkout and bare are incompatible")

    if target is None:
        target = source.split("/")[-1]

    if not os.path.exists(target):
        os.mkdir(target)

    if bare:
        r = Repo.init_bare(target)
    else:
        r = Repo.init(target)

    reflog_message = b'clone: from ' + source.encode('utf-8')
    try:
        fetch_result = fetch(
            r, source, origin, errstream=errstream, message=reflog_message,
            depth=depth, **kwargs)
        target_config = r.get_config()
        if not isinstance(source, bytes):
            source = source.encode(DEFAULT_ENCODING)
        target_config.set((b'remote', origin), b'url', source)
        target_config.set(
            (b'remote', origin), b'fetch',
            b'+refs/heads/*:refs/remotes/' + origin + b'/*')
        target_config.write_to_path()
        # TODO(jelmer): Support symref capability,
        # https://github.com/jelmer/dulwich/issues/485
        try:
            head = r[fetch_result[b'HEAD']]
        except KeyError:
            head = None
        else:
            r[b'HEAD'] = head.id
        if checkout and not bare and head is not None:
            errstream.write(b'Checking out ' + head.id + b'\n')
            r.reset_index(head.tree)
    except BaseException:
        r.close()
        raise

    return r