Example usage for org.eclipse.jgit.api Git Git

List of usage examples for org.eclipse.jgit.api Git Git

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git Git.

Prototype

public Git(Repository repo) 

Source Link

Document

Construct a new org.eclipse.jgit.api.Git object which can interact with the specified git repository.

Usage

From source file:com.mangosolutions.rcloud.rawgist.repository.git.BareCommitCommand.java

/**
 * Executes the {@code commit} command with all the options and parameters
 * collected by the setter methods of this class. Each instance of this
 * class should only be used for one invocation of the command (means: one
 * call to {@link #call()})//from  w ww . ja v  a 2s  .  c  o m
 *
 * @return a {@link RevCommit} object representing the successful commit.
 * @throws NoHeadException
 *             when called on a git repo without a HEAD reference
 * @throws NoMessageException
 *             when called without specifying a commit message
 * @throws UnmergedPathsException
 *             when the current index contained unmerged paths (conflicts)
 * @throws ConcurrentRefUpdateException
 *             when HEAD or branch ref is updated concurrently by someone
 *             else
 * @throws WrongRepositoryStateException
 *             when repository is not in the right state for committing
 * @throws AbortedByHookException
 *             if there are either pre-commit or commit-msg hooks present in
 *             the repository and one of them rejects the commit.
 */
public RevCommit call() throws GitAPIException, NoHeadException, NoMessageException, UnmergedPathsException,
        ConcurrentRefUpdateException, WrongRepositoryStateException, AbortedByHookException {
    checkCallable();
    Collections.sort(only);

    try (RevWalk rw = new RevWalk(repo)) {
        RepositoryState state = repo.getRepositoryState();

        if (!noVerify) {
            Hooks.preCommit(repo, hookOutRedirect).call();
        }

        processOptions(state, rw);

        if (all && !repo.isBare()) {
            try (Git git = new Git(repo)) {
                git.add().addFilepattern(".") //$NON-NLS-1$
                        .setUpdate(true).call();
            } catch (NoFilepatternException e) {
                // should really not happen
                throw new JGitInternalException(e.getMessage(), e);
            }
        }

        Ref head = repo.findRef(Constants.HEAD);
        if (head == null) {
            throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
        }

        // determine the current HEAD and the commit it is referring to
        ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
        if (headId == null && amend)
            throw new WrongRepositoryStateException(JGitText.get().commitAmendOnInitialNotPossible);

        if (headId != null) {
            if (amend) {
                RevCommit previousCommit = rw.parseCommit(headId);
                for (RevCommit p : previousCommit.getParents())
                    parents.add(p.getId());
                if (author == null)
                    author = previousCommit.getAuthorIdent();
            } else {
                parents.add(0, headId);
            }
        }
        if (!noVerify) {
            message = Hooks.commitMsg(repo, hookOutRedirect).setCommitMessage(message).call();
        }

        // lock the index
        //         DirCache index = repo.lockDirCache();
        index.lock();
        try (ObjectInserter odi = repo.newObjectInserter()) {
            if (!only.isEmpty())
                index = createTemporaryIndex(headId, index, rw);

            // Write the index as tree to the object database. This may
            // fail for example when the index contains unmerged paths
            // (unresolved conflicts)
            ObjectId indexTreeId = index.writeTree(odi);

            if (insertChangeId)
                insertChangeId(indexTreeId);

            // Check for empty commits
            if (headId != null && !allowEmpty.booleanValue()) {
                RevCommit headCommit = rw.parseCommit(headId);
                headCommit.getTree();
                if (indexTreeId.equals(headCommit.getTree())) {
                    return null;
                }
            }

            // Create a Commit object, populate it and write it
            CommitBuilder commit = new CommitBuilder();
            commit.setCommitter(committer);
            commit.setAuthor(author);
            commit.setMessage(message);

            commit.setParentIds(parents);
            commit.setTreeId(indexTreeId);
            ObjectId commitId = odi.insert(commit);
            odi.flush();

            RevCommit revCommit = rw.parseCommit(commitId);
            RefUpdate ru = repo.updateRef(Constants.HEAD);
            ru.setNewObjectId(commitId);
            if (reflogComment != null) {
                ru.setRefLogMessage(reflogComment, false);
            } else {
                String prefix = amend ? "commit (amend): " //$NON-NLS-1$
                        : parents.size() == 0 ? "commit (initial): " //$NON-NLS-1$
                                : "commit: "; //$NON-NLS-1$
                ru.setRefLogMessage(prefix + revCommit.getShortMessage(), false);
            }
            if (headId != null) {
                ru.setExpectedOldObjectId(headId);
            } else {
                ru.setExpectedOldObjectId(ObjectId.zeroId());
            }
            Result rc = ru.forceUpdate();
            switch (rc) {
            case NEW:
            case FORCED:
            case FAST_FORWARD: {
                setCallable(false);
                if (state == RepositoryState.MERGING_RESOLVED || isMergeDuringRebase(state)) {
                    // Commit was successful. Now delete the files
                    // used for merge commits
                    repo.writeMergeCommitMsg(null);
                    repo.writeMergeHeads(null);
                } else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) {
                    repo.writeMergeCommitMsg(null);
                    repo.writeCherryPickHead(null);
                } else if (state == RepositoryState.REVERTING_RESOLVED) {
                    repo.writeMergeCommitMsg(null);
                    repo.writeRevertHead(null);
                }
                return revCommit;
            }
            case REJECTED:
            case LOCK_FAILURE:
                throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
            default:
                throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                        Constants.HEAD, commitId.toString(), rc));
            }
        } finally {
            index.unlock();
        }
    } catch (UnmergedPathException e) {
        throw new UnmergedPathsException(e);
    } catch (IOException e) {
        throw new JGitInternalException(JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e);
    }
}

