git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 4/4] Add getNameString to TreeWalk to get only the last path component

From
Shawn O. Pearce <spearce@spearce.org>
Date
Aug 21, 2008, 20:57 UTC
Message-ID
<1219352258-15431-4-git-send-email-spearce@spearce.org>
In-Reply-To
<1219352258-15431-3-git-send-email-spearce@spearce.org>

Some applications may need to see only the name of the current entry within its parent tree and not require the full path from the root of the repository.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/treewalk/TreeWalk.java    |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java
index ef27e4e..53b94d2 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java
@@ -548,6 +548,24 @@ public boolean idEqual(final int nthA, final int nthB) {
 	}
 
 	/**
+	 * Get the current entry's name within its parent tree.
+	 * <p>
+	 * This method is not very efficient and is primarily meant for debugging
+	 * and final output generation. Applications should try to avoid calling it,
+	 * and if invoked do so only once per interesting entry, where the name is
+	 * absolutely required for correct function.
+	 * 
+	 * @return name of the current entry within the parent tree (or directory).
+	 *         The name never includes a '/'.
+	 */
+	public String getNameString() {
+		final AbstractTreeIterator t = currentHead;
+		final int off = t.pathOffset;
+		final int end = t.pathLen;
+		return RawParseUtils.decode(Constants.CHARSET, t.path, off, end);
+	}
+
+	/**
 	 * Get the current entry's complete path.
 	 * <p>
 	 * This method is not very efficient and is primarily meant for debugging
-- 
1.6.0.112.g9c75
Previous: Shawn O. PearceNext: Robin Rosenberg
Message 4 of 6 in “Simplify and micro-optimize WorkingTreeIterator.ENTRY_CMP”
  1. 1/4 Simplify and micro-optimize WorkingTreeIterator.ENTRY_CMPShawn O. Pearce, Aug 21, 2008
  2. 2/4 Support post order tree handling during TreeWalkShawn O. Pearce, Aug 21, 2008
  3. 3/4 Micro-optimize TreeWalk's exitSubtree implementationShawn O. Pearce, Aug 21, 2008
  4. 4/4 Add getNameString to TreeWalk to get only the last path componentShawn O. Pearce, Aug 21, 2008
  5. Robin RosenbergAug 22, 2008
  6. Shawn O. PearceAug 22, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.