use of org.eclipse.jgit.lib.RefUpdate in project gitblit by gitblit.
the class JGitUtils method deleteBranchRef.
/**
* Deletes the specified branch ref.
*
* @param repository
* @param branch
* @return true if successful
*/
public static boolean deleteBranchRef(Repository repository, String branch) {
try {
RefUpdate refUpdate = repository.updateRef(branch, false);
refUpdate.setForceUpdate(true);
RefUpdate.Result result = refUpdate.delete();
switch(result) {
case NEW:
case FORCED:
case NO_CHANGE:
case FAST_FORWARD:
return true;
default:
LOGGER.error(MessageFormat.format("{0} failed to delete to {1} returned result {2}", repository.getDirectory().getAbsolutePath(), branch, result));
}
} catch (Throwable t) {
error(t, repository, "{0} failed to delete {1}", branch);
}
return false;
}
use of org.eclipse.jgit.lib.RefUpdate in project gitblit by gitblit.
the class JGitUtils method createOrphanBranch.
/**
* Create an orphaned branch in a repository.
*
* @param repository
* @param branchName
* @param author
* if unspecified, Gitblit will be the author of this new branch
* @return true if successful
*/
public static boolean createOrphanBranch(Repository repository, String branchName, PersonIdent author) {
boolean success = false;
String message = "Created branch " + branchName;
if (author == null) {
author = new PersonIdent("Gitblit", "gitblit@localhost");
}
try {
ObjectInserter odi = repository.newObjectInserter();
try {
// Create a blob object to insert into a tree
ObjectId blobId = odi.insert(Constants.OBJ_BLOB, message.getBytes(Constants.CHARACTER_ENCODING));
// Create a tree object to reference from a commit
TreeFormatter tree = new TreeFormatter();
tree.append(".branch", FileMode.REGULAR_FILE, blobId);
ObjectId treeId = odi.insert(tree);
// Create a commit object
CommitBuilder commit = new CommitBuilder();
commit.setAuthor(author);
commit.setCommitter(author);
commit.setEncoding(Constants.CHARACTER_ENCODING);
commit.setMessage(message);
commit.setTreeId(treeId);
// Insert the commit into the repository
ObjectId commitId = odi.insert(commit);
odi.flush();
RevWalk revWalk = new RevWalk(repository);
try {
RevCommit revCommit = revWalk.parseCommit(commitId);
if (!branchName.startsWith("refs/")) {
branchName = "refs/heads/" + branchName;
}
RefUpdate ru = repository.updateRef(branchName);
ru.setNewObjectId(commitId);
ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
Result rc = ru.forceUpdate();
switch(rc) {
case NEW:
case FORCED:
case FAST_FORWARD:
success = true;
break;
default:
success = false;
}
} finally {
revWalk.close();
}
} finally {
odi.close();
}
} catch (Throwable t) {
error(t, repository, "Failed to create orphan branch {1} in repository {0}", branchName);
}
return success;
}
use of org.eclipse.jgit.lib.RefUpdate in project gitblit by gitblit.
the class JGitUtils method setBranchRef.
/**
* Sets the local branch ref to point to the specified commit id.
*
* @param repository
* @param branch
* @param commitId
* @return true if successful
*/
public static boolean setBranchRef(Repository repository, String branch, String commitId) {
String branchName = branch;
if (!branchName.startsWith(Constants.R_REFS)) {
branchName = Constants.R_HEADS + branch;
}
try {
RefUpdate refUpdate = repository.updateRef(branchName, false);
refUpdate.setNewObjectId(ObjectId.fromString(commitId));
RefUpdate.Result result = refUpdate.forceUpdate();
switch(result) {
case NEW:
case FORCED:
case NO_CHANGE:
case FAST_FORWARD:
return true;
default:
LOGGER.error(MessageFormat.format("{0} {1} update to {2} returned result {3}", repository.getDirectory().getAbsolutePath(), branchName, commitId, result));
}
} catch (Throwable t) {
error(t, repository, "{0} failed to set {1} to {2}", branchName, commitId);
}
return false;
}
use of org.eclipse.jgit.lib.RefUpdate in project gitblit by gitblit.
the class JGitUtils method setHEADtoRef.
/**
* Sets the symbolic ref HEAD to the specified target ref. The
* HEAD will be detached if the target ref is not a branch.
*
* @param repository
* @param targetRef
* @return true if successful
*/
public static boolean setHEADtoRef(Repository repository, String targetRef) {
try {
// detach HEAD if target ref is not a branch
boolean detach = !targetRef.startsWith(Constants.R_HEADS);
RefUpdate.Result result;
RefUpdate head = repository.updateRef(Constants.HEAD, detach);
if (detach) {
// Tag
RevCommit commit = getCommit(repository, targetRef);
head.setNewObjectId(commit.getId());
result = head.forceUpdate();
} else {
result = head.link(targetRef);
}
switch(result) {
case NEW:
case FORCED:
case NO_CHANGE:
case FAST_FORWARD:
return true;
default:
LOGGER.error(MessageFormat.format("{0} HEAD update to {1} returned result {2}", repository.getDirectory().getAbsolutePath(), targetRef, result));
}
} catch (Throwable t) {
error(t, repository, "{0} failed to set HEAD to {1}", targetRef);
}
return false;
}
use of org.eclipse.jgit.lib.RefUpdate in project gitblit by gitblit.
the class NewRepositoryPage method initialCommit.
/**
* Prepare the initial commit for the repository.
*
* @param repository
* @param addReadme
* @param gitignore
* @param addGitFlow
* @return true if an initial commit was created
*/
protected boolean initialCommit(RepositoryModel repository, boolean addReadme, String gitignore, boolean addGitFlow) {
boolean initialCommit = addReadme || !StringUtils.isEmpty(gitignore) || addGitFlow;
if (!initialCommit) {
return false;
}
// build an initial commit
boolean success = false;
Repository db = app().repositories().getRepository(repositoryModel.name);
ObjectInserter odi = db.newObjectInserter();
try {
UserModel user = GitBlitWebSession.get().getUser();
String email = Optional.fromNullable(user.emailAddress).or(user.username + "@" + "gitblit");
PersonIdent author = new PersonIdent(user.getDisplayName(), email);
DirCache newIndex = DirCache.newInCore();
DirCacheBuilder indexBuilder = newIndex.builder();
if (addReadme) {
// insert a README
String title = StringUtils.stripDotGit(StringUtils.getLastPathElement(repositoryModel.name));
String description = repositoryModel.description == null ? "" : repositoryModel.description;
String readme = String.format("## %s\n\n%s\n\n", title, description);
byte[] bytes = readme.getBytes(Constants.ENCODING);
DirCacheEntry entry = new DirCacheEntry("README.md");
entry.setLength(bytes.length);
entry.setLastModified(System.currentTimeMillis());
entry.setFileMode(FileMode.REGULAR_FILE);
entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));
indexBuilder.add(entry);
}
if (!StringUtils.isEmpty(gitignore)) {
// insert a .gitignore file
File dir = app().runtime().getFileOrFolder(Keys.git.gitignoreFolder, "${baseFolder}/gitignore");
File file = new File(dir, gitignore + ".gitignore");
if (file.exists() && file.length() > 0) {
byte[] bytes = FileUtils.readContent(file);
if (!ArrayUtils.isEmpty(bytes)) {
DirCacheEntry entry = new DirCacheEntry(".gitignore");
entry.setLength(bytes.length);
entry.setLastModified(System.currentTimeMillis());
entry.setFileMode(FileMode.REGULAR_FILE);
entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));
indexBuilder.add(entry);
}
}
}
if (addGitFlow) {
// insert a .gitflow file
Config config = new Config();
config.setString("gitflow", null, "masterBranch", Constants.MASTER);
config.setString("gitflow", null, "developBranch", Constants.DEVELOP);
config.setString("gitflow", null, "featureBranchPrefix", "feature/");
config.setString("gitflow", null, "releaseBranchPrefix", "release/");
config.setString("gitflow", null, "hotfixBranchPrefix", "hotfix/");
config.setString("gitflow", null, "supportBranchPrefix", "support/");
config.setString("gitflow", null, "versionTagPrefix", "");
byte[] bytes = config.toText().getBytes(Constants.ENCODING);
DirCacheEntry entry = new DirCacheEntry(".gitflow");
entry.setLength(bytes.length);
entry.setLastModified(System.currentTimeMillis());
entry.setFileMode(FileMode.REGULAR_FILE);
entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));
indexBuilder.add(entry);
}
indexBuilder.finish();
if (newIndex.getEntryCount() == 0) {
// nothing to commit
return false;
}
ObjectId treeId = newIndex.writeTree(odi);
// Create a commit object
CommitBuilder commit = new CommitBuilder();
commit.setAuthor(author);
commit.setCommitter(author);
commit.setEncoding(Constants.ENCODING);
commit.setMessage("Initial commit");
commit.setTreeId(treeId);
// Insert the commit into the repository
ObjectId commitId = odi.insert(commit);
odi.flush();
// set the branch refs
RevWalk revWalk = new RevWalk(db);
try {
// set the master branch
RevCommit revCommit = revWalk.parseCommit(commitId);
RefUpdate masterRef = db.updateRef(Constants.R_MASTER);
masterRef.setNewObjectId(commitId);
masterRef.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
Result masterRC = masterRef.update();
switch(masterRC) {
case NEW:
success = true;
break;
default:
success = false;
}
if (addGitFlow) {
// set the develop branch for git-flow
RefUpdate developRef = db.updateRef(Constants.R_DEVELOP);
developRef.setNewObjectId(commitId);
developRef.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
Result developRC = developRef.update();
switch(developRC) {
case NEW:
success = true;
break;
default:
success = false;
}
}
} finally {
revWalk.close();
}
} catch (UnsupportedEncodingException e) {
logger().error(null, e);
} catch (IOException e) {
logger().error(null, e);
} finally {
odi.close();
db.close();
}
return success;
}
Aggregations