From source file:com.meltmedia.cadmium.core.git.GitService.java

License:Apache License

public GitService(Repository repository) {
    git = new Git(repository);
}

From source file:com.meltmedia.cadmium.core.git.GitService.java

License:Apache License

public static GitService init(String site, String dir) throws Exception {
    String repoPath = dir + "/" + site;
    log.debug("Repository Path :" + repoPath);
    Repository repo = new FileRepository(repoPath + "/.git");
    Git git = null;/*  ww w.  jav a 2s  . c  o  m*/
    try {
        repo.create();
        git = new Git(repo);

        File localGitRepo = new File(repoPath);
        localGitRepo.mkdirs();
        new File(localGitRepo, "delete.me").createNewFile();

        git.add().addFilepattern("delete.me").call();
        git.commit().setMessage("initial commit").call();
        return new GitService(git);
    } catch (IllegalStateException e) {
        log.debug("Repo Already exists locally");
        if (repo != null) {
            repo.close();
        }
    }
    return null;
}

From source file:com.microsoft.gittf.core.tasks.CheckinHeadCommitTask.java

License:Open Source License

/**
 * Builds the commit comment to use when checking in
 * /*  ww w .  j  a  v a  2s .co m*/
 * @param commitDelta
 * @return
 */
private String buildCommitComment(CommitDelta commitDelta) {
    /* In deep mode the task will use the ToCommit message */
    if (deep) {
        /*
         * If the meta data flag was set to true, then build the meta data
         * string
         */
        if (includeMetaDataInComment) {
            return buildCommitComment(commitDelta.getToCommit());
        }
        /* Otherwise just use the full message */
        else {
            return commitDelta.getToCommit().getFullMessage();
        }
    }

    /*
     * In Shallow mode use the log command to identify all the commit
     * included in the delta
     */
    try {
        /*
         * TODO: Need to replace this code to use better logic to figure out
         * the included commits topologically and not chronologically
         */

        LogCommand logCommand = new Git(repository).log();
        logCommand.addRange(commitDelta.getFromCommit().getId(), commitDelta.getToCommit().getId());
        logCommand.setMaxCount(OutputConstants.DEFAULT_MAXCOMMENTROLLUP + 1);
        Iterable<RevCommit> commits = logCommand.call();

        int commitCounter = 0;

        StringBuilder comment = new StringBuilder();

        comment.append(Messages.formatString("CheckinHeadCommitTask.ShallowCheckinRollupFormat", //$NON-NLS-1$
                ObjectIdUtil.abbreviate(repository, commitDelta.getToCommit().getId()),
                ObjectIdUtil.abbreviate(repository, commitDelta.getFromCommit().getId()))
                + OutputConstants.NEW_LINE);
        comment.append(OutputConstants.NEW_LINE);

        for (RevCommit commit : commits) {
            commitCounter++;

            if (commitCounter > OutputConstants.DEFAULT_MAXCOMMENTROLLUP) {
                comment.append(Messages.formatString(
                        "CheckinHeadCommitTask.ShallowCheckinCommentDisplayTruncatedFormat", //$NON-NLS-1$
                        OutputConstants.DEFAULT_MAXCOMMENTROLLUP,
                        ObjectIdUtil.abbreviate(repository, commit.getId()),
                        ObjectIdUtil.abbreviate(repository, commitDelta.getFromCommit().getId())));

                break;
            }

            comment.append(buildCommitComment(commit) + OutputConstants.NEW_LINE);
        }

        if (commitCounter == 1) {
            return buildCommitComment(commitDelta.getToCommit());
        }

        return comment.toString();
    } catch (Exception e) {
        // if we fail execute the log command we default to the destination
        // commit full message

        return buildCommitComment(commitDelta.getToCommit());
    }
}

