libgit2 / libgit2

A cross-platform, linkable library implementation of Git that you can use in your application.
https://libgit2.org/
Other
9.67k stars 2.41k forks source link

git_repository_init_ext fails on Windows if you supply a working directory which is the same as the repo folder. Worked on earlier version #6823

Open giles45 opened 4 months ago

giles45 commented 4 months ago

On Windows, when calling git_repository_init_ext for a repo that doesn't exist, if you specify the working directory in the options and it is the same as the the repository directory, the call fails. If you make the same call but without specifying the working directory, the call succeeds.

Previous version used worked as expected.

Accepted that supplying the working directory in this scenario is unnecessary and I have changed our client code to do so, though this does look to be a regression.

Reproduction steps

// GitFaultDemo <repo-folder>. E.g. GitFaultDemo "c:\users\myusername\myrepo"
#include <stdio.h>
#include <stdlib.h>

#include <git2.h>

int main(int argc, char* argv[])
{
    const char* repo_path = argv[1];
    int status;

    git_repository* repo = NULL;
    git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;

    status = git_libgit2_init();
    if (status < 0)
    {
        fprintf(stderr, "Failed to initialize libgit2: %d\n", status);
        exit(EXIT_FAILURE);
    }

    opts.flags |= GIT_REPOSITORY_INIT_MKPATH;
    opts.description = "Initialize repository";
    // With this (redundant) line, the call fails on Windows.
    opts.workdir_path = repo_path;

    status = git_repository_init_ext(&repo, repo_path, &opts);
    if (status < 0)
    {
        fprintf(stderr, "Failed to initialize repository: %d\n", status);
        exit(EXIT_FAILURE);
    }
}

If you comment out the specified line, the call works correctly.

Expected behavior

The repo is created and a valid status is returned

Actual behavior

-1 is returned

Version of libgit2 (release number or SHA1)

Not working: v1.8.0 Last working: v1.6.4

Operating system(s) tested

Windows Professional 10/Server 2022 Using vcpkg

Diagnosis

I stepped through to see why it was failing and it looks to me like it was failing because

ethomson commented 2 days ago

Thanks for the report.