OLD | NEW |
1 # Copyright 2008 Google Inc. | 1 # Copyright 2008 Google Inc. |
2 # | 2 # |
3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
6 # | 6 # |
7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
8 # | 8 # |
9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 import django.template | 50 import django.template |
51 from django.template import RequestContext | 51 from django.template import RequestContext |
52 from django.utils import simplejson | 52 from django.utils import simplejson |
53 from django.utils.safestring import mark_safe | 53 from django.utils.safestring import mark_safe |
54 from django.core.urlresolvers import reverse | 54 from django.core.urlresolvers import reverse |
55 | 55 |
56 import engine | 56 import engine |
57 import library | 57 import library |
58 import models | 58 import models |
59 import patching | 59 import patching |
| 60 from codereview.exceptions import FetchError |
60 | 61 |
61 | 62 |
62 # Add our own template library. | 63 # Add our own template library. |
63 _library_name = __name__.rsplit('.', 1)[0] + '.library' | 64 _library_name = __name__.rsplit('.', 1)[0] + '.library' |
64 if not django.template.libraries.get(_library_name, None): | 65 if not django.template.libraries.get(_library_name, None): |
65 django.template.add_to_builtins(_library_name) | 66 django.template.add_to_builtins(_library_name) |
66 | 67 |
67 | 68 |
68 ### Constants ### | 69 ### Constants ### |
69 | 70 |
(...skipping 2329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2399 | 2400 |
2400 patchsets = list(request.issue.patchset_set.order('created')) | 2401 patchsets = list(request.issue.patchset_set.order('created')) |
2401 | 2402 |
2402 context = _get_context_for_user(request) | 2403 context = _get_context_for_user(request) |
2403 column_width = _get_column_width_for_user(request) | 2404 column_width = _get_column_width_for_user(request) |
2404 if patch.is_binary: | 2405 if patch.is_binary: |
2405 rows = None | 2406 rows = None |
2406 else: | 2407 else: |
2407 try: | 2408 try: |
2408 rows = _get_diff_table_rows(request, patch, context, column_width) | 2409 rows = _get_diff_table_rows(request, patch, context, column_width) |
2409 except engine.FetchError, err: | 2410 except FetchError, err: |
2410 return HttpResponseNotFound(str(err)) | 2411 return HttpResponseNotFound(str(err)) |
2411 | 2412 |
2412 _add_next_prev(patchset, patch) | 2413 _add_next_prev(patchset, patch) |
2413 return respond(request, 'diff.html', | 2414 return respond(request, 'diff.html', |
2414 {'issue': request.issue, | 2415 {'issue': request.issue, |
2415 'patchset': patchset, | 2416 'patchset': patchset, |
2416 'patch': patch, | 2417 'patch': patch, |
2417 'view_style': 'diff', | 2418 'view_style': 'diff', |
2418 'rows': rows, | 2419 'rows': rows, |
2419 'context': context, | 2420 'context': context, |
2420 'context_values': models.CONTEXT_CHOICES, | 2421 'context_values': models.CONTEXT_CHOICES, |
2421 'column_width': column_width, | 2422 'column_width': column_width, |
2422 'patchsets': patchsets, | 2423 'patchsets': patchsets, |
2423 }) | 2424 }) |
2424 | 2425 |
2425 | 2426 |
2426 def _get_diff_table_rows(request, patch, context, column_width): | 2427 def _get_diff_table_rows(request, patch, context, column_width): |
2427 """Helper function that returns rendered rows for a patch. | 2428 """Helper function that returns rendered rows for a patch. |
2428 | 2429 |
2429 Raises: | 2430 Raises: |
2430 engine.FetchError if patch parsing or download of base files fails. | 2431 FetchError if patch parsing or download of base files fails. |
2431 """ | 2432 """ |
2432 chunks = patching.ParsePatchToChunks(patch.lines, patch.filename) | 2433 chunks = patching.ParsePatchToChunks(patch.lines, patch.filename) |
2433 if chunks is None: | 2434 if chunks is None: |
2434 raise engine.FetchError('Can\'t parse the patch to chunks') | 2435 raise FetchError('Can\'t parse the patch to chunks') |
2435 | 2436 |
2436 # Possible engine.FetchErrors are handled in diff() and diff_skipped_lines(). | 2437 # Possible FetchErrors are handled in diff() and diff_skipped_lines(). |
2437 content = request.patch.get_content() | 2438 content = request.patch.get_content() |
2438 | 2439 |
2439 rows = list(engine.RenderDiffTableRows(request, content.lines, | 2440 rows = list(engine.RenderDiffTableRows(request, content.lines, |
2440 chunks, patch, | 2441 chunks, patch, |
2441 context=context, | 2442 context=context, |
2442 colwidth=column_width)) | 2443 colwidth=column_width)) |
2443 if rows and rows[-1] is None: | 2444 if rows and rows[-1] is None: |
2444 del rows[-1] | 2445 del rows[-1] |
2445 # Get rid of content, which may be bad | 2446 # Get rid of content, which may be bad |
2446 if content.is_uploaded and content.text != None: | 2447 if content.is_uploaded and content.text != None: |
(...skipping 24 matching lines...) Expand all Loading... |
2471 if where == 'a': | 2472 if where == 'a': |
2472 context = None | 2473 context = None |
2473 else: | 2474 else: |
2474 context = _get_context_for_user(request) or 100 | 2475 context = _get_context_for_user(request) or 100 |
2475 | 2476 |
2476 column_width = _clean_int(column_width, engine.DEFAULT_COLUMN_WIDTH, | 2477 column_width = _clean_int(column_width, engine.DEFAULT_COLUMN_WIDTH, |
2477 engine.MIN_COLUMN_WIDTH, engine.MAX_COLUMN_WIDTH) | 2478 engine.MIN_COLUMN_WIDTH, engine.MAX_COLUMN_WIDTH) |
2478 | 2479 |
2479 try: | 2480 try: |
2480 rows = _get_diff_table_rows(request, patch, None, column_width) | 2481 rows = _get_diff_table_rows(request, patch, None, column_width) |
2481 except engine.FetchError, err: | 2482 except FetchError, err: |
2482 return HttpResponse('Error: %s; please report!' % err, status=500) | 2483 return HttpResponse('Error: %s; please report!' % err, status=500) |
2483 return _get_skipped_lines_response(rows, id_before, id_after, where, context) | 2484 return _get_skipped_lines_response(rows, id_before, id_after, where, context) |
2484 | 2485 |
2485 | 2486 |
2486 # there's no easy way to put a control character into a regex, so brute-force it | 2487 # there's no easy way to put a control character into a regex, so brute-force it |
2487 # this is all control characters except \r, \n, and \t | 2488 # this is all control characters except \r, \n, and \t |
2488 _badchars_re = re.compile( | 2489 _badchars_re = re.compile( |
2489 r'[\000\001\002\003\004\005\006\007\010\013\014\016\017' | 2490 r'[\000\001\002\003\004\005\006\007\010\013\014\016\017' |
2490 r'\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037]') | 2491 r'\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037]') |
2491 | 2492 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2559 patch_right.patchset = ps_right | 2560 patch_right.patchset = ps_right |
2560 if patch_filename is None: | 2561 if patch_filename is None: |
2561 patch_filename = patch_right.filename | 2562 patch_filename = patch_right.filename |
2562 # Now find the corresponding patch in ps_left | 2563 # Now find the corresponding patch in ps_left |
2563 patch_left = models.Patch.gql('WHERE patchset = :1 AND filename = :2', | 2564 patch_left = models.Patch.gql('WHERE patchset = :1 AND filename = :2', |
2564 ps_left, patch_filename).get() | 2565 ps_left, patch_filename).get() |
2565 | 2566 |
2566 if patch_left: | 2567 if patch_left: |
2567 try: | 2568 try: |
2568 new_content_left = patch_left.get_patched_content() | 2569 new_content_left = patch_left.get_patched_content() |
2569 except engine.FetchError, err: | 2570 except FetchError, err: |
2570 return HttpResponseNotFound(str(err)) | 2571 return HttpResponseNotFound(str(err)) |
2571 lines_left = new_content_left.lines | 2572 lines_left = new_content_left.lines |
2572 elif patch_right: | 2573 elif patch_right: |
2573 lines_left = patch_right.get_content().lines | 2574 lines_left = patch_right.get_content().lines |
2574 else: | 2575 else: |
2575 lines_left = [] | 2576 lines_left = [] |
2576 | 2577 |
2577 if patch_right: | 2578 if patch_right: |
2578 try: | 2579 try: |
2579 new_content_right = patch_right.get_patched_content() | 2580 new_content_right = patch_right.get_patched_content() |
2580 except engine.FetchError, err: | 2581 except FetchError, err: |
2581 return HttpResponseNotFound(str(err)) | 2582 return HttpResponseNotFound(str(err)) |
2582 lines_right = new_content_right.lines | 2583 lines_right = new_content_right.lines |
2583 elif patch_left: | 2584 elif patch_left: |
2584 lines_right = patch_left.get_content().lines | 2585 lines_right = patch_left.get_content().lines |
2585 else: | 2586 else: |
2586 lines_right = [] | 2587 lines_right = [] |
2587 | 2588 |
2588 rows = engine.RenderDiff2TableRows(request, | 2589 rows = engine.RenderDiff2TableRows(request, |
2589 lines_left, patch_left, | 2590 lines_left, patch_left, |
2590 lines_right, patch_right, | 2591 lines_right, patch_right, |
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3101 if patch.no_base_file: | 3102 if patch.no_base_file: |
3102 linecache[last_key] = patching.ParsePatchToLines(patch.lines) | 3103 linecache[last_key] = patching.ParsePatchToLines(patch.lines) |
3103 else: | 3104 else: |
3104 try: | 3105 try: |
3105 if c.left: | 3106 if c.left: |
3106 old_lines = patch.get_content().text.splitlines(True) | 3107 old_lines = patch.get_content().text.splitlines(True) |
3107 linecache[last_key] = old_lines | 3108 linecache[last_key] = old_lines |
3108 else: | 3109 else: |
3109 new_lines = patch.get_patched_content().text.splitlines(True) | 3110 new_lines = patch.get_patched_content().text.splitlines(True) |
3110 linecache[last_key] = new_lines | 3111 linecache[last_key] = new_lines |
3111 except engine.FetchError: | 3112 except FetchError: |
3112 linecache[last_key] = patching.ParsePatchToLines(patch.lines) | 3113 linecache[last_key] = patching.ParsePatchToLines(patch.lines) |
3113 fetch_base_failed = True | 3114 fetch_base_failed = True |
3114 file_lines = linecache[last_key] | 3115 file_lines = linecache[last_key] |
3115 context = '' | 3116 context = '' |
3116 if patch.no_base_file or fetch_base_failed: | 3117 if patch.no_base_file or fetch_base_failed: |
3117 for old_line_no, new_line_no, line_text in file_lines: | 3118 for old_line_no, new_line_no, line_text in file_lines: |
3118 if ((c.lineno == old_line_no and c.left) or | 3119 if ((c.lineno == old_line_no and c.left) or |
3119 (c.lineno == new_line_no and not c.left)): | 3120 (c.lineno == new_line_no and not c.left)): |
3120 context = line_text.strip() | 3121 context = line_text.strip() |
3121 break | 3122 break |
(...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3875 patchsets = None | 3876 patchsets = None |
3876 for patch in patchset.patch_set.filter('delta_calculated =', False): | 3877 for patch in patchset.patch_set.filter('delta_calculated =', False): |
3877 if patchsets is None: | 3878 if patchsets is None: |
3878 # patchsets is retrieved on first iteration because patchsets | 3879 # patchsets is retrieved on first iteration because patchsets |
3879 # isn't needed outside the loop at all. | 3880 # isn't needed outside the loop at all. |
3880 patchsets = list(patchset.issue.patchset_set.order('created')) | 3881 patchsets = list(patchset.issue.patchset_set.order('created')) |
3881 patch.delta = _calculate_delta(patch, patchset_id, patchsets) | 3882 patch.delta = _calculate_delta(patch, patchset_id, patchsets) |
3882 patch.delta_calculated = True | 3883 patch.delta_calculated = True |
3883 patch.put() | 3884 patch.put() |
3884 return HttpResponse() | 3885 return HttpResponse() |
OLD | NEW |