diff --git a/lib/gollum/app.rb b/lib/gollum/app.rb index 6c03fcc5..ae477226 100644 --- a/lib/gollum/app.rb +++ b/lib/gollum/app.rb @@ -176,6 +176,7 @@ module Precious @page = page @page.version = wiki.repo.log(wiki.ref, @page.path).first @content = page.text_data + @etag = page.sha mustache :edit else redirect_to("/create/#{encodeURIComponent(@name)}") @@ -278,11 +279,18 @@ module Precious end post '/edit/*' do + etag = params[:etag] path = "/#{clean_url(sanitize_empty_params(params[:path]))}" page_name = CGI.unescape(params[:page]) wiki = wiki_new page = wiki.paged(page_name, path, exact = true) + return if page.nil? + if etag != page.sha + # Signal edit collision and return the page's most recent version + halt 412, {etag: page.sha, text_data: page.text_data}.to_json + end + committer = Gollum::Committer.new(wiki, commit_message) commit = { :committer => committer } @@ -292,7 +300,6 @@ module Precious update_wiki_page(wiki, page.sidebar, params[:sidebar], commit) if params[:sidebar] committer.commit - redirect to("/#{page.escaped_url_path}") unless page.nil? end diff --git a/lib/gollum/public/gollum/javascript/gollum.js.erb b/lib/gollum/public/gollum/javascript/gollum.js.erb index 4d17c20a..e87278a7 100755 --- a/lib/gollum/public/gollum/javascript/gollum.js.erb +++ b/lib/gollum/public/gollum/javascript/gollum.js.erb @@ -367,6 +367,34 @@ $(document).ready(function() { window.onbeforeunload = function(){ return "Leaving will discard all edits!" }; }); $.GollumEditor(); + $("#gollum-editor-submit").click( function(e) { + e.preventDefault(); + // Prevent button from being clicked again + $(this).attr('disabled', true); + + var formData = new FormData($('#gollum-editor-form').get(0)); + var endpoint = $('#gollum-editor-form').attr("action"); + + $.ajax({ + url: endpoint, + type: 'POST', + data: formData, + processData: false, + contentType: false, + success: function(data) { + window.location = window.location.href.replace(/gollum\/edit\//, '') + }, + error: function(data, textStatus, errorThrown) { + if (data.status == 412) { + $('#gollum-editor-submit').attr('disabled', false) + alert('Someone else has modified this page while you were editing it. Please store your version on disk outside of the browser, reload this page and reapply your modifications.'); + } else { + alert('Error updating page: ' + textStatus + ' ' + errorThrown); + } + } + }); + + }); } if( $("#last-edit").length ) { diff --git a/lib/gollum/templates/editor.mustache b/lib/gollum/templates/editor.mustache index fbb4e0e7..c4345ff7 100644 --- a/lib/gollum/templates/editor.mustache +++ b/lib/gollum/templates/editor.mustache @@ -1,9 +1,9 @@