web-dev-qa-db-ja.com

GitBashシェルを開き、シェルでコマンドを実行するバッチファイルを作成するにはどうすればよいですか?

Windows 7でバッチファイルを使用してGitBashシェルを開き、git呼び出しを実行しようとしています。これは私のバッチファイルの内容です。

REM Open GitBash 
C:\Windows\SysWOW64\cmd.exe /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
REM retrieve archive
git archive master | tar -x -C %~1
REM quit GitBash
exit

次のコマンド「git archive ...」の前にGitBashがログアウトしていることに気付きました。コマンドをGitBashに渡すことができるかどうか、またその方法を知っている人はいますか?

マイク

41
mariachimike
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "git archive master | tar -x -C $0" "%~1"
50
Erik

シェルスクリプトを実行して複数のコマンドを実行することもできます

#! /bin/bash
cd /c/GitRepo/PythonScripts
git status
read -p "Press enter to continue"

次に、cmd行からそれを呼び出します。

"c:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "/c/GitRepo/PythonScripts/statusandwait.sh"
11
rugg

Windowsでgit.batファイルを作成し、それを.hook拡張子に関連付けました。

if not exist %1 exit
set bash=C:\Program Files (x86)\Git\bin\bash.exe
"%bash%" --login -i -c "exec "%1""

その後、すべての.batまたは.cmdファイルと同じように.hookファイルを実行できますが、それらはgit Shellで実行されます...

5
inf3rno

Bashを使用する方がより使いやすい

# file: backup.sh

cd /c/myProyectPath/
PWD=$(pwd);

function welcome() {
   echo "current Dir   : $PWD";
}

function backup() {
   git pull

   #if you have install wamp <http://www.wampserver.com>, we making slqBackup
   MYSQLDUMP="/c/wamp/bin/mysql/mysql5.6.12/bin/mysqldump.exe";
   $MYSQLDUMP --user=login --password=pass --no-create-info bd > data/backup.sql
   git add data/backup.sql;

   #generating tar file
   git archive -o latest.tar HEAD
}

welcome;
backup;

echo "see you";
sleep 30;

スクリプトを実行できます:

"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "/c/myProyectPath/run.sh"

':¬)、ハッピースクリプティング!

1
fitorec

多くの試行錯誤の後、私はこれを機能させました。 Git For Windows Portableの現在のバージョン。 Windowsコマンドウィンドウを開き、このスクリプトを実行します。作業ディレクトリに変更がある場合、作業ディレクトリでbashターミナルが開き、現在のgitステータスが表示されます。 exec bashを呼び出すことにより、bashウィンドウを開いたままにします。

複数のプロジェクトがある場合は、異なるプロジェクトフォルダーでこのスクリプトのコピーを作成し、メインバッチスクリプトから呼び出すことができます。

更新-新しく追加されたファイル、つまり追跡されていないファイルを確認します。

==============

REM check git status of given folders. 

setlocal EnableDelayedExpansion

set GIT_HOME=D:\Eclipse\devtools\PortableGit-2.6.2

set GIT_EXEC=%GIT_HOME%\mingw64\bin\git.exe
set GIT_BASH=%GIT_HOME%\bin\bash.exe

set GITREPO=D:\source\myproject

pushd %GITREPO%
%GIT_EXEC% status
%GIT_EXEC%  diff-index --quiet --cached HEAD
set VAR1=%errorlevel%
if not "%VAR1%"=="0" (
echo.
echo There are changes which are staged i.e. in index - but not committed.
echo.
)
%GIT_EXEC% diff-files --quiet
set VAR2=%errorlevel%
if not "%VAR2%"=="0" (
echo.
echo There are changes in working directory.
echo.
)

rem below for loop requires enabledDelayedExpansion
for /f "delims=" %%i in ('%GIT_EXEC% ls-files --others --exclude-standard') do (
    if "!VAR3!"=="" (set VAR3=%%i) else (set VAR3=!VAR3!#%%i)
)

if not "%VAR1%"=="0" set REQUIRECOMMIT=true
if not "%VAR2%"=="0" set REQUIRECOMMIT=true
if not "%VAR3%"=="" set REQUIRECOMMIT=true

if "%REQUIRECOMMIT%"=="true" (
    start "gitbash" %GIT_BASH% --login -i -c "git status; exec bash"
)

popd

endlocal
1

私の場合、特定のHTTPリクエストはWindowsのGit Bash内のcurlで機能します。ただし、HttpClientおよびHttpGet(org.Apache.http.client.methods.HttpGet)を使用してJavaで実行すると、接続リセットエラーが発生します。

Execを使用してコマンドを直接実行しようとした場合、何らかの理由で機能しません。

回避策として、このコードはコマンドをバッチファイルに書き込み、バッチファイルを実行して、出力をcommand.txtに配置します。

Command.batファイルに含める必要があるコマンドを次に示します(エンドポイントとパスワードを変更しました)。

"C:\Users\scottizu\AppData\Local\Programs\Git\bin\sh.exe" --login -i -c "curl 'https://my.server.com/validate/user/scottizu' -H 'Password: MY_PASSWORD' > command.txt"

コードは次のとおりです(コマンドに特殊文字がエスケープされていることに注意してください)。

import Java.io.BufferedWriter;
import Java.io.File;
import Java.io.FileWriter;

public class CURL_Runner {
    public static void main (String[] args) throws Exception {
        String command = "\"C:\\Users\\scottizu\\AppData\\Local\\Programs\\Git\\bin\\sh.exe\" --login -i -c \"curl 'https://my.server.com/validate/user/scottizu' -H 'Password: MY_PASSWORD' > command.txt\"";
        createAndExecuteBatchFile(command);
    }

    public static void createAndExecuteBatchFile(String command) throws Exception {

        // Step 1: Write command in command.bat
        File fileToUpload = new File("C:\\command.bat");
        try {
            if(fileToUpload.getParentFile() != null && !fileToUpload.exists()) {
                fileToUpload.getParentFile().mkdirs();
            }
            FileWriter fw = new FileWriter(fileToUpload);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(command);
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Step 2: Execute command.bat
        String[] cmdArray = new String[1];
        cmdArray[0] = "C:\\command.bat";

        Process process = Runtime.getRuntime().exec(cmdArray, null, new File("C:\\"));
        int processComplete = process.waitFor();
    }
}
0
Scott Izu