/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * 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 git4idea.commands;

import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>Detects the error which happens, when some untracked working tree files prevent merge or checkout.</p>
 *
 * <p>Using a separate detector class instead of defining just an {@link GitMessageWithFilesDetector.Event},
 * because older versions of Git have other format of displaying this error that doesn't match any existing detectors.</p>
 *
 * @author Kirill Likhodedov
 */
public class GitLocalChangesWouldBeOverwrittenDetector extends GitMessageWithFilesDetector {

  private static final Pattern OLD_CHECKOUT_PATTERN = Pattern.compile(
    ".*You have local changes to '(.*)'; cannot switch branches.*"
  );

  private static final Pattern OLD_MERGE_PATTERN = Pattern.compile(
    ".*Your local changes to '(.*)' would be overwritten by merge.*"
  );

  // common for checkout and merge
  public static final Event NEW_PATTERN = new Event(
    "Your local changes to the following files would be overwritten by",
    "commit your changes or stash them before");

  @NotNull private final Operation myOperation;

  public enum Operation {
    CHECKOUT(OLD_CHECKOUT_PATTERN),
    MERGE(OLD_MERGE_PATTERN);

    @NotNull private final Pattern myPattern;

    Operation(@NotNull Pattern pattern) {
      myPattern = pattern;
    }

    @NotNull
    public Pattern getPattern() {
      return myPattern;
    }
  }

  public GitLocalChangesWouldBeOverwrittenDetector(@NotNull VirtualFile root, @NotNull Operation operation) {
    super(NEW_PATTERN, root);
    myOperation = operation;
  }

  @Override
  public void onLineAvailable(@NotNull String line, @NotNull Key outputType) {
    super.onLineAvailable(line, outputType);
    Matcher m = myOperation.getPattern().matcher(line);
    if (m.matches()) {
      myMessageDetected = true;
      myAffectedFiles.add(m.group(1));
    }
  }
}

@@ -1578,16 +1578,231 @@
 %22%0A  );%0A%0A
+  private static final Pattern%5B%5D RESET_PATTERNS = new Pattern%5B%5D%7BPattern.compile(%0A    %22.*Entry '(.*)' not uptodate. Cannot merge.*%22%0A  ),%0A  Pattern.compile(%0A    %22.*Entry '(.*)' would be overwritten by merge.*%22%0A  )%7D;%0A%0A
   // com
@@ -2143,16 +2143,43 @@
 PATTERN)
+,%0A    RESET(RESET_PATTERNS)
 ;%0A%0A    @
@@ -2207,16 +2207,18 @@
  Pattern
+%5B%5D
  myPatte
@@ -2219,16 +2219,17 @@
 yPattern
+s
 ;%0A%0A    O
@@ -2253,24 +2253,27 @@
  Pattern
+...
  pattern
 ) %7B%0A    
@@ -2264,16 +2264,17 @@
  pattern
+s
 ) %7B%0A    
@@ -2284,16 +2284,17 @@
 yPattern
+s
  = patte
@@ -2291,24 +2291,25 @@
 ns = pattern
+s
 ;%0A    %7D%0A%0A   
@@ -2326,22 +2326,17 @@
     
-public 
 Pattern
+%5B%5D
  get
@@ -2342,16 +2342,17 @@
 tPattern
+s
 () %7B%0A   
@@ -2370,16 +2370,17 @@
 yPattern
+s
 ;%0A    %7D%0A
@@ -2702,19 +2702,30 @@
     
-Matcher m =
+for (Pattern pattern :
  myO
@@ -2743,18 +2743,48 @@
 tPattern
+s
 ()
+) %7B%0A      Matcher m = pattern
 .matcher
@@ -2787,24 +2787,26 @@
 cher(line);%0A
+  
     if (m.ma
@@ -2812,24 +2812,26 @@
 atches()) %7B%0A
+  
       myMess
@@ -2856,16 +2856,18 @@
 ;%0A      
+  
 myAffect
@@ -2891,16 +2891,39 @@
 up(1));%0A
+        break;%0A      %7D%0A
     %7D%0A  

Support multiple patterns in local changes overwritten detector + add a pattern for reset --keep