Skip to content

Instantly share code, notes, and snippets.

@kelan
Created October 3, 2009 21:12
Show Gist options
  • Save kelan/200886 to your computer and use it in GitHub Desktop.
Save kelan/200886 to your computer and use it in GitHub Desktop.
chviewpatch
#!/usr/bin/env ruby -w
#
# This is a script to help view a patch file in the Changes.app[1] UI. It is
# pretty basic, but does the job. It simply creates 2 copies of the file,
# where in one (the "original") it strips all the lines starting with '+', and
# the other (the "modified") it strips all the lines starting with '-'. It
# also removes the first character of all lines starting with a space, +, or -
# Then it simply uses the chdiff command line too to view the differences.
#
# It takes input either from filename arguments (multiple files are ok, but it
# just squashes them all together), or from STDIN (when there are no args, or
# you pass '-' as the arg)
#
# Please let me know if you find this useful!
#
# Kelan Champagne
# http://yeahrightkeller.com
#
# [1] http://connectedflow.com/changes/
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# LICENSE (MIT)
#
# Copyright (c) 2009, Kelan Champagne
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# Get all lines from all files listed (or STDIN)
allLines = ARGF.readlines
# Generate the output file prefix
filename = (ARGF.filename == "-") ? "STDIN" : ARGF.filename
extension = File.extname(filename)
barename = File.basename(filename, extension)
outputFilePrefix = "/tmp/chvp_#{barename}_"
# Create the original and modified files
[ { :stripIf => '+', :saveAs => "#{outputFilePrefix}original" },
{ :stripIf => '-', :saveAs => "#{outputFilePrefix}modified" }
].each do |params|
output = ""
allLines.each do |line|
if (line[0,1] != params[:stripIf])
# remove the first character of the line if it's a space, +, or -
if [' ', '+', '-'].include? line[0,1]
output += line[1..-1]
else
output += line[0..-1]
end
end
end
File.open(params[:saveAs], "w") do |outfile|
outfile.print output
end
end
# Do the compare in Changes
system("chdiff #{outputFilePrefix}{original,modified}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment