2

I need to generate a list of all the images and css files with the modified dates, from a commit, for example.

git ls-tree -r --name-only HEAD | while read filename; do
  echo "$(git log -1 --format="%ad" -- $filename) $filename"
done

How would I modify this to just return jpg, gif, png, and css files?

Kari
  • 121

3 Answers3

0

I would recommend git diff HEAD --name-only <space separated expressions> in your scenario. Like so in the following two examples:

# return any changes to .css and .C files
git diff HEAD --name-only '*.css' '*.C' | while read filename; do
    echo "$(git log -1 --format="%ad" -- $filename) $filename"
done

Mon Mar 25 20:12:34 2024 -0600 framework/doc/content/css/moose.css Wed Jun 28 13:31:25 2023 -0600 framework/src/geomsearch/NearestNodeLocator.C

return any changes to only .css files

git diff HEAD --name-only '*.css' | while read filename; do echo "$(git log -1 --format="%ad" -- $filename) $filename" done

Mon Mar 25 20:12:34 2024 -0600 framework/doc/content/css/moose.css

Add as many as you need (*.png *.jpg *.jpeg etc)

Edit: Perhaps back in the day git did not support this functionality as it does now.

git version 2.43.5
0

Oh I see @Marco - thanks. It didn't occur to me I could toss a grep into the mix, for example this slight modification worked well.

--name-only HEAD | grep ".css" | while...
Kari
  • 121
0

Since you need a list of all images and css files, ideally, you should pipe your output to something like:

grep '.gif$\|.jpg$\|.png$\|.css$'

Of course you could add more relevant extensions your project might contain, such as .bmp, for instance.

If I try ls |grep -i '.gif$\|.jpg$\|.png$\|.css$' I get a list of all files in current folder with given extensions.

Let's take a closer look at this command:

$ ensures you won't match any undesired file, such as .css.bak. $ matches end of line.

\| allows you to specify multiple patterns, so you don't need to invoke multiple git ls-tree to get the whole list.

Should your repo contain any files with uppercase extensions you might want to add -i in order to run grep case insensitively.

Patterns between single ' ensures dots are interpreted as dots, and not as "any character" as meant in regexes.

Marco
  • 1,864