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

INSTALL.md really only talks about install-related stuff, now
[code/vcsh.git] / vcsh
1 #!/bin/sh
2
3 [ -n "$VCSH_DEBUG" ] && set -x
4
5 SELF=$(basename $0)
6
7 [ -z "$XDG_CONFIG_HOME" ] && XDG_CONFIG_HOME="$HOME/.config"
8 [ -z "$VCSH_BASE" ]       && VCSH_BASE="$XDG_CONFIG_HOME/vcsh/repo.d"
9 for check_directory in "$VCSH_BASE"
10 do
11         if [ ! -d "$check_directory" ]; then
12                 if [ -e "$check_directory" ]; then
13                         echo "$SELF: error: $check_directory exists but is not a directory" >&2
14                         exit 2
15                 else
16                         mkdir -p "$check_directory" || (echo "$SELF: error: could not create $check_directory" >&2; exit 2)
17                 fi
18         fi
19 done
20
21
22 debug() {
23         [ -n "$VCSH_DEBUG" ] && echo "$SELF: debug: $1"
24 }
25
26 verbose() {
27         if [ -n "$VCSH_DEBUG" ] || [ -n "$VCSH_VERBOSE" ]; then echo "$SELF: verbose: $1"; fi
28 }
29
30 #   use <repo>     Use this repository
31 #
32 #   exit              Exit vcsh mode" >&2
33 help() {
34         echo "usage: $SELF <args>
35
36    help              Display this help
37
38    list              List all repos
39
40    run <repo> \\
41        <command>     Use this repository
42
43    init <repo>       Initialize a new repository
44    clone <remote> \\
45          [<repo>]    Clone from an existing repository" >&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                 echo "  Once this situation has been resolved, run 'vcsh run <foo> git pull' to finish cloning.\n" &&
148                 exit 3
149         git merge origin/master
150 #       vcsh use $REPO_NAME
151         verbose "clone end"
152
153 elif [ "$1" = 'init' ]; then
154         verbose "init begin"
155         [ -z $2 ] && help && return 0
156         export REPO_NAME="$2"
157         export GIT_DIR="$VCSH_BASE/$REPO_NAME.git"
158         init
159 #       vcsh use "$REPO_NAME"
160         verbose "init end"
161
162 #elif [ "$1" = 'exit' ]; then
163 #       verbose "exit begin"
164 #       if [ -n "$ZSH_VERSION" ] && [ "$VCSH_NO_IGNORE_EOF" = '1' ]; then
165 #               unset VCSH_NO_IGNORE_EOF
166 #               setopt NO_IGNORE_EOF
167 #       fi
168 #       leave
169 #       [ -n "$ZSH_VERSION" ] && [ "$USER" = richih ] && buildPS1
170 #       verbose "exit end"
171 #       exit 0
172
173 else
174         verbose "defaulting to calling help()"
175         help
176         exit 3
177
178 fi
179