From source file:com.microsoft.gittf.core.tasks.CloneTaskTest.java

License:Open Source License

@Test
public void testShallowCloneFilesAndFolders() throws Exception {
    URI projectCollectionURI = new URI("http://fakeCollection:8080/tfs/DefaultCollection"); //$NON-NLS-1$
    String tfsPath = "$/project"; //$NON-NLS-1$
    String gitRepositoryPath = Util.getRepositoryFile(getName()).getAbsolutePath();

    final MockVersionControlService mockVersionControlService = new MockVersionControlService();

    mockVersionControlService.AddFile("$/project/folder/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file0.txt", 1); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file1.txt", 2); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file2.txt", 3); //$NON-NLS-1$

    Calendar date = Calendar.getInstance();
    date.set(2012, 11, 12, 18, 15);/*from  w  w  w. ja v  a  2  s .  co  m*/

    MockChangesetProperties changesetProperties = new MockChangesetProperties("ownerDisplayName", //$NON-NLS-1$
            "ownerName", //$NON-NLS-1$
            "committerDisplayName", //$NON-NLS-1$
            "committerName", //$NON-NLS-1$
            "comment", //$NON-NLS-1$
            date);
    mockVersionControlService.updateChangesetInformation(changesetProperties, 3);

    final Repository repository = RepositoryUtil.createNewRepository(gitRepositoryPath, false);
    final boolean defaultDeep = repository.getConfig().getInt(ConfigurationConstants.CONFIGURATION_SECTION,
            ConfigurationConstants.GENERAL_SUBSECTION, ConfigurationConstants.DEPTH,
            GitTFConstants.GIT_TF_SHALLOW_DEPTH) > 1;

    CloneTask cloneTask = new CloneTask(projectCollectionURI, mockVersionControlService, tfsPath, repository);
    TaskStatus cloneTaskStatus = cloneTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(cloneTaskStatus.isOK());

    // Load Git Repo
    Git git = new Git(repository);
    git.checkout().setName("master").call(); //$NON-NLS-1$

    // Verify Changeset 1
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file0.txt"), //$NON-NLS-1$
            "$/project/folder/file0.txt", //$NON-NLS-1$
            1));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file0.txt"), //$NON-NLS-1$
            "$/project/folder2/file0.txt", //$NON-NLS-1$
            1));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file0.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file0.txt", //$NON-NLS-1$
            1));

    // Verify Changeset 2
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file1.txt"), //$NON-NLS-1$
            "$/project/folder/file1.txt", //$NON-NLS-1$
            2));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file1.txt"), //$NON-NLS-1$
            "$/project/folder2/file1.txt", //$NON-NLS-1$
            2));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file1.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file1.txt", //$NON-NLS-1$
            2));

    // Verify Changeset 3
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file2.txt"), //$NON-NLS-1$
            "$/project/folder/file2.txt", //$NON-NLS-1$
            3));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file2.txt"), //$NON-NLS-1$
            "$/project/folder2/file2.txt", //$NON-NLS-1$
            3));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file2.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file2.txt", //$NON-NLS-1$
            3));

    // Verify Git Repo configuration
    GitTFConfiguration gitRepoServerConfig = GitTFConfiguration.loadFrom(repository);

    assertEquals(gitRepoServerConfig.getServerURI(), projectCollectionURI);
    assertEquals(gitRepoServerConfig.getServerPath(), tfsPath);
    assertEquals(gitRepoServerConfig.getDeep(), defaultDeep);

    // Verify the number of commits
    Iterable<RevCommit> commits = git.log().call();

    assertNotNull(commits);

    int commitCounter = 0;
    for (RevCommit commit : commits) {
        assertEquals(commit.getFullMessage(), "comment"); //$NON-NLS-1$

        PersonIdent ownwer = commit.getAuthorIdent();
        assertEquals(ownwer.getEmailAddress(), "ownerName"); //$NON-NLS-1$
        assertEquals(ownwer.getName(), "ownerDisplayName"); //$NON-NLS-1$

        PersonIdent committer = commit.getCommitterIdent();
        assertEquals(committer.getEmailAddress(), "committerName"); //$NON-NLS-1$
        assertEquals(committer.getName(), "committerDisplayName"); //$NON-NLS-1$

        commitCounter++;
    }

    assertEquals(commitCounter, 1);

    // Verify the tags
    List<Ref> tags = git.tagList().call();
    assertEquals(1, tags.size());
}

