Skip to content

Instantly share code, notes, and snippets.

@martin-braun
Last active August 17, 2021 23:00
Show Gist options
  • Save martin-braun/e5c61525d0a94c72c19f4ead156d9aae to your computer and use it in GitHub Desktop.
Save martin-braun/e5c61525d0a94c72c19f4ead156d9aae to your computer and use it in GitHub Desktop.
gpm - Lightweight Git Package Manager
#!/bin/bash
me=`basename "$0"`
if [[ -z $1 || $1 == "-h" || $1 == "--help" ]]; then
echo "Usage: $me <op[ install | i | update | u | force_update | fu | uninstall | un | list | l ]?> <source_platform[ github | gitlab ]> <source_name[ <author,groups>/<repo_name> ]> <protocol[ https | ssh ]?>"
else
op="$1"
force=0
if [[ $op == "force_update" || $op == "fu" ]]; then
op="u"
force=1
fi
source_platform="$2"
source_name="$3"
protocol="$4"
if [[ ! $protocol ]]; then
protocol="https"
fi
repo_path="/opt/$source_platform/$source_name"
if [[ $op == "install" || $op == "i" ]]; then
if [[ $protocol == "https" || $protocol == "ssh" ]]; then
if [ ! -d "$repo_path" ]; then
echo "$ sudo mkdir -p \"$repo_path\""
sudo mkdir -p "$repo_path"
if [[ $protocol == "https" ]]; then
echo "$ clone https://$source_platform.com/$source_name.git \"$repo_path\""
if ! sudo git clone https://$source_platform.com/$source_name.git "$repo_path"; then
echo "$ rm -rf \"$repo_path\""
sudo rm -rf "$repo_path"
else
echo "git -C \"$repo_path\" config --unset core.filemode"
sudo git -C "$repo_path" config --unset core.filemode
fi
else
echo "$ git clone git@$source_platform.com:$source_name.git \"$repo_path\""
if ! sudo git clone git@$source_platform.com:$source_name.git "$repo_path"; then
echo "$ rm -rf \"$repo_path\""
sudo rm -rf "$repo_path"
else
echo "git -C \"$repo_path\" config --unset core.filemode"
sudo git -C "$repo_path" config --unset core.filemode
fi
fi
open "$repo_path" 2> /dev/null # open finder in macOS
else
echo "$ git -C \"$repo_path\" pull"
sudo git -C "$repo_path" pull
echo "git -C \"$repo_path\" config --unset core.filemode"
sudo git -C "$repo_path" config --unset core.filemode
fi
fi
fi
if [[ $source_platform && $source_name && ( $op == "update" || $op == "u" || $op == "uninstall" || $op == "un" ) ]]; then
if [ ! -d "$repo_path" ]; then
echo "$repo_path not installed."
exit 1
fi
if [[ $op == "update" || $op == "u" ]]; then
if [[ $force == 1 ]]; then
echo "$ git -C \"$repo_path\" reset --hard HEAD"
sudo git -C "$repo_path" reset --hard HEAD
fi
echo "$ git -C \"$repo_path\" pull --ff-only"
sudo git -C "$repo_path" pull --ff-only
echo "git -C \"$repo_path\" config --unset core.filemode"
sudo git -C "$repo_path" config --unset core.filemode
else
echo "$ rm -rf \"$repo_path\""
sudo rm -rf "$repo_path"
fi
else
if [[ ( ! $source_platform || $source_platform == "github" ) && ( $op == "update" || $op == "u" ) ]]; then # update all from source platform
for dir in `find "/opt/github" -maxdepth 2 -mindepth 2`; do
if [[ $force == 1 ]]; then
echo "$ git -C \"$dir\" reset --hard HEAD"
sudo git -C "$dir" reset --hard HEAD
fi
echo "$ git -C \"$dir\" pull --ff-only"
sudo git -C "$dir" pull --ff-only
echo "git -C \"$dir\" config --unset core.filemode"
sudo git -C "$dir" config --unset core.filemode
done
fi
if [[ ( ! $source_platform || $source_platform == "gitlab" ) && ( $op == "update" || $op == "u" ) ]]; then # update all from source platform
for dir in `find "/opt/gitlab" -maxdepth 3 -mindepth 2`; do
if [ -d "$dir/.git" ]; then
if [[ $force == 1 ]]; then
echo "$ git -C \"$dir\" reset --hard HEAD"
sudo git -C "$dir" reset --hard HEAD
fi
echo "$ git -C \"$dir\" pull --ff-only"
sudo git -C "$dir" pull --ff-only
echo "git -C \"$dir\" config --unset core.filemode"
sudo git -C "$dir" config --unset core.filemode
fi
done
fi
fi
if [[ $op == "list" || $op == "l" ]]; then
if [[ ( ! $source_platform || $source_platform == "github" ) ]]; then # list all from source platform
echo ""
for dir in `find "/opt/github" -maxdepth 2 -mindepth 2`; do
echo "github ${dir//\/opt\/github\//}"
done
fi
if [[ ( ! $source_platform || $source_platform == "gitlab" ) ]]; then # list all from source platform
echo ""
for dir in `find "/opt/gitlab" -maxdepth 3 -mindepth 2`; do
if [ -d "$dir/.git" ]; then
echo "gitlab ${dir//\/opt\/gitlab\//}"
fi
done
fi
echo ""
fi
fi
@martin-braun
Copy link
Author

martin-braun commented Jun 17, 2021

This script allows to pull (install / update) or delete (uninstall) repositories from GitHub or GitLab. It's designed to work with repositories (that you want to use in read-only) in a way like working with any other package installer, but all it does is transfer the commands to git commands. It's not designed to work with branches. If you want to contribute or use different branches, you shouldn't use my lightweight gpm. This gpm is your quick method to checkout a repository on your system, locally.

Therefore, this is no replacement to the git command and also not a competition to aerys' gpm. This is just a simple quick solution when working with repositories on your local machine in read-only mode. I was just tired of manually pulling updates of all my read-only repositories. Not everybody builds proper releases and some repos contain non binaries that I wish to keep up to date. For this I made this quick script.

In this script repositories will be pulled to /opt/github or /opt/gitlab depending on the remote host.

Command samples:

  • gpm install github directus/directus: pulls Directus to /opt/github/directus/directus
  • gpm update github directus/directus: pulls from the upstream of /opt/github/directus/directus
  • gpm update github: pulls from all cloned github repositories in /opt/github
  • gpm update: pulls from all cloned repositories in /opt/*
  • gpm list github: lists all folders in /opt/github which represents your cloned GitHub repositories
  • gpm list: lists all folders in /opt/* which represents your cloned repositories (from all source platforms)
  • gpm uninstall github directus/directus: Deletes /opt/github/directus/directus with all sub directories

(Use force_update instead of update to hard reset the affected packages.)

There is also the ability to use ssh instead of https (useful for private repositories). For this, please provide an additional parameter ssh on gpm install or gpm update. Additionally, there are short arguments available (i instead of install, u instead of update, fu instead of force_update, un instead of uninstall and l instead of list).

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