From 2693708206dd3c2983c397a35f442d51f4b3ca94 Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Sun, 19 Aug 2007 21:41:47 +0200 Subject: [PATCH] Make GitIndex.Entry non-static Prepare for config dependent behaviour such as core.filemode Signed-off-by: Robin Rosenberg --- .../org/spearce/egit/core/internal/UpdateJob.java | 4 +- .../spearce/egit/core/project/CheckpointJob.java | 2 +- .../egit/ui/internal/actions/CommitAction.java | 2 +- .../egit/ui/internal/dialogs/CommitDialog.java | 2 +- .../src/org/spearce/jgit/lib/GitIndex.java | 126 ++++++++++----------- .../tst/org/spearce/jgit/lib/T0007_Index.java | 8 -- 6 files changed, 63 insertions(+), 81 deletions(-) diff --git a/org.spearce.egit.core/src/org/spearce/egit/core/internal/UpdateJob.java b/org.spearce.egit.core/src/org/spearce/egit/core/internal/UpdateJob.java index 648f502e..013874bb 100644 --- a/org.spearce.egit.core/src/org/spearce/egit/core/internal/UpdateJob.java +++ b/org.spearce.egit.core/src/org/spearce/egit/core/internal/UpdateJob.java @@ -92,7 +92,7 @@ public class UpdateJob extends Job { String path = rm.getRepoRelativePath(resource); Entry entry = index.getEntry(path); if (entry != null) { - entry.update(new File(rm.getWorkDir(),path), rm.getRepository()); + entry.update(new File(rm.getWorkDir(),path)); } fm.worked(1); } @@ -107,7 +107,7 @@ public class UpdateJob extends Job { String path = rm.getRepoRelativePath(r); Entry entry = index.getEntry(path); if (entry != null) { - entry.update(new File(rm.getWorkDir(),path), rm.getRepository()); + entry.update(new File(rm.getWorkDir(),path)); } m.worked(1); } diff --git a/org.spearce.egit.core/src/org/spearce/egit/core/project/CheckpointJob.java b/org.spearce.egit.core/src/org/spearce/egit/core/project/CheckpointJob.java index 05ddbcb9..acc8f1f2 100644 --- a/org.spearce.egit.core/src/org/spearce/egit/core/project/CheckpointJob.java +++ b/org.spearce.egit.core/src/org/spearce/egit/core/project/CheckpointJob.java @@ -63,7 +63,7 @@ public class CheckpointJob extends Job { if (i % 100 == 0) monitor.worked(i); indexEntries[i].update(new File(rm.getWorkDir(), - indexEntries[i].getName()), rm.getRepository()); + indexEntries[i].getName())); } index.write(); GitProjectData.fireRepositoryChanged(rm); diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java index 0c2e2f1e..22bafe12 100644 --- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java +++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java @@ -281,7 +281,7 @@ public class CommitAction implements IObjectActionDelegate { System.out.println("Phantom file, so removing from index"); continue; } else { - if (idxEntry.update(thisfile, repository)) + if (idxEntry.update(thisfile)) index.write(); } } diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/dialogs/CommitDialog.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/dialogs/CommitDialog.java index dc247f05..d5c49664 100644 --- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/dialogs/CommitDialog.java +++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/dialogs/CommitDialog.java @@ -284,7 +284,7 @@ public class CommitDialog extends Dialog { index = repo.getIndex(); Entry entry = index.getEntry(map.getRepoRelativePath(file)); if (entry != null && entry.isModified(map.getWorkDir())) { - entry.update(new File(map.getWorkDir(), entry.getName()), repo); + entry.update(new File(map.getWorkDir(), entry.getName())); if (!changedIndexes.contains(index)) changedIndexes.add(index); } diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/GitIndex.java b/org.spearce.jgit/src/org/spearce/jgit/lib/GitIndex.java index 75e7e547..c81217ce 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/GitIndex.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/GitIndex.java @@ -77,18 +77,18 @@ public class GitIndex { } public void add(File wd, File f) throws IOException { - byte[] key = Entry.makeKey(wd, f); + byte[] key = makeKey(wd, f); Entry e = (Entry) entries.get(key); if (e == null) { - e = new Entry(key, f, 0, this); + e = new Entry(key, f, 0); entries.put(key, e); } else { - e.update(f, db); + e.update(f); } } public boolean remove(File wd, File f) { - byte[] key = Entry.makeKey(wd, f); + byte[] key = makeKey(wd, f); return entries.remove(key) != null; } @@ -108,7 +108,7 @@ public class GitIndex { header = new Header(buffer); entries.clear(); for (int i = 0; i < header.entries; ++i) { - Entry entry = new Entry(this, buffer); + Entry entry = new Entry(buffer); entries.put(entry.name, entry); } long t1 = System.currentTimeMillis(); @@ -181,7 +181,48 @@ public class GitIndex { } } - public static class Entry { + static Method canExecute; + static { + try { + canExecute = File.class.getMethod("canExecute", (Class[]) null); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + System.out.println("This vm cannot handle execute file permission. Feature disabled"); + } + } + + /* + * JDK1.6 has file.canExecute + * + * if (file.canExecute() != FileMode.EXECUTABLE_FILE.equals(mode)) + * return true; + */ + boolean File_canExecute(File f) { + if (canExecute != null) { + try { + return ((Boolean) canExecute.invoke(f, (Object[]) null)) + .booleanValue(); + } catch (IllegalArgumentException e) { + throw new Error(e); + } catch (IllegalAccessException e) { + throw new Error(e); + } catch (InvocationTargetException e) { + throw new Error(e); + } + } else + return false; + } + + static byte[] makeKey(File wd, File f) { + if (!f.getPath().startsWith(wd.getPath())) + throw new Error("Path is not in working dir"); + String relName = f.getPath().substring(wd.getPath().length() + 1) + .replace(File.separatorChar, '/'); + return relName.getBytes(); + } + + public class Entry { private long ctime; private long mtime; @@ -206,18 +247,8 @@ public class GitIndex { private int stage; - private GitIndex theIndex; - - static byte[] makeKey(File wd, File f) { - if (!f.getPath().startsWith(wd.getPath())) - throw new Error("Path is not in working dir"); - String relName = f.getPath().substring(wd.getPath().length() + 1); - return Repository.gitInternalSlash(relName.getBytes()); - } - - public Entry(byte[] key, File f, int stage, GitIndex index) + public Entry(byte[] key, File f, int stage) throws IOException { - theIndex = index; ctime = f.lastModified() * 1000000L; mtime = ctime; // we use same here dev = -1; @@ -226,15 +257,14 @@ public class GitIndex { uid = -1; gid = -1; size = (int) f.length(); - ObjectWriter writer = new ObjectWriter(theIndex.db); + ObjectWriter writer = new ObjectWriter(db); sha1 = writer.writeBlob(f); name = key; flags = (short) ((stage << 12) | name.length); // TODO: fix flags } - public Entry(TreeEntry f, int stage, GitIndex index) + public Entry(TreeEntry f, int stage) throws UnsupportedEncodingException { - theIndex = index; ctime = -1; // hmm mtime = -1; dev = -1; @@ -248,8 +278,7 @@ public class GitIndex { flags = (short) ((stage << 12) | name.length); // TODO: fix flags } - Entry(GitIndex index, ByteBuffer b) { - theIndex = index; + Entry(ByteBuffer b) { int startposition = b.position(); ctime = b.getInt() * 1000000000L + (b.getInt() % 1000000000L); mtime = b.getInt() * 1000000000L + (b.getInt() % 1000000000L); @@ -272,7 +301,7 @@ public class GitIndex { + name.length + 8) & ~7)); } - public boolean update(File f, Repository db) throws IOException { + public boolean update(File f) throws IOException { boolean modified = false; long lm = f.lastModified() * 1000000L; if (mtime != lm) @@ -317,47 +346,6 @@ public class GitIndex { buf.put((byte) 0); } - static Method canExecute; - static { - try { - canExecute = File.class.getMethod("canExecute", (Class[]) null); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* - * JDK1.6 has file.canExecute - * - * if (file.canExecute() != FileMode.EXECUTABLE_FILE.equals(mode)) - * return true; - */ - boolean File_canExecute(File f) { - if (canExecute != null) { - try { - return ((Boolean) canExecute.invoke(f, (Object[]) null)) - .booleanValue(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } - } else - return false; - } - /** * Check if an entry's content is different from the cache, * @@ -395,11 +383,14 @@ public class GitIndex { // JDK1.6 has file.canExecute // if (file.canExecute() != FileMode.EXECUTABLE_FILE.equals(mode)) // return true; + final int exebits = FileMode.EXECUTABLE_FILE.getBits() + ^ FileMode.REGULAR_FILE.getBits(); + if (FileMode.EXECUTABLE_FILE.equals(mode)) { if (!File_canExecute(file)&& canExecute != null) return true; } else { - if (FileMode.REGULAR_FILE.equals(mode)) { + if (FileMode.REGULAR_FILE.equals(mode&~exebits)) { if (!file.isFile()) return true; if (File_canExecute(file) && canExecute != null) @@ -429,12 +420,11 @@ public class GitIndex { try { InputStream is = new FileInputStream(file); - ObjectWriter objectWriter = new ObjectWriter(theIndex.db); + ObjectWriter objectWriter = new ObjectWriter(db); try { ObjectId newId = objectWriter.computeBlobSha1(file .length(), is); boolean ret = !newId.equals(sha1); - theIndex.statDirty = true; return ret; } catch (IOException e) { e.printStackTrace(); @@ -533,7 +523,7 @@ public class GitIndex { if (te instanceof Tree) { readTree(name, (Tree) te); } else { - Entry e = new Entry(te, 0, this); + Entry e = new Entry(te, 0); entries.put(name.getBytes("UTF-8"), e); } } diff --git a/org.spearce.jgit/tst/org/spearce/jgit/lib/T0007_Index.java b/org.spearce.jgit/tst/org/spearce/jgit/lib/T0007_Index.java index 23555adb..4755e953 100644 --- a/org.spearce.jgit/tst/org/spearce/jgit/lib/T0007_Index.java +++ b/org.spearce.jgit/tst/org/spearce/jgit/lib/T0007_Index.java @@ -62,7 +62,6 @@ public class T0007_Index extends RepositoryTestCase { } public void testCreateEmptyIndex() throws Exception { - Repository db = new Repository(trash_git); GitIndex index = new GitIndex(db); index.write(); // native git doesn't like an empty index @@ -74,7 +73,6 @@ public class T0007_Index extends RepositoryTestCase { } public void testCreateSimpleSortTestIndex() throws Exception { - Repository db = new Repository(trash_git); GitIndex index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a:b", "data:a:b"); @@ -102,7 +100,6 @@ public class T0007_Index extends RepositoryTestCase { } public void testUpdateSimpleSortTestIndex() throws Exception { - Repository db = new Repository(trash_git); GitIndex index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a:b", "data:a:b"); @@ -118,7 +115,6 @@ public class T0007_Index extends RepositoryTestCase { } public void testWriteTree() throws Exception { - Repository db = new Repository(trash_git); GitIndex index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a:b", "data:a:b"); @@ -140,7 +136,6 @@ public class T0007_Index extends RepositoryTestCase { public void testReadTree() throws Exception { // Prepare tree - Repository db = new Repository(trash_git); GitIndex index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a:b", "data:a:b"); @@ -179,7 +174,6 @@ public class T0007_Index extends RepositoryTestCase { public void testReadTree2() throws Exception { // Prepare a larger tree to test some odd cases in tree writing - Repository db = new Repository(trash_git); GitIndex index = new GitIndex(db); File f1 = writeTrashFile("a/a/a/a", "data:a/a/a/a"); File f2 = writeTrashFile("a/c/c", "data:a/c/c"); @@ -224,7 +218,6 @@ public class T0007_Index extends RepositoryTestCase { } public void testDelete() throws Exception { - Repository db = new Repository(trash_git); GitIndex index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a:b", "data:a:b"); @@ -250,7 +243,6 @@ public class T0007_Index extends RepositoryTestCase { public void testCheckout() throws Exception { // Prepare tree, remote it and checkout - Repository db = new Repository(trash_git); GitIndex index = new GitIndex(db); File aslashb = writeTrashFile("a/b", "data:a/b"); File acolonb = writeTrashFile("a:b", "data:a:b"); -- 2.11.4.GIT