-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit-status
executable file
·151 lines (140 loc) · 5.25 KB
/
git-status
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
#!/usr/bin/python3
# _____ _ ____ ____ ____ ____ _____
# / // \ / _ \/ __\/ _ \/ __\/__ __\
# | __\| | | / \|| \/|| / \|| \/| / \
# | | | |_/\| |-||| __/| \_/|| / | |
# \_/ \____/\_/ \|\_/ \____/\_/\_\ \_/
#
""" git-status | gits:
small fzf-based CLI utility for easy
'git status', 'git add', 'git commit', 'git reset', 'git restore' and 'git checkout'
"""
import os
import sys
from subprocess import call, check_output, Popen, PIPE, CalledProcessError
def fzf(list_in, prompt):
fzf_in = Popen(["echo", "\n".join(list_in)], stdout=PIPE).stdout
try:
choice = check_output(
[
"fzf",
"--multi",
"--layout=reverse",
"--height=60%",
f"--header={prompt}",
"--bind=j:down,k:up,l:toggle,h:toggle,space:toggle,ctrl-j:preview-down,ctrl-k:preview-up",
"--preview-window=right",
'--preview=git-status preview "{}"',
],
stdin=fzf_in,
).decode()[:-1]
except CalledProcessError:
return []
return choice.split("\n")
def git_status():
raw_status = check_output(["git", "status", "--short"]).decode().splitlines()
if not raw_status:
status = check_output(["git", "status"]).decode()[:-1]
print(status)
exit(0)
status = []
L = 0
for s in raw_status:
if s[1] != " ":
status.append(s[:3] + " [add] " + s[3:])
if len(status[-1]) > L:
L = len(status[-1])
for s in raw_status:
if s[0] != " " and s[0] != "?":
status.append(s[:3] + " [unstage] " + s[3:])
if len(status[-1]) > L:
L = len(status[-1])
for s in raw_status:
if s[1] != "?" and s[0] != "A":
status.append(s[:3] + " [restore] " + s[3:])
if len(status[-1]) > L:
L = len(status[-1])
for s in raw_status:
if s[:2] == "??" or s[:2] == "A ":
status.append(s[:3] + " [delete] " + s[3:])
if len(status[-1]) > L:
L = len(status[-1])
_status = ["-" * L]
__status = [x for x in status if x[0] not in (" ", "?") and "[add]" not in x]
if __status:
_status += __status
_status += ["-" * L]
_status += [x for x in status if x[0] in (" ", "?") or "[add]" in x]
status = [" [add -A]"] + [" [commit]"] + [" [reset]"] + _status
return status
if __name__ == "__main__":
if len(sys.argv) == 1:
old_status = (
"old status:\n" + check_output(["git", "status", "--short"]).decode()
)
actions = fzf(git_status(), "git status")
try:
idx = actions.index(" [commit]")
del actions[idx]
actions.append(" [commit]")
except ValueError:
pass
for action in actions:
if action == " [add -A]":
print("git add -A")
call(["git", "add", "-A"])
elif action == " [reset]":
print("git reset")
call(["git", "reset", "--quiet"])
elif action == " [commit]":
print("git commit")
call(["git", "commit"])
elif "[add]" in action:
filename = action[3:].replace("[add]", "").strip()
print(f"git add -- {filename}")
call(f"git add -- {filename}", shell=True)
elif "[unstage]" in action:
filename = action[3:].replace("[unstage]", "").strip()
print(f"git reset -- {filename}")
call(f"git reset -- {filename}", shell=True)
elif "[restore]" in action:
filename = action[3:].replace("[restore]", "").strip()
print(f"git reset -- {filename}")
print(f"git checkout -- {filename}")
call(f"git reset -- {filename}", shell=True)
call(f"git checkout -- {filename}", shell=True)
elif "[delete]" in action:
filename = action[3:].replace("[delete]", "").strip()
print(f"git reset -- {filename}")
print(f"rm -- {filename}")
call(f"git reset -- {filename}", shell=True)
call(f"rm -- {filename}", shell=True)
else:
continue
new_status = (
"new status:\n" + check_output(["git", "status", "--short"]).decode()
)
exit(0)
elif len(sys.argv) == 3 and sys.argv[1] == "preview":
filename = (
sys.argv[2][3:]
.replace("[add]", "")
.replace("[unstage]", "")
.replace("[restore]", "")
.replace("[delete]", "")
.replace('"', "")
.replace("'", "")
.strip()
)
try:
with open(filename, "r") as file:
content = file.read()
diff = call(["git", "diff", "--color=always", filename])
except:
if filename == "[add -A]" or filename == "[commit]":
call(["git", "-c", "color.status=always", "status"])
elif filename == "[reset]":
call(["git","diff", "--color=always"])
exit(0)
else:
exit(1)