From source file:com.microsoft.gittf.core.tasks.CloneTaskTest.java

License:Open Source License

@Test
public void testDeepCloneFilesAndFoldersSimple() throws Exception {
    URI projectCollectionURI = new URI("http://fakeCollection:8080/tfs/DefaultCollection"); //$NON-NLS-1$
    String tfsPath = "$/project"; //$NON-NLS-1$
    String gitRepositoryPath = Util.getRepositoryFile(getName()).getAbsolutePath();

    final MockVersionControlService mockVersionControlService = new MockVersionControlService();

    mockVersionControlService.AddFile("$/project/folder/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file0.txt", 1); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file1.txt", 2); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file2.txt", 3); //$NON-NLS-1$

    Calendar date = Calendar.getInstance();
    date.set(2012, 11, 12, 18, 15);//from  w  w  w .  j av a2  s  .c  om

    MockChangesetProperties changesetProperties = new MockChangesetProperties("ownerDisplayName1", //$NON-NLS-1$
            "ownerName1", //$NON-NLS-1$
            "committerDisplayName1", //$NON-NLS-1$
            "committerName1", //$NON-NLS-1$
            "comment1", //$NON-NLS-1$
            date);

    mockVersionControlService.updateChangesetInformation(changesetProperties, 1);

    changesetProperties = new MockChangesetProperties("ownerDisplayName2", //$NON-NLS-1$
            "ownerName2", //$NON-NLS-1$
            "committerDisplayName2", //$NON-NLS-1$
            "committerName2", //$NON-NLS-1$
            "comment2", //$NON-NLS-1$
            date);

    mockVersionControlService.updateChangesetInformation(changesetProperties, 2);

    changesetProperties = new MockChangesetProperties("ownerDisplayName3", //$NON-NLS-1$
            "ownerName3", //$NON-NLS-1$
            "committerDisplayName3", //$NON-NLS-1$
            "committerName3", //$NON-NLS-1$
            "comment3", //$NON-NLS-1$
            date);

    mockVersionControlService.updateChangesetInformation(changesetProperties, 3);

    final Repository repository = RepositoryUtil.createNewRepository(gitRepositoryPath, false);
    final boolean defaultDeep = repository.getConfig().getInt(ConfigurationConstants.CONFIGURATION_SECTION,
            ConfigurationConstants.GENERAL_SUBSECTION, ConfigurationConstants.DEPTH,
            GitTFConstants.GIT_TF_SHALLOW_DEPTH) > 1;

    CloneTask cloneTask = new CloneTask(projectCollectionURI, mockVersionControlService, tfsPath, repository);
    cloneTask.setDepth(10);

    TaskStatus cloneTaskStatus = cloneTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(cloneTaskStatus.isOK());

    // Load Git Repo
    Git git = new Git(repository);
    git.checkout().setName("master").call(); //$NON-NLS-1$

    // Verify Changeset 1
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file0.txt"), //$NON-NLS-1$
            "$/project/folder/file0.txt", //$NON-NLS-1$
            1));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file0.txt"), //$NON-NLS-1$
            "$/project/folder2/file0.txt", //$NON-NLS-1$
            1));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file0.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file0.txt", //$NON-NLS-1$
            1));

    // Verify Changeset 2
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file1.txt"), //$NON-NLS-1$
            "$/project/folder/file1.txt", //$NON-NLS-1$
            2));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file1.txt"), //$NON-NLS-1$
            "$/project/folder2/file1.txt", //$NON-NLS-1$
            2));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file1.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file1.txt", //$NON-NLS-1$
            2));

    // Verify Changeset 3
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file2.txt"), //$NON-NLS-1$
            "$/project/folder/file2.txt", //$NON-NLS-1$
            3));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file2.txt"), //$NON-NLS-1$
            "$/project/folder2/file2.txt", //$NON-NLS-1$
            3));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file2.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file2.txt", //$NON-NLS-1$
            3));

    // Verify Git Repo configuration
    GitTFConfiguration gitRepoServerConfig = GitTFConfiguration.loadFrom(repository);

    assertEquals(gitRepoServerConfig.getServerURI(), projectCollectionURI);
    assertEquals(gitRepoServerConfig.getServerPath(), tfsPath);
    assertEquals(gitRepoServerConfig.getDeep(), defaultDeep);

    // Verify the number of commits
    Iterable<RevCommit> commits = git.log().call();

    assertNotNull(commits);

    int commitCounter = 3;
    for (RevCommit commit : commits) {
        assertEquals(commit.getFullMessage(), "comment" + Integer.toString(commitCounter)); //$NON-NLS-1$

        PersonIdent ownwer = commit.getAuthorIdent();
        assertEquals(ownwer.getEmailAddress(), "ownerName" + Integer.toString(commitCounter)); //$NON-NLS-1$
        assertEquals(ownwer.getName(), "ownerDisplayName" + Integer.toString(commitCounter)); //$NON-NLS-1$

        PersonIdent committer = commit.getCommitterIdent();
        assertEquals(committer.getEmailAddress(), "committerName" + Integer.toString(commitCounter)); //$NON-NLS-1$
        assertEquals(committer.getName(), "committerDisplayName" + Integer.toString(commitCounter)); //$NON-NLS-1$

        commitCounter--;
    }

    assertEquals(commitCounter, 0);

    // Verify the tags
    List<Ref> tags = git.tagList().call();
    assertEquals(3, tags.size());
}

