summaryrefslogtreecommitdiffstats
path: root/fit.py
diff options
context:
space:
mode:
Diffstat (limited to 'fit.py')
-rw-r--r--fit.py95
1 files changed, 51 insertions, 44 deletions
diff --git a/fit.py b/fit.py
index ce4d902..c81fb06 100644
--- a/fit.py
+++ b/fit.py
@@ -9,6 +9,56 @@ class Fit:
def __init__(self, path):
self.repo = init_repository(path, True)
+
+ def get_file(self, oid):
+ """ Returns the actual data of a git object """
+ return self.repo[oid].data
+
+
+ def get_modules(self):
+ """ returns a list of all modules """
+ return [x[0] for x in self._list()]
+
+
+ def get_module(self, module):
+ """ gets all entries for a module grouped by years """
+ years = self._list(module)
+ return [(year[0], self._list(os.path.join(module, year[0]))) for year in years]
+
+ def add_file(self, data, path):
+ """ Inserts the given file in the git tree and commits the changes """
+ try:
+ commit = self._get_last_commit()
+ parents = [commit.oid]
+ root = commit.tree.oid
+
+ except:
+ parents = []
+ root = None
+
+
+ blob_oid = self.repo.create_blob(data)
+ tree = self._insert_node(blob_oid, path, root)
+ author = committer = Signature('Fit', 'Fit@fit.de', int(time.time()), 120)
+
+ commit = self.repo.create_commit(
+ 'HEAD',
+ author,
+ committer,
+ 'added %s' % path,
+ tree,
+ parents
+ )
+
+ # save the actual head sha for dump git http protocol
+ # similiar to `git update-server-info`
+ info_refs_path = os.path.join(self.repo.path, 'info', 'refs')
+ with open(info_refs_path, 'w') as f:
+ f.write('%s\trefs/heads/master\n' % b2a_hex(commit).decode('ascii'))
+
+ return b2a_hex(blob_oid).decode('ascii')
+
+
def _insert_node(self, node_oid, path, root_oid):
""" Inserts a new Node in a Git Tree graph """
if root_oid:
@@ -73,43 +123,6 @@ class Fit:
return self.repo[head.oid]
- def add_file(self, data, path):
- """ Inserts the given file in the git tree and commits the changes """
- try:
- commit = self._get_last_commit()
- parents = [commit.oid]
- root = commit.tree.oid
-
- except:
- parents = []
- root = None
-
-
- blob_oid = self.repo.create_blob(data)
- tree = self._insert_node(blob_oid, path, root)
- author = committer = Signature('Fit', 'Fit@fit.de', int(time.time()), 120)
-
- commit = self.repo.create_commit(
- 'HEAD',
- author,
- committer,
- 'added %s' % path,
- tree,
- parents
- )
-
- info_refs_path = os.path.join(self.repo.path, 'info', 'refs')
- with open(info_refs_path, 'w') as f:
- f.write('%s\trefs/heads/master\n' % b2a_hex(commit).decode('ascii'))
-
- return b2a_hex(blob_oid).decode('ascii')
-
-
- def get_file(self, oid):
- """ Returns the actual data of a git object """
- return self.repo[oid].data
-
-
def _list(self, path=None):
""" Lists all entries for a given path """
try:
@@ -120,17 +133,11 @@ class Fit:
for p in path.split('/'):
tree = tree[p].to_object()
- return ((x.name, x.hex) for x in tree)
+ return [(x.name, x.hex) for x in tree]
except:
return []
- def get_modules(self):
- return (x[0] for x in self._list())
-
- def get_module(self, module):
- years = self._list(module)
- return [(year[0], self._list(os.path.join(module, year[0]))) for year in years]
#fit = Fit('static/fit.git')
#fit.add_file('main.c', 'alp3/2007/main.c')