1- var git = require ( '../' ) ,
1+ var nodegit = require ( '../' ) ,
22 path = require ( 'path' ) ,
3- fileName = 'newfile.txt'
4- ;
3+ fileName = 'newfile.txt' ;
54
65/**
7- * This example deletes a certain file `newfile.txt`, removes it from the git index and
6+ * This example deletes a certain file `newfile.txt`, removes it from the git index and
87 * commits it to head. Similar to a `git rm newfile.txt` followed by a `git commit`
98 * Use add-and-commit.js to create the file first.
109**/
1110
12- //open a git repo
13- git . Repo . open ( path . resolve ( __dirname , '../.git' ) , function ( openReporError , repo ) {
14- if ( openReporError ) throw openReporError ;
11+ var _repository ;
12+ var _index ;
13+ var _oid ;
1514
15+ //open a git repo
16+ nodegit . Repository . open ( path . resolve ( __dirname , '../.git' ) ) . then ( function ( repo ) {
17+ _repository = repo ;
18+ return repo . openIndex ( ) ;
19+ } ) . then ( function ( index ) {
20+ _index = index ;
21+ return _index . read ( ) ;
22+ } ) . then ( function ( ) {
1623 //remove the file from the index...
17- repo . openIndex ( function ( openIndexError , index ) {
18- if ( openIndexError ) throw openIndexError ;
19-
20- index . read ( function ( readError ) {
21- if ( readError ) throw readError ;
22-
23- index . removeByPath ( fileName ) ;
24-
25- index . write ( function ( writeError ) {
26- if ( writeError ) throw writeError ;
27-
28- index . writeTree ( function ( writeTreeError , oid ) {
29- if ( writeTreeError ) throw writeTreeError ;
30-
31- //get HEAD
32- git . Reference . oidForName ( repo , 'HEAD' , function ( oidForName , head ) {
33- if ( oidForName ) throw oidForName ;
34-
35- //get latest commit (will be the parent commit)
36- repo . getCommit ( head , function ( getCommitError , parent ) {
37- if ( getCommitError ) throw getCommitError ;
38- var author = git . Signature . create ( "Scott Chacon" , "schacon@gmail.com" , 123456789 , 60 ) ;
39- var committer = git . Signature . create ( "Scott A Chacon" , "scott@github.com" , 987654321 , 90 ) ;
40-
41- //commit
42- repo . createCommit ( 'HEAD' , author , committer , 'message' , oid , [ parent ] , function ( error , commitId ) {
43- console . log ( "New Commit:" , commitId . sha ( ) ) ;
44- // the file is removed from the git repo, use fs.unlink now to remove it
45- // from the filesystem.
46- } ) ;
47- } ) ;
48- } ) ;
49- } ) ;
50- } ) ;
51- } ) ;
52- } ) ;
53- } ) ;
24+ _index . removeByPath ( fileName ) ;
25+ return _index . write ( ) ;
26+ } ) . then ( function ( ) {
27+ return _index . writeTree ( ) ;
28+ } ) . then ( function ( oid ) {
29+ _oid = oid ;
30+ return nodegit . Refs . nameToId ( _repository , "HEAD" ) ;
31+ } ) . then ( function ( head ) {
32+ return _repository . getCommit ( head ) ;
33+ } ) . then ( function ( parent ) {
34+ var author = nodegit . Signature . create ( "Scott Chacon" , "schacon@gmail.com" , 123456789 , 60 ) ;
35+ var committer = nodegit . Signature . create ( "Scott A Chacon" , "scott@github.com" , 987654321 , 90 ) ;
36+
37+ return _repository . createCommit ( 'HEAD' , author , committer , "message" , _oid , [ parent ] ) ;
38+ } ) . then ( function ( commitId ) {
39+ // the file is removed from the git repo, use fs.unlink now to remove it
40+ // from the filesystem.
41+ console . log ( "New Commit:" , commitId . allocfmt ( ) ) ;
42+ } ) . done ( ) ;
0 commit comments