近期,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開源社區」,原創技術類文章第一時間推送。