Example usage for org.eclipse.jgit.lib ObjectId fromString

List of usage examples for org.eclipse.jgit.lib ObjectId fromString

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectId fromString.

Prototype

public static ObjectId fromString(String str) 

Source Link

Document

Convert an ObjectId from hex characters.

Usage

From source file:com.gitblit.plugin.smartticketbranches.SmartTicketBranchesHook.java

License:Apache License

@Override
public void onUpdateTicket(TicketModel ticket, Change change) {
    if (!ticket.hasPatchsets()) {
        // ticket has no patchsets, nothing to do
        return;/*from  w ww.jav a2 s  .c  o  m*/
    }

    if (!change.isStatusChange()) {
        // not a status change, nothing to do
        return;
    }

    final Patchset ps = ticket.getCurrentPatchset();
    final String branch = PatchsetCommand.getTicketBranch(ticket.number);
    final IRepositoryManager repositoryManager = GitblitContext.getManager(IRepositoryManager.class);
    final Repository repo = repositoryManager.getRepository(ticket.repository);
    try {
        switch (change.getStatus()) {
        case New:
            // NOOP, new proposal
            log.debug("new proposal, skipping");
            break;
        case Open:
            /*
             *  Open or Re-open: create branch, if not exists
             */
            if (null == repo.getRef(branch)) {
                log.debug("ticket re-opened, trying to create '{}'", branch);
                RefUpdate ru = repo.updateRef(branch);
                ru.setExpectedOldObjectId(ObjectId.zeroId());
                ru.setNewObjectId(ObjectId.fromString(ps.tip));

                RevWalk rw = null;
                try {
                    rw = new RevWalk(repo);
                    RefUpdate.Result result = ru.update(rw);
                    switch (result) {
                    case NEW:
                        log.info(String.format("%s ticket RE-OPENED, created %s:%s", name, ticket.repository,
                                branch));
                        break;
                    default:
                        log.error(String.format("%s failed to re-create %s:%s (%s)", name, ticket.repository,
                                branch, result));
                        break;
                    }
                } finally {
                    if (rw != null) {
                        rw.release();
                    }
                }
            }
            break;
        default:
            /*
             * Ticket closed: delete branch, if exists
             */
            log.debug("ticket closed, trying to remove '{}'", branch);
            RefUpdate ru = repo.updateRef(branch);
            ru.setExpectedOldObjectId(ObjectId.fromString(ps.tip));
            ru.setNewObjectId(ObjectId.zeroId());
            ru.setForceUpdate(true);

            RefUpdate.Result result = ru.delete();
            switch (result) {
            case FORCED:
                log.info(String.format("%s ticket %s, removed %s:%s", name, change.getStatus(),
                        ticket.repository, branch));
                break;
            default:
                log.error(String.format("%s failed to remove %s:%s (%s)", name, ticket.repository, branch,
                        result));
                break;
            }
        }
    } catch (IOException e) {
        log.error(null, e);
    } finally {
        if (repo != null) {
            repo.close();
        }
    }
}

From source file:com.gitblit.plugin.tbacl.Tests.java

License:Apache License

protected void push(User username, Branch branch, boolean expectSuccess) {

    RepositoryModel repo = new RepositoryModel();
    repo.name = "test.git";
    repo.owners = Arrays.asList(User.owner.name());

    IGitblit gitblit = new MockGitblit();
    gitblit.getSettings().overrideSetting(Plugin.SETTING_USE_TEAM_NAMESPACES, branch.useNamespace());

    try {//from  w  ww . j  a  va2  s.  co m
        gitblit.updateRepositoryModel(repo.name, repo, false);
    } catch (GitBlitException e) {
    }

    UserModel user = gitblit.getUserModel(username.name());
    Repository db = gitblit.getRepository(repo.name);

    GitblitReceivePack rp = new GitblitReceivePack(gitblit, db, repo, user);

    ObjectId sha1 = ObjectId.fromString("2d291de884b4bb3164fda516ebc8510f757495b7");
    ObjectId sha2 = ObjectId.fromString("42972d830611fa4b1aa2c2c49c824a15e1987597");

    List<ReceiveCommand> commands = Arrays
            .asList(new ReceiveCommand(sha1, sha2, "refs/heads/" + branch.toString()));

    TBACLReceiveHook hook = new TBACLReceiveHook();
    hook.onPreReceive(rp, commands);

    for (ReceiveCommand cmd : commands) {
        assertEquals(cmd.getMessage(), expectSuccess, Result.NOT_ATTEMPTED == cmd.getResult());
    }
}

