-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvCommit.py
161 lines (125 loc) · 4.46 KB
/
cvCommit.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
import argparse
from collections import namedtuple
import yaml
class Commit(object):
def __init__(self, message, parent):
self.message = message
self.__parent = parent
@property
def parent(self):
return self.__parent
def __repr__(self):
return self.__class__.__name__ + f'("{self.message}", {self.__parent})'
class Branch(object):
def __init__(self, name, commit):
self.name = name
self.commit = commit
file = '.cv.yaml'
def init(args):
try:
open(file, 'r')
print('Already a cv repository')
except FileNotFoundError:
stream = open(file, 'w')
# TODO create by default a master branch
yaml.dump({
'commits': [],
'branches': [Branch('master', None),],
'last': None,
'checked_out_branch': 0,
}, stream)
def commit(args):
data = open_repo()
if not args.message:
message = input('Please, give a message for the commit:')
else:
message = args.message
data['commits'].append(Commit(message, data['last']))
data['last'] = len(data['commits']) - 1
data['branches'][data['checked_out_branch']].commit = data['last']
stream = open(file, 'w')
yaml.dump(data, stream)
def rebase(args):
raise NotImplementedError
def checkout(args):
data = open_repo()
if args.branch_name:
print(args.branch_name)
# TODO check if already exists
data['branches'].append(Branch(args.branch_name, data['last']))
data['checked_out_branch'] = -1
else:
try:
if int(args.index) >= len(data['commits']):
print('fatal: not a commit index')
exit()
data['last'] = int(args.index)
print("You are in 'detached HEAD' state.")
data['checked_out_branch'] = None
except ValueError:
for i, branch in enumerate(data['branches']):
if branch.name == args.index:
data['last'] = branch.commit
data['checked_out_branch'] = i
save_repo(data)
return
print(f'fatal: {args.index} is not a branch name')
exit()
save_repo(data)
def export(args):
data = open_repo()
# commits_dict = {str(i): {"message": c.message, "parent": c.parent, "branch": } for i, c in
# enumerate(data['commits'])}
commits_dict = {}
for i, c in enumerate(data['commits']):
branch = None
for b in data['branches']:
if b.commit == i:
if not branch:
branch = [b.name, ]
else:
branch.append(b.name)
commits_dict[str(i)] = {"message": c.message, "parent": c.parent, "branch": branch}
name = args.name
if not name:
name = 'commits.json'
elif name[-5:] != '.json':
name += '.json'
import json
with open(f'front-end/static/{name}', 'w') as fp:
json.dump(commits_dict, fp)
def open_repo():
try:
open(file, 'r')
except FileNotFoundError:
print('fatal: not a cv repository')
exit()
stream = open(file, 'r')
return yaml.load(stream)
def save_repo(data):
stream = open(file, 'w')
yaml.dump(data, stream)
commands = {
'init': init,
'commit': commit,
'rebase': rebase,
'checkout': checkout,
'export': export,
}
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Description')
subparsers = parser.add_subparsers(help='subparsers help', dest='command')
parser_init = subparsers.add_parser('init', help='Creates a new CV repository')
parser_commit = subparsers.add_parser('commit', help='Creates a new node')
parser_commit.add_argument('-m', help='', dest='message')
parser_rebase = subparsers.add_parser('rebase', help='rebase help')
parser_rebase.add_argument('branch', help='')
parser_checkout = subparsers.add_parser('checkout', help='Changes the pointer to the given index')
pcg = parser_checkout.add_mutually_exclusive_group(required=True)
pcg.add_argument('index', help='', nargs='?')
pcg.add_argument('-b', '--branch', dest='branch_name')
parser_export = subparsers.add_parser('export', help='Export the nodes to a JSON for the viewer to render it')
parser_export.add_argument('-n', help='destination name', dest='name')
args = parser.parse_args()
# print(args)
commands[args.command](args)