Skip to content

Instantly share code, notes, and snippets.

@taichi
Last active October 7, 2015 03:27
Show Gist options
  • Select an option

  • Save taichi/3097455 to your computer and use it in GitHub Desktop.

Select an option

Save taichi/3097455 to your computer and use it in GitHub Desktop.
createOrphanBranch
/*
* Copyright (C) 2013 SATO taichi <ryushi@gmail.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.EnumSet;
import org.eclipse.jgit.api.CheckoutResult.Status;
import org.eclipse.jgit.api.errors.CheckoutConflictException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRefNameException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
import org.eclipse.jgit.api.errors.RefNotFoundException;
import org.eclipse.jgit.dircache.DirCacheCheckout;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.util.StringUtils;
/**
* Create a new orphan branch.
*
* @author taichi
* @see <a
* href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html"
* >Git documentation about Checkout</a>
*/
public class CreateOrphanBranchCommand extends GitCommand<Ref> {
String name;
String startPoint;
RevCommit startCommit;
CheckoutResult status;
/**
* @param repo
*/
protected CreateOrphanBranchCommand(Repository repo) {
super(repo);
}
/**
* @param name
* the name of the new branch
* @return this instance
*/
public CreateOrphanBranchCommand setName(String name) {
this.checkCallable();
this.name = name;
return this;
}
/**
* Set the name of the commit that should be checked out.
* <p>
* When checking out files and this is not specified or <code>null</code>,
* the index is used.
* <p>
* When creating a new branch, this will be used as the start point. If not
* specified or <code>null</code>, the current HEAD is used.
*
* @param startPoint
* commit name to check out
* @return this instance
*/
public CreateOrphanBranchCommand setStartPoint(String startPoint) {
this.checkCallable();
this.startPoint = startPoint;
this.startCommit = null;
return this;
}
/**
* Set the commit that should be checked out.
* <p>
* When creating a new branch, this will be used as the start point. If not
* specified or <code>null</code>, the current HEAD is used.
* <p>
* When checking out files and this is not specified or <code>null</code>,
* the index is used.
*
* @param startCommit
* commit to check out
* @return this instance
*/
public CreateOrphanBranchCommand setStartPoint(RevCommit startCommit) {
this.checkCallable();
this.startPoint = null;
this.startCommit = startCommit;
return this;
}
@Override
public Ref call() throws GitAPIException, RefNotFoundException,
CheckoutConflictException, InvalidRefNameException,
RefAlreadyExistsException {
this.checkCallable();
try {
this.processOptions();
this.checkoutStartPoint();
RefUpdate update = this.getRepository().updateRef(Constants.HEAD);
Result r = update.link(this.getBranchName());
if (!EnumSet.of(Result.NEW, Result.FORCED).contains(r))
throw new RefAlreadyExistsException(MessageFormat.format(
JGitText.get().checkoutUnexpectedResult, r.name()));
this.setCallable(false);
return this.getRepository().getRef(Constants.HEAD);
} catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e);
}
}
private void processOptions() throws InvalidRefNameException,
RefAlreadyExistsException, IOException {
if (name == null || !Repository.isValidRefName(getBranchName()))
throw new InvalidRefNameException(MessageFormat.format(JGitText
.get().branchNameInvalid, name == null ? "<null>" : name)); //$NON-NLS-1$
Ref refToCheck = this.getRepository().getRef(getBranchName());
if (refToCheck != null)
throw new RefAlreadyExistsException(MessageFormat.format(
JGitText.get().refAlreadyExists, name));
}
private String getBranchName() {
if (name.startsWith(Constants.R_REFS))
return name;
return Constants.R_HEADS + name;
}
private void checkoutStartPoint() throws GitAPIException,
RefNotFoundException, CheckoutConflictException, IOException {
ObjectId sp = this.getStartPoint();
if (sp != null)
this.checkout(sp);
}
private ObjectId getStartPoint() throws RefNotFoundException, IOException {
if (this.startCommit != null)
return this.startCommit.getId();
if (!StringUtils.isEmptyOrNull(this.startPoint)) {
ObjectId oid = this.getRepository().resolve(this.startPoint);
if (oid == null)
throw new RefNotFoundException(MessageFormat.format(
JGitText.get().refNotResolved, this.startPoint));
return oid;
}
return null;
}
private void checkout(ObjectId fromId) throws GitAPIException,
CheckoutConflictException, IOException {
RevWalk rw = new RevWalk(this.getRepository());
try {
Ref headRef = this.repo.getRef(Constants.HEAD);
AnyObjectId headId = headRef.getObjectId();
RevCommit headCommit = headId == null ? null : rw
.parseCommit(headId);
RevTree headTree = headCommit == null ? null : headCommit.getTree();
RevCommit from = rw.parseCommit(fromId);
this.checkout(headTree, from.getTree());
} finally {
rw.release();
}
}
private void checkout(RevTree headTree, RevTree fromTree)
throws GitAPIException, CheckoutConflictException, IOException {
// DirCacheCheckout free lock of DirCache
DirCacheCheckout dco = new DirCacheCheckout(this.getRepository(),
headTree, this.repo.lockDirCache(), fromTree);
dco.setFailOnConflict(true);
try {
dco.checkout();
if (!dco.getToBeDeleted().isEmpty())
status = new CheckoutResult(Status.NONDELETED,
dco.getToBeDeleted());
} catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
status = new CheckoutResult(Status.CONFLICTS, dco.getConflicts());
throw new CheckoutConflictException(dco.getConflicts(), e);
}
}
/**
* @return the result, never <code>null</code>
*/
public CheckoutResult getResult() {
if (status == null)
return CheckoutResult.NOT_TRIED_RESULT;
return status;
}
}
/*
* Copyright (C) 2013 SATO taichi <ryushi@gmail.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.api.errors.CheckoutConflictException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRefNameException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
import org.eclipse.jgit.api.errors.RefNotFoundException;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.Test;
/**
* Unit tests of {@link CreateOrphanBranchCommand}.
*/
public class CreateOrphanBranchCommandTest extends RepositoryTestCase {
Git git;
List<RevCommit> commits;
CreateOrphanBranchCommand target;
@Before
public void setUp() throws Exception {
super.setUp();
this.git = new Git(db);
this.commits = new ArrayList<RevCommit>();
this.commits.add(this.newFile("aaa"));
this.commits.add(this.newFile("bbb"));
this.commits.add(this.newFile("ccc"));
this.target = new CreateOrphanBranchCommand(db);
}
protected RevCommit newFile(String name) throws Exception {
return commitFile(name + ".txt", name, "master");
}
@Test
public void orphan() throws Exception {
Ref ref = this.target.setName("ppp").call();
assertNotNull(ref);
assertEquals("refs/heads/ppp", ref.getTarget().getName());
File HEAD = new File(trash, ".git/HEAD");
String headRef = read(HEAD);
assertEquals("ref: refs/heads/ppp\n", headRef);
assertEquals(4, trash.list().length);
File heads = new File(trash, ".git/refs/heads");
assertEquals(1, heads.listFiles().length);
this.noHead();
this.assertStatus(3);
assertEquals(CheckoutResult.NOT_TRIED_RESULT, this.target.getResult());
}
protected void noHead() throws GitAPIException {
try {
this.git.log().call();
fail();
} catch (NoHeadException e) {
// except to hit here
}
}
protected void assertStatus(int files) throws GitAPIException {
Status status = this.git.status().call();
assertFalse(status.isClean());
assertEquals(files, status.getAdded().size());
}
@Test
public void startCommit() throws Exception {
this.target.setStartPoint(this.commits.get(1)).setName("qqq").call();
assertEquals(3, trash.list().length);
this.noHead();
this.assertStatus(2);
}
@Test
public void startPoint() throws Exception {
this.target.setStartPoint("HEAD^^").setName("zzz").call();
assertEquals(2, trash.list().length);
this.noHead();
this.assertStatus(1);
}
@Test
public void linkFail() throws Exception {
File HEAD = new File(trash, ".git/HEAD");
FileInputStream in = new FileInputStream(HEAD);
try {
this.target.setName("aaa").call();
fail("Should have failed");
} catch (JGitInternalException e) {
// except to hit here
} finally {
in.close();
}
}
@Test
public void invalidRefName() throws Exception {
try {
this.target.setName("../hoge").call();
fail("Should have failed");
} catch (InvalidRefNameException e) {
// except to hit here
}
}
@Test
public void nullRefName() throws Exception {
try {
this.target.setName(null).call();
fail("Should have failed");
} catch (InvalidRefNameException e) {
// except to hit here
}
}
@Test
public void alreadyExists() throws Exception {
this.git.checkout().setCreateBranch(true).setName("ppp").call();
this.git.checkout().setName("master").call();
try {
this.target.setName("ppp").call();
fail("Should have failed");
} catch (RefAlreadyExistsException e) {
// except to hit here
}
}
@Test
public void refNotFound() throws Exception {
try {
this.target.setStartPoint("1234567").setName("ppp").call();
fail("Should have failed");
} catch (RefNotFoundException e) {
// except to hit here
}
}
@Test
public void toBeDeleted() throws Exception {
File ccc = new File(trash, "ccc.txt");
FileInputStream in = new FileInputStream(ccc);
try {
this.target.setName("zzz").setStartPoint(this.commits.get(0))
.call();
assertEquals(1, this.target.getResult().getUndeletedList().size());
} finally {
in.close();
}
}
@Test
public void conflicts() throws Exception {
this.git.checkout().setCreateBranch(true).setName("aaa").call();
File bbb = new File(trash, "bbb.txt");
write(bbb, "zzzz\nzzz");
try {
this.target.setName("zzz").setStartPoint(this.commits.get(0))
.call();
fail("Should have failed");
} catch (CheckoutConflictException e) {
assertEquals(1, this.target.getResult().getConflictList().size());
assertEquals(1, e.getConflictingPaths().size());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment