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
;
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
;
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
;
46 * Construct a {@link BranchOperation} object.
48 * @param refName Name of git ref to checkout
50 public BranchOperation(Repository repository
, String refName
) {
51 this.repository
= repository
;
52 this.refName
= refName
;
57 private GitIndex index
;
61 private Commit oldCommit
;
63 private Commit newCommit
;
67 public void run(IProgressMonitor monitor
) throws CoreException
{
89 private void refreshProjects() {
90 final IProject
[] projects
= ResourcesPlugin
.getWorkspace().getRoot()
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())) {
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
{
113 RefUpdate u
= repository
.updateRef(Constants
.HEAD
);
114 u
.setRefLogMessage(NLS
.bind(
115 CoreText
.BranchOperation_checkoutMovingTo
, refName
), false);
116 switch (u
.link(refName
)) {
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
{
133 } catch (IOException e
) {
134 throw new TeamException(CoreText
.BranchOperation_writingIndex
, e
);
138 private void checkoutTree() throws TeamException
{
140 new WorkDirCheckout(repository
, repository
.getWorkDir(), oldTree
,
141 index
, newTree
).checkout();
142 } catch (CheckoutConflictException e
) {
143 TeamException teamException
= new TeamException(e
.getMessage());
145 } catch (IOException e
) {
146 throw new TeamException(CoreText
.BranchOperation_checkoutProblem
, e
);
150 private void mapObjects() throws TeamException
{
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
{
162 newCommit
= repository
.mapCommit(refName
);
163 } catch (IOException e
) {
164 throw new TeamException(NLS
.bind(
165 CoreText
.BranchOperation_mappingCommit
, refName
), e
);
169 oldCommit
= repository
.mapCommit(Constants
.HEAD
);
170 } catch (IOException e
) {
171 throw new TeamException(CoreText
.BranchOperation_mappingCommitHead
,