-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-store-modes
More file actions
executable file
·142 lines (112 loc) · 2.4 KB
/
git-store-modes
File metadata and controls
executable file
·142 lines (112 loc) · 2.4 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
DFLT_MODEFILE='.git-mode-cache'
USAGE="Usage:
${0##*/} save OPTIONS
or ${0##*/} restore OPTIONS
Save or restore permissions of files in a git repository.
Options:
-f file: Save permissions to (or load from) file (default: ${DFLT_MODEFILE})
Note: if a relative path is supplied, it is interpreted relative to
the repository root.
-h: Show this help and exit.
"
function go_worktree_root() {
local CDUP
CDUP=$(git rev-parse --show-cdup)
cd "${CDUP}"
}
function files_ancestors() {
local FPATH
while read -r -d '' FPATH; do
printf '%s\0' "${FPATH}"
while [[ "${FPATH}" && "${FPATH}" == */* ]]; do
FPATH="${FPATH%/*}"
printf '%s\0' "${FPATH}"
done
done
}
function filter_symlinks() {
local FILENAME
while read -r -d '' FILENAME; do
if [[ -a "${FILENAME}" && ! -L "${FILENAME}" ]]; then
printf '%s\0' "${FILENAME}"
fi
done
}
function get_modes() {
local -a STATOPTS=(--printf '%a\t%n\0')
local STAT
if stat --printf '' . &> /dev/null; then
STAT=stat
else
STAT=gstat
fi
git ls-files -cz --full-name \
| files_ancestors \
| filter_symlinks \
| sort -zu \
| xargs -0 "${STAT}" "${STATOPTS[@]}"
}
function restore_modes() {
local MODE
local FILENAME
trap IFS="${IFS}" RETURN
printf -v IFS '\t'
while read -r -d '' MODE FILENAME; do
if [[ -a "${FILENAME}" && ! -L "${FILENAME}" ]]; then
chmod "${MODE}" "${FILENAME}"
fi
done
}
function process_args() {
local OPTIND=1
local -n CMD=$1
local -n FNAME=$2
CMD="${3^^*}"
FNAME="${DFLT_MODEFILE}"
shift 3
while getopts 'f:h' OPT; do
case "${OPT}" in
'f')
FNAME=${OPTARG}
;;
'h')
echo "${USAGE}" >&2
exit
;;
*)
echo "${USAGE}" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
if [[ $# -gt 0 ]]; then
echo "Unexpected arguments: $@" >&2
echo "${USAGE}" >&2
exit 1
fi
}
function main() {
local COMMAND
local MODEFILE
process_args COMMAND MODEFILE "$@"
go_worktree_root
case "${COMMAND}" in
'SAVE')
get_modes > "${MODEFILE}"
if [[ -f "${MODEFILE}" ]]; then
git add "$MODEFILE"
fi
;;
'RESTORE')
restore_modes < "${MODEFILE}"
;;
*)
echo "${USAGE}" >&2
exit 1
;;
esac
}
main "$@"