Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make VCSH base directory agnostic so it can more easily manage locations other than $HOME #283

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ works instead of working through the docs.
All slides, videos, and further information can be found
[on the author's talk page][talks].


# Installation

A lot of modern UNIX-based systems offer packages for `vcsh`. In case yours
Expand Down
21 changes: 21 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,27 @@ and `git commit`, use the vcsh wrapper (like above):
vcsh repo_name commit
vcsh repo_name push

### Using vcsh in directories other than $HOME

You can use `vcsh` in an agnostic manner by changing the `$VCSH_BASE`
and `$XDG_CONFIG_HOME` variables to the desired git working directory
and `vcsh` config directory respectively. These variables can be
changed a hierarchy of config files documented in the manpage, the
agnostic file being `./.config/vcsh/config`.

If the current working directory has a `.config` folder with the necessary `vcsh`
config file, it will be automatically sourced. You can execute `vcsh
where` to print out the values of the above two variables.

Setting this up for a local folder is easy, just run `vcsh
setup`. This will create `./.config/vcsh/config` with `$VCSH_BASE` set
to `$(pwd)` and `$XDG_CONFIG_HOME` set to `$(pwd)/.config`. Now, when
`vcsh` is run in this folder, it will track files in the folder, and
use the local `.config` folder for `mr` and `repos.d`.

Please note that `vcsh` will default back to `$HOME` if run in a
sub-directory. It must be run from the root folder you wish to track.

### Using vcsh without myrepos

vcsh encourages you to use [myrepos][myrepos]. It helps you manage a large number of
Expand Down
19 changes: 19 additions & 0 deletions doc/vcsh.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ vcsh(1) - Version Control System for $HOME - multiple Git repositories in $HOME

`vcsh` run <repo> <shell command>

`vcsh` setup

`vcsh` status [<repo>]

`vcsh` upgrade <repo>

`vcsh` version

`vcsh` where

`vcsh` which <substring>

`vcsh` write-gitignore [<repo>]
Expand Down Expand Up @@ -157,6 +161,16 @@ an interactive user.
This is needed to support mr and other scripts properly and of no concern to
an interactive user.

* setup:
Creates ./config/vcsh/config with <$VCSH_BASE> and
<$XDG_CONFIG_HOME> variables set to `$(dir)` and `$(dir)/.config`
respectively. Allows for agnostic `vcsh` use in folders that are not
<$HOME>. Can also be run in <$HOME> for basic initial setup without
cloning the template.

`vcsh` will detect the configuration from any directory below the
setup directory. Use `vcsh where` to verify the above settings.

* status:
Show statuses of all/one vcsh repositories.

Expand All @@ -166,6 +180,10 @@ an interactive user.
* version:
Print version information.

* where:

Print base <$VCSH_BASE> and config <$XDG_CONFIG_HOME> variables.

* which <substring>:
Find <substring> in name of any tracked file.

Expand All @@ -192,6 +210,7 @@ ascending precedence, they are:
* `VARIABLE=foo vcsh`
* </etc/vcsh/config>
* <$XDG_CONFIG_HOME/vcsh/config>
* <./.config/vcsh/config>
* `vcsh -c <file>`

Please note that those files are sourced. Any and all commands will be
Expand Down
40 changes: 38 additions & 2 deletions vcsh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ source_all() {
esac;
}

test_dir_upwards() {
# Recurses upwards testing [ $1 $2 ], returning pwd when true
(while [ $(pwd) != / ]; do
if [ "$1" "$2" ]; then
pwd
return
fi
cd ..
done)
}

# Read configuration and set defaults if anything's not set
[ -n "$VCSH_DEBUG" ] && set -vx
Expand All @@ -66,6 +76,12 @@ source_all() {
# Read configuration files if there are any
[ -r "/etc/vcsh/config" ] && . "/etc/vcsh/config"
[ -r "$XDG_CONFIG_HOME/vcsh/config" ] && . "$XDG_CONFIG_HOME/vcsh/config"
[ -r ".config/vcsh/config" ] && . ".config/vcsh/config"

# Recursively look up the directory tree for configuration file
configdir=$(test_dir_upwards -r .config/vcsh/config)
[ -n "$configdir" ] && . "$configdir/.config/vcsh/config"

if [ -n "$VCSH_OPTION_CONFIG" ]; then
# Source $VCSH_OPTION_CONFIG if it can be read and is in $PWD of $PATH
if [ -r "$VCSH_OPTION_CONFIG" ]; then
Expand Down Expand Up @@ -127,10 +143,12 @@ help() {
<newname> Rename repository
run <repo> \\
<command> Use this repository
setup Create ./.config/vcsh/config for local vcsh use
status \\
[--terse] [<repo>] Show statuses of all/one vcsh repositories
upgrade <repo> Upgrade repository to currently recommended settings
version Print version information
where Print base and config directories
which <substring> Find substring in name of any tracked file
write-gitignore \\
[<repo>] Write .gitignore.d/<repo> via git ls-files
Expand Down Expand Up @@ -401,6 +419,14 @@ run() {
hook post-run
}

setup() {
mkdir -p .config/vcsh
echo "export VCSH_BASE=\"$(pwd)\"" >> .config/vcsh/config
echo "export XDG_CONFIG_HOME=\"$(pwd)/.config\"" >> .config/vcsh/config
echo "$SELF $VCSH_COMMAND: $SELF has been initialized in \"$(pwd)/.config\""
echo "$SELF will now track files locally if run from this directory"
}

status() {
if [ -t 1 ]; then
COLORING="-c color.status=always"
Expand Down Expand Up @@ -458,6 +484,11 @@ use() {
VCSH_DIRECTORY=$VCSH_REPO_NAME; export VCSH_DIRECTORY
}

where() {
echo "\$VCSH_BASE is $VCSH_BASE"
echo "\$XDG_CONFIG_HOME is $XDG_CONFIG_HOME"
}

which() {
output=$(for VCSH_REPO_NAME in $(list); do
get_files | grep -- "$VCSH_COMMAND_PARAMETER" | sed "s/^/$VCSH_REPO_NAME: /"
Expand Down Expand Up @@ -540,10 +571,12 @@ case $VCSH_COMMAND in
pus) VCSH_COMMAND=push;;
renam|rena|ren|re) VCSH_COMMAND=rename;;
ru) VCSH_COMMAND=run;;
setup|setu|set|se) VCSH_COMMAND=setup;;
statu|stat|sta|st) VCSH_COMMAND=status;;
upgrad|upgra|upgr|up) VCSH_COMMAND=upgrade;;
versio|versi|vers|ver|ve) VCSH_COMMAND=version;;
which|whi|wh) VCSH_COMMAND=which;;
where|whe) VCSH_COMMAND=where;;
which|whi) VCSH_COMMAND=which;;
write|writ|wri|wr) VCSH_COMMAND=write-gitignore;;
esac

