17

git statusこれを与えると仮定します:

# On branch X
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   file1.cc
#   modified:   file1.h
#   modified:   file1_test.cc
#   modified:   SConscript
#
# 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)
#   (commit or discard the untracked or modified content in submodules)
#
#   modified:   file1.cc
#   modified:   tinyxml2 (untracked content)
#

この場合、file1.ccに加えられた変更の一部のみが、次のコミットのためにステージング/インデックス付けされています。

事前コミットスクリプトを実行して、スタイルチェッカーを実行します。

#!/bin/bash                                                                                                                                                                     

git stash -q --keep-index

# Do the checks                                                                                                                                                                 
RESULT=0
while read status file
do
    if python ~/python/cpplint.py "$file"; then
        let RESULT=1
    fi
done < <(git diff --cached --name-status --diff-filter=ACM | grep -P  '\.((cc)|(h)|(cpp)|(c))$' )

git stash pop -q

[ $RESULT -ne 0 ] && exit 1
exit 0

ここで提案されているように、スタイルチェックを実行する前にステージングされていないファイルを隠し、後でポップします。ただし、ファイル内の変更の一部のみがステージングされた場合、pre-commitフックの最後にスタッシュをポップすると、マージの競合が発生します。

これを行うためのより良い方法は何ですか?コミットしようとしているファイルのステージングされたバージョンに対してスタイルチェックを実行したいと思います。

4

3 に答える 3

10

git stashフックで自動的に使用することは避けたいと思います。git show ':filename'stashed ファイルの内容を取得するために使用できることがわかりました。
代わりに、次のアプローチを使用しました。

git diff --cached --name-only --diff-filter=ACMR | while read filename; do
    git show ":$filename" | GIT_VERIFY_FILENAME="$filename" verify_copyright \
        || exit $?
done \
    || exit $?
于 2013-07-04T16:56:51.570 に答える
5

次のように置き換えますgit stash -q --keep-index

git diff --full-index --binary > /tmp/stash.$$
git stash -q --keep-index

...そしてgit stash pop -q以下を使用:

git apply --whitespace=nowarn < /tmp/stash.$$` && git stash drop -q
rm /tmp/stash.$$

これにより、差分が一時ファイルに保存さgit applyれ、最後に使用して再適用されます。変更は引き続き stash に重複して保存されます (これは後で削除されます)。そのため、再適用で問題が発生した場合はgit stash show -p、一時ファイルを探すことなく を使用して調べることができます。

于 2012-12-09T08:19:47.843 に答える
2

スタッシュせずにコミットをゲートする方法

これを使用し.git/hooks/pre-commitて、atom 構文パッケージをチェックします

キービット

  1. git checkout-index -a --prefix={{temp_dir}}

スタッシングよりもはるかに遅くなる/そうではないかもしれませんが、インデックスをいじる自動化は本質的に脆いようです。.git/hooks/pre-commitおそらく、ソフト/ハードリンク ツリー、最小スペースの読み取り専用、一時的なインデックス チェックアウト、より優れた/高速な(または、たとえば) スクリプトを容易にする git contrib スクリプトが必要になる.git/hooks/pre-commit-indexため、作業中の完全な 2 番目のコピーが作成されます。 dir は必要ありません。working-dir->index が変更されるだけです。

#!/usr/bin/env ruby
require 'tmpdir'
autoload :FileUtils,  'fileutils'
autoload :Open3,      'open3'
autoload :Shellwords, 'shellwords'

# ---- setup

INTERACTIVE         = $stdout.tty? || $stderr.tty?
DOT                 = -'.'
BLOCK_SIZE          = 4096
TEMP_INDEX_DIR      = Dir.mktmpdir
TEMP_INDEX_DIR_REAL = File.realpath(TEMP_INDEX_DIR)

def cleanup
  FileUtils.remove_entry(TEMP_INDEX_DIR) if File.exist? TEMP_INDEX_DIR
end

at_exit { cleanup }

%w[INT TERM PIPE HUP QUIT].each do |sig|
  Signal.trap(sig) { cleanup }
end

# ---- functions

def fix_up_dir_output(data)
  data.gsub! TEMP_INDEX_DIR_REAL, DOT
  data.gsub! TEMP_INDEX_DIR, DOT
  data
end

def sh(*args)
  Open3.popen3(*args) do |_, stdout, stderr, w_thr|
    files = [stdout, stderr]
    until files.empty? do
      if ready = IO.select(files)
        ready[0].each do |f|
          begin
            data = f.read_nonblock BLOCK_SIZE
            data = fix_up_dir_output data
            if f.fileno == stderr.fileno
              $stderr.write data
            else
              $stdout.write data
            end
          rescue EOFError
            files.delete f
          end
        end
      end
    end
    if !(done = w_thr.value).success?
      exit(done.exitstatus)
    end
  end
end

def flags(args)
  skip = false
  r = []
  args.each do |a|
    if a[0] == '-' && !skip
      a.slice! 0
      if a[0] == '-'
        skip ||= a[1].nil?
        a.slice! 0
        r << a unless a.empty?
      else # -[^-]+
        r += a.split ''
      end
    end
  end
  r
end

def less_lint
  args = %w[lessc --lint]
  args << '--no-color' unless INTERACTIVE
  args << 'index.less'
  sh(*args)
end

def ensure_git_commit_signed
  pcmd = `ps -wwp#{Process.ppid} -ocommand=`.chop
  args = flags(Shellwords.split(pcmd)[1..-1])
  return unless (args & %w[gpg-sign S]).empty?
  $stderr.puts 'All git commits must be GPG-signed'
  $stderr.puts "    command: #{pcmd}"
  exit 1
end

# ---- main

# 1. make sure all commits are signed
ensure_git_commit_signed

# 2. check files that are in the index
sh 'git', 'checkout-index', '-a', "--prefix=#{TEMP_INDEX_DIR}/"
Dir.chdir TEMP_INDEX_DIR do
  # 3. make sure all commits contain only legal .less files
  less_lint
end
于 2016-10-21T01:35:21.107 に答える