developer tip

자식이 특정 줄을 무시할 수 있습니까?

optionbox 2020. 8. 27. 07:42
반응형

자식이 특정 줄을 무시할 수 있습니까?


전화기의 기본 브라우저에서 테스트하는 동안 git을 사용하여 phonegap에 동기화하고 있습니다. 따라서 다음 줄이 있습니다.

var isPhoneGap = false;

분명히 빌드 할 때 이것을 변경하지만,이 한 줄을 무시하도록 git을 설정할 수있는 방법이 있습니까? 아니면 가서 자체 파일에 넣고 그런 식으로 무시해야합니까?

OSX 10.6에서 Gitx와 터미널을 사용하고 있습니다.


파일이 특정 유형 인 경우 파일에서 선언 할 수있는 콘텐츠 필터 드라이버 를 선언 할 수 있습니다 .gitattributes( " Git 속성 " 의 "키워드 확장"에 나와 있음 ).

http://git-scm.com/figures/18333fig0702-tn.png

*.yourType filter=yourFilterName

( 원하는 경우 특정 파일에 대해 해당 필터를 설정할 수도 있습니다 )

도구:

  • yourFilterName.smudge(에서 트리거 됨 git checkout) 및

    git config --global filter.yourFilterName.smudge 'sed "s/isPhoneGap = .*/isPhoneGap = true/"'
    
  • yourFilterName.clean(에서 트리거 됨 git add)

    git config --global filter.yourFilterName.clean 'sed "s/isPhoneGap = .*/isPhoneGap = false/"'
    

파일은에서 변경되지 않은 상태로 표시 git status되지만 체크 아웃 된 버전은에 대해 올바른 값을가 isPhoneGap집니다.


당신이 사용할 수있는

git update-index --assume-unchanged [file]

추적하지 않으려는 한 파일의 변경 사항을 무시합니다. 저장소에 파일이 있어야 할 때이 솔루션을 사용하지만이 파일에는 항상 추적 할 필요가없는 일부 변경 사항이 있습니다.

파일에 중요한 변경 사항이있는 경우 다음을 수행해야합니다.

git update-index --no-assume-unchanged [file]

또한 매개 변수에 대한 Git 문서 업데이트 색인참조하십시오 --[no-]assume-unchanged.

이러한 플래그를 지정하면 경로에 대해 기록 된 개체 이름이 업데이트되지 않습니다. 대신 이러한 옵션은 경로에 대해 "변경되지 않은 것으로 가정"비트를 설정 및 설정 해제합니다. "변경되지 않은 것으로 가정"비트가 켜져 있으면 git은 작업 트리 파일에서 가능한 수정을 확인하는 것을 중지하므로 작업 트리 파일을 변경할 때 git에 알리기 위해 비트를 수동으로 설정 해제해야합니다.


Gitx는 개별 라인을 커밋하거나 무시하도록해야하지만 (이미 알고있을 수도 있습니다) 커밋 할 때마다 그렇게해야합니다. 배포 대상 당 구성 파일 (버전 지정 가능)과 서버를 시작하는 런타임 매개 변수 (예 :)를 갖는 것이 더 낫다고 생각합니다 ./myserver --config=whatever.js.


계속해서 https://stackoverflow.com/a/20574486/4935114 , @Mike무시하고 싶은 줄에 대해 준비된 파일에 pre-commit후크 를 만들 것을 제안했습니다 grep. 후크는 해당 라인이 준비되었는지 확인합니다. 그렇다면 echo경고이며 exit코드가 1있으므로 커밋 프로세스가 계속되지 않습니다.

@Mike 의 대답에 영감을 받아 , 나는 우리가 무시하고 싶은 특정 줄을 자동으로 reset ( -p플래그 와 함께 ) s ( 플래그 와 함께 ) 하는 개선 된 버전의 후크를 사용하고 있음을 발견했습니다 .

이 훅이 무시할 파일이 많은 상황에서 작동하는지 확실하지 않지만이 pre-commit훅은 특정 파일에서이 행의 변경 사항을 찾습니다 buildVars.java. 내 컴퓨터에서 테스트했을 때 후크 스크립트는 다음과 같이 보입니다.

