New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an after commit hook for updating repo #12

Closed
urlsangel opened this Issue Apr 19, 2013 · 17 comments

Comments

Projects
None yet
10 participants
@urlsangel

urlsangel commented Apr 19, 2013

I've been setting Gollum up on a little Debian box here so it can be used as a shared wiki resource. I'd like it to be able to accept wiki amends through either the web interface (a single centrally served one, not a localhost instance each), or through individual markdown edits and git commits.

For the web service to keep in sync though, I need it to do a pull/push on commit - I understand that this might create merge conflicts if someone else is editing that page at the same time, but I think the risk of that is low at the moment, and I can live with it.

I've created this gist of how I've hacked it to work at the moment:

https://gist.github.com/urlsangel/5411081

I also need some way for the server to pull the latest whenever a new session is started.

I'm a Ruby noob so am feeling my way along in this - any thoughts on what the best way to do this would be?

@sunny

This comment has been minimized.

Member

sunny commented Apr 19, 2013

I think this could be handled outside of gollum by using git's own hooks? Have you tried adding a pull/push in .git/hooks/post-commit ?

@urlsangel

This comment has been minimized.

urlsangel commented Apr 19, 2013

That sounds like the way forward, however the post-commit hook is never fired (although works fine when run manually as www-data).

This is the contents of my .git/hooks/post-commit file (where /path/to is an actual path):

#!/bin/sh
GIT_DIR=/path/to/.git git pull
GIT_DIR=/path/to/.git git push

However, this never gets called - I've logged out to file from here, and it isn't that it fails, it just never seems to run after Gollum commits through the web interface.

The user is www-data, the file is executable, and I've added the following to sudoers:

www-data ALL=NOPASSWD: /path/to/.git/hooks/post-commit

I'm stumped - any help gratefully received.

@jamieoliver

This comment has been minimized.

Member

jamieoliver commented Apr 19, 2013

I don't think grit supports hooks (see mojombo/grit#118). Here are some options:

  • Support custom post commit hooks in gollum-lib as per this issue
  • Help us to move gollum-lib to rugged assuming libgit2/libgit2#964 is resolved
  • Set up a cronjob to sync your repos periodically
@urlsangel

This comment has been minimized.

urlsangel commented Apr 22, 2013

I'd like to help build this functionality in, although I'm a bit out of my depth coding wise.

How can I help?

@ghost ghost assigned jamieoliver Apr 22, 2013

@jamieoliver

This comment has been minimized.

Member

jamieoliver commented Apr 22, 2013

I think there are definitely some advantages to supporting this functionality directly in gollum-lib as opposed to whatever git library we are using, particularly since Gollum::Committer has this functionality already. Essentially we just need a way to register custom code such as your pull and push calls to the after_commit callbacks.

As an initial thought, this could be done in a similar way to how custom markups are registered (see gollum/gollum#637). Does anyone have a better suggestion?

@bootstraponline

This comment has been minimized.

Member

bootstraponline commented Apr 22, 2013

I think there are definitely some advantages to supporting this functionality directly in gollum-lib

I agree.

Does anyone have a better suggestion?

I think implementing it similar to markup extensions is a good idea.

@l3iggs

This comment has been minimized.

l3iggs commented May 21, 2013

I needed this functionality in gollum as well i.e. I wanted to push to a remote repo when changes were made to the wiki through the web interface.

I achieved this by adding a remote origin to the repository:
git remote add origin git@host:user/test.git

and then putting the following code into gollum/lib/gollum/app.rb:

after do
  wikip     = wiki_page('home')
  wiki      = wikip.wiki
  wiki.repo.git.push
end

So that changes get pushed whenever a "do" fires.

This seems to work fine even when there is no remote origin defined.

@schmunk42

This comment has been minimized.

schmunk42 commented Jun 4, 2013

+1 I'd also need this - or a decent tutorial for a gollum server setup :)

@jamieoliver

This comment has been minimized.

Member

jamieoliver commented Jun 6, 2013

This issue is next on my list. I'm hoping to get some time at the weekend to make a start...

jamieoliver pushed a commit to jamieoliver/gollum-lib that referenced this issue Aug 4, 2013

jamieoliver pushed a commit to jamieoliver/gollum-lib that referenced this issue Aug 4, 2013

jamieoliver pushed a commit to jamieoliver/gollum-lib that referenced this issue Aug 4, 2013

jamieoliver pushed a commit to jamieoliver/gollum-lib that referenced this issue Aug 4, 2013

jamieoliver pushed a commit to jamieoliver/gollum-lib that referenced this issue Aug 4, 2013

@jamieoliver

This comment has been minimized.

Member

jamieoliver commented Aug 4, 2013

Finally got some time to work on this. Please take a look at the PR and let me know your thoughts.

@bootstraponline

This comment has been minimized.

Member

bootstraponline commented Aug 4, 2013

It looks good to me.

jamieoliver pushed a commit to jamieoliver/gollum-lib that referenced this issue Aug 10, 2013

@jamieoliver

This comment has been minimized.

Member

jamieoliver commented Aug 10, 2013

Feature added in #35.

@jason-sge

This comment has been minimized.

jason-sge commented Aug 14, 2013

Forgive my ignorance, but how would one add the hooks to gollum's config.rb? Simply adding a block

Gollum::Hook.register(:post_commit, :hook_id) do |committer, sha1|
  #commands
end

results in config.rb:10:in <top (required)>': uninitialized constant Gollum::Hook (NameError)`

EDIT: I figured it out!

wiki = Gollum::Wiki.new(".")

Gollum::Hook.register(:post_commit, :hook_id) do |committer, sha1|
  wiki.repo.git.pull
  wiki.repo.git.push
end
@l3iggs

This comment has been minimized.

l3iggs commented Sep 21, 2013

@jason-sge I think it would be much better to do what you're trying to do by putting this into your config.rb file instead of what you have there:

Gollum::Hook.register(:post_commit, :hook_id) do |committer, sha1|
  committer.wiki.repo.git.pull
  committer.wiki.repo.git.push
end

since you have direct access to committer

By the way @jamieoliver this works like a charm, really awesome!

@MaxPleaner

This comment has been minimized.

MaxPleaner commented Oct 30, 2014

isnt it necessary to authenticate at some point?

@dkozel

This comment has been minimized.

dkozel commented Sep 1, 2015

The grit adapter has been updated. Here's the currently working hook with remote = origin and branch = master.

wiki = Gollum::Wiki.new(".")

Gollum::Hook.register(:post_commit, :hook_id) do |committer, sha1|
  wiki.repo.git.pull("origin", "master")
  wiki.repo.git.push("origin", "master")
end
@existme

This comment has been minimized.

existme commented Nov 12, 2018

Actually the more correct way of doing this might be:

Gollum::Hook.register(:post_commit, :hook_id) do |committer, sha1|
     committer.wiki.repo.git.pull('origin', committer.wiki.ref)
     committer.wiki.repo.git.push('origin', committer.wiki.ref)
end

So there would not be any need of creating an instance of wiki.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment