Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 542dc297b8a19d1dc1a76d4b5327155780b78bd2 (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
/*******************************************************************************
 * Copyright (C) 2011, Philipp Thun <philipp.thun@sap.com>
 * Copyright (C) 2011, Dariusz Luksza <dariusz@luksza.org>
 * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.com>
 *
 * 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.ui.internal.decorators;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

import org.eclipse.egit.core.internal.indexdiff.IndexDiffData;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.GitLabels;
import org.eclipse.jgit.lib.BranchTrackingStatus;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryState;
import org.eclipse.jgit.revwalk.RevCommit;

/**
 * Helper class to create decoratable resources
 *
 * @see IDecoratableResource
 */
public class DecoratableResourceHelper {

	/**
	 * Maps repository to the branch state. The entries are removed each time
	 * {@link IndexDiffData} changes
	 *
	 * @see GitLightweightDecorator#indexDiffChanged(Repository,
	 *      org.eclipse.egit.core.internal.indexdiff.IndexDiffData)
	 */
	private static Map<Repository, String> branchState = Collections
			.synchronizedMap(new WeakHashMap<Repository, String>());

	static String getRepositoryName(Repository repository) {
		String repoName = Activator.getDefault().getRepositoryUtil()
				.getRepositoryName(repository);
		RepositoryState state = repository.getRepositoryState();
		if (state != RepositoryState.SAFE)
			return repoName + '|' + state.getDescription();
		else
			return repoName;
	}

	static String getShortBranch(Repository repository) throws IOException {
		return Activator.getDefault().getRepositoryUtil()
				.getShortBranch(repository);
	}

	static RevCommit getHeadCommit(Repository repository) {
		return Activator.getDefault().getRepositoryUtil()
				.parseHeadCommit(repository);
	}

	static String getBranchStatus(Repository repo) throws IOException {
		String cachedStatus = branchState.get(repo);
		if (cachedStatus != null)
			return cachedStatus;

		String branchName = repo.getBranch();
		if (branchName == null)
			return null;

		BranchTrackingStatus status = BranchTrackingStatus.of(repo, branchName);
		if (status == null)
			return null;

		if (status.getAheadCount() == 0 && status.getBehindCount() == 0)
			return null;

		String formattedStatus = GitLabels.formatBranchTrackingStatus(status);
		branchState.put(repo, formattedStatus);
		return formattedStatus;
	}

	static void clearState(Repository repo) {
		branchState.remove(repo);
	}
}

Back to the top