Yi EungJun 2014-08-23
PullRequest: Remove code deprecated by 2293c37
This also drops pull_request.conflict_files column.
@4591610e690985d8f53494c12429ed4fc53f1606
app/actors/PullRequestActor.java
--- app/actors/PullRequestActor.java
+++ app/actors/PullRequestActor.java
@@ -52,14 +52,14 @@
             if (!wasConflict && mergeResult.conflicts()) {
                 mergeResult.setConflictStateOfPullRequest();
                 NotificationEvent notiEvent = NotificationEvent.afterMerge(message.getSender(),
-                        pullRequest, mergeResult.getGitConflicts(), State.CONFLICT);
+                        pullRequest, State.CONFLICT);
                 PullRequestEvent.addMergeEvent(notiEvent.getSender(), EventType.PULL_REQUEST_MERGED, State.CONFLICT, pullRequest);
             }
 
-            if (wasConflict && mergeResult.resolved()) {
+            if (wasConflict && !mergeResult.conflicts()) {
                 mergeResult.setResolvedStateOfPullRequest();
                 NotificationEvent notiEvent = NotificationEvent.afterMerge(message.getSender(),
-                        pullRequest, mergeResult.getGitConflicts(), State.RESOLVED);
+                        pullRequest, State.RESOLVED);
                 PullRequestEvent.addMergeEvent(notiEvent.getSender(), EventType.PULL_REQUEST_MERGED, State.RESOLVED, pullRequest);
             }
 
app/models/CodeCommentThread.java
--- app/models/CodeCommentThread.java
+++ app/models/CodeCommentThread.java
@@ -22,19 +22,19 @@
 
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
-
-import javax.annotation.Nullable;
-import javax.persistence.*;
-import java.util.ArrayList;
-import java.util.List;
-
-import java.io.IOException;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.errors.MissingObjectException;
 import org.eclipse.jgit.lib.Repository;
 
+import javax.annotation.Nullable;
+import javax.persistence.*;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
 import static models.CodeRange.Side;
-import static models.CodeRange.Side.*;
+import static models.CodeRange.Side.A;
+import static models.CodeRange.Side.B;
 
 /**
  * @author Keesun Baik
@@ -102,20 +102,20 @@
             path = path.substring(1);
         }
 
-        Repository mergedRepository = pullRequest.getMergedRepository();
+        Repository repository = pullRequest.getRepository();
 
         try {
             if (StringUtils.isNotEmpty(prevCommitId)) {
-                _isOutdated = !PullRequest.noChangesBetween(mergedRepository,
-                    pullRequest.mergedCommitIdFrom, mergedRepository, prevCommitId, path);
+                _isOutdated = !PullRequest.noChangesBetween(repository,
+                    pullRequest.mergedCommitIdFrom, repository, prevCommitId, path);
             }
 
             if (_isOutdated) {
                 return _isOutdated;
             }
 
-            _isOutdated = !PullRequest.noChangesBetween(mergedRepository,
-                pullRequest.mergedCommitIdTo, mergedRepository, commitId, path);
+            _isOutdated = !PullRequest.noChangesBetween(repository,
+                pullRequest.mergedCommitIdTo, repository, commitId, path);
         } catch (MissingObjectException e) {
             play.Logger.warn("Possible false positive of outdated detection because of missing git object: " + e.getMessage());
             return true;
app/models/NotificationEvent.java
--- app/models/NotificationEvent.java
+++ app/models/NotificationEvent.java
@@ -30,7 +30,6 @@
 import org.apache.commons.collections.Predicate;
 import org.apache.commons.lang3.StringUtils;
 import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.joda.time.DateTime;
 import org.tmatesoft.svn.core.SVNException;
@@ -48,6 +47,7 @@
 import javax.servlet.ServletException;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
@@ -55,7 +55,6 @@
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import java.net.URLEncoder;
 
 import static models.enumeration.EventType.*;
 
@@ -347,15 +346,12 @@
     /**
      * @see {@link actors.PullRequestActor#processPullRequestMerging(models.PullRequestEventMessage, models.PullRequest)}
      */
