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.
3 # This program is licensed under the GNU GPL version 2 or later.
4 # (c) Richard "RichiH" Hartmann, 2011
5 # For details, see LICENSE. To submit patches, you have to agree to
6 # license your code under the GNU GPL version 2 or later.
9 [ -n "$VCSH_DEBUG" ] && set -vx
10 [ -z "$XDG_CONFIG_HOME" ] && XDG_CONFIG_HOME="$HOME/.config"
11 [ -z "$VCSH_REPO_D" ] && VCSH_REPO_D="$XDG_CONFIG_HOME/vcsh/repo.d"
12 [ -z "$VCSH_BASE" ] && VCSH_BASE="$HOME"
17 echo "usage: $SELF <args>
20 [<repo>] Clone from an existing repository
21 help Display this help text
22 delete Delete an existing repository
23 enter Enter repository; spawn new instance of \$SHELL
24 init <repo> Initialize a new repository
25 list List all repositories
26 list-tracked List all files tracked by vcsh
28 <repo> List files tracked by a repository
30 <newname> Rename repository
32 <command> Use this repository
33 setup Set up repository with recommended settings
35 <repo> Write .gitignore.d/<repo> via git ls-files
37 <repo> <git command> Shortcut to run git commands directly
38 <repo> Shortcut to enter repository" >&2
42 [ -n "$VCSH_DEBUG" ] && echo "$SELF: debug: $@"
46 if [ -n "$VCSH_DEBUG" ] || [ -n "$VCSH_VERBOSE" ]; then echo "$SELF: verbose: $@"; fi
50 echo "$SELF: fatal: $1" >&2
55 echo "$SELF: error: $1" >&2
59 echo "$SELF: info: $1"
64 git remote add origin "$GIT_REMOTE"
65 git config branch.master.remote origin
66 git config branch.master.merge refs/heads/master
67 if [ $(git ls-remote origin master 2> /dev/null | wc -l ) -lt 1 ]; then
68 info "remote is empty, not merging anything"
72 for object in $(git ls-tree -r origin/master | awk '{print $4}'); do
74 error "'$object' exists." &&
77 [ "$VCSH_CONFLICT" = '1' ] &&
78 fatal "will stop after fetching and not try to merge!
79 Once this situation has been resolved, run 'vcsh run $VCSH_REPO_NAME git pull' to finish cloning.\n" 17
80 git merge origin/master
84 cd "$VCSH_BASE" || fatal "could not enter '$VCSH_BASE'" 11
86 info "This operation WILL DETROY DATA!"
88 echo "These files will be deleted:
92 AGAIN, THIS WILL DELETE YOUR DATA!
93 To continue, type \"Yes, do as I say\""
95 [ "x$answer" = "xYes, do as I say" ] || exit 16
96 for file in $files; do
97 rm -f $file || info "could not delete '$file', continuing with deletion"
99 rmdir "$GIT_DIR" || error "could not delete '$GIT_DIR'"
108 [ -d "$GIT_DIR" ] || fatal "no repository found for '$VCSH_REPO_NAME'" 12
112 [ ! -e "$GIT_DIR" ] || fatal "'$GIT_DIR' exists" 10
113 export GIT_WORK_TREE="$VCSH_BASE"
114 mkdir -p "$GIT_WORK_TREE" || fatal "could not create '$GIT_WORK_TREE'" 50
115 cd "$GIT_WORK_TREE" || fatal "could not enter '$GIT_WORK_TREE'" 11
121 for i in "$VCSH_REPO_D"/*.git; do
122 echo $(basename "$i" .git)
127 export GIT_DIR="$VCSH_REPO_D/$VCSH_REPO_NAME.git"
132 for VCSH_REPO_NAME in $(list); do
139 git ls-files | sort -u
144 [ -d "$GIT_DIR_NEW" ] && fatal "'$GIT_DIR_NEW' exists" 54
145 mv -f "$GIT_DIR" "$GIT_DIR_NEW" || fatal "Could not mv '$GIT_DIR' '$GIT_DIR_NEW'" 52
151 $VCSH_EXTERNAL_COMMAND
156 git config core.worktree "$GIT_WORK_TREE"
157 git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME"
158 git config vcsh.vcsh 'true'
159 [ -e "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME" ] && git add -f "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME"
164 export GIT_WORK_TREE="$(git config --get core.worktree)"
165 export VCSH_DIRECTORY="$VCSH_REPO_NAME"
170 cd "$VCSH_BASE" || fatal "could not enter '$VCSH_BASE'" 11
171 gitignores=$(for file in $(git ls-files); do
173 echo $file; new="${file%/*}"
174 [ "$file" = "$new" ] && break
178 tempfile=$(mktemp) || fatal "could not create tempfile" 51
179 echo '*' > "$tempfile" || fatal "could not write to '$tempfile'" 57
180 for gitignore in $gitignores; do
181 echo "$gitignore" | sed 's/^/!/' >> "$tempfile" || fatal "could not write to '$tempfile'" 57
182 [ -d "$gitignore" ] && { echo "$gitignore/*" | sed 's/^/!/' >> "$tempfile" || fatal "could not write to '$tempfile'" 57; }
184 if diff -N "$tempfile" "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME" > /dev/null; then
185 rm -f "$tempfile" || error "could not delete '$tempfile'"
188 if [ -e "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME" ]; then
189 info "'$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME' differs from new data, moving it to '$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME.bak'"
190 mv -f "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME" "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME.bak" ||
191 fatal "could not move '$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME' to '$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME.bak'" 53
193 mv -f "$tempfile" "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME" ||
194 fatal "could not move '$tempfile' to '$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME'" 53
197 if [ "$1" = 'clone' ]; then
198 [ -z $2 ] && fatal "$1: please specify a remote" 1
199 export VCSH_COMMAND="$1"
201 [ -n "$3" ] && VCSH_REPO_NAME="$3" || VCSH_REPO_NAME=$(basename "$GIT_REMOTE" .git)
202 export VCSH_REPO_NAME
203 export GIT_DIR="$VCSH_REPO_D/$VCSH_REPO_NAME.git"
204 elif [ "$1" = 'delete' ] ||
205 [ "$1" = 'enter' ] ||
207 [ "$1" = 'list-tracked-by' ] ||
208 [ "$1" = 'rename' ] ||
210 [ "$1" = 'setup' ] ||
211 [ "$1" = 'write-gitignore' ]; then
212 [ -z $2 ] && fatal "$1: please specify repository to work on" 1
213 [ "$1" = 'rename' -a -z "$3" ] && fatal "$1: please specify a target name" 1
214 [ "$1" = 'run' -a -z "$3" ] && fatal "$1: please specify a command" 1
215 export VCSH_COMMAND="$1"
216 export VCSH_REPO_NAME="$2"
217 export GIT_DIR="$VCSH_REPO_D/$VCSH_REPO_NAME.git"
218 [ "$VCSH_COMMAND" = 'rename' ] && export GIT_DIR_NEW="$VCSH_REPO_D/$3.git"
219 [ "$VCSH_COMMAND" = 'run' ] && shift 2 && export VCSH_EXTERNAL_COMMAND="$*"
220 [ "$VCSH_COMMAND" = 'write-gitignore' ]
221 elif [ "$1" = 'list' ] ||
222 [ "$1" = 'list-tracked' ]; then
223 export VCSH_COMMAND="$1"
224 elif [ -n "$2" ]; then
225 export VCSH_COMMAND='run'
226 export VCSH_REPO_NAME="$1"
227 export GIT_DIR="$VCSH_REPO_D/$VCSH_REPO_NAME.git"
228 [ -d $GIT_DIR ] || { help; exit 1; }
230 export VCSH_EXTERNAL_COMMAND="git $*"
231 elif [ -n "$1" ]; then
232 export VCSH_COMMAND='enter'
233 export VCSH_REPO_NAME="$1"
234 export GIT_DIR="$VCSH_REPO_D/$VCSH_REPO_NAME.git"
235 [ -d $GIT_DIR ] || { help; exit 1; }
237 # $1 is empty, or 'help'
241 # Did we receive a directory instead of a name?
242 # Mangle the input to fit normal operation.
243 if echo $VCSH_REPO_NAME | grep -q '/'; then
244 export GIT_DIR=$VCSH_REPO_NAME
245 export VCSH_REPO_NAME=$(basename $VCSH_REPO_NAME .git)
249 for check_directory in "$VCSH_REPO_D" "$VCSH_BASE/.gitignore.d"
251 if [ ! -d "$check_directory" ]; then
252 if [ -e "$check_directory" ]; then
253 fatal "'$check_directory' exists but is not a directory" 13
255 info "attempting to create '$check_directory'"
256 mkdir -p "$check_directory" || fatal "could not create '$check_directory'" 50
261 verbose "$VCSH_COMMAND begin"
262 export VCSH_COMMAND=$(echo $VCSH_COMMAND | sed 's/-/_/g')
264 verbose "$VCSH_COMMAND end, exiting"