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(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 temp = util.writeTemp([message, ])
85 cmd = "git commit -F %s %s" % (temp, " ".join(paths))
86 self.debug("Running %s" % cmd)
87 status, output = commands.getstatusoutput(cmd)
88 os.unlink(temp)
89 if status != 0:
90 raise vcs.VCSException(output)
91 finally:
92 os.chdir(oldPath)
93
94 - def diff(self, path):
95 oldPath = os.getcwd()
96 os.chdir(self.path)
97
98
99 if path.startswith(self.path):
100 path = path[len(self.path):]
101 if not path:
102 path = "."
103 self.debug('frobnicated path to %s' % path)
104
105 cmd = "git diff --cached -- %s" % path
106 self.debug("Running %s" % cmd)
107 output = commands.getoutput(cmd)
108
109 os.chdir(oldPath)
110 return output
111
113
114 return re.compile(r'^diff --git a/(\S+) b')
115
117 oldPath = os.getcwd()
118 os.chdir(self.path)
119
120
121
122 cmd = "git pull"
123 self.debug("Running %s" % cmd)
124 status, output = commands.getstatusoutput(cmd)
125 os.chdir(oldPath)
126 if status != 0:
127 raise vcs.VCSException(output)
128
129 return output
130
131 VCSClass = Git
132