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

Cleanup of Corey's patch
[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                 vcsh_exit() {vcsh exit; zle reset-prompt;}
97                 zle -N vcsh_exit
98                 bindkey '^d' 'vcsh_exit'
99         fi
100         use $2
101         buildPS1
102         return 0
103
104 elif [ "$1" = 'clone' ]; then
105         GIT_REMOTE="$2"
106         REPO_NAME="$3"
107         [[ -z $REPO_NAME ]] && REPO_NAME=$(basename $GIT_REMOTE .git)
108         export REPO_NAME
109         export GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
110         init
111
112         git remote add origin $GIT_REMOTE
113         git config branch.master.remote origin
114         git config branch.master.merge  refs/heads/master
115         git fetch
116         for i in $(git ls-tree -r origin/master | awk '{print $4}'); do
117         [[ -e $i ]] &&
118                 echo "$SELF: error: $i exists." &&
119                 CONFLICT=1;
120         done
121         [[ -n $CONFLICT ]] &&
122                 echo "$SELF: fatal: will stop after fetching and not try to merge!\n" &&
123                 exit 3
124         git merge origin/master
125         vcsh use $REPO_NAME
126
127 elif [ "$1" = 'init' ]; then
128         export REPO_NAME="$2"
129         export GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
130         init
131         vcsh use $REPO_NAME
132
133 elif [ "$1" = 'exit' ]; then
134         if [[ $VCSH_NO_IGNORE_EOF -gt 0 ]]; then
135                 unset VCSH_NO_IGNORE_EOF
136                 setopt NO_IGNORE_EOF
137         fi
138         leave
139         buildPS1
140         return 0
141
142 else
143         help
144         return 3
145
146 fi
147