org.eclipse.jgit.api.AddCommand Java Examples

The following examples show how to use org.eclipse.jgit.api.AddCommand. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Scenarios.java    From jgitver with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a commit in the repo, by modifying the given file.
 * The git commitID will be stored in the scenario in front of the given app identifier. 
 * @param fileName the filename to be touched, added and commited into the repo.
 * @param id the application identifier to use to store the git commitID in front of
 * @return the builder itself to continue building the scenario
 */
public ScenarioBuilder commit(String fileName, String id) {
    File content = new File(scenario.getRepositoryLocation(), fileName);
    try {
        AddCommand add = git.add().addFilepattern(fileName);
        Files.touch(content);
        add.call();
        RevCommit rc = git.commit().setMessage("content " + id).call();
        scenario.getCommits().put(id, rc.getId());
    } catch (Exception ex) {
        throw new IllegalStateException(String.format("error creating a commit with new file %s", content), ex);
    }
    return this;
}
 
Example #2
Source File: RepositoryResource.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected CommitInfo doCreateDirectory(Git git, String path) throws Exception {
    File file = getRelativeFile(path);
    if (file.exists()) {
        return null;
    }
    file.mkdirs();
    String filePattern = getFilePattern(path);
    AddCommand add = git.add().addFilepattern(filePattern).addFilepattern(".");
    add.call();

    CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(message);
    RevCommit revCommit = commitThenPush(git, commit);
    return createCommitInfo(revCommit);
}
 
Example #3
Source File: RepositoryResource.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected CommitInfo doWrite(Git git, String path, byte[] contents, PersonIdent personIdent, String commitMessage) throws Exception {
    File file = getRelativeFile(path);
    file.getParentFile().mkdirs();

    Files.writeToFile(file, contents);

    String filePattern = getFilePattern(path);
    AddCommand add = git.add().addFilepattern(filePattern).addFilepattern(".");
    add.call();

    CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(commitMessage);
    RevCommit revCommit = commitThenPush(git, commit);
    return createCommitInfo(revCommit);
}
 
Example #4
Source File: GitCacheCloneReadSaveRequest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
void save() throws IOException {
    invokeOnScm(new GitSCMFileSystem.FSFunction<Void>() {
        @Override
        public Void invoke(Repository repository) throws IOException, InterruptedException {
            Git activeRepo = getActiveRepository(repository);
            Repository repo = activeRepo.getRepository();
            File repoDir = repo.getDirectory().getParentFile();
            log.fine("Repo cloned to: " + repoDir.getCanonicalPath());
            try {
                File f = new File(repoDir, filePath);
                if (!f.exists() || f.canWrite()) {
                    try (Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8")) {
                        w.write(new String(contents, "utf-8"));
                    }

                    try {
                        AddCommand add = activeRepo.add();
                        add.addFilepattern(filePath);
                        add.call();

                        CommitCommand commit = activeRepo.commit();
                        commit.setMessage(commitMessage);
                        commit.call();

                        // Push the changes
                        GitUtils.push(gitSource.getRemote(), repo, getCredential(), LOCAL_REF_BASE + sourceBranch, REMOTE_REF_BASE + branch);
                    } catch (GitAPIException ex) {
                        throw new ServiceException.UnexpectedErrorException(ex.getMessage(), ex);
                    }

                    return null;
                }
                throw new ServiceException.UnexpectedErrorException("Unable to write " + filePath);
            } finally {
                FileUtils.deleteDirectory(repoDir);
            }
        }
    });
}
 
Example #5
Source File: JGitOperator.java    From verigreen with Apache License 2.0 5 votes vote down vote up
@Override
public void add(String itemToAdd) {
    
    AddCommand command = _git.add();
    command.addFilepattern(itemToAdd);
    try {
        command.call();
    } catch (Throwable e) {
        throw new RuntimeException(String.format("Failed to add  [%s]", itemToAdd), e);
    }
    
}
 
