Skip to content

Instantly share code, notes, and snippets.

@syohex
Last active December 17, 2015 02:39
Show Gist options
  • Save syohex/5537461 to your computer and use it in GitHub Desktop.
Save syohex/5537461 to your computer and use it in GitHub Desktop.
Show directories in git repository
(require 'cl)
(defun git-repository-root ()
(with-temp-buffer
(let* ((cmd "git rev-parse --show-toplevel")
(ret (call-process-shell-command cmd nil t)))
(unless (zerop ret)
(error "Here is not Git repository"))
(goto-char (point-min))
(buffer-substring-no-properties
(line-beginning-position) (line-end-position)))))
(defun git-repository-unique-directories (files)
(loop with dirs = (make-hash-table :test 'equal)
for file in files
for dir = (file-name-directory file)
unless (gethash dir dirs)
collect
(progn
(puthash dir t dirs)
dir)))
(defun git-repository-collect-files (root)
(with-temp-buffer
(let ((default-directory root))
(call-process-shell-command "git ls-files" nil t)
(goto-char (point-min))
(loop while (not (eobp))
collect
(prog1
(buffer-substring-no-properties
(line-beginning-position) (line-end-position))
(forward-line 1))))))
(defun git-repository-collect-directories ()
(interactive)
(let* ((root (file-name-as-directory (git-repository-root)))
(files (git-repository-collect-files root)))
(sort (git-repository-unique-directories files) 'string<)))
#!/bin/sh
set -e
repo_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [ $? -eq 0 ]; then
(cd $repo_root && git ls-files | perl -MFile::Basename -nle \
'$a{dirname $_}++; END{delete $a{"."}; print for sort keys %a}')
else
echo "Here is not Git repository!!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment