     readDir =        (path) -> session.getBranch().contents   path
 
 
+    # Only support reading 1 file at a time.
+    # Writing is done in batch by save (for multifile commits and conflict resolution).
     Backbone.sync = (method, model, options) ->
 
       path = model.id or model.url?() or model.url



    # Github read/write and repo configuration

    writeFile = (path, model, commitText, isBase64) ->
      text = model.serialize()
      lastSeenSha = remoteUpdater.lastSeenSha
      promise = $.Deferred()
      # .write expects the text to be base64 encoded so no need to convert it
      session.getBranch().write(path, text, commitText, isBase64, lastSeenSha)
      .done((val) => model.onSaved?(); promise.resolve(val))
      .fail (err) =>
        # Get the new lastSeenSha
        remoteUpdater.pollUpdates().then () =>
            # Probably a conflict because of a remote change.
            # Resolve the changes and save again
            model.reload()
            .fail((err) => promise.reject(err))
            .done () =>
              # Probably a patch/cache problem.
              # Clear the cache and try again
              session.getClient().clearCache?()
              writeFile(path, model, commitText, isBase64)
              .fail((err) => promise.reject(err))
              .done (val) => promise.resolve(val)


      return promise

    writeFiles = (models, commitText) ->
      parentCommitSha = remoteUpdater.lastSeenSha
      promise = $.Deferred()

      ret = null
      switch method
        when 'read' then ret = readFile(path, model.isBinary)
        else throw "Model sync method not supported: #{method}"

      ret.done (value) => options?.success?(value)
