Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 801aa1052739008616bff2f82edcd92e9f61ea60 (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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
 * Copyright (C) 2010, 2013 Dariusz Luksza <dariusz@luksza.org> and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.internal.synchronize.model;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.synchronize.GitCommitsModelCache;
import org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change;
import org.eclipse.egit.core.synchronize.GitCommitsModelCache.Commit;
import org.eclipse.egit.core.synchronize.StagedChangeCache;
import org.eclipse.egit.core.synchronize.WorkingTreeChangeCache;
import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData;
import org.eclipse.egit.ui.Activator;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.treewalk.filter.TreeFilter;

/**
 * Representation of Git repository in Git ChangeSet model.
 */
public class GitModelRepository extends GitModelObjectContainer implements HasProjects {

	private IPath location;

	private final GitSynchronizeData gsd;

	private GitModelObject[] children;

	/**
	 * @param gsd
	 *            synchronization data
	 * @throws IOException
	 */
	public GitModelRepository(GitSynchronizeData gsd) throws IOException {
		super(null);
		this.gsd = gsd;
	}

	@Override
	public GitModelObject[] getChildren() {
		List<GitModelObjectContainer> result = new ArrayList<>();
		Repository repo = gsd.getRepository();
		RevCommit srcRevCommit = gsd.getSrcRevCommit();
		RevCommit dstRevCommit = gsd.getDstRevCommit();
		TreeFilter pathFilter = gsd.getPathFilter();
		List<Commit> commitCache;
		if (srcRevCommit != null && dstRevCommit != null)
			try {
				commitCache = GitCommitsModelCache.build(repo, srcRevCommit,
						dstRevCommit, pathFilter);
			} catch (IOException e) {
				Activator.logError(e.getMessage(), e);
				commitCache = null;
			}
		else
			commitCache = null;
		if (commitCache != null && !commitCache.isEmpty())
			result.addAll(getListOfCommit(commitCache));

		result.addAll(getWorkingChanges());
		disposeOldChildren();
		children = result.toArray(new GitModelObjectContainer[0]);

		return children;
	}

	@Override
	public String getName() {
		return gsd.getRepository().getWorkTree().toString();
	}

	@Override
	public IProject[] getProjects() {
		return gsd.getProjects().toArray(new IProject[0]);
	}

	@Override
	public int repositoryHashCode() {
		return hashCode();
	}

	/**
	 * @return source {@link RevObject}
	 */
	public ObjectId getSrcRev() {
		return gsd.getSrcRevCommit();
	}

	@Override
	public IPath getLocation() {
		if (location == null)
			location = new Path(gsd.getRepository().getWorkTree().toString());

		return location;
	}

	@Override
	public int getKind() {
		return Differencer.CHANGE;
	}

	@Override
	public void dispose() {
		disposeOldChildren();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;

		if (obj instanceof GitModelRepository) {
			File objWorkTree = ((GitModelRepository) obj).gsd.getRepository()
					.getWorkTree();
			return objWorkTree.equals(gsd.getRepository().getWorkTree());
		}

		return false;
	}

	@Override
	public int hashCode() {
		return gsd.getRepository().getWorkTree().hashCode();
	}

	@Override
	public String toString() {
		return "ModelRepository[" + gsd.getRepository().getWorkTree() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	private List<GitModelObjectContainer> getListOfCommit(List<Commit> commitCache) {
		Repository repo = gsd.getRepository();
		Set<IProject> projectsSet = gsd.getProjects();
		IProject[] projects = projectsSet.toArray(new IProject[0]);
		List<GitModelObjectContainer> result = new ArrayList<>();

		for (Commit commit : commitCache)
			result.add(new GitModelCommit(this, repo, commit, projects));

		return result;
	}

	private List<GitModelObjectContainer> getWorkingChanges() {
		List<GitModelObjectContainer> result = new ArrayList<>();
		if (gsd.shouldIncludeLocal()) {
			Repository repo = gsd.getRepository();
			Map<String, Change> stagedChanges = StagedChangeCache.build(repo);
			GitModelCache gitCache = new GitModelCache(this, repo,
					stagedChanges);
			int gitCacheLen = gitCache.getChildren().length;

			Map<String, Change> workingChanges = WorkingTreeChangeCache.build(repo);
			GitModelWorkingTree gitWorkingTree = new GitModelWorkingTree(this,
					repo, workingChanges);
			int gitWorkingTreeLen = gitWorkingTree.getChildren().length;

			if (gitCacheLen > 0 || gitWorkingTreeLen > 0) {
				result.add(gitCache);
				result.add(gitWorkingTree);
			}
		}

		return result;
	}

	/**
	 * @return repository
	 */
	public Repository getRepository() {
		return gsd.getRepository();
	}

	private void disposeOldChildren() {
		if (children == null)
			return;
		for (GitModelObject child : children)
			child.dispose();

	}

}

Back to the top