Skip to content

Commit

Permalink
Catch RefNotAdvertisedException when doing a Pull
Browse files Browse the repository at this point in the history
* This can occur when the remote master ref does not exist
* So catch it and do the rest of the action
  • Loading branch information
Phillipus committed Jun 27, 2017
1 parent a387a96 commit f6332e6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jgit.api.PullResult;
import org.eclipse.jgit.api.MergeResult.MergeStatus;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.RefNotAdvertisedException;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;

Expand Down Expand Up @@ -93,6 +94,7 @@ public void run() {
* Wrapper class to handle progress monitor
*/
class PushProgressHandler extends ProgressHandler {
private PullResult pullResult;

@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
Expand All @@ -102,9 +104,22 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
// Proxy
ProxyAuthenticater.update(getRepository().getOnlineRepositoryURL());

// First we need to Pull and resolve any conflicts
PullResult pullResult = GraficoUtils.pullFromRemote(getRepository().getLocalRepositoryFolder(), up.getUsername(), up.getPassword(), this);
// First we need to Pull
try {
pullResult = GraficoUtils.pullFromRemote(getRepository().getLocalRepositoryFolder(), up.getUsername(), up.getPassword(), this);
}
catch(Exception ex) {
// Remote is blank with no master ref, so do a push
if(ex instanceof RefNotAdvertisedException) {
doPush();
return;
}
else {
throw ex;
}
}

// There were problems on Pull
if(!pullResult.isSuccessful()) {
monitor.done();

Expand Down Expand Up @@ -149,9 +164,7 @@ public void run() {
}

// Push
GraficoUtils.pushToRemote(getRepository().getLocalRepositoryFolder(), up.getUsername(), up.getPassword(), this);

notifyChangeListeners(IRepositoryListener.HISTORY_CHANGED);
doPush();
}
}
catch(IOException | GitAPIException ex) {
Expand All @@ -161,6 +174,11 @@ public void run() {
monitor.done();
}
}

private void doPush() throws IOException, GitAPIException {
GraficoUtils.pushToRemote(getRepository().getLocalRepositoryFolder(), up.getUsername(), up.getPassword(), this);
notifyChangeListeners(IRepositoryListener.HISTORY_CHANGED);
}
}

Display.getCurrent().asyncExec(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jgit.api.PullResult;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.RefNotAdvertisedException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
Expand Down Expand Up @@ -95,6 +96,7 @@ public void run() {
* Wrapper class to handle progress monitor
*/
class RefreshProgressHandler extends ProgressHandler {
private PullResult pullResult;

@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
Expand All @@ -106,11 +108,23 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
// Proxy
ProxyAuthenticater.update(getRepository().getOnlineRepositoryURL());

// First we need to Pull and check for conflicts
PullResult pullResult = GraficoUtils.pullFromRemote(getRepository().getLocalRepositoryFolder(), up.getUsername(), up.getPassword(), this);
// First we need to Pull
try {
pullResult = GraficoUtils.pullFromRemote(getRepository().getLocalRepositoryFolder(), up.getUsername(), up.getPassword(), this);
}
catch(Exception ex) {
// Remote is blank with no master ref
if(ex instanceof RefNotAdvertisedException) {
return;
}
else {
throw ex;
}
}

monitor.done();

// Start a new thread because of dialog blocking
Display.getCurrent().asyncExec(new Runnable() {
@Override
public void run() {
Expand Down

0 comments on commit f6332e6

Please sign in to comment.