-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-sparse-checkout
More file actions
executable file
·96 lines (78 loc) · 2.26 KB
/
git-sparse-checkout
File metadata and controls
executable file
·96 lines (78 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh
# Author: K. Potamianos <karolos.potamianos@cern.ch>
# Date: 2016-IX-29
cmd=${1}
path=${2}
function usage() {
echo "usage: git sparse-checkout add path"
echo " git sparse-checkout addpkg package_name"
echo " git sparse-checkout list"
echo " git sparse-checkout rm regex"
echo " git sparse-checkout rmpkg package_name"
exit 1
}
function die() {
echo "$@"
exit 128
}
if test $# -lt 1
then
usage
fi
if test "$cmd" != "list" -a $# -lt 2
then
usage
fi
git_dir=$(git rev-parse --show-toplevel)/.git
pkg_path=
function get_sparse_checkout_file() {
if test -d ${git_dir}
then # It's the standard case (or git-new-workdir copy)
echo ${git_dir}/info/sparse-checkout
elif test -f ${git_dir}
then # It's a worktree working copy
echo `cat ${git_dir} | sed 's/gitdir: //'`/info/sparse-checkout
fi
}
# Check whether path exists in git-ls-files (otherwise it makes no sense to add it)
function check_path_in_repo() {
git-ls-files | grep -m 1 '/^${path}/' && die "Error: ${path} not found in repository"
}
# Find 'package' in files known to git
function find_pkg() {
pkg_path=`git ls-files | grep /${path}/ | sed "s@/${path}/.*@/${path}/@" | sort -u`
#echo find_pkg: ${pkg_path}
test -n "${pkg_path}" || die "Error: no pakage named ${path} found in repository."
}
function add_path() {
paths="${@}"
for path in ${paths}
do
grep -m 1 "^${path}\$" ${sparse_checkout_file} && die "Error: ${path} already in sparse-checkout file"
check_path_in_repo
echo ${path} >> ${sparse_checkout_file}
test -n $verbose && echo "${path} added to sparse checkout"
done
}
function rm_path() {
paths="${@}"
for path in ${paths}
do
grep "^${path}\$" ${sparse_checkout_file} > /dev/null || die "Error: ${path} not in sparse-checkout file"
sed -i "\|^${path}\$|d" ${sparse_checkout_file}
test -n $verbose && echo "${path} removed to sparse checkout"
done
}
function list_paths() {
echo "Content of sparse-checkout file:"
cat ${sparse_checkout_file}
}
sparse_checkout_file=`get_sparse_checkout_file`
case ${cmd} in
add) add_path "${path}" ;;
addpkg) find_pkg && add_path "${pkg_path}" ;;
list) list_paths;;
rm) rm_path "${path}" ;;
rmpkg) find_pkg && rm_path "${pkg_path}" ;;
*) usage ;;
esac