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 * See LICENSE for the full license text, also available.
9 *******************************************************************************/
10 package org
.spearce
.egit
.core
.op
;
13 import java
.io
.IOException
;
15 import org
.eclipse
.core
.resources
.IProject
;
16 import org
.eclipse
.core
.resources
.IResource
;
17 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
18 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
19 import org
.eclipse
.core
.runtime
.CoreException
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.team
.core
.TeamException
;
22 import org
.spearce
.jgit
.errors
.CheckoutConflictException
;
23 import org
.spearce
.jgit
.lib
.Commit
;
24 import org
.spearce
.jgit
.lib
.Constants
;
25 import org
.spearce
.jgit
.lib
.GitIndex
;
26 import org
.spearce
.jgit
.lib
.RefLogWriter
;
27 import org
.spearce
.jgit
.lib
.Repository
;
28 import org
.spearce
.jgit
.lib
.Tree
;
29 import org
.spearce
.jgit
.lib
.WorkDirCheckout
;
32 * This class implements checkouts of a specific revision. A check
33 * is made that this can be done without data loss.
35 public class BranchOperation
implements IWorkspaceRunnable
{
37 private final Repository repository
;
39 private final String refName
;
42 * Construct a {@link BranchOperation} object.
44 * @param refName Name of git ref to checkout
46 public BranchOperation(Repository repository
, String refName
) {
47 this.repository
= repository
;
48 this.refName
= refName
;
53 private GitIndex index
;
57 private Commit oldCommit
;
59 private Commit newCommit
;
63 public void run(IProgressMonitor monitor
) throws CoreException
{
88 private void refreshProjects() {
89 final IProject
[] projects
= ResourcesPlugin
.getWorkspace().getRoot()
91 final File parentFile
= repository
.getWorkDir();
92 for (IProject p
: projects
) {
93 final File file
= p
.getLocation().toFile();
94 if (file
.getAbsolutePath().startsWith(parentFile
.getAbsolutePath())) {
96 System
.out
.println("Refreshing " + p
);
97 p
.refreshLocal(IResource
.DEPTH_INFINITE
, null);
98 } catch (CoreException e
) {
105 private void writeHeadReflog() throws TeamException
{
107 RefLogWriter
.writeReflog(repository
, oldCommit
.getCommitId(),
108 newCommit
.getCommitId(), "checkout: moving to " + refName
,
110 } catch (IOException e
) {
111 throw new TeamException("Writing HEAD's reflog", e
);
115 private void updateHeadRef() throws TeamException
{
117 repository
.writeSymref(Constants
.HEAD
, refName
);
118 } catch (IOException e
) {
119 throw new TeamException("Updating HEAD to ref: " + refName
, e
);
123 private void writeIndex() throws TeamException
{
126 } catch (IOException e
) {
127 throw new TeamException("Writing index", e
);
131 private void checkoutTree() throws TeamException
{
133 new WorkDirCheckout(repository
, repository
.getWorkDir(), oldTree
,
134 index
, newTree
).checkout();
135 } catch (CheckoutConflictException e
) {
136 TeamException teamException
= new TeamException(e
.getMessage());
138 } catch (IOException e
) {
139 throw new TeamException("Problem while checking out:", e
);
143 private void mapObjects() throws TeamException
{
145 oldTree
= oldCommit
.getTree();
146 index
= repository
.getIndex();
147 newTree
= newCommit
.getTree();
148 } catch (IOException e
) {
149 throw new TeamException("Mapping trees", e
);
153 private void lookupRefs() throws TeamException
{
155 newCommit
= repository
.mapCommit(refName
);
156 } catch (IOException e
) {
157 throw new TeamException("Mapping commit: " + refName
, e
);
161 oldCommit
= repository
.mapCommit(Constants
.HEAD
);
162 } catch (IOException e
) {
163 throw new TeamException("Mapping commit HEAD commit", e
);