James Moger
2013-12-03 5842db3a8fa1c2e0a3163e535a9c20c09fdc6601
Simplify bare repository bugtraq config loading

Change-Id: I223df72df371ad6e5559f57da16167e56b1e66ad
1 files modified
46 ■■■■ changed files
src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java 46 ●●●● patch | view | raw | blame | history
src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java
@@ -29,10 +29,8 @@
 */
package com.syntevo.bugtraq;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -46,9 +44,7 @@
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileBasedConfig;
@@ -142,35 +138,21 @@
        if (repository.isBare()) {
            // read bugtraq config directly from the repository
            String content = null;
            String head = repository.getFullBranch();
            RevWalk rw = new RevWalk(repository);
            TreeWalk tw = new TreeWalk(repository);
            tw.setFilter(PathFilterGroup.createFromStrings(DOT_GIT_BUGTRAQ));
            try {
                ObjectId headId = repository.resolve(head);
                ObjectId headId = repository.getRef(Constants.HEAD).getTarget().getObjectId();
                RevCommit commit = rw.parseCommit(headId);
                RevTree tree = commit.getTree();
                tw.reset(tree);
                while (tw.next()) {
                    if (tw.isSubtree()) {
                        tw.enterSubtree();
                        continue;
                    }
                    ObjectId entid = tw.getObjectId(0);
                    FileMode entmode = tw.getFileMode(0);
                    if (entmode == FileMode.REGULAR_FILE) {
                        RevObject ro = rw.lookupAny(entid, entmode.getObjectType());
                        rw.parseBody(ro);
                        ByteArrayOutputStream os = new ByteArrayOutputStream();
                        ObjectLoader ldr = repository.open(ro.getId(), Constants.OBJ_BLOB);
                        byte[] tmp = new byte[4096];
                        InputStream in = ldr.openStream();
                        int n;
                        while ((n = in.read(tmp)) > 0) {
                            os.write(tmp, 0, n);
                        }
                        in.close();
                        content = new String(os.toByteArray(), commit.getEncoding());
                    if (FileMode.REGULAR_FILE == entmode) {
                        ObjectLoader ldr = repository.open(entid, Constants.OBJ_BLOB);
                        content = new String(ldr.getCachedBytes(), commit.getEncoding());
                        break;
                    }
                }
            } finally {
@@ -181,21 +163,15 @@
            if (content == null) {
                // config not found
                baseConfig = null;
            } else {
            }
            else {
                // parse the config
                Config cfg = new Config();
                cfg.fromText(content);
                baseConfig = new StoredConfig(cfg) {
                    @Override
                    public void save() throws IOException {
                Config config = new Config();
                config.fromText(content);
                baseConfig = config;
                    }
                    @Override
                    public void load() throws IOException, ConfigInvalidException {
                    }
                };
            }
        } else {
        else {
            // read bugtraq config from work tree
            final File baseFile = new File(repository.getWorkTree(), DOT_GIT_BUGTRAQ);
            if (baseFile.isFile()) {