qwertymk
qwertymk

Reputation: 35344

Bash script to extract filenames from git whatchanged

I'm trying to get a list of all the js files that changed to know what to reminify.

I previously asked this question

So far this is the best I came up with but it feels really unsafe.

GITCHANGES=$(git whatchanged -n 1 --pretty=format:)
I=0;
for f in $GITCHANGES;
do
    I=$(($I + 1));
    if [[ $(($I % 6 )) == 0 ]]; then
        echo "$f"
    fi
done

But this gives me all the files that changed (php css js) and not just the js files

How would I get just the js files? Also is there a better way to accomplish this?

Upvotes: 3

Views: 3223

Answers (2)

Schwern
Schwern

Reputation: 165606

From this answer, use git show --pretty="format:" --name-only HEAD^ to get a list of changed files. Then pipe it through grep.

git show --pretty="format:" --name-only HEAD^ | grep '\.js$'

Upvotes: 3

Lily Ballard
Lily Ballard

Reputation: 185871

Your script can be condensed really simply into

git diff-tree --name-only HEAD^ HEAD | grep '\.js$'

This will spit out a list of all .js files that differ between HEAD^ (first parent) and HEAD.

Upvotes: 1

Related Questions