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

[PATCH JGIT 2/5] FileMode: Store bit masks in int constants

From
Jonas Fonseca <fonseca@diku.dk>
Date
Aug 12, 2009, 14:33 UTC
Message-ID
<2c6b72b30908120733h4ecb3fccm3a1e80c111c1185c@mail.gmail.com>
Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
---
 .../src/org/spearce/jgit/lib/FileMode.java         |   38 ++++++++++++--------
 .../spearce/jgit/treewalk/WorkingTreeIterator.java |    8 ++--
 2 files changed, 27 insertions(+), 19 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java
b/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java
index cf42f37..a1f82f8 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java
@@ -50,21 +50,29 @@
  * </p>
  */
 public abstract class FileMode {
+
+	public static final int TYPE_MASK = 0170000;
+	public static final int TREE_MASK = 0040000;
+	public static final int SYMLINK_MASK = 0120000;
+	public static final int FILE_MASK = 0100000;
+	public static final int GITLINK_MASK = 0160000;
+	public static final int MISSING_MASK = 0000000;
+
 	/** Mode indicating an entry is a {@link Tree}. */
 	@SuppressWarnings("synthetic-access")
-	public static final FileMode TREE = new FileMode(0040000,
+	public static final FileMode TREE = new FileMode(TREE_MASK,
 			Constants.OBJ_TREE) {
 		public boolean equals(final int modeBits) {
-			return (modeBits & 0170000) == 0040000;
+			return (modeBits & TYPE_MASK) == TREE_MASK;
 		}
 	};

 	/** Mode indicating an entry is a {@link SymlinkTreeEntry}. */
 	@SuppressWarnings("synthetic-access")
-	public static final FileMode SYMLINK = new FileMode(0120000,
+	public static final FileMode SYMLINK = new FileMode(SYMLINK_MASK,
 			Constants.OBJ_BLOB) {
 		public boolean equals(final int modeBits) {
-			return (modeBits & 0170000) == 0120000;
+			return (modeBits & TYPE_MASK) == SYMLINK_MASK;
 		}
 	};

@@ -73,7 +81,7 @@ public boolean equals(final int modeBits) {
 	public static final FileMode REGULAR_FILE = new FileMode(0100644,
 			Constants.OBJ_BLOB) {
 		public boolean equals(final int modeBits) {
-			return (modeBits & 0170000) == 0100000 && (modeBits & 0111) == 0;
+			return (modeBits & TYPE_MASK) == FILE_MASK && (modeBits & 0111) == 0;
 		}
 	};

@@ -82,22 +90,22 @@ public boolean equals(final int modeBits) {
 	public static final FileMode EXECUTABLE_FILE = new FileMode(0100755,
 			Constants.OBJ_BLOB) {
 		public boolean equals(final int modeBits) {
-			return (modeBits & 0170000) == 0100000 && (modeBits & 0111) != 0;
+			return (modeBits & TYPE_MASK) == FILE_MASK && (modeBits & 0111) != 0;
 		}
 	};

 	/** Mode indicating an entry is a submodule commit in another repository. */
 	@SuppressWarnings("synthetic-access")
-	public static final FileMode GITLINK = new FileMode(0160000,
+	public static final FileMode GITLINK = new FileMode(GITLINK_MASK,
 			Constants.OBJ_COMMIT) {
 		public boolean equals(final int modeBits) {
-			return (modeBits & 0170000) == 0160000;
+			return (modeBits & TYPE_MASK) == GITLINK_MASK;
 		}
 	};

 	/** Mode indicating an entry is missing during parallel walks. */
 	@SuppressWarnings("synthetic-access")
-	public static final FileMode MISSING = new FileMode(0000000,
+	public static final FileMode MISSING = new FileMode(MISSING_MASK,
 			Constants.OBJ_BAD) {
 		public boolean equals(final int modeBits) {
 			return modeBits == 0;
@@ -112,20 +120,20 @@ public boolean equals(final int modeBits) {
 	 * @return the FileMode instance that represents the given bits.
 	 */
 	public static final FileMode fromBits(final int bits) {
-		switch (bits & 0170000) {
-		case 0000000:
+		switch (bits & TYPE_MASK) {
+		case MISSING_MASK:
 			if (bits == 0)
 				return MISSING;
 			break;
-		case 0040000:
+		case TREE_MASK:
 			return TREE;
-		case 0100000:
+		case FILE_MASK:
 			if ((bits & 0111) != 0)
 				return EXECUTABLE_FILE;
 			return REGULAR_FILE;
-		case 0120000:
+		case SYMLINK_MASK:
 			return SYMLINK;
-		case 0160000:
+		case GITLINK_MASK:
 			return GITLINK;
 		}

diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/WorkingTreeIterator.java
b/org.spearce.jgit/src/org/spearce/jgit/treewalk/WorkingTreeIterator.java
index d4291ea..6003736 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/WorkingTreeIterator.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/WorkingTreeIterator.java
@@ -134,16 +134,16 @@ protected WorkingTreeIterator(final
WorkingTreeIterator p) {
 	public byte[] idBuffer() {
 		if (contentIdFromPtr == ptr)
 			return contentId;
-		switch (mode & 0170000) {
-		case 0100000: /* normal files */
+		switch (mode & FileMode.TYPE_MASK) {
+		case FileMode.FILE_MASK:
 			contentIdFromPtr = ptr;
 			return contentId = idBufferBlob(entries[ptr]);
-		case 0120000: /* symbolic links */
+		case FileMode.SYMLINK_MASK:
 			// Java does not support symbolic links, so we should not
 			// have reached this particular part of the walk code.
 			//
 			return zeroid;
-		case 0160000: /* gitlink */
+		case FileMode.GITLINK_MASK:
 			// TODO: Support obtaining current HEAD SHA-1 from nested repository
 			//
 			return zeroid;
-- 
1.6.4.rc3.195.g2b05f
Next: Jonas Fonseca
Message 1 of 3 in “FileMode: Store bit masks in int constants”
  1. 2/5 FileMode: Store bit masks in int constantsJonas Fonseca, Aug 12, 2009
  2. 2/5 FileMode: Store bit masks in int constantsJonas Fonseca, Aug 12, 2009
  3. Shawn O. PearceAug 12, 2009

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.