1

I have seen this answer and it is almost exactly what I need.

Difference is that will print date for human eyes, while I need to check it in a Makefile.

In particular I need to rebuild a certain file IFF something changed in repo after its creation.

I might not have a local copy of repo, so I might need a full "git clone" (possibly shallow), but I cannot directly use file dates because git does not preserve them.

I need something like: if myfile is_older $(git log -1 --format=%cd origin/master); then ... or, in Makefile:

myfile: $(somefunc $(shell git log -1 --format=%cd origin/master))
        commands to rebuild myfile

but I don't know how to write it.

ZioByte
  • 2,690
  • 1
  • 32
  • 68

2 Answers2

1

As pointed out by @Slavomir, a key ingredient will be the --date option, but not with the raw value but rather the unix value (in order to only get the number of seconds from the epoch in UTC, without the timezone).

$ git log -1 --format=%cd --date=unix
1582030040

(cf. doc).

Then, you'll be able to use the date +%s -r myfile shell command to get the timestamp of myfile.

(cf. man page).

Finally, you can devise a Makefile function to ease the comparison, which leads to a minimal working example like the following one:

Makefile

# Return 'true' if $(1) does not exist or is older than branch $(2)
check_repo_change = $(shell if [ ! -e $(1) ] || [ $$(date +%s -r $(1)) -lt \
  $$(git log -1 --format=%cd --date=unix $(2)) ]; then echo true; fi)

# Note that you'll probably need to run "git fetch" at some point
BRANCH := origin/master

all: myfile
.PHONY: all

ifeq '$(call check_repo_change,myfile,$(BRANCH))' 'true'
myfile:
    set -x; echo ok >> $@
else
myfile:
    $(info No change detected in branch $(BRANCH))
endif
.PHONY: myfile

Note: the .PHONY option is mandatory so that the myfile target will be triggered even if the file already exists and doesn't have (more recent) dependencies.

ErikMD
  • 13,377
  • 3
  • 35
  • 71
0

If you need to change the date format, try to add --date option.

For example:

  • rfc

    git log -1 --format=%cd --date=rfc
    Wed, 5 Feb 2020 16:34:38 +0200
    
  • relative

    git log -1 --format=%cd --date=relative
    13 days ago
    
  • iso-strict

    git log -1 --format=%cd --date=iso-strict
    2020-02-05T16:34:38+02:00
    
  • short

    log -1 --format=%cd --date=short
    2020-02-05
    
  • raw

    git log -1 --format=%cd --date=raw
    1580913278 +0200
    

For more documentation, look into: https://git-scm.com/docs/git-log

Maybe I would use "raw". Is this date formatting convenient for your script?

Slavomir
  • 351
  • 2
  • 8