]> source.dussan.org Git - jgit.git/commitdiff
Check assume unchanged flag in Add command 70/1970/2
authorStefan Lay <stefan.lay@sap.com>
Mon, 29 Nov 2010 16:58:38 +0000 (17:58 +0100)
committerStefan Lay <stefan.lay@sap.com>
Mon, 29 Nov 2010 16:58:38 +0000 (17:58 +0100)
When the assume unchanged flag is set the Add command must not update
the index for this file if any changes are present in the working
directory.

Bug: 331351
Change-Id: I255870f689225a1d88971182e0eb377952641b42
Signed-off-by: Stefan Lay <stefan.lay@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java
org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java

index 68007b6cffed0f762eee23333baf63b16ca9c376..4357f8b0c172fb1f414ce3299f078a05a82fdf5d 100644 (file)
@@ -456,6 +456,38 @@ public class AddCommandTest extends RepositoryTestCase {
                                indexState(CONTENT));
        }
 
+       public void testAssumeUnchanged() throws Exception {
+               Git git = new Git(db);
+               String path = "a.txt";
+               writeTrashFile(path, "content");
+               git.add().addFilepattern(path).call();
+               String path2 = "b.txt";
+               writeTrashFile(path2, "content");
+               git.add().addFilepattern(path2).call();
+               git.commit().setMessage("commit").call();
+               assertEquals("[a.txt, mode:100644, content:"
+                               + "content, assume-unchanged:false]"
+                               + "[b.txt, mode:100644, content:content, "
+                               + "assume-unchanged:false]", indexState(CONTENT
+                               | ASSUME_UNCHANGED));
+               assumeUnchanged(path2);
+               assertEquals("[a.txt, mode:100644, content:content, "
+                               + "assume-unchanged:false][b.txt, mode:100644, "
+                               + "content:content, assume-unchanged:true]", indexState(CONTENT
+                               | ASSUME_UNCHANGED));
+               writeTrashFile(path, "more content");
+               writeTrashFile(path2, "more content");
+
+               git.add().addFilepattern(".").call();
+
+               assertEquals("[a.txt, mode:100644, content:more content,"
+                               + " assume-unchanged:false][b.txt, mode:100644,"
+ + "" + ""
+                               + " content:content, assume-unchanged:true]",
+                               indexState(CONTENT
+                               | ASSUME_UNCHANGED));
+       }
+
        private DirCacheEntry addEntryToBuilder(String path, File file,
                        ObjectInserter newObjectInserter, DirCacheBuilder builder, int stage)
                        throws IOException {
@@ -473,4 +505,14 @@ public class AddCommandTest extends RepositoryTestCase {
                return entry;
        }
 
+       private void assumeUnchanged(String path) throws IOException {
+               final DirCache dirc = db.lockDirCache();
+               final DirCacheEntry ent = dirc.getEntry(path);
+               if (ent != null)
+                       ent.setAssumeValid(true);
+               dirc.write();
+               if (!dirc.commit())
+                       throw new IOException("could not commit");
+       }
+
 }
index 409fabf8f3afe9cca256038e6d8965903df3c0c3..30cec127981b77ff02f021c2441cddb93510e2e9 100644 (file)
@@ -138,6 +138,8 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
 
        public static final int CONTENT = 16;
 
+       public static final int ASSUME_UNCHANGED = 32;
+
        /**
         * Represent the state of the index in one String. This representation is
         * useful when writing tests which do assertions on the state of the index.
@@ -209,6 +211,9 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
                                                + new String(db.open(entry.getObjectId(),
                                                                Constants.OBJ_BLOB).getCachedBytes(), "UTF-8"));
                        }
+                       if (0 != (includedOptions & ASSUME_UNCHANGED))
+                               sb.append(", assume-unchanged:"
+                                               + Boolean.toString(entry.isAssumeValid()));
                        sb.append("]");
                }
                return sb.toString();
index 7a01e74b064c59acf6487e9dff9a4e6b388517ac..12a5201f6e16e49aca2984467bfd05bffc53999b 100644 (file)
@@ -163,25 +163,31 @@ public class AddCommand extends GitCommand<DirCache> {
                                // new DirCacheEntry per path.
                                else if (!(path.equals(lastAddedFile))) {
                                        if (!(update && tw.getTree(0, DirCacheIterator.class) == null)) {
+                                               c = tw.getTree(0, DirCacheIterator.class);
                                                if (f != null) { // the file exists
                                                        long sz = f.getEntryLength();
                                                        DirCacheEntry entry = new DirCacheEntry(path);
-                                                       entry.setLength(sz);
-                                                       entry.setLastModified(f.getEntryLastModified());
-                                                       entry.setFileMode(f.getEntryFileMode());
-
-                                                       InputStream in = f.openEntryStream();
-                                                       try {
-                                                               entry.setObjectId(inserter.insert(
-                                                                               Constants.OBJ_BLOB, sz, in));
-                                                       } finally {
-                                                               in.close();
+                                                       if (c == null || c.getDirCacheEntry() == null
+                                                                       || !c.getDirCacheEntry().isAssumeValid()) {
+                                                               entry.setLength(sz);
+                                                               entry.setLastModified(f.getEntryLastModified());
+                                                               entry.setFileMode(f.getEntryFileMode());
+
+                                                               InputStream in = f.openEntryStream();
+                                                               try {
+                                                                       entry.setObjectId(inserter.insert(
+                                                                                       Constants.OBJ_BLOB, sz, in));
+                                                               } finally {
+                                                                       in.close();
+                                                               }
+
+                                                               builder.add(entry);
+                                                               lastAddedFile = path;
+                                                       } else {
+                                                               builder.add(c.getDirCacheEntry());
                                                        }
 
-                                                       builder.add(entry);
-                                                       lastAddedFile = path;
                                                } else if (!update){
-                                                       c = tw.getTree(0, DirCacheIterator.class);
                                                        builder.add(c.getDirCacheEntry());
                                                }
                                        }