2
\$\begingroup\$

At work, my team hosts a daily standup, in which we discuss what we're working on, what we did the day before and any potential blockers for future development.

I thought as a fun little side project that I would write a script in bash to go through each directory in my /dev/ folder and get all commits from the previous day.

We use Git for the most part, but I do have one or two repositories still under a different source control system. This code is to mitigate that problem 2>> /dev/null;.

Here is the code:

gitUsername="userNameHere"
echo "Here are yesterday's commits:"

for dir in ./*/ ;
    do (cd "$dir"; git log --pretty=format:'%Cred%h%Creset -%Creset %s %Cgreen(%cD) %C(bold blue)<%an>%Creset' --since yesterday --author $gitUsername} 2>> /dev/null;);
done;

echo "That's everything." 

I have never written anything in bash before, so any suggestions would be much appreciated!

Also, just a side note in case anybody is interested, we don't read commits to measure workload, I am printing them more as a reminder of the things I was working on the day before.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

A much much much much better way of doing this is:

find . -type f -exec git --no-pager log --pretty=format:'%Cred%h%Creset -%Creset %s %Cgreen(%cD) %C(bold blue)<%an>%Creset' --since yesterday {} --author $gitUsername \;

And a cleaner way to write long statements it to add '\' to break into new lines.

find . -type f \
-exec git --no-pager log --pretty=format: \
'%Cred%h%Creset -%Creset %s %Cgreen(%cD) %C(bold blue)<%an>%Creset' \
--since yesterday {} \
--author "${gitUsername}" \;

This way, 7 lines are written as 1 and to stdout.

  • --no-pager returns everything to your terminal stdout instead your editor.
  • find . says which directory to search in, so a for loop may be useful to only search desired directories
  • Also, explicitly defining the desired dirs will remove the need to redirect to stderr and when useful errors are displayed, will be meaningful to you.

https://linux.die.net/man/1/find

https://www.golinuxcloud.com/find-exec-multiple-commands-examples-unix/

\$\endgroup\$
2
  • \$\begingroup\$ I like this approach, thanks! The only one I don't agree with is the third point. I want this to be dynamic as I clone or remove a repository maybe once every week or two, so therefore don't want to have to keep a list. \$\endgroup\$
    – Jessica
    Commented Jul 29, 2020 at 10:10
  • \$\begingroup\$ Then changing the find . directory to the larger directory which stores all of your git repositories will do what you desire. \$\endgroup\$
    – Arlion
    Commented Jul 29, 2020 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.