programing

오류 없이 아무것도 커밋하지 않는 방법?

codeshow 2023. 10. 4. 23:04
반응형

오류 없이 아무것도 커밋하지 않는 방법?

저는 다음과 같은 작업을 수행하는 패브릭 스크립트를 작성하려고 합니다.git commit; 그러나 커밋할 것이 없으면 git은 다음 상태로 종료됩니다.1. 배포 스크립트는 이를 실패한 것으로 간주하고 종료합니다.실제 커밋 실패를 감지하고 싶으므로 패브릭을 포괄적으로 무시할 수 없습니다.git commit실패.빈 커밋 실패를 무시하여 배포는 계속할 수 있지만 실제 커밋이 실패할 때 발생하는 오류를 포착하려면 어떻게 해야 합니까?

def commit():
    local("git add -p && git commit")

다음의 종료 코드를 확인하여 이 상태를 미리 파악합니다.git diff-index?

예를 들어 (쉘에서):

git add -A
git diff-index --quiet HEAD || git commit -m 'bla'

편집: 고정git diffHolger의 말에 따라 명령을 내립니다.

From thegit commitman 페이지:

--allow-empty
    Usually recording a commit that has the exact same tree as its
    sole parent commit is a mistake, and the command prevents you
    from making such a commit. This option bypasses the safety, and
    is primarily for use by foreign SCM interface scripts.

토비 홀거의 답변을 명시적으로 확장하는 것입니다.if진술.

git add -A
if ! git diff-index --quiet HEAD; then
  git commit -m "Message here"
  git push origin main
fi

설명을 좀 해보죠.

  1. git add -A: 변경 사항 준비(다음 단계에 대한 required)

  2. git diff-index --quiet HEAD단계별 변경 내용을 헤드와 비교합니다.

    --quiet그것이 암시하듯이 중요합니다.--exit-code"차이가 있으면 코드 1로 프로그램 종료를 makes하고 0은 차이가 없다는 것을 의미합니다."

참고 --조용히 하세요.

with settings(warn_only=True):
  run('git commit ...')

이로 인해 패브릭이 장애를 무시하게 됩니다.빈 커밋을 만들지 않는 장점이 있습니다.

추가 레이어에 포장 가능합니다.with hide('warnings'):출력을 완전히 억제합니다. 그렇지 않으면 패브릭 출력에 커밋이 실패했다는 메모가 표시됩니다(그러나 fab 파일은 계속 실행됩니다).

포탄을 통과할 때, 당신은 그것을 사용할 수 있습니다.... || true실패가 예상되거나 무시됨을 선언하는 기법:

git commit -a -m "beautiful commit" || true

이를 통해 셸 스크립트를 사용할 때 셸 스크립트가 빠져나가는 것을 방지할 수 있습니다.errexit선택.

대신에... || true반환 코드가 0인 상태에서 종료되는 다른 명령어를 사용할 수도 있습니다. 예를 들어,

git commit -a -m "beautiful commit" || echo "ignore commit failure, proceed"

시도해 보세요/catch 베이비!

from fabric.api import local
from fabric.colors import green


def commit(message='updates'):
    try:
        local('git add .')
        local('git commit -m "' + message + '"')
        local('git push')
        print(green('Committed and pushed to git.', bold=False))
    except:
        print(green('Done committing, likely nothing new to commit.', bold=False))

언급URL : https://stackoverflow.com/questions/8123674/how-to-git-commit-nothing-without-an-error

반응형