-
Notifications
You must be signed in to change notification settings - Fork 1
/
with-unhidden-gitdir
executable file
·52 lines (43 loc) · 2.02 KB
/
with-unhidden-gitdir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env bash
# A command that unhides and re-hides a .git in the current directory.
#
# with-unhidden-gitdir runs a given command (or the user's VISUAL editor if no
# command is given) with a ./.git restored temporarily, so that Git and other
# things like Magit will see the current directory as a repository during the
# execution of the command. When the command finishes, the ./.git is removed so
# that it is hidden again. This is achieved by creating the ./.git as a symlink
# to a ./.git-hidden that is the actual GIT_DIR and which is assumed to exist.
#
# The motivating case for this is for a user to be able to temporarily work with
# their ~/.dotfiles repository and the checkout of its files in their ~/, as
# setup by our setup-home or my-deploy-setup scripts. Most of the time, a user
# does not want their home directory to be seen by Git as a repository, and so
# setup-home or my-deploy-setup moves the initial ~/.git (which just refers to
# ~/.dotfiles) to ~/.git-hidden.
#
# Note that when using this you must be careful to consider the effect on other
# things of making the directory become a Git repository for a span of time.
# E.g. if you have an Emacs already running with Magit, then it will handle the
# directory as a repository while the temporary unhidding is in effect, which
# could be unexpected and undesired, so be careful.
# shellcheck source=../share/my/bash/helpers.bash
source "${XDG_DATA_HOME:-$HOME/.local/share}"/my/bash/helpers.bash
_my-script-prelude
if [ $# -ge 1 ]; then
COMMAND=("$@")
else
# shellcheck disable=SC2206 # Want word-splitting and globbing for this.
COMMAND=($VISUAL)
fi;
[ -e .git-hidden ] || fail "Missing .git-hidden"
[ -e .git ] && fail "Pre-existing .git"
std ln -s .git-hidden .git
# Run the user's command like this, so that if it returns a failure status code
# then we continue executing to remove the .git, and to prevent it from being
# affected by the shell options we set.
( set +o errexit +o nounset
"${COMMAND[@]}"
) && :
EXIT_STATUS=$?
std rm .git
exit $EXIT_STATUS