libgit2 (3/5)
Staging

Example

The full example is available from our Codeplex repository at Git/libgit2/GitStaging1.

File: Git/libgit2/GitStaging1/Source/Main.cpp

#include <git2.h>
#include <iostream>
#include <fstream>

int AddFileToObjects(git_repository* repo)
{
    // Create new file in the working directory
    std::ofstream helloWorldFile("Libgit2GitRepoAfterStaging1/HelloWorld.txt", std::ios::binary);
    helloWorldFile << "Hello World!\n";
    helloWorldFile.close();

    if (!helloWorldFile.good())
    {
        std::cout << "Failed to create file HelloWorld.txt" << std::endl;
        return -1;
    }

    // Add the file to the objects
    git_oid helloWorldOid;
    int err = git_blob_create_fromworkdir(&helloWorldOid, repo, "HelloWorld.txt");
    if (err != 0)
    {
        std::cout << "Error: git_blob_create_fromworkdir failed" << std::endl;
    }
    return err;
}

void AddFileToStagingArea(git_repository* repo)
{
    // Get the staging area from the git_repository
    // instance. The git_repository_index function 
    // creates a new instance of git_index that is 
    // used to interact with the staging area.
    git_index* index = 0;
    int err = git_repository_index(&index, repo);
    if (err == 0)
    {
        // Add the HelloWorld.txt file to the staging area
        err = git_index_add_bypath(index, "HelloWorld.txt");
        if (err == 0)
        {
            // Write the updated staging area file back to disk
            err = git_index_write(index);
            if (err != 0)
            {
                std::cout << "Error: git_index_write failed" << std::endl;
            }
        }
        else
        {
            std::cout << "Error: git_index_add_bypath failed" << std::endl;
        }

        git_index_free(index);
    }
    else
    {
        std::cout << "Error: git_repository_index failed" << std::endl;
    }
}

int main(int argc, char* argv[])
{
    // Initialize the library
    int n = git_libgit2_init();
    std::cout << "git_libgit2_init: " << n << std::endl;

    // Initialize a new repository called Libgit2EmptyRepo1.
    // The git_repository_init creates the repository on the filesystem
    // and returns a corresponding git_repository instance
    // to interact with the repository.
    git_repository* repo = 0;
    int err = git_repository_init(&repo, "Libgit2GitRepoAfterStaging1", 0);
    if (err == 0)
    {
        std::cout << "Repository initialized successfully" << std::endl;

        err = AddFileToObjects(repo);
        if (err == 0)
        {
            AddFileToStagingArea(repo);
        }

        git_repository_free(repo);
    }
    else
    {
        std::cout << "Error: git_repository_init failed" << std::endl;
    }

    // git_libgit2_shutdown should be called
    // once for every call to git_libgit2_init
    n = git_libgit2_shutdown();
    std::cout << "git_libgit2_shutdown: " << n << std::endl;

    return 0;
}

blog comments powered by Disqus

Copyright(c) 2006-2015 Xavier Leclercq | Privacy policy

Home
Contact Us
Search