List of usage examples for org.eclipse.jgit.api Git commit
public CommitCommand commit()
From source file:org.eclipse.orion.server.git.jobs.InitJob.java
License:Open Source License
public IStatus performJob() { try {// w w w . ja v a 2s. c om InitCommand command = new InitCommand(); File directory = new File(clone.getContentLocation()); command.setDirectory(directory); Repository repository = command.call().getRepository(); Git git = new Git(repository); // configure the repo GitCloneHandlerV1.doConfigureClone(git, user); // we need to perform an initial commit to workaround JGit bug 339610 git.commit().setMessage("Initial commit").call(); } catch (CoreException e) { return e.getStatus(); } catch (GitAPIException e) { return getGitAPIExceptionStatus(e, "Error initializing git repository"); } catch (JGitInternalException e) { return getJGitInternalExceptionStatus(e, "Error initializing git repository"); } catch (Exception e) { return new Status(IStatus.ERROR, GitActivator.PI_GIT, "Error initializing git repository", e); } JSONObject jsonData = new JSONObject(); try { jsonData.put(ProtocolConstants.KEY_LOCATION, URI.create(this.cloneLocation)); } catch (JSONException e) { // Should not happen } return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, jsonData); }
From source file:org.eclipse.orion.server.git.servlets.GitCommitHandlerV1.java
License:Open Source License
private boolean handlePost(HttpServletRequest request, HttpServletResponse response, Repository db, Path path) throws ServletException, NoFilepatternException, IOException, JSONException, CoreException, URISyntaxException {//ww w . j a va 2 s.com IPath filePath = path.hasTrailingSeparator() ? path.removeFirstSegments(1) : path.removeFirstSegments(1).removeLastSegments(1); Set<Entry<IPath, File>> set = GitUtils.getGitDirs(filePath, Traverse.GO_UP).entrySet(); File gitDir = set.iterator().next().getValue(); if (gitDir == null) return false; // TODO: or an error response code, 405? db = new FileRepository(gitDir); JSONObject requestObject = OrionServlet.readJSONRequest(request); String commitToMerge = requestObject.optString(GitConstants.KEY_MERGE, null); if (commitToMerge != null) { return merge(request, response, db, commitToMerge); } String commitToRebase = requestObject.optString(GitConstants.KEY_REBASE, null); String rebaseOperation = requestObject.optString(GitConstants.KEY_OPERATION, null); if (commitToRebase != null) { return rebase(request, response, db, commitToRebase, rebaseOperation); } String commitToCherryPick = requestObject.optString(GitConstants.KEY_CHERRY_PICK, null); if (commitToCherryPick != null) { return cherryPick(request, response, db, commitToCherryPick); } // continue with creating new commit location String newCommitToCreatelocation = requestObject.optString(GitConstants.KEY_COMMIT_NEW, null); if (newCommitToCreatelocation != null) return createCommitLocation(request, response, db, newCommitToCreatelocation); ObjectId refId = db.resolve(path.segment(0)); if (refId == null || !Constants.HEAD.equals(path.segment(0))) { String msg = NLS.bind("Commit failed. Ref must be HEAD and is {0}", path.segment(0)); return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } String message = requestObject.optString(GitConstants.KEY_COMMIT_MESSAGE, null); if (message == null || message.isEmpty()) { return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Missing commit message.", null)); } boolean amend = Boolean.parseBoolean(requestObject.optString(GitConstants.KEY_COMMIT_AMEND, null)); String committerName = requestObject.optString(GitConstants.KEY_COMMITTER_NAME, null); String committerEmail = requestObject.optString(GitConstants.KEY_COMMITTER_EMAIL, null); String authorName = requestObject.optString(GitConstants.KEY_AUTHOR_NAME, null); String authorEmail = requestObject.optString(GitConstants.KEY_AUTHOR_EMAIL, null); Git git = new Git(db); CommitCommand commit = git.commit(); // workaround of a bug in JGit which causes invalid // support of null values of author/committer name/email PersonIdent defPersonIdent = new PersonIdent(db); if (committerName == null) committerName = defPersonIdent.getName(); if (committerEmail == null) committerEmail = defPersonIdent.getEmailAddress(); if (authorName == null) authorName = committerName; if (authorEmail == null) authorEmail = committerEmail; commit.setCommitter(committerName, committerEmail); commit.setAuthor(authorName, authorEmail); // support for committing by path: "git commit -o path" boolean isRoot = true; String pattern = GitUtils.getRelativePath(path.removeFirstSegments(1), set.iterator().next().getKey()); if (!pattern.isEmpty()) { commit.setOnly(pattern); isRoot = false; } try { // "git commit [--amend] -m '{message}' [-a|{path}]" RevCommit lastCommit = commit.setAmend(amend).setMessage(message).call(); Map<ObjectId, JSONArray> commitToBranchMap = getCommitToBranchMap(db); JSONObject result = toJSON(db, lastCommit, commitToBranchMap, getURI(request), null, isRoot); OrionServlet.writeJSONResponse(request, response, result); return true; } catch (GitAPIException e) { return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "An error occured when commiting.", e)); } catch (JGitInternalException e) { return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An internal error occured when commiting.", e)); } }
From source file:org.eclipse.orion.server.git.servlets.InitJob.java
License:Open Source License
private IStatus doInit() { try {/*from ww w . j av a2 s . c o m*/ InitCommand command = new InitCommand(); File directory = new File(clone.getContentLocation()); command.setDirectory(directory); Repository repository = command.call().getRepository(); Git git = new Git(repository); // configure the repo GitCloneHandlerV1.doConfigureClone(git, user); // we need to perform an initial commit to workaround JGit bug 339610 git.commit().setMessage("Initial commit").call(); } catch (CoreException e) { return e.getStatus(); } catch (JGitInternalException e) { return getJGitInternalExceptionStatus(e, "An internal git error initializing git repository."); } catch (Exception e) { return new Status(IStatus.ERROR, GitActivator.PI_GIT, "Error initializing git repository", e); } return Status.OK_STATUS; }
From source file:org.eclipse.orion.server.tests.servlets.git.GitTest.java
License:Open Source License
protected void createRepository() throws IOException, GitAPIException, CoreException { IPath randomLocation = getRandomLocation(); gitDir = randomLocation.toFile();//from ww w . jav a2 s.c o m randomLocation = randomLocation.addTrailingSeparator().append(Constants.DOT_GIT); File dotGitDir = randomLocation.toFile().getCanonicalFile(); db = new FileRepository(dotGitDir); assertFalse(dotGitDir.exists()); db.create(false /* non bare */); testFile = new File(gitDir, "test.txt"); testFile.createNewFile(); createFile(testFile.toURI(), "test"); File folder = new File(gitDir, "folder"); folder.mkdir(); File folderFile = new File(folder, "folder.txt"); folderFile.createNewFile(); createFile(folderFile.toURI(), "folder"); Git git = new Git(db); git.add().addFilepattern(".").call(); git.commit().setMessage("Initial commit").call(); }
From source file:org.eclipse.recommenders.internal.snipmatch.GitSnippetRepositoryTest.java
License:Open Source License
private void addFileToRemote(String filename, File remote, Git git) throws Exception { File file = new File(remote, filename); file.createNewFile();/*from w ww. j a va2s.c om*/ git.add().addFilepattern(filename).call(); git.commit().setMessage("commit message").call(); }
From source file:org.eclipse.releng.tests.GitCopyrightAdapterTest.java
License:Open Source License
public void testLastModifiedYear() throws Exception { final Git git = new Git(db); git.add().addFilepattern(PROJECT_NAME + "/" + FILE1_NAME).call(); final PersonIdent committer2011 = new PersonIdent(committer, getDateForYear(2011)); git.commit().setMessage("old commit").setCommitter(committer2011).call(); git.add().addFilepattern(PROJECT_NAME + "/" + FILE2_NAME).call(); git.commit().setMessage("new commit").call(); final GitCopyrightAdapter adapter = new GitCopyrightAdapter(new IResource[] { project }); adapter.initialize(NULL_MONITOR);/*from w ww. j a v a 2 s .co m*/ final int lastModifiedYear = adapter.getLastModifiedYear(file1, NULL_MONITOR); Assert.assertEquals(2011, lastModifiedYear); }
From source file:org.eclipse.releng.tests.GitCopyrightAdapterTest.java
License:Open Source License
public void testCopyrightUpdateComment() throws Exception { final Git git = new Git(db); git.add().addFilepattern(PROJECT_NAME + "/" + FILE1_NAME).call(); git.commit().setMessage("copyright update").call(); final GitCopyrightAdapter adapter = new GitCopyrightAdapter(new IResource[] { project }); adapter.initialize(NULL_MONITOR);// ww w . ja v a 2 s . c o m final int lastModifiedYear = adapter.getLastModifiedYear(file1, NULL_MONITOR); Assert.assertEquals(0, lastModifiedYear); }
From source file:org.exist.git.xquery.Commit.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {// w w w . jav a 2 s. c om String localPath = args[0].getStringValue(); if (!(localPath.endsWith("/"))) localPath += File.separator; Git git = Git.open(new Resource(localPath), FS); CommitCommand command = git.commit().setMessage(args[1].getStringValue()); // .setAuthor(name, email) // .setCommitter(name, email) if (args.length >= 3) { for (int i = 0; i < args[2].getItemCount(); i++) { command.setOnly(args[2].itemAt(i).getStringValue()); } } // command.setAll(true); command.call(); return BooleanValue.TRUE; } catch (Throwable e) { Throwable cause = e.getCause(); if (cause != null) { throw new XPathException(this, Module.EXGIT001, cause.getMessage()); } throw new XPathException(this, Module.EXGIT001, e); } }
From source file:org.fusesource.fabric.git.internal.FabricGitServiceImpl.java
License:Apache License
private Git openOrInit(File repo) throws IOException { try {//from w ww . j ava 2s.com return Git.open(repo); } catch (RepositoryNotFoundException e) { try { Git git = Git.init().setDirectory(repo).call(); git.commit().setMessage("First Commit").setCommitter("fabric", "user@fabric").call(); return git; } catch (GitAPIException ex) { throw new IOException(ex); } } }
From source file:org.fusesource.fabric.git.internal.GitDataStore.java
License:Apache License
public <T> T gitOperation(PersonIdent personIdent, GitOperation<T> operation, boolean pullFirst, GitContext context) {/*from ww w .java2 s. c om*/ synchronized (gitOperationMonitor) { assertValid(); try { Git git = getGit(); Repository repository = git.getRepository(); CredentialsProvider credentialsProvider = getCredentialsProvider(); // lets default the identity if none specified if (personIdent == null) { personIdent = new PersonIdent(repository); } if (GitHelpers.hasGitHead(git)) { // lets stash any local changes just in case.. git.stashCreate().setPerson(personIdent).setWorkingDirectoryMessage("Stash before a write") .call(); } String originalBranch = repository.getBranch(); RevCommit statusBefore = CommitUtils.getHead(repository); if (pullFirst) { doPull(git, credentialsProvider); } T answer = operation.call(git, context); boolean requirePush = context.isRequirePush(); if (context.isRequireCommit()) { requirePush = true; String message = context.getCommitMessage().toString(); if (message.length() == 0) { LOG.warn("No commit message from " + operation + ". Please add one! :)"); } git.commit().setMessage(message).call(); } git.checkout().setName(originalBranch).call(); if (requirePush || hasChanged(statusBefore, CommitUtils.getHead(repository))) { clearCaches(); doPush(git, context, credentialsProvider); fireChangeNotifications(); } return answer; } catch (Exception e) { throw FabricException.launderThrowable(e); } } }