4 # Michał Kuratczyk <kura@pld.org.pl>
5 # Sebastian Zagrodzki <s.zagrodzki@mimuw.edu.pl>
6 # Tomasz Kłoczko <kloczek@rudy.mif.pg.gda.pl>
7 # Artur Frysiak <wiget@pld-linux.org>
8 # Michal Kochanowicz <mkochano@pld.org.pl>
9 # Elan Ruusamäe <glen@pld-linux.org>
11 # See cvs log adapter{,.awk} for list of contributors
13 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
17 Adapter adapts .spec files for PLD Linux.
18 v$VERSION (C) 1999-2016 Free Penguins".
21 dir=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
22 adapter=$dir/adapter.awk
23 usage="Usage: $PROGRAM [FLAGS] SPECFILEs
25 -s|--no-sort|--skip-sort
26 skip BuildRequires, Requires sorting
27 -m|--no-macros|--skip-macros
28 skip use_macros() substitutions
32 skip %defattr corrections
34 do not do any diffing, just dump the output
37 if [ ! -x /usr/bin/getopt ]; then
38 echo >&2 "You need to install util-linux to use adapter"
42 if [ ! -x /usr/bin/patch ]; then
43 echo >&2 "You need to install patch to use adapter"
47 [ -n "$PAGER" ] || PAGER="/usr/bin/less -r"
49 if [ -n "$CONFIG_DIR" ]; then
50 USER_CFG="$CONFIG_DIR/.adapterrc"
51 elif [ -n "$HOME_ETC" ]; then
52 USER_CFG="$HOME_ETC/.adapterrc"
57 [ -f $USER_CFG ] && . $USER_CFG
59 t=$(getopt -o hsomdaV --long help,version,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$PROGRAM" -- "$@") || exit $?
68 -s|--no-sort|--skip-sort)
71 -m|--no-macros|--skip-macros)
92 echo >&2 "$PROGRAM: Internal error: \`$1' not recognized!"
101 # vim like diff colourization
102 LC_ALL=en_US.UTF-8 gawk ' {
103 split( $0, S, /\t/ );
105 for ( i = 2; i in S; i++ ) {
106 spaces = 7 - ( (length( $0 ) - 1) % 8 );
107 $0 = $0 "\xE2\x9E\x94";
108 for ( y = 0; y < spaces; y++ )
109 $0 = $0 "\xE2\x87\xBE";
112 gsub( /\033/, "\033[44m^[\033[49m" );
114 if ( sub( /^ /, "" ) )
116 sub( /(\xE2\x9E\x94(\xE2\x87\xBE)*| )+$/, "\033[31;41m&\033[39;49m" );
117 gsub( /\xE2\x9E\x94(\xE2\x87\xBE)*/, "\033[7m&\033[27m" );
118 gsub( /\xE2\x87\xBE/, " " );
119 # uncomment if you do not like utf-8 arrow
120 # gsub( /\xE2\x9E\x94/, ">" );
122 gsub( /\007/, "\033[44m^G\033[49m" );
123 gsub( /\r/, "\033[44m^M\033[49m" );
125 /^(Index:|diff|---|\+\+\+) / { $0 = "\033[32m" $0 }
126 /^@@ / { $0 = "\033[33m" $0 }
127 /^-/ { $0 = "\033[35m" $0 }
128 /^+/ { $0 = "\033[36m" $0 }
129 { $0 = $0 "\033[0m"; print }
135 # diff2hunks orignally by dig
141 for my $filename (@ARGV) {
144 open $fh, "<", $filename or die "$filename: open for reading: $!";
148 for my $i (0 ... $#lines) {
149 next unless $lines[$i] =~ m/^\@\@ /;
150 if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
151 @curheader = @lines[$i - 2 ... $i - 1];
153 next unless @curheader;
155 while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
157 if $j >= 3 and $j < @lines
158 and $lines[$j - 2] =~ m/^--- /
159 and $lines[$j - 1] =~ m/^\+\+\+ /;
161 $j-- until $lines[$j] =~ m/^[ @+-]/;
162 my $hunkfilename = $filename;
163 $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
165 open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
166 print $ofh @curheader, @lines[$i ... $j];
173 # import selected macros for adapter.awk
174 # you should update the list also in adapter.awk when making changes here
175 import_rpm_macros() {
237 for macro in $macros; do
238 eval_expr="$eval_expr\nexport $macro='%{$macro}'"
242 # get cvsaddress for changelog section
243 # using rpm macros as too lazy to add ~/.adapterrc parsing support.
244 eval_expr="$eval_expr
245 export _cvsmaildomain='%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'
246 export _cvsmailfeedback='%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'
249 eval $(rpm --eval "$(echo -e $eval_expr)")
254 [ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
257 workdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit $?
260 local tmp=$workdir/$(basename $SPECFILE) || exit $?
264 LC_ALL=en_US.UTF-8 $awk -f $adapter $SPECFILE > $tmp || exit $?
266 if [ "$outputonly" = 1 ]; then
269 elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
270 diff -u $SPECFILE $tmp > $tmp.diff
272 diffcol $tmp.diff | $PAGER
274 echo -n "Accept? (Yes, No, Confirm each chunk)? "
279 echo "Ok, adapterized."
282 [cC]) # confirm each chunk
284 for t in $(ls $tmp-*.diff); do
286 echo -n "Accept? (Yes, [N]o, Quit)? "
309 echo "The spec $SPECFILE is perfect ;)"
315 if [ $# -eq 0 ]; then
320 for SPECFILE in "$@"; do