Search in sources :

Example 46 with Repository

use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.

the class DiffUtilsTest method testBlame.

@Test
public void testBlame() throws Exception {
    Repository repository = GitBlitSuite.getHelloworldRepository();
    List<AnnotatedLine> lines = DiffUtils.blame(repository, "java.java", "1d0c2933a4ae69c362f76797d42d6bd182d05176");
    repository.close();
    assertTrue(lines.size() > 0);
    assertEquals("c6d31dccf5cc75e8e46299fc62d38f60ec6d41e0", lines.get(0).commitId);
}
Also used : AnnotatedLine(com.gitblit.models.AnnotatedLine) Repository(org.eclipse.jgit.lib.Repository) Test(org.junit.Test)

Example 47 with Repository

use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.

the class DiffUtilsTest method testFilePatch.

@Test
public void testFilePatch() throws Exception {
    Repository repository = GitBlitSuite.getHelloworldRepository();
    RevCommit commit = JGitUtils.getCommit(repository, "1d0c2933a4ae69c362f76797d42d6bd182d05176");
    String patch = DiffUtils.getCommitPatch(repository, null, commit, "java.java");
    repository.close();
    assertTrue(patch != null && patch.length() > 0);
    String expected = "-		system.out.println(\"Hello World\");\n+		System.out.println(\"Hello World\"";
    assertTrue(patch.indexOf(expected) > -1);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 48 with Repository

use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.

the class DiffUtilsTest method testPlainFileDiff.

@Test
public void testPlainFileDiff() throws Exception {
    Repository repository = GitBlitSuite.getHelloworldRepository();
    RevCommit commit = JGitUtils.getCommit(repository, "1d0c2933a4ae69c362f76797d42d6bd182d05176");
    String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN, 3).content;
    repository.close();
    assertTrue(diff != null && diff.length() > 0);
    String expected = "-		system.out.println(\"Hello World\");\n+		System.out.println(\"Hello World\"";
    assertTrue(diff.indexOf(expected) > -1);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 49 with Repository

use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.

the class DashboardPage method addActivity.

protected void addActivity(UserModel user, Collection<RepositoryModel> repositories, String feedTitle, int daysBack) {
    Calendar c = Calendar.getInstance();
    c.add(Calendar.DATE, -1 * daysBack);
    Date minimumDate = c.getTime();
    TimeZone timezone = getTimeZone();
    // create daily commit digest feed
    List<DailyLogEntry> digests = new ArrayList<DailyLogEntry>();
    for (RepositoryModel model : repositories) {
        if (model.isCollectingGarbage) {
            continue;
        }
        if (model.hasCommits && model.lastChange.after(minimumDate)) {
            Repository repository = app().repositories().getRepository(model.name);
            List<DailyLogEntry> entries = RefLogUtils.getDailyLogByRef(model.name, repository, minimumDate, timezone);
            digests.addAll(entries);
            repository.close();
        }
    }
    Fragment activityFragment = new Fragment("activity", "activityFragment", this);
    add(activityFragment);
    activityFragment.add(new Label("feedTitle", feedTitle));
    if (digests.size() == 0) {
        // quiet or no starred repositories
        if (repositories.size() == 0) {
            if (UserModel.ANONYMOUS.equals(user)) {
                if (daysBack == 1) {
                    activityFragment.add(new Label("digests", getString("gb.noActivityToday")));
                } else {
                    activityFragment.add(new Label("digests", MessageFormat.format(getString("gb.noActivity"), daysBack)));
                }
            } else {
                activityFragment.add(new LinkPanel("digests", null, getString("gb.findSomeRepositories"), RepositoriesPage.class));
            }
        } else {
            if (daysBack == 1) {
                activityFragment.add(new Label("digests", getString("gb.noActivityToday")));
            } else {
                activityFragment.add(new Label("digests", MessageFormat.format(getString("gb.noActivity"), daysBack)));
            }
        }
    } else {
        // show daily commit digest feed
        Collections.sort(digests);
        DigestsPanel digestsPanel = new DigestsPanel("digests", digests);
        activityFragment.add(digestsPanel);
    }
    // add the nifty charts
    if (!ArrayUtils.isEmpty(digests)) {
        // aggregate author exclusions
        Set<String> authorExclusions = new TreeSet<String>();
        for (String author : app().settings().getStrings(Keys.web.metricAuthorExclusions)) {
            authorExclusions.add(author.toLowerCase());
        }
        for (RepositoryModel model : repositories) {
            if (!ArrayUtils.isEmpty(model.metricAuthorExclusions)) {
                for (String author : model.metricAuthorExclusions) {
                    authorExclusions.add(author.toLowerCase());
                }
            }
        }
        addCharts(activityFragment, digests, authorExclusions, daysBack);
    } else {
        activityFragment.add(new Label("charts").setVisible(false));
        activityFragment.add(new Label("feedheader").setVisible(false));
    }
}
Also used : DigestsPanel(com.gitblit.wicket.panels.DigestsPanel) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) DailyLogEntry(com.gitblit.models.DailyLogEntry) Label(org.apache.wicket.markup.html.basic.Label) RepositoryModel(com.gitblit.models.RepositoryModel) Fragment(org.apache.wicket.markup.html.panel.Fragment) Date(java.util.Date) LinkPanel(com.gitblit.wicket.panels.LinkPanel) TimeZone(java.util.TimeZone) Repository(org.eclipse.jgit.lib.Repository) TreeSet(java.util.TreeSet)

