近期,GitHub 推出一个新的特性 Show the CODEOWNERS for a single file
,也便是在 GitHub 上展现某个特定文件的全部者,具体效果是怎样的呢?来,跟我一块儿看看吧。python
目前,这个特性须要手动激活,操做很简单,只须要点击 GitHub 我的主页右上角的头像,选中 Feature Preview
,并在 File Codeowners
弹框中点击 Enable
便可激活。git
首先,你须要为你的 GitHub 项目添加一个名为 CODEOWNERS
文件,用于声明代码文件或文档文件对应的全部者。该文件能够放置在项目根目录或者 docs
、.github
目录下。github
CODEOWNERS
文件内容的书写格式与 .gitignore
一致,GitHub 官方给出了一个细致的示例:bash
# This is a comment.
# Each line is a file pattern followed by one or more owners.
# These owners will be the default owners for everything in
# the repo. Unless a later match takes precedence,
# @global-owner1 and @global-owner2 will be requested for
# review when someone opens a pull request.
* @global-owner1 @global-owner2
# Order is important; the last matching pattern takes the most
# precedence. When someone opens a pull request that only
# modifies JS files, only @js-owner and not the global
# owner(s) will be requested for a review.
*.js @js-owner
# You can also use email addresses if you prefer. They'll be
# used to look up users just like we do for commit author
# emails.
*.go docs@example.com
# In this example, @doctocat owns any files in the build/logs
# directory at the root of the repository and any of its
# subdirectories.
/build/logs/ @doctocat
# The `docs/*` pattern will match files like
# `docs/getting-started.md` but not further nested files like
# `docs/build-app/troubleshooting.md`.
docs/* docs@example.com
# In this example, @octocat owns any file in an apps directory
# anywhere in your repository.
apps/ @octocat
# In this example, @doctocat owns any file in the `/docs`
# directory in the root of your repository.
/docs/ @doctocat
复制代码
依据上面 GitHub 官方示例,我简单为个人项目添加一个 CODEOWNERS
文件,放置在项目根目录下,文件内容以下:app
*.py @yanglbme
*.java @yanglbme
docs/ @yanglbme
java/ @yanglbme
python/ @yanglbme
复制代码
这样写其实就是告诉 GitHub,在本项目中,全部以 .py
或者 .java
后缀命名的文件,以及 docs
、java
、python
文件夹下的全部文件,都归 GitHub 用户 yanglbme
全部。less
点击 GitHub 项目 AutoComplete.java
文件,能够看到代码行数左侧有个 Owner
的 logo,光标移动到 logo,能够看到显示 You own this file
,说明特性已生效。ui
固然,若是是非 Owner,会看到这个文件是显示 Owned by yanglbme
。this
注意:不管是不是文件的 Owner,都能看到特性效果,前提是你须要先
Enable
激活才行。spa
CODEOWNERS
文件其实还有另外的用途,就是用在代码的 Review 上。若是你是项目的全部者,那么你能够在 Settings 中设置分支保护,设置提交须要通过代码全部者 Code Owners
审查。
一旦有其余用户对你的文件作出修改,GitHub 便会自动发起 review 请求,请求文件 Owner 进行 review。
怎么样,是否是又 Get 了一个新技巧?关注我公众号“Doocs开源社区”,原创技术类文章第一时间推送。