]> git.madduck.net Git - code/vcsh.git/blob - vcsh

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Avoid error when executed by /bin/sh
[code/vcsh.git] / vcsh
1 #!/bin/sh
2
3 #set -x
4
5 SELF=$(basename $0)
6
7 [ -z $XDG_CONFIG_HOME ] && XDG_CONFIG_HOME="$HOME/.config"
8 for check_directory in $XDG_CONFIG_HOME $XDG_CONFIG_HOME/vcsh $XDG_CONFIG_HOME/vcsh/repo.d
9 do
10         if [ ! -d $check_directory ]; then
11                 if [ -e $check_directory ]; then
12                         echo "$SELF: error: $check_directory exists but is not a directory" >&2
13                         exit 2
14                 else
15                         mkdir $check_directory || (echo "$SELF: error: could not create $check_directory" >&2; exit 2)
16                 fi
17         fi
18 done
19
20 VCSH_BASE="$XDG_CONFIG_HOME/vcsh/repo.d"
21
22 help() {
23         echo "usage: $SELF <args>
24
25    help           Display this help
26
27    list           List all repos
28
29    use <repo>     Use this repository
30    run <repo>
31        <command>  Use this repository
32
33    init           Initialize a new repository
34    clone <remote>
35          [<repo>] Clone from an existing repository
36
37    exit           Exit vcsh mode" >&2
38 }
39
40 use() {
41         REPO_NAME="$1"
42         GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
43
44         if [ ! -d "$GIT_DIR" ]; then
45                 echo E: no repository found for "$REPO_NAME" >&2
46                 return 2
47         fi
48
49         export GIT_DIR
50         export GIT_WORK_TREE="$(git config --get core.worktree)"
51         export VCSH_DIRECTORY="$REPO_NAME"
52 }
53
54 init() {
55         [[ -e $GIT_DIR ]] &&
56                 echo "$SELF: fatal: $GIT_DIR exists" &&
57                 return 21
58         export GIT_WORK_TREE="$HOME"
59         mkdir -p $GIT_WORK_TREE
60         cd $GIT_WORK_TREE ||
61                 (echo "$SELF: fatal: could not enter $GIT_WORK_TREE" &&
62                  exit 20) || exit 20
63         cd $GIT_WORK_TREE
64         git init
65         git config core.worktree $GIT_WORK_TREE
66 }
67
68 leave() {
69         unset GIT_DIR
70         unset GIT_WORK_TREE
71         unset VCSH_DIRECTORY
72 }
73
74 if [ "$1" = 'help' ] || [ $# -eq 0 ]; then
75         help
76         [ "$1" = 'help' ]
77         return $?
78
79 elif [ "$1" = 'list' ]; then
80         for i in $VCSH_BASE/*.git; do
81                 echo $(basename $i .git)
82         done
83         return 0
84
85 elif [ "$1" = 'run' ]; then
86         use $2
87         shift 2
88         "$@"
89         leave
90         return 0
91
92 elif [ "$1" = 'use' ]; then
93         if [[ -o NO_IGNORE_EOF ]]; then
94                 export VCSH_NO_IGNORE_EOF=1
95                 setopt IGNORE_EOF
96                 if [[ -n $ZSH_VERSION ]]; then
97                         vcsh_exit() {
98                                 vcsh exit;
99                                 zle reset-prompt;
100                         }
101                 fi
102                 zle -N vcsh_exit
103                 bindkey '^d' 'vcsh_exit'
104         fi
105         use $2
106         buildPS1
107         return 0
108
109 elif [ "$1" = 'clone' ]; then
110         GIT_REMOTE="$2"
111         REPO_NAME="$3"
112         [[ -z $REPO_NAME ]] && REPO_NAME=$(basename $GIT_REMOTE .git)
113         export REPO_NAME
114         export GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
115         init
116
117         git remote add origin $GIT_REMOTE
118         git config branch.master.remote origin
119         git config branch.master.merge  refs/heads/master
120         git fetch
121         for i in $(git ls-tree -r origin/master | awk '{print $4}'); do
122         [[ -e $i ]] &&
123                 echo "$SELF: error: $i exists." &&
124                 CONFLICT=1;
125         done
126         [[ -n $CONFLICT ]] &&
127                 echo "$SELF: fatal: will stop after fetching and not try to merge!\n" &&
128                 exit 3
129         git merge origin/master
130         vcsh use $REPO_NAME
131
132 elif [ "$1" = 'init' ]; then
133         export REPO_NAME="$2"
134         export GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
135         init
136         vcsh use $REPO_NAME
137
138 elif [ "$1" = 'exit' ]; then
139         if [[ $VCSH_NO_IGNORE_EOF -gt 0 ]]; then
140                 unset VCSH_NO_IGNORE_EOF
141                 setopt NO_IGNORE_EOF
142         fi
143         leave
144         buildPS1
145         return 0
146
147 else
148         help
149         return 3
150
151 fi
152