Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5bc03a06ba1f3a47a5359f09ca28b85bc184f5c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*******************************************************************************
 * Copyright (C) 2011, Dariusz Luksza <dariusz@luksza.org>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.core.synchronize;

import static org.eclipse.compare.structuremergeviewer.Differencer.RIGHT;
import static org.eclipse.egit.core.synchronize.GitCommitsModelCache.calculateAndSetChangeKind;
import static org.eclipse.jgit.treewalk.filter.TreeFilter.ANY_DIFF;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change;
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.MutableObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
import org.eclipse.jgit.treewalk.filter.NotIgnoredFilter;

/**
 * Builds list of working tree changes.
 */
public class WorkingTreeChangeCache {

	/**
	 * @param repo
	 *            with should be scanned
	 * @return list of changes in working tree
	 */
	public static Map<String, Change> build(Repository repo) {
		TreeWalk tw = new TreeWalk(repo);
		try {
			tw.addTree(new FileTreeIterator(repo));
			tw.addTree(new DirCacheIterator(repo.readDirCache()));
			tw.setFilter(AndTreeFilter.create(new NotIgnoredFilter(0), ANY_DIFF));
			tw.setRecursive(true);

			Map<String, Change> result = new HashMap<String, Change>();
			MutableObjectId idBuf = new MutableObjectId();
			while (tw.next()) {
				Change change = new Change();
				change.name = tw.getNameString();
				tw.getObjectId(idBuf, 0);
				change.objectId = AbbreviatedObjectId.fromObjectId(idBuf);
				tw.getObjectId(idBuf, 1);
				change.remoteObjectId = AbbreviatedObjectId.fromObjectId(idBuf);
				calculateAndSetChangeKind(RIGHT, change);

				result.put(tw.getPathString(), change);
			}
			tw.release();

			return result;
		} catch (IOException e) {
			Activator.error(e.getMessage(), e);
			return new HashMap<String, GitCommitsModelCache.Change>(0);
		}
	}

}

Back to the top