mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
Use branch from request if specified
This commit is contained in:
@@ -99,19 +99,23 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
|||||||
failIfNotChanged(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch()));
|
failIfNotChanged(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch()));
|
||||||
Optional<RevCommit> revCommit = doCommit(request.getCommitMessage(), request.getAuthor(), request.isSign());
|
Optional<RevCommit> revCommit = doCommit(request.getCommitMessage(), request.getAuthor(), request.isSign());
|
||||||
|
|
||||||
if (initialCommit && StringUtils.isNotBlank(context.getGlobalConfig().getDefaultBranch())) {
|
if (initialCommit) {
|
||||||
createDefaultBranch();
|
handleBranchForInitialCommit();
|
||||||
}
|
}
|
||||||
|
|
||||||
push();
|
push();
|
||||||
return revCommit.orElseThrow(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch())).name();
|
return revCommit.orElseThrow(() -> new NoChangesMadeException(repository, ModifyWorker.this.request.getBranch())).name();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createDefaultBranch() {
|
private void handleBranchForInitialCommit() {
|
||||||
|
String branch = StringUtils.isNotBlank(request.getBranch()) ? request.getBranch() : context.getGlobalConfig().getDefaultBranch();
|
||||||
|
if (StringUtils.isNotBlank(branch)) {
|
||||||
try {
|
try {
|
||||||
getClone().checkout().setName(context.getGlobalConfig().getDefaultBranch()).setCreateBranch(true).call();
|
getClone().checkout().setName(branch).setCreateBranch(true).call();
|
||||||
} catch (GitAPIException e) {
|
} catch (GitAPIException e) {
|
||||||
throw new InternalRepositoryException(repository, "could not create default branch for initial commit", e);
|
throw new InternalRepositoryException(repository, "could not create default branch for initial commit", e);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,8 +67,12 @@ class GitWorkingCopyInitializer {
|
|||||||
Ref head = clone.exactRef(Constants.HEAD);
|
Ref head = clone.exactRef(Constants.HEAD);
|
||||||
|
|
||||||
if (head == null || !head.isSymbolic() || (initialBranch != null && !head.getTarget().getName().endsWith(initialBranch))) {
|
if (head == null || !head.isSymbolic() || (initialBranch != null && !head.getTarget().getName().endsWith(initialBranch))) {
|
||||||
|
if (clone.getRefDatabase().getRefs().isEmpty()) {
|
||||||
|
LOG.warn("could not initialize empty clone with given branch {}; this has to be handled later on", initialBranch);
|
||||||
|
} else {
|
||||||
throw notFound(entity("Branch", initialBranch).in(context.getRepository()));
|
throw notFound(entity("Branch", initialBranch).in(context.getRepository()));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return new ParentAndClone<>(null, clone, target);
|
return new ParentAndClone<>(null, clone, target);
|
||||||
} catch (GitAPIException | IOException e) {
|
} catch (GitAPIException | IOException e) {
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public class GitModifyCommand_InitialCommitTest extends AbstractGitCommandTestBa
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCommitOnMasterByDkefault() throws IOException, GitAPIException {
|
public void shouldCreateCommitOnMasterByDefault() throws IOException, GitAPIException {
|
||||||
createContext().getGlobalConfig().setDefaultBranch("");
|
createContext().getGlobalConfig().setDefaultBranch("");
|
||||||
|
|
||||||
executeModifyCommand();
|
executeModifyCommand();
|
||||||
@@ -87,17 +87,32 @@ public class GitModifyCommand_InitialCommitTest extends AbstractGitCommandTestBa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeModifyCommand() throws IOException {
|
@Test
|
||||||
File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile();
|
public void shouldCreateCommitWithBranchFromRequestIfPresent() throws IOException, GitAPIException {
|
||||||
|
createContext().getGlobalConfig().setDefaultBranch("main");
|
||||||
|
|
||||||
GitModifyCommand command = createCommand();
|
ModifyCommandRequest request = createRequest();
|
||||||
|
request.setBranch("different");
|
||||||
|
createCommand().execute(request);
|
||||||
|
|
||||||
|
try (Git git = new Git(createContext().open())) {
|
||||||
|
List<Ref> branches = git.branchList().call();
|
||||||
|
assertThat(branches).extracting("name").containsExactly("refs/heads/different");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void executeModifyCommand() throws IOException {
|
||||||
|
createCommand().execute(createRequest());
|
||||||
|
}
|
||||||
|
|
||||||
|
private ModifyCommandRequest createRequest() throws IOException {
|
||||||
|
File newFile = Files.write(temporaryFolder.newFile().toPath(), "new content".getBytes()).toFile();
|
||||||
|
|
||||||
ModifyCommandRequest request = new ModifyCommandRequest();
|
ModifyCommandRequest request = new ModifyCommandRequest();
|
||||||
request.setCommitMessage("initial commit");
|
request.setCommitMessage("initial commit");
|
||||||
request.addRequest(new ModifyCommandRequest.CreateFileRequest("new_file", newFile, false));
|
request.addRequest(new ModifyCommandRequest.CreateFileRequest("new_file", newFile, false));
|
||||||
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||||
|
return request;
|
||||||
command.execute(request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private GitModifyCommand createCommand() {
|
private GitModifyCommand createCommand() {
|
||||||
|
|||||||
Reference in New Issue
Block a user