-    public static NotificationEvent afterMerge(User sender, PullRequest pullRequest, GitConflicts conflicts, State state) {
+    public static NotificationEvent afterMerge(User sender, PullRequest pullRequest, State state) {
         NotificationEvent notiEvent = createFrom(sender, pullRequest);
         notiEvent.title = formatReplyTitle(pullRequest);
         notiEvent.receivers = state == State.MERGED ? getReceiversWithRelatedAuthors(sender, pullRequest) : getReceivers(sender, pullRequest);
         notiEvent.eventType = PULL_REQUEST_MERGED;
         notiEvent.newValue = state.state();
-        if (conflicts != null) {
-            notiEvent.oldValue = StringUtils.join(conflicts.conflictFiles, "\n");
-        }
         NotificationEvent.add(notiEvent);
         return notiEvent;
     }
@@ -756,12 +752,10 @@
         String failureMessage =
                 "Failed to get authors related to the pullrequest " + pullRequest;
         try {
-            Repository clonedRepository = GitRepository.buildMergingRepository(pullRequest);
-
             if (pullRequest.mergedCommitIdFrom != null
                     && pullRequest.mergedCommitIdTo != null) {
                 receivers.addAll(GitRepository.getRelatedAuthors(
-                        clonedRepository,
+                        new GitRepository(pullRequest.toProject).getRepository(),
                         pullRequest.mergedCommitIdFrom,
                         pullRequest.mergedCommitIdTo));
             }
app/models/PullRequest.java
--- app/models/PullRequest.java
+++ app/models/PullRequest.java
@@ -63,7 +63,6 @@
 import javax.persistence.*;
 import javax.persistence.OrderBy;
 import javax.validation.constraints.Size;
-import java.io.File;
 import java.io.IOException;
 import java.text.MessageFormat;
 import java.util.*;
@@ -79,9 +78,6 @@
     public static final Finder<Long, PullRequest> finder = new Finder<>(Long.class, PullRequest.class);
 
     public static final int ITEMS_PER_PAGE = 15;
-
-    @Transient
-    public Repository mergedRepo = null;
 
     @Id
     public Long id;
@@ -146,8 +142,6 @@
     public String mergedCommitIdTo;
 
     public Long number;
-
-    public String conflictFiles;
 
     @ManyToMany(cascade = CascadeType.ALL)
     @JoinTable(
@@ -665,16 +659,6 @@
         return repository;
     }
 
-    public Repository getMergedRepository() throws IOException {
-        if (mergedRepo == null) {
-            File dir = new File(
-                    GitRepository.getDirectoryForMerging(toProject.owner, toProject.name) + "/.git");
-            mergedRepo = new RepositoryBuilder().setGitDir(dir).build();
-        }
-
-        return mergedRepo;
-    }
-
     @Transient
     public List<FileDiff> getDiff(String revA, String revB) throws IOException {
         Repository repository = getRepository();
@@ -769,11 +753,6 @@
     public void delete() {
         deleteIssueEvents();
         super.delete();
-    }
-
-    @Transient
-    public String[] getConflictFiles() {
-        return StringUtils.split(this.conflictFiles, ",");
     }
 
     @Transient
app/models/PullRequestMergeResult.java
--- app/models/PullRequestMergeResult.java
+++ app/models/PullRequestMergeResult.java
@@ -21,25 +21,16 @@
 package models;
 
 import models.enumeration.State;
-import org.apache.commons.lang3.StringUtils;
 import playRepository.GitCommit;
-import playRepository.GitConflicts;
 
 import java.util.ArrayList;
 import java.util.List;
 
 public class PullRequestMergeResult {
-    private GitConflicts gitConflicts;
     private List<GitCommit> gitCommits;
     private List<PullRequestCommit> newCommits;
     private PullRequest pullRequest;
 
-    public GitConflicts getGitConflicts() {
-        return gitConflicts;
-    }
-    public void setGitConflicts(GitConflicts gitConflicts) {
-        this.gitConflicts = gitConflicts;
-    }
     public List<GitCommit> getGitCommits() {
         return gitCommits;
     }
@@ -55,20 +46,13 @@
     public boolean hasDiffCommits() {
         return this.gitCommits.size() > 0;
     }
-    public boolean resolved() {
-        return this.gitConflicts == null && !pullRequest.isConflict;
-    }
+
     public boolean conflicts() {
-        return this.gitConflicts != null && pullRequest.isConflict;
+        return pullRequest.isConflict;
     }
+
     public List<PullRequestCommit> getNewCommits() {
         return newCommits;
-    }
-    public String getConflictFilesToString() {
-        if (gitConflicts == null) {
-            return StringUtils.EMPTY;
-        }
-        return StringUtils.join(gitConflicts.conflictFiles, PullRequest.DELIMETER);
     }
 
     public List<PullRequestCommit> findNewCommits() {
@@ -136,17 +120,14 @@
 
     public void setConflictStateOfPullRequest() {
         pullRequest.isConflict = true;
-        pullRequest.conflictFiles = StringUtils.EMPTY;
     }
 
     public void setResolvedStateOfPullRequest() {
         pullRequest.isConflict = false;
-        pullRequest.conflictFiles = StringUtils.EMPTY;
     }
 
     public void setMergedStateOfPullRequest(User receiver) {
         pullRequest.isConflict = false;
-        pullRequest.conflictFiles = StringUtils.EMPTY;
         pullRequest.state = State.MERGED;
         pullRequest.receiver = receiver;
     }
 
app/playRepository/GitConflicts.java (deleted)
--- app/playRepository/GitConflicts.java
@@ -1,89 +0,0 @@
-/**
- * Yobi, Project Hosting SW
- *
- * Copyright 2013 NAVER Corp.
- * http://yobi.io
- *
- * @Author Keesun Baik
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package playRepository;
-
-import org.eclipse.jgit.api.MergeResult;
-import org.eclipse.jgit.lib.ObjectId;
-import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.revwalk.RevWalk;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-public class GitConflicts {
-
-    public List<String> conflictFiles = new ArrayList<>();
-
-    public List<Conflict> conflictDetails = new ArrayList<>();
-
-    /**
-     * @param repository
-     * @param mergeResult
-     * @see http://download.eclipse.org/jgit/docs/latest/apidocs/org/eclipse/jgit/api/MergeResult.html#getConflicts()
-     */
-    public GitConflicts(Repository repository, MergeResult mergeResult) {
-        Map<String, int[][]> allConflicts = mergeResult.getConflicts();
-        for (String path : allConflicts.keySet()) {
-            conflictFiles.add(path);
-            int[][] conflicts = allConflicts.get(path);
-            for (int[] c : conflicts) {
-                Conflict conflict = new Conflict();
-                conflict.fileName = path;
-                for (int j = 0; j < (c.length) - 1; ++j) {
-                    if (c[j] >= 0) {
-                        ObjectId objectId = mergeResult.getMergedCommits()[j];
-                        RevWalk revWalk = new RevWalk(repository);
-
-                        CommitAndLine commitAndLine = new CommitAndLine();
-                        try {
-                            commitAndLine.gitCommit = new GitCommit(revWalk.parseCommit(objectId));
-                            commitAndLine.lineNumber = c[j];
-                            conflict.commitAndLines.add(commitAndLine);
-                        } catch (IOException e) {
-                            throw new RuntimeException(e);
-                        }
-                    }
-                }
-                conflictDetails.add(conflict);
-            }
-        }
-
-    }
-
-    public static class Conflict {
-
-        public String fileName;
-
-        public List<CommitAndLine> commitAndLines = new ArrayList<>();
-
-    }
-
-    public static class CommitAndLine {
-
-        public GitCommit gitCommit;
-
-        public int lineNumber;
-
-    }
-
-}
app/playRepository/GitRepository.java
--- app/playRepository/GitRepository.java
+++ app/playRepository/GitRepository.java
@@ -35,7 +35,8 @@
 import org.apache.tika.metadata.Metadata;
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.node.ObjectNode;
-import org.eclipse.jgit.api.*;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.LogCommand;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.blame.BlameResult;
 import org.eclipse.jgit.diff.*;
@@ -48,7 +49,6 @@
 import org.eclipse.jgit.revwalk.RevTree;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.storage.file.WindowCacheConfig;
-import org.eclipse.jgit.transport.RefSpec;
 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
 import org.eclipse.jgit.treewalk.AbstractTreeIterator;
 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
@@ -102,10 +102,6 @@
 
     public static String getRepoForMergingPrefix() {
         return repoForMergingPrefix;
-    }
-
-    public static void setRepoForMergingPrefix(String repoForMergingPrefix) {
-        GitRepository.repoForMergingPrefix = repoForMergingPrefix;
     }
 
     private final Repository repository;
@@ -881,48 +877,6 @@
                 .call();
     }
 
-    public static void deleteMergingDirectory(PullRequest pullRequest) {
-        Project toProject = pullRequest.toProject;
-        String directoryForMerging = GitRepository.getDirectoryForMerging(toProject.owner, toProject.name);
-        FileUtil.rm_rf(new File(directoryForMerging));
-    }
-
-    /**
-     * Pushes branches.
-     *
-     * @param repository the source repository
-     * @param remote the destination repository
-     * @param src src of refspec
-     * @param dest dst of refspec
-     * @throws GitAPIException
-     */
-    public static void push(Repository repository, String remote, String src, String dest) throws GitAPIException {
-        new Git(repository).push()
-                .setRemote(remote)
-                .setRefSpecs(new RefSpec(src + ":" + dest))
-                .call();
-    }
-
-    /**
-     * Merges a branch.
-     *
-     * Add --no-ff option to create a merge commit even when the merge resolves
-     * as a fast-forward.
-     *
-     * @param repository
-     * @param branchName the name of a branch to be merged.
-     * @return
-     * @throws GitAPIException
-     * @throws IOException
-     */
-    public static MergeResult merge(Repository repository, String branchName) throws GitAPIException, IOException {
-        ObjectId branch = repository.resolve(branchName);
-        return new Git(repository).merge()
-                .setFastForward(MergeCommand.FastForwardMode.NO_FF)
-                .include(branch)
-                .call();
-    }
-
     /**
      * Checks out the branch.
      *
@@ -954,27 +908,17 @@
         return getDirectoryForMerging(owner, projectName) + "/.git/objects";
     }
 
-    public static List<RevCommit> diffRevCommits(Repository repository, String fromBranch, String toBranch) throws IOException, GitAPIException {
-        ObjectId from = repository.resolve(fromBranch);
-        ObjectId to = repository.resolve(toBranch);
-        return diffRevCommits(repository, from, to);
-    }
-
     @SuppressWarnings("unchecked")
     public static List<RevCommit> diffRevCommits(Repository repository, ObjectId from, ObjectId to) throws IOException, GitAPIException {
         return IteratorUtils.toList(
                 new Git(repository).log().addRange(from, to).call().iterator());
     }
 
-    public static List<GitCommit> diffCommits(Repository repository, String fromBranch, String toBranch) throws IOException, GitAPIException {
-        return wrap(diffRevCommits(repository, fromBranch, toBranch));
-    }
-
     public static List<GitCommit> diffCommits(Repository repository, ObjectId from, ObjectId to) throws IOException, GitAPIException {
-        return wrap(diffRevCommits(repository, from, to));
+        return wrapInGitCommits(diffRevCommits(repository, from, to));
     }
 
-    public static List<GitCommit> wrap(List<RevCommit> revCommits) throws IOException, GitAPIException {
+    public static List<GitCommit> wrapInGitCommits(List<RevCommit> revCommits) throws IOException, GitAPIException {
         List<GitCommit> commits = new ArrayList<>();
         for (RevCommit revCommit : revCommits) {
             commits.add(new GitCommit(revCommit));
@@ -1166,98 +1110,6 @@
                 .setBranchNames(branchName)
                 .setForce(true)
                 .call();
-    }
-
-    /**
-     * Fetches branches.
-     *
-     * @param repository Stores fetched objects and refs in this repository.
-     * @param project Fetches from this project.
-     * @param fromBranch src of refspec
-     * @param toBranch dst of refspec
-     * @throws GitAPIException
-     * @throws IOException
-     * @see <a href="https://www.kernel.org/pub/software/scm/git/docs/git-fetch.html">git-fetch</a>
-     */
-    public static void fetch(Repository repository, Project project, String fromBranch, String toBranch) throws GitAPIException, IOException {
-        new Git(repository).fetch()
-                .setRemote(GitRepository.getGitDirectoryURL(project))
-                .setRefSpecs(new RefSpec(fromBranch + ":" + toBranch))
-                .call();
-    }
-
-    /**
-     * @see models.PullRequest#attemptMerge()
-     * @see models.PullRequest#merge(models.PullRequestEventMessage)
-     */
-    public static void cloneAndFetch(PullRequest pullRequest, AfterCloneAndFetchOperation operation) {
-        Repository cloneRepository = null;
-        String mergingBranch = null;
-        String destFromBranchName = null;
-        try {
-            synchronized (PROJECT_LOCK.get(pullRequest.toProject)) {
-                cloneRepository = buildMergingRepository(pullRequest);
-
-                String srcToBranchName = pullRequest.toBranch;
-                String destToBranchName = makeDestToBranchName(pullRequest);
-                String srcFromBranchName = pullRequest.fromBranch;
-                destFromBranchName = makeDestFromBranchName(pullRequest);
-                mergingBranch = "" + System.currentTimeMillis();
-
-                new Git(cloneRepository).fetch()
-                        .setRemote(GitRepository.getGitDirectoryURL(pullRequest.fromProject))
-                        .setRefSpecs(new RefSpec("+" + srcFromBranchName + ":" + destFromBranchName))
-                        .call();
-
-                new Git(cloneRepository).fetch()
-                        .setRemote(GitRepository.getGitDirectoryURL(pullRequest.toProject))
-                        .setRefSpecs(new RefSpec("+" + srcToBranchName + ":" + destToBranchName))
-                        .call();
-
-                resetAndClean(cloneRepository);
-
-                new Git(cloneRepository).checkout()
-                        .setCreateBranch(true)
-                        .setName(mergingBranch)
-                        .setStartPoint(destToBranchName)
-                        .call();
-
-                CloneAndFetch cloneAndFetch = new CloneAndFetch(cloneRepository, destToBranchName, destFromBranchName, mergingBranch);
-                operation.invoke(cloneAndFetch);
-
-                resetAndClean(cloneRepository);
-
-                new Git(cloneRepository).checkout().setName(destToBranchName).call();
-                new Git(cloneRepository).branchDelete().setForce(true).setBranchNames(mergingBranch).call();
-            }
-        } catch (GitAPIException e) {
-            throw new IllegalStateException(e);
-        } catch (IOException e) {
-            throw new IllegalStateException(e);
-        } finally {
-            if(cloneRepository != null) {
-                cloneRepository.close();
-            }
-        }
-    }
-
-    private static void resetAndClean(Repository cloneRepository) throws GitAPIException {
-        new Git(cloneRepository).reset().setMode(ResetCommand.ResetType.HARD).setRef(Constants.HEAD).call();
-        new Git(cloneRepository).clean().setIgnore(true).setCleanDirectories(true).call();
-    }
-
-    private static String makeDestToBranchName(PullRequest pullRequest) {
-        return Constants.R_REMOTES +
-            pullRequest.toProject.owner + "/" +
-            pullRequest.toProject.name + "/" +
-            pullRequest.toBranch.replaceFirst(Constants.R_HEADS, "");
-    }
-
-    private static String makeDestFromBranchName(PullRequest pullRequest) {
-        return Constants.R_REMOTES +
-            pullRequest.fromProject.owner + "/" +
-            pullRequest.fromProject.name + "/" +
-            pullRequest.fromBranch.replaceFirst(Constants.R_HEADS, "");
     }
 
     public static Repository buildMergingRepository(PullRequest pullRequest) {
@@ -1805,9 +1657,6 @@
         }
     }
 
-    /**
-     * @see #cloneAndFetch(models.PullRequest, playRepository.GitRepository.AfterCloneAndFetchOperation)
-     */
     public static interface AfterCloneAndFetchOperation {
         public void invoke(CloneAndFetch cloneAndFetch) throws IOException, GitAPIException;
     }
app/views/git/partial_state.scala.html
--- app/views/git/partial_state.scala.html
+++ app/views/git/partial_state.scala.html
@@ -44,14 +44,6 @@
     <div class="alert alert-error">
         <i class="yobicon-error mr5"></i>
         <span>@Messages("pullRequest.is.not.safe")</span>
-        <div>
-            <h6>@Messages("pullRequest.conflict.files")</h6>
-            <ul class="unstyled conflict-files">
-                @for(file <- pull.getConflictFiles) {
-                <li>@file</li>
-                }
-            </ul>
-        </div>
 
         @if(UserApp.currentUser().equals(pull.contributor)){
         <div class="howto-resolve-conflict">
 
conf/evolutions/default/90.sql (added)
+++ conf/evolutions/default/90.sql
@@ -0,0 +1,7 @@
+# --- !Ups
+
+ALTER TABLE pull_request DROP COLUMN conflict_files;
+
+# --- !Downs
+
+ALTER TABLE pull_request ADD COLUMN conflict_files clob;
test/controllers/ImportAppTest.java
--- test/controllers/ImportAppTest.java
+++ test/controllers/ImportAppTest.java
@@ -53,7 +53,6 @@
     @BeforeClass
     public static void before() throws Exception {
         GitRepository.setRepoPrefix("resources/test/repo/git/");
-        GitRepository.setRepoForMergingPrefix("resources/test/repo/git-merging/");
         application = support.Helpers.makeTestApplication();
         start(application);
         yobi = User.findByLoginId("yobi");
@@ -67,7 +66,6 @@
     public static void after() {
         stop(application);
         support.Files.rm_rf(new File(GitRepository.getRepoPrefix()));
-        support.Files.rm_rf(new File(GitRepository.getRepoForMergingPrefix()));
     }
 
     @Test
test/controllers/PullRequestAppTest.java
--- test/controllers/PullRequestAppTest.java
+++ test/controllers/PullRequestAppTest.java
@@ -73,7 +73,6 @@
     @Before
     public void before() {
         GitRepository.setRepoPrefix("resources/test/repo/git/");
-        GitRepository.setRepoForMergingPrefix("resources/test/repo/git-merging/");
     }
 
     @AfterClass
@@ -84,7 +83,6 @@
     @After
     public void after() {
         support.Files.rm_rf(new File(GitRepository.getRepoPrefix()));
-        support.Files.rm_rf(new File(GitRepository.getRepoForMergingPrefix()));
     }
 
     @Test
@@ -388,33 +386,6 @@
         this.ownerLoginId = ownerLoginId;
         this.projectName = projectName;
         this.pullRequestNumber = pullRequestNumber;
-        Project project = Project.findByOwnerAndProjectName(ownerLoginId, projectName);
-        PullRequest pullRequest = PullRequest.findOne(project, pullRequestNumber);
-        initRepositories(pullRequest);
     }
 
-    private void initRepositories(PullRequest pullRequest) throws Exception {
-        if (pullRequest == null) {
-            return;
-        }
-        initRepository(pullRequest.toProject,
-                StringUtils.removeStart(pullRequest.toBranch, "refs/heads/"), "1.txt");
-        initRepository(pullRequest.fromProject,
-                StringUtils.removeStart(pullRequest.fromBranch, "refs/heads/"), "2.txt");
-    }
-
-    private void initRepository(Project project, String branchName, String fileName) throws Exception {
-        GitRepository gitRepository = new GitRepository(project);
-        gitRepository.create();
-
-        Repository repository = GitRepository.buildMergingRepository(project);
-        Git git =  new Git(repository);
-
-        FileUtils.touch(new File(GitRepository.getDirectoryForMerging(project.owner, project.name + "/" + fileName)));
-        git.add().addFilepattern(fileName).call();
-        git.commit().setMessage(fileName).call();
-        git.push().setRefSpecs(new RefSpec("master:master"), new RefSpec("master:" + branchName)).call();
-        gitRepository.close();
-        repository.close();
-    }
 }
test/models/CodeCommentThreadTest.java
--- test/models/CodeCommentThreadTest.java
+++ test/models/CodeCommentThreadTest.java
@@ -28,6 +28,7 @@
 import org.junit.Test;
 import playRepository.BareCommit;
 import playRepository.GitRepository;
+import support.Git;
 import utils.JodaDateUtil;
 
 import java.io.File;
@@ -41,7 +42,6 @@
  * @author Changsung Kim
  */
 public class CodeCommentThreadTest extends ModelTest<CodeCommentThread>  {
-    private static final String MERGING_REPO_PREFIX = "resources/test/repo/git-merging/";
     private static final String REPO_PREFIX = "resources/test/repo/git/";
     private Project project;
     private ObjectId baseCommit;
@@ -57,12 +57,10 @@
     @After
     public void after() {
         rm_rf(new File(REPO_PREFIX));
-        rm_rf(new File(MERGING_REPO_PREFIX));
     }
 
     private void addTestRepository() throws IOException, GitAPIException {
         GitRepository.setRepoPrefix(REPO_PREFIX);
-        GitRepository.setRepoForMergingPrefix(MERGING_REPO_PREFIX);
 
         // given
         GitRepository.buildGitRepository(project).create(true);
test/models/PullRequestTest.java
--- test/models/PullRequestTest.java
+++ test/models/PullRequestTest.java
@@ -47,7 +47,6 @@
 import static utils.FileUtil.rm_rf;
 
 public class PullRequestTest extends ModelTest<PullRequest> {
-    private static final String MERGING_REPO_PREFIX = "resources/test/repo/git-merging/";
     private static final String REPO_PREFIX = "resources/test/repo/git/";
     private static final String LOCAL_REPO_PREFIX = "resources/test/local-repo/git/";
 
@@ -61,7 +60,6 @@
     public void initRepositories() throws IOException, GitAPIException, ServletException,
             ClientException, PullRequestException {
         GitRepository.setRepoPrefix(REPO_PREFIX);
-        GitRepository.setRepoForMergingPrefix(MERGING_REPO_PREFIX);
 
         app = support.Helpers.makeTestApplication();
         Helpers.start(app);
@@ -80,7 +78,7 @@
                     .setDirectory(new File(localRepoPath))
                     .call();
             Repository repo = git.getRepository();
-            baseCommit = support.Git.commit(repo, repo.getWorkTree().getAbsolutePath(), "test.txt",
+            baseCommit = support.Git.commit(repo, "test.txt",
                     "apple\nbanana\ncat\n", "commit 1");
             git.push().setRefSpecs(new RefSpec("+refs/heads/master:refs/heads/master")).call();
         }
@@ -99,9 +97,9 @@
             git.checkout().setName("fix/1").call();
             Repository repo = git.getRepository();
             assertThat(repo.isBare()).describedAs("projectYobi-1 must be non-bare").isFalse();
-            firstCommit = support.Git.commit(repo, repo.getWorkTree().getAbsolutePath(),
+            firstCommit = support.Git.commit(repo,
                     "test.txt", "apple\nbanana\ncorn\n", "commit 1");
-            secondCommit = support.Git.commit(repo, repo.getWorkTree().getAbsolutePath(),
+            secondCommit = support.Git.commit(repo,
                     "test.txt", "apple\nbanana\ncake\n", "commit 2");
             git.push().setRefSpecs(new RefSpec("+refs/heads/fix/1:refs/heads/fix/1")).call();
         }
@@ -120,7 +118,6 @@
     @After
     public void after() {
         rm_rf(new File(REPO_PREFIX));
-        rm_rf(new File(MERGING_REPO_PREFIX));
         rm_rf(new File(LOCAL_REPO_PREFIX));
         Helpers.stop(app);
     }
test/playRepository/GitRepositoryTest.java
--- test/playRepository/GitRepositoryTest.java
+++ test/playRepository/GitRepositoryTest.java
@@ -20,24 +20,27 @@
  */
 package playRepository;
 
-import static play.test.Helpers.*;
 import models.Project;
 import models.PullRequest;
 import models.User;
-
 import org.apache.commons.io.FileUtils;
 import org.codehaus.jackson.node.ObjectNode;
 import org.eclipse.jgit.api.CommitCommand;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.lib.*;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.RepositoryBuilder;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevTree;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.transport.RefSpec;
 import org.eclipse.jgit.treewalk.TreeWalk;
-import org.junit.*;
-
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
 import org.junit.rules.TestWatcher;
 import play.test.FakeApplication;
 import play.test.Helpers;
@@ -53,6 +56,7 @@
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.fest.assertions.Fail.fail;
+import static play.test.Helpers.running;
 
 public class GitRepositoryTest {
 
@@ -62,13 +66,11 @@
     @Before
     public void before() {
         GitRepository.setRepoPrefix("resources/test/repo/git/");
-        GitRepository.setRepoForMergingPrefix("resources/test/repo/git-merging/");
     }
 
     @After
     public void after() {
         support.Files.rm_rf(new File(GitRepository.getRepoPrefix()));
-        support.Files.rm_rf(new File(GitRepository.getRepoForMergingPrefix()));
     }
 
     @Test
@@ -217,57 +219,6 @@
         assertThat(file.exists()).isTrue();
     }
 
-    @Test
-    public void cloneRepositoryWithNonBareMode() throws IOException, GitAPIException {
-        // Given
-        Project originProject = createProject("whiteship", "test");
-        support.Files.rm_rf(new File(GitRepository.getGitDirectory(originProject)));
-        new GitRepository(originProject.owner, originProject.name).create();
-
-        String cloneWorkingTreePath = GitRepository.getDirectoryForMerging(originProject.owner, originProject.name);
-        support.Files.rm_rf(new File(cloneWorkingTreePath));
-
-        // When
-        Git.cloneRepository()
-                .setURI(GitRepository.getGitDirectoryURL(originProject))
-                .setDirectory(new File(cloneWorkingTreePath))
-                .call();
-
-        // Then
-        assertThat(new File(cloneWorkingTreePath).exists()).isTrue();
-        assertThat(new File(cloneWorkingTreePath + "/.git").exists()).isTrue();
-
-        Repository cloneRepository = new RepositoryBuilder()
-                .setWorkTree(new File(cloneWorkingTreePath))
-                .setGitDir(new File(cloneWorkingTreePath + "/.git"))
-                .build();
-
-        assertThat(cloneRepository.getFullBranch()).isEqualTo("refs/heads/master");
-
-        // When
-        Git cloneGit = new Git(cloneRepository);
-
-        // toProject를 clone 받은 워킹 디렉토리에서 테스트 파일 만들고 커밋하고 푸쉬하기
-        String readmeFileName = "readme.md";
-        String testFilePath = cloneWorkingTreePath + "/" + readmeFileName;
-        BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));
-        out.write("hello 1");
-        out.flush();
-        cloneGit.add().addFilepattern(readmeFileName).call();
-        cloneGit.commit().setMessage("commit 1").call();
-        cloneGit.push().call();
-
-        // Then
-        Repository originRepository = GitRepository.buildGitRepository(originProject);
-        String readmeFileInClone = new String(getRawFile(cloneRepository, readmeFileName));
-        assertThat(readmeFileInClone).isEqualTo("hello 1");
-        String readmeFileInOrigin = new String(getRawFile(originRepository, readmeFileName));
-        assertThat(readmeFileInOrigin).isEqualTo("hello 1");
-
-        cloneRepository.close();
-        originRepository.close();
-    }
-
 
     @Test
     public void getMetaDataFromPath() throws Exception {
@@ -301,7 +252,7 @@
                     ObjectNode notExistBranch = gitRepository.getMetaDataFromPath("not_exist_branch", "");
                     ObjectNode root = gitRepository.getMetaDataFromPath("");
                     ObjectNode dir = gitRepository.getMetaDataFromPath("dir");
-                    ObjectNode file= gitRepository.getMetaDataFromPath("hello");
+                    ObjectNode file = gitRepository.getMetaDataFromPath("hello");
                     ObjectNode branch = gitRepository.getMetaDataFromPath(branchName, "");
                     ObjectNode lightWeightTag = gitRepository.getMetaDataFromPath(lightWeightTagName, "");
                     ObjectNode annotatedTag = gitRepository.getMetaDataFromPath(annotatedTagName, "");
@@ -325,35 +276,15 @@
     }
 
     @Test
-    public void buildCloneRepository() throws GitAPIException, IOException {
-        // Given
-        Project original = createProject("keesun", "test");
-        PullRequest pullRequest = createPullRequest(original);
-        new GitRepository("keesun", "test").create();
-
-        // When
-        Repository repository = GitRepository.buildMergingRepository(pullRequest);
-
-        // Then
-        assertThat(repository).isNotNull();
-        String repositoryWorkingDirectory = GitRepository.getDirectoryForMerging(original.owner, original.name);
-        assertThat(repositoryWorkingDirectory).isNotNull();
-        String clonePath = "resources/test/repo/git-merging/keesun/test.git";
-        assertThat(repositoryWorkingDirectory).isEqualTo(clonePath);
-        assertThat(new File(clonePath).exists()).isTrue();
-        assertThat(new File(clonePath + "/.git").exists()).isTrue();
-    }
-
-    @Test
     public void deleteBranch() throws IOException, GitAPIException {
         // given
-        Repository repository = createRepository("keesun", "test", false);
+        Repository repository = support.Git.createRepository("keesun", "test", false);
         Git git = new Git(repository);
 
         String fileName = "readme.md";
         String contents = "Hello World";
         String commitMessage = "Initial commit";
-        addCommit(git, fileName, contents, commitMessage);
+        addCommit(git, fileName, contents, commitMessage, null);
 
         git.branchCreate().setName("develop").setForce(true).call();
         GitRepository.checkout(repository, "develop");
@@ -371,151 +302,19 @@
         }
     }
 
-    public Repository createRepository(String userName, String projectName, boolean bare) throws IOException {
-        String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;
-        String repoPath = wcPath + "/.git";
-        File repoDir = new File(repoPath);
-        Repository repository = new RepositoryBuilder().setGitDir(repoDir).build();
-        repository.create(bare);
-        return repository;
-    }
-
-    public void addCommit(Git git, String fileName, String contents, String commitMessage) throws IOException, GitAPIException {
+    public RevCommit addCommit(Git git, String fileName, String contents, String commitMessage,
+                          User author) throws IOException, GitAPIException {
         File newFile = new File(git.getRepository().getWorkTree().getAbsolutePath(), fileName);
         BufferedWriter out = new BufferedWriter(new FileWriter(newFile));
         out.write(contents);
         out.flush();
         git.add().addFilepattern(fileName).call();
-        git.commit().setMessage(commitMessage).call();
-    }
-
-    @Test
-    public void fetch() throws IOException, GitAPIException {
-        // Given
-        Project original = createProject("keesun", "test");
-        PullRequest pullRequest = createPullRequest(original);
-        new GitRepository(original).create();
-        Repository repository = GitRepository.buildMergingRepository(pullRequest);
-
-        RevCommit commit = newCommit(original, repository, "readme.md", "hello 1", "commit 1");
-        new Git(repository).push().call();
-
-        // When
-        String dstBranchName = "refs/heads/master-fetch";
-        GitRepository.fetch(repository, original, "refs/heads/master", dstBranchName);
-
-        // Then
-        ObjectId branch = repository.resolve(dstBranchName);
-        assertThat(branch.getName()).isEqualTo(commit.getId().getName());
-    }
-
-    private RevCommit newCommit(Project project, Repository repository, String fileName, String content, String message) throws IOException, GitAPIException {
-        return newCommit(project, repository, fileName, content, message, null);
-    }
-
-    private RevCommit newCommit(Project project, Repository repository, String fileName,
-            String content, String message, User author) throws IOException, GitAPIException {
-        String workingTreePath = GitRepository.getDirectoryForMerging(project.owner, project.name);
-        String testFilePath = workingTreePath + "/" + fileName;
-        BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));
-        out.write(content);
-        out.flush();
-        out.close();
-
-        Git git = new Git(repository);
-        git.add().addFilepattern(fileName).call();
-        CommitCommand commitCommand = git.commit().setMessage(message);
+        CommitCommand commit = git.commit().setMessage(commitMessage);
         if (author != null) {
-            commitCommand
-                .setAuthor(author.loginId, author.email)
-                .setCommitter(author.loginId, author.email);
+            commit.setAuthor(author.loginId, author.email)
+                  .setCommitter(author.loginId, author.email);
         }
-        return commitCommand.call();
-    }
-
-    @Test
-    public void checkout() throws IOException, GitAPIException {
-        // Given
-        Project original = createProject("keesun", "test");
-        PullRequest pullRequest = createPullRequest(original);
-        new GitRepository(original).create();
-        Repository repository = GitRepository.buildMergingRepository(pullRequest);
-        // 커밋이 없으면 HEAD도 없어서 브랜치 만들 때 에러가 발생하기 때문에 일단 하나 커밋한다.
-        newCommit(original, repository, "readme.md", "hello 1", "commit 1");
-
-        Git git = new Git(repository);
-        String branchName = "new-branch";
-        git.branchCreate()
-                .setName(branchName)
-                .setForce(true)
-                .call();
-
-        // When
-        GitRepository.checkout(repository, branchName);
-
-        // Then
-        assertThat(repository.getFullBranch()).isEqualTo("refs/heads/" + branchName);
-    }
-
-    @Test
-    public void merge() throws IOException, GitAPIException {
-        // Given
-        Project original = createProject("keesun", "test");
-        PullRequest pullRequest = createPullRequest(original);
-        GitRepository gitRepository = new GitRepository(original);
-        gitRepository.create();
-        Repository repository = GitRepository.buildMergingRepository(pullRequest);
-
-        // master에 commit 1 추가
-        newCommit(original, repository, "readme.md", "hello 1", "commit 1");
-        // new-branch 생성
-        Git git = new Git(repository);
-        String branchName = "new-branch";
-        git.branchCreate()
-                .setName(branchName)
-                .setForce(true)
-                .call();
-        // new-branch 로 이동
-        GitRepository.checkout(repository, branchName);
-        // new-branch에 commit 2 추가
-        String fileName = "hello.md";
-        String content = "hello 2";
-        newCommit(original, repository, fileName, content, "commit 2");
-
-        // master 로 이동
-        GitRepository.checkout(repository, "master");
-        // When
-        GitRepository.merge(repository, branchName);
-        // Then
-        assertThat(new String(getRawFile(repository, fileName))).isEqualTo(content);
-
-        gitRepository.close();
-        repository.close();
-    }
-
-    @Test
-    public void push() throws IOException, GitAPIException {
-        // Given
-        Project original = createProject("keesun", "test");
-        PullRequest pullRequest = createPullRequest(original);
-        new GitRepository(original).create();
-        Repository repository = GitRepository.buildMergingRepository(pullRequest);
-        // master에 commit 1 추가
-        String fileName = "readme.md";
-        String content = "hello 1";
-        newCommit(original, repository, fileName, content, "commit 1");
-
-        // When
-        String branchName = "master";
-        GitRepository.push(repository, GitRepository.getGitDirectoryURL(original), branchName, branchName);
-
-        // Then
-        Repository originalRepo = new RepositoryBuilder()
-                .setGitDir(new File(GitRepository.getGitDirectory(original)))
-                .build();
-        assertThat(new String(getRawFile(originalRepo, fileName))).isEqualTo(content);
-
-        originalRepo.close();
+        return commit.call();
     }
 
     @Test
@@ -608,21 +407,19 @@
         FakeApplication app = support.Helpers.makeTestApplication();
         Helpers.start(app);
 
-        Project project = createProject("yobi", "test");
-        PullRequest pullRequest = createPullRequest(project);
-        GitRepository gitRepository = new GitRepository(project);
-        gitRepository.create();
-        Repository repository = GitRepository.buildMergingRepository(pullRequest);
+        Repository repository = support.Git.createRepository("yobi", "test", true);
 
         // Given
         User yobi = User.findByLoginId("yobi");
         User laziel = User.findByLoginId("laziel");
         User doortts = User.findByLoginId("doortts");
         User nori = User.findByLoginId("nori");
-        RevCommit from = newCommit(project, repository, "README.md", "hello", "1st commit", yobi);
-        newCommit(project, repository, "test.txt", "text file", "2nd commit", laziel);
-        newCommit(project, repository, "test.txt", "edited text file", "3rd commit", doortts);
-        RevCommit to = newCommit(project, repository, "README.md", "hello, world!", "4th commit", nori);
+
+        Git git = new Git(repository);
+        RevCommit from = addCommit(git, "README.md", "hello", "1st commit", yobi);
+        addCommit(git, "test.txt", "text file", "2nd commit", laziel);
+        addCommit(git, "test.txt", "edited text file", "3rd commit", doortts);
+        RevCommit to = addCommit(git, "README.md", "hello, world!", "4th commit", nori);
 
         // When
         Set<User> authors = GitRepository.getRelatedAuthors(repository, from.getName(), to.getName());
@@ -630,66 +427,8 @@
         // Then
         assertThat(authors).containsOnly(yobi);
 
-        gitRepository.close();
+        repository.close();
         Helpers.stop(app);
-    }
-
-    @Test
-    public void testCloneAndFetch() throws IOException, GitAPIException {
-        // Given
-        Project original = createProject("keesun", "test");
-        GitRepository gitRepository = new GitRepository(original);
-        gitRepository.create();
-
-        // original master에 커밋 하나를 push한다.
-        Repository noneBareClone = GitRepository.buildMergingRepository(original);
-        newCommit(original, noneBareClone, "README.md", "hello", "first commit");
-        GitRepository.push(noneBareClone, GitRepository.getGitDirectoryURL(original), Constants.MASTER, Constants.MASTER);
-
-        // feature 브랜치를 만들고 feature 브랜치로 이동해서 두번째 커밋을 작성하고 original의 feature 브랜치로 push한다.
-        String featureBranchName = "feature";
-        new Git(noneBareClone).branchCreate().setName(featureBranchName).setForce(true).call();
-        final String branchName = Constants.R_HEADS + featureBranchName;
-        new Git(noneBareClone).checkout().setName(branchName).call();
-        newCommit(original, noneBareClone, "README.md", "world", "second commit");
-        GitRepository.push(noneBareClone, GitRepository.getGitDirectoryURL(original), featureBranchName, featureBranchName);
-
-        // feature 브랜치에서 master 브랜치로 코드를 보낸다.
-        final PullRequest pullRequest = createPullRequest(original);
-        pullRequest.toProject = original;
-        pullRequest.fromProject = original;
-        pullRequest.toBranch = Constants.R_HEADS + Constants.MASTER;
-        pullRequest.fromBranch = branchName;
-
-        // When && Then
-        GitRepository.cloneAndFetch(pullRequest, new GitRepository.AfterCloneAndFetchOperation() {
-            @Override
-            public void invoke(GitRepository.CloneAndFetch cloneAndFetch) throws IOException, GitAPIException {
-                Repository repo = cloneAndFetch.getRepository();
-                // 현재 위치가 코드를 merging하는 브랜치 인지 확인한다.
-                String fullBranch = repo.getFullBranch();
-                assertThat(fullBranch).isEqualTo(cloneAndFetch.getMergingBranchName());
-
-                List<Ref> refs = new Git(repo).branchList().call();
-                String fromBranchObjectId = null;
-                String toBranchObjectId = null;
-                for (Ref branchRef : refs) {
-                    String name = branchRef.getObjectId().getName();
-                    if (branchRef.getName().equals(pullRequest.fromBranch)) {
-                        fromBranchObjectId = name;
-                    }
-                    if (branchRef.getName().equals(pullRequest.toBranch)) {
-                        toBranchObjectId = name;
-                    }
-                }
-
-                // from 브랜치와 to 브랜치를 fetch 받았는지 확인한다.
-                ObjectId fromObjectId = repo.resolve(cloneAndFetch.getDestFromBranchName());
-                ObjectId toObjectId = repo.resolve(cloneAndFetch.getDestToBranchName());
-                assertThat(fromObjectId.getName()).isEqualTo(fromBranchObjectId);
-                assertThat(toObjectId.getName()).isEqualTo(toBranchObjectId);
-            }
-        });
     }
 
     private Project createProject(String owner, String name) {
test/support/Git.java
--- test/support/Git.java
+++ test/support/Git.java
@@ -23,15 +23,19 @@
 import org.eclipse.jgit.api.errors.GitAPIException;
 
 import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.RepositoryBuilder;
 import org.eclipse.jgit.revwalk.RevCommit;
+import playRepository.GitRepository;
 
 import java.io.BufferedWriter;
+import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 
 public class Git {
-    public static RevCommit commit(Repository repository, String wcPath, String fileName,
+    public static RevCommit commit(Repository repository, String fileName,
                                    String contents, String commitMessage) throws IOException, GitAPIException {
+        String wcPath = repository.getWorkTree().getAbsolutePath();
         org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repository);
         BufferedWriter out = new BufferedWriter(new FileWriter(wcPath + "/" + fileName));
         out.write(contents);
@@ -39,4 +43,13 @@
         git.add().addFilepattern(fileName).call();
         return git.commit().setMessage(commitMessage).call();
     }
+
+    public static Repository createRepository(String userName, String projectName, boolean bare) throws IOException {
+        String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;
+        String repoPath = wcPath + "/.git";
+        File repoDir = new File(repoPath);
+        Repository repository = new RepositoryBuilder().setGitDir(repoDir).build();
+        repository.create(bare);
+        return repository;
+    }
 }
Add a comment
List