Package org.eclipse.jgit.api

Examples of org.eclipse.jgit.api.MergeResult


        RevCommit developCommit = GitHelper.getLatestCommit(git,gfConfig.getDevelop());
        RevCommit featureCommit = GitHelper.getLatestCommit(git, prefixedBranchName);

        List<RevCommit> commitList = ImmutableList.copyOf(git.log().setMaxCount(2).addRange(developCommit,featureCommit).call());
       
        MergeResult mergeResult = null;
               
        if(commitList.size() < 2)
        {
            mergeResult = git.merge().setFastForward(MergeCommand.FastForwardMode.FF).include(featureBranch).call();
        }
        else
        {
            if(squash)
            {
                mergeResult = git.merge().setSquash(true).include(featureBranch).call();
                git.commit().call();
                this.forceDeleteBranch = true;
            }
            else
            {
                mergeResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(featureBranch).call();
            }
        }
       
        if(null == mergeResult || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.FAILED) || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
        {
            Files.createParentDirs(mergeBase);
            Files.touch(mergeBase);
            Files.write(gfConfig.getDevelop(),mergeBase,Charset.forName("UTF-8"));
            throw new MergeConflictsNotResolvedException("merge conflicts exist, please resolve!");
View Full Code Here


         */
        if (!GitHelper.isMergedInto(git, prefixedReleaseName, gfConfig.getMaster()))
        {
            git.checkout().setName(gfConfig.getMaster()).call();

            MergeResult masterResult = null;

            if (squash)
            {
                masterResult = git.merge().setSquash(true).include(releaseBranch).call();
                git.commit().call();
            }
            else
            {
                masterResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(releaseBranch).call();
            }
        }

        if (!noTag)
        {
            /*
            try to tag the release
            in case a previous attempt to finish this release branch has failed,
            but the tag was successful, we skip it now
            */
            String tagName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.VERSIONTAG.configKey()) + releaseName;
            if(!GitHelper.tagExists(git,tagName))
            {
                git.tag().setName(tagName).setMessage(message).setObjectId(releaseCommit).call();
            }
        }
       
        /*
        try to merge into develop
        in case a previous attempt to finish this release branch has failed,
        but the merge into develop was successful, we skip it now
         */
        if (!GitHelper.isMergedInto(git, prefixedReleaseName, gfConfig.getDevelop()))
        {
            git.checkout().setName(gfConfig.getDevelop()).call();

            MergeResult developResult = null;

            if (squash)
            {
                developResult = git.merge().setSquash(true).include(releaseBranch).call();
                git.commit().call();
View Full Code Here

                RevCommit developCommit = GitHelper.getLatestCommit(git, gfConfig.getDevelop());
                RevCommit featureCommit = GitHelper.getLatestCommit(git, prefixedBranchName);
   
                List<RevCommit> commitList = IterableHelper.asList(git.log().setMaxCount(2).addRange(developCommit, featureCommit).call());
   
                MergeResult mergeResult = null;
   
                if (commitList.size() < 2)
                {
                    mergeResult = git.merge().setFastForward(MergeCommand.FastForwardMode.FF).include(featureBranch).call();
                    if(mergeResult.getMergeStatus().isSuccessful())
                    {
                        git.commit().setMessage("merging " + prefixedBranchName + "' into develop").call();
                    }
                }
                else
                {
                    if (squash)
                    {
                        mergeResult = git.merge().setSquash(true).include(featureBranch).call();
                        if(mergeResult.getMergeStatus().isSuccessful())
                        {
                            git.commit().setMessage("squashing " + prefixedBranchName + "' into develop").call();
                        }
                        this.forceDeleteBranch = true;
                    }
                    else
                    {
                        mergeResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(featureBranch).call();
                        if(mergeResult.getMergeStatus().isSuccessful())
                        {
                            git.commit().setMessage("merging " + prefixedBranchName + "' into develop").call();
                        }
                    }
                }
   
                if (null == mergeResult || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.FAILED) || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
                {
                    FileHelper.createParentDirs(mergeBase);
                    FileUtils.createNewFile(mergeBase);
                    FileHelper.writeStringToFile(gfConfig.getDevelop(), mergeBase);
                    reporter.endCommand();
View Full Code Here

        .setSquash(squash).setFastForward(ff).setCommit(!noCommit);
    if (srcRef != null)
      mergeCmd.include(srcRef);
    else
      mergeCmd.include(src);
    MergeResult result;
    try {
      result = mergeCmd.call();
    } catch (CheckoutConflictException e) {
      result = new MergeResult(e.getConflictingPaths()); // CHECKOUT_CONFLICT
    }

    switch (result.getMergeStatus()) {
    case ALREADY_UP_TO_DATE:
      if (squash)
        outw.print(CLIText.get().nothingToSquash);
      outw.println(CLIText.get().alreadyUpToDate);
      break;
    case FAST_FORWARD:
      ObjectId oldHeadId = oldHead.getObjectId();
      outw.println(MessageFormat.format(CLIText.get().updating, oldHeadId
          .abbreviate(7).name(), result.getNewHead().abbreviate(7)
          .name()));
      outw.println(result.getMergeStatus().toString());
      break;
    case CHECKOUT_CONFLICT:
      outw.println(CLIText.get().mergeCheckoutConflict);
      for (String collidingPath : result.getCheckoutConflicts())
        outw.println("\t" + collidingPath); //$NON-NLS-1$
      outw.println(CLIText.get().mergeCheckoutFailed);
      break;
    case CONFLICTING:
      for (String collidingPath : result.getConflicts().keySet())
        outw.println(MessageFormat.format(CLIText.get().mergeConflict,
            collidingPath));
      outw.println(CLIText.get().mergeFailed);
      break;
    case FAILED:
      for (Map.Entry<String, MergeFailureReason> entry : result
          .getFailingPaths().entrySet())
        switch (entry.getValue()) {
        case DIRTY_WORKTREE:
        case DIRTY_INDEX:
          outw.println(CLIText.get().dontOverwriteLocalChanges);
          outw.println("        " + entry.getKey()); //$NON-NLS-1$
          break;
        case COULD_NOT_DELETE:
          outw.println(CLIText.get().cannotDeleteFile);
          outw.println("        " + entry.getKey()); //$NON-NLS-1$
          break;
        }
      break;
    case MERGED:
      String name;
      if (!isMergedInto(oldHead, src))
        name = mergeStrategy.getName();
      else
        name = "recursive"; //$NON-NLS-1$
      outw.println(MessageFormat.format(CLIText.get().mergeMadeBy, name));
      break;
    case MERGED_NOT_COMMITTED:
      outw.println(CLIText.get().mergeWentWellStoppedBeforeCommitting);
      break;
    case MERGED_SQUASHED:
    case FAST_FORWARD_SQUASHED:
    case MERGED_SQUASHED_NOT_COMMITTED:
      outw.println(CLIText.get().mergedSquashed);
      outw.println(CLIText.get().mergeWentWellStoppedBeforeCommitting);
      break;
    case ABORTED:
      throw die(CLIText.get().ffNotPossibleAborting);
    case NOT_SUPPORTED:
      outw.println(MessageFormat.format(
          CLIText.get().unsupportedOperation, result.toString()));
    }
  }
View Full Code Here

         */
        if (!GitHelper.isMergedInto(git, prefixedHotfixName, gfConfig.getMaster()))
        {
            git.checkout().setName(gfConfig.getMaster()).call();

            MergeResult masterResult = masterResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(hotfixBranch).call();
        }

        if (!noTag)
        {
            /*
            try to tag the release
            in case a previous attempt to finish this release branch has failed,
            but the tag was successful, we skip it now
            */
            String tagName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.VERSIONTAG.configKey()) + hotfixName;
            if(!GitHelper.tagExists(git,tagName))
            {
                git.tag().setName(tagName).setMessage(message).setObjectId(hotfixCommit).call();
            }
        }
       
        /*
        try to merge into develop
        in case a previous attempt to finish this release branch has failed,
        but the merge into develop was successful, we skip it now
         */
        if (!GitHelper.isMergedInto(git, prefixedHotfixName, gfConfig.getDevelop()))
        {
            git.checkout().setName(gfConfig.getDevelop()).call();

            MergeResult developResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(hotfixBranch).call();
        }

        if(!keepBranch)
        {
            //make sure we're on master
View Full Code Here

        String prefixedHotfixName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.HOTFIX.configKey()) + hotfixName;

        requireLocalBranchExists(prefixedHotfixName);
        requireCleanWorkingTree();

        MergeResult developResult = new MergeResult(null,null,new ObjectId[] { null, null }, MergeResult.MergeStatus.ALREADY_UP_TO_DATE, MergeStrategy.RESOLVE,null);
        MergeResult masterResult = new MergeResult(null,null,new ObjectId[] { null, null }, MergeResult.MergeStatus.ALREADY_UP_TO_DATE,MergeStrategy.RESOLVE,null);
        try
        {
            if (fetch)
            {
                RefSpec developSpec = new RefSpec("+" + Constants.R_HEADS + gfConfig.getDevelop() + ":" + Constants.R_REMOTES + "origin/" + gfConfig.getDevelop());
                RefSpec masterSpec = new RefSpec("+" + Constants.R_HEADS + gfConfig.getMaster() + ":" + Constants.R_REMOTES + "origin/" + gfConfig.getMaster());

                git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(masterSpec).call();
                git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(developSpec).call();
                git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).call();
            }

            if (GitHelper.remoteBranchExists(git, prefixedHotfixName, reporter))
            {
                requireLocalBranchNotBehindRemote(prefixedHotfixName);
            }

            if (GitHelper.remoteBranchExists(git, gfConfig.getMaster(), reporter))
            {
                requireLocalBranchNotBehindRemote(gfConfig.getMaster());
            }

            if (GitHelper.remoteBranchExists(git, gfConfig.getDevelop(), reporter))
            {
                requireLocalBranchNotBehindRemote(gfConfig.getDevelop());
            }

            Ref hotfixBranch = GitHelper.getLocalBranch(git, prefixedHotfixName);
            RevCommit hotfixCommit = GitHelper.getLatestCommit(git, prefixedHotfixName);
       
        /*
        try to merge into master
        in case a previous attempt to finish this release branch has failed,
        but the merge into master was successful, we skip it now
         */
            if (!GitHelper.isMergedInto(git, prefixedHotfixName, gfConfig.getMaster()))
            {
                git.checkout().setName(gfConfig.getMaster()).call();

                masterResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(hotfixBranch).call();
            }

            if (!noTag && masterResult.getMergeStatus().isSuccessful())
            {
            /*
            try to tag the release
            in case a previous attempt to finish this release branch has failed,
            but the tag was successful, we skip it now
            */
                String tagName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.VERSIONTAG.configKey()) + hotfixName;
                if (!GitHelper.tagExists(git, tagName))
                {
                    reporter.infoText(getCommandName(), "tagging hotfix with name:" + tagName);
                    git.tag().setName(tagName).setMessage(message).setObjectId(hotfixCommit).call();
                }
            }
       
        /*
        try to merge into develop
        in case a previous attempt to finish this release branch has failed,
        but the merge into develop was successful, we skip it now
         */
            if (!GitHelper.isMergedInto(git, prefixedHotfixName, gfConfig.getDevelop()))
            {
                git.checkout().setName(gfConfig.getDevelop()).call();

                developResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(hotfixBranch).call();
            }

            if (push && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
            {
                //push to develop
                RefSpec developSpec = new RefSpec(gfConfig.getDevelop());
                git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(developSpec).call();

                //push to master
                RefSpec masterSpec = new RefSpec(gfConfig.getMaster());
                git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(masterSpec).call();

                if (!noTag)
                {
                    git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setPushTags().call();
                }

                if (GitHelper.remoteBranchExists(git, prefixedHotfixName, reporter))
                {
                    RefSpec branchSpec = new RefSpec(prefixedHotfixName);
                    git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(branchSpec).call();
                }
            }

            if (!keepBranch && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
            {
                git.checkout().setName(gfConfig.getDevelop()).call();
                git.branchDelete().setForce(true).setBranchNames(prefixedHotfixName).call();

                if (push && GitHelper.remoteBranchExists(git, prefixedHotfixName, reporter))
View Full Code Here

                RevCommit developCommit = GitHelper.getLatestCommit(git, gfConfig.getDevelop());
                RevCommit featureCommit = GitHelper.getLatestCommit(git, prefixedBranchName);
   
                List<RevCommit> commitList = IterableHelper.asList(git.log().setMaxCount(2).addRange(developCommit, featureCommit).call());
   
                MergeResult mergeResult = null;
   
                if (commitList.size() < 2)
                {
                    mergeResult = git.merge().setFastForward(MergeCommand.FastForwardMode.FF).include(featureBranch).call();
                    if(mergeResult.getMergeStatus().isSuccessful())
                    {
                        git.commit().setMessage("merging '" + prefixedBranchName + "' into develop").call();
                    }
                }
                else
                {
                    if (squash)
                    {
                        mergeResult = git.merge().setSquash(true).include(featureBranch).call();
                        if(mergeResult.getMergeStatus().isSuccessful())
                        {
                            git.commit().setMessage("squashing '" + prefixedBranchName + "' into develop").call();
                        }
                        this.forceDeleteBranch = true;
                    }
                    else
                    {
                        mergeResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(featureBranch).call();
                    }
                }
   
                if (null == mergeResult || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.FAILED) || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
                {
                    FileHelper.createParentDirs(mergeBase);
                    FileUtils.createNewFile(mergeBase);
                    FileHelper.writeStringToFile(gfConfig.getDevelop(), mergeBase);
                    reporter.endCommand();
View Full Code Here

        String prefixedReleaseName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.RELEASE.configKey()) + releaseName;

        requireLocalBranchExists(prefixedReleaseName);
        requireCleanWorkingTree();

        MergeResult developResult = new MergeResult(null, null, new ObjectId[]{null, null}, MergeResult.MergeStatus.ALREADY_UP_TO_DATE, MergeStrategy.RESOLVE, null);
        MergeResult masterResult = new MergeResult(null, null, new ObjectId[]{null, null}, MergeResult.MergeStatus.ALREADY_UP_TO_DATE, MergeStrategy.RESOLVE, null);
        try
        {
            if (fetch)
            {
                RefSpec developSpec = new RefSpec("+" + Constants.R_HEADS + gfConfig.getDevelop() + ":" + Constants.R_REMOTES + "origin/" + gfConfig.getDevelop());
                RefSpec masterSpec = new RefSpec("+" + Constants.R_HEADS + gfConfig.getMaster() + ":" + Constants.R_REMOTES + "origin/" + gfConfig.getMaster());

                git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(masterSpec).call();
                git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(developSpec).call();
                git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).call();
            }

            if (GitHelper.remoteBranchExists(git, prefixedReleaseName, reporter))
            {
                requireLocalBranchNotBehindRemote(prefixedReleaseName);
            }
           
            if (GitHelper.remoteBranchExists(git, gfConfig.getMaster(), reporter))
            {
                requireLocalBranchNotBehindRemote(gfConfig.getMaster());
            }

            if (GitHelper.remoteBranchExists(git, gfConfig.getDevelop(), reporter))
            {
                requireLocalBranchNotBehindRemote(gfConfig.getDevelop());
            }

            Ref releaseBranch = GitHelper.getLocalBranch(git, prefixedReleaseName);
       
        if(!noMerge)
        {
            /*
            try to merge into master
            in case a previous attempt to finish this release branch has failed,
            but the merge into master was successful, we skip it now
             */
                if (!GitHelper.isMergedInto(git, prefixedReleaseName, gfConfig.getMaster()))
                {
                    git.checkout().setName(gfConfig.getMaster()).call();
                    reporter.infoText(getCommandName(), "merging '" + prefixedReleaseName + "' into master...");
                    if (squash)
                    {
                        reporter.infoText(getCommandName(), "squashing merge");
                        masterResult = git.merge().setSquash(true).include(releaseBranch).call();
                        if(masterResult.getMergeStatus().isSuccessful())
                        {
                            git.commit().setMessage("squashing '" + prefixedReleaseName + "' into master").call();
                        }
                    }
                    else
                    {
                        masterResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(releaseBranch).call();
                    }
                }
   
                reporter.mergeResult(getCommandName(), masterResult);
   
                if (!masterResult.getMergeStatus().isSuccessful())
                {
                    reporter.errorText(getCommandName(), "merge into master was not successful! Aborting the release...");
                    if (masterResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
                    {
                        reporter.errorText(getCommandName(), "please resolve your merge conflicts and re-run " + getCommandName());
                    }
                    else
                    {
                        reporter.errorText(getCommandName(), "until JGit supports merge resets, please run 'git reset --merge' to get back to a clean state");
                    }
                }
               
                /*
                try to merge into develop
                in case a previous attempt to finish this release branch has failed,
                but the merge into develop was successful, we skip it now
                 */
                if (!GitHelper.isMergedInto(git, prefixedReleaseName, gfConfig.getDevelop()))
                {
                    reporter.infoText(getCommandName(), "merging '" + prefixedReleaseName + "' into develop...");
                    git.checkout().setName(gfConfig.getDevelop()).call();
   
                    if (squash)
                    {
                        reporter.infoText(getCommandName(), "squashing merge");
                        developResult = git.merge().setSquash(true).include(releaseBranch).call();
                        if(developResult.getMergeStatus().isSuccessful())
                        {
                            git.commit().setMessage("squashing '" + prefixedReleaseName + "' into develop").call();
                        }
                    }
                    else
                    {
                        developResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(releaseBranch).call();
                    }
                }
   
                reporter.mergeResult(getCommandName(), developResult);
               
                if (!developResult.getMergeStatus().isSuccessful())
                {
                    reporter.errorText(getCommandName(), "merge into develop was not successful! Aborting the release...");
                    if (developResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
                    {
                        reporter.errorText(getCommandName(), "please resolve your merge conflicts and re-run " + getCommandName());
                    }
                    else
                    {
                        reporter.errorText(getCommandName(), "until JGit supports merge resets, please run 'git reset --merge' to get back to a clean state");
                    }
                }
            }

            if (!noTag && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
            {
                git.checkout().setName(gfConfig.getMaster()).call();

            /*
            try to tag the release
            in case a previous attempt to finish this release branch has failed,
            but the tag was successful, we skip it now
            */
                String tagName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.VERSIONTAG.configKey()) + releaseName;
                if (!GitHelper.tagExists(git, tagName))
                {
                    reporter.infoText(getCommandName(), "tagging release with name:" + tagName);
                    git.tag().setName(tagName).setMessage(message).call();
                }
            }

            if (push && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
            {
                reporter.infoText(getCommandName(), "pushing changes to origin...");
                //push to develop
                reporter.infoText(getCommandName(), "pushing develop");
                RefSpec developSpec = new RefSpec(gfConfig.getDevelop());
                git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(developSpec).call();
               

                //push to master
                reporter.infoText(getCommandName(), "pushing master");
                RefSpec masterSpec = new RefSpec(gfConfig.getMaster());
                git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(masterSpec).call();

                if (!noTag)
                {
                    reporter.infoText(getCommandName(), "pushing tags");
                    git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setPushTags().call();
                }

                if (GitHelper.remoteBranchExists(git, prefixedReleaseName, reporter))
                {
                    reporter.infoText(getCommandName(), "pushing release branch");
                    RefSpec branchSpec = new RefSpec(prefixedReleaseName);
                    git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(branchSpec).call();
                }
            }

            if (!keepBranch && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
            {
                reporter.infoText(getCommandName(), "deleting local release branch");
                git.checkout().setName(gfConfig.getDevelop()).call();
                git.branchDelete().setForce(true).setBranchNames(prefixedReleaseName).call();

View Full Code Here

                RevCommit developCommit = GitHelper.getLatestCommit(git, gfConfig.getDevelop());
                RevCommit featureCommit = GitHelper.getLatestCommit(git, prefixedBranchName);
   
                List<RevCommit> commitList = IterableHelper.asList(git.log().setMaxCount(2).addRange(developCommit, featureCommit).call());
   
                MergeResult mergeResult = null;
   
                if (commitList.size() < 2)
                {
                    mergeResult = git.merge().setFastForward(MergeCommand.FastForwardMode.FF).include(featureBranch).call();
                    if(mergeResult.getMergeStatus().isSuccessful())
                    {
                        git.commit().setMessage(getScmMessagePrefix() + "merging '" + prefixedBranchName + "' into '" + gfConfig.getDevelop() + "'").call();
                    }
                }
                else
                {
                    if (squash)
                    {
                        mergeResult = git.merge().setSquash(true).include(featureBranch).call();
                        if(mergeResult.getMergeStatus().isSuccessful())
                        {
                            git.commit().setMessage(getScmMessagePrefix() + "squashing '" + prefixedBranchName + "' into '" + gfConfig.getDevelop() + "'").call();
                        }
                        this.forceDeleteBranch = true;
                    }
                    else
                    {
                        mergeResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(featureBranch).call();
                    }
                }
   
                if (null == mergeResult || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.FAILED) || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
                {
                    FileHelper.createParentDirs(mergeBase);
                    FileUtils.createNewFile(mergeBase);
                    FileHelper.writeStringToFile(gfConfig.getDevelop(), mergeBase);
                    reporter.endCommand();
View Full Code Here

        String prefixedReleaseName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.RELEASE.configKey()) + releaseName;

        requireLocalBranchExists(prefixedReleaseName);
        requireCleanWorkingTree();

        MergeResult developResult = new MergeResult(null, null, new ObjectId[]{null, null}, MergeResult.MergeStatus.ALREADY_UP_TO_DATE, MergeStrategy.RESOLVE, null);
        MergeResult masterResult = new MergeResult(null, null, new ObjectId[]{null, null}, MergeResult.MergeStatus.ALREADY_UP_TO_DATE, MergeStrategy.RESOLVE, null);
        try
        {
            if (fetch)
            {
                RefSpec developSpec = new RefSpec("+" + Constants.R_HEADS + gfConfig.getDevelop() + ":" + Constants.R_REMOTES + "origin/" + gfConfig.getDevelop());
                RefSpec masterSpec = new RefSpec("+" + Constants.R_HEADS + gfConfig.getMaster() + ":" + Constants.R_REMOTES + "origin/" + gfConfig.getMaster());

                git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(masterSpec).call();
                git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(developSpec).call();
                git.fetch().setRemote(Constants.DEFAULT_REMOTE_NAME).call();
            }

            if (GitHelper.remoteBranchExists(git, prefixedReleaseName, reporter))
            {
                requireLocalBranchNotBehindRemote(prefixedReleaseName);
            }
           
            if (GitHelper.remoteBranchExists(git, gfConfig.getMaster(), reporter))
            {
                requireLocalBranchNotBehindRemote(gfConfig.getMaster());
            }

            if (GitHelper.remoteBranchExists(git, gfConfig.getDevelop(), reporter))
            {
                requireLocalBranchNotBehindRemote(gfConfig.getDevelop());
            }

            Ref releaseBranch = GitHelper.getLocalBranch(git, prefixedReleaseName);
       
        if(!noMerge)
        {
            /*
            try to merge into master
            in case a previous attempt to finish this release branch has failed,
            but the merge into master was successful, we skip it now
             */
                if (!GitHelper.isMergedInto(git, prefixedReleaseName, gfConfig.getMaster()))
                {
                    git.checkout().setName(gfConfig.getMaster()).call();
                    reporter.infoText(getCommandName(), "merging '" + prefixedReleaseName + "' into '" + gfConfig.getMaster() + "'...");
                    if (squash)
                    {
                        reporter.infoText(getCommandName(), "squashing merge");
                        masterResult = git.merge().setSquash(true).include(releaseBranch).call();
                        if(masterResult.getMergeStatus().isSuccessful())
                        {
                            git.commit().setMessage(getScmMessagePrefix() + "squashing '" + prefixedReleaseName + "' into '" + gfConfig.getMaster() + "'").call();
                        }
                    }
                    else
                    {
                        masterResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(releaseBranch).call();
                    }
                }
   
                reporter.mergeResult(getCommandName(), masterResult);
   
                if (!masterResult.getMergeStatus().isSuccessful())
                {
                    reporter.errorText(getCommandName(), "merge into '" + gfConfig.getMaster() + "' was not successful! Aborting the release...");
                    if (masterResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
                    {
                        reporter.errorText(getCommandName(), "please resolve your merge conflicts and re-run " + getCommandName());
                    }
                    else
                    {
                        reporter.errorText(getCommandName(), "until JGit supports merge resets, please run 'git reset --merge' to get back to a clean state");
                    }
                }
               
                /*
                try to merge into develop
                in case a previous attempt to finish this release branch has failed,
                but the merge into develop was successful, we skip it now
                 */
                if (!GitHelper.isMergedInto(git, prefixedReleaseName, gfConfig.getDevelop()))
                {
                    reporter.infoText(getCommandName(), "merging '" + prefixedReleaseName + "' into '" + gfConfig.getDevelop() + "'...");
                    git.checkout().setName(gfConfig.getDevelop()).call();
   
                    if (squash)
                    {
                        reporter.infoText(getCommandName(), "squashing merge");
                        developResult = git.merge().setSquash(true).include(releaseBranch).call();
                        if(developResult.getMergeStatus().isSuccessful())
                        {
                            git.commit().setMessage(getScmMessagePrefix() + "squashing '" + prefixedReleaseName + "' into '" + gfConfig.getDevelop() + "'").call();
                        }
                    }
                    else
                    {
                        developResult = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(releaseBranch).call();
                    }
                }
   
                reporter.mergeResult(getCommandName(), developResult);
               
                if (!developResult.getMergeStatus().isSuccessful())
                {
                    reporter.errorText(getCommandName(), "merge into '" + gfConfig.getDevelop() + "' was not successful! Aborting the release...");
                    if (developResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING))
                    {
                        reporter.errorText(getCommandName(), "please resolve your merge conflicts and re-run " + getCommandName());
                    }
                    else
                    {
                        reporter.errorText(getCommandName(), "until JGit supports merge resets, please run 'git reset --merge' to get back to a clean state");
                    }
                }
            }

            if (!noTag && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
            {
                /*
                Change to the release branch to create the tag so that
                'git describe' will continue to work.
                See: https://github.com/nvie/gitflow/commit/aa93d2346f840e16a05c6546e1e22c1dddfbc997
                 */
              git.checkout().setName(releaseBranch.getName()).call();

            /*
            try to tag the release
            in case a previous attempt to finish this release branch has failed,
            but the tag was successful, we skip it now
            */
                String tagName = gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.VERSIONTAG.configKey()) + releaseName;
                if (!GitHelper.tagExists(git, tagName))
                {
                    reporter.infoText(getCommandName(), "tagging release with name:" + tagName);
                    git.tag().setName(tagName).setMessage(getScmMessagePrefix() + message).call();
                }
            }

            if (push && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
            {
                reporter.infoText(getCommandName(), "pushing changes to origin...");
                //push to develop
                reporter.infoText(getCommandName(), "pushing '" + gfConfig.getDevelop() + "'");
                RefSpec developSpec = new RefSpec(gfConfig.getDevelop());
                git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(developSpec).call();
               

                //push to master
                reporter.infoText(getCommandName(), "pushing '" + gfConfig.getMaster() + "'");
                RefSpec masterSpec = new RefSpec(gfConfig.getMaster());
                git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(masterSpec).call();

                if (!noTag)
                {
                    reporter.infoText(getCommandName(), "pushing tags");
                    git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setPushTags().call();
                }

                if (GitHelper.remoteBranchExists(git, prefixedReleaseName, reporter))
                {
                    reporter.infoText(getCommandName(), "pushing release branch");
                    RefSpec branchSpec = new RefSpec(prefixedReleaseName);
                    git.push().setRemote(Constants.DEFAULT_REMOTE_NAME).setRefSpecs(branchSpec).call();
                }
            }

            if (!keepBranch && masterResult.getMergeStatus().isSuccessful() && developResult.getMergeStatus().isSuccessful())
            {
                reporter.infoText(getCommandName(), "deleting local release branch");
                git.checkout().setName(gfConfig.getDevelop()).call();
                git.branchDelete().setForce(true).setBranchNames(prefixedReleaseName).call();

View Full Code Here

TOP

Related Classes of org.eclipse.jgit.api.MergeResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.