Permalink
Browse files

Added ability to annotate a single line (for easy blame reference =)

git-svn-id: http://svn.textmate.org/trunk/Bundles/CVS.tmbundle@2827 dfb7d73b-c2ec-0310-8fea-fb051d288c6d
  • Loading branch information...
1 parent bb7bedd commit 776694e75e2e2a0ccdf43863177a5dedbfaa1dd3 Brian Donovan committed Mar 7, 2006
Showing with 87 additions and 0 deletions.
  1. +40 −0 Commands/Annotate Line.plist
  2. +47 −0 Support/diff.rb
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>beforeRunningCommand</key>
+ <string>nop</string>
+ <key>command</key>
+ <string>require_cmd "${TM_CVS:=cvs}" "If you have installed cvs, then you need to either update your &lt;tt&gt;PATH&lt;/tt&gt; or set the &lt;tt&gt;TM_CVS&lt;/tt&gt; shell variable (e.g. in Preferences / Advanced)"
+
+"${TM_RUBY:-ruby}" &lt;&lt;'END'
+
+ENV['CVS_PATH'] = ENV['TM_CVS']
+$LOAD_PATH &lt;&lt; ENV['TM_BUNDLE_SUPPORT']
+require 'versioned_file'
+require 'diff'
+
+file = CVS::VersionedFile.new(ENV['TM_FILEPATH'])
+annotation = file.cvs(:annotate)
+diff = CVS::Diff.new(file.diff(:head))
+
+n = diff.source_line(ENV['TM_LINE_NUMBER'].to_i-1)
+case n
+when Symbol then puts "#{n} locally"
+else puts annotation.split(/\n/)[n]
+end
+
+END
+</string>
+ <key>input</key>
+ <string>none</string>
+ <key>keyEquivalent</key>
+ <string>^Z</string>
+ <key>name</key>
+ <string>Annotate Line</string>
+ <key>output</key>
+ <string>showAsTooltip</string>
+ <key>uuid</key>
+ <string>76E34DE2-1DCB-47B8-BA2F-4F3341A3AB9C</string>
+</dict>
+</plist>
View
@@ -0,0 +1,47 @@
+module CVS
+ class Diff
+ attr_accessor :raw
+
+ def initialize(raw)
+ @raw = raw
+ end
+
+ def changes(reload=false)
+ @changes = nil if reload
+ @changes ||= @raw.split(/\n/)[5..-1].select { |l| l =~ /^\d/ }.inject({}) do |changes,line|
+ changes.merge change_from_hunk_header(line)
+ end
+ end
+
+# def source_line_status(n)
+# changes.find { |(lines,status)| lines.include?(n) }.last rescue :unchanged
+# end
+
+ def source_line(n)
+ m = n
+ changes.each do |(lines,status)|
+ case status
+ when :added
+ return :added if lines.include?(n)
+ m -= lines.to_a.length if lines.end < n
+ when :deleted
+ m += lines.to_a.length if lines.begin <= n
+ end
+ end
+ m
+ end
+
+ private
+
+ def change_from_hunk_header(line)
+ case line
+ when /^(\d+),(\d+)d(\d+)$/
+ {($1.to_i)..($2.to_i) => :deleted}
+ when /^(\d+)a(\d+),(\d+)$/
+ {($2.to_i)..($3.to_i) => :added}
+ when /^(\d+)c(\d+)/
+ {($1.to_i)..($1.to_i) => :changed}
+ end
+ end
+ end
+end

0 comments on commit 776694e

Please sign in to comment.