Example 50 with Repository

use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.

the class NewRepositoryPage method initialCommit.

/**
	 * Prepare the initial commit for the repository.
	 *
	 * @param repository
	 * @param addReadme
	 * @param gitignore
	 * @param addGitFlow
	 * @return true if an initial commit was created
	 */
protected boolean initialCommit(RepositoryModel repository, boolean addReadme, String gitignore, boolean addGitFlow) {
    boolean initialCommit = addReadme || !StringUtils.isEmpty(gitignore) || addGitFlow;
    if (!initialCommit) {
        return false;
    }
    // build an initial commit
    boolean success = false;
    Repository db = app().repositories().getRepository(repositoryModel.name);
    ObjectInserter odi = db.newObjectInserter();
    try {
        UserModel user = GitBlitWebSession.get().getUser();
        String email = Optional.fromNullable(user.emailAddress).or(user.username + "@" + "gitblit");
        PersonIdent author = new PersonIdent(user.getDisplayName(), email);
        DirCache newIndex = DirCache.newInCore();
        DirCacheBuilder indexBuilder = newIndex.builder();
        if (addReadme) {
            // insert a README
            String title = StringUtils.stripDotGit(StringUtils.getLastPathElement(repositoryModel.name));
            String description = repositoryModel.description == null ? "" : repositoryModel.description;
            String readme = String.format("## %s\n\n%s\n\n", title, description);
            byte[] bytes = readme.getBytes(Constants.ENCODING);
            DirCacheEntry entry = new DirCacheEntry("README.md");
            entry.setLength(bytes.length);
            entry.setLastModified(System.currentTimeMillis());
            entry.setFileMode(FileMode.REGULAR_FILE);
            entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));
            indexBuilder.add(entry);
        }
        if (!StringUtils.isEmpty(gitignore)) {
            // insert a .gitignore file
            File dir = app().runtime().getFileOrFolder(Keys.git.gitignoreFolder, "${baseFolder}/gitignore");
            File file = new File(dir, gitignore + ".gitignore");
            if (file.exists() && file.length() > 0) {
                byte[] bytes = FileUtils.readContent(file);
                if (!ArrayUtils.isEmpty(bytes)) {
                    DirCacheEntry entry = new DirCacheEntry(".gitignore");
                    entry.setLength(bytes.length);
                    entry.setLastModified(System.currentTimeMillis());
                    entry.setFileMode(FileMode.REGULAR_FILE);
                    entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));
                    indexBuilder.add(entry);
                }
            }
        }
        if (addGitFlow) {
            // insert a .gitflow file
            Config config = new Config();
            config.setString("gitflow", null, "masterBranch", Constants.MASTER);
            config.setString("gitflow", null, "developBranch", Constants.DEVELOP);
            config.setString("gitflow", null, "featureBranchPrefix", "feature/");
            config.setString("gitflow", null, "releaseBranchPrefix", "release/");
            config.setString("gitflow", null, "hotfixBranchPrefix", "hotfix/");
            config.setString("gitflow", null, "supportBranchPrefix", "support/");
            config.setString("gitflow", null, "versionTagPrefix", "");
            byte[] bytes = config.toText().getBytes(Constants.ENCODING);
            DirCacheEntry entry = new DirCacheEntry(".gitflow");
            entry.setLength(bytes.length);
            entry.setLastModified(System.currentTimeMillis());
            entry.setFileMode(FileMode.REGULAR_FILE);
            entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));
            indexBuilder.add(entry);
        }
        indexBuilder.finish();
        if (newIndex.getEntryCount() == 0) {
            // nothing to commit
            return false;
        }
        ObjectId treeId = newIndex.writeTree(odi);
        // Create a commit object
        CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(author);
        commit.setCommitter(author);
        commit.setEncoding(Constants.ENCODING);
        commit.setMessage("Initial commit");
        commit.setTreeId(treeId);
        // Insert the commit into the repository
        ObjectId commitId = odi.insert(commit);
        odi.flush();
        // set the branch refs
        RevWalk revWalk = new RevWalk(db);
        try {
            // set the master branch
            RevCommit revCommit = revWalk.parseCommit(commitId);
            RefUpdate masterRef = db.updateRef(Constants.R_MASTER);
            masterRef.setNewObjectId(commitId);
            masterRef.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
            Result masterRC = masterRef.update();
            switch(masterRC) {
                case NEW:
                    success = true;
                    break;
                default:
                    success = false;
            }
            if (addGitFlow) {
                // set the develop branch for git-flow
                RefUpdate developRef = db.updateRef(Constants.R_DEVELOP);
                developRef.setNewObjectId(commitId);
                developRef.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                Result developRC = developRef.update();
                switch(developRC) {
                    case NEW:
                        success = true;
                        break;
                    default:
                        success = false;
                }
            }
        } finally {
            revWalk.close();
        }
    } catch (UnsupportedEncodingException e) {
        logger().error(null, e);
    } catch (IOException e) {
        logger().error(null, e);
    } finally {
        odi.close();
        db.close();
    }
    return success;
}
Also used : DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) ObjectId(org.eclipse.jgit.lib.ObjectId) Config(org.eclipse.jgit.lib.Config) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Result(org.eclipse.jgit.lib.RefUpdate.Result) UserModel(com.gitblit.models.UserModel) DirCache(org.eclipse.jgit.dircache.DirCache) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Aggregations

Repository (org.eclipse.jgit.lib.Repository)326 IOException (java.io.IOException)103 RevWalk (org.eclipse.jgit.revwalk.RevWalk)102 Test (org.junit.Test)81 RevCommit (org.eclipse.jgit.revwalk.RevCommit)76 ObjectId (org.eclipse.jgit.lib.ObjectId)72 File (java.io.File)43 TestRepository (org.eclipse.jgit.junit.TestRepository)40 Change (com.google.gerrit.reviewdb.client.Change)39 OrmException (com.google.gwtorm.server.OrmException)39 Ref (org.eclipse.jgit.lib.Ref)35 Project (com.google.gerrit.reviewdb.client.Project)32 ArrayList (java.util.ArrayList)31 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)27 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)26 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)24 Map (java.util.Map)23 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)23 RepositoryModel (com.gitblit.models.RepositoryModel)20 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)20