顯示具有 dulwich 標籤的文章。 顯示所有文章
顯示具有 dulwich 標籤的文章。 顯示所有文章

2013年7月18日 星期四

[git][dulwich]在 windows 才會有的權限認定問題

前一陣子試著使用 dulwich 來操作 .git 裡面的資料,例如上傳到 changeset 到 remote、下載 changeset 到 local。看起來都很正常。但是,把檔案從 .git 裡面拿出來放,就會判定某些檔案被改變,我去檢查內容,自己使用 sha,md5 對兩個檔案檢查,結果顯示兩個檔案內容完全一樣。我想,dulwich 這麼多人用,難道我又遇到「沒人會遇到」的問題嗎?

另外我使用 tortoisegit 來檢查其內容,一樣也是說相同,但是用 dulwich 拉回來的 .git,跟 tortoisegit 拉回來的 .git 對於相同檔案,卻是一個說相同,一個說有改變。這可搞死我了,難道要放棄使用 dulwich 嗎?

後來翻到一個舊的 tortoisegit 的 issue:https://code.google.com/p/tortoisegit/issues/detail?id=812

Allow to change git file permission mask / attributes (especially executable bit)

才知道,dulwich 可能也是中同一個招數。git 是有檢查 Read, Write, Execute vs User, Group, Other 的。於是就可以解釋,為何只要是 script 或是 linux 的執行檔才會出現內容相同,但被判定有改變。在 windows 的檔案權限是 666,跟原來的不一樣了,也沒辦法改成 755。不曉得 tortoisegit 怎麼解決這問題的…。我這裡暫時停止研究了。

2013年6月6日 星期四

[python]dulwich 只是個 git client 我想…

https://github.com/jelmer/dulwich

安裝…找不到說明,BUT!

出奇的簡單,用 git 從 github 下載回來,使用 setup.py install 就搞定。

 

操作…

操作的文件在這 http://www.samba.org/~jelmer/dulwich/docs/tutorial/index.html,真的是RTFM,不注意還找不到文件在哪裡。我太嫩了。

已經知道細節的人,可以直接跳 http://www.samba.org/~jelmer/dulwich/docs/tutorial/repo.html 開始!但是,不知道細節的人,一定要從頭到尾看清楚每個字,尤其是 Introduction,它把這個軟體的功能說的很清楚。就是「它有寫的就是它能做的」。我原先以為他能像 git command line 工具一樣操作,結果不是。文章中,介紹了 .git 裡面的三個物件,commit, tree, blob,dulwich 軟體主要就是讓你操作這三個物件來達成你想要的動作。於是經過了嘗試、失敗、掙扎、又嘗試別的程式,失敗,掙扎,又回來嘗試這程式…的循環,我得到了我想要的結果。

我這裡整理一下我知道的跟我不知道的對應表。
















git init myrepo
from dulwich.repo import Repo
repo = Repo.init('myrepo',mkdri=True)
git --bare init不知道該怎辦。因為我只是把 dulwich 當 client 端,應該不會用到這個指令。
git add foo.txtrepo.stage(["foo.txt"])
git commitcommit_id = repo.do_commit("The first commit", committer="your name <name@example.com>")
git clone說明:這個大家最常用來抓新專案的指令,沒辦法直接對應,要先使用 fetch,再自行把檔案內容從 stores 裡面抓出來,寫到本地端的檔案系統。
例:

repo = Repo("myrepo")
commit_sha = repo['HEAD'].id  # 先拿到最近的 commit sha
tree = repo[commit_sha].tree    # 再拿到那個 commit 的 tree
store = repo.object_store
for filename, mode, sha in store.iter_tree_contents(tree): # 列舉那個 tree 的全部內容
    relpath = unicode(os.path.join(repo.path,filename))    # 計算出本地端的相對路徑
    ensure_dir_exists(os.path.dirname(relpath))               # 確定目錄有建立
    with open(relpath,'wb') as f:                                    # 開啟 bin mode 寫入內容
        f.write(repo[sha].data) 
    repo.stage([filename,])
    os.chmod(relpath, mode)


這是我不小心在亂晃的時候發現的 https://lists.launchpad.net/dulwich-users/msg00116.html 藏得很隱密

git fetch目前到 github 我都是用 https:// 的,所以就試了一個用 https:// 的。git:// 的我沒試。
server_address = 'https://github.com/'
client = HttpGitClient(server_address)
local = Repo.init("local", mkdir=True)
remote_refs = client.fetch("/jelmer/dulwich.git", local, determine_wants=local.object_store.determine_wants_all, progress=sys.stdout.write)
local["HEAD"] = remote_refs["HEAD"] #這個很重要,不然會沒有HEAD!