aboutsummaryrefslogtreecommitdiffstats
path: root/roles/git/files
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2018-04-18 16:41:27 +0800
committerAaron LI <aly@aaronly.me>2018-04-18 16:44:09 +0800
commit053cd1b7fe10f746792e0d1353fad4ad04e8ebe9 (patch)
tree93137af7af0b073383e0db5ae91cbe301f9bcdd1 /roles/git/files
parentf448bcbdf07cf988ab50950df95d822f359041db (diff)
downloadansible-dfly-vps-053cd1b7fe10f746792e0d1353fad4ad04e8ebe9.tar.bz2
Add git role: host git repos and share repos via cgit
* Host git repositories * Share git repositories through web interface via cgit * The static resources used by cgit is also managed/deployed by a git repo ^_^
Diffstat (limited to 'roles/git/files')
-rw-r--r--roles/git/files/cgit/syntax-highlighting.sh75
-rw-r--r--roles/git/files/git-shell-commands/addkey49
-rw-r--r--roles/git/files/git-shell-commands/create42
-rw-r--r--roles/git/files/git-shell-commands/env9
-rw-r--r--roles/git/files/git-shell-commands/help24
-rw-r--r--roles/git/files/git-shell-commands/list22
-rw-r--r--roles/git/files/git-shell-commands/make-private30
-rw-r--r--roles/git/files/git-shell-commands/make-public37
-rw-r--r--roles/git/files/git-shell-commands/no-interactive-login.disabled23
-rw-r--r--roles/git/files/git-shell-commands/set-desc31
-rw-r--r--roles/git/files/git-shell-commands/sync-github103
11 files changed, 445 insertions, 0 deletions
diff --git a/roles/git/files/cgit/syntax-highlighting.sh b/roles/git/files/cgit/syntax-highlighting.sh
new file mode 100644
index 0000000..b3ef6c6
--- /dev/null
+++ b/roles/git/files/cgit/syntax-highlighting.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+#
+# Credit:
+# * ArchWiki - cgit
+# https://wiki.archlinux.org/index.php/Cgit
+#
+# Aaron LI
+# 2017-06-19
+#
+
+#
+# This script can be used to implement syntax highlighting in the cgit
+# tree-view by refering to this file with the source-filter or repo.source-
+# filter options in cgitrc.
+#
+# This script requires a shell supporting the ${var##pattern} syntax.
+# It is supported by at least dash and bash, however busybox environments
+# might have to use an external call to sed instead.
+#
+# Note: the highlight command (http://www.andre-simon.de/) uses css for syntax
+# highlighting, so you'll probably want something like the following included
+# in your css file:
+#
+# Style definition file generated by highlight 3.13, http://www.andre-simon.de/
+#
+# body.hl { background-color:#e0eaee; }
+# pre.hl { color:#000000; background-color:#e0eaee; font-size:10pt; font-family:'Courier New',monospace;}
+# .hl.num { color:#b07e00; }
+# .hl.esc { color:#ff00ff; }
+# .hl.str { color:#bf0303; }
+# .hl.pps { color:#818100; }
+# .hl.slc { color:#838183; font-style:italic; }
+# .hl.com { color:#838183; font-style:italic; }
+# .hl.ppc { color:#008200; }
+# .hl.opt { color:#000000; }
+# .hl.ipl { color:#0057ae; }
+# .hl.lin { color:#555555; }
+# .hl.kwa { color:#000000; font-weight:bold; }
+# .hl.kwb { color:#0057ae; }
+# .hl.kwc { color:#000000; font-weight:bold; }
+# .hl.kwd { color:#010181; }
+#
+#
+# The following environment variables can be used to retrieve the configuration
+# of the repository for which this script is called:
+# CGIT_REPO_URL ( = repo.url setting )
+# CGIT_REPO_NAME ( = repo.name setting )
+# CGIT_REPO_PATH ( = repo.path setting )
+# CGIT_REPO_OWNER ( = repo.owner setting )
+# CGIT_REPO_DEFBRANCH ( = repo.defbranch setting )
+# CGIT_REPO_SECTION ( = section setting )
+# CGIT_REPO_CLONE_URL ( = repo.clone-url setting )
+#
+
+# store filename and extension in local vars
+BASENAME="$1"
+EXTENSION="${BASENAME##*.}"
+
+[ "${BASENAME}" = "${EXTENSION}" ] && EXTENSION=txt
+[ -z "${EXTENSION}" ] && EXTENSION=txt
+
+# map Makefile and Makefile.* to .mk
+[ "${BASENAME%%.*}" = "Makefile" ] && EXTENSION=mk
+
+# WARNING:
+# On DragonFly using Nginx+fcgiwrap, for unknown reason, the `PATH` is set
+# to only `/sbin:/bin:/usr/sbin:/usr/bin`, therefore, the `highlight` cannot
+# be found by default.
+# Explicitly set the `PATH`.
+export PATH="/bin:/usr/bin:/usr/games:/usr/local/bin"
+
+# For version 3
+#exec highlight --force -f -I -O xhtml -S "$EXTENSION" 2>/dev/null
+# Use `--inline-css` to be more colorful without editing cgit's CSS
+exec highlight --force -f -I --inline-css -O xhtml -S "$EXTENSION" 2>/dev/null
diff --git a/roles/git/files/git-shell-commands/addkey b/roles/git/files/git-shell-commands/addkey
new file mode 100644
index 0000000..670fd94
--- /dev/null
+++ b/roles/git/files/git-shell-commands/addkey
@@ -0,0 +1,49 @@
+#!/bin/sh
+#
+# ~/git-shell-commands/addkey
+#
+# An interactive command to add a new SSH public key to the authorized
+# key list. To ensure the integrity of the authorized_keys file, the
+# script makes sure you've entered a valid key (which must be entered
+# all on one line). For security, the script also disables some SSH
+# options for the key when it adds it.
+#
+# NOTE:
+# This interactive command is NOT allowed through a SSH connection,
+# use `sudo su - git` from other (admin) user instead.
+#
+# Credit:
+# * Hosting an admin-friendly git server with git-shell
+# http://planzero.org/blog/2012/10/24/hosting_an_admin-friendly_git_server_with_git-shell
+#
+# Aaron LI
+# 2017-06-18
+#
+
+if [ -n "${SSH_CONNECTION}" ]; then
+ echo "Sorry, this command is not allowed through a SSH connection"
+ exit 1
+fi
+
+# Read in the SSH key
+echo "Input the SSH public key to be added (ED25519/RSA):"
+read key
+
+# Generate a fingerprint
+fingerprint=$(echo "${key}" | ssh-keygen -lf -)
+
+# Check for errors
+if [ $(echo "${fingerprint}" | egrep -c '(ED25519|RSA)') -eq 0 ]; then
+ # Display the fingerprint error and clean up
+ echo "Invalid key: ${fingerprint}"
+ exit 1
+fi
+
+# Add the key to the authorized keys file and clean up
+[ ! -d "${HOME}/.ssh" ] && mkdir -m 0700 ${HOME}/.ssh
+echo ${key} >> ${HOME}/.ssh/authorized_keys
+chmod 0600 ${HOME}/.ssh/authorized_keys
+
+# Display the fingerprint for reference
+echo "Success! Added a key with the following fingerprint:"
+echo ${fingerprint}
diff --git a/roles/git/files/git-shell-commands/create b/roles/git/files/git-shell-commands/create
new file mode 100644
index 0000000..21cacc1
--- /dev/null
+++ b/roles/git/files/git-shell-commands/create
@@ -0,0 +1,42 @@
+#!/bin/sh
+#
+# ~/git-shell-commands/create
+#
+# An interactive command to create a new repository. The `.git` extension
+# is automatically added if missing, in order to let `list` command find
+# this repository.
+#
+# Credit:
+# * Hosting an admin-friendly git server with git-shell
+# http://planzero.org/blog/2012/10/24/hosting_an_admin-friendly_git_server_with_git-shell
+#
+# Aaron LI
+# 2017-06-18
+#
+
+if [ $# -eq 0 ]; then
+ echo "Usage: create <project.git> [ description ]"
+ exit 1
+fi
+
+# Set the project name, adding `.git` extension if missing
+project="${1%.[gG][iI][tT]}.git"
+shift
+
+if [ -d "${HOME}/${project}" ]; then
+ echo "ERROR: repository '${project}' already exists!"
+ exit 2
+fi
+
+# Create and initialise the project
+mkdir "${HOME}/${project}" && \
+ cd "${HOME}/${project}" && \
+ git --bare init
+
+description="$@"
+if [ -n "${description}" ]; then
+ echo "${description}" > ${HOME}/${project}/description
+fi
+
+echo "Created Git repository: ${project}"
+echo "Description: $(cat ${HOME}/${project}/description)"
diff --git a/roles/git/files/git-shell-commands/env b/roles/git/files/git-shell-commands/env
new file mode 100644
index 0000000..8b4b496
--- /dev/null
+++ b/roles/git/files/git-shell-commands/env
@@ -0,0 +1,9 @@
+#!/bin/sh
+#
+# Print current environment.
+#
+# Aaron LI
+# 2017-06-18
+#
+
+/usr/bin/env
diff --git a/roles/git/files/git-shell-commands/help b/roles/git/files/git-shell-commands/help
new file mode 100644
index 0000000..9e37fd2
--- /dev/null
+++ b/roles/git/files/git-shell-commands/help
@@ -0,0 +1,24 @@
+#!/bin/sh
+#
+# /usr/local/share/git-core/contrib/git-shell-commands/help
+#
+# Aaron LI
+# 2017-06-18
+#
+
+if tty -s; then
+ echo "Run 'help' for help, or 'exit' to leave. Available commands:"
+else
+ echo "Run 'help' for help. Available commands:"
+fi
+
+dir=$(dirname "$0")
+cd "${dir}"
+
+for cmd in *; do
+ case "${cmd}" in
+ help) ;;
+ *.disabled) ;;
+ *) [ -f "${cmd}" ] && [ -x "${cmd}" ] && echo "${cmd}" ;;
+ esac
+done
diff --git a/roles/git/files/git-shell-commands/list b/roles/git/files/git-shell-commands/list
new file mode 100644
index 0000000..2977541
--- /dev/null
+++ b/roles/git/files/git-shell-commands/list
@@ -0,0 +1,22 @@
+#!/bin/sh
+#
+# /usr/local/share/git-core/contrib/git-shell-commands/list
+#
+# Aaron LI
+# 2017-06-19
+#
+
+. ${HOME}/vars.conf
+
+for repo in $(ls -d *.git); do
+ is_bare_repo=$(git --git-dir="${repo}" rev-parse --is-bare-repository)
+ if [ "${is_bare_repo}" = "true" ]; then
+ echo "* ${repo}"
+ if [ -e "${PUBLIC}/${repo}" ]; then
+ echo " [public]"
+ fi
+ if [ -f "${repo}/description" ]; then
+ echo " $(cat ${repo}/description)"
+ fi
+ fi
+done
diff --git a/roles/git/files/git-shell-commands/make-private b/roles/git/files/git-shell-commands/make-private
new file mode 100644
index 0000000..432d847
--- /dev/null
+++ b/roles/git/files/git-shell-commands/make-private
@@ -0,0 +1,30 @@
+#!/bin/sh
+#
+# Copyright (c) 2017 Aaron LI <aly@aaronly.me>
+# MIT License
+#
+# Turn a public Git repository to be private to `cgit`.
+#
+# 2017-06-19
+#
+
+. ${HOME}/vars.conf
+
+if [ $# -ne 1 ]; then
+ echo "usage: make-private <repository.git>"
+ exit 1
+fi
+
+project="${1%.[gG][iI][tT]}.git"
+
+if [ ! -d "${project}" ]; then
+ echo "ERROR: repository '${project}' not exists!"
+ exit 2
+elif [ -L "${PUBLIC}/${project}" ]; then
+ rm "${PUBLIC}/${project}"
+ echo "Made repository '${project}' private."
+ exit 0
+else
+ echo "Repository '${project}' not public."
+ exit 0
+fi
diff --git a/roles/git/files/git-shell-commands/make-public b/roles/git/files/git-shell-commands/make-public
new file mode 100644
index 0000000..f1dfb1d
--- /dev/null
+++ b/roles/git/files/git-shell-commands/make-public
@@ -0,0 +1,37 @@
+#!/bin/sh
+#
+# Copyright (c) 2017 Aaron LI <aly@aaronly.me>
+# MIT License
+#
+# Make an existing Git repository public by linking it into
+# `repos.public`, which is exported via `cgit`.
+#
+# 2017-06-19
+#
+
+. ${HOME}/vars.conf
+
+if [ $# -ne 1 ]; then
+ echo "usage: make-public <repository.git>"
+ exit 1
+fi
+
+if [ ! -e "${PUBLIC}" ]; then
+ echo "ERROR: public directory '${PUBLIC}' not exists!"
+ exit 1
+fi
+
+project="${1%.[gG][iI][tT]}.git"
+
+if [ ! -d "${project}" ]; then
+ echo "ERROR: repository '${project}' not exists!"
+ exit 3
+elif [ -L "${PUBLIC}/${project}" ]; then
+ echo "Repository '${project}' already made public."
+ exit 0
+else
+ ln -sv ../${project} ${PUBLIC}/${project}
+ echo "Made repository '${project}' public."
+ exit 0
+fi
+
diff --git a/roles/git/files/git-shell-commands/no-interactive-login.disabled b/roles/git/files/git-shell-commands/no-interactive-login.disabled
new file mode 100644
index 0000000..3be7efc
--- /dev/null
+++ b/roles/git/files/git-shell-commands/no-interactive-login.disabled
@@ -0,0 +1,23 @@
+#!/bin/sh
+#
+# ~/git-shell-commands/no-interactive-login
+#
+# Disable interactive logins, displaying a greeting instead.
+# See git-shell(1)
+#
+# Remove/rename this script to allow interactive logins, e.g.,
+# allow `su - git` to add SSH keys.
+#
+#
+# Aaron LI
+# 2017-06-18
+#
+
+cat <<_EOF_
+Hi ${USER}!
+You've successfully authenticated, but interactive shell
+access is not provided.
+Bye~
+_EOF_
+
+exit 128
diff --git a/roles/git/files/git-shell-commands/set-desc b/roles/git/files/git-shell-commands/set-desc
new file mode 100644
index 0000000..3d0a1be
--- /dev/null
+++ b/roles/git/files/git-shell-commands/set-desc
@@ -0,0 +1,31 @@
+#!/bin/sh
+#
+# ~/git-shell-commands/set-desc
+#
+# Set the description of an existing Git repository.
+#
+# Aaron LI
+# 2017-08-22
+#
+
+if [ $# -lt 2 ]; then
+ echo "Usage: set-desc <project.git> <description>"
+ exit 1
+fi
+
+# Set the project name, adding `.git` extension if missing
+project="${1%.[gG][iI][tT]}.git"
+shift
+
+if [ ! -d "${HOME}/${project}" ]; then
+ echo "ERROR: repository '${project}' not exists!"
+ exit 2
+fi
+
+description="$@"
+if [ -n "${description}" ]; then
+ echo "${description}" > ${HOME}/${project}/description
+fi
+
+echo "Set description of Git repository: ${project}"
+echo "Description: $(cat ${HOME}/${project}/description)"
diff --git a/roles/git/files/git-shell-commands/sync-github b/roles/git/files/git-shell-commands/sync-github
new file mode 100644
index 0000000..9eef43e
--- /dev/null
+++ b/roles/git/files/git-shell-commands/sync-github
@@ -0,0 +1,103 @@
+#!/bin/sh
+#
+# Copyright (c) 2017 Aaron LI <aly@aaronly.me>
+# MIT License
+#
+# Sync public repositories to GitHub.
+#
+# 2017-06-25
+#
+
+. ${HOME}/vars.conf
+
+# Return 0 if the GitHub repository exists
+has_github_repo() {
+ local repo url
+ repo=$(basename $1)
+ repo="${repo%.git}"
+ url="${GITHUB_API}/repos/${GITHUB_USER}/${repo}"
+ echo "Check existence on GitHub: ${url}"
+ curl -s ${url} | grep -q '"owner"'
+}
+
+
+# Add the "github" remote if not exists
+add_remote_github() {
+ local gitdir_ repo url ret
+ gitdir_="$1"
+ url=$(git --git-dir="${gitdir_}" remote get-url github 2>&1)
+ ret=$?
+ if [ ${ret} -eq 0 ]; then
+ # Already has the "github" remote
+ echo "Remote 'github': ${url}"
+ else
+ repo=$(basename ${gitdir_})
+ url="${GITHUB_URL}:${GITHUB_USER}/${repo}"
+ git --git-dir="${gitdir_}" remote add github ${url}
+ echo "Added remote 'github': ${url}"
+ git --git-dir="${gitdir_}" remote update github
+ fi
+}
+
+
+# Check whether the repository needs push to remote "github";
+# Return 0 if needs push.
+#
+# Credit:
+# * Check if pull needed in Git
+# https://stackoverflow.com/a/3278427/4856091
+# * List Git commits not pushed to the origin yet
+# https://stackoverflow.com/a/3080554/4856091
+#
+need_push_github() {
+ local gitdir_ unpushed
+ gitdir_="$1"
+ unpushed=$(git --git-dir="${gitdir_}" \
+ log --oneline github/master..master | wc -l)
+ if [ ${unpushed} -eq 0 ]; then
+ echo "Already up-to-date" && false
+ else
+ echo "${unpushed} commits need push" && true
+ fi
+}
+
+
+if [ $# -eq 0 ]; then
+ cat <<_EOF_
+usage:
+ sync-github <repo.git> ...
+ sync-github @<repo_dir>
+
+e.g.,
+ sync-github @${PUBLIC}
+_EOF_
+ exit 1
+fi
+
+
+if [ "$(echo $1 | cut -c1)" = "@" ]; then
+ REPO_DIR="${1#@}"
+ REPOS=$(ls -d ${REPO_DIR}/*.git)
+else
+ REPOS="$@"
+fi
+
+echo "=== Selected Repositories ==="
+for repo in ${REPOS}; do
+ repo2="${repo%.[gG][iI][tT]}.git"
+ echo "* ${repo2}"
+done
+
+for repo in ${REPOS}; do
+ repo2="${repo%.[gG][iI][tT]}.git"
+ echo "=== Sync Repository: ${repo2} ==="
+ if ! has_github_repo ${repo2}; then
+ echo "WARNING: no such repo on GitHub: ${repo2}"
+ continue
+ fi
+ add_remote_github ${repo2}
+ if need_push_github ${repo2}; then
+ echo "Push commits to GitHub ..."
+ git --git-dir="${repo2}" push --mirror github
+ fi
+done