git-patch-cli 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
git_patch_cli/main.py
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import typer
|
|
2
|
+
import subprocess
|
|
3
|
+
import os
|
|
4
|
+
import json
|
|
5
|
+
import shutil
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import glob
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
app = typer.Typer(help="Git Patch CLI - A tool to generate and apply patches")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@app.command(help="Generate a patch using git format-patch")
|
|
14
|
+
def patch(
|
|
15
|
+
from_commit: str = typer.Option("root", "--from", "-f", help="指定 commit id"),
|
|
16
|
+
source_dir:str = typer.Option("./", "--source-dir", help="指定 git repo 路径"),
|
|
17
|
+
one_patch:bool = typer.Option("false", "--one-patch", help="只生成一个 patch 文件")
|
|
18
|
+
):
|
|
19
|
+
try:
|
|
20
|
+
git_dir = os.path.join(source_dir, ".git")
|
|
21
|
+
if not os.path.isdir(git_dir):
|
|
22
|
+
typer.echo(f"Error: {source_dir} is not a git repository", err=True)
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
source_dir = os.path.abspath(source_dir)
|
|
26
|
+
output_dir = os.path.join(source_dir, ".git-patch")
|
|
27
|
+
patch_json_path = os.path.join(output_dir, "patch.json")
|
|
28
|
+
|
|
29
|
+
commit_id = None
|
|
30
|
+
number:int = 0
|
|
31
|
+
|
|
32
|
+
if os.path.exists(patch_json_path):
|
|
33
|
+
with open(patch_json_path, "r") as f:
|
|
34
|
+
data = json.load(f)
|
|
35
|
+
commit_id = data.get("commit_id", None)
|
|
36
|
+
number = data.get("number", number)
|
|
37
|
+
typer.echo(f"Using commit id from patch.json: {commit_id}")
|
|
38
|
+
else:
|
|
39
|
+
if from_commit is None:
|
|
40
|
+
typer.echo("Error: patch.json not found and --from argument is not provided", err=True)
|
|
41
|
+
return
|
|
42
|
+
commit_id = from_commit
|
|
43
|
+
typer.echo(f"Using commit id from --from argument: {commit_id}")
|
|
44
|
+
|
|
45
|
+
result = subprocess.run(
|
|
46
|
+
["git", "rev-parse", "HEAD"],
|
|
47
|
+
check=True,
|
|
48
|
+
capture_output=True,
|
|
49
|
+
text=True
|
|
50
|
+
)
|
|
51
|
+
current_commit_id = result.stdout.strip()
|
|
52
|
+
if commit_id != "root":
|
|
53
|
+
if commit_id == current_commit_id:
|
|
54
|
+
typer.echo(f"{commit_id} 已经是最新的提交记录,无需生成补丁")
|
|
55
|
+
return
|
|
56
|
+
|
|
57
|
+
number += 1
|
|
58
|
+
|
|
59
|
+
patchs_dir = os.path.join(output_dir, "patchs", f"{number:04d}")
|
|
60
|
+
if os.path.isdir(patchs_dir):
|
|
61
|
+
shutil.rmtree(patchs_dir)
|
|
62
|
+
|
|
63
|
+
os.makedirs(patchs_dir, exist_ok=True)
|
|
64
|
+
|
|
65
|
+
if one_patch:
|
|
66
|
+
out_file_path = os.path.join(patchs_dir, f"{number:04d}.patch")
|
|
67
|
+
cmd = [
|
|
68
|
+
"git", "format-patch", f"{commit_id}", "--stdout"
|
|
69
|
+
]
|
|
70
|
+
if commit_id == "root":
|
|
71
|
+
cmd = [
|
|
72
|
+
"git", "format-patch", "--root", "--stdout"
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
with open(out_file_path, "w") as f:
|
|
76
|
+
subprocess.run(
|
|
77
|
+
cmd,
|
|
78
|
+
check=True,
|
|
79
|
+
#capture_output=True,
|
|
80
|
+
#text=True,
|
|
81
|
+
cwd=source_dir,
|
|
82
|
+
stdout=f,
|
|
83
|
+
)
|
|
84
|
+
else:
|
|
85
|
+
cmd = [
|
|
86
|
+
"git", "format-patch", f"{commit_id}",
|
|
87
|
+
"--output-directory", patchs_dir
|
|
88
|
+
]
|
|
89
|
+
if commit_id == "root":
|
|
90
|
+
cmd = [
|
|
91
|
+
"git", "format-patch", "--root",
|
|
92
|
+
"--output-directory", patchs_dir
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
typer.echo(f"执行命令: {cmd}")
|
|
96
|
+
subprocess.run(
|
|
97
|
+
cmd,
|
|
98
|
+
check=True,
|
|
99
|
+
#capture_output=True,
|
|
100
|
+
#text=True,
|
|
101
|
+
cwd=source_dir,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
meta_file_path = os.path.join(patchs_dir, "meta.json")
|
|
105
|
+
with open(meta_file_path, "w") as f:
|
|
106
|
+
m = {
|
|
107
|
+
"from_commit_id": commit_id,
|
|
108
|
+
"to_commit_id": current_commit_id,
|
|
109
|
+
"number": number,
|
|
110
|
+
}
|
|
111
|
+
json.dump(m, f, indent=2)
|
|
112
|
+
|
|
113
|
+
with open(patch_json_path, "w") as f:
|
|
114
|
+
m = {
|
|
115
|
+
"commit_id": current_commit_id,
|
|
116
|
+
"number": number
|
|
117
|
+
}
|
|
118
|
+
json.dump(m, f, indent=2)
|
|
119
|
+
|
|
120
|
+
typer.echo(f"Patch generated successfully in '{output_dir}'")
|
|
121
|
+
typer.echo(f"Commit id: {commit_id}")
|
|
122
|
+
typer.echo(f"Number: {number}")
|
|
123
|
+
typer.echo(f"patch.json written to '{patch_json_path}'")
|
|
124
|
+
|
|
125
|
+
except subprocess.CalledProcessError as e:
|
|
126
|
+
typer.echo(f"Error executing git command: {e.stderr}", err=True)
|
|
127
|
+
raise typer.Exit(code=1)
|
|
128
|
+
except Exception as e:
|
|
129
|
+
typer.echo(f"Error: {str(e)}", err=True)
|
|
130
|
+
raise typer.Exit(code=1)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# @app.command(help="Apply a patch to target")
|
|
134
|
+
# def apply(
|
|
135
|
+
# patch_file: str = typer.Option(..., "--patch", "-p", help="Patch file to apply"),
|
|
136
|
+
# target: str = typer.Option(".", "--target", "-t", help="Target directory or file")
|
|
137
|
+
# ):
|
|
138
|
+
# typer.echo(f"Applying patch '{patch_file}' to '{target}'...")
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@app.command(help="apply all patch to target")
|
|
142
|
+
def apply_all(
|
|
143
|
+
source_dir:str = typer.Option("./", "--source-dir", help="指定 git repo 路径"),
|
|
144
|
+
apply_patch_dir: str = typer.Option(..., "--patch-dir", "-p", help="Patch file to apply"),
|
|
145
|
+
):
|
|
146
|
+
source_dir = os.path.abspath(source_dir)
|
|
147
|
+
apply_patch_dir = os.path.abspath(apply_patch_dir)
|
|
148
|
+
|
|
149
|
+
git_dir = os.path.join(source_dir, ".git")
|
|
150
|
+
if not os.path.isdir(git_dir):
|
|
151
|
+
typer.echo(f"Error: {source_dir} is not a git repository", err=True)
|
|
152
|
+
return
|
|
153
|
+
|
|
154
|
+
if not os.path.isdir(apply_patch_dir):
|
|
155
|
+
typer.echo(f"Error: {apply_patch_dir} 不是一个目录", err=True)
|
|
156
|
+
return
|
|
157
|
+
|
|
158
|
+
meta_file_path = os.path.join(apply_patch_dir, "meta.json")
|
|
159
|
+
if not os.path.isfile(meta_file_path):
|
|
160
|
+
typer.echo(f"Error: {apply_patch_dir} 不是一个有效的 patch 目录", err=True)
|
|
161
|
+
return
|
|
162
|
+
|
|
163
|
+
patch_file_list = glob.glob("*.patch", root_dir=apply_patch_dir)
|
|
164
|
+
if len(patch_file_list) == 0:
|
|
165
|
+
typer.echo(f"Error: {apply_patch_dir} 不是一个有效的 patch 目录", err=True)
|
|
166
|
+
return
|
|
167
|
+
|
|
168
|
+
cmd = ["git", "diff", "--name-only"]
|
|
169
|
+
result = subprocess.check_output(cmd, cwd=source_dir)
|
|
170
|
+
if result:
|
|
171
|
+
typer.echo("有文件被修改过,请先提交,再 apply patch")
|
|
172
|
+
return
|
|
173
|
+
|
|
174
|
+
from_commit_id = ""
|
|
175
|
+
to_commit_id = ""
|
|
176
|
+
with open(meta_file_path, "r") as f:
|
|
177
|
+
data = json.load(f)
|
|
178
|
+
from_commit_id = data.get("from_commit_id", "")
|
|
179
|
+
to_commit_id = data.get("to_commit_id", "")
|
|
180
|
+
|
|
181
|
+
git_patch_dir = os.path.join(source_dir, ".git-patch")
|
|
182
|
+
apply_json_file = os.path.join(git_patch_dir, "apply.json")
|
|
183
|
+
if os.path.isfile(apply_json_file):
|
|
184
|
+
with open(apply_json_file, "r") as f:
|
|
185
|
+
data = json.load(f)
|
|
186
|
+
commit_id = data.get("apply_commit_id", "")
|
|
187
|
+
if commit_id != from_commit_id:
|
|
188
|
+
typer.echo("上次 apply 的commit id 不一致,拒绝 apply patch", err=True)
|
|
189
|
+
return
|
|
190
|
+
|
|
191
|
+
try:
|
|
192
|
+
for i in range(1, len(patch_file_list)+1):
|
|
193
|
+
patch_file_path = os.path.join(apply_patch_dir, f"{i:04d}.patch")
|
|
194
|
+
if not os.path.isfile(patch_file_path):
|
|
195
|
+
file_list = glob.glob(f"{i:04d}-*.patch", root_dir=apply_patch_dir)
|
|
196
|
+
if len(file_list) == 0:
|
|
197
|
+
typer.echo(f"没有找到 {i:04d} patch 文件", err=True)
|
|
198
|
+
return
|
|
199
|
+
name = file_list[0]
|
|
200
|
+
patch_file_path = os.path.join(apply_patch_dir, name)
|
|
201
|
+
|
|
202
|
+
cmd = ["git", "apply", patch_file_path]
|
|
203
|
+
typer.echo(f"开始 apply patch {patch_file_path}")
|
|
204
|
+
subprocess.check_call(cmd, cwd=source_dir)
|
|
205
|
+
typer.echo(f"完成 apply patch {patch_file_path}")
|
|
206
|
+
|
|
207
|
+
except Exception as e:
|
|
208
|
+
typer.echo(f"Error: {str(e)}", err=True)
|
|
209
|
+
typer.echo("apply patch 发生异常,回滚仓库")
|
|
210
|
+
cmd = ["git", "checkout", "."]
|
|
211
|
+
subprocess.check_call(cmd, cwd=source_dir)
|
|
212
|
+
raise typer.Exit(code=1)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
os.makedirs(git_patch_dir, exist_ok=True)
|
|
216
|
+
|
|
217
|
+
with open(apply_json_file, "w") as f:
|
|
218
|
+
m = {
|
|
219
|
+
"apply_commit_id":to_commit_id
|
|
220
|
+
}
|
|
221
|
+
json.dump(m, f, indent=2)
|
|
222
|
+
typer.echo(f"完成 apply {apply_patch_dir}")
|
|
223
|
+
|
|
224
|
+
if __name__ == "__main__":
|
|
225
|
+
app()
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
git_patch_cli/__init__.py,sha256=hJU2Vs2WWtHw-_Sro4644ZbROC-BsT0zx3IGWvuRw_Q,25
|
|
2
|
+
git_patch_cli/main.py,sha256=B-PfAHwOSMbssYCYxw1J7m07lsI1RIb9V9zPG-6l_Zk,8293
|
|
3
|
+
git_patch_cli-0.2.0.dist-info/METADATA,sha256=GCIMqAJUMLXkxY_l2EbWeORvQoEMs16KZdRku1RF1rU,144
|
|
4
|
+
git_patch_cli-0.2.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
5
|
+
git_patch_cli-0.2.0.dist-info/entry_points.txt,sha256=Etpj7tQV3sj5zNIrkKMvq4H-mYdGzKguOi4-rSuTdQY,57
|
|
6
|
+
git_patch_cli-0.2.0.dist-info/RECORD,,
|