开发者

List the files I own in subversion

开发者 https://www.devze.com 2023-01-05 16:02 出处:网络
So I have a bit of an issue. I work for a small startup (about 8 developers) and my boss recently decided that we need to put the owner of each file in the documentation. So I have been try to write s

So I have a bit of an issue. I work for a small startup (about 8 developers) and my boss recently decided that we need to put the owner of each file in the documentation. So I have been try to write something using svn blame and file to loop through every php file and see which files have my username on more that 15 lines, but I haven't been able to get it quite right.

What I would really like is a one-liner (or simple bash script) that will list every file in a subversion repository and the username that last edited the 开发者_StackOverflowmajority of the lines. Any ideas?


Alright, this is what I came up with:

#!/bin/bash

set -e

for file in `svn ls -R`; do
  if [ -f $file ]; then
    owner=`svn blame $file | tr -s " " " " | cut -d" " -f3 | sort | uniq -c | sort -nr | head -1 | tr -s " " " " | cut -d" " -f3`
    if [ $owner ]; then
      echo $file $owner
    fi
  fi
done

It uses svn ls to determine each file in the repository, then for each file, svn blame output is examined:

  • tr -s " " " " squeezes multiple spaces into one space
  • cut -d" " -f3 gets the third space-delimited field, which is the username
  • sort sorts the output so all lines last edited by one user are together
  • uniq -c gets all unique lines and outputs the count of how many times each line appeared
  • sort -nr sorts numerically, in reverse order (so that the username that appeared most is sorted first)
  • head -1 returns the first line
  • tr -s " " " " | cut -d" " -f3 same as before, squeezes spaces and returns the third fieldname which is user.

It'll take a while to run but at the end you'll have a list of <filename> <most prevalent author>

Caveats:

  • Error checking is not done to make sure the script is called from within an SVN working copy
  • If called from deeper than the root of a WC, only files at that level and deeper will be considered
  • As mentioned in the comments, you might want to take revision date into account (if the majority of checkins happened 10 years ago, you might want to discount them determining the owner)
  • Any working copy changes that aren't checked in won't be taken into effect


for f in $(find . -name .svn -prune -o -type f); do
   echo $f $(svn blame $f | awk '{ print $2 }' | sort | uniq -c | sort -nr | head -n 1  | cut -f 1)
done
0

精彩评论

暂无评论...
验证码 换一张
取 消