简体   繁体   中英

How to use python to check for the latest git branch?

My goal: My project should periodically check in with my GitHub repo to see if it is currently up to date with the master branch.

What I've tried to do is to use this bash code ( https://stackoverflow.com/a/3278427/10653826 ) and implement into Python, seeing as my project is in Python. Since I only want to see if LOCAL != REMOTE (at which point it sends a notification message that it's not up to date any more), I'm using the following code:

LOCAL=subprocess.check_output(['git', 'rev-parse', '@'], stdin=None, stderr=None, shell=False, universal_newlines=False)
REMOTE=subprocess.check_output(['git', 'rev-parse', '@{u}'], stdin=None, stderr=None, shell=False, universal_newlines=False)

if (LOCAL != REMOTE) :
    #does stuff here.

I'm sure it would be a lot better to write this in python instead of using subprocess to complete it in bash. But my goal is to get it to work first at least, then worry about that stuff.

So my problem is... This works fine when I call it locally: sudo python3 ./script.py But when I set it up in a crontab -e then I get errors.

fatal: not a git repository (or any of the parent directories): .git
Traceback (most recent call last):
  File "/github/Scraper-Discord-Notification/main.py", line 24, in <module>
    discord_client = DiscordClient(discord_config)
  File "/github/Scraper-Discord-Notification/kijiji_scraper/discord_client.py", line 24, in __init__
    self.check_version(update_notification_count)
  File "/github/Scraper-Discord-Notification/kijiji_scraper/discord_client.py", line 85, in check_version
    LOCAL=subprocess.check_output(['git', 'rev-parse', '@'], stdin=None, stderr=None, shell=False, universal_newlines=False)
  File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.6/subprocess.py", line 438, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['git', 'rev-parse', '@']' returned non-zero exit status 128.
fatal: not a git repository (or any of the parent directories): .git
Traceback (most recent call last):
  File "/github/Scraper-Discord-Notification/main.py", line 24, in <module>
    discord_client = DiscordClient(discord_config)
  File "/github/Scraper-Discord-Notification/kijiji_scraper/discord_client.py", line 24, in __init__
    self.check_version(update_notification_count)
  File "/github/Scraper-Discord-Notification/kijiji_scraper/discord_client.py", line 85, in check_version
    LOCAL=subprocess.check_output(['git', 'rev-parse', '@'], stdin=None, stderr=None, shell=False, universal_newlines=False)
  File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.6/subprocess.py", line 438, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['git', 'rev-parse', '@']' returned non-zero exit status 128.
fatal: not a git repository (or any of the parent directories): .git
Traceback (most recent call last):
  File "/github/Scraper-Discord-Notification/main.py", line 24, in <module>
    discord_client = DiscordClient(discord_config)
  File "/github/Scraper-Discord-Notification/kijiji_scraper/discord_client.py", line 24, in __init__
    self.check_version(update_notification_count)
  File "/github/Scraper-Discord-Notification/kijiji_scraper/discord_client.py", line 85, in check_version
    LOCAL=subprocess.check_output(['git', 'rev-parse', '@'], stdin=None, stderr=None, shell=False, universal_newlines=False)
  File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.6/subprocess.py", line 438, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['git', 'rev-parse', '@']' returned non-zero exit status 128.

Now, my understanding of this error is that it's not being called in a directory which contains the .git. I've tried doing a number of different things, including subprocess.call(['cd', '/path/to/the/.git']) which replies with an error that looks similar to this: FileNotFoundError: [Errno 2] No such file or directory: 'cd ': 'cd '

I've tried different variations on this method, and I don't seem to be having any luck.

You can use the environment variable GIT_DIR to tell the git binary where the repository is. Then you wouldn't need to actually cd to the repo and the script will work regardless of what its working directory and current location are.

GIT_DIR = '/opt/work/repo/.git'  # path to the .git subdirectory in your repo
LOCAL = subprocess.check_output(['git', 'rev-parse', '@'], env={'GIT_DIR': GIT_DIR}, stdin=None, stderr=None,
                                shell=False, universal_newlines=False)
REMOTE = subprocess.check_output(['git', 'rev-parse', '@{u}'], env={'GIT_DIR': GIT_DIR}, stdin=None, stderr=None,
                                 shell=False, universal_newlines=False)

if (LOCAL != REMOTE):
    pass

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