R.  Barbus
R. Barbus

Reputation: 93

Git log, input from another command

I have an assignment to count all the changes from all the files from a git open-source project. I know that if I use:

git log --pretty=oneline <filename> | wc  -l

I will get the number of changes of that file ( I use git bash on Windows 10)

My idea is to use

find .

and to redirect the output to the git command. How can I do the redirecting? I tried :

$ find . > git log --pretty=online | wc -l
0
find: unknown predicate `--pretty=online'

and

$ find . | git log --pretty=online | wc -l
fatal: invalid --pretty format: online
0

Upvotes: 2

Views: 372

Answers (2)

jthill
jthill

Reputation: 60255

You can do much better than that,

git log --pretty='' --name-only | sort | uniq -c

That's "show only the names of files changed in each commit, no other metadata, sort that list so uniq can easily count the occurrences of each'

Upvotes: 2

Stephen Newell
Stephen Newell

Reputation: 7828

You'll need to loop over the results of find.

find -type f | grep -v '^\./\.git' |
while read f; do
    count=$(git log --oneline ${f} | wc -l)
    echo "${f} - ${count}"
done | grep -v ' 0$'

Your find is okay, but I'd restrict it to just files (git doesn't track directories explicitly) and remove the .git folder (we don't care about those files). Pipe that into a loop (I'm using a while), and then your git log command works just fine. Lastly, I'm going to strip anything with a count of 0, since I may have files that are part of .gitignore I don't want to show up (e.g., things in __pycache__).

Upvotes: 0

Related Questions