Skip to content

Commit e2d3585

Browse files
author
Igor Vinokur
committed
Merge pull request eclipse-che#473 from eclipse/CHE-521
CHE-521: Perform git init command without initial commit
2 parents 8482352 + 0af0f96 commit e2d3585

17 files changed

Lines changed: 151 additions & 118 deletions

File tree

core/che-core-git-impl-native/src/main/java/org/eclipse/che/git/impl/nativegit/NativeGitConnection.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.google.common.collect.ImmutableMap;
1515

1616
import org.eclipse.che.api.core.ErrorCodes;
17+
import org.eclipse.che.api.core.ServerException;
1718
import org.eclipse.che.api.core.UnauthorizedException;
1819
import org.eclipse.che.api.core.util.LineConsumerFactory;
1920
import org.eclipse.che.api.git.Config;
@@ -175,8 +176,15 @@ public void checkout(CheckoutRequest request) throws GitException {
175176
public Branch branchCreate(BranchCreateRequest request) throws GitException {
176177
BranchCreateCommand branchCreateCommand = nativeGit.createBranchCreateCommand();
177178
branchCreateCommand.setBranchName(request.getName())
178-
.setStartPoint(request.getStartPoint())
179-
.execute();
179+
.setStartPoint(request.getStartPoint());
180+
try {
181+
branchCreateCommand.execute();
182+
} catch (ServerException exception) {
183+
Pattern errorPattern = Pattern.compile("fatal: Not a valid object name: '.*'.\n");
184+
if (errorPattern.matcher(exception.getMessage()).find()) {
185+
throw new GitException(exception.getMessage(), ErrorCodes.INIT_COMMIT_WAS_NOT_PERFORMED);
186+
}
187+
}
180188
return DtoFactory.getInstance().createDto(Branch.class).withName(getBranchRef(request.getName())).withActive(false)
181189
.withDisplayName(request.getName()).withRemote(false);
182190
}
@@ -358,25 +366,20 @@ public void init(InitRequest request) throws GitException {
358366
InitCommand initCommand = nativeGit.createInitCommand();
359367
initCommand.setBare(request.isBare());
360368
initCommand.execute();
361-
//make initial commit.
362-
if (!request.isBare() && request.isInitCommit()) {
363-
try {
364-
nativeGit.createAddCommand()
365-
.setFilePattern(new ArrayList<>(Collections.singletonList(".")))
366-
.execute();
367-
nativeGit.createCommitCommand()
368-
.setCommitter(getLocalCommitter())
369-
.setMessage("init")
370-
.execute();
371-
} catch (GitException ignored) {
372-
//if nothing to commit
373-
}
374-
}
375369
}
376370

377371
@Override
378372
public LogPage log(LogRequest request) throws GitException {
379-
return new LogPage(nativeGit.createLogCommand().setFileFilter(request.getFileFilter()).execute());
373+
try {
374+
return new LogPage(nativeGit.createLogCommand().setFileFilter(request.getFileFilter()).execute());
375+
} catch (ServerException exception) {
376+
Pattern errorPattern = Pattern.compile("fatal: your current branch '.*' does not have any commits yet\n");
377+
if (errorPattern.matcher(exception.getMessage()).find()) {
378+
throw new GitException(exception.getMessage(), ErrorCodes.INIT_COMMIT_WAS_NOT_PERFORMED);
379+
} else {
380+
throw exception;
381+
}
382+
}
380383
}
381384

382385
@Override

core/che-core-git-impl-native/src/main/java/org/eclipse/che/git/impl/nativegit/commands/BranchRenameCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ private void renameRemoteBranch() throws GitException {
6565
commandLine.add("-t");
6666
commandLine.add(branchName);
6767
start();
68-
} catch (GitException e) {
69-
String errorMessage = e.getMessage();
68+
} catch (GitException exception) {
69+
String errorMessage = exception.getMessage();
7070
if (!checkoutErrorPattern.matcher(errorMessage).find()) {
71-
throw new GitException(errorMessage);
71+
throw exception;
7272
}
7373
//local branch already exist - so ignore and try perform the next step
7474
}

core/ide/che-core-ide-app/src/main/resources/org/eclipse/che/ide/CoreLocalizationConstant.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ importProject.message.failure = Failed to import project {0}
147147
importProject.message.startWithWhiteSpace = The url can not start with a whitespace.
148148
importProject.message.urlInvalid = The URL provided for the project to import is invalid.
149149
importProject.message.unableGetSshKey = Unable get private ssh key. \
150-
You can create a new SSH key pair in Window->Preferences->Keys->SSH Keystore.
150+
You can create a new SSH key pair in Help->Preferences->SSH->Git.
151151
importProject.importer.info = Importer Information
152152
importProject.project.info = Project Information
153153
importProject.name.prompt = Define the name of your project...

core/platform-api-client-gwt/che-core-client-gwt-git/src/main/java/org/eclipse/che/api/git/gwt/client/GitServiceClientImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ public void init(String workspaceId, ProjectConfigDto project, boolean bare, fin
142142
InitRequest initRequest = dtoFactory.createDto(InitRequest.class);
143143
initRequest.setBare(bare);
144144
initRequest.setWorkingDir(project.getName());
145-
initRequest.setInitCommit(true);
146145

147146
String url = "/git/" + workspaceId + INIT + "?projectPath=" + project.getPath();
148147

core/platform-api/che-core-api-core/src/main/java/org/eclipse/che/api/core/ErrorCodes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ private ErrorCodes() {
2424
public static final int UNAUTHORIZED_GIT_OPERATION = 32080;
2525
public static final int FAILED_CHECKOUT = 32063;
2626
public static final int FAILED_CHECKOUT_WITH_START_POINT = 32064;
27+
public static final int INIT_COMMIT_WAS_NOT_PERFORMED = 32082;
2728
}

core/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitProjectImporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private void cloneRepository(GitConnection git, String remoteName, String url, D
224224
}
225225

226226
private void initRepository(GitConnection git, DtoFactory dtoFactory) throws GitException {
227-
final InitRequest request = dtoFactory.createDto(InitRequest.class).withInitCommit(false).withBare(false);
227+
final InitRequest request = dtoFactory.createDto(InitRequest.class).withBare(false);
228228
git.init(request);
229229
}
230230

core/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/shared/InitRequest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,4 @@ public interface InitRequest extends GitRequest {
3434
void setBare(boolean bare);
3535

3636
InitRequest withBare(boolean bare);
37-
38-
/** @return <code>true</code> then all files in newly initialized repository will be commited with "init" message */
39-
boolean isInitCommit();
40-
41-
void setInitCommit(boolean initCommit);
42-
43-
InitRequest withInitCommit(boolean initCommit);
44-
}
37+
}

plugins/plugin-git/che-plugin-git-ext-git/src/main/java/org/eclipse/che/ide/ext/git/client/GitLocalizationConstant.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ public interface GitLocalizationConstant extends Messages {
7171
String buttonScroll();
7272

7373
// MESSAGES
74-
@Key("messages.unableGetSshKeyTitle")
75-
String messagesUnableGetSshKeyTitle();
76-
7774
@Key("messages.unableGetSshKey")
7875
String messagesUnableGetSshKey();
7976

@@ -121,6 +118,9 @@ public interface GitLocalizationConstant extends Messages {
121118

122119
@Key("messages.committer_identity_info_empty")
123120
String committerIdentityInfoEmpty();
121+
122+
@Key("messages.init_commit_was_not_performed")
123+
String initCommitWasNotPerformed();
124124

125125
@Key("messages.diff.failed")
126126
String diffFailed();

plugins/plugin-git/che-plugin-git-ext-git/src/main/java/org/eclipse/che/ide/ext/git/client/branch/BranchPresenter.java

Lines changed: 56 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import com.google.inject.Singleton;
1717
import com.google.web.bindery.event.shared.EventBus;
1818

19-
import org.eclipse.che.api.core.rest.shared.dto.ServiceError;
19+
import org.eclipse.che.api.core.ErrorCodes;
2020
import org.eclipse.che.api.git.gwt.client.GitServiceClient;
2121
import org.eclipse.che.api.git.shared.Branch;
2222
import org.eclipse.che.api.git.shared.CheckoutRequest;
@@ -49,10 +49,12 @@
4949
import org.eclipse.che.ide.ui.dialogs.InputCallback;
5050

5151
import javax.validation.constraints.NotNull;
52+
5253
import java.util.List;
5354

5455
import static org.eclipse.che.api.git.shared.BranchListRequest.LIST_ALL;
5556
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;
57+
import static org.eclipse.che.ide.util.ExceptionUtils.getErrorCode;
5658

5759
/**
5860
* Presenter for displaying and work with branches.
@@ -174,12 +176,7 @@ protected void onSuccess(String result) {
174176

175177
@Override
176178
protected void onFailure(Throwable exception) {
177-
String errorMessage =
178-
(exception.getMessage() != null) ? exception.getMessage() : constant.branchRenameFailed();
179-
final GitOutputConsole console = gitOutputConsoleFactory.create(BRANCH_RENAME_COMMAND_NAME);
180-
console.printError(errorMessage);
181-
consolesPanelPresenter.addCommandOutput(appContext.getDevMachineId(), console);
182-
notificationManager.notify(constant.branchRenameFailed(), FAIL, true, project.getRootProject());
179+
handleError(exception, BRANCH_RENAME_COMMAND_NAME);
183180
getBranches();//rename of remote branch occurs in three stages, so needs update list of branches on view
184181
}
185182
});
@@ -205,9 +202,7 @@ protected void onSuccess(String result) {
205202

206203
@Override
207204
protected void onFailure(Throwable exception) {
208-
GitOutputConsole console = gitOutputConsoleFactory.create(BRANCH_DELETE_COMMAND_NAME);
209-
handleError(exception, console);
210-
consolesPanelPresenter.addCommandOutput(appContext.getDevMachineId(), console);
205+
handleError(exception, BRANCH_DELETE_COMMAND_NAME);
211206
}
212207
});
213208
}
@@ -257,9 +252,7 @@ protected void onFailure(Throwable exception) {
257252

258253
@Override
259254
protected void onFailure(Throwable exception) {
260-
final GitOutputConsole console = gitOutputConsoleFactory.create(BRANCH_CHECKOUT_COMMAND_NAME);
261-
printGitMessage(exception.getMessage(), console);
262-
consolesPanelPresenter.addCommandOutput(appContext.getDevMachineId(), console);
255+
handleError(exception, BRANCH_CHECKOUT_COMMAND_NAME);
263256
}
264257
});
265258
}
@@ -302,44 +295,25 @@ protected void onFailure(Throwable throwable) {
302295
}
303296
}
304297

305-
private void printGitMessage(String messageText, GitOutputConsole console) {
306-
if (messageText == null || messageText.isEmpty()) {
307-
return;
308-
}
309-
JSONObject jsonObject = JSONParser.parseStrict(messageText).isObject();
310-
if (jsonObject == null) {
311-
return;
312-
}
313-
String message = "";
314-
if (jsonObject.containsKey("message")) {
315-
message = jsonObject.get("message").isString().stringValue();
316-
}
317-
318-
console.print("");
319-
String[] lines = message.split("\n");
320-
for (String line : lines) {
321-
console.printError(line);
322-
}
323-
}
324-
325298
/** Get the list of branches. */
326299
private void getBranches() {
327300
service.branchList(workspaceId, project.getRootProject(), LIST_ALL,
328301
new AsyncRequestCallback<List<Branch>>(dtoUnmarshallerFactory.newListUnmarshaller(Branch.class)) {
329302
@Override
330303
protected void onSuccess(List<Branch> result) {
331-
view.setBranches(result);
332-
view.showDialogIfClosed();
304+
if (result.isEmpty()) {
305+
dialogFactory.createMessageDialog(constant.branchTitle(),
306+
constant.initCommitWasNotPerformed(),
307+
null).show();
308+
} else {
309+
view.setBranches(result);
310+
view.showDialogIfClosed();
311+
}
333312
}
334313

335314
@Override
336315
protected void onFailure(Throwable exception) {
337-
final String errorMessage =
338-
(exception.getMessage() != null) ? exception.getMessage() : constant.branchesListFailed();
339-
final GitOutputConsole console = gitOutputConsoleFactory.create(BRANCH_LIST_COMMAND_NAME);
340-
console.printError(errorMessage);
341-
consolesPanelPresenter.addCommandOutput(appContext.getDevMachineId(), console);
342-
notificationManager.notify(constant.branchesListFailed(), FAIL, true, project.getRootProject());
316+
handleError(exception, BRANCH_LIST_COMMAND_NAME);
343317
}
344318
}
345319
);
@@ -361,16 +335,10 @@ protected void onSuccess(Branch result) {
361335

362336
@Override
363337
protected void onFailure(Throwable exception) {
364-
final String errorMessage = (exception.getMessage() != null)
365-
? exception.getMessage()
366-
: constant.branchCreateFailed();
367-
final GitOutputConsole console = gitOutputConsoleFactory.create(BRANCH_CREATE_COMMAND_NAME);
368-
console.printError(errorMessage);
369-
consolesPanelPresenter.addCommandOutput(appContext.getDevMachineId(), console);
370-
notificationManager.notify(constant.branchCreateFailed(), FAIL, true, project.getRootProject());
338+
handleError(exception, BRANCH_CREATE_COMMAND_NAME);
371339
}
372-
}
373-
);
340+
341+
});
374342
}
375343

