File: git_upstream_diff.py

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (74 lines) | stat: -rwxr-xr-x 2,416 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import sys

import subprocess2

import gclient_utils
import git_common as git


def main(args):
    if gclient_utils.IsEnvCog():
        print(
            'upstream-diff command is not supported. Please navigate to source '
            'control view in the activity bar to check the diff.',
            file=sys.stderr)
        return 1
    default_args = git.get_config_list('depot-tools.upstream-diff.default-args')
    args = default_args + args

    current_branch = git.current_branch()

    parser = argparse.ArgumentParser()
    parser.add_argument('--wordwise',
                        action='store_true',
                        default=False,
                        help=('Print a colorized wordwise diff '
                              'instead of line-wise diff'))
    parser.add_argument('--branch',
                        default=current_branch,
                        help='Show changes from a different branch. Passing '
                        '"HEAD" is the same as omitting this option (it '
                        'diffs against the current branch)')
    opts, extra_args = parser.parse_known_args(args)

    if opts.branch == 'HEAD':
        opts.branch = current_branch

    if not opts.branch or opts.branch == 'HEAD':
        print('fatal: Cannot perform git-upstream-diff while not on a branch')
        return 1

    par = git.upstream(opts.branch)
    if not par:
        print('fatal: No upstream configured for branch \'%s\'' % opts.branch)
        return 1

    cmd = [
        git.GIT_EXE, '-c', 'core.quotePath=false', 'diff', '--patience', '-C',
        '-C'
    ]
    if opts.wordwise:
        cmd += ['--word-diff=color', r'--word-diff-regex=(\w+|[^[:space:]])']
    cmd += [git.get_or_create_merge_base(opts.branch, par)]
    # Only specify the end commit if it is not the current branch, this lets the
    # diff include uncommitted changes when diffing the current branch.
    if opts.branch != current_branch:
        cmd += [opts.branch]

    cmd += extra_args

    return subprocess2.check_call(cmd)


if __name__ == '__main__':
    try:
        sys.exit(main(sys.argv[1:]))
    except KeyboardInterrupt:
        sys.stderr.write('interrupted\n')
        sys.exit(1)