Skip to content

Commit 3435204

Browse files
committed
reassign: github verify
Signed-off-by: LetMeFly666 <814114971@qq.com>
1 parent cd2f160 commit 3435204

File tree

4 files changed

+256
-3
lines changed

4 files changed

+256
-3
lines changed

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ tags: [其他, 知识, 英语, Notes]
915915
|deform|v. 改变...的外形,损毁...的形状,使成畸形|
916916
|||
917917
|denominate|v. 以...(某种货币)为单位,将...命名为,称...为|
918+
|||
918919

919920
<p class="wordCounts">单词收录总数</p>
920921

Solutions/Other-Github-How2Make1VerifiedCommit.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Github - 如何提交一个带有“verified”标识的commit
2+
title: Github - 如何提交一个带有“verified”标识的commit/如何验证一次commit的签名
33
date: 2024-12-28 22:09:55
44
tags: [其他, Github]
55
---
66

7-
# Github - 如何提交一个带有“verified”标识的commit
7+
# Github - 如何提交一个带有“verified”标识的commit/如何验证一次commit的签名
88

99
## 前言(Why)
1010

@@ -22,7 +22,7 @@ tags: [其他, Github]
2222
>
2323
> 验证记录包含一个时间戳,用于标记验证完成的时间。 此持久记录可确保已验证状态的一致性,为存储库内的参与提供稳定的历史记录。 可以通过将鼠标悬停在 GitHub 上的“Verified”徽章上,或者通过[REST API](https://docs.github.com/zh/rest/commits/commits)访问提交(其中包含一个 verified_at 字段)来查看此时间戳。
2424
25-
## How
25+
## How 2 make 1 verify
2626

2727
因为SSH密钥对还可以当<span title="验证身份用">Authentication Key</span>,所以这里介绍如何使用SSH密钥对对一次commit签名(~~其实是因为我**暂时**不太了解GPG Key~~)。
2828

@@ -40,6 +40,52 @@ git config user.signingkey <ssh公钥路径> # 配置ssh公钥路径
4040

4141
在本地进行一次commit并push到Github,顺利的话,在Commit历史记录中就能看到Verified标了。
4242

43+
## How 2 verify 1 commit
44+
45+
我对一次commit通过ssh密钥进行了签名,那么我如何**验证**这次签名呢?
46+
47+
首先需要创建一个签名验证文件,例如`C:\Users\{username}\.ssh\allowed_signers`
48+
49+
对于一个ssh公钥(例如`C:\Users\{username}\.ssh\id_rsa.pub`),格式是这样的:
50+
51+
```
52+
ssh-rsa(也有可能是ssh-ed25519等) NzaC1lZDI1NTE5AAAAAC3NzaC1lZDI1NTE5AAAAI...很长一串... Tisfy@qq.com
53+
```
54+
55+
也就是`加密算法 公钥数据 邮箱`
56+
57+
我们要调换一下顺序为`邮箱 加密算法 公钥数据`并放到`allowed_signers`(或其他)新文件中。
58+
59+
只会在git中配置:
60+
61+
```bash
62+
git config gpg.ssh.allowedSignersFile c:/Users/{username}/.ssh/allowed_signers
63+
```
64+
65+
然后就能通过commit的hash验证了:
66+
67+
```bash
68+
git verify-commit cd2f160fc1e6d91db48b033bd6e5ac08102454ba
69+
```
70+
71+
## 如何对一个commit重新签名
72+
73+
假设你已经配置好了签名方式:
74+
75+
对最新一次提交重新签名:
76+
77+
```bash
78+
git commit --amend -S --no-edit
79+
```
80+
81+
对历史提交重新签名:
82+
83+
```bash
84+
git filter-branch -f --commit-filter 'git commit-tree -S "$@";' <commit-range>
85+
# <commit-range>是重新签名的commit范围,例如HEAD~3..HEAD
86+
# 若是当前分支的所有commit,可将<commit-range>设为HEAD
87+
```
88+
4389
## End
4490

4591
但是注意,git版本大于等于2.41才支持上述配置。

reassign.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-12-31 16:51:29
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-01-01 18:00:25
6+
'''
7+
import os
8+
import subprocess
9+
10+
11+
# 判断一个commit是否按配置签名
12+
def verify_commit(commit_hash: str) -> bool:
13+
result = subprocess.run(
14+
['git', 'verify-commit', commit_hash],
15+
stdout=subprocess.PIPE,
16+
stderr=subprocess.PIPE,
17+
)
18+
return not result.returncode
19+
20+
21+
# 获取一个commit的上一个commit
22+
def get_parent_commit(commit_hash: str) -> str:
23+
result = subprocess.run(
24+
['git', 'log', commit_hash, '--pretty=%H', '-n', '2'],
25+
stdout=subprocess.PIPE,
26+
stderr=subprocess.PIPE,
27+
)
28+
return result.stdout.decode('utf-8').strip().split()[-1]
29+
30+
31+
# 将从某次commit开始至HEAD的所有commit重新签名
32+
def re_assign(commit_hash: str) -> None:
33+
# cmd = f'git filter-branch -f --commit-filter \'git commit-tree -S "$@";\' {commit_hash}..HEAD'
34+
# print(cmd)
35+
# os.system(cmd)
36+
env = os.environ.copy()
37+
env['FILTER_BRANCH_SQUELCH_WARNING'] = "1"
38+
result = subprocess.run(
39+
["git", "filter-branch", "-f", "--commit-filter", 'git commit-tree -S "$@";', f"{commit_hash}..HEAD"],
40+
env=env,
41+
)
42+
print(result.returncode)
43+
# subprocess.Popen(cmd)
44+
45+
46+
def main():
47+
os.chdir('OtherSource/gitcode_knowledge')
48+
# re_assign('HEAD~2')
49+
# return
50+
if verify_commit('HEAD'): # HEAD的签名也能被验证
51+
return
52+
notVerified = 'HEAD'
53+
while True:
54+
next = get_parent_commit(notVerified)
55+
if next == notVerified:
56+
break
57+
notVerified = next
58+
if verify_commit(next):
59+
break
60+
print(notVerified)
61+
re_assign(notVerified)
62+
63+
64+
if __name__ == '__main__':
65+
main()

tryGoPy/d-github-reAssign.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-31 15:31:56
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-01 17:58:11
6+
*/
7+
很棒我已经完成了签名的验证现在我想要将一个commit重新签名其他信息不变
8+
可以做到吗
9+
10+
11+
12+
git本地有两个用户如何分别为其配置ssh公钥密钥
13+
14+
15+
16+
python写一个脚本
17+
18+
从HEAD开始往前遍历直到遇到可验证的签名为止
19+
将HEAD开始的所有连续的不可验证的签名重新签名
20+
21+
22+
23+
24+
git log --pretty=online可不可以指定显示数量
25+
26+
27+
28+
29+
git log --pretty=online有没有办法从某次commit开始显示
30+
31+
32+
33+
只输出commit hash
34+
35+
36+
37+
git某次commit的下一次commit
38+
39+
40+
41+
比如我HEAD~1是HEAD的上一次commit
42+
我如何表示一个commitHash的下一次commit
43+
44+
45+
46+
为何我执行`git filter-branch -f --commit-filter 'git commit-tree -S "$@";' HEAD~2..HEAD`命令的时候
47+
在终端中直接执行就能正常运行,而在python中调用os.system执行就会报错
48+
49+
```
50+
Proceeding with filter-branch...
51+
52+
fatal: ambiguous argument 'commit-tree': unknown revision or path not in the working tree.
53+
Use '--' to separate paths from revisions, like this:
54+
'git <command> [<revision>...] -- [<file>...]'
55+
```
56+
57+
58+
59+
60+
执行如下代码时报错
61+
62+
```
63+
result = subprocess.run(
64+
["git", "filter-branch", "-f", "--commit-filter", """'git commit-tree -S "$@";'""", f"{commit_hash}..HEAD"],
65+
check=True,
66+
text=True,
67+
# stdout=subprocess.PIPE,
68+
# stderr=subprocess.PIPE,
69+
)
70+
print(result.stdout)
71+
```
72+
73+
```
74+
Rewrite bde2a1b75e8881d2911ec557aa0ae97414b75a47 (1/2) (0 seconds passed, remaining 0 predicted) git commit-tree: line 52: git commit-tree -S "$@";: command not found
75+
could not write rewritten commit
76+
Traceback (most recent call last):
77+
File "F:\OtherApps\Program\Git\Store\Store20_LeetCode\reassign.py", line 65, in <module>
78+
main()
79+
File "F:\OtherApps\Program\Git\Store\Store20_LeetCode\reassign.py", line 49, in main
80+
re_assign('HEAD~2')
81+
File "F:\OtherApps\Program\Git\Store\Store20_LeetCode\reassign.py", line 36, in re_assign
82+
result = subprocess.run(
83+
File "F:\OtherApps\Program\Python\Python\lib\subprocess.py", line 528, in run
84+
raise CalledProcessError(retcode, process.args,
85+
subprocess.CalledProcessError: Command '['git', 'filter-branch', '-f', '--commit-filter', '\'git commit-tree -S "$@";\'', 'HEAD~2..HEAD']' returned non-zero exit status 1.
86+
```
87+
88+
89+
90+
91+
92+
很棒这个问题解决了
93+
但是我发现了一个新的问题在进行上述操作时会显示如下warning并卡住几秒
94+
```
95+
Store\Store20_LeetCode\OtherSource\gitcode_knowledge
96+
WARNING: git-filter-branch has a glut of gotchas generating mangled history
97+
rewrites. Hit Ctrl-C before proceeding to abort, then use an
98+
alternative filtering tool such as 'git filter-repo'
99+
(https://github.com/newren/git-filter-repo/) instead. See the
100+
filter-branch manual page for more details; to squelch this warning,
101+
set FILTER_BRANCH_SQUELCH_WARNING=1.
102+
Proceeding with filter-branch...
103+
```
104+
我有没有办法让他不显示这个提示并且不卡住
105+
106+
107+
108+
109+
```
110+
env = os.environ.copy()
111+
env['FILTER_BRANCH_SQUELCH_WARNING'] = 1
112+
result = subprocess.run(
113+
["git", "filter-branch", "-f", "--commit-filter", 'git commit-tree -S "$@";', f"{commit_hash}..HEAD"],
114+
check=True,
115+
text=True,
116+
# stdout=subprocess.PIPE,
117+
# stderr=subprocess.PIPE,
118+
env=env,
119+
)
120+
```
121+
报错
122+
```
123+
File "F:\OtherApps\Program\Git\Store\Store20_LeetCode\reassign.py", line 68, in <module>
124+
main()
125+
File "F:\OtherApps\Program\Git\Store\Store20_LeetCode\reassign.py", line 52, in main
126+
re_assign('HEAD~2')
127+
File "F:\OtherApps\Program\Git\Store\Store20_LeetCode\reassign.py", line 38, in re_assign
128+
result = subprocess.run(
129+
File "F:\OtherApps\Program\Python\Python\lib\subprocess.py", line 505, in run
130+
with Popen(*popenargs, **kwargs) as process:
131+
File "F:\OtherApps\Program\Python\Python\lib\subprocess.py", line 951, in __init__
132+
self._execute_child(args, executable, preexec_fn, close_fds,
133+
File "F:\OtherApps\Program\Python\Python\lib\subprocess.py", line 1420, in _execute_child
134+
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
135+
TypeError: environment can only contain strings
136+
```
137+
138+
139+
140+
subprocess.CalledProcessError: Command '['git', 'filter-branch', '-f', '--commit-filter', 'git commit-tree -S "$@";', 'HEAD..HEAD']' returned non-zero exit status 2.
141+
Found nothing to rewrite

0 commit comments

Comments
 (0)