Replace System.out with proper tracing
[egit/spearce.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / BranchOperation.java
blobbf629a4b49ff8dff42386faba439628e5cbab872
1 /*******************************************************************************
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *******************************************************************************/
11 package org.eclipse.egit.core.op;
13 import java.io.File;
14 import java.io.IOException;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IWorkspaceRunnable;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.egit.core.CoreText;
23 import org.eclipse.egit.core.internal.trace.GitTraceLocation;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.team.core.TeamException;
26 import org.eclipse.jgit.errors.CheckoutConflictException;
27 import org.eclipse.jgit.lib.Commit;
28 import org.eclipse.jgit.lib.Constants;
29 import org.eclipse.jgit.lib.GitIndex;
30 import org.eclipse.jgit.lib.RefUpdate;
31 import org.eclipse.jgit.lib.Repository;
32 import org.eclipse.jgit.lib.Tree;
33 import org.eclipse.jgit.lib.WorkDirCheckout;
35 /**
36 * This class implements checkouts of a specific revision. A check
37 * is made that this can be done without data loss.
39 public class BranchOperation implements IWorkspaceRunnable {
41 private final Repository repository;
43 private final String refName;
45 /**
46 * Construct a {@link BranchOperation} object.
47 * @param repository
48 * @param refName Name of git ref to checkout
50 public BranchOperation(Repository repository, String refName) {
51 this.repository = repository;
52 this.refName = refName;
55 private Tree oldTree;
57 private GitIndex index;
59 private Tree newTree;
61 private Commit oldCommit;
63 private Commit newCommit;
67 public void run(IProgressMonitor monitor) throws CoreException {
68 lookupRefs();
69 monitor.worked(1);
71 mapObjects();
72 monitor.worked(1);
74 checkoutTree();
75 monitor.worked(1);
77 writeIndex();
78 monitor.worked(1);
80 updateHeadRef();
81 monitor.worked(1);
83 refreshProjects();
84 monitor.worked(1);
86 monitor.done();
89 private void refreshProjects() {
90 final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
91 .getProjects();
92 final File parentFile = repository.getWorkDir();
93 for (IProject p : projects) {
94 final File file = p.getLocation().toFile();
95 if (file.getAbsolutePath().startsWith(parentFile.getAbsolutePath())) {
96 try {
97 // TODO is this the right location?
98 if (GitTraceLocation.CORE.isActive())
99 GitTraceLocation.getTrace().trace(
100 GitTraceLocation.CORE.getLocation(),
101 "Refreshing " + p); //$NON-NLS-1$
102 p.refreshLocal(IResource.DEPTH_INFINITE, null);
103 } catch (CoreException e) {
104 if (GitTraceLocation.CORE.isActive())
105 GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
111 private void updateHeadRef() throws TeamException {
112 try {
113 RefUpdate u = repository.updateRef(Constants.HEAD);
114 u.setRefLogMessage(NLS.bind(
115 CoreText.BranchOperation_checkoutMovingTo, refName), false);
116 switch (u.link(refName)) {
117 case NEW:
118 case FORCED:
119 case NO_CHANGE:
120 break;
121 default:
122 throw new IOException(u.getResult().name());
124 } catch (IOException e) {
125 throw new TeamException(NLS.bind(
126 CoreText.BranchOperation_updatingHeadToRef, refName), e);
130 private void writeIndex() throws TeamException {
131 try {
132 index.write();
133 } catch (IOException e) {
134 throw new TeamException(CoreText.BranchOperation_writingIndex, e);
138 private void checkoutTree() throws TeamException {
139 try {
140 new WorkDirCheckout(repository, repository.getWorkDir(), oldTree,
141 index, newTree).checkout();
142 } catch (CheckoutConflictException e) {
143 TeamException teamException = new TeamException(e.getMessage());
144 throw teamException;
145 } catch (IOException e) {
146 throw new TeamException(CoreText.BranchOperation_checkoutProblem, e);
150 private void mapObjects() throws TeamException {
151 try {
152 oldTree = oldCommit.getTree();
153 index = repository.getIndex();
154 newTree = newCommit.getTree();
155 } catch (IOException e) {
156 throw new TeamException(CoreText.BranchOperation_mappingTrees, e);
160 private void lookupRefs() throws TeamException {
161 try {
162 newCommit = repository.mapCommit(refName);
163 } catch (IOException e) {
164 throw new TeamException(NLS.bind(
165 CoreText.BranchOperation_mappingCommit, refName), e);
168 try {
169 oldCommit = repository.mapCommit(Constants.HEAD);
170 } catch (IOException e) {
171 throw new TeamException(CoreText.BranchOperation_mappingCommitHead,