376344
}
@@ -400,32 +368,50 @@ public void onBranchSelected(@NotNull Branch branch) {
400368
/**
401369
* Handler some action whether some exception happened.
402370
*
403-
* @param throwable
371+
* @param exception
404372
* exception what happened
405-
* @param console
406-
* console for displaying error
373+
* @param commandName
374+
* name of the executed command
407375
*/
408-
void handleError(@NotNull Throwable throwable, GitOutputConsole console) {
409-
String errorMessage = throwable.getMessage();
410-
if (errorMessage == null) {
411-
console.printError(constant.branchDeleteFailed());
412-
notificationManager.notify(constant.branchDeleteFailed(), FAIL, true, project.getRootProject());
376+
void handleError(@NotNull Throwable exception, String commandName) {
377+
if (getErrorCode(exception) == ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY) {
378+
dialogFactory.createMessageDialog(commandName, constant.messagesUnableGetSshKey(), null).show();
413379
return;
414380
}
415381

416-
try {
417-
errorMessage = dtoFactory.createDtoFromJson(errorMessage, ServiceError.class).getMessage();
418-
if (errorMessage.equals("Unable get private ssh key")) {
419-
console.printError(constant.messagesUnableGetSshKey());
420-
notificationManager.notify(constant.messagesUnableGetSshKeyTitle(), constant.messagesUnableGetSshKey(), FAIL, true,
421-
project.getRootProject());
422-
return;
382+
String errorMessage = exception.getMessage();
383+
if (errorMessage == null) {
384+
switch (commandName) {
385+
case BRANCH_CREATE_COMMAND_NAME:
386+
errorMessage = constant.branchCreateFailed();
387+
break;
388+
case BRANCH_DELETE_COMMAND_NAME:
389+
errorMessage = constant.branchDeleteFailed();
390+
break;
391+
case BRANCH_LIST_COMMAND_NAME:
392+
errorMessage = constant.branchesListFailed();
393+
break;
394+
case BRANCH_RENAME_COMMAND_NAME:
395+
errorMessage = constant.branchRenameFailed();
396+
break;
397+
case BRANCH_CHECKOUT_COMMAND_NAME:
398+
errorMessage = constant.branchCheckoutFailed();
399+
break;
423400
}
424-
console.printError(errorMessage);
425-
notificationManager.notify(errorMessage, FAIL, true, project.getRootProject());
426-
} catch (Exception e) {
427-
console.printError(errorMessage);
428-
notificationManager.notify(errorMessage, FAIL, true, project.getRootProject());
429401
}
402+
403+
GitOutputConsole console = gitOutputConsoleFactory.create(commandName);
404+
printGitMessage(errorMessage, console);
405+
consolesPanelPresenter.addCommandOutput(appContext.getDevMachineId(), console);
406+
notificationManager.notify(errorMessage, FAIL, true, project.getRootProject());
430407
}
408+
409+
private void printGitMessage(String messageText, GitOutputConsole console) {
410+
console.print("");
411+
String[] lines = messageText.split("\n");
412+
for (String line : lines) {
413+
console.printError(line);
414+
}
415+
}
416+
431417
}

plugins/plugin-git/che-plugin-git-ext-git/src/main/java/org/eclipse/che/ide/ext/git/client/commit/CommitPresenter.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.eclipse.che.api.git.gwt.client.GitServiceClient;
1818
import org.eclipse.che.api.git.shared.LogResponse;
1919
import org.eclipse.che.api.git.shared.Revision;
20+
import org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto;
2021
import org.eclipse.che.ide.api.app.AppContext;
2122
import org.eclipse.che.ide.api.notification.NotificationManager;
2223
import org.eclipse.che.ide.api.project.node.HasStorablePath;
@@ -43,6 +44,7 @@
4344
import java.util.List;
4445

4546
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;
47+
import static org.eclipse.che.ide.util.ExceptionUtils.getErrorCode;
4648

4749
/**
4850
* Presenter for commit changes on git.
@@ -275,8 +277,9 @@ public void onValueChanged() {
275277

276278
@Override
277279
public void setAmendCommitMessage() {
280+
final ProjectConfigDto project = appContext.getCurrentProject().getRootProject();
278281
final Unmarshallable<LogResponse> unmarshall = dtoUnmarshallerFactory.newUnmarshaller(LogResponse.class);
279-
this.service.log(workspaceId, appContext.getCurrentProject().getRootProject(), null, false,
282+
this.service.log(workspaceId, project, null, false,
280283
new AsyncRequestCallback<LogResponse>(unmarshall) {
281284
@Override
282285
protected void onSuccess(final LogResponse result) {
@@ -294,8 +297,15 @@ protected void onSuccess(final LogResponse result) {
294297

295298
@Override
296299
protected void onFailure(final Throwable exception) {
297-
Log.warn(CommitPresenter.class, "Git log failed", exception);
298-
CommitPresenter.this.view.setMessage("");
300+
if (getErrorCode(exception) == ErrorCodes.INIT_COMMIT_WAS_NOT_PERFORMED) {
301+
dialogFactory.createMessageDialog(constant.commitTitle(),
302+
constant.initCommitWasNotPerformed(),
303+
null).show();
304+
} else {
305+
Log.warn(CommitPresenter.class, "Git log failed", exception);
306+
CommitPresenter.this.view.setMessage("");
307+
notificationManager.notify(constant.logFailed(), FAIL, false, project);
308+
}
299309
}
300310
});
301311
}

0 commit comments

Comments
 (0)