var fs = require("fs");
var nodePath = require("path");
var index = require("./index");
var files = require("./files");
var objects = require("./objects");
var refs = require("./refs");
var diff = require("./diff");
var util = require("./util");

var checkout = module.exports = {
  readChangedFilesCheckoutWouldOverwrite: function(checkoutHash) {
    var localChanges = diff.readDiff("HEAD");
    var headToBranchChanges = diff.readDiff("HEAD", checkoutHash);
    return Object.keys(localChanges)
      .filter(function(path) { return path in headToBranchChanges; });
  },

  writeCheckout: function(ref) {
    addModifyDelete("HEAD", refs.readHash(ref));
    fs.readdirSync(files.repoDir())
      .filter(function(dirChild) { return dirChild !== ".gitlet"; })
      .filter(function(dirChild) { return fs.statSync(dirChild).isDirectory(); })
      .forEach(files.deleteEmptyDirs);

    refs.writeLocal("HEAD", objects.readExists(ref) ? ref : "ref: " + refs.toLocalRef(ref));
  }
};

function addModifyDelete(diffFromRef, diffToRef) {
  var changes = diff.readDiff(diffFromRef, diffToRef);
  var checkoutIndex = index.readCommitIndex(diffToRef);
  Object.keys(changes).forEach(function(path) {
    if (changes[path] === diff.FILE_STATUS.ADD ||
        changes[path] === diff.FILE_STATUS.MODIFY) { // no line by line for now
        var content = objects.read(checkoutIndex[path]);
      files.writeFilesFromTree(util.assocIn({}, path.split(nodePath.sep).concat(content)),
                               files.repoDir());
    } else if (changes[path] === diff.FILE_STATUS.DELETE) {
      fs.unlinkSync(path);
    }
  });
};

@@ -597,24 +597,59 @@
 tion(ref) %7B%0A
+    var hash = refs.readHash(ref);%0A
     addModif
@@ -664,34 +664,20 @@
 %22HEAD%22, 
-refs.readHash(ref)
+hash
 );%0A    f
@@ -991,16 +991,62 @@
 (ref));%0A
+    index.write(index.readCommitIndex(hash));%0A
   %7D%0A%7D;%0A%0A

Make checkout write index of checked out commit