Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you check for differences? #1040

Closed
jamesgeddes opened this issue Jul 21, 2020 · 2 comments
Closed

How do you check for differences? #1040

jamesgeddes opened this issue Jul 21, 2020 · 2 comments
Labels
Q&A

Comments

@jamesgeddes
Copy link

@jamesgeddes jamesgeddes commented Jul 21, 2020

Bit of a n00b question so apologies in advance.

I am trying to write a little script that,

  1. pull the repo
  2. Check if that pull included any changes
  3. If there have been changes, restart a target program

I have found several ways of checking for changes but none seem to work. Each time, I tested by making a change in my sandbox repo directly on GitHub and then executed the pull, so that the change is applied on my PC. For example,

changed_files = [item.a_path for item in repo.index.diff(None)]

Always appears to output an empty list. This seems to be what the tutorial suggests so I'm not sure why it does not work.

    current = repo.head.commit

    if current == repo.head.commit:
        print("Repo not changed. Sleep mode activated.")
        return 0
    else:
        print("Repo changed! Activated.")
        return 1

Always returns 0.

    diff = repo.git.diff(repo.head.commit.tree)
    print(diff)

When I checked the output when there was a change vs the output where there was no change, both outputs were the same.

What am I doing wrong here?

I have also found a SO question that was asked some time ago but never answered, so I'm not alone in this! Comments in that suggested git diff however I think I have tried that with the first method.

What is the best way to check if a pull has introduced changes since the last pull?

@jamesgeddes
Copy link
Author

@jamesgeddes jamesgeddes commented Jul 21, 2020

Found the problem! I was doing the pull in the wrong place.

    current = repo.head.commit

    repo.remotes.origin.pull()

    if current == repo.head.commit:
        print("Repo not changed. Sleep mode activated.")
        return 0
    else:
        print("Repo changed! Activated.")
        return 1

Not sure if this is the best way to do it, but it appears to work. Would still be grateful of any guidance.

@Byron Byron added the Q&A label Aug 12, 2020
@Byron
Copy link
Member

@Byron Byron commented Aug 12, 2020

Thanks for posting your findings here, I believe that's exactly how you would do it! Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.