Skip to content

Instantly share code, notes, and snippets.

@smudge
Last active March 2, 2020 20:23
Show Gist options
  • Save smudge/4a3100ea965b576ec19a468b68970ef6 to your computer and use it in GitHub Desktop.
Save smudge/4a3100ea965b576ec19a468b68970ef6 to your computer and use it in GitHub Desktop.
Count the number of models in a Rails app over time.
APP_DIR=.
DATE=0
echo "Date, Tier 1, Tier 2, Tier 3, Tier 4, Tier 5, Total" > model_counts.csv
for COMMIT in $(git log --first-parent --reverse --format=format:%H -- $APP_DIR); do
NEW_DATE=$(git show -s --format=%cs ${COMMIT})
if [[ "$DATE" == "$NEW_DATE" ]]; then
continue
fi
git checkout ${COMMIT} -f >/dev/null 2>&1;
DATE=$NEW_DATE
LAYER_1=$(find $APP_DIR/app/models/* -maxdepth 0 -type f ! -path '*/concerns/*' 2>/dev/null | wc -l)
LAYER_2=$(find $APP_DIR/app/models/**/* -maxdepth 0 -type f ! -path '*/concerns/*' 2>/dev/null | wc -l)
LAYER_3=$(find $APP_DIR/app/models/**/**/* -maxdepth 0 -type f ! -path '*/concerns/*' 2>/dev/null | wc -l)
LAYER_4=$(find $APP_DIR/app/models/**/**/**/* -maxdepth 0 -type f ! -path '*/concerns/*' 2>/dev/null | wc -l)
LAYER_5=$(find $APP_DIR/app/models/**/**/**/**/* -maxdepth 0 -type f ! -path '*/concerns/*' 2>/dev/null | wc -l)
TOTAL=$(find $APP_DIR/app/models -type f ! -path '*/concerns/*' 2>/dev/null | wc -l)
echo "$DATE, $LAYER_1, $LAYER_2, $LAYER_3, $LAYER_4, $LAYER_5, $TOTAL"
done >> model_counts.csv
@smudge
Copy link
Author

smudge commented Mar 2, 2020

Note to self: Even though this tries to only take 1 row per date, commits aren't sorted in date order, so I'll need to add a step to remove duplicates.

Edit: adding --first-parent seems to mostly resolve the issue -- it'll skip intermediary commits and favor the history of merges (and squash merges). This assumes that the history of a repo follows a relatively sane merge pattern 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment