git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: How can I tell if a file has been updated upstream?

From
TTTimur Tabi <timur@freescale.com>
Date
Feb 26, 2010, 00:18 UTC
Message-ID
<ed82fe3e1002251618g750764bm82b0653770554dcf@mail.gmail.com>
In-Reply-To
<ed82fe3e1002050823gec57827j184c9c4cff4f4a45@mail.gmail.com>
On Fri, Feb 5, 2010 at 10:23 AM, Timur Tabi <timur@freescale.com> wrote:
> Is there a way for me to tell if a particular file in my repository
> has an update in the upstream repository?
Thanks to everyone who replied.  Here's what I came up with:
def check_for_updates():
    # Get the path to our script.  This should be a git repository.
    script_path = os.path.dirname(os.path.realpath(__file__))
    # Determine the upstream URL
    p = subprocess.Popen(['git', 'config', '-f', script_path + '/.git/config',
        '--get', 'remote.origin.url'],
        shell=False, stdout=subprocess.PIPE, stderr=open(os.devnull, 'w'))
    url = p.communicate()[0].strip()
    if not url:
        # The script is not running from a git repository
        return
    # Determine the HEAD of the upstream repository
    p = subprocess.Popen(['git', 'ls-remote', url, 'HEAD'],
        shell=False, stdout=subprocess.PIPE, stderr=open(os.devnull, 'w'))
    sha = p.communicate()[0].split()
    if not sha:
        # Something is wrong with the upstream repository
        return
    sha = sha[0] # The SHA of the upstream head
    # Check if the remote SHA is in the local repository.
    # git --git-dir=/home/b04825/bin/.git show-ref HEAD
    p = subprocess.Popen(['git', '--git-dir=' + script_path + '/.git',
        'log', '-1', sha],
        shell=False, stdout=subprocess.PIPE, stderr=open(os.devnull, 'w'))
    if not p.communicate()[0]:
        # If we can't find the SHA locally, then it means that the local
        # repository is out of date.  We pretend that it means that this script
        # is out of date.
        print 'There is an update for this script available.  Please
pull from the remote'
        print 'repository (e.g. "cd %s; git pull")' % script_path
-- 
Timur Tabi
Linux kernel developer at Freescale
Previous: Timur Tabi
Message 7 of 7 in “How can I tell if a file has been updated upstream?”
  1. Timur TabiFeb 5, 2010
  2. Shawn O. PearceFeb 5, 2010
  3. Timur TabiFeb 5, 2010
  4. Nicolas PitreFeb 5, 2010
  5. Junio C HamanoFeb 5, 2010
  6. Timur TabiFeb 5, 2010
  7. Timur TabiFeb 26, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.