Example usage for org.eclipse.jgit.lib Constants HEAD

List of usage examples for org.eclipse.jgit.lib Constants HEAD

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants HEAD.

Prototype

String HEAD

To view the source code for org.eclipse.jgit.lib Constants HEAD.

Click Source Link

Document

Special name for the "HEAD" symbolic-ref.

Usage

From source file:fr.duminy.tools.jgit.JGitToolbox.java

License:Open Source License

public String track(Parameters parameters) throws GitToolboxException {
    try {//from   w  ww. j a  va2 s.c o  m
        Git targetGit = Git.open(parameters.getGitDirectory());
        ProgressMonitor progressMonitor = new TextProgressMonitor();
        PullCommand pullCommand = targetGit.pull().setProgressMonitor(progressMonitor);
        PullResult result = pullCommand.call();
        System.out.println(result);
        if (!result.isSuccessful()) {
            throw new GitToolboxException("Failed to update tracking branch : " + result.toString());
        }

        MergeResult.MergeStatus mergeStatus = result.getMergeResult().getMergeStatus();
        if (!ALREADY_UP_TO_DATE.equals(mergeStatus) && !FAST_FORWARD.equals(mergeStatus)) {
            throw new GitToolboxException("Failed to update tracking branch : " + result.toString());
        }

        return targetGit.getRepository().getRef(Constants.HEAD).getName();
    } catch (Exception e) {
        throw new GitToolboxException("Error while updating tracking branch", e);
    }
}

From source file:fr.duminy.tools.jgit.JGitToolboxTest.java

License:Open Source License

private static Ref getHead(Git targetGit) throws IOException {
    return targetGit.getRepository().getRef(Constants.HEAD);
}

From source file:GitBackend.GitAPI.java

License:Apache License