From source file:com.microsoft.gittf.core.tasks.FetchTaskTest.java

License:Open Source License

@Test
public void testFetchShallow() throws Exception {
    URI projectCollectionURI = new URI("http://fakeCollection:8080/tfs/DefaultCollection"); //$NON-NLS-1$
    String tfsPath = "$/project"; //$NON-NLS-1$
    String gitRepositoryPath = Util.getRepositoryFile(getName()).getAbsolutePath();

    final MockVersionControlService mockVersionControlService = new MockVersionControlService();

    mockVersionControlService.AddFile("$/project/folder/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file0.txt", 1); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file1.txt", 2); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file2.txt", 3); //$NON-NLS-1$

    Calendar date = Calendar.getInstance();
    date.set(2012, 11, 12, 18, 15);//from w  w w.j  av a  2  s  .  c  o  m

    MockChangesetProperties changesetProperties = new MockChangesetProperties("ownerDisplayName", //$NON-NLS-1$
            "ownerName", //$NON-NLS-1$
            "committerDisplayName", //$NON-NLS-1$
            "committerName", //$NON-NLS-1$
            "comment", //$NON-NLS-1$
            date);
    mockVersionControlService.updateChangesetInformation(changesetProperties, 3);

    final Repository repository = RepositoryUtil.createNewRepository(gitRepositoryPath, false);

    CloneTask cloneTask = new CloneTask(projectCollectionURI, mockVersionControlService, tfsPath, repository);
    TaskStatus cloneTaskStatus = cloneTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(cloneTaskStatus.isOK());

    // Update some files
    mockVersionControlService.AddFile("$/project/folder/file1.txt", 4); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file1.txt", 4); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file1.txt", 4); //$NON-NLS-1$

    MockChangesetProperties changesetProperties2 = new MockChangesetProperties("ownerDisplayName4", //$NON-NLS-1$
            "ownerName4", //$NON-NLS-1$
            "committerDisplayName4", //$NON-NLS-1$
            "committerName4", //$NON-NLS-1$
            "comment4", //$NON-NLS-1$
            date);
    mockVersionControlService.updateChangesetInformation(changesetProperties2, 4);

    FetchTask fetchTask = new FetchTask(repository, mockVersionControlService);
    TaskStatus fetchTaskStatus = fetchTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(fetchTaskStatus.isOK());

    // Verify the commit fetched
    Ref fetchHeadRef = repository.getRef(Constants.FETCH_HEAD);
    Ref headRef = repository.getRef(Constants.HEAD);

    assertNotNull(fetchHeadRef);
    assertNotNull(headRef);

    ObjectId fetchHeadCommitID = fetchHeadRef.getObjectId();
    ObjectId headCommitID = headRef.getObjectId();

    RevWalk revWalk = new RevWalk(repository);
    RevCommit fetchedCommit = revWalk.parseCommit(fetchHeadCommitID);
    RevCommit headCommit = revWalk.parseCommit(headCommitID);

    assertEquals(fetchedCommit.getFullMessage(), "comment4"); //$NON-NLS-1$

    PersonIdent ownwer = fetchedCommit.getAuthorIdent();
    assertEquals(ownwer.getEmailAddress(), "ownerName4"); //$NON-NLS-1$
    assertEquals(ownwer.getName(), "ownerDisplayName4"); //$NON-NLS-1$

    PersonIdent committer = fetchedCommit.getCommitterIdent();
    assertEquals(committer.getEmailAddress(), "committerName4"); //$NON-NLS-1$
    assertEquals(committer.getName(), "committerDisplayName4"); //$NON-NLS-1$

    // Verify the file content
    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.setRecursive(true);

    treeWalk.addTree(headCommit.getTree());
    treeWalk.addTree(fetchedCommit.getTree());

    treeWalk.setFilter(TreeFilter.ANY_DIFF);

    int count = 0;
    while (treeWalk.next()) {
        ObjectId fileObjectId = treeWalk.getObjectId(1);
        byte[] fileContent = repository.getObjectDatabase().open(fileObjectId, OBJ_BLOB).getBytes();

        switch (count) {
        case 0:
            assertTrue(mockVersionControlService.verifyFileContent(fileContent, "$/project/folder/file1.txt", //$NON-NLS-1$
                    4));
            break;
        case 2:
            assertTrue(mockVersionControlService.verifyFileContent(fileContent, "$/project/folder2/file1.txt", //$NON-NLS-1$
                    4));
            break;
        case 1:
            assertTrue(mockVersionControlService.verifyFileContent(fileContent,
                    "$/project/folder/nestedFolder/file1.txt", //$NON-NLS-1$
                    4));
            break;
        }

        count++;
    }

    Git git = new Git(repository);

    // Verify the tags
    List<Ref> tags = git.tagList().call();
    assertEquals(2, tags.size());
}

