forked from ywwa/ranger-gitplug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitplug.py
More file actions
213 lines (168 loc) · 9.85 KB
/
gitplug.py
File metadata and controls
213 lines (168 loc) · 9.85 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
import subprocess
import os
from ranger.api.commands import Command
class git(Command):
commands = 'init status clone add rm restore commit remote push reset checkout pull fetch diff'
def execute(self):
def is_git_repo():
output = subprocess.run(["git", "rev-parse", "--is-inside-work-tree"], capture_output=True, text=True)
if "true" not in output.stdout:
return False
return True
match self.arg(1):
case "help":
return self.fm.notify("Supported git commands: " + self.commands)
case "status":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
output = subprocess.run(["git", "status"], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
with open('/tmp/gitplug-status', 'w') as out:
out.write(output.stdout)
return self.fm.edit_file('/tmp/gitplug-status')
case "init":
output = subprocess.run(["git", "init"], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
else:
return self.fm.notify("git: " + output.stdout)
case "clone":
if not self.arg(2):
return self.fm.notify("Missing url!", bad=True)
if self.arg(2):
subprocess.run(["git", "clone", self.arg(2), "--quiet"])
return self.fm.notify("Repository successfully cloned!")
case "add":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
if not self.arg(2):
return self.fm.notify("Missing arguments! Usage :git add <file>", bad=True)
if not os.path.exists(self.arg(2)):
return self.fm.notify("File or folder does not exist", bad=True)
output = subprocess.run(["git", "add", self.arg(2)], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
return self.fm.notify("git: Successfully added files to branch!")
case "rm":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
if not self.arg(2):
return self.fm.notify("Missing arguments! Usage :git rm <file>", bad=True)
if not os.path.exists(self.arg(2)):
return self.fm.notify("File or folder does not exist", bad=True)
subprocess.run(["git", "rm", self.arg(2)], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
return self.fm.notify("git: Successfully removed files from branch!")
case "restore":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
if not self.arg(2):
return self.fm.notify("Missing arguments! Usage :git restore <file>", bad=True)
if not os.path.exists(self.arg(2)):
return self.fm.notify("File or folder does not exist", bad=True)
subprocess.run(["git", "restore", "--staged", self.arg(2), "--quiet"])
return self.fm.notify("Successfully restored files!")
case "commit":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
if not self.rest(2):
return self.fm.notify("Missing commit text", bad=True)
output = subprocess.run(["git", "commit", "-m", self.rest(2)],capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
return self.fm.notify("git: Successfully commited! " + output.stdout.splitlines()[1])
case "remote":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
match self.arg(2):
case "add":
if not self.arg(3):
return self.fm.notify("Missing name and url!", bad=True)
if not self.arg(4):
return self.fm.notify("Missing url!", bad=True)
subprocess.run(["git", "remote", "add", self.arg(3), self.arg(4)])
return self.fm.notify("Remote successfully added!")
case "rm":
if not self.arg(3):
return self.fm.notify("Missing name!", bad=True)
if self.arg(3):
subprocess.run(["git", "remote", "rm", self.arg(3)])
return self.fm.notify("Remote successfully removed")
case _:
return self.fm.notify("Missing arguments! Use: git remote add/rm <name> <url>", bad=True)
case "push":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
if self.arg(2) == "-u" and self.arg(3) and self.arg(4):
subprocess.run(["git", "push", "--quiet", "-u", self.arg(3), self.arg(4)])
return self.fm.notify("Repository successfully pushed")
if not self.arg(2):
subprocess.run(["git", "push", "--quiet"])
return self.fm.notify("Repository successfully pushed")
case "reset":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
if self.arg(2) == "--hard":
output = subprocess.run(["git", "reset", "--hard"], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
return self.fm.notify("Repository hard reset!")
if self.arg(2) == "--soft":
output = subprocess.run(["git", "reset", "--soft"], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
return self.fm.notify("Repository soft reset!")
return self.fm.notify("Usage: git reset [--hard | --soft]")
case "checkout":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
match self.arg(2):
case None:
return self.fm.notify("Usage: git checkout <branch-name> or git checkout -b <new-branch>", bad=True)
case "-b" if self.arg(3):
output = subprocess.run(["git", "checkout", "-b", self.arg(3)], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
return self.fm.notify(f"Created and switched to new branch '{self.arg(3)}'")
case _:
output = subprocess.run(["git", "checkout", self.arg(2)], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
return self.fm.notify(f"Switched to branch '{self.arg(2)}'")
case "pull":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
output = subprocess.run(["git", "pull"], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
if "Already up to date" in output.stdout:
return self.fm.notify("git: Already up to date.", bad=True)
if "CONFLICT" in output.stdout:
with open('/tmp/gitplug-conflict', 'w') as out:
out.write(output.stdout)
return self.fm.edit_file('/tmp/gitplug-conflict')
return self.fm.notify("git: Successfully pulled updates from remote repository.")
case "fetch":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
output = subprocess.run(["git", "fetch"], capture_output=True, text=True)
if output.returncode != 0:
return self.fm.notify("git: " + output.stderr, bad=True)
return self.fm.notify("git: Successfully fetched updates from remote repository.")
case "diff":
if not is_git_repo():
return self.fm.notify("git: This is not a valid git repo", bad=True)
cmd = ["git", "diff"]
if self.arg(2):
cmd.append(self.arg(2))
self.fm.ui.suspend()
process = subprocess.Popen(
cmd,
env=os.environ
)
process.wait()
self.fm.ui.initialize()
case _:
return self.fm.notify("Command not supported, use :git help for all supported commands", bad=True)