-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathgit_commands.py
More file actions
284 lines (257 loc) · 7.94 KB
/
git_commands.py
File metadata and controls
284 lines (257 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
Utility functions for git operations.
"""
from .shell_commands import run_command
def git_clone(
repo,
directory=None,
depth=None,
capture_output=True
):
"""
Clone a Git repository.
:param repo: URL of the repository to clone.
:param directory: Directory to clone into (optional).
:param depth: Depth for shallow cloning (optional).
:param capture_output: Whether to capture the command output.
"""
if directory is not None:
command = f"git clone {repo} {directory}"
else:
command = f"git clone {repo}"
if depth:
command += f" --depth {depth}"
return run_command(command, capture_output)
def git_commit(
message="update",
amend=False,
signoff=False,
capture_output=True
):
"""
Commit changes to the repository.
:param message: Commit message.
:param amend: Whether to amend the previous commit.
:param signoff: Whether to add a Signed-off-by line.
:param capture_output: Whether to capture the command output.
"""
command = "git commit"
if amend:
command += " --amend"
if signoff:
command += " --signoff"
command += f' -m "{message}"'
return run_command(command, capture_output)
def git_push(
remote_repo_name="origin",
branch="main",
force=False,
delete_existing=False,
capture_output=True,
):
"""
Push changes to a remote repository.
:param remote_repo_name: Name of the remote repository (default: "origin").
:param branch: Branch to push (default: "main").
:param force: Whether to force push.
:param delete_existing: Whether to delete the branch on the remote.
:param capture_output: Whether to capture the command output.
"""
if delete_existing:
command = f"git push {remote_repo_name} --delete {branch}"
else:
command = f"git push {remote_repo_name} {branch}"
if force:
command += " --force"
return run_command(command, capture_output)
def git_pull(
remote=None,
branch=None,
rebase=False,
capture_output=True
):
"""
Pull the latest changes from a remote repository.
:param remote: Name of the remote repository (optional).
:param branch: Branch to pull from (optional).
:param rebase: Whether to rebase instead of merging.
:param capture_output: Whether to capture the command output.
"""
command = "git pull"
if rebase:
command += " --rebase"
if remote is not None and branch is not None:
command += f" {remote} {branch}"
return run_command(command, capture_output)
def git_branch(
branch_name,
show_current=False,
create=False,
delete=False,
capture_output=True
):
"""
Handle branch operations: create, delete, or list branches.
:param branch_name: Name of the branch (required for create or delete).
:param show_current: Whether to show the current branch.
:param create: Whether to create a new branch.
:param delete: Whether to delete the branch.
:param capture_output: Whether to capture the command output.
"""
if show_current:
command = "git branch --show-current"
elif create:
command = f"git branch {branch_name}"
elif delete:
command = f"git branch -D {branch_name}"
else:
command = "git branch"
return run_command(command, capture_output)
def git_checkout(
branch_name,
create=False,
force_checkout=False,
capture_output=True
):
"""
Checkout a branch, creating it if it doesn't exist.
:param branch_name: Name of the branch to checkout.
:param force_checkout: Whether to force checkout.
:param capture_output: Whether to capture the command output.
"""
if create:
git_branch(branch_name, create=True)
if force_checkout:
command = f"git checkout -f {branch_name}"
else:
command = f"git checkout {branch_name}"
return run_command(command, capture_output)
def git_fetch(
remote=None,
branch=None
):
"""
Fetch the latest changes from a remote repository.
:param remote: Name of the remote repository (optional).
:param branch: Branch to fetch (optional).
"""
command = "git fetch"
if remote:
command += f" {remote}"
if branch:
command += f" {branch}"
return run_command(command, capture_output=True)
def git_remote(
name=None,
url=None,
remove=False,
capture_output=True
):
"""
Handle listing, adding, and removing remote repositories.
:param name: Name of the remote repository.
:param url: URL of the remote repository (required for adding).
:param remove: Whether to remove the remote repository.
:param capture_output: Whether to capture the command output.
"""
if name is not None:
if url is not None:
return run_command(f"git remote add {name} {url}", capture_output)
if remove:
return run_command(f"git remote remove {name}", capture_output)
return run_command("git remote", capture_output)
def git_merge(
branch_name,
message="Merge Branch",
no_ff=False,
signoff=False,
capture_output=True
):
"""
Merge a branch into the current branch.
:param branch_name: Name of the branch to merge.
:param message: Commit message for the merge.
:param no_ff: Whether to use a no-fast-forward merge.
:param signoff: Whether to sign off the merge.
:param capture_output: Whether to capture the command output.
"""
if signoff:
command = (
f'git merge {branch_name} --signoff -m "{message}"'
)
else:
command = f'git merge {branch_name} -m "{message}"'
if no_ff:
command += " --no-ff"
return run_command(command, capture_output)
def git_diff(
target="HEAD",
compare_with=None,
staged=False,
name_only=False,
capture_output=True
):
"""
Show differences between commits or the working directory.
:param target: Target commit or branch (default: "HEAD").
:param compare_with: Commit or branch to compare with (optional).
:param staged: Whether to show staged changes.
:param name_only: Show only changed file names.
:param capture_output: Whether to capture the command output.
"""
if compare_with:
command = f"git diff {target} {compare_with}"
else:
command = "git diff --staged" if staged else "git diff"
if name_only:
command += " --name-only"
return run_command(command, capture_output)
def git_pull_request(
title,
body="",
repo="",
base_branch="main",
head_branch=None,
capture_output=True
):
"""
Create a pull request using the GitHub CLI.
:param title: Title of the pull request.
:param body: Body/description of the pull request.
:param base_branch: Base branch for the pull request
(default: "main").
:param head_branch: Head branch for the pull request
(default: current branch).
:param capture_output: Whether to capture the command output.
"""
if head_branch is None:
head_branch = run_command(
"git rev-parse --abbrev-ref HEAD", capture_output=True
)[1]
if repo == "":
command = (
f'gh pr create --title "{title}" --body "{body}" '
f'--base {base_branch} --head {head_branch}'
)
else:
command = (
f'gh pr create --repo {repo} --title "{title}" --body "{body}" '
f'--base {base_branch} --head {head_branch}'
)
return run_command(command, capture_output)
def send_email(
to_address,
subject,
file
):
"""
Send an email using git send-email.
:param to_address: Recipient email address.
:param subject: Subject of the email.
:param file: File to attach to the email.
"""
command = (
f'git send-email --to {to_address} --subject "{subject}" '
f'--confirm=never --encoding=UTF-8 {file}'
)
return run_command(command)