git_command.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import sys
  17. import subprocess
  18. import tempfile
  19. from error import GitError
  20. from trace import REPO_TRACE, IsTrace, Trace
  21. GIT = 'git'
  22. MIN_GIT_VERSION = (1, 5, 4)
  23. GIT_DIR = 'GIT_DIR'
  24. LAST_GITDIR = None
  25. LAST_CWD = None
  26. _ssh_proxy_path = None
  27. _ssh_sock_path = None
  28. def _ssh_sock(create=True):
  29. global _ssh_sock_path
  30. if _ssh_sock_path is None:
  31. if not create:
  32. return None
  33. _ssh_sock_path = os.path.join(
  34. tempfile.mkdtemp('', 'ssh-'),
  35. 'master-%r@%h:%p')
  36. return _ssh_sock_path
  37. def _ssh_proxy():
  38. global _ssh_proxy_path
  39. if _ssh_proxy_path is None:
  40. _ssh_proxy_path = os.path.join(
  41. os.path.dirname(__file__),
  42. 'git_ssh')
  43. return _ssh_proxy_path
  44. class _GitCall(object):
  45. def version(self):
  46. p = GitCommand(None, ['--version'], capture_stdout=True)
  47. if p.Wait() == 0:
  48. return p.stdout
  49. return None
  50. def __getattr__(self, name):
  51. name = name.replace('_','-')
  52. def fun(*cmdv):
  53. command = [name]
  54. command.extend(cmdv)
  55. return GitCommand(None, command).Wait() == 0
  56. return fun
  57. git = _GitCall()
  58. class GitCommand(object):
  59. def __init__(self,
  60. project,
  61. cmdv,
  62. bare = False,
  63. provide_stdin = False,
  64. capture_stdout = False,
  65. capture_stderr = False,
  66. disable_editor = False,
  67. ssh_proxy = False,
  68. cwd = None,
  69. gitdir = None):
  70. env = dict(os.environ)
  71. for e in [REPO_TRACE,
  72. GIT_DIR,
  73. 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
  74. 'GIT_OBJECT_DIRECTORY',
  75. 'GIT_WORK_TREE',
  76. 'GIT_GRAFT_FILE',
  77. 'GIT_INDEX_FILE']:
  78. if e in env:
  79. del env[e]
  80. if disable_editor:
  81. env['GIT_EDITOR'] = ':'
  82. if ssh_proxy:
  83. env['REPO_SSH_SOCK'] = _ssh_sock()
  84. env['GIT_SSH'] = _ssh_proxy()
  85. if project:
  86. if not cwd:
  87. cwd = project.worktree
  88. if not gitdir:
  89. gitdir = project.gitdir
  90. command = [GIT]
  91. if bare:
  92. if gitdir:
  93. env[GIT_DIR] = gitdir
  94. cwd = None
  95. command.extend(cmdv)
  96. if provide_stdin:
  97. stdin = subprocess.PIPE
  98. else:
  99. stdin = None
  100. if capture_stdout:
  101. stdout = subprocess.PIPE
  102. else:
  103. stdout = None
  104. if capture_stderr:
  105. stderr = subprocess.PIPE
  106. else:
  107. stderr = None
  108. if IsTrace():
  109. global LAST_CWD
  110. global LAST_GITDIR
  111. dbg = ''
  112. if cwd and LAST_CWD != cwd:
  113. if LAST_GITDIR or LAST_CWD:
  114. dbg += '\n'
  115. dbg += ': cd %s\n' % cwd
  116. LAST_CWD = cwd
  117. if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
  118. if LAST_GITDIR or LAST_CWD:
  119. dbg += '\n'
  120. dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
  121. LAST_GITDIR = env[GIT_DIR]
  122. dbg += ': '
  123. dbg += ' '.join(command)
  124. if stdin == subprocess.PIPE:
  125. dbg += ' 0<|'
  126. if stdout == subprocess.PIPE:
  127. dbg += ' 1>|'
  128. if stderr == subprocess.PIPE:
  129. dbg += ' 2>|'
  130. Trace('%s', dbg)
  131. try:
  132. p = subprocess.Popen(command,
  133. cwd = cwd,
  134. env = env,
  135. stdin = stdin,
  136. stdout = stdout,
  137. stderr = stderr)
  138. except Exception, e:
  139. raise GitError('%s: %s' % (command[1], e))
  140. self.process = p
  141. self.stdin = p.stdin
  142. def Wait(self):
  143. p = self.process
  144. if p.stdin:
  145. p.stdin.close()
  146. self.stdin = None
  147. if p.stdout:
  148. self.stdout = p.stdout.read()
  149. p.stdout.close()
  150. else:
  151. p.stdout = None
  152. if p.stderr:
  153. self.stderr = p.stderr.read()
  154. p.stderr.close()
  155. else:
  156. p.stderr = None
  157. return self.process.wait()