Revert "Rewrite GitMoveDeleteHook to use DirCacheBuilder"
[egit.git] / org.spearce.egit.core / src / org / spearce / egit / core / GitMoveDeleteHook.java
blobcc4059cf4f822f23e2ae8bf0e7ee8c122dba6d5b
1 /*******************************************************************************
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * See LICENSE for the full license text, also available.
8 *******************************************************************************/
9 package org.spearce.egit.core;
11 import java.io.File;
12 import java.io.IOException;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IFolder;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IProjectDescription;
18 import org.eclipse.core.resources.team.IMoveDeleteHook;
19 import org.eclipse.core.resources.team.IResourceTree;
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.spearce.egit.core.project.GitProjectData;
25 import org.spearce.egit.core.project.RepositoryMapping;
26 import org.spearce.jgit.lib.GitIndex;
27 import org.spearce.jgit.lib.Repository;
29 class GitMoveDeleteHook implements IMoveDeleteHook {
30 private static final boolean I_AM_DONE = true;
32 private static final boolean FINISH_FOR_ME = false;
34 private final GitProjectData data;
36 GitMoveDeleteHook(final GitProjectData d) {
37 Assert.isNotNull(d);
38 data = d;
41 public boolean deleteFile(final IResourceTree tree, final IFile file,
42 final int updateFlags, final IProgressMonitor monitor) {
43 try {
44 RepositoryMapping map = RepositoryMapping.getMapping(file);
45 if (map != null) {
46 Repository repository = map.getRepository();
47 GitIndex index = repository.getIndex();
48 if (index.remove(map.getWorkDir(), file.getLocation().toFile()))
49 index.write();
51 return FINISH_FOR_ME;
52 } catch (IOException e) {
53 e.printStackTrace();
54 tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0,
55 CoreText.MoveDeleteHook_operationError, e));
56 return I_AM_DONE;
60 public boolean deleteFolder(final IResourceTree tree, final IFolder folder,
61 final int updateFlags, final IProgressMonitor monitor) {
62 // Deleting a GIT repository which is in use is a pretty bad idea. To
63 // delete disconnect the team provider first.
65 if (data.isProtected(folder)) {
66 return cannotModifyRepository(tree);
67 } else {
68 return FINISH_FOR_ME;
72 public boolean deleteProject(final IResourceTree tree,
73 final IProject project, final int updateFlags,
74 final IProgressMonitor monitor) {
75 // TODO: Note that eclipse thinks folders are real, while
76 // Git does not care.
77 return FINISH_FOR_ME;
80 public boolean moveFile(final IResourceTree tree, final IFile source,
81 final IFile destination, final int updateFlags,
82 final IProgressMonitor monitor) {
83 try {
84 final RepositoryMapping map1 = RepositoryMapping.getMapping(source);
85 if (map1 == null) {
86 // Source is not in a Git controlled project, fine
87 return FINISH_FOR_ME;
89 final GitIndex index1 = map1.getRepository().getIndex();
90 final RepositoryMapping map2 = RepositoryMapping.getMapping(destination);
91 final File sourceFile = source.getLocation().toFile();
92 if (map2 == null) {
93 if (index1.getEntry(Repository.stripWorkDir(map1.getWorkDir(), sourceFile)) == null) {
94 // if the source resource is not tracked by Git that is ok too
95 return FINISH_FOR_ME;
97 tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(),
98 0, "Destination not in a git versioned project", null));
99 return FINISH_FOR_ME;
101 GitIndex index2 = map2.getRepository().getIndex();
102 tree.standardMoveFile(source, destination, updateFlags, monitor);
103 if (index1.remove(map1.getWorkDir(), sourceFile)) {
104 index2.add(map2.getWorkDir(), destination.getLocation().toFile());
105 index1.write();
106 if (index2 != index1)
107 index2.write();
109 return I_AM_DONE;
111 } catch (IOException e) {
112 // Recover properly!
113 e.printStackTrace();
114 tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0,
115 CoreText.MoveDeleteHook_operationError, e));
116 return I_AM_DONE;
121 public boolean moveFolder(final IResourceTree tree, final IFolder source,
122 final IFolder destination, final int updateFlags,
123 final IProgressMonitor monitor) {
124 // TODO: Implement this. Should be relatively easy, but consider that
125 // Eclipse thinks folders are real thinsgs, while Git does not care.
126 return FINISH_FOR_ME;
129 public boolean moveProject(final IResourceTree tree, final IProject source,
130 final IProjectDescription description, final int updateFlags,
131 final IProgressMonitor monitor) {
132 // TODO: We should be able to do this without too much effort when the
133 // projects belong to the same Git repository.
134 return FINISH_FOR_ME;
137 private boolean cannotModifyRepository(final IResourceTree tree) {
138 tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0,
139 CoreText.MoveDeleteHook_cannotModifyFolder, null));
140 return I_AM_DONE;