Skip to content

Instantly share code, notes, and snippets.

@davidegironi
Last active January 27, 2021 19:45
Show Gist options
  • Save davidegironi/80b6569f077314f3e6768caff4e93d78 to your computer and use it in GitHub Desktop.
Save davidegironi/80b6569f077314f3e6768caff4e93d78 to your computer and use it in GitHub Desktop.
Script to collect git stats and save to mysql
# Git Stats
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
# Save git stats to mysql
#!/bin/sh
# Git Stats
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
#save all commits by author to csv
#find all folders with git
find . -name .git -type d -prune -execdir pwd \; > repositories.txt
#csv header
echo "\"repository\",\"author\",\"commits\",\"period\""
#iterate repositories
input="repositories.txt"
while IFS= read -r line
do
cd $line
REPOSITORYNAME=`basename "$PWD"`
git shortlog -s -n --all --no-merges | awk -v repositoryname=$REPOSITORYNAME '{print "\042"repositoryname"\042,\042"$2"\042,\042"$1"\042,\042all\042"}'
done < "$input"
#!/bin/sh
# Git Stats
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
#save all last month commits by author to csv
#find all folders with git
find . -name .git -type d -prune -execdir pwd \; > repositories.txt
#csv header
echo "\"repository\",\"author\",\"commits\",\"period\""
#iterate repositories
input="repositories.txt"
while IFS= read -r line
do
cd $line
REPOSITORYNAME=`basename "$PWD"`
git shortlog --since="1 month ago" -s -n --all --no-merges | awk -v repositoryname=$REPOSITORYNAME '{print "\042"repositoryname"\042,\042"$2"\042,\042"$1"\042,\042lastmonth\042"}'
done < "$input"
CREATE DATABASE IF NOT EXISTS gitstats;
USE gitstats;
DROP TABLE IF EXISTS linescounter;
CREATE TABLE linescounter (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
repository VARCHAR(1000) NOT NULL,
author VARCHAR(1000) NOT NULL,
linesadded INTEGER NOT NULL,
linesremoved INTEGER NOT NULL,
period VARCHAR(50) NOT NULL
);
DROP TABLE IF EXISTS commitscounter;
CREATE TABLE commitscounter (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
repository VARCHAR(1000) NOT NULL,
author VARCHAR(1000) NOT NULL,
period VARCHAR(50) NOT NULL,
commits INTEGER NOT NULL
);
SET GLOBAL local_infile=1;
#!/bin/bash
# Git Stats
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
if [ -z "$DB_USERNAME" ]; then
echo "No DB_USERNAME supplied"
exit
fi
if [ -z "$DB_PASSWORD" ]; then
echo "No DB_PASSWORD supplied"
exit
fi
./gitstats_linescounterall.sh > gslinescounterall.csv
./gitstats_linescounterlastmonth.sh > gslinescounterlastmonth.csv
./gitstats_commitscounterall.sh > gscommitscounterall.csv
./gitstats_commitscounterlastmonth.sh > gscommitscounterlastmonth.csv
mysql -u$DB_USERNAME -p$DB_PASSWORD gitstats -e "TRUNCATE TABLE commitscounter"
mysql -u$DB_USERNAME -p$DB_PASSWORD --local-infile gitstats -e "LOAD DATA LOCAL INFILE 'gscommitscounterall.csv' INTO TABLE commitscounter FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES (repository,author,commits,period) SET id=null"
mysql -u$DB_USERNAME -p$DB_PASSWORD --local-infile gitstats -e "LOAD DATA LOCAL INFILE 'gscommitscounterlastmonth.csv' INTO TABLE commitscounter FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES (repository,author,commits,period) SET id=null"
mysql -u$DB_USERNAME -p$DB_PASSWORD gitstats -e "TRUNCATE TABLE linescounter"
mysql -u$DB_USERNAME -p$DB_PASSWORD --local-infile gitstats -e "LOAD DATA LOCAL INFILE 'gslinescounterall.csv' INTO TABLE linescounter FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES (repository,author,linesadded,linesremoved,period) SET id=null"
mysql -u$DB_USERNAME -p$DB_PASSWORD --local-infile gitstats -e "LOAD DATA LOCAL INFILE 'gslinescounterlastmonth.csv' INTO TABLE linescounter FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES (repository,author,linesadded,linesremoved,period) SET id=null"
#!/bin/sh
# Git Stats
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
#save all lines count by author to csv
#find all folders with git
find . -name .git -type d -prune -execdir pwd \; > repositories.txt
#csv header
echo "\"repository\",\"author\",\"linesadded\",\"linesremoved\",\"period\""
#iterate repositories
input="repositories.txt"
while IFS= read -r line
do
cd $line
REPOSITORYNAME=`basename "$PWD"`
git log --numstat --all --pretty="%ae %H" | sed 's/@.*//g' | awk -v repositoryname=$REPOSITORYNAME '{ if (NF == 1){ name = $1}; if(NF == 3) {plus[name] += $1; minus[name] += $2}} END { for (name in plus) {print "\042"repositoryname"\042,\042"name"\042,\042+"plus[name]"\042,\042-"minus[name]"\042,\042all\042"}}' | sort -k2 -gr
done < "$input"
#!/bin/sh
# Git Stats
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
#save all last month lines count by author to csv
#find all folders with git
find . -name .git -type d -prune -execdir pwd \; > repositories.txt
#csv header
echo "\"repository\",\"author\",\"linesadded\",\"linesremoved\",\"period\""
#iterate repositories
input="repositories.txt"
while IFS= read -r line
do
cd $line
REPOSITORYNAME=`basename "$PWD"`
git log --since="1 month ago" --numstat --all --pretty="%ae %H" | sed 's/@.*//g' | awk -v repositoryname=$REPOSITORYNAME '{ if (NF == 1){ name = $1}; if(NF == 3) {plus[name] += $1; minus[name] += $2}} END { for (name in plus) {print "\042"repositoryname"\042,\042"name"\042,\042+"plus[name]"\042,\042-"minus[name]"\042,\042lastmonth\042"}}' | sort -k2 -gr
done < "$input"
#!/bin/bash
# Git Stats
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
#save your git stats to mysql
#gitstats_createdatabase.sql contains the database schema
# mysql -u DB_USERNAME -p -e "CREATE DATABASE IF NOT EXISTS gitstats"
# mysql -u DB_USERNAME -p gitstats < gitstats_createdatabase.sql
#enable mysql LOCAL capability
# mysql -u DB_USERNAME -p -e "SET GLOBAL local_infile=1"
#disable git renames for bit git
# git config --global diff.renames 0
#set your mysql credentials here
export DB_USERNAME=mysqlusername
export DB_PASSWORD=mysqlpassword
./gitstats_importtomysql.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment