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

Initial commit
[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 "$(basename $0): 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 "$(basename $0): 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         export GIT_REMOTE="$2"
93         export REPO_NAME="$3"
94         export GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
95         init
96
97         git remote add origin $GIT_REMOTE
98         git config branch.master.remote origin
99         git config branch.master.merge  refs/heads/master
100         git fetch
101         for i in $(git ls-tree -r origin/master | awk '{print $4}'); do
102         [[ -e $i ]] &&
103                 echo "$(basename $0): error: $i exists." &&
104                 CONFLICT=1;
105         done
106         [[ -n $CONFLICT ]] &&
107                 echo "$(basename $0): fatal: will stop after fetching and not try to merge!\n" &&
108                 exit 3
109         git merge origin/master
110         vcsh use $REPO_NAME
111
112 elif [ "$1" = 'init' ]; then
113         export REPO_NAME="$2"
114         export GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
115         init
116         vcsh use $REPO_NAME
117
118 elif [ "$1" = 'exit' ]; then
119         if [[ $VCSH_NO_IGNORE_EOF -gt 0 ]]; then
120                 unset VCSH_NO_IGNORE_EOF
121                 setopt NO_IGNORE_EOF
122         fi
123         leave
124         buildPS1
125         return 0
126
127 else
128         help
129         return 3
130
131 fi
132