2

Hi I want to git clone into my current project folder in c# without using any external nuget packages.

I am looking at System.Diagnostics but am not sure how to implement this.

Any advice?

`

ProcessStartInfo gitInfo = new ProcessStartInfo();
gitInfo.CreateNoWindow = true;

Process gitProcess = new Process();
gitInfo.Arguments = "git clone";
gitInfo.WorkingDirectory = ?

`

Sienna
  • 83
  • 8
  • Does this answer your question? [Run git commands from a C# function](https://stackoverflow.com/questions/26167387/run-git-commands-from-a-c-sharp-function) – Chetan Oct 27 '22 at 02:51
  • @Chetan that works but how can I clone it into the current directory rather than currentDirectory/bin/Releases/ ? – Sienna Oct 27 '22 at 05:47
  • Can you share the code which is now working for you? – Chetan Oct 27 '22 at 06:19
  • 2 possibilities: 1.Git clone accept a directory as argument where to clone to 2. You could fill the WorkingDirectory property but git clone command will create a sub folder and will clone into it (except if you pass '.' as path argument to the command. See 1.) – Philippe Oct 27 '22 at 06:55
  • @Philippe how can I set git commands and WorkingDirectory for a Process? I a currently doing Process.start("git", "git clone") – Sienna Oct 27 '22 at 11:51
  • @Sona Put everything in an answer... – Philippe Oct 27 '22 at 13:12

1 Answers1

1

The 3 options:

  1. Don't let git create the repo folder (by specifying it in the command line):
    var process = new Process
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = "git",
            Arguments = "clone https://repo/url c:/target/path/for/repo",
        }
    };
    process.Start();

equivalent also to:

    Process.start("git", "clone https://repo/url c:/target/path/for/repo");

But if you want to specify the working directory, you have to use ProcessStartInfo class.

1.bis. Don't let git create the repo folder (by using the current working directory . so you have to fill the WorkingDirectory property):

    var process = new Process
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = "git",
            Arguments = "clone https://repo/url .",
            WorkingDirectory = "c:/target/path/for/repo",
        }
    };
    process.Start();
  1. Let git create the repo folder in the working directory folder provided:
    var process = new Process
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = "git",
            Arguments = "clone https://repo/url",
            WorkingDirectory = "c:/target/path/for/repo",
        }
    };
    process.Start();
Philippe
  • 28,207
  • 6
  • 54
  • 78
  • I tried this but I'm now facing the issue here when my URL is ssh://git@github.com/repo and I also tried git@github.con/repo https://stackoverflow.com/questions/65061142/git-clone-on-windows-gitgithub-com-is-not-a-git-command – Sienna Oct 27 '22 at 13:57
  • @Sona Difficult to say because there are typos and you don't provide the real data. The uri should be something like `git@github.com/account/repo_name`. But try to find the exact command line in a shell running successfully before putting it in your source code... – Philippe Oct 27 '22 at 14:38