2016-11-21 26 views
0

我試圖使用GitPython進行一些回購操作,但遇到了與我的應用程序的問題,與處理打開我wouldn'沒想到。GitPython`repo.index.commit()`產生持久的git.exe實例,擁有處理回購

Bug-jarring的問題,似乎呼籲repo.index.commit()結果處理目錄(大概是在.git\)。之後,這會導致我的應用嘗試執行的其他失敗。

這裏是一個工作單元測試:

import unittest 
import git 
import tempfile 
import os.path 

class Test(unittest.TestCase): 

    def testCreateRepo(self): 
     with tempfile.TemporaryDirectory(prefix=(__loader__.name) + "_") as mydir: 

      # MAKE NEW REPO 
      repo = git.Repo.init(path=os.path.join(mydir, "newRepo"), mkdir=True) 
      self.assertTrue(os.path.isdir(os.path.join(repo.working_dir, ".git")), "Failed to make new repo?") 

      # MAKE FILE, COMMIT REPO 
      testFileName = "testFile.txt" 
      open(os.path.join(repo.working_dir, testFileName) , "w").close() 
      repo.index.add([testFileName]) 
      self.assertTrue(repo.is_dirty()) 

      #### 
      # COMMENTING THIS OUT --> TEST PASSES 
      repo.index.commit("added initial test file") 
      self.assertFalse(repo.is_dirty()) 
      #### 

      # adding this does not affect the handle 
      git.cmd.Git.clear_cache() 


      print("done") # exception thrown right after this, on __exit__ 

PermissionError:[WinError 32]的方法,因爲它被另一個過程不能訪問該文件:C:\用戶\%USER%\應用程序數據\本地的\ Temp \ EXAMPLE_gitpython_v3kbrly_ \ newRepo」

挖得更深一些,似乎gitPython滋生git.exe進程的多個實例,和他們每個人持有的句柄回購newRepo文件夾。

  • 立即設置斷點的錯誤之前,使用Sysinternals的/手柄,看看打開的句柄newRepo ... git.exe使用的Sysinternals/procexp我(git.exe的4個獨立PID的要準確)
  • 可以看到它們都是從eclipse衍生出來的 - > python

單步執行,這是對repo.index.commit()的調用,它實際上會導致生成git.exe。

+0

這似乎沒有得到任何地方,這似乎是不好的行爲,所以我提交了一個bug:https://github.com/gitpython-developers/GitPython/issues/553 – mike

回答

0

與gitpython開發者的工作,我找到了答案:

由於gitpython的內部緩存行爲,必須強制進行垃圾回收,並告訴回購,以清除它的緩存。我在做後者,但在錯誤的對象上。

以下必須清理您的目錄之前添加(__exit__()荷蘭國際集團我with: /上下文經理條款,在上面的代碼)

import gc 
gc.collect() 
repo.git.clear_cache() 

這些似乎並沒有遵守最低驚喜:)希望api可以在未來得到改進。