IT story

Git에서 현재 커밋 해시를 같은 커밋의 파일에 쓰는 방법

hot-time 2020. 7. 19. 09:13
반응형

Git에서 현재 커밋 해시를 같은 커밋의 파일에 쓰는 방법


Git hooks로 멋진 일을하려고하지만 실제로 어떻게 해야하는지 (또는 가능한지) 모르겠습니다.

내가해야 할 일은 : 모든 커밋마다 해시를 가져 와서 커밋의 파일을이 해시로 업데이트하고 싶습니다.

어떤 아이디어?


SHA1을 추적되지 않은 파일 에 배치 하고 빌드 / 설치 / 배포 프로세스의 일부로 생성 된 것과 비슷한 작업을 수행하는 것이 좋습니다 . 분명히 ( git rev-parse HEAD > filename또는 아마도 git describe [--tags] > filename) 쉽고 , git의 추적과 다른 파일로 끝나는 것과 같은 미친 짓을 피합니다.

그러면 코드에서 버전 번호가 필요할 때이 파일을 참조하거나 빌드 프로세스에서 정보를 최종 제품에 통합 할 수 있습니다. 후자는 실제로 git 자체가 버전 번호를 얻는 방법입니다. 빌드 프로세스는 저장소에서 버전 번호를 가져온 다음 실행 파일에 빌드합니다.


누군가가 ident에 대한 "man gitattributes"섹션을 지적했습니다.

신분

속성 ident가 경로로 설정되면, git은 blob 객체의 $ Id $를 $ Id :로 대체하고, 그 뒤에 40 자 16 진 blob 객체 이름, 체크 아웃시 $ 기호 $가 뒤 따릅니다. 작업 트리 파일에서 $ Id :로 시작하고 $로 끝나는 모든 바이트 시퀀스는 체크인시 $ Id $로 바뀝니다.

당신이 그것에 대해 생각한다면, 이것은 CVS, Subversion 등이하는 일입니다. 저장소를 보면 저장소의 파일에 항상 $ Id $가 포함되어 있음을 알 수 있습니다. 그것은 그것의 확장을 포함하지 않습니다. 텍스트가 펼쳐지는 것은 체크 아웃시에만 가능합니다.


현재 커밋 해시를 작성하는 것은 불가능합니다. 향후 커밋 해시를 미리 계산하면 파일을 수정하자마자 변경됩니다.

그러나 세 가지 옵션이 있습니다.

  1. 스크립트를 사용하여 '커밋 ID'를 늘리고 어딘가에 포함하십시오. 추한
  2. 해시를 저장할 파일을 무시하십시오. 별로 편리하지 않다
  3. 에서 pre-commit, 가게 이전 해시를 커밋 : 당신은 너무,이 의지 작업, 99.99 %의 경우에 커밋 삽입 / 수정하지 마십시오. 최악의 경우 여전히 소스 개정을 식별 할 수 있습니다.

나는 후크 스크립트를 작성 중이며 여기에 '완료되면'게시하지만 Duke Nukem Forever 이전에 출시되었습니다 :))

UPD : 코드 .git/hooks/pre-commit:

#!/usr/bin/env bash
set -e

#=== 'prev-commit' solution by o_O Tync
#commit_hash=$(git rev-parse --verify HEAD)
commit=$(git log -1 --pretty="%H%n%ci") # hash \n date
commit_hash=$(echo "$commit" | head -1)
commit_date=$(echo "$commit" | head -2 | tail -1) # 2010-12-28 05:16:23 +0300

branch_name=$(git symbolic-ref -q HEAD) # http://stackoverflow.com/questions/1593051/#1593487
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD} # 'HEAD' indicates detached HEAD situation

# Write it
echo -e "prev_commit='$commit_hash'\ndate='$commit_date'\nbranch='$branch'\n" > gitcommit.py

이제 필요한 것은 prev_commit,branch페어를 실제 커밋 해시 로 변환하는 도구입니다. :)

이 방법으로 커밋을 병합 할 수 있는지 여부를 모르겠습니다. 곧 확인하겠습니다


gitattributesfilter속성 을 사용하면 됩니다. 당신은 제공해야 할 것 삽입이 ID를 커밋 명령 및 삭제합니다, 그것은 삽입있어 파일이 단지 때문에 변경하지 않을 것이라고 등이 ID를 커밋 명령을 사용합니다.smudgeclean

따라서 커밋 ID는 파일의 Blob에 저장되지 않습니다. 작업 복사본에서 확장되었습니다. 실제로 커밋 ID를 Blob에 삽입하면 무한 재귀 작업이됩니다. ☺)이 트리를 복제하는 사람은 자신을 위해 속성을 설정해야합니다.


커밋 박스 밖에서 생각하십시오!

이것을 파일 후크 / 체크 아웃에 넣습니다.

#!/bin/sh
git describe --all --long > config/git-commit-version.txt

버전은 사용하는 모든 곳에서 사용할 수 있습니다.


커밋의 파일이 변경되면 커밋의 해시도 변경되기 때문에 실제로 그렇게하고 싶지는 않습니다.


git internals를 사용하여 이것이 왜 어려운 문제인지 살펴 보겠습니다. 현재 커밋의 sha1을 다음과 같이 얻을 수 있습니다.

#!/bin/bash
commit=$(git cat-file commit HEAD) #
sha1=($((printf "commit %s\0" $(echo "$commit" | wc -c); echo "$commit") | sha1sum))
echo ${sha1[0]}

Essentially you run a sha1 checksum on the message returned by git cat-file commit HEAD. Two things immediately jump out as a problem when you examine this message. One is the tree sha1 and the second is the commit time.

Now the commit time is easily taken care of by altering the message and guessing how long it takes to make a commit or scheduling to commit at a specific time. The true issue is the tree sha1, which you can get from git ls-tree $(git write-tree) | git mktree. Essentially you are doing a sha1 checksum on the message from ls-tree, which is a list of all the files and their sha1 checksum.

Therefore your commit sha1 checksum depends on your tree sha1 checksum, which directly depends on the files sha1 checksum, which completes the circle and depends on the commit sha1. Thus you have a circular problem with techniques available to myself.

With less secure checksums, it has been shown possible to write the checksum of the file into the file itself through brute force; however, I do not know of any work that accomplished that task with sha1. This is not impossible, but next to impossible with our current understanding (but who knows maybe in a couple years it will be trivial). However, still this is even harder to brute force since you have to write the (commit) checksum of a (tree) checksum of a (blob) checksum into the file.

참고URL : https://stackoverflow.com/questions/3442874/in-git-how-can-i-write-the-current-commit-hash-to-a-file-in-the-same-commit

반응형