blob: 7ff1abaccbacaaf112c2bad598be362e6f3dbdb2 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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
16import os
17import sys
18import subprocess
Shawn O. Pearcefb231612009-04-10 18:53:46 -070019import tempfile
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020from error import GitError
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070021from trace import REPO_TRACE, IsTrace, Trace
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022
23GIT = 'git'
24MIN_GIT_VERSION = (1, 5, 4)
25GIT_DIR = 'GIT_DIR'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026
27LAST_GITDIR = None
28LAST_CWD = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029
Shawn O. Pearcefb231612009-04-10 18:53:46 -070030_ssh_proxy_path = None
31_ssh_sock_path = None
32
33def _ssh_sock(create=True):
34 global _ssh_sock_path
35 if _ssh_sock_path is None:
36 if not create:
37 return None
Shawn O. Pearced63bbf42009-04-21 08:05:27 -070038 dir = '/tmp'
39 if not os.path.exists(dir):
40 dir = tempfile.gettempdir()
Shawn O. Pearcefb231612009-04-10 18:53:46 -070041 _ssh_sock_path = os.path.join(
Shawn O. Pearced63bbf42009-04-21 08:05:27 -070042 tempfile.mkdtemp('', 'ssh-', dir),
Shawn O. Pearcefb231612009-04-10 18:53:46 -070043 'master-%r@%h:%p')
44 return _ssh_sock_path
45
46def _ssh_proxy():
47 global _ssh_proxy_path
48 if _ssh_proxy_path is None:
49 _ssh_proxy_path = os.path.join(
50 os.path.dirname(__file__),
51 'git_ssh')
52 return _ssh_proxy_path
53
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070054
55class _GitCall(object):
56 def version(self):
57 p = GitCommand(None, ['--version'], capture_stdout=True)
58 if p.Wait() == 0:
59 return p.stdout
60 return None
61
62 def __getattr__(self, name):
63 name = name.replace('_','-')
64 def fun(*cmdv):
65 command = [name]
66 command.extend(cmdv)
67 return GitCommand(None, command).Wait() == 0
68 return fun
69git = _GitCall()
70
71class GitCommand(object):
72 def __init__(self,
73 project,
74 cmdv,
75 bare = False,
76 provide_stdin = False,
77 capture_stdout = False,
78 capture_stderr = False,
79 disable_editor = False,
Shawn O. Pearcefb231612009-04-10 18:53:46 -070080 ssh_proxy = False,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081 cwd = None,
82 gitdir = None):
83 env = dict(os.environ)
84
85 for e in [REPO_TRACE,
86 GIT_DIR,
87 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
88 'GIT_OBJECT_DIRECTORY',
89 'GIT_WORK_TREE',
90 'GIT_GRAFT_FILE',
91 'GIT_INDEX_FILE']:
92 if e in env:
93 del env[e]
94
95 if disable_editor:
96 env['GIT_EDITOR'] = ':'
Shawn O. Pearcefb231612009-04-10 18:53:46 -070097 if ssh_proxy:
98 env['REPO_SSH_SOCK'] = _ssh_sock()
99 env['GIT_SSH'] = _ssh_proxy()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700100
101 if project:
102 if not cwd:
103 cwd = project.worktree
104 if not gitdir:
105 gitdir = project.gitdir
106
107 command = [GIT]
108 if bare:
109 if gitdir:
110 env[GIT_DIR] = gitdir
111 cwd = None
112 command.extend(cmdv)
113
114 if provide_stdin:
115 stdin = subprocess.PIPE
116 else:
117 stdin = None
118
119 if capture_stdout:
120 stdout = subprocess.PIPE
121 else:
122 stdout = None
123
124 if capture_stderr:
125 stderr = subprocess.PIPE
126 else:
127 stderr = None
128
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700129 if IsTrace():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130 global LAST_CWD
131 global LAST_GITDIR
132
133 dbg = ''
134
135 if cwd and LAST_CWD != cwd:
136 if LAST_GITDIR or LAST_CWD:
137 dbg += '\n'
138 dbg += ': cd %s\n' % cwd
139 LAST_CWD = cwd
140
141 if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
142 if LAST_GITDIR or LAST_CWD:
143 dbg += '\n'
144 dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
145 LAST_GITDIR = env[GIT_DIR]
146
147 dbg += ': '
148 dbg += ' '.join(command)
149 if stdin == subprocess.PIPE:
150 dbg += ' 0<|'
151 if stdout == subprocess.PIPE:
152 dbg += ' 1>|'
153 if stderr == subprocess.PIPE:
154 dbg += ' 2>|'
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700155 Trace('%s', dbg)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700156
157 try:
158 p = subprocess.Popen(command,
159 cwd = cwd,
160 env = env,
161 stdin = stdin,
162 stdout = stdout,
163 stderr = stderr)
164 except Exception, e:
165 raise GitError('%s: %s' % (command[1], e))
166
167 self.process = p
168 self.stdin = p.stdin
169
170 def Wait(self):
171 p = self.process
172
173 if p.stdin:
174 p.stdin.close()
175 self.stdin = None
176
177 if p.stdout:
178 self.stdout = p.stdout.read()
179 p.stdout.close()
180 else:
181 p.stdout = None
182
183 if p.stderr:
184 self.stderr = p.stderr.read()
185 p.stderr.close()
186 else:
187 p.stderr = None
188
189 return self.process.wait()