[Demonstration 1]

[Buggy Method]
@Test
public void testScanningForPackfiles() throws Exception {
    ObjectId unknownID = ObjectId.fromString("c0ffee09d0b63d694bf49bc1e6847473f42d4a8c");
    GC gc = new GC(db);
    gc.setExpireAgeMillis(0);
    gc.setPackExpireAgeMillis(0);
    // the default repo db is used to create the objects. The receivingDB
    // repo is used to trigger gc's
    try (FileRepository receivingDB = new FileRepository(db.getDirectory())) {
        // set trustfolderstat to true. If set to false the test always
        // succeeds.
        FileBasedConfig cfg = receivingDB.getConfig();
        cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_TRUSTFOLDERSTAT, true);
        cfg.save();
        // setup a repo which has at least one pack file and trigger
        // scanning of the packs directory
        ObjectId id = commitFile("file.txt", "test", "master").getId();
        gc.gc();
        assertFalse(receivingDB.hasObject(unknownID));
        assertTrue(receivingDB.getObjectDatabase().hasPackedObject(id));
        // preparations
        File packsFolder = new File(receivingDB.getObjectsDirectory(), "pack");
        // prepare creation of a temporary file in the pack folder. This
        // simulates that a native git gc is happening starting to write
        // temporary files but has not yet finished
        File tmpFile = new File(packsFolder, "1.tmp");
        RevCommit id2 = commitFile("file.txt", "test2", "master");
        // wait until filesystem timer ticks. This raises probability that
        // the next statements are executed in the same tick as the
        // filesystem timer
        fsTick(null);
        // create a Temp file in the packs folder and trigger a rescan of
        // the packs folder. This lets receivingDB think it has scanned the
        // packs folder at the current fs timestamp t1. The following gc
        // will create new files which have the same timestamp t1 but this
        // will not update the mtime of the packs folder. Because of that
        // JGit will not rescan the packs folder later on and fails to see
        // the pack file created during gc.
        assertTrue(tmpFile.createNewFile());
        assertFalse(receivingDB.hasObject(unknownID));
        // trigger a gc. This will create packfiles which have likely the
        // same mtime than the packfolder
        gc.gc();
        // To deal with racy-git situations JGit's Filesnapshot class will
        // report a file/folder potentially dirty if
        // cachedLastReadTime-cachedLastModificationTime < 2500ms. This
        // causes JGit to always rescan a file after modification. But:
        // this was true only if the difference between current system time
        // and cachedLastModification time was less than 2500ms. If the
        // modification is more than 2500ms ago we may have reported a
        // file/folder to be clean although it has not been rescanned. A
        // Bug. To show the bug we sleep for more than 2500ms
        Thread.sleep(2600);
        File[] ret = packsFolder.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".pack");
            }
        });
        assertTrue(ret.length == 1);
        Assume.assumeTrue(tmpFile.lastModified() == ret[0].lastModified());
        // all objects are in a new packfile but we will not detect it
        assertFalse(receivingDB.hasObject(unknownID));
        assertTrue(receivingDB.hasObject(id2));
    }
}

[Buggy Hunk]
assertTrue(ret.length == 1);

[Patch]
assertTrue(ret != null && ret.length == 1);

[Demonstration 2]

[Buggy Method]
private ITypedElement getHeadTypedElement(final IFile baseFile) throws IOException {
    final RepositoryMapping mapping = RepositoryMapping.getMapping(baseFile.getProject());
    final Repository repository = mapping.getRepository();
    final String gitPath = mapping.getRepoRelativePath(baseFile);
    DirCache dc = repository.lockDirCache();
    final DirCacheEntry entry = dc.getEntry(gitPath);
    dc.unlock();
    if (entry == null) {
        // the file cannot be found in the index
        return new GitCompareFileRevisionEditorInput.EmptyTypedElement(NLS.bind(UIText.CompareWithIndexAction_FileNotInIndex, baseFile.getName()));
    }
    IFileRevision nextFile = GitFileRevision.inIndex(repository, gitPath);
    String encoding = CompareUtils.getResourceEncoding(baseFile);
    final EditableRevision next = new EditableRevision(nextFile, encoding);
    IContentChangeListener listener = new IContentChangeListener() {

        public void contentChanged(IContentChangeNotifier source) {
            final byte[] newContent = next.getModifiedContent();
            DirCache cache = null;
            try {
                cache = repository.lockDirCache();
                DirCacheEditor editor = cache.editor();
                editor.add(new PathEdit(gitPath) {

                    @Override
                    public void apply(DirCacheEntry ent) {
                        ent.copyMetaData(entry);
                        ObjectInserter inserter = repository.newObjectInserter();
                        ent.copyMetaData(entry);
                        ent.setLength(newContent.length);
                        ent.setLastModified(System.currentTimeMillis());
                        InputStream in = new ByteArrayInputStream(newContent);
                        try {
                            ent.setObjectId(inserter.insert(Constants.OBJ_BLOB, newContent.length, in));
                            inserter.flush();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        } finally {
                            try {
                                in.close();
                            } catch (IOException e) {
                                // ignore here
                            }
                        }
                    }
                });
                try {
                    editor.commit();
                } catch (RuntimeException e) {
                    if (e.getCause() instanceof IOException)
                        throw (IOException) e.getCause();
                    else
                        throw e;
                }
            } catch (IOException e) {
                Activator.handleError(UIText.CompareWithIndexAction_errorOnAddToIndex, e, true);
            } finally {
                if (cache != null)
                    cache.unlock();
            }
        }
    };
    next.addContentChangeListener(listener);
    return next;
}

[Buggy Hunk]
String encoding = CompareUtils.getResourceEncoding(baseFile);

[Patch]
String encoding = CompareCoreUtils.getResourceEncoding(baseFile);

[Demonstration 3]

[Buggy Method]
@Override
public List<Meal> getAll(int userId) {
    return repository.getAll();
}

[Buggy Hunk]
return repository.getAll();

[Patch]
return repository.getAll(userId);