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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/*******************************************************************************
* 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.egit.core.synchronize.GitCommitsModelCache.RIGHT;
import static org.eclipse.egit.core.synchronize.GitCommitsModelCache.calculateAndSetChangeKind;
import static org.eclipse.jgit.lib.Constants.HEAD;
import static org.eclipse.jgit.lib.FileMode.MISSING;
import static org.eclipse.jgit.lib.FileMode.TREE;
import static org.eclipse.jgit.lib.ObjectId.zeroId;
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.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.EmptyTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
/**
* Builds list of changes in git staging area.
*/
public class StagedChangeCache {
/**
* @param repo
* repository which should be scanned
* @return list of changes in git staging area
*/
public static Map<String, Change> build(Repository repo) {
try (TreeWalk tw = new TreeWalk(repo)) {
tw.addTree(new DirCacheIterator(repo.readDirCache()));
ObjectId headId = repo.resolve(HEAD);
RevCommit headCommit = null;
if (headId != null) {
try (RevWalk rw = new RevWalk(repo)) {
headCommit = rw.parseCommit(headId);
}
}
AbbreviatedObjectId commitId;
if (headCommit != null) {
tw.addTree(headCommit.getTree());
commitId = AbbreviatedObjectId.fromObjectId(headCommit);
} else {
tw.addTree(new EmptyTreeIterator());
commitId =AbbreviatedObjectId.fromObjectId(zeroId());
}
tw.setRecursive(true);
headCommit = null;
MutableObjectId idBuf = new MutableObjectId();
Map<String, Change> result = new HashMap<String, Change>();
while(tw.next()) {
if (!shouldIncludeEntry(tw))
continue;
Change change = new Change();
change.name = tw.getNameString();
change.remoteCommitId = commitId;
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);
}
return result;
} catch (IOException e) {
Activator.logError(e.getMessage(), e);
return new HashMap<String, Change>(0);
}
}
private static boolean shouldIncludeEntry(TreeWalk tw) {
final int mHead = tw.getRawMode(1);
final int mCache = tw.getRawMode(0);
return mHead == MISSING.getBits() // initial add to cache
|| mCache == MISSING.getBits() // removed from cache
|| (mHead != mCache || (mCache != TREE.getBits() && !tw
.idEqual(1, 0))); // modified
}
}
|