| | |
| | | import java.io.OutputStream;
|
| | | import java.nio.charset.Charset;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.Collection;
|
| | | import java.util.Collections;
|
| | | import java.util.Date;
|
| | |
| | | import java.util.zip.ZipEntry;
|
| | | import java.util.zip.ZipOutputStream;
|
| | |
|
| | | import org.eclipse.jgit.api.CloneCommand;
|
| | | import org.eclipse.jgit.api.FetchCommand;
|
| | | import org.eclipse.jgit.api.Git;
|
| | | import org.eclipse.jgit.diff.DiffEntry;
|
| | | import org.eclipse.jgit.diff.DiffEntry.ChangeType;
|
| | |
| | | import org.eclipse.jgit.revwalk.RevTree;
|
| | | import org.eclipse.jgit.revwalk.RevWalk;
|
| | | import org.eclipse.jgit.revwalk.filter.RevFilter;
|
| | | import org.eclipse.jgit.storage.file.FileRepository;
|
| | | import org.eclipse.jgit.transport.FetchResult;
|
| | | import org.eclipse.jgit.transport.RefSpec;
|
| | | import org.eclipse.jgit.treewalk.TreeWalk;
|
| | | import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
|
| | | import org.eclipse.jgit.treewalk.filter.OrTreeFilter;
|
| | |
| | | return r.toString().trim();
|
| | | }
|
| | |
|
| | | public static Repository createRepository(File repositoriesFolder, String name, boolean bare) {
|
| | | Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(bare).call();
|
| | | public static FetchResult cloneRepository(File repositoriesFolder, String name, String fromUrl)
|
| | | throws Exception {
|
| | | FetchResult result = null;
|
| | | if (!name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
|
| | | name += Constants.DOT_GIT_EXT;
|
| | | }
|
| | | File folder = new File(repositoriesFolder, name);
|
| | | if (folder.exists()) {
|
| | | File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED);
|
| | | FileRepository repository = new FileRepository(gitDir);
|
| | | result = fetchRepository(repository);
|
| | | repository.close();
|
| | | } else {
|
| | | CloneCommand clone = new CloneCommand();
|
| | | clone.setBare(true);
|
| | | clone.setCloneAllBranches(true);
|
| | | clone.setURI(fromUrl);
|
| | | clone.setDirectory(folder);
|
| | | clone.call();
|
| | | // Now we have to fetch because CloneCommand doesn't fetch
|
| | | // refs/notes nor does it allow manual RefSpec.
|
| | | File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED);
|
| | | FileRepository repository = new FileRepository(gitDir);
|
| | | result = fetchRepository(repository);
|
| | | repository.close();
|
| | | }
|
| | | return result;
|
| | | }
|
| | |
|
| | | public static FetchResult fetchRepository(Repository repository, RefSpec... refSpecs)
|
| | | throws Exception {
|
| | | Git git = new Git(repository);
|
| | | FetchCommand fetch = git.fetch();
|
| | | List<RefSpec> specs = new ArrayList<RefSpec>();
|
| | | if (refSpecs == null || refSpecs.length == 0) {
|
| | | specs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
|
| | | specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
|
| | | specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));
|
| | | } else {
|
| | | specs.addAll(Arrays.asList(refSpecs));
|
| | | }
|
| | | fetch.setRefSpecs(specs);
|
| | | FetchResult result = fetch.call();
|
| | | repository.close();
|
| | | return result;
|
| | | }
|
| | |
|
| | | public static Repository createRepository(File repositoriesFolder, String name) {
|
| | | Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call();
|
| | | return git.getRepository();
|
| | | }
|
| | |
|
| | |
| | | refs.put(objectid, new ArrayList<RefModel>());
|
| | | }
|
| | | refs.get(objectid).add(ref);
|
| | | } |
| | | }
|
| | | return refs;
|
| | | }
|
| | |
|
| | |
| | | public static List<PathChangeModel> getFilesInCommit(Repository r, RevCommit commit) {
|
| | | List<PathChangeModel> list = new ArrayList<PathChangeModel>();
|
| | | RevWalk rw = new RevWalk(r);
|
| | | TreeWalk tw = new TreeWalk(r);
|
| | | try {
|
| | | if (commit == null) {
|
| | | ObjectId object = r.resolve(Constants.HEAD);
|
| | | commit = rw.parseCommit(object);
|
| | | }
|
| | | RevTree commitTree = commit.getTree();
|
| | |
|
| | | tw.reset();
|
| | | tw.setRecursive(true);
|
| | | if (commit.getParentCount() == 0) {
|
| | | tw.addTree(commitTree);
|
| | | TreeWalk tw = new TreeWalk(r);
|
| | | tw.reset();
|
| | | tw.setRecursive(true);
|
| | | tw.addTree(commit.getTree());
|
| | | while (tw.next()) {
|
| | | list.add(new PathChangeModel(tw.getPathString(), tw.getPathString(), 0, tw
|
| | | .getRawMode(0), commit.getId().getName(), ChangeType.ADD));
|
| | | }
|
| | | tw.release();
|
| | | } else {
|
| | | RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
|
| | | RevTree parentTree = parent.getTree();
|
| | | tw.addTree(parentTree);
|
| | | tw.addTree(commitTree);
|
| | | tw.setFilter(TreeFilter.ANY_DIFF);
|
| | |
|
| | | RawTextComparator cmp = RawTextComparator.DEFAULT;
|
| | | DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
|
| | | df.setRepository(r);
|
| | | df.setDiffComparator(cmp);
|
| | | df.setDiffComparator(RawTextComparator.DEFAULT);
|
| | | df.setDetectRenames(true);
|
| | | List<DiffEntry> diffs = df.scan(parentTree, commitTree);
|
| | | List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
|
| | | for (DiffEntry diff : diffs) {
|
| | | if (diff.getChangeType().equals(ChangeType.DELETE)) {
|
| | | list.add(new PathChangeModel(diff.getOldPath(), diff.getOldPath(), 0, diff
|
| | |
| | | LOGGER.error("failed to determine files in commit!", t);
|
| | | } finally {
|
| | | rw.dispose();
|
| | | tw.release();
|
| | | }
|
| | | return list;
|
| | | }
|
| | |
| | | public static List<RevCommit> getRevLog(Repository r, String objectId, String path, int offset,
|
| | | int maxCount) {
|
| | | List<RevCommit> list = new ArrayList<RevCommit>();
|
| | | if (maxCount == 0) {
|
| | | return list;
|
| | | }
|
| | | if (!hasCommits(r)) {
|
| | | return list;
|
| | | }
|
| | |
| | | final SearchType type, int offset, int maxCount) {
|
| | | final String lcValue = value.toLowerCase();
|
| | | List<RevCommit> list = new ArrayList<RevCommit>();
|
| | | if (maxCount == 0) {
|
| | | return list;
|
| | | }
|
| | | if (!hasCommits(r)) {
|
| | | return list;
|
| | | }
|
| | |
| | |
|
| | | private static List<RefModel> getRefs(Repository r, String refs, boolean fullName, int maxCount) {
|
| | | List<RefModel> list = new ArrayList<RefModel>();
|
| | | if (maxCount == 0) {
|
| | | return list;
|
| | | }
|
| | | try {
|
| | | Map<String, Ref> map = r.getRefDatabase().getRefs(refs);
|
| | | RevWalk rw = new RevWalk(r);
|