2016-03-28 106 views
2

代碼非常簡單,它只是打開Windows命令提示符並執行calling()函數。它有基本的git命令,可以幫助我推送到git倉庫。我配置了ssh和遠程回購。Python Git Bash CMD腳本

鏈接:https://github.com/vivekpatani/git-script-gitter

我可以更改日期,但是當我把它與git,它會顯示在我推開,而不是一個我所犯下的當前日期。

The Commit List where it shows committed 9 days ago and 11 days ago together

提交列表中顯示位置承諾7天前和11天前,我希望它實際上顯示爲犯了同樣的日期。

def calling(): 

    #Simply opening command prompt in Windows 
    subprocess.call("git --version") 
    subprocess.call("git status") 
    subprocess.call("git add .") 
    subprocess.call("git commit -am \"Changing Things\" --date=\"Sat, 26 Mar 2016 18:46:44 -0800\"") 
    subprocess.call("git push origin master") 

    #To stop from cmd closing automatically 
    temp = input("Enter to close:") 

def main(): 
    calling() 

if __name__ == "__main__": 
    main() 

環顧四周後,我看到我需要將AUTHOR DATE和COMMIT DATE一起更改?有人可以幫我解決問題嗎?編輯1: 我正在使用Windows操作系統。

它在我通過Git Bash運行時起作用,不知何故只需將它轉換爲Python即可。

git --version 
git status 
git add . 
GIT_AUTHOR_DATE='Fri Mar 25 19:32:10 2016 -0800' GIT_COMMITTER_DATE='Fri Mar 25 19:32:10 2016 -0800' git commit -am "Hello Laney" 
git push origin master 

編輯2:解決方案

def calling(git_date): 
    subprocess.call("git --version") 
    subprocess.call("git status") 
    subprocess.call("git add .") 

    #The next statement is important as updates/adds new GitCommiterDate in environment making it the current commit date. 
    os.environ["GIT_COMMITTER_DATE"] = 'Fri Mar 25 19:32:10 2016 -0800' 

    #The date in commit command only changes author date. 
    subprocess.call("git commit -am \"Changing Things\" --date=\"Fri Mar 25 19:32:10 2016 -0800\"") 
    subprocess.call("git push origin master") 

回答

2

​​只修改作者日期。

您需要設置GIT_COMMITTER_DATE環境變量才能具有與作者日期相同的日期(using the env option of Popen()merging it with the current environment)。

subprocess.call("git commit -am \"Changing Things\" --date=\"Sat, 26 Mar 2016 18:46:44 -0800\"", env=dict(os.environ, "GIT_COMMITTER_DATE":"Sat, 26 Mar 2016 18:46:44 -0800")) 
+0

謝謝,但是當我打印os.environ,我找不到GIT_COMMITTER_DATE。此行還包含語法問題。請你能指導我嗎? –

+1

@VivekPatani的目標是*將該變量添加到環境變量中,而不是找到它。 – VonC

+0

@VivekPatani此外,缺少一個雙引號。 – VonC