Skip to content

Commit 788fe26

Browse files
committed
Update (un)installation scripts. Improve handling of root and rootless environments. Run remove commands interactively.
1 parent 1bf8e31 commit 788fe26

File tree

3 files changed

+101
-47
lines changed

3 files changed

+101
-47
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@ This script clones a subdirectory of a github/gitlab repository.
1414
- [Optional variables](#optional-variables)
1515

1616
# Install
17-
Clone this repository
18-
```zsh
19-
sudo apt install -y git
20-
git clone https://github.com/lu0/git-partial-clone.git
21-
```
2217
Install the script and autocompletion rules.
2318
```zsh
24-
cd git-partial-clone/ && ./install.sh
19+
./install.sh
2520
```
2621
Then you can call the command `git-partial-clone` from any directory and use `TAB` to autocomplete the CLI options.
2722

install.sh

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
#
44
# Installer for the git-partial-clone script
@@ -7,22 +7,63 @@
77
# https://github.com/lu0/git-partial-clone
88
#
99

10-
echo "Installing git-partial-clone ..."
11-
LOCAL_PATH=~/.local/bin
12-
BASH_COMPLETION_DIR=~/.local/etc/bash_completion.d
13-
BASH_COMPLETION_SCRIPT=~/.bash_completion
14-
15-
echo "Adding script to the local PATH ..."
16-
ln -srf git-partial-clone.sh ${LOCAL_PATH}/git-partial-clone
17-
18-
echo "Adding the completion rules ..."
19-
mkdir -p ${BASH_COMPLETION_DIR}
20-
ln -srf completion-rules.sh ${BASH_COMPLETION_DIR}/git-partial-clone
21-
echo "for file in ${BASH_COMPLETION_DIR}/* ; do" >> ${BASH_COMPLETION_SCRIPT}
22-
echo -e "\t. \$file" >> ${BASH_COMPLETION_SCRIPT}
23-
echo "done" >> ${BASH_COMPLETION_SCRIPT}
24-
echo "Done!"
25-
26-
# Force sourcing of /etc/bash_completion
27-
exec bash
28-
10+
main() {
11+
if [ ${SUDO_USER} ]; then
12+
[ "$EUID" -eq 0 ] && CURR_USER=${SUDO_USER} || CURR_USER=$(whoami)
13+
else
14+
CURR_USER=$(whoami)
15+
fi
16+
CURR_HOME=$(getent passwd ${CURR_USER} | cut -d ':' -f 6)
17+
18+
# Define configuration variables
19+
BIN_PATH=${CURR_HOME}/.local/bin
20+
CONFIG_DIR=${CURR_HOME}/.config/git-partial-clone
21+
COMPLETION_RULES_FILE=${CONFIG_DIR}/completion_rules
22+
BLOCK_DELIMITER="### added by git-partial-clone"
23+
24+
# Create install directories
25+
mkdir -p ${CONFIG_DIR}
26+
27+
# Install the script
28+
echo "Adding script to the local PATH ..."
29+
mkdir -p ${BIN_PATH}
30+
echo -e "\n${BLOCK_DELIMITER}" >> ${CURR_HOME}/.bashrc
31+
echo -e "PATH=\"${BIN_PATH}:\$PATH\"" >> ${CURR_HOME}/.bashrc
32+
echo "${BLOCK_DELIMITER}" >> ${CURR_HOME}/.bashrc
33+
ln -srf git-partial-clone.sh ${BIN_PATH}/git-partial-clone
34+
35+
# Install the autocompletion rules
36+
echo "Installing the completion rules ..."
37+
BASH_COMPLETION_SCRIPT=${CURR_HOME}/.bash_completion
38+
_install-package bash-completion
39+
ln -srf completion-rules.sh ${COMPLETION_RULES_FILE}
40+
echo -e "\n${BLOCK_DELIMITER}" >> ${BASH_COMPLETION_SCRIPT}
41+
echo -e ". ${COMPLETION_RULES_FILE}" >> ${BASH_COMPLETION_SCRIPT}
42+
echo "${BLOCK_DELIMITER}" >> ${BASH_COMPLETION_SCRIPT}
43+
44+
# Force reload of the completition rules
45+
echo -e "\n${BLOCK_DELIMITER}" >> ${CURR_HOME}/.bashrc
46+
echo -e ". /etc/bash_completion" >> ${CURR_HOME}/.bashrc
47+
echo "${BLOCK_DELIMITER}" >> ${CURR_HOME}/.bashrc
48+
49+
echo "Done!"
50+
exec bash
51+
}
52+
53+
_install-package() {
54+
# Installs a package if not found
55+
# Usage: _install-package <PACKAGE>
56+
local IS_PACKAGE_INSTALLED=$(
57+
dpkg-query -Wf='${Status}' ${1} 2>/dev/null \
58+
| grep --count "install ok installed"
59+
)
60+
local ERR_MSG="Error installing the autocompletion rules."
61+
if [[ ${IS_PACKAGE_INSTALLED} -eq 0 ]]; then
62+
echo "Installing required package ${1} ..."
63+
[ "$EUID" -eq 0 ] \
64+
&& { apt install ${1} || echo ${ERR_MSG} ;} \
65+
|| { sudo apt install ${1} || echo ${ERR_MSG} ;}
66+
fi
67+
}
68+
69+
main

uninstall.sh

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
#
44
# Uninstaller for the git-partial-clone script
@@ -7,23 +7,41 @@
77
# https://github.com/lu0/git-partial-clone
88
#
99

10-
echo "Uninstalling git-partial-clone ..."
11-
LOCAL_PATH=~/.local/bin
12-
BASH_COMPLETION_DIR=~/.local/etc/bash_completion.d
13-
BASH_COMPLETION_SCRIPT=~/.bash_completion
14-
15-
echo "Removing script from the local PATH ..."
16-
rm -rf ${LOCAL_PATH}/git-partial-clone
17-
18-
echo "Removing the completion rules ..."
19-
LINES_IN_FILE=$([[ -f ~/.bash_completion ]] \
20-
&& sort ~/.bash_completion | uniq | wc -l)
21-
[[ ${LINES_IN_FILE} == 3 ]] \
22-
&& rm -rf ${BASH_COMPLETION_DIR} \
23-
&& rm -rf ~/.bash_completion \
24-
|| rm -rf ${BASH_COMPLETION_SCRIPT}
25-
echo "Done!"
26-
27-
# Force sourcing of /etc/bash_completion
28-
exec bash
29-
10+
main() {
11+
# Uninstall local installation
12+
if [ ${SUDO_USER} ]; then
13+
[ "$EUID" -eq 0 ] && CURR_USER=${SUDO_USER} || CURR_USER=$(whoami)
14+
else
15+
CURR_USER=$(whoami)
16+
fi
17+
CURR_HOME=$(getent passwd ${CURR_USER} | cut -d ':' -f 6)
18+
19+
# Define configuration variables
20+
BIN_PATH=${CURR_HOME}/.local/bin
21+
CONFIG_DIR=${CURR_HOME}/.config/git-partial-clone
22+
COMPLETION_RULES_FILE=${CONFIG_DIR}/completion_rules
23+
BLOCK_DELIMITER="### added by git-partial-clone"
24+
25+
# The files are removed interactively, as this is a beta release
26+
# and I can't guarantee the script is not deleting important files...
27+
28+
# Uninstall the script
29+
echo "Removing script from the local PATH ..."
30+
[ -f ${CURR_HOME}/.bashrc ] && \
31+
sed -i "/${BLOCK_DELIMITER}/,/${BLOCK_DELIMITER}/d" ${CURR_HOME}/.bashrc
32+
rm -i "${BIN_PATH}/git-partial-clone"
33+
34+
# Uninstall the autocompletion rules
35+
echo "Removing the autocompletion rules ..."
36+
[ -f ${CURR_HOME}/.bash_completion ] && \
37+
sed -i "/${BLOCK_DELIMITER}/,/${BLOCK_DELIMITER}/d" ${CURR_HOME}/.bash_completion
38+
rm -ri "${COMPLETION_RULES_FILE}"
39+
40+
# Remove the installation directory
41+
rm -ri "${CONFIG_DIR}"
42+
43+
exec bash
44+
echo "Done!"
45+
}
46+
47+
main

0 commit comments

Comments
 (0)