Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. |
| 2 | # |
| 3 | # See file CREDITS for list of people who contributed to this |
| 4 | # project. |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or |
| 7 | # modify it under the terms of the GNU General Public License as |
| 8 | # published by the Free Software Foundation; either version 2 of |
| 9 | # the License, or (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | # MA 02111-1307 USA |
| 20 | # |
| 21 | |
| 22 | import command |
| 23 | import re |
| 24 | import os |
| 25 | import series |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 26 | import subprocess |
| 27 | import sys |
| 28 | import terminal |
| 29 | |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 30 | import settings |
| 31 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 32 | |
| 33 | def CountCommitsToBranch(): |
| 34 | """Returns number of commits between HEAD and the tracking branch. |
| 35 | |
| 36 | This looks back to the tracking branch and works out the number of commits |
| 37 | since then. |
| 38 | |
| 39 | Return: |
| 40 | Number of patches that exist on top of the branch |
| 41 | """ |
Albert ARIBAUD | 313d448 | 2012-10-15 09:55:50 +0000 | [diff] [blame] | 42 | pipe = [['git', 'log', '--no-color', '--oneline', '@{upstream}..'], |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 43 | ['wc', '-l']] |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 44 | stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 45 | patch_count = int(stdout) |
| 46 | return patch_count |
| 47 | |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 48 | def GetUpstream(git_dir, branch): |
| 49 | """Returns the name of the upstream for a branch |
| 50 | |
| 51 | Args: |
| 52 | git_dir: Git directory containing repo |
| 53 | branch: Name of branch |
| 54 | |
| 55 | Returns: |
| 56 | Name of upstream branch (e.g. 'upstream/master') or None if none |
| 57 | """ |
| 58 | remote = command.OutputOneLine('git', '--git-dir', git_dir, 'config', |
| 59 | 'branch.%s.remote' % branch) |
| 60 | merge = command.OutputOneLine('git', '--git-dir', git_dir, 'config', |
| 61 | 'branch.%s.merge' % branch) |
| 62 | if remote == '.': |
| 63 | return merge |
| 64 | elif remote and merge: |
| 65 | leaf = merge.split('/')[-1] |
| 66 | return '%s/%s' % (remote, leaf) |
| 67 | else: |
| 68 | raise ValueError, ("Cannot determine upstream branch for branch " |
| 69 | "'%s' remote='%s', merge='%s'" % (branch, remote, merge)) |
| 70 | |
| 71 | |
| 72 | def GetRangeInBranch(git_dir, branch, include_upstream=False): |
| 73 | """Returns an expression for the commits in the given branch. |
| 74 | |
| 75 | Args: |
| 76 | git_dir: Directory containing git repo |
| 77 | branch: Name of branch |
| 78 | Return: |
| 79 | Expression in the form 'upstream..branch' which can be used to |
| 80 | access the commits. |
| 81 | """ |
| 82 | upstream = GetUpstream(git_dir, branch) |
| 83 | return '%s%s..%s' % (upstream, '~' if include_upstream else '', branch) |
| 84 | |
| 85 | def CountCommitsInBranch(git_dir, branch, include_upstream=False): |
| 86 | """Returns the number of commits in the given branch. |
| 87 | |
| 88 | Args: |
| 89 | git_dir: Directory containing git repo |
| 90 | branch: Name of branch |
| 91 | Return: |
| 92 | Number of patches that exist on top of the branch |
| 93 | """ |
| 94 | range_expr = GetRangeInBranch(git_dir, branch, include_upstream) |
| 95 | pipe = [['git', '--git-dir', git_dir, 'log', '--oneline', range_expr], |
| 96 | ['wc', '-l']] |
| 97 | result = command.RunPipe(pipe, capture=True, oneline=True) |
| 98 | patch_count = int(result.stdout) |
| 99 | return patch_count |
| 100 | |
| 101 | def CountCommits(commit_range): |
| 102 | """Returns the number of commits in the given range. |
| 103 | |
| 104 | Args: |
| 105 | commit_range: Range of commits to count (e.g. 'HEAD..base') |
| 106 | Return: |
| 107 | Number of patches that exist on top of the branch |
| 108 | """ |
| 109 | pipe = [['git', 'log', '--oneline', commit_range], |
| 110 | ['wc', '-l']] |
| 111 | stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout |
| 112 | patch_count = int(stdout) |
| 113 | return patch_count |
| 114 | |
| 115 | def Checkout(commit_hash, git_dir=None, work_tree=None, force=False): |
| 116 | """Checkout the selected commit for this build |
| 117 | |
| 118 | Args: |
| 119 | commit_hash: Commit hash to check out |
| 120 | """ |
| 121 | pipe = ['git'] |
| 122 | if git_dir: |
| 123 | pipe.extend(['--git-dir', git_dir]) |
| 124 | if work_tree: |
| 125 | pipe.extend(['--work-tree', work_tree]) |
| 126 | pipe.append('checkout') |
| 127 | if force: |
| 128 | pipe.append('-f') |
| 129 | pipe.append(commit_hash) |
| 130 | result = command.RunPipe([pipe], capture=True, raise_on_error=False) |
| 131 | if result.return_code != 0: |
| 132 | raise OSError, 'git checkout (%s): %s' % (pipe, result.stderr) |
| 133 | |
| 134 | def Clone(git_dir, output_dir): |
| 135 | """Checkout the selected commit for this build |
| 136 | |
| 137 | Args: |
| 138 | commit_hash: Commit hash to check out |
| 139 | """ |
| 140 | pipe = ['git', 'clone', git_dir, '.'] |
| 141 | result = command.RunPipe([pipe], capture=True, cwd=output_dir) |
| 142 | if result.return_code != 0: |
| 143 | raise OSError, 'git clone: %s' % result.stderr |
| 144 | |
| 145 | def Fetch(git_dir=None, work_tree=None): |
| 146 | """Fetch from the origin repo |
| 147 | |
| 148 | Args: |
| 149 | commit_hash: Commit hash to check out |
| 150 | """ |
| 151 | pipe = ['git'] |
| 152 | if git_dir: |
| 153 | pipe.extend(['--git-dir', git_dir]) |
| 154 | if work_tree: |
| 155 | pipe.extend(['--work-tree', work_tree]) |
| 156 | pipe.append('fetch') |
| 157 | result = command.RunPipe([pipe], capture=True) |
| 158 | if result.return_code != 0: |
| 159 | raise OSError, 'git fetch: %s' % result.stderr |
| 160 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 161 | def CreatePatches(start, count, series): |
| 162 | """Create a series of patches from the top of the current branch. |
| 163 | |
| 164 | The patch files are written to the current directory using |
| 165 | git format-patch. |
| 166 | |
| 167 | Args: |
| 168 | start: Commit to start from: 0=HEAD, 1=next one, etc. |
| 169 | count: number of commits to include |
| 170 | Return: |
| 171 | Filename of cover letter |
| 172 | List of filenames of patch files |
| 173 | """ |
| 174 | if series.get('version'): |
| 175 | version = '%s ' % series['version'] |
| 176 | cmd = ['git', 'format-patch', '-M', '--signoff'] |
| 177 | if series.get('cover'): |
| 178 | cmd.append('--cover-letter') |
| 179 | prefix = series.GetPatchPrefix() |
| 180 | if prefix: |
| 181 | cmd += ['--subject-prefix=%s' % prefix] |
| 182 | cmd += ['HEAD~%d..HEAD~%d' % (start + count, start)] |
| 183 | |
| 184 | stdout = command.RunList(cmd) |
| 185 | files = stdout.splitlines() |
| 186 | |
| 187 | # We have an extra file if there is a cover letter |
| 188 | if series.get('cover'): |
| 189 | return files[0], files[1:] |
| 190 | else: |
| 191 | return None, files |
| 192 | |
| 193 | def ApplyPatch(verbose, fname): |
| 194 | """Apply a patch with git am to test it |
| 195 | |
| 196 | TODO: Convert these to use command, with stderr option |
| 197 | |
| 198 | Args: |
| 199 | fname: filename of patch file to apply |
| 200 | """ |
| 201 | cmd = ['git', 'am', fname] |
| 202 | pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 203 | stderr=subprocess.PIPE) |
| 204 | stdout, stderr = pipe.communicate() |
| 205 | re_error = re.compile('^error: patch failed: (.+):(\d+)') |
| 206 | for line in stderr.splitlines(): |
| 207 | if verbose: |
| 208 | print line |
| 209 | match = re_error.match(line) |
| 210 | if match: |
| 211 | print GetWarningMsg('warning', match.group(1), int(match.group(2)), |
| 212 | 'Patch failed') |
| 213 | return pipe.returncode == 0, stdout |
| 214 | |
| 215 | def ApplyPatches(verbose, args, start_point): |
| 216 | """Apply the patches with git am to make sure all is well |
| 217 | |
| 218 | Args: |
| 219 | verbose: Print out 'git am' output verbatim |
| 220 | args: List of patch files to apply |
| 221 | start_point: Number of commits back from HEAD to start applying. |
| 222 | Normally this is len(args), but it can be larger if a start |
| 223 | offset was given. |
| 224 | """ |
| 225 | error_count = 0 |
| 226 | col = terminal.Color() |
| 227 | |
| 228 | # Figure out our current position |
| 229 | cmd = ['git', 'name-rev', 'HEAD', '--name-only'] |
| 230 | pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 231 | stdout, stderr = pipe.communicate() |
| 232 | if pipe.returncode: |
| 233 | str = 'Could not find current commit name' |
| 234 | print col.Color(col.RED, str) |
| 235 | print stdout |
| 236 | return False |
| 237 | old_head = stdout.splitlines()[0] |
| 238 | |
| 239 | # Checkout the required start point |
| 240 | cmd = ['git', 'checkout', 'HEAD~%d' % start_point] |
| 241 | pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 242 | stderr=subprocess.PIPE) |
| 243 | stdout, stderr = pipe.communicate() |
| 244 | if pipe.returncode: |
| 245 | str = 'Could not move to commit before patch series' |
| 246 | print col.Color(col.RED, str) |
| 247 | print stdout, stderr |
| 248 | return False |
| 249 | |
| 250 | # Apply all the patches |
| 251 | for fname in args: |
| 252 | ok, stdout = ApplyPatch(verbose, fname) |
| 253 | if not ok: |
| 254 | print col.Color(col.RED, 'git am returned errors for %s: will ' |
| 255 | 'skip this patch' % fname) |
| 256 | if verbose: |
| 257 | print stdout |
| 258 | error_count += 1 |
| 259 | cmd = ['git', 'am', '--skip'] |
| 260 | pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 261 | stdout, stderr = pipe.communicate() |
| 262 | if pipe.returncode != 0: |
| 263 | print col.Color(col.RED, 'Unable to skip patch! Aborting...') |
| 264 | print stdout |
| 265 | break |
| 266 | |
| 267 | # Return to our previous position |
| 268 | cmd = ['git', 'checkout', old_head] |
| 269 | pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 270 | stdout, stderr = pipe.communicate() |
| 271 | if pipe.returncode: |
| 272 | print col.Color(col.RED, 'Could not move back to head commit') |
| 273 | print stdout, stderr |
| 274 | return error_count == 0 |
| 275 | |
| 276 | def BuildEmailList(in_list, tag=None, alias=None): |
| 277 | """Build a list of email addresses based on an input list. |
| 278 | |
| 279 | Takes a list of email addresses and aliases, and turns this into a list |
| 280 | of only email address, by resolving any aliases that are present. |
| 281 | |
| 282 | If the tag is given, then each email address is prepended with this |
| 283 | tag and a space. If the tag starts with a minus sign (indicating a |
| 284 | command line parameter) then the email address is quoted. |
| 285 | |
| 286 | Args: |
| 287 | in_list: List of aliases/email addresses |
| 288 | tag: Text to put before each address |
| 289 | |
| 290 | Returns: |
| 291 | List of email addresses |
| 292 | |
| 293 | >>> alias = {} |
| 294 | >>> alias['fred'] = ['f.bloggs@napier.co.nz'] |
| 295 | >>> alias['john'] = ['j.bloggs@napier.co.nz'] |
| 296 | >>> alias['mary'] = ['Mary Poppins <m.poppins@cloud.net>'] |
| 297 | >>> alias['boys'] = ['fred', ' john'] |
| 298 | >>> alias['all'] = ['fred ', 'john', ' mary '] |
| 299 | >>> BuildEmailList(['john', 'mary'], None, alias) |
| 300 | ['j.bloggs@napier.co.nz', 'Mary Poppins <m.poppins@cloud.net>'] |
| 301 | >>> BuildEmailList(['john', 'mary'], '--to', alias) |
| 302 | ['--to "j.bloggs@napier.co.nz"', \ |
| 303 | '--to "Mary Poppins <m.poppins@cloud.net>"'] |
| 304 | >>> BuildEmailList(['john', 'mary'], 'Cc', alias) |
| 305 | ['Cc j.bloggs@napier.co.nz', 'Cc Mary Poppins <m.poppins@cloud.net>'] |
| 306 | """ |
| 307 | quote = '"' if tag and tag[0] == '-' else '' |
| 308 | raw = [] |
| 309 | for item in in_list: |
| 310 | raw += LookupEmail(item, alias) |
| 311 | result = [] |
| 312 | for item in raw: |
| 313 | if not item in result: |
| 314 | result.append(item) |
| 315 | if tag: |
| 316 | return ['%s %s%s%s' % (tag, quote, email, quote) for email in result] |
| 317 | return result |
| 318 | |
| 319 | def EmailPatches(series, cover_fname, args, dry_run, cc_fname, |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 320 | self_only=False, alias=None, in_reply_to=None): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 321 | """Email a patch series. |
| 322 | |
| 323 | Args: |
| 324 | series: Series object containing destination info |
| 325 | cover_fname: filename of cover letter |
| 326 | args: list of filenames of patch files |
| 327 | dry_run: Just return the command that would be run |
| 328 | cc_fname: Filename of Cc file for per-commit Cc |
| 329 | self_only: True to just email to yourself as a test |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 330 | in_reply_to: If set we'll pass this to git as --in-reply-to. |
| 331 | Should be a message ID that this is in reply to. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 332 | |
| 333 | Returns: |
| 334 | Git command that was/would be run |
| 335 | |
Doug Anderson | 51d7321 | 2012-11-26 15:21:40 +0000 | [diff] [blame] | 336 | # For the duration of this doctest pretend that we ran patman with ./patman |
| 337 | >>> _old_argv0 = sys.argv[0] |
| 338 | >>> sys.argv[0] = './patman' |
| 339 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 340 | >>> alias = {} |
| 341 | >>> alias['fred'] = ['f.bloggs@napier.co.nz'] |
| 342 | >>> alias['john'] = ['j.bloggs@napier.co.nz'] |
| 343 | >>> alias['mary'] = ['m.poppins@cloud.net'] |
| 344 | >>> alias['boys'] = ['fred', ' john'] |
| 345 | >>> alias['all'] = ['fred ', 'john', ' mary '] |
| 346 | >>> alias[os.getenv('USER')] = ['this-is-me@me.com'] |
| 347 | >>> series = series.Series() |
| 348 | >>> series.to = ['fred'] |
| 349 | >>> series.cc = ['mary'] |
| 350 | >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, 'cc-fname', False, \ |
| 351 | alias) |
| 352 | 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \ |
| 353 | "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2' |
| 354 | >>> EmailPatches(series, None, ['p1'], True, 'cc-fname', False, alias) |
| 355 | 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \ |
| 356 | "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" p1' |
| 357 | >>> series.cc = ['all'] |
| 358 | >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, 'cc-fname', True, \ |
| 359 | alias) |
| 360 | 'git send-email --annotate --to "this-is-me@me.com" --cc-cmd "./patman \ |
| 361 | --cc-cmd cc-fname" cover p1 p2' |
| 362 | >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, 'cc-fname', False, \ |
| 363 | alias) |
| 364 | 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \ |
| 365 | "f.bloggs@napier.co.nz" --cc "j.bloggs@napier.co.nz" --cc \ |
| 366 | "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2' |
Doug Anderson | 51d7321 | 2012-11-26 15:21:40 +0000 | [diff] [blame] | 367 | |
| 368 | # Restore argv[0] since we clobbered it. |
| 369 | >>> sys.argv[0] = _old_argv0 |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 370 | """ |
| 371 | to = BuildEmailList(series.get('to'), '--to', alias) |
| 372 | if not to: |
| 373 | print ("No recipient, please add something like this to a commit\n" |
| 374 | "Series-to: Fred Bloggs <f.blogs@napier.co.nz>") |
| 375 | return |
| 376 | cc = BuildEmailList(series.get('cc'), '--cc', alias) |
| 377 | if self_only: |
| 378 | to = BuildEmailList([os.getenv('USER')], '--to', alias) |
| 379 | cc = [] |
| 380 | cmd = ['git', 'send-email', '--annotate'] |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 381 | if in_reply_to: |
| 382 | cmd.append('--in-reply-to="%s"' % in_reply_to) |
| 383 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 384 | cmd += to |
| 385 | cmd += cc |
| 386 | cmd += ['--cc-cmd', '"%s --cc-cmd %s"' % (sys.argv[0], cc_fname)] |
| 387 | if cover_fname: |
| 388 | cmd.append(cover_fname) |
| 389 | cmd += args |
| 390 | str = ' '.join(cmd) |
| 391 | if not dry_run: |
| 392 | os.system(str) |
| 393 | return str |
| 394 | |
| 395 | |
| 396 | def LookupEmail(lookup_name, alias=None, level=0): |
| 397 | """If an email address is an alias, look it up and return the full name |
| 398 | |
| 399 | TODO: Why not just use git's own alias feature? |
| 400 | |
| 401 | Args: |
| 402 | lookup_name: Alias or email address to look up |
| 403 | |
| 404 | Returns: |
| 405 | tuple: |
| 406 | list containing a list of email addresses |
| 407 | |
| 408 | Raises: |
| 409 | OSError if a recursive alias reference was found |
| 410 | ValueError if an alias was not found |
| 411 | |
| 412 | >>> alias = {} |
| 413 | >>> alias['fred'] = ['f.bloggs@napier.co.nz'] |
| 414 | >>> alias['john'] = ['j.bloggs@napier.co.nz'] |
| 415 | >>> alias['mary'] = ['m.poppins@cloud.net'] |
| 416 | >>> alias['boys'] = ['fred', ' john', 'f.bloggs@napier.co.nz'] |
| 417 | >>> alias['all'] = ['fred ', 'john', ' mary '] |
| 418 | >>> alias['loop'] = ['other', 'john', ' mary '] |
| 419 | >>> alias['other'] = ['loop', 'john', ' mary '] |
| 420 | >>> LookupEmail('mary', alias) |
| 421 | ['m.poppins@cloud.net'] |
| 422 | >>> LookupEmail('arthur.wellesley@howe.ro.uk', alias) |
| 423 | ['arthur.wellesley@howe.ro.uk'] |
| 424 | >>> LookupEmail('boys', alias) |
| 425 | ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz'] |
| 426 | >>> LookupEmail('all', alias) |
| 427 | ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz', 'm.poppins@cloud.net'] |
| 428 | >>> LookupEmail('odd', alias) |
| 429 | Traceback (most recent call last): |
| 430 | ... |
| 431 | ValueError: Alias 'odd' not found |
| 432 | >>> LookupEmail('loop', alias) |
| 433 | Traceback (most recent call last): |
| 434 | ... |
| 435 | OSError: Recursive email alias at 'other' |
| 436 | """ |
| 437 | if not alias: |
| 438 | alias = settings.alias |
| 439 | lookup_name = lookup_name.strip() |
| 440 | if '@' in lookup_name: # Perhaps a real email address |
| 441 | return [lookup_name] |
| 442 | |
| 443 | lookup_name = lookup_name.lower() |
| 444 | |
| 445 | if level > 10: |
| 446 | raise OSError, "Recursive email alias at '%s'" % lookup_name |
| 447 | |
| 448 | out_list = [] |
| 449 | if lookup_name: |
| 450 | if not lookup_name in alias: |
| 451 | raise ValueError, "Alias '%s' not found" % lookup_name |
| 452 | for item in alias[lookup_name]: |
| 453 | todo = LookupEmail(item, alias, level + 1) |
| 454 | for new_item in todo: |
| 455 | if not new_item in out_list: |
| 456 | out_list.append(new_item) |
| 457 | |
| 458 | #print "No match for alias '%s'" % lookup_name |
| 459 | return out_list |
| 460 | |
| 461 | def GetTopLevel(): |
| 462 | """Return name of top-level directory for this git repo. |
| 463 | |
| 464 | Returns: |
| 465 | Full path to git top-level directory |
| 466 | |
| 467 | This test makes sure that we are running tests in the right subdir |
| 468 | |
Doug Anderson | 51d7321 | 2012-11-26 15:21:40 +0000 | [diff] [blame] | 469 | >>> os.path.realpath(os.path.dirname(__file__)) == \ |
| 470 | os.path.join(GetTopLevel(), 'tools', 'patman') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 471 | True |
| 472 | """ |
| 473 | return command.OutputOneLine('git', 'rev-parse', '--show-toplevel') |
| 474 | |
| 475 | def GetAliasFile(): |
| 476 | """Gets the name of the git alias file. |
| 477 | |
| 478 | Returns: |
| 479 | Filename of git alias file, or None if none |
| 480 | """ |
Simon Glass | 519fad2 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 481 | fname = command.OutputOneLine('git', 'config', 'sendemail.aliasesfile', |
| 482 | raise_on_error=False) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 483 | if fname: |
| 484 | fname = os.path.join(GetTopLevel(), fname.strip()) |
| 485 | return fname |
| 486 | |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 487 | def GetDefaultUserName(): |
| 488 | """Gets the user.name from .gitconfig file. |
| 489 | |
| 490 | Returns: |
| 491 | User name found in .gitconfig file, or None if none |
| 492 | """ |
| 493 | uname = command.OutputOneLine('git', 'config', '--global', 'user.name') |
| 494 | return uname |
| 495 | |
| 496 | def GetDefaultUserEmail(): |
| 497 | """Gets the user.email from the global .gitconfig file. |
| 498 | |
| 499 | Returns: |
| 500 | User's email found in .gitconfig file, or None if none |
| 501 | """ |
| 502 | uemail = command.OutputOneLine('git', 'config', '--global', 'user.email') |
| 503 | return uemail |
| 504 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 505 | def Setup(): |
| 506 | """Set up git utils, by reading the alias files.""" |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 507 | # Check for a git alias file also |
| 508 | alias_fname = GetAliasFile() |
| 509 | if alias_fname: |
| 510 | settings.ReadGitAliases(alias_fname) |
| 511 | |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 512 | def GetHead(): |
| 513 | """Get the hash of the current HEAD |
| 514 | |
| 515 | Returns: |
| 516 | Hash of HEAD |
| 517 | """ |
| 518 | return command.OutputOneLine('git', 'show', '-s', '--pretty=format:%H') |
| 519 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 520 | if __name__ == "__main__": |
| 521 | import doctest |
| 522 | |
| 523 | doctest.testmod() |