Example #6
Source File: RepositoryManagementServiceInternalImpl.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean commitResolution(String siteId, String commitMessage) throws CryptoException, ServiceLayerException {
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration);
    Repository repo = helper.getRepository(siteId, SANDBOX);
    logger.debug("Commit resolution for merge conflict for site " + siteId);
    try (Git git = new Git(repo)) {
        Status status = git.status().call();

        logger.debug("Add all uncommitted changes/files");
        AddCommand addCommand = git.add();
        for (String uncommited : status.getUncommittedChanges()) {
            addCommand.addFilepattern(uncommited);
        }
        addCommand.call();
        logger.debug("Commit changes");
        CommitCommand commitCommand = git.commit();
        String userName = securityService.getCurrentUser();
        User user = userServiceInternal.getUserByIdOrUsername(-1, userName);
        PersonIdent personIdent = helper.getAuthorIdent(user);
        String prologue = studioConfiguration.getProperty(REPO_COMMIT_MESSAGE_PROLOGUE);
        String postscript = studioConfiguration.getProperty(REPO_COMMIT_MESSAGE_POSTSCRIPT);

        StringBuilder sbMessage = new StringBuilder();
        if (StringUtils.isNotEmpty(prologue)) {
            sbMessage.append(prologue).append("\n\n");
        }
        sbMessage.append(commitMessage);
        if (StringUtils.isNotEmpty(postscript)) {
            sbMessage.append("\n\n").append(postscript);
        }
        commitCommand.setCommitter(personIdent).setAuthor(personIdent).setMessage(sbMessage.toString()).call();
        return true;
    } catch (GitAPIException | UserNotFoundException | ServiceLayerException e) {
        logger.error("Error while committing conflict resolution for site " + siteId, e);
        throw new ServiceLayerException("Error while committing conflict resolution for site " + siteId, e);
    }
}
 
Example #7
Source File: ForgeTestSupport.java    From fabric8-forge with Apache License 2.0 4 votes vote down vote up
protected Build assertCodeChangeTriggersWorkingBuild(final String projectName, Build firstBuild) throws Exception {
    File cloneDir = new File(getBasedir(), "target/projects/" + projectName);

    String gitUrl = asserGetAppGitCloneURL(forgeClient, projectName);
    Git git = ForgeClientAsserts.assertGitCloneRepo(gitUrl, cloneDir);

    // lets make a dummy commit...
    File readme = new File(cloneDir, "ReadMe.md");
    boolean mustAdd = false;
    String text = "";
    if (readme.exists()) {
        text = IOHelpers.readFully(readme);
    } else {
        mustAdd = true;
    }
    text += "\nupdated at: " + new Date();
    Files.writeToFile(readme, text, Charset.defaultCharset());

    if (mustAdd) {
        AddCommand add = git.add().addFilepattern("*").addFilepattern(".");
        add.call();
    }


    LOG.info("Committing change to " + readme);

    CommitCommand commit = git.commit().setAll(true).setAuthor(forgeClient.getPersonIdent()).setMessage("dummy commit to trigger a rebuild");
    commit.call();
    PushCommand command = git.push();
    command.setCredentialsProvider(forgeClient.createCredentialsProvider());
    command.setRemote("origin").call();

    LOG.info("Git pushed change to " + readme);

    // now lets wait for the next build to start
    int nextBuildNumber = firstBuild.getNumber() + 1;


    Asserts.assertWaitFor(10 * 60 * 1000, new Block() {
        @Override
        public void invoke() throws Exception {
            JobWithDetails job = assertJob(projectName);
            Build lastBuild = job.getLastBuild();
            assertThat(lastBuild.getNumber()).describedAs("Waiting for latest build for job " + projectName + " to start").isGreaterThanOrEqualTo(nextBuildNumber);
        }
    });

    return ForgeClientAsserts.assertBuildCompletes(forgeClient, projectName);
}
 
Example #8
Source File: GitControl.java    From juneau with Apache License 2.0 4 votes vote down vote up
public void addToRepo() throws IOException, NoFilepatternException, GitAPIException {
	AddCommand add = git.add();
	add.addFilepattern(".").call();
}