【问题标题】:Run Batch file with Git Commands in ASP.Net Web Application在 ASP.Net Web 应用程序中使用 Git 命令运行批处理文件
【发布时间】:2019-01-30 11:13:56
【问题描述】:

我使用 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之后没有得到任何输出。

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

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

【问题讨论】:

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


    【解决方案1】:

    我发现了我的错误并想自己纠正它...

    错误是在创建批处理文件时,文件名和 cmets 没有用双引号引起来。应该是这样的:

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

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

    【讨论】:

      猜你喜欢
      • 2018-09-08
      • 2021-07-15
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 2020-03-23
      相关资源
      最近更新 更多