This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_from_sisters.lib.sh
138 lines (124 loc) · 3.84 KB
/
update_from_sisters.lib.sh
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
## ---------------------------------------------------------------------
##
## Copyright (C) 2017 by the gint authors
##
## This file is part of gint.
##
## gint is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## gint is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with gint. If not, see <http://www.gnu.org/licenses/>.
##
## ---------------------------------------------------------------------
## DO NOT EDIT
## This file is automatically generated from a file in the repository "lazyten".
## Edit the original and call the script "update_from_sister_repos.sh" instead.
# A script file which contains a script licence header
SCRIPT_FILE="CMakeLists.txt"
if [ ! -r "$SCRIPT_FILE" ]; then
echo "SCRIPT_FILE ($SCRIPT_FILE) not readable. Are you running" \
"the script from the top level of the repo?" >&2
return 1
fi
# extract licence header from SCRIPT_FILE
script_header() {
# The header is delimited by "-------" characters
awk '
$1 == "##" && $2 ~ /^-+$/ {
# invert printing flag and print line
pr = !pr
print
next
}
# print the line
pr
' "$SCRIPT_FILE"
}
# Replace the licence header on the stream
replace_header() {
local REPO="$1" # The original repo the file is from
awk -v "newheader=`script_header`" -v "repo=$REPO" '
!headerprinted && noprint{
# print header exactly once if inside
# the noprint part of the file (where the
# header to replace is located)
headerprinted=1
print newheader
print "\n## DO NOT EDIT"
print "## This file is automatically generated from " \
"a file in the repository \"" repo "\"."
print "## Edit the original and call the script " \
"\"update_from_sister_repos.sh\" instead."
}
$1 == "##" && $2 ~ /^-+$/ {
# Invert printing flag => no printing
# if inside header
noprint = !noprint
next
}
# print the line if required
!noprint
'
}
# Try to find another repository
find_repo() {
local REPO="$1"
if [ -e "../$REPO/.git" ]; then
echo "../$REPO"
return 0
fi
if [ -e "external/$REPO/.git" ]; then
echo "external/$REPO"
return 0
fi
if [ -e "modules/$REPO/.git" ]; then
echo "modules/$REPO"
return 0
fi
return 1
}
# Update a file if necessary and replace its licence header
update_file() {
local ORIGREPO=$1 # repo to get the file from
local FILE=$2 # File to update (same location in both repos)
local REPLACEHEADER=$3 # Replace the licence header or not
# Try to find the other repository to get the file from
local REPODIR
if ! REPODIR=`find_repo "$ORIGREPO"`; then
echo "Could not find repo $ORIGREPO" >&2
return 1
fi
# Path to get the file from:
local FROMPATH="$REPODIR/$FILE"
if [ ! -f "$FROMPATH" ]; then
echo "FILE file ($FILE) does not exist in source repository $REPODIR." >&2
echo "Are you running the script from the top level directory of this repo?" >&2
return 1
fi
# relative dir in which file will be placed
local TODIR=`dirname "$FILE"`
if [ ! -d "$TODIR" ]; then
echo "TODIR ($TODIR) is not a valid directory." >&2
echo "Are you running the script from the top level directory of this repo?" >&2
return 1
fi
if [ ! -f "$FILE" -o "$FROMPATH" -nt "$FILE" ]; then
echo "Updating $FILE from $FROMPATH"
if [ "$REPLACEHEADER" == "keep_header" ]; then
cp "$FROMPATH" "$FILE" || return 1
else
< "$FROMPATH" replace_header "$ORIGREPO"> "$FILE" || return 1
fi
touch --reference="$FROMPATH" "$FILE"
chmod --reference="$FROMPATH" "$FILE"
fi
return 0
}