Expand Down Expand Up @@ -577,6 +610,8 @@ elif [ "$VCSH_COMMAND" = 'version' ]; then
echo "$SELF $VERSION"
git version
exit
elif [ x"$VCSH_COMMAND" = x'setup' ]; then
[ -r "./.config/vcsh/config" ] && fatal "$VCSH_COMMAND: .config already exists!"
elif [ x"$VCSH_COMMAND" = x'which' ]; then
[ -z "$2" ] && fatal "$VCSH_COMMAND: please specify a filename" 1
[ -n "$3" ] && fatal "$VCSH_COMMAND: too many parameters" 1
Expand Down Expand Up @@ -608,7 +643,8 @@ elif [ x"$VCSH_COMMAND" = x'commit' ] ||
[ x"$VCSH_COMMAND" = x'list-tracked' ] ||
[ x"$VCSH_COMMAND" = x'list-untracked' ] ||
[ x"$VCSH_COMMAND" = x'pull' ] ||
[ x"$VCSH_COMMAND" = x'push' ]; then
[ x"$VCSH_COMMAND" = x'push' ] ||
[ x"$VCSH_COMMAND" = x'where' ]; then
:
elif [ x"$VCSH_COMMAND" = x'status' ]; then
if [ x"$2" = x'--terse' ]; then
Expand Down