in maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommand.java [78:165]
protected CheckInScmResult executeCheckInCommand(
ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException {
Git git = null;
try {
File basedir = fileSet.getBasedir();
git = JGitUtils.openRepo(basedir);
boolean doCommit = false;
if (!fileSet.getFileList().isEmpty()) {
// add files first
doCommit = JGitUtils.addAllFiles(git, fileSet).size() > 0;
if (!doCommit) {
doCommit = git.status().call().hasUncommittedChanges();
}
} else {
// add all tracked files which are modified manually
Status status = git.status().call();
Set<String> changeds = git.status().call().getModified();
if (changeds.isEmpty()) {
if (!status.hasUncommittedChanges()) {
// warn there is nothing to add
logger.warn("There are neither files to be added nor any uncommitted changes");
doCommit = false;
} else {
logger.debug("There are uncommitted changes in the git index");
doCommit = true;
}
} else {
// TODO: gitexe only adds if fileSet is not empty
AddCommand add = git.add();
for (String changed : changeds) {
logger.debug("Add manually: {}", changed);
add.addFilepattern(changed);
doCommit = true;
}
add.call();
}
}
List<ScmFile> checkedInFiles = Collections.emptyList();
if (doCommit) {
UserInfo author = getAuthor(repo, git);
UserInfo committer = getCommitter(repo, git);
CommitCommand command = git.commit().setMessage(message).setAuthor(author.name, author.email);
command.setCommitter(committer.name, committer.email);
RevCommit commitRev = command.call();
logger.info("commit done: " + commitRev.getShortMessage());
checkedInFiles = JGitUtils.getFilesInCommit(git.getRepository(), commitRev, fileSet.getBasedir());
if (logger.isDebugEnabled()) {
for (ScmFile scmFile : checkedInFiles) {
logger.debug("in commit: " + scmFile);
}
}
}
if (repo.isPushChanges()) {
String branch = version != null ? version.getName() : null;
if (StringUtils.isBlank(branch)) {
branch = git.getRepository().getBranch();
}
RefSpec refSpec = new RefSpec(Constants.R_HEADS + branch + ":" + Constants.R_HEADS + branch);
logger.info("push changes to remote... " + refSpec);
Iterable<PushResult> pushResultList = JGitUtils.push(git, (GitScmProviderRepository) repo, refSpec);
for (PushResult pushResult : pushResultList) {
for (RemoteRefUpdate remoteRefUpdate : pushResult.getRemoteUpdates()) {
if (!isSuccessStatus(remoteRefUpdate.getStatus())) {
return new CheckInScmResult(
"JGit checkin",
"The git-push command failed, with status: " + remoteRefUpdate.getStatus(),
remoteRefUpdate.getMessage(),
false);
}
}
}
}
return new CheckInScmResult("JGit checkin", checkedInFiles);
} catch (Exception e) {
throw new ScmException("JGit checkin failure!", e);
} finally {
JGitUtils.closeRepo(git);
}
}