2010-02-25 71 views
41

在試圖爲開發人員標準化平臺時,我的需求之一是提交.git/config,以便每個人都擁有相同的CRLF配置,而不會忘記手動設置它。使用代碼分發git配置

我該如何設置?

對於autocrlf,我有點擔心所有這些消極情緒。如果它不起作用,爲什麼不刪除此功能?要麼這個功能的製造者被誤解了,要麼他們做了一個失敗的實驗,它應該被刪除以阻止更多的人浪費他們的時間(閱讀不明確的手冊頁,提出問題,回答這些問題的人等)。

+2

參見http://stackoverflow.com/questions/2332349/best-practices-for-cross-platform-git-config:你可能有添加到這個類似問題的答案。 – VonC 2010-03-02 09:13:30

+2

謝謝,但我對所有這些對autocrlf的否定性有點擔心,爲什麼不刪除這個功能,如果它不工作?這個功能的製造者或者被誤解了,或者他們做了一個失敗的實驗,它應該被刪除,以阻止更多的人浪費他們的時間(閱讀不明確的手冊頁,提問,回答這些問題的人等)。 – nraynaud 2010-03-02 23:08:40

回答

67

我一直髮現autocrlf config屬性有問題。

注意:msysgit issue 538設置它爲true(這是由msysgit安裝程序設置的默認值),但我不相信。

我寧願爲以下三個解決方案之一:

  • 配置一個結束線型
  • 使得該配置傳播通過不同的Git回購

1.使用新的config setting core.eol(1.7.2+)

將行結束類型設置爲使用i n設置文本屬性的文件的工作目錄。
替代品是'lf','crlf'和'native',它使用平臺的本地行結尾。
默認值是本機。

2.a checkout/checking .gitattribute。 請參閱gitattributes手冊頁: crlfcore.autocrlf是在.gitattributes文件中記錄以前是本地配置屬性的方法。

3。一個git attribute filter driver可以:

  • 執行任何類型的格式標準,你可能要設置
  • 適用這些標準的某些文件/目錄
  • 被記錄爲一個配置文件(.gitattributes)能夠在任何地方推。
+0

感謝您的回答。我想我收到關於autocrlf的混淆信息。我讀過的地方git確實只在\ n模式下提交,它很好。我的一些開發人員正在使用Windows,而且由於世界上的每個人都可以讀取crlf,所以我認爲每個人的crlf都是最好的。但我們必須容納似乎只喜歡cr的git。你怎麼看 ? 我在審查屬性的想法,我排除之前,因爲我的理解是,它適用於每個文件的基礎上,而不是存儲庫範圍內的作品。也許我錯了。 – nraynaud 2010-03-01 11:11:55

+2

@narynaud:個人而言,我喜歡''\ n'',而且我不想改變關於eol的暗示。但是,如果你強制執行一種eol風格,'.gitattributes'真的很有趣,因爲你可以將它應用到所有倉庫,或者只應用到倉庫中的特定部分。 – VonC 2010-03-01 11:19:09

+0

如果我在項目根目錄中定義了 * + crlf ,我想我會用我的png圖片去下地獄? 我怎麼說「如果你認爲這是文字,然後做crlf,否則,做二元」。我想避免自己做出艱難的決定,系統擅長這種猜測和調試,錯誤會非常困難。 – nraynaud 2010-03-01 13:37:15

5

如果您使用的是Unix系列操作系統,我會建議您只創建一個符號鏈接。

ln -s .git/config git-config 
git add git-config 
git commit -m "Now tracking git config file" 
+0

這是一個聰明的的想法,我們混合Windows/Mac/Linux。但也許git可以使用Windows鏈接的東西,我會嘗試谷歌。 – nraynaud 2010-02-25 11:43:35

+0

它不起作用,在Unix上git甚至沒有看到配置文件的變化。 – nraynaud 2010-02-26 11:28:33

+2

那麼如果你在unix上它應該反過來工作 mv .git/config git-config; cd .git; ln -s ../git-config config; 012 ..,..,.. ..。 git add git-config; git commit -m「現在跟蹤git配置文件」; – 2010-03-19 21:05:41

0

可能是使用一個更好的方式hardlink

在Unix或者OS X系統:

ln .git/config git-config 
git add git-config 
git commit -m "Now tracking git config file" 

在Windows上的NTFS文件系統系統:

mklink /H git-config .git\config 
git add git-config 
git commit -m "Now tracking git config file" 

但我們必須記住,在克隆項目時應用設置來執行反向過程:

在Unix或者OS X系統:

git clone FROM_PROJ_URL 
rm .git/config 
ln git-config .git\config 

在Windows上的NTFS文件系統的系統:

git clone FROM_PROJ_URL 
del .git\config 
mklink /H .git\config git-config 
0

.git/config可以通過~/.gitconfig局部覆蓋。

因此,作爲構建的一部分,Makefile文件或提供腳本,可以提出用戶更改到他們的~/.gitconfig,或通過git config加載本地腳本.gitconfig

例如,創建一個新的.gitconfig一些設置,並通過加載:

git config --local include.path "/path/to/.gitconfig" 

或要求用戶在他們的~/.gitconfig這些行:

[include] 
    path = .gitconfig 

如果您使用放浪作爲代碼分發的一部分,你可以通過以下方式從Vagrantfile加載git config:

system('GIT_TRACE=1 git config --local include.path "$(git rev-parse --show-toplevel)/git/gitconfig"'); 

然後在git/gitconfig中提交你的git配置,所以每當用戶運行VM的配置時,這個文件就會自動加載到他們在主機上的git上。強制core.filemode被禁用,所以Windows將不會有任何文件權限問題)。


要強制行結尾的用戶,你應該使用.gitattributes代替它應該工作外的框。語法示例使用類Unix行結尾(LF):

# Drupal git normalization 
# @see https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html 
# @see https://www.drupal.org/node/1542048 

# Define text file attributes. 
# - Treat them as text. 
# - Ensure no CRLF line-endings, neither on checkout nor on checkin. 
# - Detect whitespace errors. 
# - Exposed by default in `git diff --color` on the CLI. 
# - Validate with `git diff --check`. 
# - Deny applying with `git apply --whitespace=error-all`. 
# - Fix automatically with `git apply --whitespace=fix`. 

*.css  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.html text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=html 
*.js  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.json text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 

# Auto-detect text files, ensure they use LF. 
*   text=auto eol=lf 

# Define binary file attributes. 
# - Do not treat them as text. 
# - Include binary diff in patches instead of "binary files differ." 
*.gz  -text diff