Skip to content
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

update isn't a fast forward #21

Closed
Pomax opened this issue Mar 7, 2014 · 9 comments
Closed

update isn't a fast forward #21

Pomax opened this issue Mar 7, 2014 · 9 comments

Comments

@Pomax
Copy link

Pomax commented Mar 7, 2014

I'm writing a new file to my repo, and afterward updating an existing file, using the .done handler. However, while the new file gets written without issue, the update throws the following API error:

{
  "message": "Update is not a fast forward",
  "documentation_url": "http://developer.github.com/v3/git/refs/#update-a-reference"
}

According to the indicated link, there's a way to force the update (I love forcing updates!) but the real question is why this update isn't considered a fast-forward update =/

@Pomax
Copy link
Author

Pomax commented Mar 7, 2014

fun fact, with force fixed to true, the second write causes the first write to get overwritten, so I guess that explains the "not a fast forward" warning... am I missing something that causes a branch write to "take effect"?

@philschatz
Copy link
Owner

Can you add a code sample exhibiting the problem? I did the following on http://philschatz.com/octokit.js/demo/ and was able to run it without problems (you can paste all lines at once):

USERNAME = 'octokit-test'
TOKEN = 'dca7f85a5911df8e9b7aeb4c5be8f5f50806ac49'
REPO = 'octokit-test-repo'
gh = new Octokit({token:TOKEN})
branch = gh.getRepo(USERNAME, REPO).getDefaultBranch()
branch.write('test.txt', 'Aloha')

and verified by running branch.contents('test.txt') and checking https://github.com/octokit-test/octokit-test-repo/blob/master/test.txt

@Pomax
Copy link
Author

Pomax commented Mar 7, 2014

try a branch.write(...).done( function() { branch.write(...); }. I see it consistently suceeding for single writes, it's when it tries to do two writes that things go wrong. My code is this one: https://github.com/Pomax/gh-blog/blob/gh-pages/gh-weblog/js/administration.js#L137 -- it saves a new entry, and the on done() it updates a file (in js/content.js). It seems that inside done() the head reference hasn't been updated yet, so the second write tries to get applied on top of the origin head, rather than head + first write.

@Pomax
Copy link
Author

Pomax commented Mar 7, 2014

I tested using:

USERNAME = 'octokit-test'
TOKEN = 'dca7f85a5911df8e9b7aeb4c5be8f5f50806ac49'
REPO = 'octokit-test-repo'
gh = new Octokit({token:TOKEN})
branch = gh.getRepo(USERNAME, REPO).getDefaultBranch()
branch.write('test.txt', 'Aloha').done(function() {
  branch.write('test.txt', 'Aloha2');
});

and looking at the network tab we see the same thing; the second write leads to

{
  "message": "Update is not a fast forward",
  "documentation_url": "http://developer.github.com/v3/git/refs/#update-a-reference"
}

and the file on the repo contains "aloha" rather than "aloha2"

@philschatz
Copy link
Owner

That indeed does not work. I'm guessing GitHub has a delay in updating their caches.

If you are updating multiple files you should use branch.writeMany (1 commit; also supports binary files) or you can probably daisy-chain the commit sha's so it is fast-forward. Something like:

branch.write('test.txt', 'Beta')
.then(function(ret) {
  branch.write('test.txt', 'Delta', 'Commit Message', false /* not binary */, ret.sha);
});

@Pomax
Copy link
Author

Pomax commented Mar 7, 2014

oh interesting. how does .then differ from .done in this use? I rewrote the .done to .then, but the effect's still the same. I suppose I can just stick in a several second timeout, but not the most ideal (definitely works though, a 2 second delay certainly works)

Also, one route is two writes, however, the other route is a remove and write, so unless writeMany can do simultaneous write/removes, this might still be a problem.

@philschatz
Copy link
Owner

RE https://github.com/Pomax/gh-blog/blob/gh-pages/gh-weblog/js/administration.js#L137: setTimeout might work most of the time but it relies on GitHub taking 2 seconds to invalidate all their caches.

.then returns another Promise while .done does not. I used .then because the REPL in philschatz.com/octokit.js/demo is smart enough to output the resolved value when the entered expression returns a promise.

@Pomax
Copy link
Author

Pomax commented Mar 7, 2014

true, it's not ideal, although for my specific use case it'll do the trick for now; the .then() construction seems to kick in too early. That said, with timeout it works quite brilliantly and I now finally have a blog system that's a single click, then typing markdown =)

Thank you very much for this library.

@philschatz
Copy link
Owner

Currently, .writeMany cannot do writes and removals; but maybe it should (.writeMany is intended to be the abstraction for a multi-file commit and does accept parameters for each file).

Thanks for using octokit.js; please blog and tweet about it ; )

In any case, closing the issue unless you have a reason to reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants