From 712a15874795ebc75df3c3a30b7aa7eb99aa3f42 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Sun, 25 Dec 2016 18:14:45 +0300 Subject: [PATCH] git: collect affected files from correct stdout/err stream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the message is detected (e.g. "untracked working tree files would be overwritten by merge"), remember the output type, and read the files only from the stream of that type. Before that, since stdout and stderr are reported asynchronously, messages could arrive like that: error: The following untracked working tree files would be overwritten by merge:  Updating 48295bf..cfba4dc  untracked.txt And we treated "Updating..." as a file. This caused blinking of the GitBranchWorkerTest. Also add some debug logging for possible future needs. --- ...ocalChangesWouldBeOverwrittenDetector.java | 5 +-- .../commands/GitMessageWithFilesDetector.java | 39 ++++++++++++------- ...edFilesOverwrittenByOperationDetector.java | 1 + 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/plugins/git4idea/src/git4idea/commands/GitLocalChangesWouldBeOverwrittenDetector.java b/plugins/git4idea/src/git4idea/commands/GitLocalChangesWouldBeOverwrittenDetector.java index e9d8df89614a..adfceb572bd1 100644 --- a/plugins/git4idea/src/git4idea/commands/GitLocalChangesWouldBeOverwrittenDetector.java +++ b/plugins/git4idea/src/git4idea/commands/GitLocalChangesWouldBeOverwrittenDetector.java @@ -48,9 +48,8 @@ public class GitLocalChangesWouldBeOverwrittenDetector extends GitMessageWithFil )}; // 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"); + public static final Event NEW_PATTERN = new Event("LocalChangesDetector", + "Your local changes to the following files would be overwritten by", "commit your changes or stash them before"); @NotNull private final Operation myOperation; diff --git a/plugins/git4idea/src/git4idea/commands/GitMessageWithFilesDetector.java b/plugins/git4idea/src/git4idea/commands/GitMessageWithFilesDetector.java index 383bc64e1fe0..15dc16210dfe 100644 --- a/plugins/git4idea/src/git4idea/commands/GitMessageWithFilesDetector.java +++ b/plugins/git4idea/src/git4idea/commands/GitMessageWithFilesDetector.java @@ -15,10 +15,12 @@ */ package git4idea.commands; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collection; @@ -26,22 +28,22 @@ import java.util.HashSet; import java.util.Set; /** - * The listener of {@link git4idea.commands.GitLineHandler} which watches Git output and detects some message in console, + * The listener of {@link GitLineHandler} which watches Git output and detects some message in console, * captures the following list of files and stops saving them when another message occurs. * * For example, the situation, when local changes would be overwritten by checkout. * * @see GitSimpleEventDetector - * @author Kirill Likhodedov */ public class GitMessageWithFilesDetector implements GitLineHandlerListener { + private static final Logger LOG = Logger.getInstance(GitMessageWithFilesDetector.class); - private final Event myEvent; - private final VirtualFile myRoot; + @NotNull private final Event myEvent; + @NotNull private final VirtualFile myRoot; - protected final Set myAffectedFiles = new HashSet<>(); - protected boolean myMessageDetected; - private boolean myFilesAreDisplayed; + @NotNull protected final Set myAffectedFiles = new HashSet<>(); + protected volatile boolean myMessageDetected; + @Nullable private volatile Key myMessageOutputType; public GitMessageWithFilesDetector(@NotNull Event event, @NotNull VirtualFile root) { myEvent = event; @@ -51,15 +53,21 @@ public class GitMessageWithFilesDetector implements GitLineHandlerListener { @Override public void onLineAvailable(@NotNull String line, @NotNull Key outputType) { if (line.contains(myEvent.getMessageStartMarker())) { + LOG.debug("|" + myEvent.name + "| message start marker detected in [" + line + "]" + "of type " + outputType); myMessageDetected = true; - myFilesAreDisplayed = true; + myMessageOutputType = outputType; } else if (line.contains(myEvent.getMessageEndMarker())) { - myFilesAreDisplayed = false; + LOG.debug("|" + myEvent.name + "| message end marker detected in [" + line + "]" + "of type " + outputType); + myMessageOutputType = null; } - else if (myFilesAreDisplayed) { + else if (outputType.equals(myMessageOutputType)) { + LOG.debug("|" + myEvent.name + "| Treating as a file: [" + line + "]" + "of type " + outputType); myAffectedFiles.add(line.trim()); } + else { + LOG.debug("|" + myEvent.name + "| Plain message: [" + line + "]" + "of type " + outputType); + } } @Override @@ -98,21 +106,24 @@ public class GitMessageWithFilesDetector implements GitLineHandlerListener { } public static class Event { - private final String myMessageStartMarker; - private final String myMessageEndMarker; + @NotNull private final String name; + @NotNull private final String myMessageStartMarker; + @NotNull private final String myMessageEndMarker; - Event(String messageStartMarker, String messageEndMarker) { + Event(@NotNull String eventName, @NotNull String messageStartMarker, @NotNull String messageEndMarker) { + name = eventName; myMessageStartMarker = messageStartMarker; myMessageEndMarker = messageEndMarker; } + @NotNull public String getMessageStartMarker() { return myMessageStartMarker; } + @NotNull public String getMessageEndMarker() { return myMessageEndMarker; } } - } diff --git a/plugins/git4idea/src/git4idea/commands/GitUntrackedFilesOverwrittenByOperationDetector.java b/plugins/git4idea/src/git4idea/commands/GitUntrackedFilesOverwrittenByOperationDetector.java index c4cb4a9232e7..14e6d84016c1 100644 --- a/plugins/git4idea/src/git4idea/commands/GitUntrackedFilesOverwrittenByOperationDetector.java +++ b/plugins/git4idea/src/git4idea/commands/GitUntrackedFilesOverwrittenByOperationDetector.java @@ -37,6 +37,7 @@ public class GitUntrackedFilesOverwrittenByOperationDetector extends GitMessageW ); private static final Event NEW_UNTRACKED_FILES_OVERWRITTEN_BY = new Event( + "UntrackedFileDetector", "The following untracked working tree files would be overwritten by", "Please move or remove them before" );