Skip to content

Instantly share code, notes, and snippets.

@loganmzz
Created June 8, 2021 15:34
Show Gist options
  • Save loganmzz/78c481e27d5f11ea3ec5b54a2a012e9a to your computer and use it in GitHub Desktop.
Save loganmzz/78c481e27d5f11ea3ec5b54a2a012e9a to your computer and use it in GitHub Desktop.
Git ignore demo
#!/usr/bin/env bash
basedir="$(cd "$(dirname "${BASH_SOURCE}")" >/dev/null; pwd)" || exit 1
current_datetime="$(date --utc --iso-8601=ns)"
function main.clean() {
rm -rf "${basedir}/"{001..004}".txt" "${basedir}/"{boo,foo,bar,far}
}
function main.mkfile() {
local path="$1"; shift
echo "${path}"
local absolutefile="${basedir}/${path}"
local absoluteparent="$(dirname "${absolutefile}")"
[[ -d "${absoluteparent}" ]] || {
mkdir -p "${absoluteparent}" || return 1
}
cat > "${absolutefile}" || return 2
}
function main.mkgitignore() {
local dirpath="$1"
main.mkfile "${dirpath}/.gitignore"
}
function main.mkfiles() {
local rc=0
while [[ $# > 0 ]]; do
local path="$1"; shift
main.mkfile "${path}" <<<"${current_datetime}" || rc=$?
done
return $rc
}
function main.mkdir() {
local dirpath="$1"; shift
main.mkfiles "${dirpath}/"{001..004}".txt"
}
function main.mkdirs() {
local rc=0
while [[ $# > 0 ]]; do
local dirpath="$1"; shift
main.mkdir "${dirpath}" || rc=$?
done
return $rc
}
function main.mktree() {
local treepath="$1"
main.mkdirs "${treepath}/"{,boo,foo,bar,far}
}
function main.mktrees() {
local rc=0
while [[ $# > 0 ]]; do
local treepath="$1"; shift
main.mktree "${treepath}" || rc=$?
done
return $rc
}
function main() {
echo
echo " ¤ Clean file tree"
main.clean
echo
echo " ¤ Generating file tree"
main.mktrees {.,boo,foo,bar,far}
echo
echo " ¤ Generating Git ignore file"
main.mkgitignore '.' <<EOF
# Ignore <001> in <tree>
# Ignore <boo> in <tree>
# Ignore <002> in <folder>
# Ignore <far> in <folder>
001.txt
boo
/002.txt
/far
EOF
main.mkgitignore 'foo' <<EOF
# Reckon <001> in <folder>
# Ignore <002> in <folder>
# Ignore <003> in <tree>
!/001.txt
/002.txt
003.txt
EOF
main.mkgitignore 'bar' <<EOF
# Reckon <001> in <tree>
!001.txt
EOF
echo
echo " ¤ Show changed files"
git ls-files --others --exclude-standard
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment