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
;
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
) {
41 public boolean deleteFile(final IResourceTree tree
, final IFile file
,
42 final int updateFlags
, final IProgressMonitor monitor
) {
44 RepositoryMapping map
= RepositoryMapping
.getMapping(file
);
46 Repository repository
= map
.getRepository();
47 GitIndex index
= repository
.getIndex();
48 if (index
.remove(map
.getWorkDir(), file
.getLocation().toFile()))
52 } catch (IOException e
) {
54 tree
.failed(new Status(IStatus
.ERROR
, Activator
.getPluginId(), 0,
55 CoreText
.MoveDeleteHook_operationError
, e
));
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
);
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
80 public boolean moveFile(final IResourceTree tree
, final IFile source
,
81 final IFile destination
, final int updateFlags
,
82 final IProgressMonitor monitor
) {
84 final RepositoryMapping map1
= RepositoryMapping
.getMapping(source
);
86 // Source is not in a Git controlled project, fine
89 final GitIndex index1
= map1
.getRepository().getIndex();
90 final RepositoryMapping map2
= RepositoryMapping
.getMapping(destination
);
91 final File sourceFile
= source
.getLocation().toFile();
93 if (index1
.getEntry(Repository
.stripWorkDir(map1
.getWorkDir(), sourceFile
)) == null) {
94 // if the source resource is not tracked by Git that is ok too
97 tree
.failed(new Status(IStatus
.ERROR
, Activator
.getPluginId(),
98 0, "Destination not in a git versioned project", null));
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());
106 if (index2
!= index1
)
111 } catch (IOException e
) {
114 tree
.failed(new Status(IStatus
.ERROR
, Activator
.getPluginId(), 0,
115 CoreText
.MoveDeleteHook_operationError
, e
));
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));