Skip to content

Instantly share code, notes, and snippets.

@farnoy
Created January 9, 2011 20:42
Show Gist options
  • Save farnoy/771994 to your computer and use it in GitHub Desktop.
Save farnoy/771994 to your computer and use it in GitHub Desktop.
TreeView in console for Grit
#!/usr/bin/env ruby
# Takes two parameters, location of repo and branch name
# First defaults to directory where you're running from
# Second defaults to 'master'
# To override, use
# -p path/to/repo
# -b branch_name
if ARGV.include? '--path' or ARGV.include? '-p'
$repo_path = File.absolute_path(ARGV[(ARGV.index('--path') || ARGV.index('-p')).to_i + 1])
else
$repo_path = Dir.pwd
end
if ARGV.include? '-b'
$branch_name = ARGV[ARGV.index('-b').to_i + 1]
else
$branch_name = 'master'
end
require 'grit'
$repo = Grit::Repo.new($repo_path)
class Tree
attr_writer :tree, :nesting
def initialize(tree, nesting = [])
@tree = tree
@nesting = nesting
end
def pretty_print
@tree.contents.each do |c|
if c.class == Grit::Blob
puts "#{$repo_path}#{"/" unless @nesting.empty?}#{@nesting.join('/')}/#{c.name}"
else
Tree.new(c, (@nesting + [c.name])).pretty_print
end
end
end
end
Tree.new($repo.tree($branch_name)).pretty_print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment