1 /*******************************************************************************
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2008, Google Inc.
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * See LICENSE for the full license text, also available.
9 *******************************************************************************/
10 package org
.spearce
.egit
.core
.op
;
12 import java
.io
.IOException
;
13 import java
.util
.Collection
;
14 import java
.util
.IdentityHashMap
;
17 import org
.eclipse
.core
.resources
.IContainer
;
18 import org
.eclipse
.core
.resources
.IProject
;
19 import org
.eclipse
.core
.resources
.IResource
;
20 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.IAdaptable
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
25 import org
.spearce
.egit
.core
.Activator
;
26 import org
.spearce
.egit
.core
.CoreText
;
27 import org
.spearce
.egit
.core
.project
.GitProjectData
;
28 import org
.spearce
.egit
.core
.project
.RepositoryMapping
;
29 import org
.spearce
.jgit
.dircache
.DirCache
;
30 import org
.spearce
.jgit
.dircache
.DirCacheEntry
;
31 import org
.spearce
.jgit
.lib
.Repository
;
34 * Tell JGit to ignore changes in selected files
36 public class AssumeUnchangedOperation
implements IWorkspaceRunnable
{
37 private final Collection rsrcList
;
39 private final IdentityHashMap
<Repository
, DirCache
> caches
;
41 private final IdentityHashMap
<RepositoryMapping
, Object
> mappings
;
44 * Create a new operation to ignore changes in tracked files
47 * collection of {@link IResource}s which should be ignored when
48 * looking for changes or committing.
50 public AssumeUnchangedOperation(final Collection rsrcs
) {
52 caches
= new IdentityHashMap
<Repository
, DirCache
>();
53 mappings
= new IdentityHashMap
<RepositoryMapping
, Object
>();
56 public void run(IProgressMonitor m
) throws CoreException
{
58 m
= new NullProgressMonitor();
63 m
.beginTask(CoreText
.AssumeUnchangedOperation_adding
,
64 rsrcList
.size() * 200);
66 for (Object obj
: rsrcList
) {
67 obj
= ((IAdaptable
) obj
).getAdapter(IResource
.class);
68 if (obj
instanceof IResource
)
69 assumeValid((IResource
) obj
);
73 for (Map
.Entry
<Repository
, DirCache
> e
: caches
.entrySet()) {
74 final Repository db
= e
.getKey();
75 final DirCache editor
= e
.getValue();
76 m
.setTaskName("Writing index for " + db
.getDirectory());
80 } catch (RuntimeException e
) {
81 throw Activator
.error(CoreText
.UntrackOperation_failed
, e
);
82 } catch (IOException e
) {
83 throw Activator
.error(CoreText
.UntrackOperation_failed
, e
);
85 for (final RepositoryMapping rm
: mappings
.keySet())
86 rm
.fireRepositoryChanged();
93 private void assumeValid(final IResource resource
) throws CoreException
{
94 final IProject proj
= resource
.getProject();
95 final GitProjectData pd
= GitProjectData
.get(proj
);
98 final RepositoryMapping rm
= pd
.getRepositoryMapping(resource
);
101 final Repository db
= rm
.getRepository();
103 DirCache cache
= caches
.get(db
);
106 cache
= DirCache
.lock(db
);
107 } catch (IOException err
) {
108 throw Activator
.error(CoreText
.UntrackOperation_failed
, err
);
110 caches
.put(db
, cache
);
111 mappings
.put(rm
, rm
);
114 final String path
= rm
.getRepoRelativePath(resource
);
115 if (resource
instanceof IContainer
) {
116 for (final DirCacheEntry ent
: cache
.getEntriesWithin(path
))
117 ent
.setAssumeValid(true);
119 final DirCacheEntry ent
= cache
.getEntry(path
);
121 ent
.setAssumeValid(true);