-
Notifications
You must be signed in to change notification settings - Fork 3
/
GitViper.py
280 lines (230 loc) · 9.63 KB
/
GitViper.py
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
import os
import argparse
import configparser
from pprint import pprint
import gitviper
import gitviper.utilities
import gitviper.gitconnector as gitconnector
from gitviper.gitconnector import connection
from gitviper.colors import *
import time
from pathlib import Path
import os
home_dir = str(Path.home())
project_dir = os.getcwd()
directories = [home_dir, project_dir]
gitviper_path = os.path.dirname(os.path.realpath(__file__))
import sys
if len(sys.argv) > 1:
if sys.argv[1] == "init":
if not os.path.isdir(project_dir + "/.git"):
arg_string = "--force"
if (len(sys.argv) > 2 and sys.argv[2] == arg_string) or (len(sys.argv) > 3 and sys.argv[3] == arg_string):
pass
else:
print("This is not a git repository!")
print("Use " + arg_string + " to force the creation of the .gitviper directory outside of a repository")
exit()
if not os.path.isdir(project_dir + "/.gitviper"):
os.makedirs(project_dir + "/.gitviper")
print("Initialized GitViper.")
else:
arg_string = "--re-init"
if (len(sys.argv) > 2 and sys.argv[2] == arg_string) or (len(sys.argv) > 3 and sys.argv[3] == arg_string):
print("Reinitialized GitViper configuration.")
else:
print("Configuration folder already exists!")
print("Use " + arg_string + " to overwrite existing settings.")
exit()
# copy template files
from shutil import copyfile
template_path = gitviper_path + "/templates"
local_template_path = project_dir + "/.gitviper"
for item in os.listdir(template_path):
copyfile(template_path + "/" + item, local_template_path + "/" + item)
exit()
else:
sys_argv_has_been_handled = True
gitconnector.connect()
if sys.argv[1] == "status":
print()
gitviper.list_status()
elif sys.argv[1] == "stash":
print()
gitviper.list_stash()
elif sys.argv[1] == "log":
print()
if len(sys.argv) < 3:
gitviper.list_logs(-1, 0, True)
else:
gitviper.list_logs(sys.argv[2], 0, True)
elif sys.argv[1] == "branch" or sys.argv[1] == "branches":
print()
gitviper.list_branches(True)
elif sys.argv[1] == "tasks" or sys.argv[1] == "todo":
print()
gitviper.list_tasks()
else:
sys_argv_has_been_handled = False
if sys_argv_has_been_handled:
print()
exit()
# module variables
label = "GitViper"
version = "v0.3"
branch = ""
branch_path = gitviper_path + '/.git/HEAD'
if os.path.isfile(branch_path):
with open(branch_path, 'r') as myfile:
branch = myfile.readline().rstrip().rsplit("/")[-1]
if branch == "master":
branch = ""
# load default values for cli arguments
default_values = {
"ignore_conf" : False,
"tasks" : True,
"branches" : True,
"status" : True,
"stash" : True,
"logs" : True,
"time" : False,
"separate_commits" : False,
"log_number" : 5,
"max_days" : 0,
"invert": False,
"tasks-diff": True
}
# setup value dictionaries
cli_arg_values = {}
conf_values = {}
final_values = {}
for key in default_values:
cli_arg_values[key] = None
conf_values[key] = None
final_values[key] = default_values[key]
def load_defaults(dir_path):
full_path = dir_path + "/.gitviper/config"
if not os.path.isfile(full_path):
return
config = configparser.ConfigParser()
config.read(full_path)
try:
conf_values["tasks"] = config["Display Settings"].getboolean("show-tasks")
conf_values["branches"] = config["Display Settings"].getboolean("show-branches")
conf_values["status"] = config["Display Settings"].getboolean("show-status")
conf_values["stash"] = config["Display Settings"].getboolean("show-stash")
conf_values["logs"] = config["Display Settings"].getboolean("show-logs")
conf_values["time"] = config["Display Settings"].getboolean("show-time")
conf_values["tasks-diff"] = config["Display Settings"].getboolean("show-tasks-only-for-current-changes")
except KeyError:
pass
try:
conf_values["separate_commits"] = config["Log Settings"].getboolean("show-separator")
conf_values["log_number"] = config["Log Settings"].getint("log-number")
conf_values["max_days"] = config["Log Settings"].getint("log-days")
except KeyError:
pass
for directory in directories:
load_defaults(directory)
# command line arguments
parser = argparse.ArgumentParser(description="This will show all values that can be toggled. The initial value is gathered from the configuration file(s). The default values are used if there are no files provided.")
parser.add_argument("-ig", "--ignore-conf", action="store_true", help="ignore all configuration files and use the default values")
parser.add_argument("-t", "--tasks", action="store_true", help="toggle the tasks category")
parser.add_argument("-td", "--tasks-diff", action="store_true", help="toggle tasks showing only for the currently modified files or all files")
parser.add_argument("-b", "--branches", action="store_true", help="toggle the branches category")
parser.add_argument("-s", "--status", action="store_true", help="toggle the status category")
parser.add_argument("-st", "--stash", action="store_true", help="toggle the stash category")
parser.add_argument("-l", "--logs", action="store_true", help="toggle the commit logs category")
parser.add_argument("-ln", "--log-number", type=int, default=0, help="specifiy the number of logs that will be shown")
parser.add_argument("-tm", "--time", action="store_true", help="show time needed for each category")
parser.add_argument("-d", "--max-days-old", type=int, default=0, help="specifiy the number of days to consider for the commit log")
parser.add_argument("-sep", "--separate-commits", action="store_true", help="separate the commit logs by days")
parser.add_argument("--debug", action="store_true", help="show debug logs")
parser.add_argument("-inv", "--invert", action='store_true', help="invert the given values")
args = parser.parse_args()
# cli toggles
cli_arg_values["ignore_conf"] = args.ignore_conf
cli_arg_values["tasks"] = args.tasks
cli_arg_values["tasks-diff"] = args.tasks_diff
cli_arg_values["branches"] = args.branches
cli_arg_values["status"] = args.status
cli_arg_values["stash"] = args.stash
cli_arg_values["logs"] = args.logs
cli_arg_values["time"] = args.time
cli_arg_values["separate_commits"] = args.separate_commits
cli_arg_values["invert"] = args.invert
# cli values
cli_arg_values["max_days"] = args.max_days_old
cli_arg_values["log_number"] = args.log_number
# overwrite values with config values
if cli_arg_values["ignore_conf"] == False:
for key in conf_values:
if conf_values[key] != None:
final_values[key] = conf_values[key] # do i have to init final_ with default values? because conf_ will also have default values if not set, right?
# command line args to flip value
for key in cli_arg_values:
if isinstance(final_values[key], bool):
if cli_arg_values[key] == True:
final_values[key] = not final_values[key]
elif int(cli_arg_values[key]) > 0:
final_values[key] = int(cli_arg_values[key])
# # # print values
if args.debug:
print("key".ljust(16), "default", "cli-ar", "conf", "final", sep="\t")
print()
for key in default_values:
print(key.ljust(16), default_values[key], cli_arg_values[key], conf_values[key], final_values[key], sep="\t")
# Execute main program
# print GitViper label
window_width = gitviper.utilities.get_window_size().x
text = label + " " + version + "-" + branch
if text[-1] == "-":
text = text[0 : -1]
text = text.rjust(window_width)
print(text)
start_time = time.time()
current_time = start_time
time_separator = "-" * window_width
def show_time():
global current_time
delta_time = str(round(time.time() - current_time, 3))
print(BOLD + BLUE + (delta_time + " seconds ").rjust(window_width) + RESET)
reset_time()
def reset_time():
global current_time
current_time = time.time()
def finalize_category(category_is_visible):
if category_is_visible:
if final_values["time"] == True:
show_time()
print(BOLD + BLUE + time_separator + RESET)
print()
else:
print()
else:
reset_time()
try:
if not final_values['tasks-diff']:
if final_values["tasks"] != final_values["invert"]:
finalize_category(gitviper.list_tasks())
# git
gitconnector.connect()
if final_values['tasks-diff']:
if final_values["tasks"] != final_values["invert"]:
finalize_category(gitviper.list_tasks_of_diff())
if connection.is_git_repo:
if final_values["branches"] != final_values["invert"]:
finalize_category(gitviper.list_branches())
if final_values["logs"] != final_values["invert"]:
finalize_category(gitviper.list_logs(final_values["log_number"], final_values["max_days"], final_values["separate_commits"]))
if final_values["stash"] != final_values["invert"]:
finalize_category(gitviper.list_stash())
if final_values["status"] != final_values["invert"]:
finalize_category(gitviper.list_status())
except KeyboardInterrupt:
print()
except BrokenPipeError: # occurs sometimes after quitting less when big git-logs are displayed
pass
if final_values["time"] == True:
current_time = start_time
show_time()