From source file:com.microsoft.gittf.core.tasks.FetchTaskTest.java

License:Open Source License

@Test
public void testFetchDeep() throws Exception {
    URI projectCollectionURI = new URI("http://fakeCollection:8080/tfs/DefaultCollection"); //$NON-NLS-1$
    String tfsPath = "$/project"; //$NON-NLS-1$
    String gitRepositoryPath = Util.getRepositoryFile(getName()).getAbsolutePath();

    final MockVersionControlService mockVersionControlService = new MockVersionControlService();

    mockVersionControlService.AddFile("$/project/folder/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file0.txt", 1); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file1.txt", 2); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file2.txt", 3); //$NON-NLS-1$

    Calendar date = Calendar.getInstance();
    date.set(2012, 11, 12, 18, 15);//from   w ww.j av  a2 s  .c  o m

    MockChangesetProperties changesetProperties = new MockChangesetProperties("ownerDisplayName", //$NON-NLS-1$
            "ownerName", //$NON-NLS-1$
            "committerDisplayName", //$NON-NLS-1$
            "committerName", //$NON-NLS-1$
            "comment", //$NON-NLS-1$
            date);
    mockVersionControlService.updateChangesetInformation(changesetProperties, 3);

    final Repository repository = RepositoryUtil.createNewRepository(gitRepositoryPath, false);

    CloneTask cloneTask = new CloneTask(projectCollectionURI, mockVersionControlService, tfsPath, repository);
    TaskStatus cloneTaskStatus = cloneTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(cloneTaskStatus.isOK());

    // Update some files
    mockVersionControlService.AddFile("$/project/folder/file1.txt", 4); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file1.txt", 4); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file1.txt", 4); //$NON-NLS-1$

    MockChangesetProperties changesetProperties2 = new MockChangesetProperties("ownerDisplayName4", //$NON-NLS-1$
            "ownerName4", //$NON-NLS-1$
            "committerDisplayName4", //$NON-NLS-1$
            "committerName4", //$NON-NLS-1$
            "comment4", //$NON-NLS-1$
            date);
    mockVersionControlService.updateChangesetInformation(changesetProperties2, 4);

    mockVersionControlService.AddFile("$/project/folder/file1.txt", 5); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file1.txt", 5); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file1.txt", 5); //$NON-NLS-1$

    MockChangesetProperties changesetProperties3 = new MockChangesetProperties("ownerDisplayName5", //$NON-NLS-1$
            "ownerName5", //$NON-NLS-1$
            "committerDisplayName5", //$NON-NLS-1$
            "committerName5", //$NON-NLS-1$
            "comment5", //$NON-NLS-1$
            date);
    mockVersionControlService.updateChangesetInformation(changesetProperties3, 5);

    FetchTask fetchTask = new FetchTask(repository, mockVersionControlService);
    fetchTask.setDeep(true);
    TaskStatus fetchTaskStatus = fetchTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(fetchTaskStatus.isOK());

    // Retrieve commits
    Ref fetchHeadRef = repository.getRef(Constants.FETCH_HEAD);
    Ref headRef = repository.getRef(Constants.HEAD);

    assertNotNull(fetchHeadRef);
    assertNotNull(headRef);

    ObjectId fetchHeadCommitID = fetchHeadRef.getObjectId();
    ObjectId headCommitID = headRef.getObjectId();

    RevWalk revWalk = new RevWalk(repository);
    RevCommit fetchedCommit = revWalk.parseCommit(fetchHeadCommitID);
    RevCommit headCommit = revWalk.parseCommit(headCommitID);

    assertEquals(fetchedCommit.getParentCount(), 1);

    RevCommit intermediateCommit = revWalk.parseCommit(fetchedCommit.getParent(0).getId());

    // Verify intermediateCommit
    assertEquals(intermediateCommit.getFullMessage(), "comment4"); //$NON-NLS-1$

    PersonIdent ownwer = intermediateCommit.getAuthorIdent();
    assertEquals(ownwer.getEmailAddress(), "ownerName4"); //$NON-NLS-1$
    assertEquals(ownwer.getName(), "ownerDisplayName4"); //$NON-NLS-1$

    PersonIdent committer = intermediateCommit.getCommitterIdent();
    assertEquals(committer.getEmailAddress(), "committerName4"); //$NON-NLS-1$
    assertEquals(committer.getName(), "committerDisplayName4"); //$NON-NLS-1$

    // Verify fetch_head commit
    assertEquals(fetchedCommit.getFullMessage(), "comment5"); //$NON-NLS-1$

    ownwer = fetchedCommit.getAuthorIdent();
    assertEquals(ownwer.getEmailAddress(), "ownerName5"); //$NON-NLS-1$
    assertEquals(ownwer.getName(), "ownerDisplayName5"); //$NON-NLS-1$

    committer = fetchedCommit.getCommitterIdent();
    assertEquals(committer.getEmailAddress(), "committerName5"); //$NON-NLS-1$
    assertEquals(committer.getName(), "committerDisplayName5"); //$NON-NLS-1$

    // Verify the file content
    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.setRecursive(true);

    treeWalk.addTree(headCommit.getTree());
    treeWalk.addTree(intermediateCommit.getTree());

    treeWalk.setFilter(TreeFilter.ANY_DIFF);

    int count = 0;
    while (treeWalk.next()) {
        ObjectId fileObjectId = treeWalk.getObjectId(1);
        byte[] fileContent = repository.getObjectDatabase().open(fileObjectId, OBJ_BLOB).getBytes();

        switch (count) {
        case 0:
            assertTrue(mockVersionControlService.verifyFileContent(fileContent, "$/project/folder/file1.txt", //$NON-NLS-1$
                    4));
            break;
        case 2:
            assertTrue(mockVersionControlService.verifyFileContent(fileContent, "$/project/folder2/file1.txt", //$NON-NLS-1$
                    4));
            break;
        case 1:
            assertTrue(mockVersionControlService.verifyFileContent(fileContent,
                    "$/project/folder/nestedFolder/file1.txt", //$NON-NLS-1$
                    4));
            break;
        }

        count++;
    }

    treeWalk.reset();
    treeWalk.setRecursive(true);

    treeWalk.addTree(headCommit.getTree());
    treeWalk.addTree(fetchedCommit.getTree());

    treeWalk.setFilter(TreeFilter.ANY_DIFF);

    count = 0;
    while (treeWalk.next()) {
        ObjectId fileObjectId = treeWalk.getObjectId(1);
        byte[] fileContent = repository.getObjectDatabase().open(fileObjectId, OBJ_BLOB).getBytes();

        switch (count) {
        case 0:
            assertTrue(mockVersionControlService.verifyFileContent(fileContent, "$/project/folder/file1.txt", //$NON-NLS-1$
                    5));
            break;
        case 2:
            assertTrue(mockVersionControlService.verifyFileContent(fileContent, "$/project/folder2/file1.txt", //$NON-NLS-1$
                    5));
            break;
        case 1:
            assertTrue(mockVersionControlService.verifyFileContent(fileContent,
                    "$/project/folder/nestedFolder/file1.txt", //$NON-NLS-1$
                    5));
            break;
        }

        count++;
    }

    Git git = new Git(repository);

    // Verify the tags
    List<Ref> tags = git.tagList().call();
    assertEquals(3, tags.size());
}

