1

As a fun project I'm trying to run several commands through a git-cmd.exe process via C#.

I have this code to run multiple commands:

var info = new ProcessStartInfo(@"C:\Program Files\Git\git-cmd.exe");
info.RedirectStandardInput = true;
info.UseShellExecute = false;
var gitProcess = new Process { StartInfo = info };
gitProcess.Start();

using (StreamWriter sw = gitProcess.StandardInput)
{
    // commands here
}

gitProcess.WaitForExit();

This runs fine, but when I try to run the following command, I am getting several messages.

Command (https://stackoverflow.com/a/33656490/1671161):

curl -X POST -v -u username:password https://api.bitbucket.org/2.0/repositories/TeamName/reponame -H "Content-Type: application/json" -d '{"scm": "git", "is_private": true, "fork-policy": "no_public_forks", "has_issues": true}'

Messages:

Note: Unnecessary use of -X or --request, POST is already inferred.
* timeout on name lookup is not supported
*   Trying 2401:1d80:1010::153...
* Connected to api.bitbucket.org (2401:1d80:1010::153) port 443 (#0)

#more messages, seems OK to me so far, but then this:

Connection state changed (MAX_CONCURRENT_STREAMS updated)!
* We are completely uploaded and fine
* HTTP 1.0, assume close after body
< HTTP/2 400
< server: nginx/1.6.2
< vary: Authorization, Cookie
< content-type: text/plain
< strict-transport-security: max-age=31536000
< date: Wed, 10 Aug 2016 17:34:57 GMT
< x-served-by: app-112
< x-static-version: 89c5b48218a9
< etag: "825644f747baab2c00e420dbbc39e4b3"
< x-render-time: 0.0194010734558
< x-accepted-oauth-scopes: repository:admin
< x-version: 89c5b48218a9
< x-request-count: 459
< x-frame-options: SAMEORIGIN
< content-length: 11
<
Bad Request* Closing connection 0
* TLSv1.2 (OUT), TLS alert, Client hello (1):
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: git,/
* timeout on name lookup is not supported
* getaddrinfo(3) failed for git,:80
* Couldn't resolve host 'git,'
* Closing connection 1

Looking up BitBuckets API documentation, the 400 return state means "If the input document was invalid, or if the caller lacks the privilege to create repositories under the targeted account." Unfortunately, I couldn't find out the right format for the command to run in git-cmd.exe.

The command runs fine in Git Bash, so I tried to run a Git bash process:

var gitbashinfo = new ProcessStartInfo(@"C:\Program Files\Git\git-bash.exe");
gitbashinfo.RedirectStandardInput = true;
gitbashinfo.UseShellExecute = false;


var gitbashProcess = new Process { StartInfo = gitbashinfo };
gitbashProcess.Start();

using (StreamWriter sw = gitbashProcess.StandardInput)
{
    // The command
}   

gitbashProcess.WaitForExit();

But, this opens a Git Bash window, and does not execute my command. I honestly expected the program to do the exact same thing as I did with the git-cmd.exe process. Hence, not opening a new window, but execute all commands in the already opened window, and show the output as well.

So, my question is: how can I run the CURL command to run successfully in my git-cmd.exe process? OR, how should I run the git-bash.exe process inside my program window?

Community
  • 1
  • 1
FJPoort
  • 225
  • 2
  • 7
  • 16

2 Answers2

0

Set the CreateNoWindow property on the ProcessStartInfo to true and it will appear as git is running in your own application. Both ways will start a child process but with the CreateNoWindow set to true it will prevent the console window from showing.

Jetti
  • 2,418
  • 1
  • 17
  • 25
0

Another approach would be to use the new Git 2.42 (Q3 2023) headless-git.exe, to directly execute the command you want (instead of opening a bash session, which triggers a separate window).

See commit 0050f8e, commit 4b8a271 (09 Aug 2023) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 32f4fa8, 15 Aug 2023)

win32: add a helper to run git.exe without a foreground window

Helped-by: Carlo Marcelo Arenas Belón
Helped-by: Yuyi Wang
Signed-off-by: Johannes Schindelin
Signed-off-by: Derrick Stolee

On Windows, there are two kinds of executables, console ones and non-console ones.
Git's executables are all console ones.

When launching the former e.g. in a scheduled task, a CMD window pops up.
This is not what we want for the tasks installed via the git maintenance(man) command.

To work around this, let's introduce headless-git.exe, which is a non-console program that does not pop up any window.
All it does is to re-launch git.exe, suppressing that console window, passing through all command-line arguments as-are.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250