git_command.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. dir = '/tmp'
  34. if not os.path.exists(dir):
  35. dir = tempfile.gettempdir()
  36. _ssh_sock_path = os.path.join(
  37. tempfile.mkdtemp('', 'ssh-', dir),
  38. 'master-%r@%h:%p')
  39. return _ssh_sock_path
  40. def _ssh_proxy():
  41. global _ssh_proxy_path
  42. if _ssh_proxy_path is None:
  43. _ssh_proxy_path = os.path.join(
  44. os.path.dirname(__file__),
  45. 'git_ssh')
  46. return _ssh_proxy_path
  47. class _GitCall(object):
  48. def version(self):
  49. p = GitCommand(None, ['--version'], capture_stdout=True)
  50. if p.Wait() == 0:
  51. return p.stdout
  52. return None
  53. def __getattr__(self, name):
  54. name = name.replace('_','-')
  55. def fun(*cmdv):
  56. command = [name]
  57. command.extend(cmdv)
  58. return GitCommand(None, command).Wait() == 0
  59. return fun
  60. git = _GitCall()
  61. class GitCommand(object):
  62. def __init__(self,
  63. project,
  64. cmdv,
  65. bare = False,
  66. provide_stdin = False,
  67. capture_stdout = False,
  68. capture_stderr = False,
  69. disable_editor = False,
  70. ssh_proxy = False,
  71. cwd = None,
  72. gitdir = None):
  73. env = dict(os.environ)
  74. for e in [REPO_TRACE,
  75. GIT_DIR,
  76. 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
  77. 'GIT_OBJECT_DIRECTORY',
  78. 'GIT_WORK_TREE',
  79. 'GIT_GRAFT_FILE',
  80. 'GIT_INDEX_FILE']:
  81. if e in env:
  82. del env[e]
  83. if disable_editor:
  84. env['GIT_EDITOR'] = ':'
  85. if ssh_proxy:
  86. env['REPO_SSH_SOCK'] = _ssh_sock()
  87. env['GIT_SSH'] = _ssh_proxy()
  88. if project:
  89. if not cwd:
  90. cwd = project.worktree
  91. if not gitdir:
  92. gitdir = project.gitdir
  93. command = [GIT]
  94. if bare:
  95. if gitdir:
  96. env[GIT_DIR] = gitdir
  97. cwd = None
  98. command.extend(cmdv)
  99. if provide_stdin:
  100. stdin = subprocess.PIPE
  101. else:
  102. stdin = None
  103. if capture_stdout:
  104. stdout = subprocess.PIPE
  105. else:
  106. stdout = None
  107. if capture_stderr:
  108. stderr = subprocess.PIPE
  109. else:
  110. stderr = None
  111. if IsTrace():
  112. global LAST_CWD
  113. global LAST_GITDIR
  114. dbg = ''
  115. if cwd and LAST_CWD != cwd:
  116. if LAST_GITDIR or LAST_CWD:
  117. dbg += '\n'
  118. dbg += ': cd %s\n' % cwd
  119. LAST_CWD = cwd
  120. if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
  121. if LAST_GITDIR or LAST_CWD:
  122. dbg += '\n'
  123. dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
  124. LAST_GITDIR = env[GIT_DIR]
  125. dbg += ': '
  126. dbg += ' '.join(command)
  127. if stdin == subprocess.PIPE:
  128. dbg += ' 0<|'
  129. if stdout == subprocess.PIPE:
  130. dbg += ' 1>|'
  131. if stderr == subprocess.PIPE:
  132. dbg += ' 2>|'
  133. Trace('%s', dbg)
  134. try:
  135. p = subprocess.Popen(command,
  136. cwd = cwd,
  137. env = env,
  138. stdin = stdin,
  139. stdout = stdout,
  140. stderr = stderr)
  141. except Exception, e:
  142. raise GitError('%s: %s' % (command[1], e))
  143. self.process = p
  144. self.stdin = p.stdin
  145. def Wait(self):
  146. p = self.process
  147. if p.stdin:
  148. p.stdin.close()
  149. self.stdin = None
  150. if p.stdout:
  151. self.stdout = p.stdout.read()
  152. p.stdout.close()
  153. else:
  154. p.stdout = None
  155. if p.stderr:
  156. self.stderr = p.stderr.read()
  157. p.stderr.close()
  158. else:
  159. p.stderr = None
  160. return self.process.wait()