private static HashMap getRevisionsByLog(Repository repository, String filePath) {

    HashMap commitMap = new HashMap<String, DateTime>();

    Git git = new Git(repository);
    LogCommand logCommand = null;/*from w w w . j a v a2  s . co m*/
    try {
        logCommand = git.log().add(git.getRepository().resolve(Constants.HEAD)).addPath(filePath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        for (RevCommit revCommit : logCommand.call()) {
            DateTime commitDate = new DateTime(1000L * revCommit.getCommitTime());
            // Store commit hash and date
            commitMap.put(revCommit.getName(), commitDate);
        }
    } catch (GitAPIException e) {
        e.printStackTrace();
    }

    return commitMap;
}

From source file:GitBackend.GitAPI.java

License:Apache License

private void getCommitByExactDate(Repository repo, Date execDate, String path) {
    RevWalk walk = new RevWalk(repo);
    try {/*from   www  . ja  va  2  s.  co m*/
        walk.markStart(walk.parseCommit(repo.resolve(Constants.HEAD)));

        walk.sort(RevSort.COMMIT_TIME_DESC);
        walk.setTreeFilter(PathFilter.create(path));

        for (RevCommit commit : walk) {
            if (commit.getCommitterIdent().getWhen().equals(execDate)) {
                // this is the commit you are looking for
                walk.parseCommit(commit);
                System.out.println("Commit found: " + commit.getName());
                break;
            }
        }
        walk.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:git_manager.tool.GitOperations.java

License:Open Source License

public void resetHard() {
    ResetCommand reset = git.reset();/*from w  w  w .ja  v a  2  s . c om*/
    reset.setRef(Constants.HEAD);
    //      reset.addPath(".");
    reset.setMode(ResetType.HARD);
    try {
        // Though this should actually have GitManager.frame as the parent, I think 
        // having editor as the parent is far more convenient, since I observe that I generally 
        // tend to keep tool window at the corner and the processing IDE in the centre, 
        // and prefer the dialog box displaying at the center... I think...
        int x = Messages.showYesNoQuestion(editor, "Reset sketch to previous commit?",
                "Are you sure you want to reset the entire skecthbook to<br>the exact state it was in the previous commit?",
                "All changes made since then will be permanently lost.");
        if (x == JOptionPane.YES_OPTION) {
            x = Messages.showYesNoQuestion(editor, "Really reset sketch to previous commit?",
                    "Are you absolutely, positively sure you want to reset the entire<br>skecthbook to the exact state it was in the previous commit?",
                    "All changes made since then will be permanently lost, and even<br>git can't recover them.");
            if (x == JOptionPane.YES_OPTION) {
                reset.call();
                System.out
                        .println("Hard reset completed. Sketch is now exactly like the last snapshot/commit.");
            }
        }
    } catch (CheckoutConflictException e) {
        e.printStackTrace();
    } catch (GitAPIException e) {
        e.printStackTrace();
    }
}

From source file:hd3gtv.configuration.GitInfo.java

License:Open Source License

public GitInfo(File repository_file) throws IOException {
    if (repository_file == null) {
        throw new NullPointerException("\"repository_file\" can't to be null");
    }//from www  . j a va2 s. co m
    if (repository_file.exists() == false) {
        throw new IOException("Can't found \"" + repository_file + "\"");
    }
    if (repository_file.isDirectory() == false) {
        throw new IOException("\"" + repository_file + "\" is not a directory");
    }

    try {
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(repository_file).readEnvironment().findGitDir().build();
        if (repository.getBranch() == null) {
            throw new FileNotFoundException("Can't found branch in \"" + repository_file + "\"");
        }
        branch = repository.getBranch();
        commit = repository.getRef(Constants.HEAD).getObjectId().abbreviate(8).name();
    } catch (Exception e) {
        throw new IOException("Can't load git repository \"" + repository_file + "\"");
    }
}

From source file:hudson.plugins.git.GitPublisherTest.java

License:Open Source License

@Test
public void testMergeAndPush() throws Exception {
    FreeStyleProject project = setupSimpleProject("master");

    GitSCM scm = new GitSCM(remoteConfigs(), Collections.singletonList(new BranchSpec("*")), false,
            Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList());
    scm.getExtensions().add(new PreBuildMerge(new UserMergeOptions("origin", "integration", null, null)));
    scm.getExtensions().add(new LocalBranch("integration"));
    project.setScm(scm);/*from   ww w.  j a v  a2  s. co  m*/

    project.getPublishersList()
            .add(new GitPublisher(Collections.<TagToPush>emptyList(),
                    Collections.singletonList(new BranchToPush("origin", "integration")),
                    Collections.<NoteToPush>emptyList(), true, true, false));

    // create initial commit and then run the build against it:
    commitNewFile("commitFileBase");
    testGitClient.branch("integration");
    build(project, Result.SUCCESS, "commitFileBase");

    testGitClient.checkout(null, "topic1");
    final String commitFile1 = "commitFile1";
    commitNewFile(commitFile1);
    final FreeStyleBuild build1 = build(project, Result.SUCCESS, commitFile1);
    assertTrue(build1.getWorkspace().child(commitFile1).exists());

    String sha1 = getHeadRevision(build1, "integration");
    assertEquals(sha1, testGitClient.revParse(Constants.HEAD).name());

}

From source file:hudson.plugins.git.GitPublisherTest.java

License:Open Source License

@Issue("JENKINS-12402")
@Test//from  www. j a va2 s  . c  o  m
public void testMergeAndPushFF() throws Exception {
    FreeStyleProject project = setupSimpleProject("master");

    GitSCM scm = new GitSCM(remoteConfigs(), Collections.singletonList(new BranchSpec("*")), false,
            Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList());
    scm.getExtensions().add(new PreBuildMerge(
            new UserMergeOptions("origin", "integration", null, MergeCommand.GitPluginFastForwardMode.FF)));
    scm.getExtensions().add(new LocalBranch("integration"));
    project.setScm(scm);

    project.getPublishersList()
            .add(new GitPublisher(Collections.<TagToPush>emptyList(),
                    Collections.singletonList(new BranchToPush("origin", "integration")),
                    Collections.<NoteToPush>emptyList(), true, true, false));

    // create initial commit and then run the build against it:
    commitNewFile("commitFileBase");
    testGitClient.branch("integration");
    final FreeStyleBuild build1 = build(project, Result.SUCCESS, "commitFileBase");
    assertTrue(build1.getWorkspace().child("commitFileBase").exists());
    String shaIntegration = getHeadRevision(build1, "integration");
    assertEquals("the integration branch should be at HEAD", shaIntegration,
            testGitClient.revParse(Constants.HEAD).name());

    // create a new branch and build, this results in a fast-forward merge
    testGitClient.checkout("master");
    ObjectId master = testGitClient.revParse("HEAD");
    testGitClient.branch("branch1");
    testGitClient.checkout("branch1");
    final String commitFile1 = "commitFile1";
    commitNewFile(commitFile1);
    String shaBranch1 = testGitClient.revParse("branch1").name();
    final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile1);

    // Test that the build (including publish) performed as expected.
    //   - commitFile1 is in the workspace
    //   - HEAD and integration should line up with branch1 like so:
    //     * f4d190c (HEAD, integration, branch1) Commit number 1
    //     * f787536 (master) Initial Commit
    //
    assertTrue(build2.getWorkspace().child("commitFile1").exists());
    shaIntegration = getHeadRevision(build2, "integration");
    String shaHead = testGitClient.revParse(Constants.HEAD).name();
    assertEquals("the integration branch and branch1 should line up", shaIntegration, shaBranch1);
    assertEquals("the integration branch should be at HEAD", shaIntegration, shaHead);
    // integration should have master as the parent commit
    List<ObjectId> revList = testGitClient.revList("integration^1");
    ObjectId integrationParent = revList.get(0);
    assertEquals("Fast-forward merge should have had master as a parent", master, integrationParent);

    // create a second branch off of master, so as to force a merge commit and to test
    // that --ff gracefully falls back to a merge commit
    testGitClient.checkout("master");
    testGitClient.branch("branch2");
    testGitClient.checkout("branch2");
    final String commitFile2 = "commitFile2";
    commitNewFile(commitFile2);
    String shaBranch2 = testGitClient.revParse("branch2").name();
    final FreeStyleBuild build3 = build(project, Result.SUCCESS, commitFile2);

    // Test that the build (including publish) performed as expected
    //   - commitFile1 is in the workspace
    //   - commitFile2 is in the workspace
    //   - the integration branch has branch1 and branch2 as parents, like so:
    //   *   f9b37d8 (integration) Merge commit '96a11fd...' into integration
    //   |\
    //   | * 96a11fd (HEAD, branch2) Commit number 2
    //   * | f4d190c (branch1) Commit number 1
    //   |/
    //   * f787536 (master) Initial Commit
    //
    assertTrue(build1.getWorkspace().child(commitFile1).exists());
    assertTrue(build1.getWorkspace().child(commitFile2).exists());
    // the integration branch should have branch1 and branch2 as parents
    revList = testGitClient.revList("integration^1");
    assertEquals("Integration should have branch1 as a parent", revList.get(0).name(), shaBranch1);
    revList = testGitClient.revList("integration^2");
    assertEquals("Integration should have branch2 as a parent", revList.get(0).name(), shaBranch2);
}

From source file:hudson.plugins.git.GitPublisherTest.java

License:Open Source License

@Issue("JENKINS-12402")
@Test/*  w ww.ja v  a2 s.  c o m*/
public void testMergeAndPushNoFF() throws Exception {
    FreeStyleProject project = setupSimpleProject("master");

    GitSCM scm = new GitSCM(remoteConfigs(), Collections.singletonList(new BranchSpec("*")), false,
            Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList());
    scm.getExtensions().add(new PreBuildMerge(
            new UserMergeOptions("origin", "integration", null, MergeCommand.GitPluginFastForwardMode.NO_FF)));
    scm.getExtensions().add(new LocalBranch("integration"));
    project.setScm(scm);

    project.getPublishersList()
            .add(new GitPublisher(Collections.<TagToPush>emptyList(),
                    Collections.singletonList(new BranchToPush("origin", "integration")),
                    Collections.<NoteToPush>emptyList(), true, true, false));

    // create initial commit and then run the build against it:
    commitNewFile("commitFileBase");
    testGitClient.branch("integration");
    final FreeStyleBuild build1 = build(project, Result.SUCCESS, "commitFileBase");
    assertTrue(build1.getWorkspace().child("commitFileBase").exists());
    String shaIntegration = getHeadRevision(build1, "integration");
    assertEquals("integration branch should be at HEAD", shaIntegration,
            testGitClient.revParse(Constants.HEAD).name());

    // create a new branch and build
    // This would be a fast-forward merge, but we're calling for --no-ff and that should work
    testGitClient.checkout("master");
    ObjectId master = testGitClient.revParse("HEAD");
    testGitClient.branch("branch1");
    testGitClient.checkout("branch1");
    final String commitFile1 = "commitFile1";
    commitNewFile(commitFile1);
    String shaBranch1 = testGitClient.revParse("branch1").name();
    final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile1);
    ObjectId mergeCommit = testGitClient.revParse("integration");

    // Test that the build and publish performed as expected.
    //   - commitFile1 is in the workspace
    //   - integration has branch1 and master as parents, like so:
    //     *   6913e57 (integration) Merge commit '257e33c...' into integration
    //     |\
    //     | * 257e33c (HEAD, branch1) Commit number 1
    //     |/
    //     * 3066c87 (master) Initial Commit
    //
    assertTrue(build2.getWorkspace().child("commitFile1").exists());
    List<ObjectId> revList = testGitClient.revList("integration^1");
    assertEquals("Integration should have master as a parent", revList.get(0), master);
    revList = testGitClient.revList("integration^2");
    assertEquals("Integration should have branch1 as a parent", revList.get(0).name(), shaBranch1);

    // create a second branch off of master, so as to test that --no-ff is published as expected
    testGitClient.checkout("master");
    testGitClient.branch("branch2");
    testGitClient.checkout("branch2");
    final String commitFile2 = "commitFile2";
    commitNewFile(commitFile2);
    String shaBranch2 = testGitClient.revParse("branch2").name();
    final FreeStyleBuild build3 = build(project, Result.SUCCESS, commitFile2);

    // Test that the build performed as expected
    //   - commitFile1 is in the workspace
    //   - commitFile2 is in the workspace
    //   - the integration branch has branch1 and the previous merge commit as parents, like so:
    //     *   5908447 (integration) Merge commit '157fd0b...' into integration
    //     |\
    //     | * 157fd0b (HEAD, branch2) Commit number 2
    //     * |   7afa661 Merge commit '0a37dd6...' into integration
    //     |\ \
    //     | |/
    //     |/|
    //     | * 0a37dd6 (branch1) Commit number 1
    //     |/
    //     * a5dda1a (master) Initial Commit
    //
    assertTrue("commitFile1 should exist in the workspace", build1.getWorkspace().child(commitFile1).exists());
    assertTrue("commitFile2 should exist in the workspace", build1.getWorkspace().child(commitFile2).exists());
    // the integration branch should have branch1 and branch2 as parents
    revList = testGitClient.revList("integration^1");
    assertEquals("Integration should have the first merge commit as a parent", revList.get(0), mergeCommit);
    revList = testGitClient.revList("integration^2");
    assertEquals("Integration should have branch2 as a parent", revList.get(0).name(), shaBranch2);
}

From source file:hudson.plugins.git.GitPublisherTest.java

License:Open Source License

@Issue("JENKINS-12402")
@Test//from  www . j a  va  2  s.  c om
public void testMergeAndPushFFOnly() throws Exception {
    FreeStyleProject project = setupSimpleProject("master");

    GitSCM scm = new GitSCM(remoteConfigs(), Collections.singletonList(new BranchSpec("*")), false,
            Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList());
    scm.getExtensions().add(new PreBuildMerge(new UserMergeOptions("origin", "integration", null,
            MergeCommand.GitPluginFastForwardMode.FF_ONLY)));
    scm.getExtensions().add(new LocalBranch("integration"));
    project.setScm(scm);

    project.getPublishersList()
            .add(new GitPublisher(Collections.<TagToPush>emptyList(),
                    Collections.singletonList(new BranchToPush("origin", "integration")),
                    Collections.<NoteToPush>emptyList(), true, true, false));

    // create initial commit and then run the build against it:
    commitNewFile("commitFileBase");
    testGitClient.branch("integration");
    final FreeStyleBuild build1 = build(project, Result.SUCCESS, "commitFileBase");
    assertTrue(build1.getWorkspace().child("commitFileBase").exists());
    String shaIntegration = getHeadRevision(build1, "integration");
    assertEquals("integration should be at HEAD", shaIntegration,
            testGitClient.revParse(Constants.HEAD).name());

    // create a new branch and build
    // This merge can work with --ff-only
    testGitClient.checkout("master");
    ObjectId master = testGitClient.revParse("HEAD");
    testGitClient.branch("branch1");
    testGitClient.checkout("branch1");
    final String commitFile1 = "commitFile1";
    commitNewFile(commitFile1);
    String shaBranch1 = testGitClient.revParse("branch1").name();
    final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile1);
    ObjectId mergeCommit = testGitClient.revParse("integration");

    // Test that the build (including publish) performed as expected.
    //   - commitFile1 is in the workspace
    //   - HEAD and integration should line up with branch1 like so:
    //     * f4d190c (HEAD, integration, branch1) Commit number 1
    //     * f787536 (master) Initial Commit
    //
    assertTrue("commitFile1 should exist in the worksapce",
            build2.getWorkspace().child("commitFile1").exists());
    shaIntegration = getHeadRevision(build2, "integration");
    String shaHead = testGitClient.revParse(Constants.HEAD).name();
    assertEquals("integration and branch1 should line up", shaIntegration, shaBranch1);
    assertEquals("integration and head should line up", shaIntegration, shaHead);
    // integration should have master as the parent commit
    List<ObjectId> revList = testGitClient.revList("integration^1");
    ObjectId integrationParent = revList.get(0);
    assertEquals("Fast-forward merge should have had master as a parent", master, integrationParent);

    // create a second branch off of master, so as to force a merge commit
    // but the publish will fail as --ff-only cannot work with a parallel branch
    testGitClient.checkout("master");
    testGitClient.branch("branch2");
    testGitClient.checkout("branch2");
    final String commitFile2 = "commitFile2";
    commitNewFile(commitFile2);
    String shaBranch2 = testGitClient.revParse("branch2").name();
    final FreeStyleBuild build3 = build(project, Result.FAILURE, commitFile2);

    // Test that the publish did not merge the branches
    //   - The workspace will contain commitFile2, but not branch1's file (commitFile1)
    //   - The repository will be left with branch2 unmerged like so:
    //     * c19a55d (HEAD, branch2) Commit number 2
    //     | * 79c49b2 (integration, branch1) Commit number 1
    //     |/
    //     * ebffeb3 (master) Initial Commit
    assertFalse("commitFile1 should not exist in the worksapce",
            build2.getWorkspace().child("commitFile1").exists());
    assertTrue("commitFile2 should exist in the worksapce",
            build2.getWorkspace().child("commitFile2").exists());
    revList = testGitClient.revList("branch2^1");
    assertEquals("branch2 should have master as a parent", revList.get(0), master);
    try {
        revList = testGitClient.revList("branch2^2");
        assertTrue("branch2 should have no other parent than master", false);
    } catch (java.lang.NullPointerException err) {
        // expected
    }
}