简体   繁体   中英

Python Git Bash CMD Script

The code is pretty straightforward, it just opens windows command prompt and executes the calling() function. It has basic git commands which help me push to a git repo. I have configured ssh and remote repo.

Link: https://github.com/vivekpatani/git-script-gitter

I can change the date, but when I push it to git, it displays the current date on which I pushed rather than the one I committed.

The Commit List where it shows committed 9 days ago and 11 days ago, I want it to actually show the same date as committed.

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()

After looking around I read that I need to change the AUTHOR DATE and COMMIT DATE together? Can somebody please help me out.

EDIT 1: I'm working on Windows OS.

It works when I run it through Git Bash, somehow just need to convert that to 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

EDIT 2: Solution

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")

--date only modifies the author date.

You need to set GIT_COMMITTER_DATE environment variable in order to have the same date as author date ( using the env option of Popen() , and 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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM