Skip to content

Instantly share code, notes, and snippets.

@localhostdotdev
Created February 25, 2019 13:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save localhostdotdev/78f6a372a8de4cfed6fe4268bae7f8a3 to your computer and use it in GitHub Desktop.
Save localhostdotdev/78f6a372a8de4cfed6fe4268bae7f8a3 to your computer and use it in GitHub Desktop.
Like `tree` but takes into account the `.gitignore` file

Made a working one with only ruby and git as a dependency:

One liner to type in irb:

def format(file); split = file.split('/'); return file unless split.size > 1; "│  " * (split.size - 2) + "└── " + split.last; end; def list(dir, start: true); puts dir if start; `git ls-tree master "#{dir}" --name-only`.split("\n").sort.each { |line| split = line.split('/'); puts format(line); list(line + '/', start: false) if File.directory?(line)  }; end; list('.')

And as a more proper script:

#!/usr/bin/env ruby

def format(file)
  split = file.split('/')
  return file unless split.size > 1
  "│  " * (split.size - 2) + "└── " + split.last
end

def list(dir, start: true)
  puts dir if start

  `git ls-tree master "#{dir}" --name-only`.split("\n").sort.each do |line|
    split = line.split('/')
    puts format(line)
    list(line + '/', start: false) if File.directory?(line)
  end
end

list(ARGV.first || '.')

Name it gittree then chmod +x gittree and then ./gittree or ./gittree app/ for instance.

  • This only works if the current directory is the same as the script's current directory
  • This could potentially execute arbitrary commands passed as parameter
  • It's far from fool proof :D Might make it better if people ask, for now it scratch my inch and I don't have to trust somebody's code with < 100 downloads a month
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment