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

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

Introduction

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

Prototype

public ListTagCommand tagList() 

Source Link

Document

Return a command object used to list tags

Usage

From source file:com.chungkwong.jgitgui.TagListTreeItem.java

License:Open Source License

public TagListTreeItem(Git git) throws GitAPIException {
    super(java.util.ResourceBundle.getBundle("com/chungkwong/jgitgui/text").getString("TAG"));
    for (Ref ref : git.tagList().call())
        getChildren().add(new TagTreeItem(ref));
}

From source file:com.GitAnalytics.BranchInfo.java

public static List<BranchInfo> getBranches(Git git, List<CommitInfo> allCommits) throws Exception {
    List<BranchInfo> list = new LinkedList<>();

    List<Ref> tags = git.tagList().call();

    for (Ref branch : git.branchList().call()) {
        LinkedList commits = new LinkedList<>();

        Set<RevCommit> branchCommits = new HashSet<>();
        for (RevCommit commit : git.log().add(branch.getObjectId()).call()) {
            branchCommits.add(commit);//from w  w w  . j  a v  a 2s. c o m
        }

        allCommits.stream().filter((commit) -> (branchCommits.contains(commit.getCommit())))
                .forEachOrdered((commit) -> {
                    commits.add(commit);
                });

        list.add(new BranchInfo(branch, commits));
    }

    return list;
}

From source file:com.maiereni.synchronizer.git.utils.GitDownloaderImpl.java

License:Apache License

private GitResults downloadProject(final Git git, final GitProperties properties) throws Exception {
    GitResults ret = new GitResults();
    String refName = REF_HEADS + properties.getBranchName();
    if (StringUtils.isBlank(properties.getBranchName())) {
        refName = REF_HEADS_MASTER;//w  w w  .  jav  a 2s.co m
    }
    Ref tagRef = null;
    if (StringUtils.isNotBlank(properties.getTagName())) {
        String compTagName = "refs/tags/" + properties.getTagName();
        List<Ref> tags = git.tagList().call();
        for (Ref tag : tags) {
            String tn = tag.getName();
            if (tn.equals(compTagName)) {
                logger.debug("Download repository from: " + tag.getName());
                refName = tag.getName();
                tagRef = tag;
                break;
            }
        }
    }
    List<Ref> branches = git.branchList().call();
    Ref localBranch = null;
    for (Ref branch : branches) {
        String bn = branch.getName();
        if (bn.equals(REF_HEADS_LOCAL)) {
            localBranch = branch;
            break;
        }
    }
    if (localBranch == null) {
        localBranch = git.checkout().setCreateBranch(true).setName(LOCAL_BRANCH)
                .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).setStartPoint(refName).call();
        logger.debug("Created a local clone of the project repository ");
        ret.setCreated(true);
    } else {
        List<Change> updates = update(git, properties, localBranch, tagRef);
        ret.setChanges(updates);
    }
    return ret;
}

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  ww.  jav  a  2s.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);
    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  a  v a  2 s.com*/

    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);/*  w w w  . j av a  2 s.  com*/

    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);// ww  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, 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.spotify.docker.Utils.java

License:Apache License

public static String getGitCommitId()
        throws GitAPIException, DockerException, IOException, MojoExecutionException {

    final FileRepositoryBuilder builder = new FileRepositoryBuilder();
    builder.readEnvironment(); // scan environment GIT_* variables
    builder.findGitDir(); // scan up the file system tree

    if (builder.getGitDir() == null) {
        throw new MojoExecutionException("Cannot tag with git commit ID because directory not a git repo");
    }/*from  w ww . ja  v  a 2 s .  c  o  m*/

    final StringBuilder result = new StringBuilder();
    final Repository repo = builder.build();

    try {
        // get the first 7 characters of the latest commit
        final ObjectId head = repo.resolve("HEAD");
        result.append(head.getName().substring(0, 7));
        final Git git = new Git(repo);

        // append first git tag we find
        for (Ref gitTag : git.tagList().call()) {
            if (gitTag.getObjectId().equals(head)) {
                // name is refs/tag/name, so get substring after last slash
                final String name = gitTag.getName();
                result.append(".");
                result.append(name.substring(name.lastIndexOf('/') + 1));
                break;
            }
        }

        // append '.DIRTY' if any files have been modified
        final Status status = git.status().call();
        if (status.hasUncommittedChanges()) {
            result.append(".DIRTY");
        }
    } finally {
        repo.close();
    }

    return result.length() == 0 ? null : result.toString();
}

From source file:fr.brouillard.oss.jgitver.GitVersionCalculator.java

License:Apache License

private Version buildVersion(Git git, VersionStrategy strategy) {
    try {//  w w w.j  av  a 2 s.  c  o m
        //
        metadatas.registerMetadata(Metadatas.DIRTY, "" + GitUtils.isDirty(git));

        // retrieve all tags matching a version, and get all info for each of them
        List<Ref> allTags = git.tagList().call().stream().map(this::peel)
                .collect(Collectors.toCollection(ArrayList::new));
        // let's have tags sorted from most recent to oldest
        Collections.reverse(allTags);

        metadatas.registerMetadataTags(Metadatas.ALL_TAGS, allTags.stream());
        metadatas.registerMetadataTags(Metadatas.ALL_ANNOTATED_TAGS,
                allTags.stream().filter(GitUtils::isAnnotated));
        metadatas.registerMetadataTags(Metadatas.ALL_LIGHTWEIGHT_TAGS,
                allTags.stream().filter(as(GitUtils::isAnnotated).negate()));

        List<Ref> allVersionTags = allTags.stream().filter(strategy::considerTagAsAVersionOne)
                .collect(Collectors.toCollection(ArrayList::new));

        List<Ref> normals = allVersionTags.stream().filter(GitUtils::isAnnotated).collect(Collectors.toList());
        List<Ref> lights = allVersionTags.stream().filter(as(GitUtils::isAnnotated).negate())
                .collect(Collectors.toList());

        metadatas.registerMetadataTags(Metadatas.ALL_VERSION_TAGS, allVersionTags.stream());
        metadatas.registerMetadataTags(Metadatas.ALL_VERSION_ANNOTATED_TAGS, normals.stream());
        metadatas.registerMetadataTags(Metadatas.ALL_VERSION_LIGHTWEIGHT_TAGS, lights.stream());

        ObjectId rootId = repository.resolve("HEAD");

        // handle a call on an empty git repository
        if (rootId == null) {
            // no HEAD exist
            // the GIT repo might just be initialized without any commit
            return Version.EMPTY_REPOSITORY_VERSION;
        }

        git.log().add(rootId).setMaxCount(1).call().spliterator().tryAdvance(rc -> {
            PersonIdent commitInfo = rc.getAuthorIdent();
            metadatas.registerMetadata(Metadatas.HEAD_COMMITTER_NAME, commitInfo.getName());
            metadatas.registerMetadata(Metadatas.HEAD_COMMITER_EMAIL, commitInfo.getEmailAddress());
            dtfmt.setTimeZone(commitInfo.getTimeZone());
            metadatas.registerMetadata(Metadatas.HEAD_COMMIT_DATETIME, dtfmt.format(commitInfo.getWhen()));
        });

        metadatas.registerMetadataTags(Metadatas.HEAD_TAGS, tagsOf(allTags, rootId).stream());
        metadatas.registerMetadataTags(Metadatas.HEAD_ANNOTATED_TAGS,
                tagsOf(allTags.stream().filter(GitUtils::isAnnotated).collect(Collectors.toList()), rootId)
                        .stream());
        metadatas.registerMetadataTags(Metadatas.HEAD_LIGHTWEIGHT_TAGS,
                tagsOf(allTags.stream().filter(as(GitUtils::isAnnotated).negate()).collect(Collectors.toList()),
                        rootId).stream());

        metadatas.registerMetadata(Metadatas.GIT_SHA1_FULL, rootId.getName());
        metadatas.registerMetadata(Metadatas.GIT_SHA1_8, rootId.getName().substring(0, 8));

        Commit head = new Commit(rootId, 0, tagsOf(normals, rootId), tagsOf(lights, rootId));
        List<Commit> commits = new LinkedList<>();

        try (RevWalk revWalk = new RevWalk(repository)) {
            revWalk.markStart(revWalk.parseCommit(rootId));

            int depth = 0;
            ObjectId id = null;
            for (RevCommit rc : revWalk) {
                id = rc.getId();

                List<Ref> annotatedCommitTags = tagsOf(normals, id);
                List<Ref> lightCommitTags = tagsOf(lights, id);

                if (annotatedCommitTags.size() > 0 || lightCommitTags.size() > 0) {
                    // we found a commit with version tags
                    Commit c = new Commit(id, depth, annotatedCommitTags, lightCommitTags);
                    commits.add(c);

                    // shall we stop searching for commits
                    if (StrategySearchMode.STOP_AT_FIRST.equals(strategy.searchMode())) {
                        break; // let's stop
                    } else if (depth >= strategy.searchDepthLimit()) {
                        break; // let's stop
                    }
                }

                depth++;
            }

            // handle the case where we reached the first commit without finding anything
            if (commits.size() == 0) {
                commits.add(new Commit(id, depth - 1, Collections.emptyList(), Collections.emptyList()));
            }
        }

        return strategy.build(head, commits);
    } catch (Exception ex) {
        throw new IllegalStateException("failure calculating version", ex);
    }
}

From source file:fr.treeptik.cloudunit.utils.GitUtils.java

License:Open Source License

/**
 * this method is associate with listGitTagsOfApplication() method
 * which list all tags with index, this is this index which must pass as parammeter of this method
 *
 * @param application/*from   ww w  .ja v a2 s.c om*/
 * @param indexChosen
 * @param dockerManagerAddress
 * @param containerGitAddress
 * @return
 * @throws InvalidRemoteException
 * @throws TransportException
 * @throws GitAPIException
 * @throws IOException
 */
public static List<String> resetOnChosenGitTag(Application application, int indexChosen,
        String dockerManagerAddress, String containerGitAddress)
        throws InvalidRemoteException, TransportException, GitAPIException, IOException {
    User user = application.getUser();
    String sshPort = application.getServers().get(0).getSshPort();
    String password = user.getPassword();
    String userNameGit = user.getLogin();
    String dockerManagerIP = dockerManagerAddress.substring(0, dockerManagerAddress.length() - 5);
    String remoteRepository = "ssh://" + userNameGit + "@" + dockerManagerIP + ":" + sshPort
            + containerGitAddress;
    File gitworkDir = Files.createTempDirectory("clone").toFile();
    CloneCommand clone = Git.cloneRepository();
    clone.setDirectory(gitworkDir);

    CredentialsProvider credentialsProvider = configCredentialsForGit(userNameGit, password);
    clone.setCredentialsProvider(credentialsProvider);
    clone.setURI(remoteRepository);
    Git git = clone.call();

    ListTagCommand listTagCommand = git.tagList();
    List<Ref> listRefs = listTagCommand.call();

    Ref ref = listRefs.get(indexChosen);

    ResetCommand resetCommand = git.reset();
    resetCommand.setMode(ResetType.HARD);
    resetCommand.setRef(ref.getName());
    resetCommand.call();

    PushCommand pushCommand = git.push();
    pushCommand.setCredentialsProvider(credentialsProvider);
    pushCommand.setForce(true);

    List<PushResult> listPushResults = (List<PushResult>) pushCommand.call();
    List<String> listPushResultsMessages = new ArrayList<>();
    for (PushResult pushResult : listPushResults) {
        listPushResultsMessages.add(pushResult.getMessages());
    }
    FilesUtils.deleteDirectory(gitworkDir);
    return listPushResultsMessages;
}