0

With libgit2 API, is there any difference between 'adding' a file for tracking, or adding a modified file to the staging area?

This is the code I am currently using to stage tracked files which are modified:

int giterror = git_repository_index( &index, open_repo );
if( giterror != 0 )
{
    return giterror;
}

//  Refresh the index from disk to load the entries that may already be staged
giterror = git_index_read( index );
if( giterror != 0 )
{
    git_index_free( index );

    return giterror;
}


giterror = git_index_add_bypath( index, relativeFilePath );
if( giterror != 0 )
{
    git_index_free( index );

    return giterror;
}


// write updated index to disk - aka staging area
giterror = git_index_write( index );
if( giterror != 0 )
{
    git_index_free( index );

    return giterror;
}


// write the index of changes to a tree
git_oid rootTreetOID;
giterror = git_index_write_tree( &rootTreetOID, index );
if( giterror != 0 )
{
    git_index_free( index );

    return giterror;
}

Should I use the same code to add an untracked file to the index?

Dan
  • 1,513
  • 1
  • 24
  • 34

1 Answers1

1

Yes, you should.

git_index_add_bypath() documentation states that this method should be used when one is willing to "Add or update an index entry from a file on disk".

This method is able to

  • add an untracked file to the index
  • stage the modification of a file
nulltoken
  • 64,429
  • 20
  • 138
  • 130