| 1 |
# -*- coding: iso-8859-1 -*- |
|---|
| 2 |
# |
|---|
| 3 |
# Copyright (C) 2006 Herbert Valerio Riedel <hvr@gnu.org> |
|---|
| 4 |
# |
|---|
| 5 |
# This program is free software; you can redistribute it and/or |
|---|
| 6 |
# modify it under the terms of the GNU General Public License |
|---|
| 7 |
# as published by the Free Software Foundation; either version 2 |
|---|
| 8 |
# of the License, or (at your option) any later version. |
|---|
| 9 |
# |
|---|
| 10 |
# This program is distributed in the hope that it will be useful, |
|---|
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 |
# GNU General Public License for more details. |
|---|
| 14 |
|
|---|
| 15 |
import os, re |
|---|
| 16 |
|
|---|
| 17 |
class GIT: |
|---|
| 18 |
def __init__(self,repo): |
|---|
| 19 |
self.repo = repo |
|---|
| 20 |
|
|---|
| 21 |
def _git_call_f(self,cmd): |
|---|
| 22 |
#print cmd |
|---|
| 23 |
(input, output, error) = os.popen3('GIT_DIR="%s" %s' % (self.repo,cmd)) |
|---|
| 24 |
return output |
|---|
| 25 |
|
|---|
| 26 |
def _git_call(self,cmd): |
|---|
| 27 |
return self._git_call_f(cmd).read() |
|---|
| 28 |
|
|---|
| 29 |
def head(self): |
|---|
| 30 |
return self._git_call("git-rev-parse --verify HEAD").strip() |
|---|
| 31 |
|
|---|
| 32 |
def tree_ls(self,sha,path=""): |
|---|
| 33 |
if len(path)>0 and path[0]=='/': |
|---|
| 34 |
path=path[1:] |
|---|
| 35 |
return [e[:-1].split(None, 3) for e in self._git_call_f("git-ls-tree %s '%s'" % (sha,path)).readlines()] |
|---|
| 36 |
|
|---|
| 37 |
def read_commit(self, sha): |
|---|
| 38 |
raw = self._git_call("git-cat-file commit "+sha) |
|---|
| 39 |
lines = raw.splitlines() |
|---|
| 40 |
|
|---|
| 41 |
line = lines.pop(0) |
|---|
| 42 |
d = {} |
|---|
| 43 |
while line != "": |
|---|
| 44 |
(key,value)=line.split(None, 1) |
|---|
| 45 |
if not d.has_key(key): |
|---|
| 46 |
d[key] = [] |
|---|
| 47 |
d[key].append(value.strip()) |
|---|
| 48 |
line = lines.pop(0) |
|---|
| 49 |
|
|---|
| 50 |
return ("\n".join(lines),d) |
|---|
| 51 |
|
|---|
| 52 |
def get_file(self, sha): |
|---|
| 53 |
return self._git_call_f("git-cat-file blob "+sha) |
|---|
| 54 |
|
|---|
| 55 |
def parents(self, sha): |
|---|
| 56 |
tmp=self._git_call("git-rev-list --max-count=1 --parents "+sha) |
|---|
| 57 |
tmp=tmp.strip() |
|---|
| 58 |
tmp=tmp.split() |
|---|
| 59 |
return tmp[1:] |
|---|
| 60 |
|
|---|
| 61 |
def children(self, sha): |
|---|
| 62 |
for revs in self._git_call_f("git-rev-list --parents HEAD").readlines(): |
|---|
| 63 |
revs = revs.strip() |
|---|
| 64 |
revs = revs.split() |
|---|
| 65 |
if sha in revs[1:]: |
|---|
| 66 |
yield revs[0] |
|---|
| 67 |
|
|---|
| 68 |
def history(self, sha, path, skip=0): |
|---|
| 69 |
for rev in self._git_call_f("git-rev-list %s -- '%s'" % (sha,path)).readlines(): |
|---|
| 70 |
if(skip > 0): |
|---|
| 71 |
skip = skip - 1 |
|---|
| 72 |
continue |
|---|
| 73 |
yield rev.strip() |
|---|
| 74 |
|
|---|
| 75 |
def last_change(self, sha, path): |
|---|
| 76 |
for rev in self.history(sha, path): |
|---|
| 77 |
return rev |
|---|
| 78 |
return None |
|---|
| 79 |
|
|---|
| 80 |
def diff_tree(self, tree1, tree2, path=""): |
|---|
| 81 |
if tree1 is None: |
|---|
| 82 |
tree1 = "--root" |
|---|
| 83 |
cmd = "git-diff-tree -r %s %s -- '%s'" % (tree1, tree2, path) |
|---|
| 84 |
for chg in self._git_call_f(cmd).readlines(): |
|---|
| 85 |
if chg.startswith(tree2): |
|---|
| 86 |
continue |
|---|
| 87 |
(mode1,mode2,obj1,obj2,action,path) = chg[:-1].split(None, 5) |
|---|
| 88 |
mode1 = mode1[1:] |
|---|
| 89 |
yield (mode1,mode2,obj1,obj2,action,path) |
|---|
| 90 |
|
|---|
| 91 |
if __name__ == '__main__': |
|---|
| 92 |
import sys |
|---|
| 93 |
|
|---|
| 94 |
g = GIT(sys.argv[1]) |
|---|
| 95 |
|
|---|
| 96 |
print "[%s]" % g.head() |
|---|
| 97 |
print g.tree_ls(g.head()) |
|---|
| 98 |
print "--------------" |
|---|
| 99 |
print g.read_commit(g.head()) |
|---|
| 100 |
print "--------------" |
|---|
| 101 |
print g.parents(g.head()) |
|---|