Package moap :: Package vcs :: Module git
[hide private]
[frames] | no frames]

Source Code for Module moap.vcs.git

  1  # -*- Mode: Python; test-case-name: moap.test.test_vcs_git -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  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   
15 -def detect(path):
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
30 -class Git(vcs.VCS):
31 name = 'Git' 32
33 - def getUnknown(self, path):
34 oldPath = os.getcwd() 35 os.chdir(path) 36 37 # doing an ls-files with --others does not look at .gitignore 38 # unless you tell it to 39 result = commands.getoutput( 40 "git ls-files --others --exclude-per-directory=.gitignore").split( 41 '\n') 42 # one empty line does not a return value make 43 if result and result == ['']: 44 result = [] 45 46 os.chdir(oldPath) 47 return result
48
49 - def ignore(self, paths, commit=True):
50 # git ignores entries by appending to a .gitignore file in the parent 51 oldPath = os.getcwd() 52 os.chdir(self.path) 53 54 tree = self.createTree(paths) 55 toCommit = [] 56 for path in tree.keys(): 57 # this does the right thing if path == '' 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 # FIXME: also commit .gitignore files when done. Should we make 69 # this part of the interface ? 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 # to do a diff, git needs relative paths, relative to the root 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
112 - def getFileMatcher(self):
113 # example: diff --git a/t b/t 114 return re.compile(r'^diff --git a/(\S+) b')
115
116 - def update(self, path):
117 oldPath = os.getcwd() 118 os.chdir(self.path) 119 120 # FIXME: No way to pull just updates to the given path; so update 121 # the whole checkout 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