Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Create blobs in-memory #2050

Open
kaspermarstal opened this issue Jul 13, 2023 · 0 comments
Open

Feature request: Create blobs in-memory #2050

kaspermarstal opened this issue Jul 13, 2023 · 0 comments

Comments

@kaspermarstal
Copy link

kaspermarstal commented Jul 13, 2023

I would like to make a diff between two strings with libgit2sharp. The strings hold file contents loaded elsewhere. The diff.Compare-methods only take blobs, and the constructors of the Blobclass don't accept strings or streams and are marked internal. This prevents me from creating blobs directly. I have to write to disk first.

    var oldFileContents = @"
        Line 1
        Line 2
        Line 3
    ";

    var newFileContents = @"
        Line 1
        Line 2 has changed
    ";

    
    var path = Repository.Init("C:\\tmp");
    var repo = new Repository(path);

    var oldObjectId = repo.ObjectDatabase.Write<Blob>(Encoding.UTF8.GetBytes(oldFileContents));
    var newObjectId = repo.ObjectDatabase.Write<Blob>(Encoding.UTF8.GetBytes(newFileContents));
    var oldBlob = repo.Lookup<Blob>(oldObjectId);
    var newBlob = repo.Lookup<Blob>(newObjectId);
    var diff = repo.Diff.Compare(oldBlob, newBlob);  // Works as expected

    // git repo for in-memory operations
    var in_memory_repo = new Repository();

    // This line failes with the LibGit2SharpException "path cannot exist in repository". As far as I can see this is intentional.
    var in_memory_blob = in_memory_repo.ObjectDatabase.Write<Blob>(Encoding.UTF8.GetBytes(oldFileContents));

Seeing that libgit2sharp already supports working with diffs and patches in-memory, it would be nice to be able to create blobs from strings or streams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant