Brad Lucas

Programming, Clojure and other interests
June 17, 2017

Git Scripts - First Commit Authors

Overview

When you work in an organization with a large number of repoositories created over time there is sometimes a need to figure who created what. You want to know who was the author of each repo.

The following three scripts can help with this.

The first get-first-commit-log.sh will show you who was the first committer to a repo. You run it in the directory of a repo.

#!/bin/bash
# @see http://stackoverflow.com/a/5189296

# If the repo has no commits you'll get the following
# fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
# Use '--' to separate paths from revisions, like this:
# 'git <command> [<revision>...] -- [<file>...]'
#
# Using 2> /dev/null to ignore these

git log $(git rev-list --max-parents=0 HEAD 2> /dev/null) 2> /dev/null

This is perfect for a single repo. But, what about when you've cloned each of your organization's repos into a directory. Here, as I write this there are 194 directories in my work directory. To find out the authors of each I could use another script. The following will help. It is called get-all-local-authors.sh.

#!/bin/bash
(for f in `find . -maxdepth 1 -type d`;
 do
   pushd > /dev/null $f
   # only do something if there is a .git directory
    # do something only if there is a .git directory
    if [[ -d ".git" ]]
        then
            get-first-commit-log.sh | grep -m 1 Author: | cut -f2- -d' ' | awk -F'[<>]' '{print $2}'
   fi
   popd > /dev/null
 done) | sort --ignore-case | uniq

But this script produces all the unique repo authors for the organization. This would answer the question, who here has created a repo. How about who and which repo did they create.

For this, we can have a script called get-all-local-repos-authors.sh.

#!/bin/bash
echo ""
printf "| %-55s  | %-40s | \n" "-------------------------------------------------------" "----------------------------------------";
printf "| %-55s  | %-40s | \n" "Repo" "Author";
printf "| %-55s  | %-40s | \n" "-------------------------------------------------------" "----------------------------------------";

for d in `find . -name .git -type d`; do
    pushd $d/.. > /dev/null; 
    # do something only if there is a .git directory
    if [[ -d ".git" ]]
        then
            AUTHOR_EMAIL=`(get-repo-first-author.sh)`
            REPO_NAME=`basename ${PWD}`;
            printf "| %-55s  | %-40s | \n" $REPO_NAME $AUTHOR_EMAIL;
    fi
    popd > /dev/null; 
done
printf "| %-55s  | %-40s | \n" "-------------------------------------------------------" "----------------------------------------";
 
Tags: bash git