Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: tools/git/git-diff-ide.py

Issue 9562011: Add git diff python tool that outputs file:line in front of diff output. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: thakis feedback Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """
7 Invokes git diff [args...] and inserts file:line in front of each line of diff
8 output where possible.
9
10 This is useful from an IDE that allows you to double-click lines that begin
11 with file:line to open and jump to that point in the file.
12
13 Synopsis:
14 %prog [git diff args...]
15
16 Examples:
17 %prog
18 %prog HEAD
19 """
20
21 import subprocess
22 import sys
23
24
25 def GitShell(args, ignore_return=False):
26 """A shell invocation suitable for communicating with git. Returns
27 output as list of lines, raises exception on error.
28 """
29 job = subprocess.Popen(args,
30 shell=True,
31 stdout=subprocess.PIPE,
32 stderr=subprocess.STDOUT)
33 (out, err) = job.communicate()
34 if job.returncode != 0 and not ignore_return:
35 print out
36 raise Exception("Error %d running command %s" % (
37 job.returncode, args))
38 return out.split('\n')
39
40
41 def PrintGitDiff(extra_args):
42 """Outputs git diff extra_args with file:line inserted into relevant lines."""
43 current_file = '';
44 line_num = 0;
45 lines = GitShell('git diff %s' % ' '.join(extra_args))
46 for line in lines:
47 # Pass-through lines:
48 # diff --git a/file.c b/file.c
49 # index 0e38c2d..8cd69ae 100644
50 # --- a/file.c
51 if (line.startswith('diff ') or
52 line.startswith('index ') or
53 line.startswith('--- ')):
54 print line
55 continue
56
57 # Get the filename from the +++ line:
58 # +++ b/file.c
59 if line.startswith('+++ '):
60 # Filename might be /dev/null or a/file or b/file.
61 # Skip the first two characters unless it starts with /.
62 current_file = line[4:] if line[4] == '/' else line[6:]
63 print line
64 continue
65
66 # Update line number from the @@ lines:
67 # @@ -41,9 +41,9 @@ def MyFunc():
68 # ^^
69 if line.startswith('@@ '):
70 _, old_nr, new_nr, _ = line.split(' ', 3)
71 line_num = int(new_nr.split(',')[0])
72 print line
73 continue
74 print current_file + ':' + repr(line_num) + ':' + line
75
76 # Increment line number for lines that start with ' ' or '+':
77 # @@ -41,4 +41,4 @@ def MyFunc():
78 # file.c:41: // existing code
79 # file.c:42: // existing code
80 # file.c:43:-// deleted code
81 # file.c:43:-// deleted code
82 # file.c:43:+// inserted code
83 # file.c:44:+// inserted code
84 if line.startswith(' ') or line.startswith('+'):
85 line_num += 1
86
87
88 def main():
89 PrintGitDiff(sys.argv[1:])
90
91
92 if __name__ == '__main__':
93 main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698