Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get a list of files with N commits from git

I am trying to get a list of files in a git repository that have only a single commit. So from the subject N would be 1.

I have a bash script which can perform the job. However, I would like to do this with just git commands. I have access to git filter-repo and I can't find a way in that documentation either.

This is part of an svn to git migration and it would be nice to get this information without the find since the first repository created does not have files until it is clone'd once.

The bash script that works is:

export OFS=$IFS; export IFS=$'\n'; total=0; for i in $(find . -type f -not -path "./.git/*"); do sum=`git log --graph --oneline --reflog --all --pretty='format:%C(auto)%h%d (%an, %ar)' $I | wc -l ; if [[ $sum -gt 0 ]]; then let "total++"; echo $i" "$sum; fi; done; echo "total files = $total"

The bash one-liner can be shortened but that isn't what I'm trying to figure out. I'm attempting to find a way to get similar information with git. Namely, I would like a programmatic git solution to get the list of files with N commits. In my use N is 1.

like image 856
jj2f1 Avatar asked Dec 07 '25 07:12

jj2f1


2 Answers

These build-with-just-one-tool conceits might make fun puzzles, but the fact is Git's one tool in a very capable toolbox.

git log --pretty= --name-status | awk -F$'\t' '!seen[$2]++ && $1=="A"'

will find all files that haven't been touched since being added.

like image 162
jthill Avatar answered Dec 08 '25 21:12

jthill


I doubt there is a pure Git solution to this exact problem. Git is designed to be used with Unix shell utilities. But we can do it easier.

Assuming you want only information about the current set of files (not those which have been deleted), use git ls-files to get a list of the current files known to Git (safer than using find). Then run each through git log --oneline.

for f in `git ls-files`; do
  echo "$f: $(git log --oneline $f | wc -l)";
done

To get a list of files which only appear in one commit, check when git log only returns one line.

for f in `git ls-files`; do
  if [ $(git log --oneline $f | wc -l) = "1" ]; then
    echo $f;
  fi;
done

You can make this a Git alias or an actual program by calling it git-whatever and putting it in your path.

$ cat ~/bin/git-files-with-one-commit 
#!/bin/bash

for f in `git ls-files`; do
  if [ $(git log --oneline $f | wc -l) = "1" ]; then
    echo $f;
  fi;
done

$ chmod +x ~/bin/git-files-with-one-commit 

$ git files-with-one-commit
...
like image 31
Schwern Avatar answered Dec 08 '25 23:12

Schwern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!