Exemplo n.º 1
0
test('test rebase',function(assert) {
  assert.plan(1)
  var git = gitjson()
  git.pipe(process.stdout)
  //testing rebase
  git.init()

  //A
  git.save('mydoc',{foo:'bar'})
  git.add('mydoc')
  git.commit('first commit into master')

  //B
  git.save('bigplans',{baz:19})
  git.add('bigplans')
  git.commit('second commit into master')

  //W
  git.branch('topic')
  var obj = git.checkout('topic')
  obj.bigplans.baz--
  git.save('bigplans',obj.bigplans)
  git.add('bigplans')
  git.commit('first commit into topic branch bz is 18')

  //X
  obj.bigplans.baz--
  git.save('bigplans',obj.bigplans)
  git.add('bigplans')
  git.commit('second commit into topic branch bz is 17')


  //Y
  obj.bigplans.baz--
  git.save('bigplans',obj.bigplans)
  git.add('bigplans')
  git.commit('third commit into topic branch bz is 16')

  //C
  var obj = git.checkout('master')
  git.save('anotherdoc',{berry:true,count:0})
  git.add('anotherdoc')
  git.commit('third commit into master')

  //D 
  git.save('anotherdoc',{berry:true,count:1})
  git.add('anotherdoc')
  git.commit('fourth commit into master')

  /*

  A -> B -> C -> D      master
       \
        \_ W -> X -> Y  topic

  */

  git.checkout('topic')
  git.rebase('master')
//  git.visual()
  git.log()
  var mytree = treelib()
  mytree.setTree(git.visualtree)
  var util = require('util')
  assert.equals(mytree.checkPath('dc57103ee7f8c5c16091067962f84717e68abb46/1cf7d4e448eb0006f8c5989ae887c0630478a8ce/5ea0d30a839222ef088068561b9a9f1ada81f1e0/7880388867b39024d469b07c3b5b3cbe73790e31/259c9dbc467ad6dfbd4ccafa6aafe3a8e858a116/c05c403089c6e7f3ce3d76708930669dd0fe6ce4/b5cf769c4f8f5a89e840c795fcb546a9f6c0a8d9').depth,7)
  git.visual()
})
Exemplo n.º 2
0
module.exports = exports = function(message,opts) {
  if (opts === undefined) 
    opts = {} 
  var author = opts.author || this._config.name + ' <'+this._config.email+'>'
  var committer = opts.committer || this._config.name + ' <'+this._config.email+'>'
  this.push("commit:message:"+message+'\n')
  if (message === undefined) 
    message = ''
  if (Object.keys(this.staging).length === 0)
    return {error:'nothing to commit'}

  // we want to take current head towards staged
  // which is represented by diff(head,staged)
  
  var result = common.parseHEAD(this.HEAD)
  var current
  if ((result.type == 'name') && (this.refs[result.value] === null)) {
    // means this is first commit
  } else {
    // otherwise we can set parent
    if (result.type == 'name') {
      var key = this.refs[result.value]
      var _commit = this.commits[key]
      current = this.trees[_commit.tree] 
    } else if (result.type == 'key') {
      var _commit = this.commits[result.value]
      current = this.trees[_commit.tree] 
    }
  }

  var staged = this.staging 
  var new_doc = {}
  if (current !== undefined) {
    var doc_curr = retrieve(current,this.blobs)
    // we look at the diff between doc_curr and staged
    // but not as a whole, but on a per key basis
    new_doc = diffpatch(doc_curr,staged)
  } else {
    new_doc = staged
  }

  var tree = genTree(new_doc)
  var treehash = genTreeSHA(tree)
  // now that we have computed the sha-keys for every doc
  // we can remove them and put them into this.blobs
  var keyDocs = moveDocuments(tree,this.blobs)
  // and add blob pointers (sha-keys) in their stead
  // key docs is a list of {key:<sha-key>,name:<docname>}

  // store tree
  this.trees[treehash] = tree
  var commitObject = {}
  commitObject['tree'] = treehash

  // parent identification
  // check if HEAD resolves to null
  var result = common.parseHEAD(this.HEAD)
  if ((result.type == 'name') && (this.refs[result.value] === null)) {
    // means this is first commit
  } else {
    // otherwise we can set parent
    if (result.type == 'name') {
      commitObject['parent'] = this.refs[result.value]
    } else if (result.type == 'key') {
      commitObject['parent'] = result.value
    }
  }
  var now = new Date()
  commitObject['author'] = author
  commitObject['committer'] = committer
  commitObject['message'] = message
  commitObject['Date'] = now
  var commitString = genCommitString(commitObject)
  // update commitString with the header
  commitString = 'commit '.concat(commitString.length).concat('\0').concat(commitString)

  var shasum = crypto.createHash('sha1')
  var hash = shasum.update(commitString).digest('hex')
  this.commits[hash] = commitObject
  var logC = Hash(commitObject).clone.end
  logC.commit = hash
  logC.date = now
  this.logs.push(logC)
  common.assignRefs(this.refs,this.HEAD,hash)


  // begin tree visualization
  var visualtree = treelib()
  visualtree.setTree(this.visualtree)
  var shakeylist = walkback.apply(this,[hash,true])
  shakeylist.reverse()
  visualtree.path(shakeylist)
  this.visualtree = visualtree.tree()
  this.visualtreekey.push({hash:hash,head:result.value,message:message})
  // end tree visualize 

  // clear staging ({} should trigger recursive GC as = null)
  this.staging = {}
  return hash
}