jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/329610 )

Change subject: Get conflict results from diff3
......................................................................


Get conflict results from diff3

This patch extends the global wfMerge function to also return the
result of the first merge attempt that detects merge conflicts.

The additional output explicitly names the conflicting lines and
could help when solving edit conflicts.

Bug: T151320
Change-Id: I97acebdc87b31779200c7fde4dd4449cd1ee8ead
---
M includes/GlobalFunctions.php
M tests/phpunit/includes/GlobalFunctions/GlobalTest.php
2 files changed, 44 insertions(+), 8 deletions(-)

Approvals:
  Addshore: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index bb1951d..1a33b76 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -2404,9 +2404,10 @@
  * @param string $mine
  * @param string $yours
  * @param string &$result
+ * @param string &$mergeAttemptResult
  * @return bool
  */
-function wfMerge( $old, $mine, $yours, &$result ) {
+function wfMerge( $old, $mine, $yours, &$result, &$mergeAttemptResult = null ) 
{
        global $wgDiff3;
 
        # This check may also protect against code injection in
@@ -2442,13 +2443,18 @@
                $oldtextName, $yourtextName );
        $handle = popen( $cmd, 'r' );
 
-       if ( fgets( $handle, 1024 ) ) {
-               $conflict = true;
-       } else {
-               $conflict = false;
-       }
+       $mergeAttemptResult = '';
+       do {
+               $data = fread( $handle, 8192 );
+               if ( strlen( $data ) == 0 ) {
+                       break;
+               }
+               $mergeAttemptResult .= $data;
+       } while ( true );
        pclose( $handle );
 
+       $conflict = $mergeAttemptResult !== '';
+
        # Merge differences
        $cmd = Shell::escape( $wgDiff3, '-a', '-e', '--merge', $mytextName,
                $oldtextName, $yourtextName );
diff --git a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php 
b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php
index 5e54b8d..d961e41 100644
--- a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php
+++ b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php
@@ -474,25 +474,45 @@
        }
 
        /**
+        * @covers ::wfMerge
+        */
+       public function testMerge_worksWithLessParameters() {
+               $this->markTestSkippedIfNoDiff3();
+
+               $mergedText = null;
+               $successfulMerge = wfMerge( "old1\n\nold2", "old1\n\nnew2", 
"new1\n\nold2", $mergedText );
+
+               $mergedText = null;
+               $conflictingMerge = wfMerge( 'old', 'old and mine', 'old and 
yours', $mergedText );
+
+               $this->assertEquals( true, $successfulMerge );
+               $this->assertEquals( false, $conflictingMerge );
+       }
+
+       /**
         * @param string $old Text as it was in the database
         * @param string $mine Text submitted while user was editing
         * @param string $yours Text submitted by the user
         * @param bool $expectedMergeResult Whether the merge should be a 
success
         * @param string $expectedText Text after merge has been completed
+        * @param string $expectedMergeAttemptResult Diff3 output if conflicts 
occur
         *
         * @dataProvider provideMerge()
         * @group medium
         * @covers ::wfMerge
         */
-       public function testMerge( $old, $mine, $yours, $expectedMergeResult, 
$expectedText ) {
+       public function testMerge( $old, $mine, $yours, $expectedMergeResult, 
$expectedText,
+                                                          
$expectedMergeAttemptResult ) {
                $this->markTestSkippedIfNoDiff3();
 
                $mergedText = null;
-               $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
+               $attemptMergeResult = null;
+               $isMerged = wfMerge( $old, $mine, $yours, $mergedText, 
$mergeAttemptResult );
 
                $msg = 'Merge should be a ';
                $msg .= $expectedMergeResult ? 'success' : 'failure';
                $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
+               $this->assertEquals( $expectedMergeAttemptResult, 
$mergeAttemptResult );
 
                if ( $isMerged ) {
                        // Verify the merged text
@@ -530,6 +550,9 @@
                                "one one one ONE ONE\n" .
                                        "\n" .
                                        "two two TWO TWO\n", // note: will 
always end in a newline
+
+                               // mergeAttemptResult:
+                               "",
                        ],
 
                        // #1: conflict, fail
@@ -552,6 +575,13 @@
 
                                // result:
                                null,
+
+                               // mergeAttemptResult:
+                               "1,3c\n" .
+                               "one one one\n" .
+                               "\n" .
+                               "two two\n" .
+                               ".\n",
                        ],
                ];
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/329610
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I97acebdc87b31779200c7fde4dd4449cd1ee8ead
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: WMDE-Fisch <christoph.jau...@wikimedia.de>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Andrew-WMDE <andrew.kos...@wikimedia.de>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de>
Gerrit-Reviewer: WMDE-Fisch <christoph.jau...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to