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