List of usage examples for org.eclipse.jgit.lib RefUpdate forceUpdate
public Result forceUpdate() throws IOException
From source file:org.eclipse.egit.core.test.internal.mapping.T0002_HistoryTest.java
License:Open Source License
@Before public void setUp() throws Exception { super.setUp(); project.createSourceFolder();/*from w w w. ja va2s .c o m*/ gitDir = new File(project.getProject().getWorkspace().getRoot().getRawLocation().toFile(), Constants.DOT_GIT); thisGit = new Repository(gitDir); workDir = thisGit.getWorkDir(); thisGit.create(); objectWriter = new ObjectWriter(thisGit); tree = new Tree(thisGit); Tree projectTree = tree.addTree("Project-1"); File project1_a_txt = createFile("Project-1/A.txt", "A.txt - first version\n"); addFile(projectTree, project1_a_txt); projectTree.setId(objectWriter.writeTree(projectTree)); File project1_b_txt = createFile("Project-1/B.txt", "B.txt - first version\n"); addFile(projectTree, project1_b_txt); projectTree.setId(objectWriter.writeTree(projectTree)); tree.setId(objectWriter.writeTree(tree)); Commit commit = new Commit(thisGit); commit.setAuthor(new PersonIdent(jauthor, new Date(0L), TimeZone.getTimeZone("GMT+1"))); commit.setCommitter(new PersonIdent(jcommitter, new Date(0L), TimeZone.getTimeZone("GMT+1"))); commit.setMessage("Foo\n\nMessage"); commit.setTree(tree); ObjectId commitId = objectWriter.writeCommit(commit); tree = new Tree(thisGit); projectTree = tree.addTree("Project-1"); addFile(projectTree, project1_a_txt); File project1_b_v2_txt = createFile("Project-1/B.txt", "B.txt - second version\n"); addFile(projectTree, project1_b_v2_txt); projectTree.setId(objectWriter.writeTree(projectTree)); tree.setId(objectWriter.writeTree(tree)); commit = new Commit(thisGit); commit.setAuthor(new PersonIdent(jauthor, new Date(0L), TimeZone.getTimeZone("GMT+1"))); commit.setCommitter(new PersonIdent(jcommitter, new Date(0L), TimeZone.getTimeZone("GMT+1"))); commit.setMessage("Modified"); commit.setParentIds(new ObjectId[] { commitId }); commit.setTree(tree); commitId = objectWriter.writeCommit(commit); RefUpdate lck = thisGit.updateRef("refs/heads/master"); assertNotNull("obtained lock", lck); lck.setNewObjectId(commitId); assertEquals(RefUpdate.Result.NEW, lck.forceUpdate()); ConnectProviderOperation operation = new ConnectProviderOperation(project.getProject(), gitDir); operation.execute(null); }
From source file:org.eclipse.egit.core.test.op.T0001_ConnectProviderOperationTest.java
License:Open Source License
@Test public void testNewUnsharedFile() throws CoreException, IOException, InterruptedException { project.createSourceFolder();//www . j a v a 2s .com IFile fileA = project.getProject().getFolder("src").getFile("A.java"); String srcA = "class A {\n" + "}\n"; fileA.create(new ByteArrayInputStream(srcA.getBytes()), false, null); File gitDir = new File(project.getProject().getWorkspace().getRoot().getRawLocation().toFile(), Constants.DOT_GIT); Repository thisGit = new Repository(gitDir); thisGit.create(); Tree rootTree = new Tree(thisGit); Tree prjTree = rootTree.addTree(project.getProject().getName()); Tree srcTree = prjTree.addTree("src"); FileTreeEntry entryA = srcTree.addFile("A.java"); ObjectWriter writer = new ObjectWriter(thisGit); entryA.setId(writer.writeBlob(fileA.getRawLocation().toFile())); srcTree.setId(writer.writeTree(srcTree)); prjTree.setId(writer.writeTree(prjTree)); rootTree.setId(writer.writeTree(rootTree)); Commit commit = new Commit(thisGit); commit.setTree(rootTree); commit.setAuthor(new PersonIdent("J. Git", "j.git@egit.org", new Date(60876075600000L), TimeZone.getTimeZone("GMT+1"))); commit.setCommitter(commit.getAuthor()); commit.setMessage("testNewUnsharedFile\n\nJunit tests\n"); ObjectId id = writer.writeCommit(commit); RefUpdate lck = thisGit.updateRef("refs/heads/master"); assertNotNull("obtained lock", lck); lck.setNewObjectId(id); assertEquals(RefUpdate.Result.NEW, lck.forceUpdate()); ConnectProviderOperation operation = new ConnectProviderOperation(project.getProject(), gitDir); operation.execute(null); final boolean f[] = new boolean[1]; new Job("wait") { protected IStatus run(IProgressMonitor monitor) { System.out.println("MyJob"); f[0] = true; return null; } { setRule(project.getProject()); schedule(); } }; while (!f[0]) { System.out.println("Waiting"); Thread.sleep(1000); } System.out.println("DONE"); assertNotNull(RepositoryProvider.getProvider(project.getProject())); }
From source file:org.gitective.tests.RepositoryUtilsTest.java
License:Open Source License
/** * Test one origin change// www . j a v a 2 s. c om * * * @throws Exception */ @Test public void oneOriginChange() throws Exception { RevCommit commit1 = add("test.txt", "content"); File repo2 = initRepo(); RevCommit commit2 = add(repo2, "test2.txt", "content2.txt"); Repository repo = new FileRepository(testRepo); RefUpdate originMaster = repo .updateRef(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER); originMaster.setNewObjectId(commit1); originMaster.forceUpdate(); RemoteConfig config = new RemoteConfig(repo.getConfig(), Constants.DEFAULT_REMOTE_NAME); config.addURI(new URIish(repo2.toURI().toString())); config.update(repo.getConfig()); Collection<RefDiff> diffs = RepositoryUtils.diffOriginRefs(repo); assertNotNull(diffs); assertFalse(diffs.isEmpty()); assertNotNull(diffs.iterator().next().getLocal()); assertNotNull(diffs.iterator().next().getRemote()); assertEquals(commit1, diffs.iterator().next().getLocal().getObjectId()); assertEquals(commit2, diffs.iterator().next().getRemote().getObjectId()); }
From source file:org.jboss.forge.git.Clone.java
License:Eclipse Distribution License
private void doCheckout(final Ref branch) throws IOException { if (branch == null) throw new Die(CLIText.get().cannotChekoutNoHeadsAdvertisedByRemote); if (!Constants.HEAD.equals(branch.getName())) { RefUpdate u = db.updateRef(Constants.HEAD); u.disableRefLog();/* ww w . ja v a 2 s . c o m*/ u.link(branch.getName()); } final RevCommit commit = parseCommit(branch); final RefUpdate u = db.updateRef(Constants.HEAD); u.setNewObjectId(commit); u.forceUpdate(); DirCache dc = db.lockDirCache(); DirCacheCheckout co = new DirCacheCheckout(db, dc, commit.getTree()); co.checkout(); }
From source file:org.jenkinsci.git.RepositoryCheckoutOperation.java
License:Open Source License
public Boolean invoke(File file, VirtualChannel channel) throws IOException, InterruptedException { CommitLogWriter writer = new CommitLogWriter(new OutputStreamWriter(log.write())); CommitLogWriterFilter filter = new CommitLogWriterFilter(writer); StreamProgressMonitor monitor = null; if (listener != null) monitor = new StreamProgressMonitor(listener.getLogger()); try {/*from www. ja v a2 s.co m*/ for (BuildRepository repo : repos) { Repository gitRepo = new FileRepositoryOperation(repo).invoke(file, channel); RevCommit current = null; if (gitRepo == null) gitRepo = new InitOperation(repo).invoke(file, channel); else current = CommitUtils.getLatest(gitRepo); RevCommit fetched = new FetchOperation(repo, gitRepo, monitor).call(); if (fetched == null) return false; if (current != null) new CommitFinder(gitRepo).setFilter(filter).findBetween(fetched, current); else writer.write(new Commit(gitRepo, fetched)); new TreeCheckoutOperation(gitRepo, fetched).call(); RefUpdate refUpdate = gitRepo.updateRef(Constants.HEAD, true); refUpdate.setNewObjectId(fetched); Result result = refUpdate.forceUpdate(); if (result == null) throw new IOException("Null ref update result"); switch (result) { case NEW: case FORCED: case FAST_FORWARD: case NO_CHANGE: // These are the acceptable results break; default: throw new IOException(result.name()); } } } finally { writer.close(); } return true; }
From source file:org.jfrog.bamboo.release.scm.git.ResetCommand.java
License:Eclipse Distribution License
/** * Executes the {@code Reset} command. Each instance of this class should only be used for one invocation of the * command. Don't call this method twice on an instance. * * @return the Ref after reset/* w w w .j a va 2 s . c o m*/ */ @Override public Ref call() throws GitAPIException { checkCallable(); Ref r; RevCommit commit; try { boolean merging = false; if (repo.getRepositoryState().equals(RepositoryState.MERGING) || repo.getRepositoryState().equals(RepositoryState.MERGING_RESOLVED)) { merging = true; } // resolve the ref to a commit final ObjectId commitId; try { commitId = repo.resolve(ref); } catch (IOException e) { throw new JGitInternalException(MessageFormat.format(JGitText.get().cannotRead, ref), e); } RevWalk rw = new RevWalk(repo); try { commit = rw.parseCommit(commitId); } catch (IOException e) { throw new JGitInternalException( MessageFormat.format(JGitText.get().cannotReadCommit, commitId.toString()), e); } finally { rw.release(); } // write the ref final RefUpdate ru = repo.updateRef(Constants.HEAD); ru.setNewObjectId(commitId); String refName = Repository.shortenRefName(ref); String message = "reset --" //$NON-NLS-1$ + mode.toString().toLowerCase() + " " + refName; //$NON-NLS-1$ ru.setRefLogMessage(message, false); if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) { throw new JGitInternalException(MessageFormat.format(JGitText.get().cannotLock, ru.getName())); } switch (mode) { case HARD: checkoutIndex(commit); break; case MIXED: resetIndex(commit); break; case SOFT: // do nothing, only the ref was changed break; } if (mode != ResetType.SOFT && merging) { resetMerge(); } setCallable(false); r = ru.getRef(); } catch (IOException e) { throw new JGitInternalException("Error while executing reset command"); } return r; }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static void commit(final Git git, final String branchName, final String name, final String email, final String message, final TimeZone timeZone, final Date when, final Map<String, File> content) { final PersonIdent author = buildPersonIdent(git, name, email, timeZone, when); try {/*from ww w . j av a2 s.c o m*/ final ObjectInserter odi = git.getRepository().newObjectInserter(); try { // Create the in-memory index of the new/updated issue. final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}"); final DirCache index = createTemporaryIndex(git, headId, content); final ObjectId indexTreeId = index.writeTree(odi); // Create a commit object final CommitBuilder commit = new CommitBuilder(); commit.setAuthor(author); commit.setCommitter(author); commit.setEncoding(Constants.CHARACTER_ENCODING); commit.setMessage(message); //headId can be null if the repository has no commit yet if (headId != null) { commit.setParentId(headId); } commit.setTreeId(indexTreeId); // Insert the commit into the repository final ObjectId commitId = odi.insert(commit); odi.flush(); final RevWalk revWalk = new RevWalk(git.getRepository()); try { final RevCommit revCommit = revWalk.parseCommit(commitId); final RefUpdate ru = git.getRepository().updateRef("refs/heads/" + branchName); if (headId == null) { ru.setExpectedOldObjectId(ObjectId.zeroId()); } else { ru.setExpectedOldObjectId(headId); } ru.setNewObjectId(commitId); ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false); final RefUpdate.Result rc = ru.forceUpdate(); switch (rc) { case NEW: case FORCED: case FAST_FORWARD: break; case REJECTED: case LOCK_FAILURE: throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc); default: throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, Constants.HEAD, commitId.toString(), rc)); } } finally { revWalk.release(); } } finally { odi.release(); } } catch (final Throwable t) { throw new RuntimeException(t); } }
From source file:org.kuali.student.git.model.ref.utils.GitRefUtils.java
License:Educational Community License
public static Ref createBranch(Repository repo, String absoluteBranchName, ObjectId commitId, boolean allowUpdate) throws BranchRefExistsException, IOException { if (!allowUpdate && repo.getRef(absoluteBranchName) != null) throw new BranchRefExistsException(absoluteBranchName + "already exists"); RefUpdate update = repo.updateRef(absoluteBranchName); update.setNewObjectId(commitId);//from w ww . j a v a 2 s .c om update.setRefLogMessage("created new branch " + absoluteBranchName, true); Result result = update.forceUpdate(); if (result.equals(Result.LOCK_FAILURE)) { log.warn("lockfailure updating " + absoluteBranchName + " to commitId = " + commitId); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { //fall through } return createBranch(repo, absoluteBranchName, commitId, allowUpdate); } if (result == null || !(result.equals(Result.NEW) || result.equals(Result.FORCED) || result.equals(Result.FAST_FORWARD))) { throw new RuntimeException("failed to create new branch: " + absoluteBranchName); } Ref ref = repo.getRef(absoluteBranchName); return ref; }
From source file:org.kuali.student.git.model.ref.utils.GitRefUtils.java
License:Educational Community License
public static Result createTagReference(Repository repo, String simpleTagName, ObjectId tagId) throws IOException { String refName = Constants.R_TAGS + simpleTagName; RefUpdate tagRef = repo.updateRef(refName); tagRef.setNewObjectId(tagId);/* ww w .j a va 2 s. c o m*/ tagRef.setForceUpdate(true); tagRef.setRefLogMessage("tagged " + simpleTagName, false); Result updateResult = tagRef.forceUpdate(); return updateResult; }
From source file:org.moxie.utils.JGitUtils.java
License:Apache License
/** * Create an orphaned branch in a repository. * // w ww . jav a2 s . c o m * @param repository * @param branchName * @param author * if unspecified, Moxie 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("Moxie", "moxie@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("NEWBRANCH", 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.release(); } } finally { odi.release(); } } catch (Throwable t) { t.printStackTrace(); } return success; }