Skip to content

Commit

Permalink
add script, remove GHAE references (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
some-natalie authored Jun 13, 2024
1 parent c8e7445 commit a2df606
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Creates a CSV file of some `git log` data, useful for exporting to audit reports

[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/some-natalie/gitlog-to-csv/badge)](https://securityscorecards.dev/viewer/?uri=github.com/some-natalie/gitlog-to-csv)

> [!NOTE]
> Only need the shell script? Here you go - [git-history-report.sh](git-history-report.sh)
## Inputs and Outputs

Inputs
Expand Down Expand Up @@ -57,17 +60,19 @@ Per the [git documentation](https://git-scm.com/docs/git-log#_pretty_formats), t
| E | Signature cannot be checked (e.g. missing key) |
| N | No signature |
:information_source: The runner that you use to execute this Action might need to be set up with your key management server. This may mean you'll need to chat with your key management / identity management folks to get things set up on a private key server.
> [!NOTE]
> The runner that you use to execute this Action might need to be set up to trust your key management server. This may mean you'll need to chat with your key management / identity management folks to get things set up on a private key server.
## GitHub Enterprise version compatibility
Naturally, this works without any hitch on GitHub.com. As a composite Action that calls other Actions, you'll need to be on at least GitHub Enterprise Server or GitHub AE version 3.3 to use this if you're not in GitHub.com.
:information_source: This references the tag `v3` of [`actions/checkout`](https://github.com/actions/checkout) and [`actions/upload-artifact`](https://github.com/actions/upload-artifact), which is (currently) beyond the version shipped bundled in GHES and GHAE. Your enterprise administrator might need to [update](https://docs.github.com/en/enterprise-server@latest/admin/github-actions/managing-access-to-actions-from-githubcom/using-the-latest-version-of-the-official-bundled-actions) the bundled actions. Alternatively, you can copy this repository to your GHES or GHAE instance and downgrade the versions of these dependencies in that process.
> [!NOTE]
> This references the tag `v4` of [`actions/checkout`](https://github.com/actions/checkout) and [`actions/upload-artifact`](https://github.com/actions/upload-artifact), which may be beyond the version shipped bundled in GHES. Your enterprise administrator might need to [update](https://docs.github.com/en/enterprise-server@latest/admin/github-actions/managing-access-to-actions-from-githubcom/using-the-latest-version-of-the-official-bundled-actions) the bundled actions. Alternatively, you can copy this repository to your GHES instance and downgrade the versions of these dependencies in that process.

## Using it without GitHub Actions

:question: Not using or can't use GitHub Actions? Not a problem - the core logic of this report is a plain [bash script](https://github.com/some-natalie/gitlog-to-csv/blob/main/action.yml#L40-L81) that you can plug into your CI system of choice or run _ad hoc_. To run on an arbitrary machine, you'll need the following:
:question: Not using or can't use GitHub Actions? Not a problem - the core logic of this report is a plain [bash script](git-history-report.sh) that you can plug into your CI system of choice or run _ad hoc_. To run on an arbitrary machine, you'll need the following:

- BASH, of course
- GNU `awk` and `sed`
Expand Down
53 changes: 53 additions & 0 deletions git-history-report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# This script generates a CSV file with the git history of the repository.
# Run from the project's root directory.

# set environment variables
# COMMIT_URL="${{ github.server_url }}/${{ github.repository }}/commit/"
COMMIT_URL="" # makes a nice, clickable link to the commit in the CSV
input_gpg="true" # whether to include GPG signature information
input_diffs="true" # whether to create a diff file for each commit

# git log
if [ $input_gpg = "true" ]; then
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
else
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
fi

# sed magic to remove text from number fields
sed -i.bak 's/ files changed//' log.csv
sed -i.bak 's/ file changed//' log.csv
sed -i.bak 's/ insertions(+)//' log.csv
sed -i.bak 's/ insertion(+)//' log.csv
sed -i.bak 's/ deletions(-)//' log.csv
sed -i.bak 's/ deletion(-)//' log.csv

# download diffs if needed
if [ $input_diffs = "true" ]; then
sed -i.bak -e "1d" log.csv # delete blank line at the top
initial_commit_id=$(git rev-list --max-parents=0 HEAD) # get the initial commit id
initial_commit_short_id=$(git rev-list --max-parents=0 HEAD | cut -c 1-7) # get the initial commit short id
git show "$initial_commit_id" > "$initial_commit_short_id".diff # write the first diff to the file
while read -r line; do # loop through the rest of the diffs
commit_id=$(echo "$line" | awk -F"," '{print $1}')
short_commit_id=$(echo "$line" | awk -F"," '{print $2}')
git show "$commit_id" > "$short_commit_id".diff
done < log.csv
fi

# awk to insert the commit url to click and view the diff
awk -F"," 'OFS = ", " {$1 = "'"$COMMIT_URL"'"$1; print}' log.csv > history.csv

# now add that header row
if [ $input_gpg = "true" ]; then
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
else
sed -i.bak '1s/.*/url,commit id,author,date,comment,changed files,lines added,lines deleted/' history.csv
fi

# clean up
rm log.csv
rm log.csv.bak
rm history.csv.bak

0 comments on commit a2df606

Please sign in to comment.