Last active
July 31, 2022 09:56
-
-
Save ryot4/245a4b2c28b064c68c1d957b8d580d18 to your computer and use it in GitHub Desktop.
"git ls" command (GitHub-like file listing in CLI)
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 | |
ls_file() { | |
file_path="${1%%/}" | |
file_basename=$(basename "${file_path}") | |
ls_tree_opt= | |
if [ -d "${file_path}" ]; then | |
file_basename="${file_basename}/" | |
ls_tree_opt='-d' | |
fi | |
file_mode=$(git ls-tree ${ls_tree_opt} HEAD -- "${file_path}" | cut -d ' ' -f 1) | |
if [ -n "${file_mode}" ]; then | |
git log -1 --pretty=format:"${file_mode} ${file_basename} %x09 %h %s %x09 %cr%n" HEAD -- "${file_path}" | |
else | |
printf '%s %s \t %s \t uncommitted\n' '------' "${file_basename}" '-------' | |
fi | |
} | |
path="${1:-.}" | |
if [ -f "${path}" ]; then | |
pattern="${path}" | |
elif [ -d "${path}" ]; then | |
pattern="${path}/.* ${path}/*" | |
else | |
echo "no such file or directory: ${path}" | |
exit 1 | |
fi | |
for file in ${pattern}; do | |
case "${file}" in | |
"${path}"/.|"${path}"/..) | |
continue | |
;; | |
"${path}"/.git) | |
continue | |
;; | |
"${path}/.*"|"${file}/*") | |
continue | |
;; | |
esac | |
ls_file "${file##./}" | |
done | column -t -s ' ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output example (https://github.com/githubtraining/hellogitworld):