-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitpush.py
127 lines (102 loc) · 3.74 KB
/
gitpush.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
import argparse
import os
import platform
import subprocess as sp
import time
from styles import stylise, logo
def read_cmds_from_file():
if is_windows():
commands_file = 'C:\\commands\\gitpush_commands.txt'
elif is_linux():
commands_file = '~/commands/gitpush_commands.txt'
with open(commands_file, 'r') as cmd_file:
cmds = cmd_file.readlines()
cmds = [cmd.strip().split(' ') for cmd in cmds]
return cmds
def find_git_repo():
path = os.getcwd()
while not os.path.isdir(os.path.join(path, ".git")):
path = os.path.join(path, "..")
return os.path.dirname(os.path.join(path, ".git"))
def is_windows():
return platform.system() == "Windows"
def is_linux():
return platform.system() == "Linux"
# TODO: integrate OpenAI to generate custom commit messages
# TODO: how to handle error messages of a command?
def main(args):
"""add, commit and push git changes of the current repo
Args:
msg (str): commit message
interval_seconds (float): time gap in between 2 commits
"""
# global USE_GPT
if is_windows():
os.system("cls")
elif is_linux():
os.system("clear")
quit = stylise("<CTRL+C / CMD+C>", "red")
colorised_logo = stylise(logo, "cyan")
print(colorised_logo, end="\n\n")
print(f"Press {quit} to quit...")
print(stylise(f"Working on \"{find_git_repo()}\" repo", "yellow"), '\n')
commit_msg = args.msg
# if args.msg.lower() == "gpt":
# USE_GPT = True
# while True:
# api_key = input(
# stylise("Please provide OpenAI's API key: ", "yellow"))
# print(f"{api_key = }")
# res = input(stylise("Is this your API key?", "yellow") +
# f" {api_key}\n [y/n/Enter for y] ")
# if res == 'y' or res == '':
# break
# ...
start = time.perf_counter()
stop = False
while not stop:
if args.msg.lower() == "default":
# if no message specified, default to this
commit_msg = "auto commit at " + time.asctime()
if is_windows():
os.system("cls")
elif is_linux():
os.system("clear")
# if USE_GPT:
# commit_msg to be changed here each time we need to commit
# some call to LLM with prompt as `git status` to get a basic commit message
cmds = read_cmds_from_file()
for cmd in cmds:
if "$msg" in cmd:
cmd[cmd.index("$msg")] = commit_msg
print(colorised_logo, end="\n\n")
print(f"Press {quit} to quit...")
print(
stylise(f"Working on \"{find_git_repo()}\" repo", "yellow"), '\n')
print(stylise("[TIME] ", "green"), stylise(
time.asctime(), "green"))
for cmd in cmds:
print(stylise("🎯 Running " + " ".join(arg for arg in cmd), "cyan"))
res = sp.run(cmd)
print(stylise("✅ Completed running commands.", "cyan"))
interval = args.interval - (time.perf_counter() - start)
while interval > 0:
start = time.perf_counter()
print(stylise(f"🎯 Re-running commands in {round(interval, 1)} seconds", "cyan"), end='\r')
interval -= (time.perf_counter() - start)
if __name__ == "__main__":
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"-i", "--interval",
type=float,
default=300.0,
help="Interval (seconds) with which you want to add commits | default=300")
parser.add_argument(
"-m", "--msg",
type=str,
default="default",
help="custom commit message for each push | default=default"
)
args = parser.parse_args()
main(args)