timvink / mkdocs-git-revision-date-localized-plugin

MkDocs plugin to add a last updated date to your site pages
https://timvink.github.io/mkdocs-git-revision-date-localized-plugin/index.html
MIT License
193 stars 39 forks source link

Errors when trying to use with mkdocs-material #92

Closed EDIflyer closed 2 years ago

EDIflyer commented 2 years ago

Thanks for creating this plugin, it looks great! I'm running mkdocs-material in Docker but unfortunately I'm not having any success running the plugin so far.

I saw in the documentation that it was built-in to mkdocs-material, however when I tried to add This page was last updated: *{{ git_revision_date_localized }}* text to a page it didn't get parsed and just came through as-is.

I then tried to add

plugins:
  - search
  - git-revision-date-localized

to my mkdocs.yml, but this led to an ERROR - Config value: 'plugins'. Error: The "git-revision-date-localized" plugin is not installed error message.

I then created a new Dockerfile with pip install mkdocs-git-revision-date-localized-plugin included in the build and tried again but this time got the following error:

today at 12:42:30Container started
today at 12:34:46   git config --global --add safe.directory /docs'
today at 12:41:59INFO     -  Building documentation...
today at 12:41:59WARNING  -  Config value: 'dev_addr'. Warning: The use of the IP address '0.0.0.0' suggests a production environment or the use of a proxy to connect to the MkDocs server. However, the MkDocs' server is intended for local development purposes only. Please use a third party production-ready server instead.
today at 12:41:59ERROR    -  [git-revision-date-localized-plugin] Unable to read git logs of '/docs/docs'.  To ignore this error, set option 'fallback_to_build_date: true'
today at 12:41:59Traceback (most recent call last):
today at 12:41:59  File "/usr/local/bin/mkdocs", line 8, in <module>
today at 12:41:59    sys.exit(cli())
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
today at 12:41:59    return self.main(*args, **kwargs)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1055, in main
today at 12:41:59    rv = self.invoke(ctx)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1657, in invoke
today at 12:41:59    return _process_result(sub_ctx.command.invoke(sub_ctx))
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
today at 12:41:59    return ctx.invoke(self.callback, **ctx.params)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 760, in invoke
today at 12:41:59    return __callback(*args, **kwargs)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/mkdocs/__main__.py", line 181, in serve_command
today at 12:41:59    serve.serve(dev_addr=dev_addr, livereload=livereload, watch=watch, **kwargs)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/mkdocs/commands/serve.py", line 63, in serve
today at 12:41:59    config = builder()
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/mkdocs/commands/serve.py", line 58, in builder
today at 12:41:59    build(config, live_server=live_server, dirty=dirty)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/mkdocs/commands/build.py", line 257, in build
today at 12:41:59    config = config['plugins'].run_event('config', config)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/mkdocs/plugins.py", line 102, in run_event
today at 12:41:59    result = method(item, **kwargs)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/mkdocs_git_revision_date_localized_plugin/plugin.py", line 70, in on_config
today at 12:41:59    self.last_site_revision_timestamp = self.util.get_git_commit_timestamp(
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/mkdocs_git_revision_date_localized_plugin/util.py", line 154, in get_git_commit_timestamp
today at 12:41:59    raise err
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/mkdocs_git_revision_date_localized_plugin/util.py", line 124, in get_git_commit_timestamp
today at 12:41:59    commit_timestamp = git.log(
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/git/cmd.py", line 639, in <lambda>
today at 12:41:59    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/git/cmd.py", line 1184, in _call_process
today at 12:41:59    return self.execute(call, **exec_kwargs)
today at 12:41:59  File "/usr/local/lib/python3.9/site-packages/git/cmd.py", line 984, in execute
today at 12:41:59    raise GitCommandError(redacted_command, status, stderr_value, stdout_value)
today at 12:41:59git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
today at 12:41:59  cmdline: git log --date=unix --format=%at -n 1 --no-show-signature /docs/docs
today at 12:41:59  stderr: 'fatal: unsafe repository ('/docs' is owned by someone else)
today at 12:41:59To add an exception for this directory, call:
today at 12:41:59
today at 12:41:59   git config --global --add safe.directory /docs'
today at 12:42:31Container stopped

My markdown files are stored in a docs subdirectory of my git repository and everything builds fine if I don't enable the plugin, so as far as I can tell I've got things mapped OK?

whentojump commented 2 years ago

I am having similar issues. Looks like the plugin wasn't playing well with newer versions of Git inside the container.

You may want to try the following:

timvink commented 2 years ago

Interesting. Thanks for the report & the research.

Does the suggestion git config --global --add safe.directory /docs' fix the problem?

If so, I could ask mkdocs-material to update the docker image, and on my end update the installation instructions when used within docker images.

EDIflyer commented 2 years ago

Thanks both - @timvink am I correct in thinking that this command would be something I need to run within the Docker container?

timvink commented 2 years ago

I am able to reproduce the error:

mkdir testmkdocsdocker
cd testmkdocsdocker
mkdocs new .
# enable mkdocs-material theme and this plugin in mkdocs.yml

Then use the docker image by creating a Dockerfile

FROM squidfunk/mkdocs-material
RUN pip install mkdocs-git-revision-date-localized-plugin

And run docker build -t squidfunk/mkdocs-material . to build it locally.

Then

docker run --rm -it -p 8000:8000 -v ${PWD}:/docs squidfunk/mkdocs-material

Gives me:

  stderr: 'fatal: unsafe repository ('/docs' is owned by someone else)
To add an exception for this directory, call:

        git config --global --add safe.directory /docs'

To solve this, I changed the Dockerfile to:

FROM squidfunk/mkdocs-material
RUN pip install mkdocs-git-revision-date-localized-plugin
RUN git config --global --add safe.directory /docs

Rebuilt it (docker build -t squidfunk/mkdocs-material .), and ran it again, now with success!

So, to close this issue, I will do two things:

1) Provide a PR to mkdocs-material to update the docs (because this applies to all git-based plugins) 2) Update the setup instructions of this plugin.

