private boolean mergeBranch( String value, String mergeStrategy ) { try { ObjectId obj = git.getRepository().resolve( value ); MergeResult result = git.merge() .include( obj ) .setStrategy( MergeStrategy.get( mergeStrategy ) ) .call(); if ( result.getMergeStatus().isSuccessful() ) { showMessageBox( BaseMessages.getString( PKG, "Dialog.Success" ), BaseMessages.getString( PKG, "Dialog.Success" ) ); return true; } else { showMessageBox( BaseMessages.getString( PKG, "Dialog.Error" ), result.getMergeStatus().toString() ); if ( result.getMergeStatus() == MergeStatus.CONFLICTING ) { result.getConflicts().keySet().forEach( path -> { checkout( path, Constants.HEAD, ".ours" ); checkout( path, getExpandedName( value, IVCS.TYPE_BRANCH ), ".theirs" ); } ); return true; } } } catch ( Exception e ) { showMessageBox( BaseMessages.getString( PKG, "Dialog.Error" ), e.getMessage() ); } return false; }
@Override public void doExecute() { try { PullCommand pullCommand = git.pull().setRebase(rebase); if (getProgressMonitor() != null) { pullCommand.setProgressMonitor(getProgressMonitor()); } setupCredentials(pullCommand); PullResult pullResult = pullCommand.call(); if (!pullResult.isSuccessful()) { FetchResult fetchResult = pullResult.getFetchResult(); GitTaskUtils.validateTrackingRefUpdates(MESSAGE_PULLED_FAILED, fetchResult.getTrackingRefUpdates()); MergeStatus mergeStatus = pullResult.getMergeResult().getMergeStatus(); if (!mergeStatus.isSuccessful()) { throw new BuildException(String.format(MESSAGE_PULLED_FAILED_WITH_STATUS, mergeStatus.name())); } } } catch (Exception e) { throw new GitBuildException(String.format(MESSAGE_PULLED_FAILED_WITH_URI, getUri()), e); } }
@Test public void remoteToLocal_merge() throws Exception { pushMirrorSettings(null, null); // Mirror an empty git repository, which will; // - Create /mirror_state.json // - Remove the sample files created by createProject(). mirroringService.mirror().join(); // Create a text file, modify it in two branches ('master' and 'fork') and merge 'fork' into 'master'. addToGitIndex("alphabets.txt", // 'c' and 'x' are missing. "a\nb\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\ny\nz\n"); git.commit().setMessage("Add alphabets.txt").call(); //// Create a new branch 'fork' and add the missing 'x'. git.checkout().setCreateBranch(true).setName("fork").call(); addToGitIndex("alphabets.txt", // Add the missing 'x'. "a\nb\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n"); final RevCommit commit1 = git.commit().setMessage("Add missing 'x'").call(); //// Check out 'master' and add the missing 'c'. git.checkout().setName("master").call(); addToGitIndex("alphabets.txt", // Add the missing 'c'. "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\ny\nz\n"); final RevCommit commit2 = git.commit().setMessage("Add missing 'c'").call(); //// Merge 'fork' into 'master' to create a merge commit. final MergeResult mergeResult = git.merge() .include(commit1.getId()) .setFastForward(FastForwardMode.NO_FF) .setMessage("Merge 'fork'").call(); //// Make sure the merge commit has been added. assertThat(mergeResult.getMergeStatus()).isEqualTo(MergeStatus.MERGED); final RevCommit lastCommit = git.log().all().call().iterator().next(); assertThat(lastCommit.getParentCount()).isEqualTo(2); assertThat(lastCommit.getParents()).containsExactlyInAnyOrder(commit1, commit2); // Run the mirror and ensure alphabets.txt contains all alphabets. mirroringService.mirror().join(); final Entry<JsonNode> expectedMirrorState = expectedMirrorState("/"); final Entry<String> expectedAlphabets = Entry.ofText( "/alphabets.txt", "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n"); assertThat(client.getFiles(projName, REPO_MAIN, Revision.HEAD, "/**").join().values()) .containsExactlyInAnyOrder(expectedMirrorState, expectedAlphabets); }
private Pair<Boolean, String> checkResult( String branchToUpdate, String branchHead, Pair<Boolean, String> ret, MergeResult mergeResult) throws IOException { if (mergeResult.getMergeStatus().equals(MergeStatus.CONFLICTING) || mergeResult.getMergeStatus().equals(MergeStatus.FAILED)) { VerigreenLogger.get().log( getClass().getName(), RuntimeUtils.getCurrentMethodName(), String.format( "Merge conflicts for parent_branch:%s into:%s. rejecting commit", branchHead, branchToUpdate)); reset(_repo.getRef(branchToUpdate).getName()); } else if (mergeResult.getMergeStatus().equals(MergeStatus.ALREADY_UP_TO_DATE) || mergeResult.getMergeStatus().equals(MergeStatus.FAST_FORWARD)) { VerigreenLogger.get().log( getClass().getName(), RuntimeUtils.getCurrentMethodName(), String.format( "Merge not needed for parent_branch:%s into:%s", branchHead, branchToUpdate)); ret = new Pair<>(true, ""); } else if (mergeResult.getMergeStatus().equals(MergeStatus.MERGED_NOT_COMMITTED)) { String autoMergeMessage = createMessageAutoCommit(mergeResult); String commitId = commit(commited_By_Collector, email_Address, autoMergeMessage ); String adjustCommitId = commitId.substring(0,7) + "_" + commited_By_Collector; VerigreenLogger.get().log( getClass().getName(), RuntimeUtils.getCurrentMethodName(), String.format( "Verigreen merge for parent_branch:%s into:%s was not committed. Performing auto commit [%s]", branchHead, branchToUpdate, adjustCommitId)); ret = new Pair<>(true, adjustCommitId); }else if (mergeResult.getMergeStatus().equals(MergeStatus.MERGED)) { VerigreenLogger.get().log( getClass().getName(), RuntimeUtils.getCurrentMethodName(), "Merge was made after diverted branch with auto commit"); ret = new Pair<>(true, ""); new RestClientImpl().post(CollectorApi.getPostVerigreenNeededRequest(mergeResult.getNewHead().getName().substring(0, 7))); } return ret; }
@Override public void execute(Wandora wandora, Context context) { try { Git git = getGit(); if(git != null) { if(isNotEmpty(getGitRemoteUrl())) { PullCommand pull = git.pull(); String user = getUsername(); if(user == null) { if(pullUI == null) { pullUI = new PullUI(); } pullUI.setUsername(getUsername()); pullUI.setPassword(getPassword()); pullUI.setRemoteUrl(getGitRemoteUrl()); pullUI.openInDialog(); if(pullUI.wasAccepted()) { setUsername(pullUI.getUsername()); setPassword(pullUI.getPassword()); // setGitRemoteUrl(pullUI.getRemoteUrl()); // pull.setRemote(pullUI.getRemoteUrl()); if(isNotEmpty(getUsername())) { CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider( getUsername(), getPassword() ); pull.setCredentialsProvider(credentialsProvider); } } else { return; } } setDefaultLogger(); setLogTitle("Git pull"); log("Pulling changes from remote repository..."); PullResult result = pull.call(); FetchResult fetchResult = result.getFetchResult(); MergeResult mergeResult = result.getMergeResult(); MergeStatus mergeStatus = mergeResult.getMergeStatus(); String fetchResultMessages = fetchResult.getMessages(); if(isNotEmpty(fetchResultMessages)) { log(fetchResult.getMessages()); } log(mergeStatus.toString()); if(mergeStatus.equals(MergeStatus.MERGED)) { int a = WandoraOptionPane.showConfirmDialog(wandora, "Reload Wandora project after pull?", "Reload Wandora project after pull?", WandoraOptionPane.YES_NO_OPTION); if(a == WandoraOptionPane.YES_OPTION) { reloadWandoraProject(); } } log("Ready."); } else { log("Repository has no remote origin and can't be pulled. " + "Initialize repository by cloning remote repository to set the remote origin."); } } else { logAboutMissingGitRepository(); } } catch(GitAPIException gae) { log(gae.toString()); } catch(NoWorkTreeException nwte) { log(nwte.toString()); } catch(Exception e) { log(e); } setState(WAIT); }