Skip to content

Instantly share code, notes, and snippets.

@gabyx
Created June 10, 2020 17:57
Show Gist options
  • Save gabyx/7ee55125aa58d96a703e0a77d8a8c42c to your computer and use it in GitHub Desktop.
Save gabyx/7ee55125aa58d96a703e0a77d8a8c42c to your computer and use it in GitHub Desktop.
#!/bin/bash
# shellcheck disable=SC2015
repoDir="$1"
example="$2"
function die() {
echo -e "$@" >&2
exit 1
}
if [ -d "$repoDir" ]; then
die "! You need to enter an non-existing path to\n" \
" the repo you want to create.\n" \
" Path '$repoDir' exists"
fi
function setupRepo() {
mkdir -p "${repoDir}" || true
echo "Creating repository at '$repoDir'"
git init "$repoDir" && cd "$repoDir" ||
die "! Git init failed"
git commit --allow-empty -m "Init" ||
die "! Could not create init"
}
function addFileCommit() {
echo "changes $1.1" >>"$1.1" && echo "changes $1.2" >>"$1.2" && git add .
msg="Added file $1"
[ -n "$2" ] && msg="$msg $2"
git commit -a -m "$msg"
}
function finalize() {
echo "Repository succesfully setup at '$repoDir'"
echo "Try your stuff now! :-)"
}
function showCommits() {
echo "Current history is:"
git --no-pager log \
--color \
--graph \
--pretty=format:'%Cred%h%Creset - %s %C(yellow)%d%Creset' \
--abbrev-commit \
--branches
echo
}
function setupWrongCommittedFiles() {
setupRepo || die "! Setup failed"
git checkout -b feature
# Write the check file
cat <<EOF >check.sh
#!/bin/bash
set -e
trap 'echo "Your solution is not correct!"' ERR
git checkout feature &>/dev/null
files=\$(git --no-pager log feature --oneline |
cut -d ' ' -f 1 |
xargs -n 1 git diff-tree --no-commit-id --name-only -r)
echo "\$files" | grep -q 'W.2' && false
echo "Your solution is correct: no 'W.2' file found!"
EOF
chmod u+x check.sh
git add . && git commit -a -m "Test your solution"
# Wrong commited files -> rebasing
addFileCommit "A"
addFileCommit "B"
addFileCommit "W" "(Wrong committed file)"
addFileCommit "C"
addFileCommit "D"
addFileCommit "W" "(Wrong changes)"
showCommits
finalize || die "! Finalize failed"
}
if [ "$example" = "ex-wrong-committed-files" ]; then
setupWrongCommittedFiles
else
die "! No such example '$example'\n" \
" Examples are:\n" \
" - ex-wrong-committed-files"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment