3

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?

Community
  • 1
  • 1
qwertymk
  • 34,200
  • 28
  • 121
  • 184
  • If you're simply trying to reminify files, maybe you should record the modification timestamps when you minify (you could keep a list in a dotfile) and just consult that file to see what needs reminifying. – Lily Ballard Feb 21 '12 at 01:55
  • BTW, your output from `git whatchanged -n 1 --pretty=format:` is basically doing the same thing as `git diff-tree HEAD^ HEAD`, so maybe you should just use that, especially since you can do things like pass the `--name-only` flag. – Lily Ballard Feb 21 '12 at 01:56
  • possible duplicate of [How do I list all the files for a commit in git](http://stackoverflow.com/questions/424071/how-do-i-list-all-the-files-for-a-commit-in-git) – Schwern Feb 21 '12 at 02:01
  • @Schwern: not a dupe, check the comments on the answer – qwertymk Feb 21 '12 at 02:03
  • @qwertymk The comments on the answer are "Thank that works perfectly" and "Thank you. Just perfect." What am I missing? Do you just need a `grep '\.js$'`? – Schwern Feb 21 '12 at 02:05

2 Answers2

3

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$'
Community
  • 1
  • 1
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • `git whatchanged -n 1 --pretty=format: --name-only | grep '\.js$'` – qwertymk Feb 21 '12 at 02:13
  • @qwertymk Yeah, that works better. It'll pick the last commit in `git log` order otherwise `HEAD^` might go down the wrong side of a merge. BTW I misunderstood which answer you were referring to above, sorry. – Schwern Feb 21 '12 at 02:16
1

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.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • `diff-tree` returns the directories changed, not the files. At least not according to my tests. – Schwern Feb 21 '12 at 01:59
  • I think you need a `-r` to `diff-tree`. – Schwern Feb 21 '12 at 02:04
  • It also outputs the commit ID which could be accidentally be taken as a filename by the script. You'd need to strip the first line with a `tail -n +2` which is a GNUism or put together something with `sed`. (Sorry for the stream of corrections, I'm researching this myself) – Schwern Feb 21 '12 at 02:09
  • @Schwern: Ah hrm, in my test repo the commit only touched top-level files, so I forgot about the recursion bit. In any case, with `-r` it gives precisely the right output. I see no commit ID information in the output of `git diff-tree`, nor should there be since it's just diffing trees. – Lily Ballard Feb 21 '12 at 03:34
  • @Schwern: Extra "ah hrm": It does output a commit ID if you simply say `git diff-tree -r --name-only HEAD` (i.e. only provide one tree-ish). If you provide two, it doesn't. – Lily Ballard Feb 21 '12 at 03:35
  • @KevinBallard yes, command should be `git diff-tree -r --name-only HEAD^..HEAD` – thebugfinder Mar 26 '15 at 04:40