git-重命名所有默认分支

发布于 2025-01-20 04:55:15 字数 383 浏览 2 评论 0原文

我使用gitlab进行源控制。 Gitlab最近将使用main切换为新存储库的默认分支,如上所述在这里,这很好,我已经调整了我的开发环境以匹配。

我也想更改现有的存储库。 <步骤和仅一次用于一个存储库,更不用说复制历史的步骤了。由于我们的代码库由数百个存储库组成,因此这是不可行的。

有没有办法在Gitlab或Gitbash中自动化这一点?

I use Gitlab for source control. Gitlab recently made the switch to using main as the default branch for new repositories, as described here, which is fine, and I've adjusted my development environments to match.

I would like to change my existing repositories as well. This tutorial describes a way to do this, but involves multiple manual steps and only works on one repository at a time, not to mention the steps of copying over history. As our code base is comprised of hundreds of repositories, this is just not feasible.

Is there a way to automate this in Gitlab or Gitbash?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2025-01-27 04:55:15

这只是从 master 创建一个名为 main 的新分支,删除 master 分支,并调整默认/受保护分支规则的问题。这些操作可以使用 GitLab API 执行,它允许您编写这些操作的脚本。

基本步骤和 API 参考:

  1. 使用 创建分支 API 从 master 创建 main 分支

  2. 使用编辑项目API 设置默认分支到 main

  3. 使用保护分支API 保护主要

  4. (可选)使用 取消保护分支 API 删除 master 的保护和/或使用 删除分支 API 以删除 master 分支。

当然,您必须拥有足够的权限才能在项目中执行所有这些操作。

最后,您还可以使用列出项目 API列出所有项目并循环结果以将其应用到所有项目 - 您可以为每个项目编写一个类似 的脚本; 执行步骤 1-4

使用 python-gitlab< /code> 库,像这样的脚本应该可以正确执行所有这些步骤:

import gitlab

gl = gitlab.Gitlab("https://gitlab.example.com", private_token="your token")

# add your project IDs here
all_projects = [gl.projects.get(1234), gl.projects.get(4567), ...]

# or list all projects programatically: (careful if using gitlab.com!)
# all_projects = list(gl.projects.list(as_list=False))


def get_or_create_main_branch(project):
    try:
        b = project.branches.get("main")
        print("main branch already exists")
        return b
    except gitlab.exceptions.GitlabError:
        print("Creating main branch from ref:", project.default_branch)
        project.branches.create({"branch": "main", "ref": project.default_branch})
        print("Creating protection rule for main branch")
        # Edit access levels per your needs
        project.protectedbranches.create(
            {
                "name": "main",
                "merge_access_level": gitlab.const.DEVELOPER_ACCESS,
                "push_access_level": gitlab.const.MAINTAINER_ACCESS,
            }
        )
        return project.branches.get("main")


def fix_project(project):
    main_branch = get_or_create_main_branch(project)
    print("setting default branch to main...", end="")
    old_default_branch = project.branches.get(project.default_branch)
    project.default_branch = "main"
    project.save()
    print("Done!")
    print("deleting original default branch", old_default_branch.name, "...", end="")
    old_default_branch.delete()
    print("done!")


for project in all_projects:
    print("-" * 20)
    print("Checking", project.path_with_namespace, "for default branch")
    if project.default_branch != "main":
        print(
            f'Default branch is "{project.default_branch}", not "main". Attempting Fix...'
        )

        try:
            fix_project(project)
        except gitlab.exceptions.GitlabError as e:
            # sometimes bare repos without any commits will error out
            print("FATAL. Failure getting or creating main branch", e)
            print("Skipping")
            continue  # move on to next project in the loop
        print("successfully fixed", project.path_with_namespace)
    else:
        print('Default branch is already "main", skipping')
        continue

This is just a matter of creating a new branch from master called main, deleting the master branch, and adjusting the default/protected branches rules. These operations can be performed using the GitLab API, which would allow you to script these actions.

Basic steps and API references:

  1. Use the create branch API to create the main branch from master

  2. Use the edit project API to set the default branch to main

  3. use the protect branch API to protect main

  4. (optionally) use the unprotect branch API to remove protections from master and/or use the delete branch API to remove the master branch.

You must, of course, have sufficient permissions to take all these actions in the project.

Finally, you can also use the list projects API to list all of your projects and loop over the results to apply this to all your projects -- you can write a script like for each project; do steps 1-4

Using the python-gitlab library, a script like this should be about right to do all those steps:

import gitlab

gl = gitlab.Gitlab("https://gitlab.example.com", private_token="your token")

# add your project IDs here
all_projects = [gl.projects.get(1234), gl.projects.get(4567), ...]

# or list all projects programatically: (careful if using gitlab.com!)
# all_projects = list(gl.projects.list(as_list=False))


def get_or_create_main_branch(project):
    try:
        b = project.branches.get("main")
        print("main branch already exists")
        return b
    except gitlab.exceptions.GitlabError:
        print("Creating main branch from ref:", project.default_branch)
        project.branches.create({"branch": "main", "ref": project.default_branch})
        print("Creating protection rule for main branch")
        # Edit access levels per your needs
        project.protectedbranches.create(
            {
                "name": "main",
                "merge_access_level": gitlab.const.DEVELOPER_ACCESS,
                "push_access_level": gitlab.const.MAINTAINER_ACCESS,
            }
        )
        return project.branches.get("main")


def fix_project(project):
    main_branch = get_or_create_main_branch(project)
    print("setting default branch to main...", end="")
    old_default_branch = project.branches.get(project.default_branch)
    project.default_branch = "main"
    project.save()
    print("Done!")
    print("deleting original default branch", old_default_branch.name, "...", end="")
    old_default_branch.delete()
    print("done!")


for project in all_projects:
    print("-" * 20)
    print("Checking", project.path_with_namespace, "for default branch")
    if project.default_branch != "main":
        print(
            f'Default branch is "{project.default_branch}", not "main". Attempting Fix...'
        )

        try:
            fix_project(project)
        except gitlab.exceptions.GitlabError as e:
            # sometimes bare repos without any commits will error out
            print("FATAL. Failure getting or creating main branch", e)
            print("Skipping")
            continue  # move on to next project in the loop
        print("successfully fixed", project.path_with_namespace)
    else:
        print('Default branch is already "main", skipping')
        continue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文