2

OK, so I'm setting up my .zshrc, and in my function, I want to know the reason why git push fails, when it does.

I know we can tell if command was successful by checking if $? is 0 or not, like this:

git push origin master   # this command might fail for some reason.
if (($?)); then
    # 1 == $? Failure
    echo "Command Failed!!"
    return 1
fi
echo "Command Successful!"

But this does not tell us the reason for this (there were conflicts, couldn't connect to the internet in the first place, etc.) How can I do it?

Edit
I'm creating my own .zshrc. Current work is on GitHub here.
I wanted my .zshrc to be able to save changes made to itself to remote repo and to load changes from remote repo. I'm almost done, and now I'm creating some error trapping. As you can see in my remote repo, the function savezshrc will:

  1. see if there are changes made in ~/.zshrc (call it home zshrc) by comparing home zshrc and .zshrc in local repo (call it repo zshrc).
  2. if there are changes, copy home zshrc into local repo, git add . there, then git commit.
  3. if commit succeeds, it'll try to git push origin master.

Here, step 3. might yield error. And if that error is:

  1. can't connect to github or something, I want everything rolled back by git reset --hard HEAD~. Without this, when you try again you'll be stuck at step 1.
  2. merge conflict detected, doing further git process might mess everything up, so I want it to advise me to solve the conflict and git push manually.

Currently I'm stuck how can I distinguish the state between 1. and 2. Is there any way to do this?

Collapsed PLUG
  • 304
  • 5
  • 14
  • 3
    Does git not send that information to standard output/error when it fails? Do you not see that output? – Etan Reisner May 28 '15 at 01:20
  • I do see one! Might be able to utilise it - but I don't see how to do it... I'll try to figure it out... – Collapsed PLUG May 28 '15 at 01:26
  • If you want to *capture* the output from the git command then you need to do that when you run the command (e.g. `var=$(git ... 2>&1)`) you can't do it after the fact. What are you trying to do here exactly? – Etan Reisner May 28 '15 at 02:48
  • I don't think git uses different error codes for different errors like that but you can check for certain errors manually (e.g. does `git remote show origin` work to detect auth/connectivity problems) before doing the work. That being said you don't need to push to be able to detect changes again at a later date (you just need to compare the local repo ref to the remote ref instead of the files themselves or whatever). – Etan Reisner May 28 '15 at 13:04

1 Answers1

0

Consider first trying to fetch from Github. If git fetch bla bla fails, then you surely could not reach the repository (or didn't have rights for access, or the repo does not exist or ... etc).

Once fetch succeeds, you should do a local merge in memory as explained here https://stackoverflow.com/a/6283843/766289 if the merge in memory succeeds. See the latest comments to that answer for "somewhat tested" means to do this.

If the merge did not succeed, you abort.

Now if the merge with upstream went ok, then pushing will also work out ok.

Writing out the shell code is left as an exercise to the reader :-P

Community
  • 1
  • 1
Francisco
  • 3,980
  • 1
  • 23
  • 27
  • Ah, just found that `git pull` is actually `git fetch` and `git merge (remote)`! I won't be able to try this out now cause I've got another task, but I'll try for sure and when I do, I'll comment again. – Collapsed PLUG May 31 '15 at 14:36