|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script generates a CSV file with the git history of the repository. |
| 4 | +# Run from the project's root directory. |
| 5 | + |
| 6 | +# set environment variables |
| 7 | +# COMMIT_URL="${{ github.server_url }}/${{ github.repository }}/commit/" |
| 8 | +COMMIT_URL="" # makes a nice, clickable link to the commit in the CSV |
| 9 | +input_gpg="true" # whether to include GPG signature information |
| 10 | +input_diffs="true" # whether to create a diff file for each commit |
| 11 | + |
| 12 | +# git log |
| 13 | +if [ $input_gpg = "true" ]; then |
| 14 | + git log main --date=local --pretty="%x40%H%x2C%h%x2C%an%x2C%G?%x2C%GS%x2C%GK%x2C%ad%x2C%x22%s%x22%x2C" --shortstat | tr "\n" " " | tr "@" "\n" >> log.csv |
| 15 | +else |
| 16 | + git log main --date=local --pretty="%x40%H%x2C%h%x2C%an%x2C%ad%x2C%x22%s%x22%x2C" --shortstat | tr "\n" " " | tr "@" "\n" >> log.csv |
| 17 | +fi |
| 18 | + |
| 19 | +# sed magic to remove text from number fields |
| 20 | +sed -i.bak 's/ files changed//' log.csv |
| 21 | +sed -i.bak 's/ file changed//' log.csv |
| 22 | +sed -i.bak 's/ insertions(+)//' log.csv |
| 23 | +sed -i.bak 's/ insertion(+)//' log.csv |
| 24 | +sed -i.bak 's/ deletions(-)//' log.csv |
| 25 | +sed -i.bak 's/ deletion(-)//' log.csv |
| 26 | + |
| 27 | +# download diffs if needed |
| 28 | +if [ $input_diffs = "true" ]; then |
| 29 | + sed -i.bak -e "1d" log.csv # delete blank line at the top |
| 30 | + initial_commit_id=$(git rev-list --max-parents=0 HEAD) # get the initial commit id |
| 31 | + initial_commit_short_id=$(git rev-list --max-parents=0 HEAD | cut -c 1-7) # get the initial commit short id |
| 32 | + git show "$initial_commit_id" > "$initial_commit_short_id".diff # write the first diff to the file |
| 33 | + while read -r line; do # loop through the rest of the diffs |
| 34 | + commit_id=$(echo "$line" | awk -F"," '{print $1}') |
| 35 | + short_commit_id=$(echo "$line" | awk -F"," '{print $2}') |
| 36 | + git show "$commit_id" > "$short_commit_id".diff |
| 37 | + done < log.csv |
| 38 | +fi |
| 39 | + |
| 40 | +# awk to insert the commit url to click and view the diff |
| 41 | +awk -F"," 'OFS = ", " {$1 = "'"$COMMIT_URL"'"$1; print}' log.csv > history.csv |
| 42 | + |
| 43 | +# now add that header row |
| 44 | +if [ $input_gpg = "true" ]; then |
| 45 | + sed -i.bak '1s/.*/url,commit id,author,commit signature status,name of signer,key used to sign,date,comment,changed files,lines added,lines deleted/' history.csv |
| 46 | +else |
| 47 | + sed -i.bak '1s/.*/url,commit id,author,date,comment,changed files,lines added,lines deleted/' history.csv |
| 48 | +fi |
| 49 | + |
| 50 | +# clean up |
| 51 | +rm log.csv |
| 52 | +rm log.csv.bak |
| 53 | +rm history.csv.bak |
0 commit comments