<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by 丁沛堯 on Medium]]></title>
        <description><![CDATA[Stories by 丁沛堯 on Medium]]></description>
        <link>https://medium.com/@momoyo0717?source=rss-ab3f75bb637c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3-Z2eJqetUWBXvqS35WwKw.jpeg</url>
            <title>Stories by 丁沛堯 on Medium</title>
            <link>https://medium.com/@momoyo0717?source=rss-ab3f75bb637c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 11:11:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@momoyo0717/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[透過 Git Keyword Expansion 來讓 Git 過濾敏感字詞]]></title>
            <link>https://medium.com/ichef/%E9%80%8F%E9%81%8E-git-keyword-expansion-%E4%BE%86%E8%AE%93-git-%E9%81%8E%E6%BF%BE%E6%95%8F%E6%84%9F%E5%AD%97%E8%A9%9E-80e98af0d7b2?source=rss-ab3f75bb637c------2</link>
            <guid isPermaLink="false">https://medium.com/p/80e98af0d7b2</guid>
            <category><![CDATA[keyword-expansion]]></category>
            <category><![CDATA[sensitivity]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[filters]]></category>
            <dc:creator><![CDATA[丁沛堯]]></dc:creator>
            <pubDate>Tue, 27 Aug 2019 07:30:36 GMT</pubDate>
            <atom:updated>2019-08-27T07:32:11.133Z</atom:updated>
            <content:encoded><![CDATA[<p>大家在平常開發工作時，應該常常會遇到程式碼一些敏感字詞是不想被 commit 進 Git 的，比方說 database 的帳號密碼，或是一些 IDE 自動生成的字詞，比方說 XCode 的 .xcscheme 等。這時候有的人就會說『那就手動把它 unstage 就好啦』，但同一件事重複做好幾年實在不符合懶人的個性，所以這時候 <strong>Git Keyword Expansion</strong> 就派上用場啦！</p><h3>Git Keyword Expansion</h3><p>關於 Keyword Expansion ， <a href="https://git-scm.com/book/zh-tw/v1/Git-%E5%AE%A2%E8%A3%BD%E5%8C%96-Git-%E5%B1%AC%E6%80%A7">Git 中文網</a> 是這麼解釋的</p><blockquote>使用 SVN 或 CVS 的開發人員經常要求關鍵字擴展。這在 Git 中主要的問題是，你無法在一個檔案被提交後再修改它，因為 Git 會先對該檔計算 checksum。然而，你可以在檔案 check out 之後注入(inject)一些文字，然後在提交前再把它移除。</blockquote><p>聽起來很美好，對吧？接下來我們透過一個實際的例子來展示如何設定 Keyword Expansion 以及它如何實際作用在我們的 repository 中。</p><p>現在，我有一個 python 寫的 connect_db.py ，如下</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d26b78557ba7eb5cccb06c80208fd01/href">https://medium.com/media/3d26b78557ba7eb5cccb06c80208fd01/href</a></iframe><p>可以看到這段 script 裡面充滿了各種的敏感資訊，我的目標是可以讓 db 的連線資訊全部被隱藏起來不要 commit 進 git 。git 提供兩種方向的過濾設定，分別為 <strong>clean</strong> 與 <strong>smudge </strong>（如下圖）。在 .gitattributes 中，你能對特定的檔案或路徑設定過濾程式，然後在 .git/config 中設定處理這些檔案的腳本，這些腳本會在 checkout 前 （<strong>smudge</strong>）與 add 前（<strong>clean</strong>）被執行。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*EmZkyR4hOjGgZlaHBMeuIw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*XcXuKdzPD0gX3HrVW9HonA.png" /></figure><p>接下來我們就一步一步的來設定 git filter 達成我們的目的。</p><h4>設定 .gitattributes ：</h4><p>首先我們打開 cmd ，進到專案的資料夾底下，新增一個 .gitattributes （如果你本來沒有的話），並且在.gitattributes 內加上</p><pre>connect_db.py filter=sensitive_filter</pre><p>這段的意思是在告訴 git 說『如果遇到 connect_db.py 這個檔案，幫我套用 sensitive_filter 這個 filter 。』當然你也可以寫成 *.py 讓 git 幫你把過濾器套用到所有的 .py 檔。這邊只有單純地告訴 .gitattributes 說有個叫 sensitive_filter 的過濾器，至於這個過濾器的實作細節則必須在 .git/config 內設定。</p><h4>設定 .git/config：</h4><p>接下來一樣在 cmd 輸入</p><pre>git config filter.sensitive_filter.clean &quot;python remove_sensitive_string.py&quot;</pre><p>這段是在告訴 git 說『我這邊有一個叫 sensitive_filter 的過濾器，然後在這個過濾器的 clean 階段請幫我執行 python remove_sensitive_string.py 這個指令』。當你設定完後，在接下來只要執行 git add 的指令時，git 就會將被 add 的檔案傳送給remove_sensitive_string.py並執行我們撰寫的過濾腳本。</p><h4>撰寫過濾腳本：</h4><p>接下來來撰寫過濾腳本。新增remove_sensitive_string.py ，並輸入以下內容</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f22fe7ec8567ce07f8278e2efd4f8af9/href">https://medium.com/media/f22fe7ec8567ce07f8278e2efd4f8af9/href</a></iframe><p>這段程式讓 git filter 透過 sys.stdin 輸入你的程式碼，並且將敏感字詞取代為空字串，最後透過 sys.stdout.write 來輸出到 git staging area。</p><p>完成後存檔，接著你可以試試看在 cmd 輸入 git diff ，就會發現 db 連線資訊全部都變成空字串，但你的 source code 依然帶有這些 db 資訊，這樣就可以安心地進行開發工作，而不用擔心一不注意就把 db 資訊暴露到 git server 上了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/644/1*1TcGghqjwFghuQjvkzVjTg.png" /><figcaption>輸入 git diff 時會發現 db 連線資訊都不見了</figcaption></figure><h3>結語</h3><p><strong>Git Keyword Expansion</strong> 除了可以避免將程式傳到 staging area 上，也可以反過來透過設定 <strong>smudge </strong>來避免 git 上的資訊傳回自己本地端的 working directory，讓自己的工作資料夾保持乾淨(?)。但這類型的指令使用上一定要謹慎，需要與團隊充分溝通後再進行設定，否則就會發生『咦我明明改了程式碼但 git 上卻沒有』之類的慘劇。</p><h3>參考資料</h3><p><a href="https://git-scm.com/book/zh-tw/v1/Git-%E5%AE%A2%E8%A3%BD%E5%8C%96-Git-%E5%B1%AC%E6%80%A7">Git - Git 屬性</a></p><p><a href="https://pascalbugnion.net/blog/ipython-notebooks-and-git.html">https://pascalbugnion.net/blog/ipython-notebooks-and-git.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80e98af0d7b2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ichef/%E9%80%8F%E9%81%8E-git-keyword-expansion-%E4%BE%86%E8%AE%93-git-%E9%81%8E%E6%BF%BE%E6%95%8F%E6%84%9F%E5%AD%97%E8%A9%9E-80e98af0d7b2">透過 Git Keyword Expansion 來讓 Git 過濾敏感字詞</a> was originally published in <a href="https://medium.com/ichef">iCHEF</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[XCode Build 超加速]]></title>
            <link>https://medium.com/ichef/xcode-build-%E8%B6%85%E5%8A%A0%E9%80%9F-ccc505445dc9?source=rss-ab3f75bb637c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ccc505445dc9</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[丁沛堯]]></dc:creator>
            <pubDate>Wed, 16 Jan 2019 14:01:01 GMT</pubDate>
            <atom:updated>2019-01-16T14:01:01.090Z</atom:updated>
            <content:encoded><![CDATA[<h3>Xcode Build 超加速</h3><h3>前言</h3><p>相信所有的 iOS Developer 一定都有類似的經驗，每天的生活被等 Xcode rebuild 佔去了大部分的時間。以 iCHEF 為例，即使用最新的第八代 6 核心的 i7 每次 rebuild 也需要等約 50 秒左右，更別提我用自己的 2014 年 MBP ，每次 rebuild 大概都要等 5 分鐘左右。假設我每天 Build 10 次 Code ，一個月工作 22 天，一年就會浪費整整 10 個小時在等 Xcode Build ，這種時間浪費實在是讓人很不能忍啊！所以我就開始著手改善這個狀況。</p><h3>成果</h3><p>最後在東試試西調調以後，我主要做了以下的調整：</p><ul><li>關閉 Compile Optimization</li><li>關閉 dSYM</li><li>把 Framework 從 CocoaPods 移到 Carthage 管理</li></ul><p>而經過這樣的調整後，得到了以下的成果：</p><p>```<br>Macbook Air ：340 s -&gt; 150 s<br>Mac Mini 2012 ( i5 ) ：350 s -&gt; 180 s<br>Mac Mini 2012 ( i7 ) ：220 s -&gt; 130 s<br>```</p><p>基本上所有機種的 rebuild 時間都可以降低 50 %左右。</p><h3>關閉 Compile Optimization</h3><p>GCC_OPTIMIZATION_LEVEL 和 SWIFT_OPTIMIZATION_LEVEL 這兩個選項是針對 Compiler 包 Binary 時的優化，包含優化 App 容量大小與執行速度，而優化的程度愈大編譯的速度就會愈慢。一般我們在 debug 時是不需要太在意 App 容量或執行速度，所以我們針對 debug mode 把這兩個 optimization 關閉以加速編譯的速度。</p><p>修改方式：</p><p>Targets -&gt; Build Settings -&gt; Apple Clang -Code Generation -&gt; Optimization Level -&gt; Debug 改為 <strong>None [-OO]</strong></p><p>Targets -&gt; Build Settings -&gt; Swift Compiler -Code Generation -&gt; Optimization Level -&gt; Debug 改為 <strong>No Optimization [-Onone]</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tKFtu6JxuRvP1Jm4ppZ3IA.png" /></figure><h3>關閉 dSYM</h3><p>dSYM 檔記錄著 debug symbols ，可以幫助我們在用戶閃退時分析閃退報告使用。但在 debug Mode 下我們是不需要分析閃退報告的（畢竟 console 就會直接告訴你發生什麼事了…），所以跟上面一樣，我們在 Debug mode 下不需要浪費額外資源去產生 dSYM 檔。</p><p>修改方式：</p><p>Targets -&gt; Build Settings -&gt; Debug Information Format-&gt; Debug 改為 <strong>DWARF</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WTbOayP6bNq2AiXSJ-to9g.png" /></figure><h3>把 Framework 從 CocoaPods 移到 Carthage 管理</h3><p><a href="https://cocoapods.org/">CocoaPods</a> 和 <a href="https://github.com/Carthage/Carthage">Carthage</a> 都是很棒的套件管理軟體，這邊就不贅述兩者的優劣比較，只針對我們的主題 — Build time 來比較兩者的差異。</p><p>CocoaPods 是把第三方套件的原始碼從網路上抓下來，並且放到一個獨立的 xcodeproject 中管理，這樣的做法會導致如果你需要 rebuild 整個專案時，連同所有被 Cocoapods 管理的第三方套件都會一併 rebuild ，然後如果很不巧的你也用了 <a href="https://realm.io/">Realm</a> 這個套件的話，就會跟我們一樣，光等 Realm build 就要等超過 30 秒以上…</p><p>相較於 CocoaPods ，Carthage 的作法則是將原始碼抓下來後就先 compile 成 dynamic framework，然後才將 dynamic framework 放到專案裡。這代表即使需要 rebuild 整個專案，也不會需要再編譯那些透過 Carthage 管理的套件。</p><p>原先我們用 CocoaPods 管理差不多 30 個套件，後來嘗試將一些套件移至Carthage 管理後，雖然第一次的 Carthage build 要等很久，但用 dynamic framework 的好處就是以後 rebuild 再也不用等 Realm 在那邊轉半天了。</p><p>目前我們採取混合使用 Cocoapods 和 Carthage 的方式，讓 Carthage 管理龐大或已經穩定的套件，Cocoapods 則管理較輕量或變動頻繁的套件。</p><h3>結論</h3><p>這三項調整看起來很簡單，但真正困難的點在於你要找出整個 XCode Build 的過程中造成漫長等待時間的原因在哪，而且每個專案 build 很慢的原因也都不盡相同。舉例來說，假設你的專案用到的第三方套件很少，那花費大把力氣把第三方套件從 Cocoapods 移到 Carthage 就不一定對你會有幫助，更何況過多的 dynamic framework 甚至會造成 App <a href="https://www.jianshu.com/p/3263009e9228">啟動緩慢</a> 的問題。所以比起『怎麼做』，我覺得更重要的是理解『為什麼要這樣做』，然後在套用到自己的專案上時才可以仔細評估這個調整是否合適。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ccc505445dc9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ichef/xcode-build-%E8%B6%85%E5%8A%A0%E9%80%9F-ccc505445dc9">XCode Build 超加速</a> was originally published in <a href="https://medium.com/ichef">iCHEF</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>