@Override public Revision commit(CommitRequest request) throws GitException { ensureExistenceRepoRootInWorkingDirectory(); CommitCommand command = nativeGit.createCommitCommand(); GitUser committer = getLocalCommitter(); command.setCommitter(committer); try { // overrider author from .gitconfig. We may set it in previous versions. // We need to override it since committer can differ from the person who clone or init // repository. getConfig().get("user.name"); command.setAuthor(committer); } catch (GitException e) { // ignore property not found. } command.setAll(request.isAll()); command.setAmend(request.isAmend()); command.setMessage(request.getMessage()); command.setFiles(request.getFiles()); try { command.execute(); LogCommand log = nativeGit.createLogCommand(); Revision rev = log.execute().get(0); rev.setBranch(getCurrentBranch()); return rev; } catch (Exception e) { Revision revision = DtoFactory.getInstance().createDto(Revision.class); revision.setMessage(e.getMessage()); revision.setFake(true); return revision; } }
@Override public PullResponse pull(PullRequest request) throws GitException, UnauthorizedException { String remoteUri = getRemoteUri(request.getRemote()); PullCommand pullCommand = nativeGit.createPullCommand(); pullCommand .setRemote(request.getRemote()) .setRefSpec(request.getRefSpec()) .setAuthor(getLocalCommitter()) .setRemoteUri(remoteUri) .setTimeout(request.getTimeout()); try { executeRemoteCommand(pullCommand); } catch (GitException exception) { if (noInitCommitWhenPullErrorPattern.matcher(exception.getMessage()).find()) { throw new GitException( exception.getMessage(), ErrorCodes.NO_COMMITTER_NAME_OR_EMAIL_DEFINED); } else if ("Unable get private ssh key".equals(exception.getMessage())) { throw new GitException(exception.getMessage(), ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY); } else if (("Auto-merging file\nCONFLICT (content): Merge conflict in file\n" + "Automatic merge failed; fix conflicts and then commit the result.\n") .equals(exception.getMessage())) { throw new GitException(exception.getMessage(), ErrorCodes.MERGE_CONFLICT); } else { throw exception; } } return pullCommand.getPullResponse(); }
@Override public void mv(MoveRequest request) throws GitException { nativeGit .createMoveCommand() .setSource(request.getSource()) .setTarget(request.getTarget()) .execute(); }
@Override public List<RemoteReference> lsRemote(LsRemoteRequest request) throws GitException, UnauthorizedException { LsRemoteCommand command = nativeGit.createLsRemoteCommand().setRemoteUrl(request.getRemoteUrl()); executeRemoteCommand(command); return command.getRemoteReferences(); }
@Override public void add(AddRequest request) throws GitException { AddCommand command = nativeGit.createAddCommand(); command.setFilePattern( request.getFilepattern() == null ? AddRequest.DEFAULT_PATTERN : request.getFilepattern()); command.setUpdate(request.isUpdate()); command.execute(); }
@Override public void add(AddRequest request) throws GitException { ensureExistenceRepoRootInWorkingDirectory(); AddCommand command = nativeGit.createAddCommand(); command.setFilePattern( request.getFilepattern() == null ? AddRequest.DEFAULT_PATTERN : request.getFilepattern()); command.setUpdate(request.isUpdate()); command.execute(); }
@Override public void reset(ResetRequest request) throws GitException { nativeGit .createResetCommand() .setMode(request.getType().getValue()) .setCommit(request.getCommit()) .setFilePattern(request.getFilePattern()) .execute(); }
@Override public List<RemoteReference> lsRemote(LsRemoteRequest request) throws GitException, UnauthorizedException { ensureExistenceRepoRootInWorkingDirectory(); LsRemoteCommand command = nativeGit.createLsRemoteCommand().setRemoteUrl(request.getRemoteUrl()); executeRemoteCommand(command); return command.getRemoteReferences(); }
@Override public void remoteAdd(RemoteAddRequest request) throws GitException { nativeGit .createRemoteAddCommand() .setName(request.getName()) .setUrl(request.getUrl()) .setBranches(request.getBranches()) .execute(); }
@Override public void mv(MoveRequest request) throws GitException { ensureExistenceRepoRootInWorkingDirectory(); nativeGit .createMoveCommand() .setSource(request.getSource()) .setTarget(request.getTarget()) .execute(); }
@Override public void rm(RmRequest request) throws GitException { nativeGit .createRemoveCommand() .setCached(request.isCached()) .setListOfItems(request.getItems()) .setRecursively(request.isRecursively()) .execute(); }
@Override public List<GitUser> getCommiters() throws GitException { List<GitUser> users = new LinkedList<>(); List<Revision> revList = nativeGit.createLogCommand().execute(); for (Revision rev : revList) { users.add(rev.getCommitter()); } return users; }
@Override public void reset(ResetRequest request) throws GitException { ensureExistenceRepoRootInWorkingDirectory(); nativeGit .createResetCommand() .setMode(request.getType().getValue()) .setCommit(request.getCommit()) .setFilePattern(request.getFilePattern()) .execute(); }
/** * Gets type of git object. * * @param gitObject revision object e.g. commit, tree, blob, tag. * @return type of git object */ private String getRevisionType(String gitObject) throws GitException { EmptyGitCommand command = nativeGit .createEmptyGitCommand() .setNextParameter("cat-file") .setNextParameter("-t") .setNextParameter(gitObject); command.execute(); return command.getText(); }
@Override public ShowFileContentResponse showFileContent(ShowFileContentRequest request) throws GitException { ShowFileContentCommand showCommand = nativeGit .createShowFileContentCommand() .withFile(request.getFile()) .withVersion(request.getVersion()); return showCommand.execute(); }
@Override public void remoteAdd(RemoteAddRequest request) throws GitException { ensureExistenceRepoRootInWorkingDirectory(); nativeGit .createRemoteAddCommand() .setName(request.getName()) .setUrl(request.getUrl()) .setBranches(request.getBranches()) .execute(); }
@Override public List<GitUser> getCommiters() throws GitException { ensureExistenceRepoRootInWorkingDirectory(); List<GitUser> users = new LinkedList<>(); List<Revision> revList = nativeGit.createLogCommand().execute(); for (Revision rev : revList) { users.add(rev.getCommitter()); } return users; }
@Override public void rm(RmRequest request) throws GitException { ensureExistenceRepoRootInWorkingDirectory(); nativeGit .createRemoveCommand() .setCached(request.isCached()) .setListOfItems(request.getItems()) .setRecursively(request.isRecursively()) .execute(); }
/** * Gets branch ref by branch name. * * @param branchName existing git branch name * @return ref to the branch * @throws GitException when it is not possible to get branchName ref */ private String getBranchRef(String branchName) throws GitException { EmptyGitCommand command = nativeGit.createEmptyGitCommand(); command.setNextParameter("show-ref").setNextParameter(branchName).execute(); final String output = command.getText(); if (output.isEmpty()) { throw new GitException("Error getting reference of branch."); } return output.split(" ")[1]; }
/** * Gets current branch name. * * @return name of current branch or <code>null</code> if current branch not exists * @throws GitException if any error occurs */ private String getCurrentBranch() throws GitException { BranchListCommand command = nativeGit.createBranchListCommand(); command.execute(); String branchName = null; for (String outLine : command.getLines()) { if (outLine.indexOf('*') != -1) { branchName = outLine.substring(2); } } return branchName; }
@Override public Tag tagCreate(TagCreateRequest request) throws GitException { return nativeGit .createTagCreateCommand() .setName(request.getName()) .setCommitter(getLocalCommitter()) .setCommit(request.getCommit()) .setMessage(request.getMessage()) .setForce(request.isForce()) .execute(); }
@Override public void fetch(FetchRequest request) throws GitException, UnauthorizedException { String remoteUri = getRemoteUri(request.getRemote()); FetchCommand fetchCommand = nativeGit.createFetchCommand(); fetchCommand .setRemote(request.getRemote()) .setPrune(request.isRemoveDeletedRefs()) .setRefSpec(request.getRefSpec()) .setRemoteUri(remoteUri) .setTimeout(request.getTimeout()); executeRemoteCommand(fetchCommand); }
@Override public void checkout(CheckoutRequest request) throws GitException { ensureExistenceRepoRootInWorkingDirectory(); nativeGit .createCheckoutCommand() .setBranchName(request.getName()) .setStartPoint(request.getStartPoint()) .setCreateNew(request.isCreateNew()) .setTrackBranch(request.getTrackBranch()) .setFilePaths(request.getFiles()) .execute(); }
@Override public Tag tagCreate(TagCreateRequest request) throws GitException { ensureExistenceRepoRootInWorkingDirectory(); return nativeGit .createTagCreateCommand() .setName(request.getName()) .setCommitter(getLocalCommitter()) .setCommit(request.getCommit()) .setMessage(request.getMessage()) .setForce(request.isForce()) .execute(); }
@Override public void checkout(CheckoutRequest request) throws GitException { nativeGit .createCheckoutCommand() .setBranchName(request.getName()) .setStartPoint(request.getStartPoint()) .setCreateNew(request.isCreateNew()) .setTrackBranch(request.getTrackBranch()) .setFilePaths(request.getFiles()) .setNoTrack(request.isNoTrack()) .execute(); }
@Override public MergeResult merge(MergeRequest request) throws GitException { final String gitObjectType = getRevisionType(request.getCommit()); if (!("commit".equalsIgnoreCase(gitObjectType) || "tag".equalsIgnoreCase(gitObjectType))) { throw new GitException("Invalid object for merge " + request.getCommit() + "."); } return nativeGit .createMergeCommand() .setCommit(request.getCommit()) .setCommitter(getLocalCommitter()) .execute(); }
@Override public void init(InitRequest request) throws GitException { InitCommand initCommand = nativeGit.createInitCommand(); initCommand.setBare(request.isBare()); initCommand.execute(); // make initial commit. if (!request.isBare() && request.isInitCommit()) { try { nativeGit .createAddCommand() .setFilePattern(new ArrayList<>(Collections.singletonList("."))) .execute(); nativeGit .createCommitCommand() .setCommitter(getLocalCommitter()) .setMessage("init") .execute(); } catch (GitException ignored) { // if nothing to commit } } }
@Override public LogPage log(LogRequest request) throws GitException { try { return new LogPage( nativeGit.createLogCommand().setFileFilter(request.getFileFilter()).execute()); } catch (ServerException exception) { if (noInitCommitWhenLogErrorPattern.matcher(exception.getMessage()).find()) { throw new GitException(exception.getMessage(), ErrorCodes.INIT_COMMIT_WAS_NOT_PERFORMED); } else { throw exception; } } }
@Override public void remoteUpdate(RemoteUpdateRequest request) throws GitException { nativeGit .createRemoteUpdateCommand() .setRemoteName(request.getName()) .setAddUrl(request.getAddUrl()) .setBranchesToAdd(request.getBranches()) .setAddBranches(request.isAddBranches()) .setAddPushUrl(request.getAddPushUrl()) .setRemovePushUrl(request.getRemovePushUrl()) .setRemoveUrl(request.getRemoveUrl()) .execute(); }
/** * Ensure existence repository root directory inside working directory and in our virtual file * system * * @throws GitException if git root folder is not in working directory */ void ensureExistenceRepoRootInWorkingDirectory() throws GitException { if (isInsideWorkTree()) { final EmptyGitCommand emptyGitCommand = nativeGit.createEmptyGitCommand(); emptyGitCommand.setNextParameter("rev-parse").setNextParameter("--git-dir").execute(); final String gitDir = emptyGitCommand.getText(); // here we check that git repo inside our file system mount point if (!gitDir.startsWith(mountRoot.getAbsolutePath()) && !gitDir.equals(".git")) { throw new GitException("Project is not a git repository."); } } else { throw new GitException("Project is not a git repository."); } }