Skip to content

Commit

Permalink
provide only either local or global hints for tab-completion
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiksalvet committed Dec 2, 2024
1 parent dfe5b3b commit bbc81c0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

The changes not yet present in any release are listed in this section.

### Fixed

* The Bash completion now uses only files that are in its current access scope (root vs user).

## 1.2.0 (2024-11-27)

### Changed
Expand Down
75 changes: 50 additions & 25 deletions src/bash/gitpack-completion
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

#-------------------------------------------------------------------------------
# Copyright 2019-2021 Dominik Salvet
# Copyright 2019-2024 Dominik Salvet
# https://github.com/dominiksalvet/gitpack
#-------------------------------------------------------------------------------
# This file represents a GitPack extension, which incorporates a prompting tab
Expand Down Expand Up @@ -116,11 +116,9 @@ _gitpack_get_urls() (
)

_gitpack_get_db_urls() {
cut -f 1 -d ' ' "$LOCAL_STATE_DIR/status" \
"$GLOBAL_STATE_DIR/status" 2>/dev/null
cut -f 1 -d ' ' "$STATE_DIR/status" 2>/dev/null
test "$command" != uninstall && # only installed packages for uninstall
cut -f 1 -d ' ' "$LOCAL_CACHE_DIR/fetched" \
"$GLOBAL_CACHE_DIR/fetched" 2>/dev/null
cut -f 1 -d ' ' "$CACHE_DIR/fetched" 2>/dev/null
true # always successful
}

Expand All @@ -133,9 +131,7 @@ _gitpack_get_versions() (
fi

_gitpack_init_paths &&
{ cd "$LOCAL_CACHE_DIR/repos/$1/" 2>/dev/null &&
test -d .git/ && git show-ref
cd "$GLOBAL_CACHE_DIR/repos/$1/" 2>/dev/null &&
{ cd "$CACHE_DIR/repos/$1/" 2>/dev/null &&
test -d .git/ && git show-ref
true # just for sure
} | sed 's/^.* //; s|^.*/||; s/\^{}$//' # beautify show-ref output
Expand All @@ -146,29 +142,58 @@ _gitpack_get_versions() (
#-------------------------------------------------------------------------------

_gitpack_init_paths() {
local system &&
local user_id access system &&

user_id="$(id -u)" &&
if [ "$user_id" -eq 0 ]; then
access=global
else
access=local
fi &&

system="$(uname)" &&

case "$system" in
Linux) readonly LOCAL_STATE_DIR="$HOME/.local/share/gitpack"
readonly GLOBAL_STATE_DIR=/var/lib/gitpack
readonly LOCAL_CACHE_DIR="$HOME/.cache/gitpack"
readonly GLOBAL_CACHE_DIR=/var/cache/gitpack
;;
Darwin) readonly LOCAL_STATE_DIR="$HOME/.local/share/gitpack"
readonly GLOBAL_STATE_DIR=/var/lib/gitpack
readonly LOCAL_CACHE_DIR="$HOME/Library/Caches/gitpack"
readonly GLOBAL_CACHE_DIR=/Library/Caches/gitpack
;;
# fallback paths are now the same as Linux ones (separated to keep the idea)
*) readonly LOCAL_STATE_DIR="$HOME/.local/share/gitpack"
readonly GLOBAL_STATE_DIR=/var/lib/gitpack
readonly LOCAL_CACHE_DIR="$HOME/.cache/gitpack"
readonly GLOBAL_CACHE_DIR=/var/cache/gitpack
;;
Linux) init_linux_paths "$access" ;;
Darwin) init_macos_paths "$access" ;;
*) init_fallback_paths "$access" ;;
esac
}

# $1 - local/global access
init_linux_paths() {
if [ "$1" = local ]; then # respect XDG
readonly STATE_DIR="$HOME/.local/share/gitpack"
readonly CACHE_DIR="$HOME/.cache/gitpack"
else # respect FHS
readonly STATE_DIR=/var/lib/gitpack
readonly CACHE_DIR=/var/cache/gitpack
fi
}

# $1 - local/global access
init_macos_paths() {
if [ "$1" = local ]; then
readonly STATE_DIR="$HOME/.local/share/gitpack"
readonly CACHE_DIR="$HOME/Library/Caches/gitpack"
else
readonly STATE_DIR=/var/lib/gitpack
readonly CACHE_DIR=/Library/Caches/gitpack
fi
}

# $1 - local/global access
init_fallback_paths() {
# fallback paths are now the same as Linux ones (separated to keep the idea)
if [ "$1" = local ]; then
readonly STATE_DIR="$HOME/.local/share/gitpack"
readonly CACHE_DIR="$HOME/.cache/gitpack"
else
readonly STATE_DIR=/var/lib/gitpack
readonly CACHE_DIR=/var/cache/gitpack
fi
}

#-------------------------------------------------------------------------------
# REGISTER COMPLETION
#-------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/gitpack
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ OPTION: -h/-H show short/full version hashes
readonly ABOUT_MESSAGE="GitPack $VERSION
Simple Git-based package manager
Copyright 2019-2022 Dominik Salvet
Copyright 2019-2024 Dominik Salvet
https://github.com/dominiksalvet/gitpack"
}

Expand Down

0 comments on commit bbc81c0

Please sign in to comment.