Whamcloud - gitweb
LU-1346 contrib: fix libcfs_cleanup script
[fs/lustre-release.git] / lustre / kernel_patches / scripts / extract_description
1 #!/bin/sh
2
3 insert_line()
4 {
5         PATTERN="$1"
6         LINE="$2"
7         FILE="$3"
8         awk ' BEGIN { found=0; }
9                 /'"$PATTERN"'/ { 
10                         print; 
11                         if (!found)
12                                 printf("%s\n", "'$LINE'"); 
13                         found=1; 
14                         next;
15                 }
16                 { print; }
17         ' < "$FILE"
18 }
19
20 # extract the description from the top of a patch
21 # filter stdin
22 # collapse adjacent blank lines to a single blank line
23 # remove any lines that look like diffstat output
24 # stop output on encountering a line beginning with '---' (beginning of patch)
25
26         TMPFILE=`mktemp /tmp/xdtmp.XXXXXX` || exit 1
27         formail -kfcb -X 'From:' -X 'Subject:' |\
28         awk '
29                 BEGIN { found_end=0; lastone="x"; }
30                 /^ .* [|] +[0-9]+ [+-]+$/ { 
31                         #/* we found something like diffstat output... */
32                         if (found_end == 1) {
33                                 /* we are past end of diffstat, let it pass */
34                                 print;
35                         }
36                         next;
37                 }
38                 /^ [1-9][0-9]* files changed/ {
39                         #/* end of diffstat output, stop filtering diffstat */
40                         found_end=1;
41                         next;
42                 }
43                 /^--- / { exit; }
44                 {
45                         #/* collapse adjacent blank lines to 1 blank line */ 
46                         if ( $0 == "" && lastone == "" )
47                                 next;
48                         else 
49                                 print; 
50                         lastone=$0;
51                 }
52         ' | awk '{ if ($0 == "" && FNR == 1)  next; print; }' > "$TMPFILE"
53
54         descs=`head -10 $TMPFILE | grep -c '^[  ]*DESC[         ]*$'`
55         if [ "$descs" = "0" ]
56         then
57                 # DESC is not 1st non blank line in the file
58                 echo "DESC"
59                 descs=0
60         fi
61         edescs=`grep -c '^EDESC$' "$TMPFILE"`
62         subjects=`grep -c '^[   ]*Subject[:]' "$TMPFILE"`
63         froms=`grep -c '^[      ]*From[:]' "$TMPFILE"`
64         if [ "$edescs" = "0" ]
65         then
66                 if [ "$subjects" != "0" ]
67                 then
68                         insert_line '^Subject[:]' 'EDESC' "$TMPFILE"
69                 else
70                         if [ "$froms" != "0" ]
71                         then
72                                 insert_line '^From[:]' 'EDESC' "$TMPFILE"
73                         else
74                                 if [ "$descs" = "0" ]
75                                 then
76                                         # blank DESC line...
77                                         echo '(undescribed patch)'
78                                         echo EDESC
79                                         cat "$TMPFILE"
80                                 else
81                                         insert_line '^DESC$' "EDESC" "$TMPFILE"
82                                 fi
83                         fi
84                 fi
85         else
86                 cat $TMPFILE
87         fi