annotate.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # annotate.py -- Annotate files with last changed revision
  2. # Copyright (C) 2015 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Annotate file contents indicating when they were last changed.
  19. Annotated lines are represented as tuples with last modified revision SHA1
  20. and contents.
  21. Please note that this is a very naive annotate implementation; it's
  22. """
  23. import difflib
  24. from dulwich.object_store import tree_lookup_path
  25. from dulwich.walk import (
  26. ORDER_DATE,
  27. Walker,
  28. )
  29. # Walk over ancestry graph breadth-first
  30. # When checking each revision, find lines that according to difflib.Differ()
  31. # are common between versions.
  32. # Any lines that are not in common were introduced by the newer revision.
  33. # If there were no lines kept from the older version, stop going deeper in the graph.
  34. def update_lines(differ, annotated_lines, new_history_data, new_blob):
  35. """Update annotation lines with old blob lines.
  36. """
  37. old_index = 0
  38. for diffline in differ.compare(
  39. [l for (h, l) in annotated_lines],
  40. new_blob.splitlines()):
  41. if diffline[0:1] == '?':
  42. continue
  43. elif diffline[0:1] == ' ':
  44. yield annotated_lines[old_index]
  45. old_index += 1
  46. elif diffline[0:1] == '+':
  47. yield (new_history_data, diffline[2:])
  48. elif diffline[0:1] == '-':
  49. old_index += 1
  50. else:
  51. raise RuntimeError('Unknown character %s returned in diff' % diffline[0])
  52. def annotate_lines(store, commit_id, path, order=ORDER_DATE, lines=None, follow=True):
  53. """Annotate the lines of a blob.
  54. :param store: Object store to retrieve objects from
  55. :param commit_id: Commit id in which to annotate path
  56. :param path: Path to annotate
  57. :param order: Order in which to process history (defaults to ORDER_DATE)
  58. :param lines: Initial lines to compare to (defaults to specified)
  59. :param follow: Wether to follow changes across renames/copies
  60. :return: List of (commit, line) entries where
  61. commit is the oldest commit that changed a line
  62. """
  63. walker = Walker(store, include=[commit_id], paths=[path], order=order,
  64. follow=follow)
  65. d = difflib.Differ()
  66. revs = []
  67. for log_entry in walker:
  68. for tree_change in log_entry.changes():
  69. if type(tree_change) is not list:
  70. tree_change = [tree_change]
  71. for change in tree_change:
  72. if change.new.path == path:
  73. path = change.old.path
  74. revs.append((log_entry.commit, change.new))
  75. break
  76. lines = []
  77. for (commit, entry) in reversed(revs):
  78. lines = list(update_lines(d, lines, (commit, entry), store[entry.sha]))
  79. return lines