简体   繁体   中英

Pass “file name” from a text file to a command line where each line of a file is file name

I'm running the following code

git log --pretty=format: --numstat -- SOMEFILENAME |
   perl -ane '$i += ($F[0]-$F[1]); END{print "changed: $i\n"}' \
      >> random.txt

What this does is it takes a file with a name "SOMEFILENAME" and saves the sum of the total amount of added and removed lines to a textfile called "random.txt"

I need to run this program on every file in repository and there are looots of them. What would be an easy way to do this?

If you want a total per file:

git log --pretty=format: --numstat |
   perl -ane'
      $c{$F[2]} += $F[0]-$F[1] if $F[2];
      END { print "$_\t$c{$_}\n" for sort keys %c }
   ' >random.txt

If you want a single total:

git log --pretty=format: --numstat |
   perl -ane'
      $c += $F[0]-$F[1];
      END { print "$c\n" }
   ' >random.txt

Their respective outputs are:

.gitignore      22
Build.PL        48
CHANGES.txt     0
Changes 25
LICENSE 132
LICENSE.txt     0
MANIFEST        18
MANIFEST.SKIP   9
README.txt      67
TODO.txt        1
lib/feature/qw_comments.pm      129
lib/feature/qw_comments.xs      250
t/00_load.t     13
t/01_basic.t    85
t/02_pragma.t   56
t/03_line_numbers.t     37
t/04_errors.t   177
t/05-unicode.t  39
t/devel-pod-coverage.t  26
t/pod.t 17

and

1151

Rather than use find , you can just let git give you all the files by using the name . (representing the current directory). With that, here's a version using awk that prints out stats per file:

git log --pretty=format: --numstat -- . |
awk '
    NF == 3 {changed[$3] += $1 - $2}
    END { for (name in changed) { printf("%s: %d changed\n", name, changed[name]); } }
'

And an even shorter one that prints a single overall changed line:

git log --pretty=format: --numstat -- . |
awk '
    NF == 3 {changed += $1 - $2}
    END { printf("%d changed\n", changed); }
'

(The NF == 3 is to account for the fact that git seems to print spurious blank lines in its output. I didn't try to figure out if there's a better git command.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM