【问题标题】:Open git bash process without window打开没有窗口的git bash进程
【发布时间】:2016-08-10 18:04:53
【问题描述】:

作为一个有趣的项目,我尝试通过 C# 的 git-cmd.exe 进程运行多个命令。

我有这段代码可以运行多个命令:

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();

这运行正常,但是当我尝试运行以下命令时,我收到了几条消息。

命令(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}'

消息:

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

查看 BitBuckets API 文档,400 返回状态表示“如果输入文档无效,或者调用者没有在目标帐户下创建存储库的权限”。不幸的是,我找不到在 git-cmd.exe 中运行命令的正确格式。

该命令在 Git Bash 中运行良好,所以我尝试运行一个 Git bash 进程:

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();

但是,这会打开一个 Git Bash 窗口,并且不会执行我的命令。老实说,我希望该程序能做与 git-cmd.exe 进程完全相同的事情。因此,不是打开一个新窗口,而是在已经打开的窗口中执行所有命令,并显示输出。

所以,我的问题是:如何在我的 git-cmd.exe 进程中运行 CURL 命令才能成功运行?或者,我应该如何在我的程序窗口中运行 git-bash.exe 进程?

【问题讨论】:

    标签: c# git bash process


    【解决方案1】:

    将 ProcessStartInfo 上的 CreateNoWindow 属性设置为 true,它将显示为 git 在您自己的应用程序中运行。两种方式都会启动一个子进程,但将CreateNoWindow 设置为true 会阻止控制台窗口显示。

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 2014-09-04
      • 2014-02-12
      • 2018-01-21
      相关资源
      最近更新 更多