hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

copyright.sh (2980B)


      1 #!/bin/sh -eu
      2 
      3 files=$(find . -name '*.ha')
      4 
      5 for file in $files; do
      6   printf '%s\n' "$file"
      7 
      8   authorinfo=$(git blame $file --porcelain --incremental)
      9 
     10   # Format as: Author Name <test@example.com>;2011
     11   year_authorinfo=""
     12   ignore_this_commit=false
     13   while read -r line; do
     14     if [ "$ignore_this_commit" = true ]; then
     15       case $line in
     16         "filename "*) ignore_this_commit=false ;;
     17         *) ;;
     18       esac
     19     else
     20       case $line in
     21         # Add commits that updated the copyright info here in order to ignore them
     22         "b791275bfb0fe5d7928c19635e75fd3d98e3ba97"*) ignore_this_commit=true ;;
     23         # This is the “not committed yet” commit
     24         "0000000000000000000000000000000000000000"*) ignore_this_commit=true ;;
     25         "author "*) author=$(printf '%s' "$line" | sed 's/author //' ) ;;
     26         "author-mail "*) mail=$(printf '%s' "$line" | sed 's/author-mail //' ) ;;
     27         "author-time "*) timestamp=$(printf '%s' "$line" | sed 's/author-time //' ) ;;
     28         "filename "*)
     29           year=$(date +%Y -d @${timestamp})
     30           year_authorinfo=$(printf '%s\n%s' "${year_authorinfo}" "${author} ${mail};${year}")
     31           ;;
     32         *) ;;
     33       esac
     34     fi
     35   done <<EOF
     36   $authorinfo
     37 EOF
     38 
     39   # Get only the unique author names
     40   uniq_authors="$(printf '%s\n' "$year_authorinfo" | tail -n +2 | awk -F ';' '{print $1}' | sort -u)"
     41 
     42   # Get all years for each author, and condense them into one line per author,
     43   # with the earliest contribution as the start year, and the latest
     44   # contribution as the end year
     45   condensed_authorinfo=""
     46   while read -r author; do
     47     years_for_author="$(printf '%s' "$year_authorinfo" | awk -F ';' "{if (\$1 == \"$author\") print \$2}")"
     48     min_year="$(printf '%s' "$years_for_author" | sort | head -n 1)"
     49     max_year="$(printf '%s' "$years_for_author" | sort | tail -n 1)"
     50     if [ "$min_year" = "$max_year" ]; then
     51       condensed_authorinfo=$(printf '%s\n%s' "$condensed_authorinfo" "$author;$min_year")
     52     else
     53       condensed_authorinfo=$(printf '%s\n%s' "$condensed_authorinfo" "$author;$min_year-$max_year")
     54     fi
     55   done <<EOF
     56   $uniq_authors
     57 EOF
     58 
     59   sorted_condensed_authorinfo="$(printf '%s' "$condensed_authorinfo" | tail -n +2 | sort -u)"
     60   formatted_authorinfo="$(printf '%s' "$sorted_condensed_authorinfo" | awk -F ';' '{print "// (c) " $2 " " $1}')"
     61 
     62   case $file in
     63     "./cmd/"*) header="// License: GPL-3.0" ;;
     64     *) header="// License: MPL-2.0" ;;
     65   esac
     66 
     67   n_existing_license_lines=$(sed '/\(^\/\/ License\|^\/\/ (c)\|^$\)/! Q' $file | wc -l)
     68   line_to_start_from=$((n_existing_license_lines + 1))
     69 
     70   tail -n +${line_to_start_from} $file > copyright_tmp
     71 
     72   if [ -z "$(sed -n '1{/^use/p};q' copyright_tmp)" ]; then
     73     # File does not start with "use"
     74     printf '%s\n%s\n\n' "$header" "$formatted_authorinfo" | cat - copyright_tmp > $file
     75   else
     76     # File starts with "use"
     77     printf '%s\n%s\n' "$header" "$formatted_authorinfo" | cat - copyright_tmp > $file
     78   fi
     79 
     80   rm copyright_tmp
     81 done