/*
 * Copyright 2000-2009 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.actions;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.FileStatus;
import com.intellij.openapi.vcs.FileStatusManager;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vcs.changes.ui.RollbackChangesDialog;
import com.intellij.openapi.vfs.VirtualFile;
import git4idea.GitVcs;
import git4idea.i18n.GitBundle;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Git "revert" action
 */
public class GitRevert extends BasicAction {

  @Override
  public boolean perform(@NotNull final Project project, GitVcs vcs, @NotNull final List<VcsException> exceptions, @NotNull VirtualFile[] affectedFiles) {
    final ChangeListManager changeListManager = ChangeListManager.getInstance(project);
    if (changeListManager.isFreezedWithNotification("Can not revert now")) return true;
    final List<Change> changes = new ArrayList<Change>(affectedFiles.length);
    for (VirtualFile f : affectedFiles) {
      Change ch = changeListManager.getChange(f);
      if (ch != null) {
        changes.add(ch);
      }
    }
    RollbackChangesDialog.rollbackChanges(project, changes);
    for (VirtualFile conflictedFile : affectedFiles) {
      final GitRepository repo = GitRepositoryManager.getInstance(project).getRepositoryForFile(conflictedFile);
      if (repo != null) {
        repo.update(GitRepository.TrackedTopic.ALL_CURRENT);
      }
    }
    return false;
  }

  @Override
  @NotNull
  protected String getActionName() {
    return GitBundle.getString("revert.action.name");
  }

  @Override
  protected boolean isEnabled(@NotNull Project project, @NotNull GitVcs vcs, @NotNull VirtualFile... vFiles) {
    for (VirtualFile file : vFiles) {
      FileStatus status = FileStatusManager.getInstance(project).getStatus(file);
      if (status == FileStatus.UNKNOWN || status == FileStatus.NOT_CHANGED) return false;
    }
    return true;
  }
}

@@ -2558,17 +2558,21 @@
 eStatus 
-s
+fileS
 tatus = 
@@ -2637,24 +2637,51 @@
     if (
-s
+file.isDirectory() %7C%7C (fileS
 tatus 
-=
+!
 = FileSt
@@ -2689,27 +2689,35 @@
 tus.
-UNKNOWN %7C%7C s
+NOT_CHANGED && fileS
 tatus 
-=
+!
 = Fi
@@ -2729,28 +2729,35 @@
 tus.
-NOT_CHANGED)
+UNKNOWN)) %7B%0A       
  return 
 fals
@@ -2756,21 +2756,28 @@
 urn 
-fals
+tru
 e;%0A
+  
     %7D%0A
+    %7D%0A
     
@@ -2779,24 +2779,25 @@
 %0A    return 
-tru
+fals
 e;%0A  %7D%0A%7D%0A

enable for directories and if at least one file from the selection can be reverted.