Update EGit to match ref API changes
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / BranchOperation.java
blob9f174036144690e5caeba0c4455f6a910896e29d
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.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;
32 /**
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;
42 /**
43 * Construct a {@link BranchOperation} object.
44 * @param repository
45 * @param refName Name of git ref to checkout
47 public BranchOperation(Repository repository, String refName) {
48 this.repository = repository;
49 this.refName = refName;
52 private Tree oldTree;
54 private GitIndex index;
56 private Tree newTree;
58 private Commit oldCommit;
60 private Commit newCommit;
64 public void run(IProgressMonitor monitor) throws CoreException {
65 lookupRefs();
66 monitor.worked(1);
68 mapObjects();
69 monitor.worked(1);
71 checkoutTree();
72 monitor.worked(1);
74 writeIndex();
75 monitor.worked(1);
77 updateHeadRef();
78 monitor.worked(1);
80 refreshProjects();
81 monitor.worked(1);
83 monitor.done();
86 private void refreshProjects() {
87 final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
88 .getProjects();
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())) {
93 try {
94 System.out.println("Refreshing " + p);
95 p.refreshLocal(IResource.DEPTH_INFINITE, null);
96 } catch (CoreException e) {
97 e.printStackTrace();
103 private void updateHeadRef() throws TeamException {
104 try {
105 RefUpdate u = repository.updateRef(Constants.HEAD);
106 u.setRefLogMessage("checkout: moving to " + refName, false);
107 switch (u.link(refName)) {
108 case NEW:
109 case FORCED:
110 case NO_CHANGE:
111 break;
112 default:
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 {
121 try {
122 index.write();
123 } catch (IOException e) {
124 throw new TeamException("Writing index", e);
128 private void checkoutTree() throws TeamException {
129 try {
130 new WorkDirCheckout(repository, repository.getWorkDir(), oldTree,
131 index, newTree).checkout();
132 } catch (CheckoutConflictException e) {
133 TeamException teamException = new TeamException(e.getMessage());
134 throw teamException;
135 } catch (IOException e) {
136 throw new TeamException("Problem while checking out:", e);
140 private void mapObjects() throws TeamException {
141 try {
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 {
151 try {
152 newCommit = repository.mapCommit(refName);
153 } catch (IOException e) {
154 throw new TeamException("Mapping commit: " + refName, e);
157 try {
158 oldCommit = repository.mapCommit(Constants.HEAD);
159 } catch (IOException e) {
160 throw new TeamException("Mapping commit HEAD commit", e);