summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/commands/GitLocalChangesWouldBeOverwrittenDetector.java
blob: e9d8df89614a66da3e85ffa88d2bad8565d40194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * 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.*"
  );

  private static final Pattern[] RESET_PATTERNS = new Pattern[]{Pattern.compile(
    ".*Entry '(.*)' not uptodate. Cannot merge.*"
  ),
  Pattern.compile(
    ".*Entry '(.*)' 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),
    RESET(RESET_PATTERNS);

    @NotNull private final Pattern[] myPatterns;

    Operation(@NotNull Pattern... patterns) {
      myPatterns = patterns;
    }

    @NotNull
    Pattern[] getPatterns() {
      return myPatterns;
    }
  }

  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);
    for (Pattern pattern : myOperation.getPatterns()) {
      Matcher m = pattern.matcher(line);
      if (m.matches()) {
        myMessageDetected = true;
        myAffectedFiles.add(m.group(1));
        break;
      }
    }
  }
}