public void setProjectDescription(final Project.NameKey name, final String description) { // Update git's description file, in case gitweb is being used // try { final Repository e; final LockFile f; e = openRepository(name); try { f = new LockFile(new File(e.getDirectory(), "description"), FS.DETECTED); if (f.lock()) { String d = description; if (d != null) { d = d.trim(); if (d.length() > 0) { d += "\n"; } } else { d = ""; } f.write(Constants.encode(d)); f.commit(); } } finally { e.close(); } } catch (RepositoryNotFoundException e) { log.error("Cannot update description for " + name, e); } catch (IOException e) { log.error("Cannot update description for " + name, e); } }
/** * Save the configuration as a Git text style configuration file. * * <p><b>Warning:</b> Although this method uses the traditional Git file locking approach to * protect against concurrent writes of the configuration file, it does not ensure that the file * has not been modified since the last read, which means updates performed by other objects * accessing the same backing file may be lost. * * @throws IOException the file could not be written. */ public void save() throws IOException { final byte[] out; final String text = toText(); if (utf8Bom) { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(0xEF); bos.write(0xBB); bos.write(0xBF); bos.write(text.getBytes(RawParseUtils.UTF8_CHARSET.name())); out = bos.toByteArray(); } else { out = Constants.encode(text); } final LockFile lf = new LockFile(getFile(), fs); if (!lf.lock()) throw new LockFailedException(getFile()); try { lf.setNeedSnapshot(true); lf.write(out); if (!lf.commit()) throw new IOException(MessageFormat.format(JGitText.get().cannotCommitWriteTo, getFile())); } finally { lf.unlock(); } snapshot = lf.getCommitSnapshot(); hash = hash(out); // notify the listeners fireConfigChangedEvent(); }
/** * Send a message to the client, if it supports receiving them. * * <p>If the client doesn't support receiving messages, the message will be discarded, with no * other indication to the caller or to the client. * * @param what string describing the problem identified by the hook. The string must not end with * an LF, and must not contain an LF. */ public void sendMessage(final String what) { try { if (msgOut != null) msgOut.write(Constants.encode(what + "\n")); } catch (IOException e) { // Ignore write failures. } }
private DirCacheEntry makeFile(final String path) throws Exception { final byte[] pathBytes = Constants.encode(path); final DirCacheEntry ent = new DirCacheEntry(path); ent.setFileMode(REGULAR_FILE); ent.setObjectId( new ObjectWriter(db) .computeBlobSha1(pathBytes.length, new ByteArrayInputStream(pathBytes))); return ent; }
/** * Construct a new substring pattern. * * @param patternText text to locate. This should be a literal string, as no meta-characters are * supported by this implementation. The string may not be the empty string. */ public RawSubStringPattern(final String patternText) { if (patternText.length() == 0) throw new IllegalArgumentException(JGitText.get().cannotMatchOnEmptyString); needleString = patternText; final byte[] b = Constants.encode(patternText); needle = new byte[b.length]; for (int i = 0; i < b.length; i++) needle[i] = lc(b[i]); }
public FakeTreeIterator(String pathName, FileMode fileMode) { super(prefix(pathName), new WorkingTreeOptions(AutoCRLF.FALSE)); mode = fileMode.getBits(); final int s = pathName.lastIndexOf('/'); final byte[] name = Constants.encode(pathName.substring(s + 1)); ensurePathCapacity(pathOffset + name.length, pathOffset); System.arraycopy(name, 0, path, pathOffset, name.length); pathLen = pathOffset + name.length; }
/** * Send an error message to the client. * * <p>If any error messages are sent before the references are advertised to the client, the * errors will be sent instead of the advertisement and the receive operation will be aborted. All * clients should receive and display such early stage errors. * * <p>If the reference advertisements have already been sent, messages are sent in a side channel. * If the client doesn't support receiving messages, the message will be discarded, with no other * indication to the caller or to the client. * * <p>{@link PreReceiveHook}s should always try to use {@link ReceiveCommand#setResult(Result, * String)} with a result status of {@link Result#REJECTED_OTHER_REASON} to indicate any reasons * for rejecting an update. Messages attached to a command are much more likely to be returned to * the client. * * @param what string describing the problem identified by the hook. The string must not end with * an LF, and must not contain an LF. */ public void sendError(final String what) { if (refs == null) { if (advertiseError == null) advertiseError = new StringBuilder(); advertiseError.append(what).append('\n'); } else { try { if (msgOut != null) msgOut.write(Constants.encode("error: " + what + "\n")); } catch (IOException e) { // Ignore write failures. } } }
public void start(final Environment env) throws IOException { Context old = sshScope.set(context); String message; try { message = messageFactory.get().getMessage(); } finally { sshScope.set(old); } err.write(Constants.encode(message.toString())); err.flush(); in.close(); out.close(); err.close(); exit.onExit(127); }
/** * Recursively get all entries within a subtree. * * @param path the subtree path to get all entries within. * @return all entries recursively contained within the subtree. */ public DirCacheEntry[] getEntriesWithin(String path) { if (path.length() == 0) { final DirCacheEntry[] r = new DirCacheEntry[sortedEntries.length]; System.arraycopy(sortedEntries, 0, r, 0, sortedEntries.length); return r; } if (!path.endsWith("/")) // $NON-NLS-1$ path += "/"; // $NON-NLS-1$ final byte[] p = Constants.encode(path); final int pLen = p.length; int eIdx = findEntry(p, pLen); if (eIdx < 0) eIdx = -(eIdx + 1); final int lastIdx = nextEntry(p, pLen, eIdx); final DirCacheEntry[] r = new DirCacheEntry[lastIdx - eIdx]; System.arraycopy(sortedEntries, eIdx, r, 0, r.length); return r; }
@Test public void testRejectInvalidWindowsPaths() throws Exception { SystemReader.setInstance( new MockSystemReader() { { setUnix(); } }); String path = "src/con.txt"; DirCache dc = db.lockDirCache(); DirCacheBuilder b = dc.builder(); DirCacheEntry e = new DirCacheEntry(path); e.setFileMode(FileMode.REGULAR_FILE); try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) { e.setObjectId(formatter.idFor(Constants.OBJ_BLOB, Constants.encode(path))); } b.add(e); b.commit(); db.readDirCache(); SystemReader.setInstance( new MockSystemReader() { { setWindows(); } }); try { db.readDirCache(); fail("should have rejected " + path); } catch (CorruptObjectException err) { assertEquals(MessageFormat.format(JGitText.get().invalidPath, path), err.getMessage()); assertNotNull(err.getCause()); assertEquals("invalid name 'CON'", err.getCause().getMessage()); } }
/** * Encode a string pattern for faster matching on byte arrays. * * <p>Force the characters to our funny UTF-8 only convention that we use on raw buffers. This * avoids needing to perform character set decodes on the individual commit buffers. * * @param patternText original pattern string supplied by the user or the application. * @return same pattern, but re-encoded to match our funny raw UTF-8 character sequence {@link * RawCharSequence}. */ protected static final String forceToRaw(final String patternText) { final byte[] b = Constants.encode(patternText); final StringBuilder needle = new StringBuilder(b.length); for (int i = 0; i < b.length; i++) needle.append((char) (b[i] & 0xff)); return needle.toString(); }
@Test public void testFindOnEmpty() throws Exception { final DirCache dc = DirCache.newInCore(); final byte[] path = Constants.encode("a"); assertEquals(-1, dc.findEntry(path, path.length)); }
private void insert(String text) throws IOException { insert(Constants.encode(text)); }
/** * Locate the position a path's entry is at in the index. * * <p>If there is at least one entry in the index for this path the position of the lowest stage * is returned. Subsequent stages can be identified by testing consecutive entries until the path * differs. * * <p>If no path matches the entry -(position+1) is returned, where position is the location it * would have gone within the index. * * @param path the path to search for. * @return if >= 0 then the return value is the position of the entry in the index; pass to {@link * #getEntry(int)} to obtain the entry information. If < 0 the entry does not exist in the * index. */ public int findEntry(final String path) { final byte[] p = Constants.encode(path); return findEntry(p, p.length); }
protected void saveUTF8(String fileName, String text) throws IOException { saveFile(fileName, text != null ? Constants.encode(text) : null); }