Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 575d50d9c4287003ab3db9c5706752ca85ca8ec2 (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
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 *
 * 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/**
 * 
 */
package org.eclipse.team.internal.ccvs.core.filesystem;

import java.util.HashMap;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSTag;
import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile;
import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder;
import org.eclipse.team.internal.ccvs.core.ICVSRemoteResource;
import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.ICVSResource;
import org.eclipse.team.internal.ccvs.core.resources.RemoteFolderTree;

class RLogTreeBuilder {

	private ICVSRepositoryLocation location;
	private RemoteFolderTree tree;
	private CVSTag tag;
	private HashMap folderMap;
	private HashMap logMap;
	private LogEntryCache cache;

	public RLogTreeBuilder(ICVSRepositoryLocation location, CVSTag tag, LogEntryCache cache) {
		this.tag = tag;
		this.location = location;
		this.cache = cache;
		reset();
	}

	public RemoteFolderTree getTree() {
		return tree;
	}

	/**
	 * Reset the builder to prepare for a new build
	 */
	public void reset() {
		folderMap = new HashMap(16);
		logMap = new HashMap(16);
		tree = new RemoteFolderTree(null, location, ICVSRemoteFolder.REPOSITORY_ROOT_FOLDER_NAME, tag);
		tree.setChildren(new ICVSRemoteResource[0]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ccvs.core.client.listeners.RDiffSummaryListener.IFileDiffListener#newFile(java.lang.String, java.lang.String)
	 */
	public void newFile(IPath remoteFilePath, ICVSRemoteFile remoteFile) {
		try {
			addFile(tree, tag, remoteFile, remoteFilePath);
		} catch (CVSException e) {
		}
	}

	private void addFile(RemoteFolderTree tree, CVSTag tag, ICVSRemoteFile file, IPath filePath) throws CVSException {
		RemoteFolderTree parent = (RemoteFolderTree) getFolder(tree, tag, filePath.removeLastSegments(1), Path.EMPTY);
		addChild(parent, file);
	}

	private void addChild(RemoteFolderTree tree, ICVSRemoteResource resource) {
		//get the log entry info for this file and save it
		logMap.put(resource, cache.getLogEntry(resource));

		ICVSRemoteResource[] children = tree.getChildren();
		ICVSRemoteResource[] newChildren;
		if (children == null) {
			newChildren = new ICVSRemoteResource[] {resource};
		} else {
			newChildren = new ICVSRemoteResource[children.length + 1];
			System.arraycopy(children, 0, newChildren, 0, children.length);
			newChildren[children.length] = resource;
		}
		tree.setChildren(newChildren);
	}

	/* 
	 * Get the folder at the given path in the given tree, creating any missing folders as needed.
	 */
	private ICVSRemoteFolder getFolder(RemoteFolderTree tree, CVSTag tag, IPath remoteFolderPath, IPath parentPath) throws CVSException {
		if (remoteFolderPath.segmentCount() == 0)
			return tree;
		String name = remoteFolderPath.segment(0);
		ICVSResource child;
		IPath childPath = parentPath.append(name);
		if (tree.childExists(name)) {
			child = tree.getChild(name);
		} else {
			child = new RemoteFolderTree(tree, tree.getRepository(), childPath.toString(), tag);
			//Save this folder in hash map
			folderMap.put(childPath.toString(), child);
			((RemoteFolderTree) child).setChildren(new ICVSRemoteResource[0]);
			addChild(tree, (ICVSRemoteResource) child);
		}
		return getFolder((RemoteFolderTree) child, tag, remoteFolderPath.removeFirstSegments(1), childPath);
	}

	public HashMap getFolderMap() {
		return folderMap;
	}

	public HashMap getLogMap() {
		return logMap;
	}
}

Back to the top