Last active
December 17, 2015 02:39
-
-
Save syohex/5537461 to your computer and use it in GitHub Desktop.
Show directories in git repository
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
(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<))) |
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
#!/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