获取文件上次从 Github 存储库更新的时间
Get when the file was last updated from a Github repository
我可以通过 using the GitHub v3 API. 获取文件内容(如果是文件夹,我可以获取文件列表)
示例:
https://api.github.com/repos/[Owner]/[Repository]/contents/[Folder]
但是我怎么知道文件上次更新的时间?有 API 吗?
考虑到 git 不会 store file timestamps (and other metadata like permissions and ownership), for reasons I detailed here。
因此远程存储库端(此处 GitHub)也不存在该信息。
如果你知道确切的文件路径,你可以使用 list commits on repository API 指定一个 path
,它只包括具有这个特定文件路径的提交,然后提取最近的提交(最近的是第一个):
使用 Rest API v3
curl -s "https://api.github.com/repos/bertrandmartel/speed-test-lib/commits?path=jspeedtest%2Fbuild.gradle&page=1&per_page=1" | \
jq -r '.[0].commit.committer.date'
使用 GraphqQL API v4
{
repository(owner: "bertrandmartel", name: "speed-test-lib") {
ref(qualifiedName: "refs/heads/master") {
target {
... on Commit {
history(first: 1, path: "jspeedtest/build.gradle") {
edges {
node {
committedDate
}
}
}
}
}
}
}
}
curl -s -H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type:application/json" \
-d '{
"query": "{ repository(owner: \"bertrandmartel\", name: \"speed-test-lib\") { ref(qualifiedName: \"refs/heads/master\") { target { ... on Commit { history(first: 1, path: \"jspeedtest/build.gradle\") { edges { node { committedDate } } } } } } } }"
}' https://api.github.com/graphql | \
jq -r '.data.repository.ref.target.history.edges[0].node.committedDate'
您实际上可以使用您特别提到的请求来确定您想要什么。
请注意,以下所有 dates/times 均采用 GMT(当然)。
Copy & paste the following command to find the last modified date and time of Folder/File in repository ForStackExchange by user YenForYang:
\curl -sIA. --ignore-content-length \
-H"If-Modified-Since: Sun May 01 00:00:00 9999" \
"https://api.github.com/repos/YenForYang/ForStackExchange/contents/Folder/File?ref=branch" \
| \grep -m1 -oP "(?<=Last-Modified: )[ADFJMNOSTWa-eghilnoprtuvy0-9:, ]{25}" \
(If Perl regex isn't available, you can ... | grep -F -m1 "Last-Modified:"
)
The above command should return (GMT): Thu, 27 Dec 2018 11:01:26
(or later, if I update the file for some reason)
注意,如果ref
参数未指定,ref=master
.
如果您不会复制和粘贴,并且不关心 API 速率限制,您可能会选择较短的:
\curl -sIL "api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch" | \grep "^Las"
如果你在 Windows 上没有 grep
,只需使用 find "Last-Modified: "
(需要双引号)。
如果您在 Windows 上没有 curl(下载...或)使用 Powershell
(iwr -me HEAD -usebasic "https://api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch").Headers."Last-Modified"
使用Python
pip 安装 PyGithub
from github import Github
g = Github()
repo = g.get_repo("datasets/population")
print(repo.name)
commits = repo.get_commits(path='data/population.csv')
print(commits.totalCount)
if commits.totalCount:
print(commits[0].commit.committer.date)
输出:
population
5
2020-04-14 15:09:26
我可以通过 using the GitHub v3 API. 获取文件内容(如果是文件夹,我可以获取文件列表) 示例:
https://api.github.com/repos/[Owner]/[Repository]/contents/[Folder]
但是我怎么知道文件上次更新的时间?有 API 吗?
考虑到 git 不会 store file timestamps (and other metadata like permissions and ownership), for reasons I detailed here。
因此远程存储库端(此处 GitHub)也不存在该信息。
如果你知道确切的文件路径,你可以使用 list commits on repository API 指定一个 path
,它只包括具有这个特定文件路径的提交,然后提取最近的提交(最近的是第一个):
使用 Rest API v3
curl -s "https://api.github.com/repos/bertrandmartel/speed-test-lib/commits?path=jspeedtest%2Fbuild.gradle&page=1&per_page=1" | \
jq -r '.[0].commit.committer.date'
使用 GraphqQL API v4
{
repository(owner: "bertrandmartel", name: "speed-test-lib") {
ref(qualifiedName: "refs/heads/master") {
target {
... on Commit {
history(first: 1, path: "jspeedtest/build.gradle") {
edges {
node {
committedDate
}
}
}
}
}
}
}
}
curl -s -H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type:application/json" \
-d '{
"query": "{ repository(owner: \"bertrandmartel\", name: \"speed-test-lib\") { ref(qualifiedName: \"refs/heads/master\") { target { ... on Commit { history(first: 1, path: \"jspeedtest/build.gradle\") { edges { node { committedDate } } } } } } } }"
}' https://api.github.com/graphql | \
jq -r '.data.repository.ref.target.history.edges[0].node.committedDate'
您实际上可以使用您特别提到的请求来确定您想要什么。
请注意,以下所有 dates/times 均采用 GMT(当然)。
Copy & paste the following command to find the last modified date and time of Folder/File in repository ForStackExchange by user YenForYang:
\curl -sIA. --ignore-content-length \ -H"If-Modified-Since: Sun May 01 00:00:00 9999" \ "https://api.github.com/repos/YenForYang/ForStackExchange/contents/Folder/File?ref=branch" \ | \grep -m1 -oP "(?<=Last-Modified: )[ADFJMNOSTWa-eghilnoprtuvy0-9:, ]{25}" \
(If Perl regex isn't available, you can
... | grep -F -m1 "Last-Modified:"
)
The above command should return (GMT):
Thu, 27 Dec 2018 11:01:26
(or later, if I update the file for some reason)
注意,如果ref
参数未指定,ref=master
.
如果您不会复制和粘贴,并且不关心 API 速率限制,您可能会选择较短的:
\curl -sIL "api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch" | \grep "^Las"
如果你在 Windows 上没有 grep
,只需使用 find "Last-Modified: "
(需要双引号)。
如果您在 Windows 上没有 curl(下载...或)使用 Powershell
(iwr -me HEAD -usebasic "https://api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch").Headers."Last-Modified"
使用Python
pip 安装 PyGithub
from github import Github
g = Github()
repo = g.get_repo("datasets/population")
print(repo.name)
commits = repo.get_commits(path='data/population.csv')
print(commits.totalCount)
if commits.totalCount:
print(commits[0].commit.committer.date)
输出:
population
5
2020-04-14 15:09:26