Exemplo n.º 1
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)
Exemplo n.º 2
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