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

问题描述 投票:-1回答:1

我创建了使用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个回答
0
投票

我发现我的错误,并愿意改正自己...

错误是在创建批处理文件,文件名和注释没有双引号括起来。它应该是如下:

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

希望这可以帮助别人。编码愉快:)

© www.soinside.com 2019 - 2024. All rights reserved.