2

YUI Compressorを使用して、コミット用にステージングされたCSSファイルとJavaScriptファイルを縮小するpre-commitフックに取り組んでいます。ファイルが縮小された後、縮小されたバージョンは自動的にコミットのためにステージングされます。マシンで生成されたファイルをコミットに自動的に追加することは一般的には良い考えではないことを読みましたが、この場合は問題ないと思います。これはどのように見えるかです:

の出力git status

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   _site-wide.css
#
# 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:   _subpage.css
#

の出力git commit -m "Updated site-wide styling"

1 CSS file was minified and added to the Git repository
0 JavaScript files were minified and added to the Git repository
[master 41f1815] Updated site-wide styling
 2 files changed, 2 insertions(+), 2 deletions(-)

ここで起こったことは、YUI Compressorを使用して縮小_site-wide.cssし、結果を出力するpre-commitフックsite-wide.cssです(先頭にアンダースコアはありません)。site-wide.css次に、コミットのためにステージングしました。pre-commitフックは_subpage.css、変更されていてもコミット用にステージングされていなかったため、スキップされました。

ディスク上のCSSファイルとJavaScriptファイルは、コミット用にステージングされたCSSファイルとJavaScriptファイルと同じではない可能性があるためgit stash -q --keep-index、ファイルを縮小する前に実行してから実行git stash pop -qします。このpre-commitフックは、すでにコミットされているリポジトリで正常に機能しますが、最初のコミットが行われる前にpre-commitフックを配置すると、次のようになります。

の出力git status

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   _site-wide.css
#   new file:   _subpage.css
#

の出力git commit -m "Initial commit"

fatal: bad revision 'HEAD'
fatal: bad revision 'HEAD'
fatal: Needed a single revision
You do not have the initial commit yet
2 CSS files were minified and added to the Git repository
0 JavaScript files were minified and added to the Git repository
No stash found.

の出力git status

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   _site-wide.css
#   new file:   _subpage.css
#   new file:   site-wide.css
#   new file:   subpage.css
#

次に、pre-commitフックのコードを貼り付けます。柔軟性を持たせるために、Gitリポジトリを持つプロジェクトだけでなく、任意のCakePHPプロジェクトで実行できるようにこのスクリプトを作成したことを覚えておいてください。また、コミット用にステージングされたファイルだけでなく、すべてのCSSファイルとJavaScriptファイルを強制的に縮小できるように作成しました。これは、を実行することによって実行されます.git/hooks/pre-commit force。コードは次のとおりです。

#!/bin/bash

css_files_to_ignore=(
    #"_do_not_minify.css"
)

js_files_to_ignore=(
    #"_do_not_minify.js"
)

if git rev-parse --git-dir > /dev/null 2>&1; then
    git_repository=true
    base_folder="$(git rev-parse --show-toplevel)/app/webroot"

    if [ "$1" == "force" ]; then
        process_unstaged_files=true
    else
        process_unstaged_files=false
    fi
else
    git_repository=false
    base_folder="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/webroot"
fi

if [ -f /Applications/yuicompressor.jar ]; then
    # Mac

    yuicompressor_path=/Applications/yuicompressor.jar
else
    # Linux

    yuicompressor_path=$(command -v yui-compressor)
fi

function process_assets()
{
    extension=$1
    files_minified=0

    for infile in $(echo "$base_folder/$extension/*.$extension")
    do
        # Only process files.

        [[ -f $infile ]] || continue

        filename=${infile##*/}

        # If the filename starts with an underscore, that means that the file is
        # eligible for minification.

        [[ ${filename:0:1} == "_" ]] || continue

        ignore_this_file=false

        files_to_ignore=$extension"_files_to_ignore"

        for i in $(eval echo \${$files_to_ignore[@]})
        do
            if [[ $i == $filename ]]; then
                ignore_this_file=true
                break
            fi
        done

        if [ $git_repository == true ] && [ $process_unstaged_files == false ] && git diff --quiet --cached $infile; then
            # This file is NOT staged for commit.

            ignore_this_file=true
        fi

        if [ $ignore_this_file == false ]; then
            minified_file="$base_folder/$extension/${filename:1}"

            if [ ! -f "$minified_file" ] || test $infile -nt $minified_file; then
                $yuicompressor_command "$infile" -o "$minified_file"

                if [ $git_repository == true ] && [ $process_unstaged_files == false ]; then
                    git add "$minified_file"
                fi

                ((files_minified++))
            fi
        fi
    done

    # Output a summary of what was done.

    if [ $extension == "css" ]; then
        file_type="CSS"
    else
        file_type="JavaScript"
    fi

    echo -n "$files_minified $file_type file"

    if [ $files_minified -eq 1 ]; then
        echo -n " was"
    else
        echo -n "s were"
    fi

    echo -n " minified"

    if [ $git_repository == true ] && [ $process_unstaged_files == false ]; then
        echo " and added to the Git repository"
    else
        echo
    fi
}

if [ -f "$yuicompressor_path" ]; then
    if [ ${yuicompressor_path: -4} == ".jar" ]; then
        yuicompressor_command="java -jar $yuicompressor_path"
    else
        yuicompressor_command=$yuicompressor_path
    fi

    if [ $git_repository == true ] && [ $process_unstaged_files == false ] && ! git diff --quiet --cached; then
        # The staging area is what should be processed rather than what is currently
        # on disk.

        git stash -q --keep-index

        stashed=true
    else
        stashed=false
    fi

    process_assets css
    process_assets js

    if [ $stashed == true ]; then
        git stash pop -q
    fi
else
    echo "YUI Compressor was not found. Aborting."
    exit 1
fi

どうすればこれを機能させることができますか?どんな助けでもいただければ幸いです。

4

2 に答える 2

1

一部のGitコマンドは、HEADがないと機能しないため、これに対する解決策はないという結論に達しました。また、もう少し考えてみたところ、少なくとも私にとっては、これは一種のエッジケースであると判断しました。最初のコミットは通常、使用するフレームワークのファイルなどの基本的なものだからです。したがって、最初のコミット後にフックを実装できます。

Hassekの時間だけでなく、これを読んだ他の人の時間にも感謝します。ありがとうございました!

于 2012-07-17T16:30:33.003 に答える
1

Git 2.22(2019年第2四半期、7年後)にはgit stash、Cで書き直されたものがあります。

Paul-Sebastian Ungureanu()によるcommit 1ac528c(2019年2月25日)を参照してください。 weekly-digest[bot]

stashpush -q静かにする

このコミットで動作に変更があります。
最初のコミットがなかった場合でも、シェルバージョンのstashはメッセージを表示していました。
このコミットにより、またはが指定されているpush場合はメッセージが表示されなくなります。--quiet-q

したがって、Git 2.22の新しいrewritten-in-Cコマンドから始めて、 「 You do not have the initial commit yet」が表示されないようにする必要があります。git stash -q <command>stash

于 2019-04-22T16:09:05.060 に答える