|
| 1 | +#/usr/bin/sh |
| 2 | + |
| 3 | +program_name=${0##*/} |
| 4 | + |
| 5 | +_reset="\033[0m" |
| 6 | +_bold="\033[1m" |
| 7 | +_italic="\033[3m" |
| 8 | +_underline="\033[4m" |
| 9 | + |
| 10 | +print_help(){ |
| 11 | + echo -e "$(cat <<USAGE |
| 12 | +NAME: |
| 13 | + ${program_name} - a command line tool to clone a git repository's specific files/directories |
| 14 | + |
| 15 | +SYNOPSIS: |
| 16 | + $_underline$program_name$_reset [OPTIONS]... REQUIRED_ARGS... <${_underline}repository_url$_reset> |
| 17 | + |
| 18 | +DESCRIPTION: |
| 19 | + -t=<${_underline}fetch_target$_reset>, --target=<${_underline}fetch_target$_reset> |
| 20 | + Path for all the files that you want to clone. |
| 21 | + |
| 22 | + Note: |
| 23 | + - The fetch targets follow a glob pattern, henceforth you can specify what to fetch in a generic sense with regex. |
| 24 | + Something that you want to fetch will have the following pattern, /<path>/<in>/<repo>, the leading '/' is necessary as it is a part of the specifiaction |
| 25 | + - This option can be used multiple times to add more than one fetch target. |
| 26 | + |
| 27 | +OPTIONS: |
| 28 | + -h, --help |
| 29 | + Prints this help message and exits |
| 30 | + -b=<${_underline}branch/tag$_reset>, --branch=<${_underline}branch/tag$_reset> |
| 31 | + Name of branch/tag to be checked out. |
| 32 | + -c=<${_underline}commit_sha$_reset>, --commit=<${_underline}commit_sha$_reset> |
| 33 | + Commit Hash of the commit that you want to checkout. |
| 34 | + -o=<${_underline}out_dir_path$_reset> |
| 35 | + The output directory for the fetched files. |
| 36 | + |
| 37 | + Notes: |
| 38 | + - If no branch/tag/commit-sha is provided the program will default to the default branch of the repository |
| 39 | + - You cannot use the branch option with commit option as they are mutually exclusive. |
| 40 | + - For valid branch/tag names, see ${_bold}git-check-ref-format(1)$_reset |
| 41 | + - The -o option is optional and <${_underline}out_dir_path$_reset> defaults to the basename of the repository |
| 42 | + - In case the <${_underline}out_dir_path$_reset> already exist, the program will an appropriate suffix to the <${underline}out_dir_path$_reset> to prevent overwriting files. |
| 43 | + |
| 44 | +EXAMPLES: |
| 45 | + - Fetch the files and directories in the root directory of the repo starting with \`hello' into a directory named \`hello-files' from the \`dev' branch |
| 46 | + |
| 47 | + $ ${program_name} --target=/hello\* -o=hello-files -b=dev https://github.com/... |
| 48 | + |
| 49 | + - Fetch all the png images from a repository at the commit \`ae45bb8' |
| 50 | + |
| 51 | + $ ${program_name} --target=/\*\*/\*.png -c=ae45bb8 https://github.com/... |
| 52 | +USAGE |
| 53 | +)" |
| 54 | + exit $@ |
| 55 | +} |
| 56 | + |
| 57 | +parse_args() { |
| 58 | + [[ "0" == "#$" ]] && print_help 1 |
| 59 | + |
| 60 | + while [ $# -gt 0 ];do |
| 61 | + case "$1" in |
| 62 | + -b=*|--branch=*) |
| 63 | + [[ -z $clone_opts ]] || { |
| 64 | + echo commit sha already specified >&2 |
| 65 | + print_help 1 |
| 66 | + } |
| 67 | + local branch="${1#*=}" |
| 68 | + [[ -z $branch ]] && { |
| 69 | + echo branch can\'t have empty value |
| 70 | + print_help 1 |
| 71 | + } |
| 72 | + git check-ref-format --branch "$branch" >/dev/null || exit 1 |
| 73 | + clone_opts="-n -j4 -b ${branch} --depth=1 --filter=blob:none" |
| 74 | + shift |
| 75 | + ;; |
| 76 | + -c=*|--commit=*) |
| 77 | + [[ -z $clone_opts ]] || { |
| 78 | + echo branch/tag already specified >&2 |
| 79 | + print_help 1 |
| 80 | + } |
| 81 | + local commit_sha="${1#*=}" |
| 82 | + [[ $commit_sha =~ ^[0-9a-fA-F]{1,64}$ ]] || { |
| 83 | + echo "invalid commit sha" |
| 84 | + exit 1 |
| 85 | + } |
| 86 | + [[ -z $commit_sha ]] && { |
| 87 | + echo sha can\'t have empty value |
| 88 | + print_help 1 |
| 89 | + } |
| 90 | + clone_opts="-n -j4 --filter=blob:none" |
| 91 | + checkout_opts=$commit_sha |
| 92 | + shift |
| 93 | + ;; |
| 94 | + -o=*) |
| 95 | + [[ -z $out_dir ]] || { |
| 96 | + echo can\'t have multiple output directories |
| 97 | + print_help 1 |
| 98 | + } |
| 99 | + out_dir="${1#*=}" |
| 100 | + [[ -z $out_dir ]] && { |
| 101 | + echo out dir can\'t have empty value |
| 102 | + print_help 1 |
| 103 | + } |
| 104 | + shift |
| 105 | + ;; |
| 106 | + -t=*|--target=*) |
| 107 | + local sub_dir="${1#*=}" |
| 108 | + [[ -z $sub_dir ]] && { |
| 109 | + echo clone targets can\'t have empty value |
| 110 | + print_help 1 |
| 111 | + } |
| 112 | + sub_dirs="${sub_dirs} ${sub_dir}" |
| 113 | + shift |
| 114 | + ;; |
| 115 | + -h|--help) |
| 116 | + print_help 0 |
| 117 | + ;; |
| 118 | + -*|--*) |
| 119 | + echo invalid option |
| 120 | + print_help 1 |
| 121 | + ;; |
| 122 | + *) |
| 123 | + [[ -z $repo_url ]] || { |
| 124 | + echo you can\'t clone multiple url\'s |
| 125 | + print_help 1 |
| 126 | + } |
| 127 | + repo_url="$1" |
| 128 | + shift |
| 129 | + ;; |
| 130 | + esac |
| 131 | + done |
| 132 | + |
| 133 | + [[ -z $repo_url ]] && { |
| 134 | + echo "Need a url to clone" |
| 135 | + print_help 1 |
| 136 | + } |
| 137 | + repo_base="${repo_url##*/}" |
| 138 | + repo_base="${repo_base%.git}" |
| 139 | + [[ -z $repo_base ]] && { |
| 140 | + echo invalid remote repository url |
| 141 | + } |
| 142 | + out_dir="${out_dir:-${repo_base}}" |
| 143 | + clone_opts="${clone_opts:-"-n --depth=1 --filter=blob:none"}" |
| 144 | + sub_dirs="${sub_dirs# }" |
| 145 | + [[ -z $sub_dirs ]] && { |
| 146 | + echo at least one target node is required for cloning |
| 147 | + print_help 1 |
| 148 | + } |
| 149 | +} |
| 150 | +parse_args "$@" |
| 151 | +tmpdir=$(mktemp -d) |
| 152 | +git clone $clone_opts "$repo_url" "$tmpdir/$repo_base" || exit 1 |
| 153 | +git -C "$tmpdir/$repo_base" sparse-checkout set --no-cone $sub_dirs || exit 1 |
| 154 | +git -C "$tmpdir/$repo_base" checkout $checkout_opts || exit 1 |
| 155 | +rm -rf "$tmpdir/$repo_base/.git" |
| 156 | +[[ -d "$out_dir" ]] && while true; do |
| 157 | + suff=1 |
| 158 | + if [[ -d "$out_dir-$suff" ]]; then |
| 159 | + suff=$(( suff + 1 )) |
| 160 | + else |
| 161 | + out_dir="$out_dir-$suff" |
| 162 | + break |
| 163 | + fi |
| 164 | +done |
| 165 | +mkdir -p "$out_dir" |
| 166 | +mv "$tmpdir/$repo_base"/* "$tmpdir/$repo_base"/.* "$out_dir" 2>/dev/null |
| 167 | +rm -rf $tmpdir |
0 commit comments