From b441bd890b21ace9741e30510e4202ba07a93333 Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Sat, 11 Sep 2021 23:31:25 +0300 Subject: [PATCH] Git scaffolding alias. For the 2nd commit in the repo (.gitignore, README and LICENSE.txt). --- .config/git/skel/README.rst | 2 +- Documents/bin/git-scaffolding | 87 +++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100755 Documents/bin/git-scaffolding diff --git a/.config/git/skel/README.rst b/.config/git/skel/README.rst index c8db619..7a65a4c 100644 --- a/.config/git/skel/README.rst +++ b/.config/git/skel/README.rst @@ -4,7 +4,7 @@ PROJECT_NAME License ------- -This software is licensed under the AGPL 3+ license (see the :code:`LICENSE.txt` +This software is licensed under the MIT license (see the :code:`LICENSE.txt` file). Author diff --git a/Documents/bin/git-scaffolding b/Documents/bin/git-scaffolding new file mode 100755 index 0000000..e5ddd05 --- /dev/null +++ b/Documents/bin/git-scaffolding @@ -0,0 +1,87 @@ +#!/bin/sh +set -eu + +DEFAULT_LICENSE='mit' +STAGED_FILES='' + +log_skipping () { + echo "$1 already exists, skipping" >&2 +} + +usage () { + echo "$0: [-l license-type] [-n project_name] [-r]" >&2 + exit 1 +} + +while getopts l:n:rh flag +do + case $flag in + l) LICENSE="$OPTARG";; + n) PROJECT_NAME="$OPTARG";; + r) RESTRUCTURED_TEXT='1';; + h) usage;; + \?) usage;; + esac +done +shift "$(( OPTIND - 1 ))" + +if [ -n "${LICENSE:-}" ] && ! \ + license -list | awk '{print \$1}' | grep --quiet --fixed-strings --line-regexp "$LICENSE" +then + echo 'Unknown license type.' >&2 + exit 1 +fi + +if [ -z "${PROJECT_NAME:-}" ] +then + PROJECT_NAME="$(basename "$PWD")" +fi + +if [ -f .gitignore ] +then + log_skipping .gitignore +else + git skel .gitignore + STAGED_FILES="$STAGED_FILES .gitignore" +fi + +if [ -z "${RESTRUCTURED_TEXT:-}" ] +then + if [ -f README.md ] + then + log_skipping README.md + else + git skel README.md + sed -i "s/PROJECT_NAME/$PROJECT_NAME/" README.md + git add README.md + STAGED_FILES="$STAGED_FILES README.md" + fi +else + if [ -f README.rst ] + then + log_skipping README.rst + else + git skel README.rst + UNDERLINE="$(echo "$PROJECT_NAME" | sed 's/./#/g')" + sed -i "s/PROJECT_NAME/$PROJECT_NAME/" README.rst + sed -i "2s/.*/$UNDERLINE/" README.rst + git add README.rst + STAGED_FILES="$STAGED_FILES README.rst" + fi +fi + +if [ -f LICENSE.txt ] +then + log_skipping LICENSE.txt +else + git license "${LICENSE:-$DEFAULT_LICENSE}" + STAGED_FILES="$STAGED_FILES LICENSE.txt" +fi + +if [ -n "$STAGED_FILES" ] +then + # shellcheck disable=SC2086 + git commit --message "Scaffolding." --only $STAGED_FILES +else + echo 'Skipped all files, nothing to commit.' >&2 +fi -- GitLab