Rex Butler
Rex Butler

Reputation: 1060

git: list all files with owner/identity at first commit (or rather, the first user to commit the file)

How do I, using git, list all files in a particular directory together with the owner/identity of those files at first commit?

Is getting slices of information like this across many files usually difficult?

Edit: Okay, git doesn't provide a direct way to do this, but it does store who commits various files, right? I need this list in a particular directory so I can get a sense of which files I'm 'responsible' for.

Upvotes: 10

Views: 6697

Answers (5)

holygeek
holygeek

Reputation: 16185

Give this a try:

$ cd thatdirectory
$ git ls-files |
  while read fname; do
    echo "`git log --reverse --format="%cn" "$fname" | head -1` first added $fname"
  done

The "first added" can be misleading in case of renames.

Refs:

Upvotes: 17

sehe
sehe

Reputation: 392893

A very straightforward approach would be

git rev-list --objects --all |
    cut -d' ' -f2- |
    sort -u |
    while read name; do 
         git --work-tree=. log --reverse --format="%cn%x09$name" -- "$name" | head -n1
    done

Caveats:

  • This shows the first author name (%an) of each path that exists in the object database (not just in (any) current revision). You may also want the committer name (%cn), though be aware that if person B rebased a commit from person A that created the file, B will be the committer and A will be the author.
  • The --all flag signifies that you want all objects on all branches. To limit scope, replace it by the name of the branch/tag or just by HEAD

  • n2 performance (doesn't scale well for very large repo's)

  • improper output if the pathname contains formatting sequences (e.g. %H etc.)

It will start out with the empty name, which is the root tree object.

Upvotes: 4

jthill
jthill

Reputation: 60255

To do this efficiently,

git log --raw --date-order --reverse --diff-filter=A --format=%H%x09%an \
| awk -F$'\t' '
       /^[^:]/   {thisauthor=$2}
       $1~/A$/   {print thisauthor "\t" $2}
'

with maybe a |sort -t$'\t' -k1,1 or something to make it a bit prettier

Upvotes: 1

pinxue
pinxue

Reputation: 1746

I happened to run into a similar situation. The accepted answer is working, but why don't you guys use find with working copy?

find . -type f -exec git log --reverse --format="{} %cn" -1 {} \;

Upvotes: 6

Andy
Andy

Reputation: 46334

Using git itself, this is not possible. Git does not keep track of the owner of the file.

Upvotes: 0

Related Questions