output asked headers in the order they were asked; avoid header name spoofing by...
[hband-tools.git] / user-tools / git-checkout-sparse
blob75c89e0b55998e8abb8314a9752f0180bb230260
1 #!/bin/bash
3 set -e
4 set -u
6 repourl=''
7 dir=''
8 depth=1
9 remote=origin
10 branch=''
11 declare -a paths=()
13 while [ -n "${1:-}" ]
15 case "$1" in
16 -d)
17 shift
18 dir=$1
20 -b)
21 shift
22 branch=$1
24 -o)
25 shift
26 origin=$1
28 -h|--help)
29 echo "${0##*/} GIT_REPO_URL [-d CLONE_DIRECTORY] [-o ORIGIN_NAME] [-b BRANCH] PATH [PATH [PATH ...]]"
30 exit 1
32 -*)
33 echo "${0##*/}: invalid option: $1" >&2
34 exit 1
37 if [ "${repourl:-}" = "" ]
38 then
39 repourl=$1
40 else
41 paths+=("$1")
44 esac
45 shift
46 done
48 repourlnoslash=${repourl%%/}
49 reponame=${repourlnoslash##*/}
50 dir="${dir:-$reponame}"
52 clean()
54 rm -rv "$dir"
57 trap clean SIGINT SIGQUIT SIGTERM
59 mkdir "$dir"
61 cd "$dir"
62 git init
63 git config core.sparseCheckout true
64 git remote add "$remote" "$repourl"
65 git fetch --depth=$depth "$remote" "$branch:$branch"
66 for path in "${paths[@]}"
68 #[ "${path: -1}" = / ] && path="$path*"
69 echo "$path" >> .git/info/sparse-checkout
70 done
71 git checkout "$branch"