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
.team
.core
.TeamException
;
23 import org
.eclipse
.jgit
.errors
.CheckoutConflictException
;
24 import org
.eclipse
.jgit
.lib
.Commit
;
25 import org
.eclipse
.jgit
.lib
.Constants
;
26 import org
.eclipse
.jgit
.lib
.GitIndex
;
27 import org
.eclipse
.jgit
.lib
.RefUpdate
;
28 import org
.eclipse
.jgit
.lib
.Repository
;
29 import org
.eclipse
.jgit
.lib
.Tree
;
30 import org
.eclipse
.jgit
.lib
.WorkDirCheckout
;
33 * This class implements checkouts of a specific revision. A check
34 * is made that this can be done without data loss.
36 public class BranchOperation
implements IWorkspaceRunnable
{
38 private final Repository repository
;
40 private final String refName
;
43 * Construct a {@link BranchOperation} object.
45 * @param refName Name of git ref to checkout
47 public BranchOperation(Repository repository
, String refName
) {
48 this.repository
= repository
;
49 this.refName
= refName
;
54 private GitIndex index
;
58 private Commit oldCommit
;
60 private Commit newCommit
;
64 public void run(IProgressMonitor monitor
) throws CoreException
{
86 private void refreshProjects() {
87 final IProject
[] projects
= ResourcesPlugin
.getWorkspace().getRoot()
89 final File parentFile
= repository
.getWorkDir();
90 for (IProject p
: projects
) {
91 final File file
= p
.getLocation().toFile();
92 if (file
.getAbsolutePath().startsWith(parentFile
.getAbsolutePath())) {
94 System
.out
.println("Refreshing " + p
);
95 p
.refreshLocal(IResource
.DEPTH_INFINITE
, null);
96 } catch (CoreException e
) {
103 private void updateHeadRef() throws TeamException
{
105 RefUpdate u
= repository
.updateRef(Constants
.HEAD
);
106 u
.setRefLogMessage("checkout: moving to " + refName
, false);
107 switch (u
.link(refName
)) {
113 throw new IOException(u
.getResult().name());
115 } catch (IOException e
) {
116 throw new TeamException("Updating HEAD to ref: " + refName
, e
);
120 private void writeIndex() throws TeamException
{
123 } catch (IOException e
) {
124 throw new TeamException("Writing index", e
);
128 private void checkoutTree() throws TeamException
{
130 new WorkDirCheckout(repository
, repository
.getWorkDir(), oldTree
,
131 index
, newTree
).checkout();
132 } catch (CheckoutConflictException e
) {
133 TeamException teamException
= new TeamException(e
.getMessage());
135 } catch (IOException e
) {
136 throw new TeamException("Problem while checking out:", e
);
140 private void mapObjects() throws TeamException
{
142 oldTree
= oldCommit
.getTree();
143 index
= repository
.getIndex();
144 newTree
= newCommit
.getTree();
145 } catch (IOException e
) {
146 throw new TeamException("Mapping trees", e
);
150 private void lookupRefs() throws TeamException
{
152 newCommit
= repository
.mapCommit(refName
);
153 } catch (IOException e
) {
154 throw new TeamException("Mapping commit: " + refName
, e
);
158 oldCommit
= repository
.mapCommit(Constants
.HEAD
);
159 } catch (IOException e
) {
160 throw new TeamException("Mapping commit HEAD commit", e
);