2016-03-28 74 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