Use Constant.HEAD for "HEAD" references
[egit/imyousuf.git] / org.spearce.egit.core / src / org / spearce / egit / core / op / BranchOperation.java
blobf472f29adf14b0c7826359268a6332da0a68923b
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;
12 import java.io.File;
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;
31 /**
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;
41 /**
42 * Construct a {@link BranchOperation} object.
43 * @param repository
44 * @param refName Name of git ref to checkout
46 public BranchOperation(Repository repository, String refName) {
47 this.repository = repository;
48 this.refName = refName;
51 private Tree oldTree;
53 private GitIndex index;
55 private Tree newTree;
57 private Commit oldCommit;
59 private Commit newCommit;
63 public void run(IProgressMonitor monitor) throws CoreException {
64 lookupRefs();
65 monitor.worked(1);
67 mapObjects();
68 monitor.worked(1);
70 checkoutTree();
71 monitor.worked(1);
73 writeIndex();
74 monitor.worked(1);
76 updateHeadRef();
77 monitor.worked(1);
79 writeHeadReflog();
80 monitor.worked(1);
82 refreshProjects();
83 monitor.worked(1);
85 monitor.done();
88 private void refreshProjects() {
89 final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
90 .getProjects();
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())) {
95 try {
96 System.out.println("Refreshing " + p);
97 p.refreshLocal(IResource.DEPTH_INFINITE, null);
98 } catch (CoreException e) {
99 e.printStackTrace();
105 private void writeHeadReflog() throws TeamException {
106 try {
107 RefLogWriter.writeReflog(repository, oldCommit.getCommitId(),
108 newCommit.getCommitId(), "checkout: moving to " + refName,
109 Constants.HEAD);
110 } catch (IOException e) {
111 throw new TeamException("Writing HEAD's reflog", e);
115 private void updateHeadRef() throws TeamException {
116 try {
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 {
124 try {
125 index.write();
126 } catch (IOException e) {
127 throw new TeamException("Writing index", e);
131 private void checkoutTree() throws TeamException {
132 try {
133 new WorkDirCheckout(repository, repository.getWorkDir(), oldTree,
134 index, newTree).checkout();
135 } catch (CheckoutConflictException e) {
136 TeamException teamException = new TeamException(e.getMessage());
137 throw teamException;
138 } catch (IOException e) {
139 throw new TeamException("Problem while checking out:", e);
143 private void mapObjects() throws TeamException {
144 try {
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 {
154 try {
155 newCommit = repository.mapCommit(refName);
156 } catch (IOException e) {
157 throw new TeamException("Mapping commit: " + refName, e);
160 try {
161 oldCommit = repository.mapCommit(Constants.HEAD);
162 } catch (IOException e) {
163 throw new TeamException("Mapping commit HEAD commit", e);