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

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