Cloning only the main branch using PyGit2

混江龙づ霸主 提交于 2019-12-13 07:36:59

问题


I want to clone some remote repositories, but only retrieving the main branch.

My code currently gets all of the branches.

def init_remote(repo, name, url):
    # Create the remote with a mirroring url
    remote = repo.remotes.create(name, url, "+refs/*:refs/*")
    # And set the configuration option to true for the push command
    mirror_var = "remote.{}.mirror".format(name)
    repo.config[mirror_var] = True
    # Return the remote, which pygit2 will use to perform the clone
    return remote

pygit2.clone_repository(url, "../../clones/"+location, remote=init_remote)

回答1:


Your code doesn't just get all the branches, it mirrors the remote, getting its remote-tracking branches as well, which can lead to some confusing layout.

You're already setting your own refspec, so what you need to do is set the refspec to download the default branch. If you know it you can change the code to get just the one branch

remote = repo.remotes.create(name, url, "+refs/heads/master:refs/heads/master")


来源:https://stackoverflow.com/questions/34711540/cloning-only-the-main-branch-using-pygit2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!