#!/bin/sh

# this hook looks for lines with the text `var isPhoneGap = false;` in the file `buildVars.java` and it resets these lines to the previous state before staged with `reset -p`

if [[ $(git diff --no-ext-diff --cached buildVars.java | grep --count -e "var\ isPhoneGap[\ ]*=[\ ]*") -ne 0 ]]; then
    cat <<EOW
WARNING: You are attempting to commit changes which are not supposed to be commited according to this \`pre-commit\` hook
This \`pre-commit\` hook will reset all the files containing this line to it's previous state in the last commit.
EOW
    echo /$'\n'isPhoneGap$'\n'y$'\n'q | git reset -p
    # BONUS: Check if after reseting, there is no actual changes to be commited and if so, exit 1 so the commit process will abort.
    if [[ $(git diff --no-ext-diff --cached | wc -l) -eq 0 ]]; then
        echo there are no actual changes to be commited and besides the change to the variable \'isPhoneGap\' so I won\'t commit.
        exit 1
    fi
fi

설명

내가 한 일은 isPhoneGap대화식 reset프로세스 중에 정규식을 검색하는 제어 시퀀스를 에코하는 것입니다. 따라서 프레스 사용자 에뮬레이션 /을 검색 할 isPhoneGap프레스, y그는이 패치를 폐기하고 싶어 마지막으로 누를 때 물었을 때 q대화를 종료를 reset.

대화식 역 패치 프로세스는 https://git-scm.com/docs/git-add#git-add-patch에 문서화되어 있습니다.


NOTE: The above script assuming that the variable interactive.singleKey is false. If you configured yours to true, remove any $'\n' from the echo command right after the warning.


This is how you can kind of do it with git filters:

  1. Create/Open gitattributes file:
    • <project root>/.gitattributes (will be committed into repo)
      OR
    • <project root>/.git/info/attributes (won't be committed into repo)
  2. Add a line defining the files to be filtered:
    • *.rb filter=gitignore, i.e. run filter named gitignore on all *.rb files
  3. Define the gitignore filter in your gitconfig:
    • $ git config --global filter.gitignore.clean "sed '/#gitignore$/'d", i.e. delete these lines
    • $ git config --global filter.gitignore.smudge cat, i.e. do nothing when pulling file from repo

Notes:
Of course, this is for ruby files, applied when a line ends with #gitignore, applied globally in ~/.gitconfig. Modify this however you need for your purposes.

Warning!!
This leaves your working file different from the repo (of course). Any checking out or rebasing will mean these lines will be lost! This trick may seem useless since these lines are repeatedly lost on check out, rebase, or pull, but I've a specific use case in order to make use of it.

Just git stash save "proj1-debug" while the filter is inactive (just temporarily disable it in gitconfig or something). This way, my debug code can always be git stash apply'd to my code at any time without fear of these lines ever being accidentally committed.

I have a possible idea for dealing with these problems, but I'll try implementing it some other time.

Thanks to Rudi and jw013 for mentioning git filters and gitattributes.


A content filter driver is not a good solution. You may be able to hide this line from git status/etc but it's not really being ignored. As soon as you change the value, your working directory will be marked dirty even though the change may not be visible.

If you really want this line out of version control, changing it to a command line argument or placing it in an ignored include or build file may be the only practical means.


I'm guessing this might be something that comes up in more than one line of your source.

I think it would be cleanest to have a .userbuildconfig file of some sort that you just include and have the defaults checked in. Then you can use Carlos's suggestion to mark that file alone as assume unchanged. This way other changes to the file where you need to check the setting are not missed.

This can allow you to tune preprocessor macros locally (Or for java something like this https://stackoverflow.com/a/1813873/1270965).


Nope. You can only ignore individual files (and more) since the lines in .gitignore matches file names and not file content. You have already mentioned the solution to this, i.e. ignore a single file that contains that content you want to ignore.

참고URL : https://stackoverflow.com/questions/6557467/can-git-ignore-a-specific-line

반응형