LoginSignup
0
1

More than 3 years have passed since last update.

gitのリポジトリをDropboxやGoogle Drive内に置きたくないとき

Last updated at Posted at 2020-09-13

ローカルマシンの特定の場所をgit置き場にするスクリプト

Dropbox内にgitリポジトリを置きたくないときの方法

 git init と引っ越し用スクリプト

  • git init --seprated-git-dir=XXXの簡略化
  • 既にgit初期化れている場合はコピーして、.gitファイルで参照する
git_init.bat
echo off
setlocal
setlocal enabledelayedexpansion

echo ----------------------
echo git initialize script
echo ----------------------
echo init in %CD%

rem ローカルgitまとめ場
set PATH_GIT_REPO=D:\git_repos
set PATH_WORK=%CD%
call :GET_FILENAME "%CD%"

set PATH_TARGET_GIT_REPO=%PATH_GIT_REPO%%TARGET_PATH%
echo git repository will put in "%PATH_TARGET_GIT_REPO%"
pause


rem git初期化されているか確認
echo checking git repository...
if exist .git\ ( 
   echo this directory has git repository
   set /P selected="replace git?(Y=YES / N=NO)?"
   if /i !selected!!==y ( 
      goto git_replace
   ) else (
      goto abort
   )
) else (
   if exist .git (
       echo this directory has separated git repository    
       goto abort
   ) else (
       echo this directory does NOT have a git repository
       set selected=y
       set /P selected="git init here?(Y=YES / N=NO)?"
       if /i !selected!!==y ( 
          goto git_init_separated
       ) else (
          goto abort
       )
   )
)

:git_init_separated
     echo git init separated in %PATH_TARGET_GIT_REPO%
     git init --separate-git-dir="%PATH_TARGET_GIT_REPO%"
     goto end

:git_init_here
     echo git init here
     git init
     goto end

:end
    echo finish
    pause
    exit /b 0

:abort
    echo script cancelled
    pause
    exit /b 0



rem 既存のリポジトリの引っ越し
:git_replace
echo replace git to "%PATH_TARGET_GIT_REPO%"
rem フォルダを作成
rem パスが存在するか調べてなかったらつくる
if exist %PATH_TARGET_GIT_REPO% (
    echo %PATH_TAGET_GIT_REPO%% already exist
    set /P selected="overwrite them?(Y=YES / N=NO)?"
    if /i !selected!!==y ( 
       goto git_copy
    ) else (
       goto abort
    )
) else (
    mkdir "%PATH_TARGET_GIT_REPO%"
    echo make directory "%PATH_TARGET_GIT_REPO%"
)

:git_copy
rem .git内部をコピー
echo copy .git to "%PATH_TARGET_GIT_REPO%"
xcopy /d /e /y "%PATH_WORK%\.git" "%PATH_TARGET_GIT_REPO%"
pause
rem .gitフォルダを削除
echo delete origin .git files
rmdir "%PATH_WORK%\.git" /s
rem \を/に変更
echo make a reference .git file
set GIT_DIR=%PATH_TARGET_GIT_REPO:\=/%
set GIT_DIR=%GIT_DIR:"=%
type nul > .git
echo gitdir: "%GIT_DIR%"> .git
goto end


rem サブルーチン

rem ドライブ名以下のパスを取得
:GET_FILENAME
set TARGET_PATH=%~pn1
exit /b 0

git clone用スクリプト

git_clone.bat

echo off
setlocal
setlocal enabledelayedexpansion

echo ----------------------
echo git clone script
echo ----------------------
echo git clone in %CD%

set GIT_URL=
set GIT_URL=%~1

if "%GIT_URL%"=="" ( 
   echo empty argument
   goto abort
) else ( 
   echo git url is %GIT_URL%%
) 


set GIT_NAME_PATH=%GIT_URL:/=\%
call :GET_GIT_NAME "%GIT_NAME_PATH%"

rem ローカルgitまとめ場
set PATH_GIT_REPO=D:\git_repos
set PATH_WORK=%CD%
call :GET_FILENAME "%CD%"

rem パスが存在するか調べて作りなおすか確認
rem git cloneはすでにディレクトリがあると失敗する
if exist %PATH_TARGET_GIT_REPO% (
    echo %PATH_TAGET_GIT_REPO%% already exist
    set /P selected="overwrite them?(Y=YES / N=NO)?"
    if /i !selected!!==y ( 
       rmdir /s /q "%PATH_TARGET_GIT_REPO%" 
       echo remove dir "%PATH_TARGET_GIT_REPO%"
       goto git_clone_separated
    ) 
) 

:git_clone_separated
     echo git clone separated in %PATH_TARGET_GIT_REPO%
     git clone %GIT_URL% --depth 1 --separate-git-dir="%PATH_TARGET_GIT_REPO%"
     goto end

:end
    echo finish
    pause
    exit /b 0

:abort
    echo script cancelled
    pause
    exit /b 0


rem サブルーチン

:GET_GIT_NAME
set GIT_NAME=%~n1
echo git name is %GIT_NAME%
exit /b 0

rem ドライブ名以下のパスを取得
:GET_FILENAME
set TARGET_PATH=%~pn1
set PATH_TARGET_GIT_REPO=%PATH_GIT_REPO%%TARGET_PATH%\%GIT_NAME%
echo git repository will put into "%PATH_TARGET_GIT_REPO%"
exit /b 0

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1