From source file:com.gitblit.tests.GroovyScriptTest.java

License:Apache License

@Test
public void testFogbugz() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"),
            ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
    commands.add(new ReceiveCommand(ObjectId.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"),
            ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master2"));

    RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
    repository.customFields = new HashMap<String, String>();
    repository.customFields.put("fogbugzUrl", "http://bugs.test.com");
    repository.customFields.put("fogbugzRepositoryId", "1");
    repository.customFields.put("fogbugzCommitMessageRegex",
            "\\s*[Bb][Uu][Gg][(Zz)(Ss)]*\\s*[(IDs)]*\\s*[#:; ]+((\\d+[ ,:;#]*)+)");

    test("fogbugz.groovy", gitblit, logger, clientLogger, commands, repository);
}

From source file:com.gitblit.tests.GroovyScriptTest.java

License:Apache License

@Test
public void testSendHtmlMail() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"),
            ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
    commands.add(new ReceiveCommand(ObjectId.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"),
            ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master2"));

    RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
    repository.mailingLists.add("list@helloworld.git");

    test("sendmail-html.groovy", gitblit, logger, clientLogger, commands, repository);
    assertEquals(1, logger.messages.size());
    assertEquals(1, gitblit.messages.size());
    MockMail m = gitblit.messages.get(0);
    assertEquals(5, m.toAddresses.size());
    assertTrue(m.message.contains("BIT"));
    assertTrue(m.message.contains("<html>"));
}

From source file:com.gitblit.tests.GroovyScriptTest.java

License:Apache License

@Test
public void testSendMail() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"),
            ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
    commands.add(new ReceiveCommand(ObjectId.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"),
            ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master2"));

    RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
    repository.mailingLists.add("list@helloworld.git");

    test("sendmail.groovy", gitblit, logger, clientLogger, commands, repository);
    assertEquals(1, logger.messages.size());
    assertEquals(1, gitblit.messages.size());
    MockMail m = gitblit.messages.get(0);
    assertEquals(5, m.toAddresses.size());
    assertTrue(m.message.contains("BIT"));
}

From source file:com.gitblit.tests.GroovyScriptTest.java

License:Apache License

@Test
public void testProtectRefsCreateBranch() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.zeroId(),
            ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));

    RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());

    test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
}

From source file:com.gitblit.tests.GroovyScriptTest.java

License:Apache License

@Test
public void testProtectRefsCreateTag() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.zeroId(),
            ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/tags/v1.0"));

    RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());

    test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
    assertEquals(0, logger.messages.size());
}

From source file:com.gitblit.tests.GroovyScriptTest.java

License:Apache License

@Test
public void testProtectRefsFastForward() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"),
            ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));

    RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());

    test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
    assertEquals(0, logger.messages.size());
}

From source file:com.gitblit.tests.GroovyScriptTest.java

License:Apache License

@Test
public void testProtectRefsDeleteMasterBranch() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    ReceiveCommand command = new ReceiveCommand(ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"),
            ObjectId.zeroId(), "refs/heads/master");
    commands.add(command);/*from  w w w .ja  v  a  2 s  .  c o m*/

    RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());

    test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
    assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
    assertEquals(0, logger.messages.size());
}

From source file:com.gitblit.tests.GroovyScriptTest.java

License:Apache License

@Test
public void testProtectRefsDeleteOtherBranch() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"),
            ObjectId.zeroId(), "refs/heads/other"));

    RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());

    test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
    assertEquals(0, logger.messages.size());
}