git_command.py 3.7 KB

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