EDIflyer commented 2 years ago

Thanks, @timvink - your errors seem to be slightly different to the ones I was getting though?

today at 12:34:46   git config --global --add safe.directory /docs'
today at 12:41:59INFO     -  Building documentation...
today at 12:41:59WARNING  -  Config value: 'dev_addr'. Warning: The use of the IP address '0.0.0.0' suggests a production environment or the use of a proxy to connect to the MkDocs server. However, the MkDocs' server is intended for local development purposes only. Please use a third party production-ready server instead.
today at 12:41:59ERROR    -  [git-revision-date-localized-plugin] Unable to read git logs of '/docs/docs'.  To ignore this error, set option 'fallback_to_build_date: true'

I've realised mine already includes the 'add safe directory' bit - although is that ' at the end maybe an issue? Also mind refers to /docs/docs?

Will there be a way for mkdocs-materials to be updated so it doesn't require a separate Dockerfile or was the intention always that you would need to create a custom Dockerfile with the pip install command?

Thanks 😃 (and appreciate I may be totally off base as have only been learning Docker for the past week!)

EDIT: I see suggestion no 2 in the issue you just opened on mkdocs-material deals with the Dockerfile issue - you're way ahead of me!!

timvink commented 2 years ago

your errors seem to be slightly different to the ones

Nope, same error, I just copied the last bit for brevity.

I've realised mine already includes the 'add safe directory' bit - although is that ' at the end maybe an issue? Also mind refers to /docs/docs?

Yeah the error message include the trailing ' which is wrong. Try updating your Dockerfile with the example I gave.

Will there be a way for mkdocs-materials to be updated so it doesn't require a separate Dockerfile

You'll always have to install your packages separately (to keep the mkdocs-material image as small as possible). Hopefully we can solve this setup problem in the base image though.

whentojump commented 2 years ago

@EDIflyer I think your log in the OP includes two separate runs and somehow they are not in chronological order and get mixed. Please note the timestamp.

I've realised mine already includes the 'add safe directory' bit

Indeed it hasn't. The line today at 12:34:46 git config --global --add safe.directory /docs' is actually the very end part of your previous run. Git was suggesting you issue the command, which means, you haven't yet done so.

As for the ' I don't think it's the problem. They are misleading but correctly in pair.

                    the opening one
                           |
today at 12:41:59  stderr: 'fatal: unsafe repository ('/docs' is owned by someone else)
today at 12:41:59To add an exception for this directory, call:
today at 12:41:59
today at 12:41:59   git config --global --add safe.directory /docs'
                                                                      |
                                                               the closing one
EDIflyer commented 2 years ago

Thanks @timvink so I rebuilt the Dockerfile as suggested and indeed no errors now 👍

I wonder if the documentation needs updated to clarify how the plugin is supported in mkdocs-material as when I first read this I hadn't realised I needed to do a new Dockerfile: image

My next issue is to try and work out why it's reporting a date from a couple of days ago rather than today's date when using git_revision_date_localized! image

Will also have a play about with options now for getting non-US date format and automatically including on pages but at least seems to be up and running now 😃

timvink commented 2 years ago

https://github.com/squidfunk/mkdocs-material/pull/4118 will address this issue when accepted, so closing this issue.

I wonder if the documentation needs updated to clarify how the plugin is supported in mkdocs-material

Fair point, you might want to open an issue with mkdocs-material.

My next issue is to try and work out why it's reporting a date from a couple of days ago rather than today's date

Remember this plugin does reporting the last file edit date, rather the last file commit date. Did you make a commit for today's changes?

Will also have a play about with options

Have fun :)

EDIflyer commented 2 years ago

Thanks, @timvink - it was this bit on your documentation at https://timvink.github.io/mkdocs-git-revision-date-localized-plugin/ that I thought might be worth tweaking? image

I appreciate it was my own fault for not understanding, but I read it at first that the plugin was supported by mkdocs-material, I then thought I should try to activate it in plugins without realising it needing installed too. Would it be worth changing the text as follows (bold bit):

The mkdocs-material theme has built in support for git-revision-date-localized and you should already see the last revision date on the bottom of your pages once you have activated the plugin as noted above.

Also for the install bit above would it be worth adding a comment with that Dockerfile you helpfully provided above?

Totally appreciate in retrospect how obvious this probably is, but when I came fresh to it a couple of days ago I hadn't quite appreciate the process!

timvink commented 2 years ago

Totally appreciate in retrospect how obvious this probably is, but when I came fresh to it a couple of days ago I hadn't quite appreciate the process!

Yeah it's hard to write documentation trying to image how to come to this fresh. If you have any improvements to the docs of this plugin I'd appreciate a PR!

EDIflyer commented 2 years ago

PR #94 created - sorry also stupidly hadn't realised the docs were in the repo - got too used to seeing the edit icon in mkdocs-material 😂 Also added a bonus PR #95 re locale.