/** * The constructor * * @param base the base configuration file * @param cfgLocation the location of the configuration file on the file system * @param fs the file system abstraction which will be necessary to perform certain file system * operations. */ public FileBasedConfig(Config base, File cfgLocation, FS fs) { super(base); configFile = cfgLocation; this.fs = fs; this.snapshot = FileSnapshot.DIRTY; this.hash = ObjectId.zeroId(); }
private RefUpdate getPendingRefUpdate(Branch.NameKey destBranch) throws IntegrationException { if (pendingRefUpdates.containsKey(destBranch)) { logDebug("Access cached open branch {}: {}", destBranch.get(), openBranches.get(destBranch)); return pendingRefUpdates.get(destBranch); } try { RefUpdate branchUpdate = repo.updateRef(destBranch.get()); CodeReviewCommit branchTip; if (branchUpdate.getOldObjectId() != null) { branchTip = rw.parseCommit(branchUpdate.getOldObjectId()); } else if (Objects.equals(repo.getFullBranch(), destBranch.get())) { branchTip = null; branchUpdate.setExpectedOldObjectId(ObjectId.zeroId()); } else { throw new IntegrationException( "The destination branch " + destBranch.get() + " does not exist anymore."); } logDebug("Opened branch {}: {}", destBranch.get(), branchTip); pendingRefUpdates.put(destBranch, branchUpdate); openBranches.put(destBranch, branchTip); return branchUpdate; } catch (IOException e) { throw new IntegrationException("Cannot open branch", e); } }
private void execute(final ReceiveCommand cmd) { try { final RefUpdate ru = db.updateRef(cmd.getRefName()); ru.setRefLogIdent(getRefLogIdent()); switch (cmd.getType()) { case DELETE: if (!ObjectId.zeroId().equals(cmd.getOldId())) { // We can only do a CAS style delete if the client // didn't bork its delete request by sending the // wrong zero id rather than the advertised one. // ru.setExpectedOldObjectId(cmd.getOldId()); } ru.setForceUpdate(true); status(cmd, ru.delete(walk)); break; case CREATE: case UPDATE: case UPDATE_NONFASTFORWARD: ru.setForceUpdate(isAllowNonFastForwards()); ru.setExpectedOldObjectId(cmd.getOldId()); ru.setNewObjectId(cmd.getNewId()); ru.setRefLogMessage("push", true); status(cmd, ru.update(walk)); break; } } catch (IOException err) { cmd.setResult( Result.REJECTED_OTHER_REASON, MessageFormat.format(JGitText.get().lockError, err.getMessage())); } }
/** @author taichi */ public class BlobModel extends BasicGitModel { ObjectId objectId = ObjectId.zeroId(); String content; public BlobModel() {} public BlobModel(BlobModel src) { super(src); this.objectId = src.objectId; this.content = src.content; } public ObjectId getObjectId() { return this.objectId; } public void setObjectId(ObjectId objectid) { this.objectId = objectid; } public String getContent() { return this.content; } public void setContent(String content) { this.content = content; } }
@NotNull private InputStream openAttributes(@NotNull ObjectReader reader, @Nullable ObjectId id) throws IOException { if (ObjectId.zeroId().equals(id)) { return new ByteArrayInputStream(new byte[0]); } return reader.open(id, Constants.OBJ_BLOB).openStream(); }
@Test public void testUsingHiddenDeltaBaseFails() throws Exception { byte[] delta = {0x1, 0x1, 0x1, 'c'}; TestRepository<Repository> s = new TestRepository<Repository>(src); RevCommit N = s.commit() .parent(B) .add("q", s.blob(BinaryDelta.apply(dst.open(b).getCachedBytes(), delta))) .create(); final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); packHeader(pack, 3); copy(pack, src.open(N)); copy(pack, src.open(s.parseBody(N).getTree())); pack.write((Constants.OBJ_REF_DELTA) << 4 | 4); b.copyRawTo(pack); deflate(pack, delta); digest(pack); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); final PacketLineOut inPckLine = new PacketLineOut(inBuf); inPckLine.writeString( ObjectId.zeroId().name() + ' ' + N.name() + ' ' + "refs/heads/s" + '\0' + BasePackPushConnection.CAPABILITY_REPORT_STATUS); inPckLine.end(); pack.writeTo(inBuf, PM); final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); final ReceivePack rp = new ReceivePack(dst); rp.setCheckReceivedObjects(true); rp.setCheckReferencedObjectsAreReachable(true); rp.setAdvertiseRefsHook(new HidePrivateHook()); try { receive(rp, inBuf, outBuf); fail("Expected UnpackException"); } catch (UnpackException failed) { Throwable err = failed.getCause(); assertTrue(err instanceof MissingObjectException); MissingObjectException moe = (MissingObjectException) err; assertEquals(b, moe.getObjectId()); } final PacketLineIn r = asPacketLineIn(outBuf); String master = r.readString(); int nul = master.indexOf('\0'); assertTrue("has capability list", nul > 0); assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul)); assertSame(PacketLineIn.END, r.readString()); assertEquals("unpack error Missing blob " + b.name(), r.readString()); assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString()); assertSame(PacketLineIn.END, r.readString()); }
@Test public void testUsingUnknownBlobFails() throws Exception { // Try to use the 'n' blob that is not on the server. // TestRepository<Repository> s = new TestRepository<Repository>(src); RevBlob n = s.blob("n"); RevCommit N = s.commit().parent(B).add("q", n).create(); // But don't include it in the pack. // final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); packHeader(pack, 2); copy(pack, src.open(N)); copy(pack, src.open(s.parseBody(N).getTree())); digest(pack); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); final PacketLineOut inPckLine = new PacketLineOut(inBuf); inPckLine.writeString( ObjectId.zeroId().name() + ' ' + N.name() + ' ' + "refs/heads/s" + '\0' + BasePackPushConnection.CAPABILITY_REPORT_STATUS); inPckLine.end(); pack.writeTo(inBuf, PM); final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); final ReceivePack rp = new ReceivePack(dst); rp.setCheckReceivedObjects(true); rp.setCheckReferencedObjectsAreReachable(true); rp.setAdvertiseRefsHook(new HidePrivateHook()); try { receive(rp, inBuf, outBuf); fail("Expected UnpackException"); } catch (UnpackException failed) { Throwable err = failed.getCause(); assertTrue(err instanceof MissingObjectException); MissingObjectException moe = (MissingObjectException) err; assertEquals(n, moe.getObjectId()); } final PacketLineIn r = asPacketLineIn(outBuf); String master = r.readString(); int nul = master.indexOf('\0'); assertTrue("has capability list", nul > 0); assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul)); assertSame(PacketLineIn.END, r.readString()); assertEquals("unpack error Missing blob " + n.name(), r.readString()); assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString()); assertSame(PacketLineIn.END, r.readString()); }
private void insertChangeId(ObjectId treeId) throws IOException { ObjectId firstParentId = null; if (!parents.isEmpty()) firstParentId = parents.get(0); ObjectId changeId = ChangeIdUtil.computeChangeId(treeId, firstParentId, author, committer, message); message = ChangeIdUtil.insertId(message, changeId); if (changeId != null) message = message.replaceAll( "\nChange-Id: I" + ObjectId.zeroId().getName() + "\n", "\nChange-Id: I" + changeId.getName() + "\n"); }
@Test public void testPush_UnpackError_TruncatedPack() throws Exception { StringBuilder sb = new StringBuilder(); sb.append(ObjectId.zeroId().name()); sb.append(' '); sb.append(a_blob.name()); sb.append(' '); sb.append("refs/objects/A"); sb.append('\0'); sb.append("report-status"); ByteArrayOutputStream reqbuf = new ByteArrayOutputStream(); PacketLineOut reqpck = new PacketLineOut(reqbuf); reqpck.writeString(sb.toString()); reqpck.end(); packHeader(reqbuf, 1); byte[] reqbin = reqbuf.toByteArray(); URL u = new URL(remoteURI.toString() + "/git-receive-pack"); HttpURLConnection c = (HttpURLConnection) u.openConnection(); try { c.setRequestMethod("POST"); c.setDoOutput(true); c.setRequestProperty("Content-Type", GitSmartHttpTools.RECEIVE_PACK_REQUEST_TYPE); c.setFixedLengthStreamingMode(reqbin.length); OutputStream out = c.getOutputStream(); try { out.write(reqbin); } finally { out.close(); } assertEquals(200, c.getResponseCode()); assertEquals(GitSmartHttpTools.RECEIVE_PACK_RESULT_TYPE, c.getContentType()); InputStream rawin = c.getInputStream(); try { PacketLineIn pckin = new PacketLineIn(rawin); assertEquals("unpack error " + JGitText.get().packfileIsTruncated, pckin.readString()); assertEquals("ng refs/objects/A n/a (unpacker error)", pckin.readString()); assertSame(PacketLineIn.END, pckin.readString()); } finally { rawin.close(); } } finally { c.disconnect(); } }
private static class PackedRefList extends RefList<Ref> { static final PackedRefList NO_PACKED_REFS = new PackedRefList(RefList.emptyList(), FileSnapshot.MISSING_FILE, ObjectId.zeroId()); final FileSnapshot snapshot; final ObjectId id; PackedRefList(RefList<Ref> src, FileSnapshot s, ObjectId i) { super(src); snapshot = s; id = i; } }
private String getAbbreviatedId(GitModelCommit commit) { RevCommit remoteCommit = commit.getBaseCommit(); ObjectReader reader = commit.getRepository().newObjectReader(); ObjectId commitId = remoteCommit.getId(); AbbreviatedObjectId shortId; try { shortId = reader.abbreviate(commitId, 6); } catch (IOException e) { shortId = AbbreviatedObjectId.fromObjectId(ObjectId.zeroId()); Activator.logError(e.getMessage(), e); } finally { reader.release(); } return shortId.name(); }
/** * Generate an advertisement of available refs and capabilities. * * @param adv the advertisement formatter. * @throws IOException the formatter failed to write an advertisement. */ public void sendAdvertisedRefs(final RefAdvertiser adv) throws IOException { if (advertiseError != null) { adv.writeOne("ERR " + advertiseError); return; } adv.init(db); adv.advertiseCapability(CAPABILITY_SIDE_BAND_64K); adv.advertiseCapability(CAPABILITY_DELETE_REFS); adv.advertiseCapability(CAPABILITY_REPORT_STATUS); if (allowOfsDelta) adv.advertiseCapability(CAPABILITY_OFS_DELTA); adv.send(getAdvertisedRefs()); for (ObjectId obj : advertisedHaves) adv.advertiseHave(obj); if (adv.isEmpty()) adv.advertiseId(ObjectId.zeroId(), "capabilities^{}"); adv.end(); }
OpenBranch(OpenRepo or, Branch.NameKey name) throws IntegrationException { this.name = name; try { update = or.repo.updateRef(name.get()); if (update.getOldObjectId() != null) { oldTip = or.rw.parseCommit(update.getOldObjectId()); } else if (Objects.equals(or.repo.getFullBranch(), name.get())) { oldTip = null; update.setExpectedOldObjectId(ObjectId.zeroId()); } else { throw new IntegrationException( "The destination branch " + name + " does not exist anymore."); } } catch (IOException e) { throw new IntegrationException("Cannot open branch " + name, e); } }
@Test public void testCreateBranchAtHiddenCommitFails() throws Exception { final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64); packHeader(pack, 0); digest(pack); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256); final PacketLineOut inPckLine = new PacketLineOut(inBuf); inPckLine.writeString( ObjectId.zeroId().name() + ' ' + P.name() + ' ' + "refs/heads/s" + '\0' + BasePackPushConnection.CAPABILITY_REPORT_STATUS); inPckLine.end(); pack.writeTo(inBuf, PM); final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); final ReceivePack rp = new ReceivePack(dst); rp.setCheckReceivedObjects(true); rp.setCheckReferencedObjectsAreReachable(true); rp.setAdvertiseRefsHook(new HidePrivateHook()); try { receive(rp, inBuf, outBuf); fail("Expected UnpackException"); } catch (UnpackException failed) { Throwable err = failed.getCause(); assertTrue(err instanceof MissingObjectException); MissingObjectException moe = (MissingObjectException) err; assertEquals(P, moe.getObjectId()); } final PacketLineIn r = asPacketLineIn(outBuf); String master = r.readString(); int nul = master.indexOf('\0'); assertTrue("has capability list", nul > 0); assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul)); assertSame(PacketLineIn.END, r.readString()); assertEquals("unpack error Missing commit " + P.name(), r.readString()); assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString()); assertSame(PacketLineIn.END, r.readString()); }
private void refreshChangeIdText() { if (createChangeId) { // ChangeIdUtil uses \n line endings String text = commitText.getText().replaceAll(Text.DELIMITER, "\n"); // $NON-NLS-1$ String changedText = ChangeIdUtil.insertId( text, originalChangeId != null ? originalChangeId : ObjectId.zeroId(), true); if (!text.equals(changedText)) { changedText = changedText.replaceAll("\n", Text.DELIMITER); // $NON-NLS-1$ commitText.setText(changedText); } } else { String text = commitText.getText(); int changeIdOffset = findOffsetOfChangeIdLine(text); if (changeIdOffset > 0) { String cleanedText; int endOfChangeId = findNextEOL(changeIdOffset, text); if (endOfChangeId == -1) cleanedText = text.substring(0, changeIdOffset); else cleanedText = text.substring(0, changeIdOffset) + text.substring(endOfChangeId); commitText.setText(cleanedText); } } }
/** * Executes the {@code commit} command with all the options and parameters collected by the setter * methods of this class. Each instance of this class should only be used for one invocation of * the command (means: one call to {@link #call()}) * * @return a {@link RevCommit} object representing the successful commit. * @throws NoHeadException when called on a git repo without a HEAD reference * @throws NoMessageException when called without specifying a commit message * @throws UnmergedPathsException when the current index contained unmerged paths (conflicts) * @throws ConcurrentRefUpdateException when HEAD or branch ref is updated concurrently by someone * else * @throws WrongRepositoryStateException when repository is not in the right state for committing * @throws AbortedByHookException if there are either pre-commit or commit-msg hooks present in * the repository and one of them rejects the commit. */ public RevCommit call() throws GitAPIException, NoHeadException, NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException, WrongRepositoryStateException, AbortedByHookException { checkCallable(); Collections.sort(only); try (RevWalk rw = new RevWalk(repo)) { RepositoryState state = repo.getRepositoryState(); if (!state.canCommit()) throw new WrongRepositoryStateException( MessageFormat.format(JGitText.get().cannotCommitOnARepoWithState, state.name())); if (!noVerify) { Hooks.preCommit(repo, hookOutRedirect).call(); } processOptions(state, rw); if (all && !repo.isBare() && repo.getWorkTree() != null) { try (Git git = new Git(repo)) { git.add() .addFilepattern(".") // $NON-NLS-1$ .setUpdate(true) .call(); } catch (NoFilepatternException e) { // should really not happen throw new JGitInternalException(e.getMessage(), e); } } Ref head = repo.getRef(Constants.HEAD); if (head == null) throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported); // determine the current HEAD and the commit it is referring to ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}"); // $NON-NLS-1$ if (headId == null && amend) throw new WrongRepositoryStateException(JGitText.get().commitAmendOnInitialNotPossible); if (headId != null) if (amend) { RevCommit previousCommit = rw.parseCommit(headId); for (RevCommit p : previousCommit.getParents()) parents.add(p.getId()); if (author == null) author = previousCommit.getAuthorIdent(); } else { parents.add(0, headId); } if (!noVerify) { message = Hooks.commitMsg(repo, hookOutRedirect).setCommitMessage(message).call(); } // lock the index DirCache index = repo.lockDirCache(); try (ObjectInserter odi = repo.newObjectInserter()) { if (!only.isEmpty()) index = createTemporaryIndex(headId, index, rw); // Write the index as tree to the object database. This may // fail for example when the index contains unmerged paths // (unresolved conflicts) ObjectId indexTreeId = index.writeTree(odi); if (insertChangeId) insertChangeId(indexTreeId); // Create a Commit object, populate it and write it CommitBuilder commit = new CommitBuilder(); commit.setCommitter(committer); commit.setAuthor(author); commit.setMessage(message); commit.setParentIds(parents); commit.setTreeId(indexTreeId); ObjectId commitId = odi.insert(commit); odi.flush(); RevCommit revCommit = rw.parseCommit(commitId); RefUpdate ru = repo.updateRef(Constants.HEAD); ru.setNewObjectId(commitId); if (reflogComment != null) { ru.setRefLogMessage(reflogComment, false); } else { String prefix = amend ? "commit (amend): " //$NON-NLS-1$ : parents.size() == 0 ? "commit (initial): " //$NON-NLS-1$ : "commit: "; //$NON-NLS-1$ ru.setRefLogMessage(prefix + revCommit.getShortMessage(), false); } if (headId != null) ru.setExpectedOldObjectId(headId); else ru.setExpectedOldObjectId(ObjectId.zeroId()); Result rc = ru.forceUpdate(); switch (rc) { case NEW: case FORCED: case FAST_FORWARD: { setCallable(false); if (state == RepositoryState.MERGING_RESOLVED || isMergeDuringRebase(state)) { // Commit was successful. Now delete the files // used for merge commits repo.writeMergeCommitMsg(null); repo.writeMergeHeads(null); } else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) { repo.writeMergeCommitMsg(null); repo.writeCherryPickHead(null); } else if (state == RepositoryState.REVERTING_RESOLVED) { repo.writeMergeCommitMsg(null); repo.writeRevertHead(null); } return revCommit; } 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 { index.unlock(); } } catch (UnmergedPathException e) { throw new UnmergedPathsException(e); } catch (IOException e) { throw new JGitInternalException( JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e); } }
/** * Obtain the ObjectId for the current entry. * * <p>Using this method to compare ObjectId values between trees of this walker is very * inefficient. Applications should try to use {@link #idEqual(int, int)} or {@link * #getObjectId(MutableObjectId, int)} whenever possible. * * <p>Every tree supplies an object id, even if the tree does not contain the current entry. In * the latter case {@link ObjectId#zeroId()} is returned. * * @param nth tree to obtain the object identifier from. * @return object identifier for the current tree entry. * @see #getObjectId(MutableObjectId, int) * @see #idEqual(int, int) */ public ObjectId getObjectId(final int nth) { final AbstractTreeIterator t = trees[nth]; return t.matches == currentHead ? t.getEntryObjectId() : ObjectId.zeroId(); }
private void validateCommands() { for (final ReceiveCommand cmd : commands) { final Ref ref = cmd.getRef(); if (cmd.getResult() != Result.NOT_ATTEMPTED) continue; if (cmd.getType() == ReceiveCommand.Type.DELETE && !isAllowDeletes()) { // Deletes are not supported on this repository. // cmd.setResult(Result.REJECTED_NODELETE); continue; } if (cmd.getType() == ReceiveCommand.Type.CREATE) { if (!isAllowCreates()) { cmd.setResult(Result.REJECTED_NOCREATE); continue; } if (ref != null && !isAllowNonFastForwards()) { // Creation over an existing ref is certainly not going // to be a fast-forward update. We can reject it early. // cmd.setResult(Result.REJECTED_NONFASTFORWARD); continue; } if (ref != null) { // A well behaved client shouldn't have sent us a // create command for a ref we advertised to it. // cmd.setResult( Result.REJECTED_OTHER_REASON, MessageFormat.format(JGitText.get().refAlreadyExists, ref)); continue; } } if (cmd.getType() == ReceiveCommand.Type.DELETE && ref != null && !ObjectId.zeroId().equals(cmd.getOldId()) && !ref.getObjectId().equals(cmd.getOldId())) { // Delete commands can be sent with the old id matching our // advertised value, *OR* with the old id being 0{40}. Any // other requested old id is invalid. // cmd.setResult(Result.REJECTED_OTHER_REASON, JGitText.get().invalidOldIdSent); continue; } if (cmd.getType() == ReceiveCommand.Type.UPDATE) { if (ref == null) { // The ref must have been advertised in order to be updated. // cmd.setResult(Result.REJECTED_OTHER_REASON, JGitText.get().noSuchRef); continue; } if (!ref.getObjectId().equals(cmd.getOldId())) { // A properly functioning client will send the same // object id we advertised. // cmd.setResult(Result.REJECTED_OTHER_REASON, JGitText.get().invalidOldIdSent); continue; } // Is this possibly a non-fast-forward style update? // RevObject oldObj, newObj; try { oldObj = walk.parseAny(cmd.getOldId()); } catch (IOException e) { cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd.getOldId().name()); continue; } try { newObj = walk.parseAny(cmd.getNewId()); } catch (IOException e) { cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd.getNewId().name()); continue; } if (oldObj instanceof RevCommit && newObj instanceof RevCommit) { try { if (!walk.isMergedInto((RevCommit) oldObj, (RevCommit) newObj)) { cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD); } } catch (MissingObjectException e) { cmd.setResult(Result.REJECTED_MISSING_OBJECT, e.getMessage()); } catch (IOException e) { cmd.setResult(Result.REJECTED_OTHER_REASON); } } else { cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD); } } if (!cmd.getRefName().startsWith(Constants.R_REFS) || !Repository.isValidRefName(cmd.getRefName())) { cmd.setResult(Result.REJECTED_OTHER_REASON, JGitText.get().funnyRefname); } } }
private static RawText getRawText(ObjectId id, ObjectReader reader) throws IOException { if (id.equals(ObjectId.zeroId())) return new RawText(new byte[] {}); return new RawText(reader.open(id, OBJ_BLOB).getCachedBytes()); }