在ASP.Net Web应用程序中使用Git命令运行批处理文件

时间:2019-01-30 11:13:57

标签: c# asp.net git batch-file

我已经使用Web应用程序创建了一个批处理文件来提交并将我的文件推送到Git存储库中,如下所示:

git status
git pull
git add file.properties file.yaml file_metric.yaml
git commit -m Test Message
git push

我通过在Web应用程序中单击按钮来调用此批处理文件。

我的代码如下:

//** File is created, now call the Batch file.
                    // Get the full file path

                    string strFilePath = sPathName;
                    // Create the ProcessInfo object
                    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
                    psi.UseShellExecute = false;
                    psi.WorkingDirectory = Path.GetDirectoryName(strFilePath);
                    psi.RedirectStandardOutput = true;
                    psi.RedirectStandardInput = true;
                    psi.RedirectStandardError = true;
                    psi.WorkingDirectory = logFolderPath;

                    // Start the process
                    System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

                    // Open the batch file for reading
                    System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

                    // Attach the output for reading
                    System.IO.StreamReader sOut = proc.StandardOutput;

                    // Attach the in for writing
                    System.IO.StreamWriter sIn = proc.StandardInput;

                    // Write each line of the batch file to standard input
                    while (strm.Peek() != -1)
                    {
                        sIn.WriteLine(strm.ReadLine());
                    }

                    strm.Close();

                    // Exit CMD.EXE
                    string stEchoFmt = "# {0} run successfully. Exiting";


                    sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
                    //sIn.WriteLine("EXIT");


                    // Close the process
                    proc.Close();


                    // Read the sOut to a string.
                    string results = sOut.ReadToEnd().Trim();



                    // Close the io Streams;
                    sIn.Close(); 
                    sOut.Close();

我收到结果,但是结果不完整,文件没有提交或推送到存储库中。

我得到的输出如下:

Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation.
All rights reserved.

folderPath\Test>git status On branch master Your branch is up to date
with 'origin/master'.

Changes to be committed:   (use "git reset HEAD <file>..." to unstage)

    new file:   file.properties     new file:   file.yaml   new file:  
file_metric.yaml

Changes not staged for commit:   (use "git add <file>..." to update
what will be committed)   (use "git checkout -- <file>..." to discard
changes in working directory)

    modified:   Git5.bat

Untracked files:   (use "git add <file>..." to include in what will be
committed)

    Git_Bat.Bat     ../TestGit_Bat.Bat


folderPath\Test>git pull
Already up to date.

folderPath\Test>git add file.properties file.yaml file_metric.yaml

folderPath\Test>git commit -m YAML Files

folderPath\Test>git push

folderPath\Test># folderPath\Test/Git_Bat.Bat run successfully.
Exiting

folderPath\Test>

可以看出,在 Git.add Files Command 之后我没有任何输出。

当我尝试使用终端直接运行上述批处理文件时,它成功运行,并且文件被正确推送。

通过“代码”调用时是否需要任何更改?

1 个答案:

答案 0 :(得分:0)

我发现了我的错误,并想自行纠正...

错误是在创建批处理文件时出现的,文件名和注释未用双引号引起来。应该如下:

git status
git pull
git add "file.properties" "file.yaml" "file_metric.yaml"
git commit -m "Test Message"
git push

希望这对其他人有帮助。 快乐编码:)