Skip to content

Instantly share code, notes, and snippets.

@gabyx
Last active June 4, 2020 13:55
Show Gist options
  • Save gabyx/6ea9cf93e3aaecc9229234ea1f1960fd to your computer and use it in GitHub Desktop.
Save gabyx/6ea9cf93e3aaecc9229234ea1f1960fd to your computer and use it in GitHub Desktop.
testGitExamples.sh
#!/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 "$1.1" >>"$1.1" && echo "$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
@gabyx
Copy link
Author

gabyx commented Jun 4, 2020

Changing
echo "changes $1.1" >>"$1.1" && echo "changes $1.2" >>"$1.2" && git add . suddenly drops the git rebase error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment