git_command.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. from error import GitError
  19. GIT = 'git'
  20. MIN_GIT_VERSION = (1, 5, 4)
  21. GIT_DIR = 'GIT_DIR'
  22. REPO_TRACE = 'REPO_TRACE'
  23. LAST_GITDIR = None
  24. LAST_CWD = None
  25. try:
  26. TRACE = os.environ[REPO_TRACE] == '1'
  27. except KeyError:
  28. TRACE = False
  29. class _GitCall(object):
  30. def version(self):
  31. p = GitCommand(None, ['--version'], capture_stdout=True)
  32. if p.Wait() == 0:
  33. return p.stdout
  34. return None
  35. def __getattr__(self, name):
  36. name = name.replace('_','-')
  37. def fun(*cmdv):
  38. command = [name]
  39. command.extend(cmdv)
  40. return GitCommand(None, command).Wait() == 0
  41. return fun
  42. git = _GitCall()
  43. class GitCommand(object):
  44. def __init__(self,
  45. project,
  46. cmdv,
  47. bare = False,
  48. provide_stdin = False,
  49. capture_stdout = False,
  50. capture_stderr = False,
  51. disable_editor = False,
  52. cwd = None,
  53. gitdir = None):
  54. env = dict(os.environ)
  55. for e in [REPO_TRACE,
  56. GIT_DIR,
  57. 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
  58. 'GIT_OBJECT_DIRECTORY',
  59. 'GIT_WORK_TREE',
  60. 'GIT_GRAFT_FILE',
  61. 'GIT_INDEX_FILE']:
  62. if e in env:
  63. del env[e]
  64. if disable_editor:
  65. env['GIT_EDITOR'] = ':'
  66. if project:
  67. if not cwd:
  68. cwd = project.worktree
  69. if not gitdir:
  70. gitdir = project.gitdir
  71. command = [GIT]
  72. if bare:
  73. if gitdir:
  74. env[GIT_DIR] = gitdir
  75. cwd = None
  76. command.extend(cmdv)
  77. if provide_stdin:
  78. stdin = subprocess.PIPE
  79. else:
  80. stdin = None
  81. if capture_stdout:
  82. stdout = subprocess.PIPE
  83. else:
  84. stdout = None
  85. if capture_stderr:
  86. stderr = subprocess.PIPE
  87. else:
  88. stderr = None
  89. if TRACE:
  90. global LAST_CWD
  91. global LAST_GITDIR
  92. dbg = ''
  93. if cwd and LAST_CWD != cwd:
  94. if LAST_GITDIR or LAST_CWD:
  95. dbg += '\n'
  96. dbg += ': cd %s\n' % cwd
  97. LAST_CWD = cwd
  98. if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
  99. if LAST_GITDIR or LAST_CWD:
  100. dbg += '\n'
  101. dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
  102. LAST_GITDIR = env[GIT_DIR]
  103. dbg += ': '
  104. dbg += ' '.join(command)
  105. if stdin == subprocess.PIPE:
  106. dbg += ' 0<|'
  107. if stdout == subprocess.PIPE:
  108. dbg += ' 1>|'
  109. if stderr == subprocess.PIPE:
  110. dbg += ' 2>|'
  111. print >>sys.stderr, dbg
  112. try:
  113. p = subprocess.Popen(command,
  114. cwd = cwd,
  115. env = env,
  116. stdin = stdin,
  117. stdout = stdout,
  118. stderr = stderr)
  119. except Exception, e:
  120. raise GitError('%s: %s' % (command[1], e))
  121. self.process = p
  122. self.stdin = p.stdin
  123. def Wait(self):
  124. p = self.process
  125. if p.stdin:
  126. p.stdin.close()
  127. self.stdin = None
  128. if p.stdout:
  129. self.stdout = p.stdout.read()
  130. p.stdout.close()
  131. else:
  132. p.stdout = None
  133. if p.stderr:
  134. self.stderr = p.stderr.read()
  135. p.stderr.close()
  136. else:
  137. p.stderr = None
  138. return self.process.wait()