Created
January 9, 2011 20:42
-
-
Save farnoy/771994 to your computer and use it in GitHub Desktop.
TreeView in console for Grit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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