1
2
3
4 """
5 Git functionality.
6 """
7
8 import os
9 import commands
10 import re
11
12 from moap.util import util, log
13 from moap.vcs import vcs
14
16 """
17 Detect if the given source tree is using Git.
18
19 @return: True if the given path looks like a Git tree.
20 """
21 while path and path != '/':
22 if (os.path.exists(os.path.join(path, '.git'))
23 and os.path.exists(os.path.join(path, '.git', 'description'))
24 and (not os.path.exists(os.path.join(path, '.git', 'svn')))):
25 return True
26 path = os.path.dirname(path)
27 return False
28
29
31 name = 'Git'
32
34 oldPath = os.getcwd()
35 os.chdir(self.path)
36
37
38
39 result = commands.getoutput(
40 "git ls-files --others --exclude-per-directory=.gitignore").split(
41 '\n')
42
43 if result and result == ['']:
44 result = []
45
46 os.chdir(oldPath)
47 return result
48
49 - def ignore(self, paths, commit=True):
50
51 oldPath = os.getcwd()
52 os.chdir(self.path)
53
54 tree = self.createTree(paths)
55 toCommit = []
56 for path in tree.keys():
57
58 gitignore = os.path.join(path, '.gitignore')
59 toCommit.append(gitignore)
60
61 handle = open(gitignore, "a")
62
63 for child in tree[path]:
64 handle.write('%s\n' % child)
65
66 handle.close()
67
68
69
70 self.commit(toCommit, 'moap ignore')
71
72 os.chdir(oldPath)
73
74 - def commit(self, paths, message):
75 try:
76 oldPath = os.getcwd()
77 os.chdir(self.path)
78 self.debug('Committing paths %r' % paths)
79 cmd = "git add %s" % " ".join(paths)
80 self.debug("Running %s" % cmd)
81 status, output = commands.getstatusoutput(cmd)
82 if status != 0:
83 raise vcs.VCSException(output)
84 cmd = "git commit -m '%s' %s" % (message, " ".join(paths))
85 self.debug("Running %s" % cmd)
86 status, output = commands.getstatusoutput(cmd)
87 if status != 0:
88 raise vcs.VCSException(output)
89 finally:
90 os.chdir(oldPath)
91
92 - def diff(self, path):
93 oldPath = os.getcwd()
94 os.chdir(self.path)
95
96
97 if path.startswith(self.path):
98 path = path[len(self.path):]
99 if not path:
100 path = "."
101 self.debug('frobnicated path to %s' % path)
102
103 cmd = "git diff --cached -- %s" % path
104 self.debug("Running %s" % cmd)
105 output = commands.getoutput(cmd)
106
107 os.chdir(oldPath)
108 return output
109
111
112 return re.compile(r'^diff --git a/(\S+) b')
113
115
116
117 status, output = commands.getstatusoutput("git pull %s" % self.path)
118 if status != 0:
119 raise vcs.VCSException(output)
120
121 return output
122
123 VCSClass = Git
124