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

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