]> 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:

4e99718046fbc56bf2f6d287700bcd36a8a25d12
[code/vcsh.git] / vcsh
1 #!/bin/sh
2
3 [ "$VCSH_DEBUG" = '1' ] && 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 debug() {
23         [ "$VCSH_DEBUG" = '1' ] && echo "$SELF: debug: $1"
24 }
25
26 verbose() {
27         if [ "$VCSH_DEBUG" = '1' ] || [ "$VCSH_VERBOSE" = '1' ]; then echo "$SELF: verbose: $1"; fi
28 }
29
30 help() {
31         echo "usage: $SELF <args>
32
33    help           Display this help
34
35    list           List all repos
36
37    use <repo>     Use this repository
38    run <repo>
39        <command>  Use this repository
40
41    init           Initialize a new repository
42    clone <remote>
43          [<repo>] Clone from an existing repository
44
45    exit           Exit vcsh mode" >&2
46 }
47
48 use() {
49         verbose "use() begin"
50         REPO_NAME="$1"
51         GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
52
53         if [ ! -d "$GIT_DIR" ]; then
54                 echo E: no repository found for "$REPO_NAME" >&2
55                 return 2
56         fi
57
58         export GIT_DIR
59         export GIT_WORK_TREE="$(git config --get core.worktree)"
60         export VCSH_DIRECTORY="$REPO_NAME"
61         verbose "use() end"
62 }
63
64 init() {
65         verbose "init() begin"
66         [ -e "$GIT_DIR" ] &&
67                 echo "$SELF: fatal: $GIT_DIR exists" &&
68                 return 21
69         export GIT_WORK_TREE="$HOME"
70         mkdir -p $GIT_WORK_TREE
71         cd $GIT_WORK_TREE ||
72                 (echo "$SELF: fatal: could not enter $GIT_WORK_TREE" &&
73                  exit 20) || exit 20
74         cd $GIT_WORK_TREE
75         git init
76         git config core.worktree $GIT_WORK_TREE
77         verbose "init() end"
78 }
79
80 leave() {
81         unset GIT_DIR
82         unset GIT_WORK_TREE
83         unset VCSH_DIRECTORY
84 }
85
86 if [ "$1" = 'help' ] || [ "$#" -eq 0 ]; then
87         help
88         [ "$1" = 'help' ]
89         exit $?
90
91 elif [ "$1" = 'list' ]; then
92         verbose "list begin"
93         for i in $VCSH_BASE/*.git; do
94                 echo $(basename $i .git)
95         done
96         verbose "list end"
97         exit 0
98
99 elif [ "$1" = 'run' ]; then
100         verbose "run begin"
101         use $2
102         shift 2
103         "$@"
104         leave
105         verbose "run end"
106         exit 0
107
108 elif [ "$1" = 'use' ]; then
109         verbose "use begin"
110         if [ -n "$ZSH_VERSION" ]; then
111                 if [ -o NO_IGNORE_EOF ]; then
112                         export VCSH_NO_IGNORE_EOF=1
113                         setopt IGNORE_EOF
114                 fi
115                 vcsh_exit() {
116                         vcsh exit;
117                         zle reset-prompt;
118                 }
119                 zle -N vcsh_exit
120                 bindkey '^d' 'vcsh_exit'
121         fi
122         use $2
123         [ -n "$ZSH_VERSION" ] && [ "$USER" = richih ] && buildPS1
124         verbose "use end"
125         exit 0
126
127 elif [ "$1" = 'clone' ]; then
128         verbose "clone begin"
129         GIT_REMOTE="$2"
130         REPO_NAME="$3"
131         [ -z "$REPO_NAME" ] && REPO_NAME=$(basename $GIT_REMOTE .git)
132         export REPO_NAME
133         export GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
134         init
135
136         git remote add origin $GIT_REMOTE
137         git config branch.master.remote origin
138         git config branch.master.merge  refs/heads/master
139         git fetch
140         for object in $(git ls-tree -r origin/master | awk '{print $4}'); do
141                 [ -e "$object" ] &&
142                         echo "$SELF: error: $object exists." &&
143                         VCSH_CONFLICT=1;
144         done
145         [ "$VCSH_CONFLICT" = '1' ] &&
146                 echo "$SELF: fatal: will stop after fetching and not try to merge!\n" &&
147                 exit 3
148         git merge origin/master
149         vcsh use $REPO_NAME
150         verbose "clone end"
151
152 elif [ "$1" = 'init' ]; then
153         verbose "init begin"
154         export REPO_NAME="$2"
155         export GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
156         init
157         vcsh use $REPO_NAME
158         verbose "init end"
159
160 elif [ "$1" = 'exit' ]; then
161         verbose "exit begin"
162         if [ -n "$ZSH_VERSION" ] && [ "$VCSH_NO_IGNORE_EOF" = '1' ]; then
163                 unset VCSH_NO_IGNORE_EOF
164                 setopt NO_IGNORE_EOF
165         fi
166         leave
167         [ -n "$ZSH_VERSION" ] && [ "$USER" = richih ] && buildPS1
168         verbose "exit end"
169         exit 0
170
171 else
172         verbose "defaulting to calling help()"
173         help
174         exit 3
175
176 fi
177