-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit2web.py
More file actions
131 lines (110 loc) · 4.61 KB
/
git2web.py
File metadata and controls
131 lines (110 loc) · 4.61 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
# -*- coding: utf-8 -*-
import sys, json, os
from shutil import copyfile
from os.path import join, exists
from jinja2 import Environment, FileSystemLoader
from pygit2 import Repository
class git2web:
# shitty classname i know, sue me
def __init__(self, repos):
# set some initial values
self.markup = {}
for name, repo in repos.items():
self.parse(name, repo)
def parse(self, name, repo):
# do not parse empty repos
if repo.is_empty:
return
# set the repo-name as a key to an empty dictionary.
self.markup.update({name : {'name' : name}})
self.markup[name].update({'branches' : repo.listall_branches()})
# iterate over each branch
for branch in repo.listall_branches():
br = repo.lookup_branch(branch)
referenceLog = [reference for reference in br.log()]
self.markup[name].update(
{
branch : {
'name' : branch,
'latestDate': repo.revparse_single(br.target.hex).commit_time,
'latestHash': br.target.hex,
'commits' : {}
}
}
)
# Watch out for bug-hell right about now
# good luck debugging this spaghetti code.
# I listened to this song while coding this shit:
# https://www.youtube.com/watch?v=SW-BU6keEUw
for reference in referenceLog:
oid = str(reference.oid_new)
commit = repo.revparse_single(oid)
parents = [str(parent.oid) for parent in commit.parents]
self.markup[name][branch]['commits'].update(
{ oid : {
'hash' : oid,
'affected': [],
'time' : commit.commit_time, # unix time-format?
'parents' : parents,
'author' : commit.committer.name,
'message' : commit.message
}
}
)
for twig in commit.tree:
self.markup[name][branch]['commits'][oid]['affected'].append({
'hash' : twig.hex,
'filename' : twig.name
# :STRETCH: Individual patches per file, Version 1.1?
# Make it so that a file can have an individual
# patch assigned with it. This was not included
# as a defined goal within the scope of our project;
})
def main():
repos = {}
config = {}
markup = ""
# read the config
with open('config.json', 'r') as conf:
config = json.load(conf)
# iterate over all specified repository
for r in config['repos']:
try:
repos.update({r['name'] : Repository(r['path'])})
except KeyError:
print('Unable to open repository {} in config. Wrong path?'.format(r['name']))
return 1
if config['markup'] == 'json':
# :HMM: the json spat out by this if-block gave some warnings
# on firefox when loading it as text/javascript. Is this present
# on other JSON files aswell??
markup = git2web(repos)
output = json.dumps(markup.markup, sort_keys=True, indent=4)
path = join(config['outputPath'], "data.json")
# copy the jsonTemplate to the output-directory.
jsonTemplate = join(config['templatesDirectory'], config['jsonTemplate'])
jsonTemplateOutput = join(config['outputPath'], "index.html")
copyfile(jsonTemplate, jsonTemplateOutput)
print("[i] Copied {} into {}".format(config['jsonTemplate'], config['outputPath']))
if not exists(config['outputPath']):
os.mkdir(config['outputPath'])
with open(path, 'w') as fh:
fh.write(output)
print("[i] wrote markup into", path)
# copy all the things defined in config['assets']
# into config['outputPath']
for asset in config['assets']:
src = join(config['templatesDirectory'], asset)
dstFile = join(config['outputPath'], asset)
dstPath = dstFile[:dstFile.rfind("/")]
if not exists(src):
print("[!] Unable to find", src)
return 1
if not exists(dstPath):
os.mkdir(dstPath)
copyfile(src, dstFile)
# smooth sailing, everything is fine!
return 0
# launch the program at start
if __name__ == '__main__':
sys.exit(main())