/** * Returns <code>String</code> representation of <code>value</code>. * * <p>If <code>value</code> is a {@link SVNPropertyValue#isBinary() binary} property value, then * its bytes are converted to a <code>String</code> encoding them with the <span * class="javastring">"UTF-8"</span> charset and returned back to the caller. If that encoding * fails, bytes are encoded with the default platform's charset. * * <p>Otherwise, {@link SVNPropertyValue#getString()} is returned. * * @param value property value object * @return string property value; <span class="javakeyword">null</span> if <code>value</code> is * <span class="javakeyword">null</span> */ public static char[] getPropertyAsChars(SVNPropertyValue value) { if (value == null) { return null; } if (value.isBinary()) { return SVNEncodingUtil.getChars(value.getBytes(), "UTF-8"); } return value.getString().toCharArray(); }
public static boolean areEqual(SVNPropertyValue propertyValue1, SVNPropertyValue propertyValue2) { if (propertyValue1 == null) { return propertyValue2 == null; } if (propertyValue2 == null) { return false; } byte[] propertyValueBytes1 = SVNPropertyValue.getPropertyAsBytes(propertyValue1); byte[] propertyValueBytes2 = SVNPropertyValue.getPropertyAsBytes(propertyValue2); return Arrays.equals(propertyValueBytes1, propertyValueBytes2); }
/** * Returns <code>String</code> representation of <code>value</code>. * * <p>If <code>value</code> is a {@link SVNPropertyValue#isBinary() binary} property value, then * its bytes are converted to a <code>String</code> encoding them with the <span * class="javastring">"UTF-8"</span> charset and returned back to the caller. If that encoding * fails, bytes are encoded with the default platform's charset. * * <p>Otherwise, {@link SVNPropertyValue#getString()} is returned. * * @param value property value object * @return string property value; <span class="javakeyword">null</span> if <code>value</code> is * <span class="javakeyword">null</span> */ public static String getPropertyAsString(SVNPropertyValue value) { if (value == null) { return null; } if (value.isBinary()) { try { return new String(value.getBytes(), "UTF-8"); } catch (UnsupportedEncodingException e) { return new String(value.getBytes()); } } return value.getString(); }
/** * Returns <code>byte[]</code> representation of <code>value</code>. * * <p>If <code>value</code> is a {@link SVNPropertyValue#isString() string} property value, then * bytes of the string are encoded using the <span class="javastring">"UTF-8"</span> charset and * returned by this method. If encoding fails, then bytes are encoded using the default platform's * charset. * * <p>Otherwise, {@link SVNPropertyValue#getBytes()} is returned. * * @param value property value object * @return bytes of the property value represented by <code>value</code>; <span * class="javakeyword">null</span> if <code>value</code> is <span * class="javakeyword">null</span> */ public static byte[] getPropertyAsBytes(SVNPropertyValue value) { if (value == null) { return null; } if (value.isString()) { try { return value.getString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { return value.getString().getBytes(); } } return value.getBytes(); }
/** * Says whether this object and <code>obj</code> are equal or not. * * @param obj object to compare with * @return <span class="javakeyword">true</span> in the following cases: * <ul> * <li/><code>obj</code> is the same as this one (by reference) * <li/>if <code>obj</code> is an <code>SVNPropertyValue</code> and either has got the same * <code>String</code> value in case this object holds a <code>String</code> value, or * the same byte array contents if this object represents a binary property value * </ul> */ public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (obj instanceof SVNPropertyValue) { SVNPropertyValue value = (SVNPropertyValue) obj; if (isString()) { return myValue.equals(value.getString()); } else if (isBinary()) { return Arrays.equals(myData, getPropertyAsBytes(value)); } } return false; }
private static void addPropertyPresentation( final SVNPropertyData property, final StringBuilder sb) { if (sb.length() != 0) { sb.append(ourPropertiesDelimiter); } sb.append(property.getName()) .append("=") .append( (property.getValue() == null) ? "" : SVNPropertyValue.getPropertyAsString(property.getValue())); }
public void create(ISVNEditor commitEditor, final String path, final InputStream content) throws SVNException, IOException { final BufferedInputStream bis = new BufferedInputStream(content); final String autoDetectedMimeType = detectMimeType(bis); commitEditor.addFile(path, null, -1); commitEditor.applyTextDelta(path, null); SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator(); String checksum = deltaGenerator.sendDelta(path, bis, commitEditor, true); final Map<String, String> autoprops = _autoPropertiesApplier.apply(path); for (Map.Entry<String, String> entry : autoprops.entrySet()) { commitEditor.changeFileProperty( path, entry.getKey(), SVNPropertyValue.create(entry.getValue())); } if (!autoprops.containsKey(SVNProperty.MIME_TYPE) && autoDetectedMimeType != null) { commitEditor.changeFileProperty( path, SVNProperty.MIME_TYPE, SVNPropertyValue.create(autoDetectedMimeType)); } commitEditor.closeFile(path, checksum); }
public static void appendProperty(String name, SVNPropertyValue value, OutputStream target) throws SVNException { if (name == null || value == null) { return; } byte[] bytes = SVNPropertyValue.getPropertyAsBytes(value); try { writeProperty(target, 'K', name.getBytes("UTF-8")); writeProperty(target, 'V', bytes); } catch (IOException ioe) { SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, ioe.getLocalizedMessage()); SVNErrorManager.error(err, ioe, SVNLogType.WC); } }
private void listExternals( SVNRepository repository, Map<SVNURL, SVNPropertyValue> externals, SVNDepth depth, int entryFields, boolean fetchLocks, ISVNDirEntryHandler handler) throws SVNException { for (Map.Entry<SVNURL, SVNPropertyValue> entry : externals.entrySet()) { SVNURL externalParentUrl = entry.getKey(); SVNPropertyValue externalValue = entry.getValue(); SVNExternal[] externalItems = SVNExternal.parseExternals( externalParentUrl, SVNPropertyValue.getPropertyAsString(externalValue)); if (externalItems == null || externalItems.length == 0) { continue; } listExternalItems( repository, externalItems, externalParentUrl, depth, entryFields, fetchLocks, handler); } }
public static void setProperties( SVNProperties namesToValues, OutputStream target, String terminator) throws SVNException { try { Object[] keys = namesToValues.nameSet().toArray(); Arrays.sort(keys); for (int i = 0; i < keys.length; i++) { String propertyName = (String) keys[i]; writeProperty(target, 'K', propertyName.getBytes("UTF-8")); writeProperty( target, 'V', SVNPropertyValue.getPropertyAsBytes(namesToValues.getSVNPropertyValue(propertyName))); } if (terminator != null) { target.write(terminator.getBytes("UTF-8")); target.write('\n'); } } catch (IOException ioe) { SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, ioe.getLocalizedMessage()); SVNErrorManager.error(err, ioe, SVNLogType.WC); } }
protected void endElement(DAVElement parent, DAVElement element, StringBuffer cdata) throws SVNException { if (element == APPLY_TEXT_DELTA) { setDeltaProcessing(false); } else if (element == CHANGE_FILE_PROPERTY || element == CHANGE_DIR_PROPERTY) { if (cdata != null && !"".equals(cdata.toString()) && myPropertyName == null) { SVNErrorMessage err = SVNErrorMessage.create( SVNErrorCode.RA_DAV_MALFORMED_DATA, "Got cdata content for a prop delete"); SVNErrorManager.error(err, SVNLogType.NETWORK); } if (myPropertyName != null) { StringBuffer sb = SVNBase64.normalizeBase64(cdata); byte[] buffer = allocateBuffer(sb.length()); int length = SVNBase64.base64ToByteArray(sb, buffer); SVNPropertyValue property = SVNPropertyValue.create(myPropertyName, buffer, 0, length); if (element == CHANGE_FILE_PROPERTY) { myEditor.changeFileProperty(myPath, myPropertyName, property); } else { myEditor.changeDirProperty(myPropertyName, property); } } } }
private long commit() throws SVNException { long oldRev = myFSFS.getYoungestRevision(); if (myTxn.getBaseRevision() != oldRev) { SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.FS_TXN_OUT_OF_DATE, "Transaction out of date"); SVNErrorManager.error(err, SVNLogType.FSFS); } verifyLocks(); final String startNodeId; final String startCopyId; if (myFSFS.getDBFormat() < FSFS.MIN_NO_GLOBAL_IDS_FORMAT) { String[] ids = myFSFS.getNextRevisionIDs(); startNodeId = ids[0]; startCopyId = ids[1]; } else { startNodeId = null; startCopyId = null; } final long newRevision = oldRev + 1; final OutputStream protoFileOS = null; final FSID newRootId = null; final FSTransactionRoot txnRoot = getTxnRoot(); FSWriteLock txnWriteLock = FSWriteLock.getWriteLockForTxn(myTxn.getTxnId(), myFSFS); synchronized (txnWriteLock) { try { // start transaction. txnWriteLock.lock(); final File revisionPrototypeFile = txnRoot.getTransactionProtoRevFile(); final long offset = revisionPrototypeFile.length(); if (myFSFS.getRepositoryCacheManager() != null) { myFSFS .getRepositoryCacheManager() .runWriteTransaction( new IFSSqlJetTransaction() { public void run() throws SVNException { commit( startNodeId, startCopyId, newRevision, protoFileOS, newRootId, txnRoot, revisionPrototypeFile, offset); } }); } else { commit( startNodeId, startCopyId, newRevision, protoFileOS, newRootId, txnRoot, revisionPrototypeFile, offset); } File dstRevFile = myFSFS.getNewRevisionFile(newRevision); SVNFileUtil.rename(revisionPrototypeFile, dstRevFile); } finally { txnWriteLock.unlock(); FSWriteLock.release(txnWriteLock); } } String commitTime = SVNDate.formatDate(new Date(System.currentTimeMillis())); SVNProperties presetRevisionProperties = myFSFS.getTransactionProperties(myTxn.getTxnId()); if (presetRevisionProperties == null || !presetRevisionProperties.containsName(SVNRevisionProperty.DATE)) { myFSFS.setTransactionProperty( myTxn.getTxnId(), SVNRevisionProperty.DATE, SVNPropertyValue.create(commitTime)); } File txnPropsFile = myFSFS.getTransactionPropertiesFile(myTxn.getTxnId()); if (myFSFS.getDBFormat() < FSFS.MIN_PACKED_REVPROP_FORMAT || newRevision >= myFSFS.getMinUnpackedRevProp()) { File dstRevPropsFile = myFSFS.getNewRevisionPropertiesFile(newRevision); SVNFileUtil.rename(txnPropsFile, dstRevPropsFile); } else { /* Read the revprops, and commit them to the permenant sqlite db. */ FSFile fsfProps = new FSFile(txnPropsFile); try { final SVNProperties revProperties = fsfProps.readProperties(false, true); final SVNSqlJetStatement stmt = myFSFS.getRevisionProperitesDb().getStatement(SVNWCDbStatements.FSFS_SET_REVPROP); try { stmt.insert( new Object[] {newRevision, SVNSkel.createPropList(revProperties.asMap()).getData()}); } finally { stmt.reset(); } } finally { fsfProps.close(); } } try { txnRoot.writeFinalCurrentFile(newRevision, startNodeId, startCopyId); } catch (IOException ioe) { SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, ioe.getLocalizedMessage()); SVNErrorManager.error(err, ioe, SVNLogType.FSFS); } myFSFS.setYoungestRevisionCache(newRevision); myFSFS.purgeTxn(myTxn.getTxnId()); return newRevision; }
public void setPropertyValue(String name, SVNPropertyValue value) throws SVNException { byte[] bytes = SVNPropertyValue.getPropertyAsBytes(value); int length = bytes != null && bytes.length >= 0 ? bytes.length : -1; setPropertyValue(name, bytes != null ? new ByteArrayInputStream(bytes) : null, length); }
/** * Pass the absolute path of the base directory where all example data will be created in arg[0]. * The sample will create: * * <p>- arg[0]/exampleRepository - repository with some test data - arg[0]/exampleWC - working * copy checked out from exampleRepository */ public static void main(String[] args) { // initialize SVNKit to work through file:/// protocol SamplesUtility.initializeFSFSprotocol(); File baseDirectory = new File(args[0]); File reposRoot = new File(baseDirectory, "exampleRepository"); File wcRoot = new File(baseDirectory, "exampleWC"); try { // first create a repository and fill it with data SamplesUtility.createRepository(reposRoot); SVNCommitInfo info = SamplesUtility.createRepositoryTree(reposRoot); // print out new revision info System.out.println(info); SVNClientManager clientManager = SVNClientManager.newInstance(); clientManager.setEventHandler(new EventHandler()); SVNURL reposURL = SVNURL.fromFile(reposRoot); // copy A to A_copy in repository (url-to-url copy) SVNCopyClient copyClient = clientManager.getCopyClient(); SVNURL A_URL = reposURL.appendPath("A", true); SVNURL copyTargetURL = reposURL.appendPath("A_copy", true); SVNCopySource copySource = new SVNCopySource(SVNRevision.UNDEFINED, SVNRevision.HEAD, A_URL); info = copyClient.doCopy( new SVNCopySource[] {copySource}, copyTargetURL, false, false, true, "copy A to A_copy", null); // print out new revision info System.out.println(info); // checkout the entire repository tree SamplesUtility.checkOutWorkingCopy(reposURL, wcRoot); // now make some changes to the A tree SamplesUtility.writeToFile(new File(wcRoot, "iota"), "New text appended to 'iota'", true); SamplesUtility.writeToFile(new File(wcRoot, "A/mu"), "New text in 'mu'", false); SVNWCClient wcClient = SVNClientManager.newInstance().getWCClient(); wcClient.doSetProperty( new File(wcRoot, "A/B"), "spam", SVNPropertyValue.create("egg"), false, SVNDepth.EMPTY, null, null); // commit local changes SVNCommitClient commitClient = clientManager.getCommitClient(); commitClient.doCommit( new File[] {wcRoot}, false, "committing changes", null, null, false, false, SVNDepth.INFINITY); // now diff the base revision of the working copy against the repository SVNDiffClient diffClient = clientManager.getDiffClient(); SVNRevisionRange rangeToMerge = new SVNRevisionRange(SVNRevision.create(1), SVNRevision.HEAD); diffClient.doMerge( A_URL, SVNRevision.HEAD, Collections.singleton(rangeToMerge), new File(wcRoot, "A_copy"), SVNDepth.UNKNOWN, true, false, false, false); // now make some changes to the A tree again // change file contents of iota and A/D/gamma SamplesUtility.writeToFile(new File(wcRoot, "iota"), "New text2 appended to 'iota'", true); SamplesUtility.writeToFile(new File(wcRoot, "A/D/gamma"), "New text in 'gamma'", false); // remove A/C from version control wcClient.doDelete(new File(wcRoot, "A/C"), false, true, false); // commit local changes commitClient.doCommit( new File[] {wcRoot}, false, "committing changes again", null, null, false, false, SVNDepth.INFINITY); /* do the same merge call, merge-tracking feature will merge only those revisions * which were not still merged. */ diffClient.doMerge( A_URL, SVNRevision.HEAD, Collections.singleton(rangeToMerge), new File(wcRoot, "A_copy"), SVNDepth.UNKNOWN, true, false, false, false); } catch (SVNException svne) { System.out.println(svne.getErrorMessage()); System.exit(1); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(1); } }