From source file:com.microsoft.gittf.core.tasks.pendDiff.CheckinAnalysisChangeCollectionTest.java

License:Open Source License

private void initRepository() throws Exception {
    URI projectCollectionURI = new URI("http://fakeCollection:8080/tfs/DefaultCollection"); //$NON-NLS-1$
    String tfsPath = "$/project"; //$NON-NLS-1$
    String gitRepositoryPath = Util.getRepositoryFile(getName()).getAbsolutePath();

    final MockVersionControlService mockVersionControlService = new MockVersionControlService();

    mockVersionControlService.AddFile("$/project/root/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/file3.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/file3.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/file3.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/file3.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/greatGrandChild/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/greatGrandChild/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/greatGrandChild/file3.txt", 1); //$NON-NLS-1$

    Calendar date = Calendar.getInstance();
    date.set(2012, 11, 12, 18, 15);//from  w w w  .  j  a  v a 2  s  . c o  m
    MockChangesetProperties changesetProperties = new MockChangesetProperties("ownerDisplayName", //$NON-NLS-1$
            "ownerName", //$NON-NLS-1$
            "committerDisplayName", //$NON-NLS-1$
            "committerName", //$NON-NLS-1$
            "comment", //$NON-NLS-1$
            date);

    mockVersionControlService.updateChangesetInformation(changesetProperties, 1);

    repository = RepositoryUtil.createNewRepository(gitRepositoryPath, false);

    CloneTask cloneTask = new CloneTask(projectCollectionURI, mockVersionControlService, tfsPath, repository);
    TaskStatus cloneTaskStatus = cloneTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(cloneTaskStatus.isOK());

    Git git = new Git(repository);
    initialCommit = git.log().call().iterator().next();

    // Verify the first commit exists
    assertFalse(ObjectId.zeroId().equals(initialCommit));
}

From source file:com.microsoft.gittf.core.tasks.pendDiff.CheckinAnalysisChangeCollectionTest.java

License:Open Source License

private void add(String folder) throws NoFilepatternException, GitAPIException {
    Git git = new Git(repository);

    git.add().setUpdate(false).addFilepattern(folder).call();
}