List of usage examples for org.eclipse.jgit.api Git open
public static Git open(File dir) throws IOException
From source file:org.ajoberstar.gradle.git.plugins.GitPluginExtension.java
License:Apache License
/** * Opens a {@code Git} instance for the//from w ww . j a va2 s .c om * repository path. * @param repositoryPath the path to the * repository * @return a new Git instance */ public Git open(Object repositoryPath) { try { return Git.open(project.file(repositoryPath)); } catch (IOException e) { throw new UncheckedIOException(e); } }
From source file:org.ajoberstar.gradle.git.tasks.GitBase.java
License:Apache License
/** * Gets a Git instance for this task's repository. * @return a new Git instance//from ww w. jav a 2s. c o m */ protected Git getGit() { try { return Git.open(getRepoDir()); } catch (IOException e) { throw new UncheckedIOException(e); } }
From source file:org.alex73.osm.monitors.export.GitClient.java
License:Open Source License
public GitClient(String dir) throws Exception { this.dir = new File(dir); repository = Git.open(this.dir).getRepository(); }
From source file:org.apache.maven.scm.provider.git.jgit.command.add.JGitAddCommand.java
License:Apache License
/** * {@inheritDoc}/*from ww w . java2 s . co m*/ */ protected ScmResult executeAddCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message, boolean binary) throws ScmException { if (fileSet.getFileList().isEmpty()) { throw new ScmException("You must provide at least one file/directory to add (e.g. -Dincludes=...)"); } Git git = null; try { git = Git.open(fileSet.getBasedir()); List<ScmFile> addedFiles = JGitUtils.addAllFiles(git, fileSet); if (getLogger().isDebugEnabled()) { for (ScmFile scmFile : addedFiles) { getLogger().info("added file: " + scmFile); } } return new AddScmResult("JGit add", addedFiles); } catch (Exception e) { throw new ScmException("JGit add failure!", e); } finally { JGitUtils.closeRepo(git); } }
From source file:org.apache.maven.scm.provider.git.jgit.command.blame.JGitBlameCommand.java
License:Apache License
@Override public BlameScmResult executeBlameCommand(ScmProviderRepository repo, ScmFileSet workingDirectory, String filename) throws ScmException { Git git = null;/* w ww .j av a 2 s.c o m*/ File basedir = workingDirectory.getBasedir(); try { git = Git.open(basedir); BlameResult blameResult = git.blame().setFilePath(filename).call(); List<BlameLine> lines = new ArrayList<BlameLine>(); int i = 0; while ((i = blameResult.computeNext()) != -1) { lines.add(new BlameLine(blameResult.getSourceAuthor(i).getWhen(), blameResult.getSourceCommit(i).getName(), blameResult.getSourceAuthor(i).getName(), blameResult.getSourceCommitter(i).getName())); } return new BlameScmResult("JGit blame", lines); } catch (Exception e) { throw new ScmException("JGit blame failure!", e); } finally { JGitUtils.closeRepo(git); } }
From source file:org.apache.maven.scm.provider.git.jgit.command.branch.JGitBranchCommand.java
License:Apache License
/** * {@inheritDoc}//from ww w. j av a 2 s .com */ @Override protected ScmResult executeBranchCommand(ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message) throws ScmException { if (branch == null || StringUtils.isEmpty(branch.trim())) { throw new ScmException("branch name must be specified"); } if (!fileSet.getFileList().isEmpty()) { throw new ScmException("This provider doesn't support branching subsets of a directory"); } Git git = null; try { git = Git.open(fileSet.getBasedir()); Ref branchResult = git.branchCreate().setName(branch).call(); getLogger().info("created [" + branchResult.getName() + "]"); if (getLogger().isDebugEnabled()) { for (String branchName : getShortLocalBranchNames(git)) { getLogger().debug("local branch available: " + branchName); } } if (repo.isPushChanges()) { getLogger().info("push branch [" + branch + "] to remote..."); JGitUtils.push(getLogger(), git, (GitScmProviderRepository) repo, new RefSpec(Constants.R_HEADS + branch)); } // search for the tagged files final RevWalk revWalk = new RevWalk(git.getRepository()); RevCommit commit = revWalk.parseCommit(branchResult.getObjectId()); revWalk.release(); final TreeWalk walk = new TreeWalk(git.getRepository()); walk.reset(); // drop the first empty tree, which we do not need here walk.setRecursive(true); walk.addTree(commit.getTree()); List<ScmFile> files = new ArrayList<ScmFile>(); while (walk.next()) { files.add(new ScmFile(walk.getPathString(), ScmFileStatus.CHECKED_OUT)); } walk.release(); return new BranchScmResult("JGit branch", files); } catch (Exception e) { throw new ScmException("JGit branch failed!", e); } finally { JGitUtils.closeRepo(git); } }
From source file:org.apache.maven.scm.provider.git.jgit.command.changelog.JGitChangeLogCommand.java
License:Apache License
protected ChangeLogScmResult executeChangeLogCommand(ScmProviderRepository repo, ScmFileSet fileSet, Date startDate, Date endDate, ScmBranch branch, String datePattern, ScmVersion startVersion, ScmVersion endVersion) throws ScmException { Git git = null;/*from w ww . ja v a 2 s. com*/ try { git = Git.open(fileSet.getBasedir()); String startRev = startVersion != null ? startVersion.getName() : null; String endRev = endVersion != null ? endVersion.getName() : null; List<ChangeEntry> gitChanges = this.whatchanged(git.getRepository(), null, startRev, endRev, startDate, endDate, -1); List<ChangeSet> modifications = new ArrayList<ChangeSet>(gitChanges.size()); for (ChangeEntry change : gitChanges) { ChangeSet scmChange = new ChangeSet(); scmChange.setAuthor(change.getAuthorName()); scmChange.setComment(change.getBody()); scmChange.setDate(change.getAuthorDate()); scmChange.setRevision(change.getCommitHash()); // X TODO scmChange.setFiles( change.get ) modifications.add(scmChange); } ChangeLogSet changeLogSet = new ChangeLogSet(modifications, startDate, endDate); changeLogSet.setStartVersion(startVersion); changeLogSet.setEndVersion(endVersion); return new ChangeLogScmResult("JGit changelog", changeLogSet); } catch (Exception e) { throw new ScmException("JGit changelog failure!", e); } finally { JGitUtils.closeRepo(git); } }
From source file:org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand.java
License:Apache License
/** * {@inheritDoc}/* www . ja v a 2 s . c o m*/ */ protected CheckInScmResult executeCheckInCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException { Git git = null; try { File basedir = fileSet.getBasedir(); git = Git.open(basedir); boolean doCommit = false; if (!fileSet.getFileList().isEmpty()) { doCommit = JGitUtils.addAllFiles(git, fileSet).size() > 0; } else { // add all tracked files which are modified manually Set<String> changeds = git.status().call().getModified(); if (changeds.isEmpty()) { // warn there is nothing to add getLogger().warn("there are no files to be added"); doCommit = false; } else { AddCommand add = git.add(); for (String changed : changeds) { getLogger().debug("add manualy: " + 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(); getLogger().info("commit done: " + commitRev.getShortMessage()); checkedInFiles = JGitUtils.getFilesInCommit(git.getRepository(), commitRev); if (getLogger().isDebugEnabled()) { for (ScmFile scmFile : checkedInFiles) { getLogger().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); getLogger().info("push changes to remote... " + refSpec.toString()); JGitUtils.push(getLogger(), git, (GitScmProviderRepository) repo, refSpec); } return new CheckInScmResult("JGit checkin", checkedInFiles); } catch (Exception e) { throw new ScmException("JGit checkin failure!", e); } finally { JGitUtils.closeRepo(git); } }
From source file:org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommandCommitterAuthorTckTest.java
License:Apache License
@Override public void testCheckInCommandTest() throws Exception { File fooJava = new File(getWorkingCopy(), "src/main/java/Foo.java"); assertFalse("check Foo.java doesn't yet exist", fooJava.canRead()); Git git = Git.open(getWorkingCopy()); RevCommit head = getHeadCommit(git.getRepository()); // Mark created the test repo... assertEquals("Mark Struberg", head.getCommitterIdent().getName()); JGitUtils.closeRepo(git);//from w w w .j a v a 2 s. co m createAndCommitFile(fooJava, null); // change user in config git = Git.open(getWorkingCopy()); StoredConfig config = git.getRepository().getConfig(); unsetConfig(config); config.setString("user", null, "name", "Dominik"); config.setString("user", null, "email", "domi@mycomp.com"); config.save(); // make a commit createAndCommitFile(fooJava, null); // check new commit is done with new user in config head = getHeadCommit(git.getRepository()); assertEquals("Dominik", head.getCommitterIdent().getName()); assertEquals("Dominik", head.getAuthorIdent().getName()); assertEquals("domi@mycomp.com", head.getAuthorIdent().getEmailAddress()); assertEquals("domi@mycomp.com", head.getCommitterIdent().getEmailAddress()); JGitUtils.closeRepo(git); // change user in config git = Git.open(getWorkingCopy()); config = git.getRepository().getConfig(); unsetConfig(config); config.setString("user", null, "name", "dbartholdi"); config.save(); // make a change createAndCommitFile(fooJava, null); // check new commit is done with new user in config head = getHeadCommit(git.getRepository()); assertEquals("dbartholdi", head.getCommitterIdent().getName()); assertFalse("no mail domain is configured, git system default should be used", head.getCommitterIdent().getEmailAddress().contains("dbartholdi")); JGitUtils.closeRepo(git); // unset a user and maven user but set default mail domain git = Git.open(getWorkingCopy()); config = git.getRepository().getConfig(); unsetConfig(config); config.setString(JGitCheckInCommand.GIT_MAVEN_SECTION, null, JGitCheckInCommand.GIT_MAILDOMAIN, "comp.com"); config.save(); // make a change with an user on the commandline createAndCommitFile(fooJava, "dude"); // check new commit is done with new maven user in config head = getHeadCommit(git.getRepository()); assertEquals("dude", head.getCommitterIdent().getName()); assertEquals("dude@comp.com", head.getCommitterIdent().getEmailAddress()); assertEquals("dude", head.getAuthorIdent().getName()); assertEquals("dude@comp.com", head.getAuthorIdent().getEmailAddress()); JGitUtils.closeRepo(git); // unset a user and maven user but set default mail domain git = Git.open(getWorkingCopy()); config = git.getRepository().getConfig(); unsetConfig(config); config.setString("user", null, "name", "dbartholdi"); config.setBoolean(JGitCheckInCommand.GIT_MAVEN_SECTION, null, JGitCheckInCommand.GIT_FORCE, true); config.setString(JGitCheckInCommand.GIT_MAVEN_SECTION, null, JGitCheckInCommand.GIT_MAILDOMAIN, "anycomp.com"); config.save(); // make a change with an user on the commandline createAndCommitFile(fooJava, "dude"); // check new commit is done with new maven user in config head = getHeadCommit(git.getRepository()); assertEquals("dude", head.getCommitterIdent().getName()); assertEquals("dude@anycomp.com", head.getCommitterIdent().getEmailAddress()); assertEquals("dude", head.getAuthorIdent().getName()); assertEquals("dude@anycomp.com", head.getAuthorIdent().getEmailAddress()); JGitUtils.closeRepo(git); // unset a user and maven user but set default mail domain git = Git.open(getWorkingCopy()); config = git.getRepository().getConfig(); unsetConfig(config); config.setString(JGitCheckInCommand.GIT_MAVEN_SECTION, null, JGitCheckInCommand.GIT_MAILDOMAIN, "anycomp.com"); config.save(); // make a change with no username given createAndCommitFile(fooJava, null); // check new commit does not contain the configured email domain head = getHeadCommit(git.getRepository()); assertFalse(head.getCommitterIdent().getEmailAddress().contains("anycomp.com")); assertFalse(head.getAuthorIdent().getEmailAddress().contains("anycomp.com")); JGitUtils.closeRepo(git); // unset a user and full maven section git = Git.open(getWorkingCopy()); config = git.getRepository().getConfig(); unsetConfig(config); config.save(); // make a change with an user on the commandline createAndCommitFile(fooJava, "dundy"); // check new commit is done with new maven user in config head = getHeadCommit(git.getRepository()); assertEquals("dundy", head.getCommitterIdent().getName()); assertEquals("dundy", head.getAuthorIdent().getName()); assertTrue( "the maven user (from parameter) name must be in the committer mail when nothing else is configured", head.getCommitterIdent().getEmailAddress().contains("dundy")); assertTrue("the user name (from parameter) must be in the author mail when nothing else is configured", head.getAuthorIdent().getEmailAddress().contains("dundy")); JGitUtils.closeRepo(git); // unset all configs git = Git.open(getWorkingCopy()); config = git.getRepository().getConfig(); unsetConfig(config); config.save(); // make a change with no user on the commandline createAndCommitFile(fooJava, null); // check new commit is has a committer/author with email set head = getHeadCommit(git.getRepository()); assertNotNull(head.getCommitterIdent().getName()); assertNotNull(head.getAuthorIdent().getName()); assertNotNull(head.getCommitterIdent().getEmailAddress()); assertNotNull(head.getAuthorIdent().getEmailAddress()); JGitUtils.closeRepo(git); }
From source file:org.apache.maven.scm.provider.git.jgit.command.checkout.JGitCheckOutCommand.java
License:Apache License
/** * For git, the given repository is a remote one. We have to clone it first if the working directory does not * contain a git repo yet, otherwise we have to git-pull it. * <p/>/*from ww w .ja va2 s.com*/ * {@inheritDoc} */ protected CheckOutScmResult executeCheckOutCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive) throws ScmException { GitScmProviderRepository repository = (GitScmProviderRepository) repo; if (GitScmProviderRepository.PROTOCOL_FILE.equals(repository.getFetchInfo().getProtocol()) && repository.getFetchInfo().getPath().indexOf(fileSet.getBasedir().getPath()) >= 0) { throw new ScmException("remote repository must not be the working directory"); } Git git = null; try { ProgressMonitor monitor = JGitUtils.getMonitor(getLogger()); String branch = version != null ? version.getName() : null; if (StringUtils.isBlank(branch)) { branch = Constants.MASTER; } getLogger().debug("try checkout of branch: " + branch); if (!fileSet.getBasedir().exists() || !(new File(fileSet.getBasedir(), ".git").exists())) { if (fileSet.getBasedir().exists()) { // git refuses to clone otherwise fileSet.getBasedir().delete(); } // FIXME only if windauze WindowCacheConfig cfg = new WindowCacheConfig(); cfg.setPackedGitMMAP(false); cfg.install(); // no git repo seems to exist, let's clone the original repo CredentialsProvider credentials = JGitUtils.getCredentials((GitScmProviderRepository) repo); getLogger().info("cloning [" + branch + "] to " + fileSet.getBasedir()); CloneCommand command = Git.cloneRepository().setURI(repository.getFetchUrl()); command.setCredentialsProvider(credentials).setBranch(branch).setDirectory(fileSet.getBasedir()); command.setProgressMonitor(monitor); git = command.call(); } JGitRemoteInfoCommand remoteInfoCommand = new JGitRemoteInfoCommand(); remoteInfoCommand.setLogger(getLogger()); RemoteInfoScmResult result = remoteInfoCommand.executeRemoteInfoCommand(repository, fileSet, null); if (git == null) { git = Git.open(fileSet.getBasedir()); } if (fileSet.getBasedir().exists() && new File(fileSet.getBasedir(), ".git").exists() && result.getBranches().size() > 0) { // git repo exists, so we must git-pull the changes CredentialsProvider credentials = JGitUtils.prepareSession(getLogger(), git, repository); if (version != null && StringUtils.isNotEmpty(version.getName()) && (version instanceof ScmTag)) { // A tag will not be pulled but we only fetch all the commits from the upstream repo // This is done because checking out a tag might not happen on the current branch // but create a 'detached HEAD'. // In fact, a tag in git may be in multiple branches. This occurs if // you create a branch after the tag has been created getLogger().debug("fetch..."); git.fetch().setCredentialsProvider(credentials).setProgressMonitor(monitor).call(); } else { getLogger().debug("pull..."); git.pull().setCredentialsProvider(credentials).setProgressMonitor(monitor).call(); } } Set<String> localBranchNames = JGitBranchCommand.getShortLocalBranchNames(git); if (version instanceof ScmTag) { getLogger().info("checkout tag [" + branch + "] at " + fileSet.getBasedir()); git.checkout().setName(branch).call(); } else if (localBranchNames.contains(branch)) { getLogger().info("checkout [" + branch + "] at " + fileSet.getBasedir()); git.checkout().setName(branch).call(); } else { getLogger().info("checkout remote branch [" + branch + "] at " + fileSet.getBasedir()); git.checkout().setName(branch).setCreateBranch(true) .setStartPoint(Constants.DEFAULT_REMOTE_NAME + "/" + branch).call(); } RevWalk revWalk = new RevWalk(git.getRepository()); RevCommit commit = revWalk.parseCommit(git.getRepository().resolve(Constants.HEAD)); revWalk.release(); final TreeWalk walk = new TreeWalk(git.getRepository()); walk.reset(); // drop the first empty tree, which we do not need here walk.setRecursive(true); walk.addTree(commit.getTree()); List<ScmFile> listedFiles = new ArrayList<ScmFile>(); while (walk.next()) { listedFiles.add(new ScmFile(walk.getPathString(), ScmFileStatus.CHECKED_OUT)); } walk.release(); getLogger().debug("current branch: " + git.getRepository().getBranch()); return new CheckOutScmResult("checkout via JGit", listedFiles); } catch (Exception e) { throw new ScmException("JGit checkout failure!", e); } finally { JGitUtils.closeRepo(git); } }